github.com/cosmos/cosmos-proto@v1.0.0-beta.3/support/timepb/cmp_test.go (about) 1 package timepb 2 3 import ( 4 "math" 5 "testing" 6 "time" 7 8 "github.com/stretchr/testify/require" 9 durpb "google.golang.org/protobuf/types/known/durationpb" 10 tspb "google.golang.org/protobuf/types/known/timestamppb" 11 "pgregory.net/rapid" 12 ) 13 14 func new(s int64, n int32) *tspb.Timestamp { 15 return &tspb.Timestamp{Seconds: s, Nanos: n} 16 } 17 18 func TestIsZero(t *testing.T) { 19 tcs := []struct { 20 t *tspb.Timestamp 21 expected bool 22 }{ 23 {nil, true}, 24 25 {&tspb.Timestamp{}, false}, 26 {new(0, 0), false}, 27 {new(1, 0), false}, 28 {new(0, 1), false}, 29 {tspb.New(time.Time{}), false}, 30 } 31 32 for i, tc := range tcs { 33 require.Equal(t, tc.expected, IsZero(tc.t), "test_id %d", i) 34 } 35 } 36 37 func TestCompare(t *testing.T) { 38 tcs := []struct { 39 t1 *tspb.Timestamp 40 t2 *tspb.Timestamp 41 expected int 42 }{ 43 {&tspb.Timestamp{}, &tspb.Timestamp{}, 0}, 44 {new(1, 1), new(1, 1), 0}, 45 {new(-1, 1), new(-1, 1), 0}, 46 {new(231, -5), new(231, -5), 0}, 47 48 {new(1, -1), new(1, 0), -1}, 49 {new(1, -1), new(12, -1), -1}, 50 {new(-11, -1), new(-1, -1), -1}, 51 52 {new(1, -1), new(0, -1), 1}, 53 {new(1, -1), new(1, -2), 1}, 54 } 55 for i, tc := range tcs { 56 r := Compare(tc.t1, tc.t2) 57 require.Equal(t, tc.expected, r, "test %d", i) 58 } 59 60 // test panics 61 tcs2 := []struct { 62 t1 *tspb.Timestamp 63 t2 *tspb.Timestamp 64 }{ 65 {nil, new(1, 1)}, 66 {new(1, 1), nil}, 67 {nil, nil}, 68 } 69 for i, tc := range tcs2 { 70 require.Panics(t, func() { 71 Compare(tc.t1, tc.t2) 72 }, "test-panics %d", i) 73 } 74 } 75 76 func TestAddFuzzy(t *testing.T) { 77 check := func(t require.TestingT, s, n int64, d time.Duration) { 78 t_in := time.Unix(s, n) 79 t_expected := tspb.New(t_in.Add(d)) 80 tb := tspb.New(t_in) 81 tbPb := Add(tb, durpb.New(d)) 82 tbStd := AddStd(tb, d) 83 require.Equal(t, *t_expected, *tbStd, "checking pb add") 84 require.Equal(t, *t_expected, *tbPb, "checking stdlib add") 85 } 86 gen := rapid.Int64Range(0, 1<<62) 87 genNano := rapid.Int64Range(0, 1e9-1) 88 rInt := func(t *rapid.T, label string) int64 { return gen.Draw(t, label) } 89 90 rapid.Check(t, func(t *rapid.T) { 91 s, n, d := rInt(t, "sec"), genNano.Draw(t, "nanos"), time.Duration(rInt(t, "dur")) 92 check(t, s, n, d) 93 }) 94 95 check(t, 0, 0, 0) 96 check(t, 1, 2, 0) 97 check(t, -1, -1, 1) 98 99 require.Nil(t, Add(nil, &durpb.Duration{Seconds: 1}), "Pb works with nil values") 100 require.Nil(t, AddStd(nil, time.Second), "Std works with nil values") 101 } 102 103 func TestAddOverflow(t *testing.T) { 104 require := require.New(t) 105 tb := tspb.Timestamp{ 106 Seconds: math.MaxInt64, 107 Nanos: 1000, 108 } 109 require.Panics(func() { 110 AddStd(&tb, time.Second) 111 }, "AddStd should panic on overflow") 112 113 require.Panics(func() { 114 Add(&tb, &durpb.Duration{Nanos: second - 1}) 115 }, "Add should panic on overflow") 116 117 // should panic on underflow 118 119 tb = tspb.Timestamp{ 120 Seconds: -math.MaxInt64 - 1, 121 Nanos: -1000, 122 } 123 require.True(tb.Seconds < 0, "sanity check") 124 require.Panics(func() { 125 tt := AddStd(&tb, -time.Second) 126 t.Log(tt) 127 }, "AddStd should panic on underflow") 128 129 require.Panics(func() { 130 tt := Add(&tb, &durpb.Duration{Nanos: -second + 1}) 131 t.Log(tt) 132 }, "Add should panic on underflow") 133 134 }