Skip to content

Commit e98b93d

Browse files
mergify[bot]github-actions[bot]Copilot
authored
libbeat/common: stabilize TestRound assertions (#50872) (#50879)
Use table-driven subtests and assert.InDelta in TestRound to avoid exact float equality comparisons on midpoint rounding cases that can vary by tiny representation differences across platforms. Assisted-By: GitHub Copilot CLI (cherry picked from commit 0291d3b) Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 95f416c commit e98b93d

1 file changed

Lines changed: 41 additions & 6 deletions

File tree

libbeat/common/math_test.go

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,46 @@ import (
2424
)
2525

2626
func TestRound(t *testing.T) {
27-
assert.EqualValues(t, 0.5, Round(0.5, DefaultDecimalPlacesCount))
28-
assert.EqualValues(t, 0.5, Round(0.50004, DefaultDecimalPlacesCount))
29-
assert.EqualValues(t, 0.5001, Round(0.50005, DefaultDecimalPlacesCount))
27+
testCases := []struct {
28+
name string
29+
input float64
30+
expected float64
31+
}{
32+
{
33+
name: "keep exact half",
34+
input: 0.5,
35+
expected: 0.5,
36+
},
37+
{
38+
name: "truncate below midpoint",
39+
input: 0.50004,
40+
expected: 0.5,
41+
},
42+
{
43+
name: "round up at midpoint",
44+
input: 0.50005,
45+
expected: 0.5001,
46+
},
47+
{
48+
name: "keep exact integer plus half",
49+
input: 1234.5,
50+
expected: 1234.5,
51+
},
52+
{
53+
name: "truncate larger number below midpoint",
54+
input: 1234.50004,
55+
expected: 1234.5,
56+
},
57+
{
58+
name: "round up larger number at midpoint",
59+
input: 1234.50005,
60+
expected: 1234.5001,
61+
},
62+
}
3063

31-
assert.EqualValues(t, 1234.5, Round(1234.5, DefaultDecimalPlacesCount))
32-
assert.EqualValues(t, 1234.5, Round(1234.50004, DefaultDecimalPlacesCount))
33-
assert.EqualValues(t, 1234.5001, Round(1234.50005, DefaultDecimalPlacesCount))
64+
for _, tc := range testCases {
65+
t.Run(tc.name, func(t *testing.T) {
66+
assert.InDelta(t, tc.expected, Round(tc.input, DefaultDecimalPlacesCount), 1e-10, "rounding %v should be %v", tc.input, tc.expected)
67+
})
68+
}
3469
}

0 commit comments

Comments
 (0)