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 package types
33
34 import (
35 "math"
36 "testing"
37 "time"
38
39 "github.com/gogo/protobuf/proto"
40 )
41
42 const (
43 minGoSeconds = math.MinInt64 / int64(1e9)
44 maxGoSeconds = math.MaxInt64 / int64(1e9)
45 )
46
47 var durationTests = []struct {
48 proto *Duration
49 isValid bool
50 inRange bool
51 dur time.Duration
52 }{
53
54 {&Duration{Seconds: 0, Nanos: 0}, true, true, 0},
55
56 {&Duration{Seconds: 100, Nanos: 0}, true, true, 100 * time.Second},
57 {&Duration{Seconds: -100, Nanos: 0}, true, true, -100 * time.Second},
58 {&Duration{Seconds: 100, Nanos: 987}, true, true, 100*time.Second + 987},
59 {&Duration{Seconds: -100, Nanos: -987}, true, true, -(100*time.Second + 987)},
60
61 {&Duration{Seconds: maxGoSeconds, Nanos: int32(math.MaxInt64 - 1e9*maxGoSeconds)}, true, true, math.MaxInt64},
62
63 {&Duration{Seconds: minGoSeconds, Nanos: int32(math.MinInt64 - 1e9*minGoSeconds)}, true, true, math.MinInt64},
64 {nil, false, false, 0},
65 {&Duration{Seconds: -100, Nanos: 987}, false, false, 0},
66 {&Duration{Seconds: 100, Nanos: -987}, false, false, 0},
67 {&Duration{Seconds: math.MinInt64, Nanos: 0}, false, false, 0},
68 {&Duration{Seconds: math.MaxInt64, Nanos: 0}, false, false, 0},
69
70 {&Duration{Seconds: maxSeconds, Nanos: 1e9 - 1}, true, false, 0},
71
72 {&Duration{Seconds: minSeconds, Nanos: -(1e9 - 1)}, true, false, 0},
73
74 {&Duration{Seconds: maxSeconds + 1, Nanos: 0}, false, false, 0},
75
76 {&Duration{Seconds: minSeconds - 1, Nanos: -(1e9 - 1)}, false, false, 0},
77
78 {&Duration{Seconds: maxGoSeconds, Nanos: int32(math.MaxInt64-1e9*maxGoSeconds) + 1}, true, false, 0},
79
80 {&Duration{Seconds: minGoSeconds, Nanos: int32(math.MinInt64-1e9*minGoSeconds) - 1}, true, false, 0},
81
82 {&Duration{Seconds: maxGoSeconds + 1, Nanos: int32(math.MaxInt64 - 1e9*maxGoSeconds)}, true, false, 0},
83
84 {&Duration{Seconds: minGoSeconds - 1, Nanos: int32(math.MinInt64 - 1e9*minGoSeconds)}, true, false, 0},
85 }
86
87 func TestValidateDuration(t *testing.T) {
88 for _, test := range durationTests {
89 err := validateDuration(test.proto)
90 gotValid := (err == nil)
91 if gotValid != test.isValid {
92 t.Errorf("validateDuration(%v) = %t, want %t", test.proto, gotValid, test.isValid)
93 }
94 }
95 }
96
97 func TestDurationFromProto(t *testing.T) {
98 for _, test := range durationTests {
99 got, err := DurationFromProto(test.proto)
100 gotOK := (err == nil)
101 wantOK := test.isValid && test.inRange
102 if gotOK != wantOK {
103 t.Errorf("DurationFromProto(%v) ok = %t, want %t", test.proto, gotOK, wantOK)
104 }
105 if err == nil && got != test.dur {
106 t.Errorf("DurationFromProto(%v) = %v, want %v", test.proto, got, test.dur)
107 }
108 }
109 }
110
111 func TestDurationProto(t *testing.T) {
112 for _, test := range durationTests {
113 if test.isValid && test.inRange {
114 got := DurationProto(test.dur)
115 if !proto.Equal(got, test.proto) {
116 t.Errorf("DurationProto(%v) = %v, want %v", test.dur, got, test.proto)
117 }
118 }
119 }
120 }
121
View as plain text