forked from bombsimon/jtd-infer-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inferred_number_test.go
68 lines (60 loc) · 1.99 KB
/
inferred_number_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package jtdinfer
import (
"math"
"testing"
jtd "github.com/jsontypedef/json-typedef-go"
"github.com/stretchr/testify/assert"
)
func TestInferredNumberDefault(t *testing.T) {
nn1 := NewNumber()
nn2 := NewNumber().Infer(0.0).Infer(float64(math.MaxUint8))
nn3 := NewNumber().Infer(float64(math.MinInt8)).Infer(float64(math.MaxInt8))
nn4 := NewNumber().Infer(0.5)
cases := []struct {
inferredNumber *InferredNumber
numType NumType
jtdType jtd.Type
}{
// Defaults are honored.
{nn1, NumTypeUint8, jtd.TypeUint8},
{nn1, NumTypeInt8, jtd.TypeInt8},
{nn1, NumTypeUint16, jtd.TypeUint16},
{nn1, NumTypeInt16, jtd.TypeInt16},
{nn1, NumTypeUint32, jtd.TypeUint32},
{nn1, NumTypeInt32, jtd.TypeInt32},
{nn1, NumTypeFloat32, jtd.TypeFloat32},
{nn1, NumTypeFloat64, jtd.TypeFloat64},
// Expand to limits of uint8.
{nn2, NumTypeUint8, jtd.TypeUint8},
{nn2, NumTypeInt8, jtd.TypeUint8},
{nn2, NumTypeUint16, jtd.TypeUint16},
{nn2, NumTypeInt16, jtd.TypeInt16},
{nn2, NumTypeUint32, jtd.TypeUint32},
{nn2, NumTypeInt32, jtd.TypeInt32},
{nn2, NumTypeFloat32, jtd.TypeFloat32},
{nn2, NumTypeFloat64, jtd.TypeFloat64},
// Expand to limits of int8.
{nn3, NumTypeUint8, jtd.TypeInt8},
{nn3, NumTypeInt8, jtd.TypeInt8},
{nn3, NumTypeUint16, jtd.TypeInt8},
{nn3, NumTypeInt16, jtd.TypeInt16},
{nn3, NumTypeUint32, jtd.TypeInt8},
{nn3, NumTypeInt32, jtd.TypeInt32},
{nn3, NumTypeFloat32, jtd.TypeFloat32},
{nn3, NumTypeFloat64, jtd.TypeFloat64},
// Test including a non-integer.
{nn4, NumTypeUint8, jtd.TypeFloat64},
{nn4, NumTypeInt8, jtd.TypeFloat64},
{nn4, NumTypeUint16, jtd.TypeFloat64},
{nn4, NumTypeInt16, jtd.TypeFloat64},
{nn4, NumTypeUint32, jtd.TypeFloat64},
{nn4, NumTypeInt32, jtd.TypeFloat64},
{nn4, NumTypeFloat32, jtd.TypeFloat32},
{nn4, NumTypeFloat64, jtd.TypeFloat64},
}
for _, tc := range cases {
t.Run(string(tc.jtdType), func(t *testing.T) {
assert.Equal(t, tc.jtdType, tc.inferredNumber.IntoType(tc.numType))
})
}
}