github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/utils/unittest/math_test.go (about) 1 package unittest_test 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/require" 7 8 "github.com/onflow/flow-go/utils/unittest" 9 ) 10 11 func TestAreNumericallyClose(t *testing.T) { 12 tests := []struct { 13 name string 14 a float64 15 b float64 16 epsilon float64 17 expected bool 18 }{ 19 {"close enough under epsilon", 1.0, 1.1, 0.1, true}, 20 {"not close under epsilon", 1.0, 1.1, 0.01, false}, 21 {"equal values", 2.0, 2.0, 0.1, true}, 22 {"zero epsilon with equal values", 2.0, 2.0, 0.0, true}, 23 {"zero epsilon with different values", 2.0, 2.1, 0.0, false}, 24 {"first value zero", 0, 0.1, 0.1, true}, 25 {"both values zero", 0, 0, 0.1, true}, 26 {"negative values close enough", -1.0, -1.1, 0.1, true}, 27 {"negative values not close enough", -1.0, -1.2, 0.1, false}, 28 } 29 30 for _, tt := range tests { 31 t.Run(tt.name, func(t *testing.T) { 32 actual := unittest.AreNumericallyClose(tt.a, tt.b, tt.epsilon) 33 require.Equal(t, tt.expected, actual, "test Failed: %s", tt.name) 34 }) 35 } 36 }