Repository navigation
fix: support non-finite floats in complex results #574
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cathleeny
wants to merge
1
commit into
main
Choose a base branch
from
fix/nested-nonfinite-floats
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,7 +33,6 @@ func TestMarshalMatchesEncodingJSON(t *testing.T) { | |
| math.MaxFloat64, math.SmallestNonzeroFloat64, -math.MaxFloat64, | ||
| float32(0), float32(math.Copysign(0, -1)), float32(3.14), float32(0.1), float32(1e21), float32(1e-6), float32(1e-7), | ||
| float32(math.MaxFloat32), float32(math.SmallestNonzeroFloat32), | ||
| math.NaN(), math.Inf(1), math.Inf(-1), float32(math.NaN()), float32(math.Inf(-1)), | ||
| "", "alpha", "with space", "a\"b", `back\slash`, "<tag>", "a&b", "tab\there", "nl\n", "\x00", "\x7f", | ||
| "héllo", "日本語", " ", "\xff\xfe", "emoji 😀", | ||
| []byte("abc"), []byte(nil), | ||
|
|
@@ -89,13 +88,59 @@ func TestMarshalRandomUnicodeStrings(t *testing.T) { | |
| func TestMarshalRandomFloats(t *testing.T) { | ||
| r := rand.New(rand.NewSource(1)) //nolint:gosec // G404: deterministic test inputs, not security | ||
| for i := 0; i < 200000; i++ { | ||
| assertParity(t, math.Float64frombits(r.Uint64())) | ||
| assertParity(t, math.Float32frombits(r.Uint32())) | ||
| f64 := math.Float64frombits(r.Uint64()) | ||
| if !math.IsNaN(f64) && !math.IsInf(f64, 0) { | ||
| assertParity(t, f64) | ||
| } | ||
| f32 := math.Float32frombits(r.Uint32()) | ||
| if !math.IsNaN(float64(f32)) && !math.IsInf(float64(f32), 0) { | ||
| assertParity(t, f32) | ||
| } | ||
| assertParity(t, r.NormFloat64()*math.Pow(10, float64(r.Intn(50)-25))) | ||
| assertParity(t, float32(r.NormFloat64()*math.Pow(10, float64(r.Intn(30)-15)))) | ||
| } | ||
| } | ||
|
|
||
| func TestAppendNonFiniteFloats(t *testing.T) { | ||
| cases := []struct { | ||
| name string | ||
| value any | ||
| want string | ||
| }{ | ||
| {"float32_nan", float32(math.NaN()), `"NaN"`}, | ||
| {"float32_infinity", float32(math.Inf(1)), `"Infinity"`}, | ||
| {"float32_negative_infinity", float32(math.Inf(-1)), `"-Infinity"`}, | ||
| {"float64_nan", math.NaN(), `"NaN"`}, | ||
| {"float64_infinity", math.Inf(1), `"Infinity"`}, | ||
| {"float64_negative_infinity", math.Inf(-1), `"-Infinity"`}, | ||
| } | ||
| for _, tc := range cases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| for _, prefix := range []string{"", "[1.5,"} { | ||
| got, err := Append([]byte(prefix), tc.value) | ||
| if err != nil || string(got) != prefix+tc.want { | ||
| t.Fatalf("Append(%q, %v) = %q, %v; want %q", prefix, tc.value, got, err, prefix+tc.want) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestMapKeyPreservesEncoding(t *testing.T) { | ||
| values := []any{nil, true, int32(7), int64(-9), "NaN", "+Inf", "key", float32(1.5), | ||
| float32(math.Copysign(0, -1)), 1.5, math.Copysign(0, -1), math.MaxFloat64} | ||
| for _, value := range values { | ||
| want, err := json.Marshal(value) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| got, err := Append(nil, MapKey(value)) | ||
| if err != nil || string(got) != string(want) { | ||
| t.Errorf("MapKey(%v): got %q, %v; want %s", value, got, err, want) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestAppendKeepsPrefix(t *testing.T) { | ||
| got, err := Append([]byte("["), int32(7)) | ||
| if err != nil || string(got) != "[7" { | ||
|
|
@@ -112,6 +157,7 @@ func TestAppendKeepsPrefix(t *testing.T) { | |
| func TestAppendFastPathDoesNotAllocate(t *testing.T) { | ||
| buf := make([]byte, 0, 64) | ||
| for _, v := range []any{int64(-9000), int32(42), uint64(7), 3.14, 0.30000000000000004, 1e-7, float32(0.5), true, | ||
| math.NaN(), math.Inf(1), math.Inf(-1), float32(math.NaN()), float32(math.Inf(1)), float32(math.Inf(-1)), | ||
| "alpha", "café 日本語 😀"} { | ||
| if n := testing.AllocsPerRun(100, func() { buf, _ = Append(buf[:0], v) }); n != 0 { | ||
| t.Errorf("Append(%#v) allocated %v times per run, want 0", v, n) | ||
|
|
@@ -121,7 +167,7 @@ func TestAppendFastPathDoesNotAllocate(t *testing.T) { | |
|
|
||
| // jsonfmt appends into a reused buffer, as both renderers do. | ||
| func BenchmarkAppendScalars(b *testing.B) { | ||
| // The last three take json.Marshal's fallback. | ||
| // The escaped string and []byte take json.Marshal's fallback. | ||
| values := []any{int32(42), int64(-9000), 3.14, 0.30000000000000004, float32(0.5), true, "alpha0", "café crème", | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. math.NaN() is still in BenchmarkAppendScalars. It's now a fast-path append for jsonfmt but an error path for the encoding_json baseline, so the comparison is skewed. Could we drop it, or add a comment that the baseline errors on it? |
||
| `a"b`, []byte("abc"), math.NaN()} | ||
| b.Run("jsonfmt", func(b *testing.B) { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we remove MapKey and use "NaN", "Infinity", and "-Infinity" consistently for both keys and values? Currently, a map containing positive infinity as both key and value renders as {"+Inf":"Infinity"}.
JDBC’s text rendering and Node’s object keys use Infinity/-Infinity; Python preserves native numeric keys. The +Inf/-Inf spelling comes from the kernel’s old formatting fallback, while Thrift previously errored on these keys. Using the same spelling throughout would simplify the contract; we can document the kernel key spelling change explicitly in the changelog.