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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
package speed
import (
"math"
"testing"
)
// only tests that work on 32 bit architectures or both go here
// tests only working on 64 bit architectures go in _amd64_test.go
func TestIsCompatible(t *testing.T) {
cases := []struct {
t MetricType
v interface{}
result bool
}{
{Int32Type, -1, true},
{Int64Type, -1, true},
{Uint64Type, -1, false},
{Uint32Type, -1, false},
{Int32Type, math.MinInt32, true},
{Int64Type, math.MinInt32, true},
{Uint32Type, math.MinInt32, false},
{Uint64Type, math.MinInt32, false},
{Int32Type, int32(math.MinInt32), true},
{Int64Type, int64(math.MinInt32), true},
{Uint32Type, int32(math.MinInt32), false},
{Uint64Type, int64(math.MinInt32), false},
{Int32Type, math.MaxInt32, true},
{Int64Type, math.MaxInt32, true},
{Uint32Type, math.MaxInt32, true},
{Uint64Type, math.MaxInt32, true},
{Int32Type, int32(math.MaxInt32), true},
{Int64Type, int64(math.MaxInt32), true},
{Uint32Type, int32(math.MaxInt32), false},
{Uint64Type, int64(math.MaxInt32), false},
{Int64Type, int64(math.MaxInt64), true},
{Uint32Type, uint32(math.MaxUint32), true},
{Uint64Type, uint64(math.MaxUint32), true},
{Uint32Type, uint(math.MaxUint32), true},
{Uint32Type, uint32(math.MaxUint32), true},
{Uint64Type, uint64(math.MaxUint64), true},
{FloatType, math.MaxFloat32, true},
{DoubleType, math.MaxFloat32, true},
{FloatType, -math.MaxFloat32, true},
{DoubleType, -math.MaxFloat32, true},
{FloatType, float32(math.MaxFloat32), true},
{DoubleType, float32(math.MaxFloat32), false},
{FloatType, float32(-math.MaxFloat32), true},
{DoubleType, float32(-math.MaxFloat32), false},
{FloatType, float64(math.MaxFloat32), true},
{DoubleType, float64(math.MaxFloat32), true},
{FloatType, float64(-math.MaxFloat32), true},
{DoubleType, float64(-math.MaxFloat32), true},
{StringType, 10, false},
{StringType, 10.10, false},
{StringType, "10", true},
}
for _, c := range cases {
r := c.t.IsCompatible(c.v)
if r != c.result {
t.Errorf("%v.IsCompatible(%v(%T)) should be %v, not %v", c.t, c.v, c.v, c.result, r)
}
}
}
func TestResolve(t *testing.T) {
cases := []struct {
t MetricType
val, resval interface{}
}{
{Int32Type, 10, int32(10)},
{Int64Type, 10, int64(10)},
{Uint32Type, 10, uint32(10)},
{Uint64Type, 10, uint64(10)},
{Int32Type, int32(10), int32(10)},
{Int64Type, int64(10), int64(10)},
{Uint32Type, uint32(10), uint32(10)},
{Uint64Type, uint64(10), uint64(10)},
{Uint32Type, uint(10), uint32(10)},
{Uint64Type, uint(10), uint64(10)},
{Uint32Type, uint32(10), uint32(10)},
{Uint64Type, uint64(10), uint64(10)},
{FloatType, 3.14, float32(3.14)},
{DoubleType, 3.14, float64(3.14)},
{FloatType, float32(3.14), float32(3.14)},
{DoubleType, float64(3.14), float64(3.14)},
}
for _, c := range cases {
if c.t.resolve(c.val) != c.resval {
t.Errorf("expected %T to resolve to %T", c.val, c.resval)
}
}
}
func TestComposition(t *testing.T) {
ms := MegabyteUnit.Time(SecondUnit, -1)
if ms.String() != "MegabyteUnit^1SecondUnit^-1" {
t.Errorf("expected ms.String() to be MegabyteUnit^1SecondUnit^-1, got %s", ms.String())
}
if ms.PMAPI() != 520237056 {
t.Errorf("expected ms.PMAPI() to be 520237056, got %v", ms.PMAPI())
}
hz := NewMetricUnit().Time(SecondUnit, -1)
if hz.String() != "SecondUnit^-1" {
t.Errorf("expected hz.String() to be SecondUnit^-1, got %s", ms.String())
}
if hz.PMAPI() != 251670528 {
t.Errorf("expected hz.PMAPI() to be 251670528, got %v", hz.PMAPI())
}
cs1 := OneUnit.Space(MegabyteUnit, 2).Time(SecondUnit, -2)
cs2 := NewMetricUnit().Time(SecondUnit, -2).Space(MegabyteUnit, 2).Count(OneUnit, 1)
if cs1.PMAPI() != cs2.PMAPI() {
t.Errorf("expected %v to be equal to %v", cs1.PMAPI(), cs2.PMAPI())
}
if cs1.String() != cs2.String() {
t.Errorf("expected %v to be equal to %v", cs1.String(), cs2.String())
}
}
|