Skip to content

Commit c0855a1

Browse files
committed
metrics: tests for resolve and more for isCompatible
1 parent e194cc4 commit c0855a1

File tree

2 files changed

+112
-10
lines changed

2 files changed

+112
-10
lines changed

metrics_amd64_test.go

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,63 @@ func TestIsCompatible64(t *testing.T) {
1111
v interface{}
1212
result bool
1313
}{
14+
{Int32Type, math.MaxInt32 + 1, false},
15+
{Int64Type, math.MaxInt32 + 1, true},
16+
{Uint32Type, math.MaxInt32 + 1, true},
17+
{Uint64Type, math.MaxInt32 + 1, true},
18+
1419
{Int32Type, math.MinInt32 - 1, false},
20+
{Int64Type, math.MinInt32 - 1, true},
21+
{Uint32Type, math.MinInt32 - 1, false},
22+
{Uint64Type, math.MinInt32 - 1, false},
23+
1524
{Int32Type, math.MinInt64, false},
16-
{Uint32Type, math.MaxUint32 + 1, false},
25+
{Int64Type, math.MinInt64, true},
26+
{Uint32Type, math.MinInt64, false},
27+
{Uint64Type, math.MinInt64, false},
28+
29+
{Int32Type, math.MaxInt64, false},
30+
{Int64Type, math.MaxInt64, true},
1731
{Uint32Type, math.MaxInt64, false},
32+
{Uint64Type, math.MaxInt64, true},
33+
34+
{Uint32Type, math.MaxUint32 + 1, false},
35+
{Uint64Type, math.MaxUint32 + 1, true},
36+
37+
{Uint32Type, uint(math.MaxUint32 + 1), false},
38+
{Uint64Type, uint(math.MaxUint32 + 1), true},
39+
40+
{Uint64Type, uint(math.MaxUint64), true},
41+
42+
{Uint64Type, uint64(math.MaxUint64), true},
43+
44+
{FloatType, math.MaxFloat32, true},
45+
{FloatType, -math.MaxFloat32, true},
46+
{DoubleType, math.MaxFloat32, true},
47+
{DoubleType, -math.MaxFloat32, true},
48+
49+
// we cannot test for `math.MaxFloat32 + 1`
50+
// or even `math.MaxFloat32 + math.MaxUint64`
51+
// because in floating point those aren't significant
52+
// enough additions, so the comparison will be equal
53+
// for more http://stackoverflow.com/q/17588419/3673043
54+
55+
{FloatType, math.MaxFloat32 * 2, false},
56+
{DoubleType, math.MaxFloat32 * 2, true},
57+
58+
{FloatType, math.MaxFloat64, false},
59+
{FloatType, -math.MaxFloat64, false},
60+
61+
{DoubleType, math.MaxFloat64, true},
62+
{DoubleType, -math.MaxFloat64, true},
1863
}
1964

20-
for _, c := range cases {
65+
for i, c := range cases {
2166
r := c.t.IsCompatible(c.v)
2267
if r != c.result {
23-
t.Errorf("%v.IsCompatible(%v(%T)) should be %v, not %v", c.t, c.v, c.v, c.result, r)
68+
f := math.MaxFloat32
69+
println((f + 1) > f)
70+
t.Errorf("case %v: %v.IsCompatible(%v(%T)) should be %v, not %v", i+1, c.t, c.v, c.v, c.result, r)
2471
}
2572
}
2673
}

metrics_test.go

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"testing"
66
)
77

8+
// only tests that work on 32 bit architectures or both go here
9+
// tests only working on 64 bit architectures go in _amd64_test.go
810
func TestIsCompatible(t *testing.T) {
911
cases := []struct {
1012
t MetricType
@@ -15,16 +17,34 @@ func TestIsCompatible(t *testing.T) {
1517
{Int64Type, -1, true},
1618
{Uint64Type, -1, false},
1719
{Uint32Type, -1, false},
18-
{Int32Type, 2147483648, false},
19-
{Int64Type, 2147483648, true},
20-
{Int32Type, int32(-2147483648), true},
21-
{Int64Type, int64(-2147483648), true},
22-
{Uint32Type, int32(-2147483648), false},
23-
{Uint64Type, int64(-2147483648), false},
20+
21+
{Int32Type, math.MinInt32, true},
22+
{Int64Type, math.MinInt32, true},
23+
{Uint32Type, math.MinInt32, false},
24+
{Uint64Type, math.MinInt32, false},
25+
26+
{Int32Type, int32(math.MinInt32), true},
27+
{Int64Type, int64(math.MinInt32), true},
28+
{Uint32Type, int32(math.MinInt32), false},
29+
{Uint64Type, int64(math.MinInt32), false},
30+
31+
{Int32Type, math.MaxInt32, true},
32+
{Int64Type, math.MaxInt32, true},
33+
{Uint32Type, math.MaxInt32, true},
34+
{Uint64Type, math.MaxInt32, true},
35+
36+
{Int32Type, int32(math.MaxInt32), true},
37+
{Int64Type, int64(math.MaxInt32), true},
38+
{Uint32Type, int32(math.MaxInt32), false},
39+
{Uint64Type, int64(math.MaxInt32), false},
40+
41+
{Int64Type, int64(math.MaxInt64), true},
42+
2443
{Uint32Type, uint32(math.MaxUint32), true},
2544
{Uint64Type, uint64(math.MaxUint32), true},
45+
2646
{Uint32Type, uint(math.MaxUint32), true},
27-
{Uint64Type, uint(math.MaxUint64), true},
47+
2848
{Uint32Type, math.MaxUint32, true},
2949
{Uint64Type, uint64(math.MaxUint64), true},
3050
}
@@ -36,3 +56,38 @@ func TestIsCompatible(t *testing.T) {
3656
}
3757
}
3858
}
59+
60+
func TestResolve(t *testing.T) {
61+
cases := []struct {
62+
t MetricType
63+
val, resval interface{}
64+
}{
65+
{Int32Type, 10, int32(10)},
66+
{Int64Type, 10, int64(10)},
67+
{Uint32Type, 10, uint32(10)},
68+
{Uint64Type, 10, uint64(10)},
69+
70+
{Int32Type, int32(10), int32(10)},
71+
{Int64Type, int64(10), int64(10)},
72+
{Uint32Type, uint32(10), uint32(10)},
73+
{Uint64Type, uint64(10), uint64(10)},
74+
75+
{Uint32Type, uint(10), uint32(10)},
76+
{Uint64Type, uint(10), uint64(10)},
77+
78+
{Uint32Type, uint32(10), uint32(10)},
79+
{Uint64Type, uint64(10), uint64(10)},
80+
81+
{FloatType, 3.14, float32(3.14)},
82+
{DoubleType, 3.14, float64(3.14)},
83+
84+
{FloatType, float32(3.14), float32(3.14)},
85+
{DoubleType, float64(3.14), float64(3.14)},
86+
}
87+
88+
for _, c := range cases {
89+
if c.t.resolve(c.val) != c.resval {
90+
t.Errorf("expected %T to resolve to %T", c.val, c.resval)
91+
}
92+
}
93+
}

0 commit comments

Comments
 (0)