diff --git a/test/test_encoder_compatibility.rb b/test/test_encoder_compatibility.rb index 8ffe6d3..a15553a 100644 --- a/test/test_encoder_compatibility.rb +++ b/test/test_encoder_compatibility.rb @@ -50,6 +50,7 @@ def test_encode_float 0.upto(1023) do |e| assert_compat(2.0 ** e) + assert_compat(2.0 ** -e) end end @@ -61,6 +62,24 @@ def test_encode_randomized_floats end end + def test_float_scientific_threshold + assert_implementations_equal do |json| + (1.0..).bsearch{ |x| json.dump(x).include?("e") } + end + + assert_implementations_equal do |json| + (1.0..).bsearch{ |x| json.dump(-x).include?("e") } + end + + assert_implementations_equal do |json| + (1.0..).bsearch{ |x| json.dump(1.0 / x).include?("e") } + end + + assert_implementations_equal do |json| + (1.0..).bsearch{ |x| json.dump(-1.0 / x).include?("e") } + end + end + def test_encode_hash assert_compat({}) assert_compat({ "foo" => "bar" }) @@ -187,10 +206,20 @@ def assert_compat(object) end def assert_dump_equal(object, *args) - assert_equal ::JSON.dump(object, *args), RapidJSON::JSONGem.dump(object, *args) + assert_implementations_equal do |json| + json.dump(object, *args) + end end def assert_generate_equal(object, *args) - assert_equal ::JSON.generate(object, *args), RapidJSON::JSONGem.generate(object, *args) + assert_implementations_equal do |json| + json.generate(object, *args) + end + end + + def assert_implementations_equal(&block) + expected = yield(::JSON) + actual = yield(RapidJSON::JSONGem) + assert_equal expected, actual end end