Skip to content

Commit c228ab9

Browse files
committed
Deal with approximation on tests
1 parent 19e15d4 commit c228ab9

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

tests_and_examples/run_query_tests.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"io/ioutil"
88
"log"
99
"os"
10+
"math"
1011
"os/signal"
1112
"strings"
1213
"time"
@@ -312,6 +313,18 @@ func (s *SlicingDiceTester) compareJSONArray(expected []interface{}, got []inter
312313
return true
313314
}
314315

316+
var floatType = reflect.TypeOf(float64(0))
317+
318+
func (s *SlicingDiceTester) getFloat(unk interface{}) (float64, error) {
319+
v := reflect.ValueOf(unk)
320+
v = reflect.Indirect(v)
321+
if !v.Type().ConvertibleTo(floatType) {
322+
return 0, fmt.Errorf("cannot convert %v to float64", v.Type())
323+
}
324+
fv := v.Convert(floatType)
325+
return fv.Float(), nil
326+
}
327+
315328
func (s *SlicingDiceTester) compareJSONValue(expected interface{}, got interface{}) bool {
316329
if reflect.ValueOf(expected).Kind() == reflect.Map {
317330
expectedMap := expected.(map[string]interface{})
@@ -325,20 +338,25 @@ func (s *SlicingDiceTester) compareJSONValue(expected interface{}, got interface
325338
expected_type := reflect.TypeOf(expected)
326339
expected_kind := expected_type.Kind()
327340
if expected_kind == reflect.Int && s.isJsonNumber(got) {
328-
fmt.Print(got)
329341
f, _ := got.(json.Number).Int64()
330342
return expected == f
331343
} else if expected_kind == reflect.Float64 && s.isJsonNumber(got) {
332-
fmt.Print(expected)
333-
fmt.Print(got)
334344
f, _ := got.(json.Number).Float64()
335-
return expected == f
345+
return s.floatIsClose(expected, f)
346+
} else if expected_kind == reflect.Float64 {
347+
return s.floatIsClose(expected, got)
336348
} else {
337349
return reflect.DeepEqual(expected, got)
338350
}
339351
}
340352
}
341353

354+
func (s *SlicingDiceTester) floatIsClose(a interface{}, b interface{}) bool {
355+
a_float, _ := s.getFloat(a)
356+
b_float, _ := s.getFloat(b)
357+
return math.Abs(a_float - b_float) <= math.Max(1e-09 * math.Max(math.Abs(a_float), math.Abs(b_float)), 0.0)
358+
}
359+
342360
func (s *SlicingDiceTester) isJsonNumber(to_test interface{}) bool {
343361
switch to_test.(type) {
344362
case json.Number:

0 commit comments

Comments
 (0)