Skip to content

Commit

Permalink
More tests of float compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
jhawthorn committed Aug 29, 2024
1 parent 0fdd97a commit d6d428d
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions test/test_encoder_compatibility.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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" })
Expand Down Expand Up @@ -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

0 comments on commit d6d428d

Please sign in to comment.