-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse_test.go
253 lines (234 loc) · 7.47 KB
/
parse_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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// SPDX-FileCopyrightText: 2009 The Go Authors.
// SPDX-FileCopyrightText: 2023 Weston Schmidt <[email protected]>
// SPDX-License-Identifier: BSD-3-Clause
package approx_test
import (
"errors"
"math"
"math/rand"
"reflect"
"strings"
"testing"
"time"
"github.com/goschtalt/approx"
)
func TestParseDuration_Mine(t *testing.T) {
unknownErr := errors.New("unknown")
tests := []struct {
in string
expect time.Duration
err error
}{
{
in: "1.012h",
expect: time.Hour + 43*time.Second + 200*time.Millisecond,
}, {
in: "1y0w0d0h0m0s",
expect: approx.Year,
}, {
in: "1y0w0d0h0m0s",
expect: 52*approx.Week + approx.Day,
}, {
in: "4y0w0d0h0m0s",
expect: 4 * approx.Year,
}, {
in: "4y3w2d1h0m0s",
expect: 4*approx.Year + 3*approx.Week + 2*approx.Day + 1*time.Hour,
}, {
in: "3w0d0h0m0s",
expect: 3 * approx.Week,
}, {
in: "3w0d0h0m1s",
expect: 3*approx.Week + time.Second,
}, {
in: "-3w0d0h0m1s",
expect: -1 * (3*approx.Week + time.Second),
}, {
in: "1h0m0s",
expect: 60 * time.Minute,
},
}
for _, tc := range tests {
t.Run(tc.in, func(t *testing.T) {
got, err := approx.ParseDuration(tc.in)
if tc.err != nil {
if errors.Is(tc.err, unknownErr) {
if err == nil {
t.Logf("expected and error but it was nil")
t.Fail()
}
return
}
if !errors.Is(err, tc.err) {
t.Logf("expected error %s but got %s", reflect.TypeOf(tc.err), reflect.TypeOf(err))
t.Fail()
}
return
}
if tc.expect != got {
t.Logf("expected: %q got: %q\n", tc.expect, got)
t.Fail()
}
})
}
}
// -- the following tests are from golang stl ----------------------------------
// https://github.com/golang/go/blob/go1.20.5/src/time/time_test.go
var durationTests = []struct {
str string
d time.Duration
}{
{"0s", 0},
{"1ns", 1 * time.Nanosecond},
{"1.1µs", 1100 * time.Nanosecond},
{"2.2ms", 2200 * time.Microsecond},
{"3.3s", 3300 * time.Millisecond},
{"4m5s", 4*time.Minute + 5*time.Second},
{"4m5.001s", 4*time.Minute + 5001*time.Millisecond},
{"5h6m7.001s", 5*time.Hour + 6*time.Minute + 7001*time.Millisecond},
{"8m0.000000001s", 8*time.Minute + 1*time.Nanosecond},
{"2562047h47m16.854775807s", 1<<63 - 1},
{"-2562047h47m16.854775808s", -1 << 63},
}
func TestDurationString(t *testing.T) {
for _, tt := range durationTests {
if str := tt.d.String(); str != tt.str {
t.Errorf("Duration(%d).String() = %s, want %s", int64(tt.d), str, tt.str)
}
if tt.d > 0 {
if str := (-tt.d).String(); str != "-"+tt.str {
t.Errorf("Duration(%d).String() = %s, want %s", int64(-tt.d), str, "-"+tt.str)
}
}
}
}
var parseDurationTests = []struct {
in string
want time.Duration
}{
// simple
{"0", 0},
{"5s", 5 * time.Second},
{"30s", 30 * time.Second},
{"1478s", 1478 * time.Second},
// sign
{"-5s", -5 * time.Second},
{"+5s", 5 * time.Second},
{"-0", 0},
{"+0", 0},
// decimal
{"5.0s", 5 * time.Second},
{"5.6s", 5*time.Second + 600*time.Millisecond},
{"5.s", 5 * time.Second},
{".5s", 500 * time.Millisecond},
{"1.0s", 1 * time.Second},
{"1.00s", 1 * time.Second},
{"1.004s", 1*time.Second + 4*time.Millisecond},
{"1.0040s", 1*time.Second + 4*time.Millisecond},
{"100.00100s", 100*time.Second + 1*time.Millisecond},
// different units
{"10ns", 10 * time.Nanosecond},
{"11us", 11 * time.Microsecond},
{"12µs", 12 * time.Microsecond}, // U+00B5
{"12μs", 12 * time.Microsecond}, // U+03BC
{"13ms", 13 * time.Millisecond},
{"14s", 14 * time.Second},
{"15m", 15 * time.Minute},
{"16h", 16 * time.Hour},
// composite durations
{"3h30m", 3*time.Hour + 30*time.Minute},
{"10.5s4m", 4*time.Minute + 10*time.Second + 500*time.Millisecond},
{"-2m3.4s", -(2*time.Minute + 3*time.Second + 400*time.Millisecond)},
{"1h2m3s4ms5us6ns", 1*time.Hour + 2*time.Minute + 3*time.Second + 4*time.Millisecond + 5*time.Microsecond + 6*time.Nanosecond},
{"39h9m14.425s", 39*time.Hour + 9*time.Minute + 14*time.Second + 425*time.Millisecond},
// large value
{"52763797000ns", 52763797000 * time.Nanosecond},
// more than 9 digits after decimal point, see https://golang.org/issue/6617
{"0.3333333333333333333h", 20 * time.Minute},
// 9007199254740993 = 1<<53+1 cannot be stored precisely in a float64
{"9007199254740993ns", (1<<53 + 1) * time.Nanosecond},
// largest duration that can be represented by int64 in nanoseconds
{"9223372036854775807ns", (1<<63 - 1) * time.Nanosecond},
{"9223372036854775.807us", (1<<63 - 1) * time.Nanosecond},
{"9223372036s854ms775us807ns", (1<<63 - 1) * time.Nanosecond},
{"-9223372036854775808ns", -1 << 63 * time.Nanosecond},
{"-9223372036854775.808us", -1 << 63 * time.Nanosecond},
{"-9223372036s854ms775us808ns", -1 << 63 * time.Nanosecond},
// largest negative value
{"-9223372036854775808ns", -1 << 63 * time.Nanosecond},
// largest negative round trip value, see https://golang.org/issue/48629
{"-2562047h47m16.854775808s", -1 << 63 * time.Nanosecond},
// huge string; issue 15011.
{"0.100000000000000000000h", 6 * time.Minute},
// This value tests the first overflow check in leadingFraction.
{"0.830103483285477580700h", 49*time.Minute + 48*time.Second + 372539827*time.Nanosecond},
}
func TestParseDuration(t *testing.T) {
for _, tc := range parseDurationTests {
d, err := approx.ParseDuration(tc.in)
if err != nil || d != tc.want {
t.Errorf("ParseDuration(%q) = %v, %v, want %v, nil", tc.in, d, err, tc.want)
}
}
}
var parseDurationErrorTests = []struct {
in string
expect string
}{
// invalid
{"", `""`},
{"3", `"3"`},
{"-", `"-"`},
{"s", `"s"`},
{".", `"."`},
{"-.", `"-."`},
{".s", `".s"`},
{"+.s", `"+.s"`},
// {"1d", `"1d"`}, WTS This is valid now
{"\x85\x85", `"\x85\x85"`},
{"\xffff", `"\xffff"`},
{"hello \xffff world", `"hello \xffff world"`},
{"\uFFFD", `"\xef\xbf\xbd"`}, // utf8.RuneError
{"\uFFFD hello \uFFFD world", `"\xef\xbf\xbd hello \xef\xbf\xbd world"`}, // utf8.RuneError
// overflow
{"9223372036854775810ns", `"9223372036854775810ns"`},
{"9223372036854775808ns", `"9223372036854775808ns"`},
{"-9223372036854775809ns", `"-9223372036854775809ns"`},
{"9223372036854776us", `"9223372036854776us"`},
{"3000000h", `"3000000h"`},
{"9223372036854775.808us", `"9223372036854775.808us"`},
{"9223372036854ms775us808ns", `"9223372036854ms775us808ns"`},
}
func TestParseDurationErrors(t *testing.T) {
for _, tc := range parseDurationErrorTests {
_, err := approx.ParseDuration(tc.in)
if err == nil {
t.Errorf("ParseDuration(%q) = _, nil, want _, non-nil", tc.in)
} else if !strings.Contains(err.Error(), tc.expect) {
t.Errorf("ParseDuration(%q) = _, %q, error does not contain %q", tc.in, err, tc.expect)
}
}
}
func TestParseDurationRoundTrip(t *testing.T) {
// https://golang.org/issue/48629
max0 := time.Duration(math.MaxInt64)
max1, err := approx.ParseDuration(max0.String())
if err != nil || max0 != max1 {
t.Errorf("round-trip failed: %d => %q => %d, %v", max0, max0.String(), max1, err)
}
min0 := time.Duration(math.MinInt64)
min1, err := approx.ParseDuration(min0.String())
if err != nil || min0 != min1 {
t.Errorf("round-trip failed: %d => %q => %d, %v", min0, min0.String(), min1, err)
}
for i := 0; i < 100; i++ {
// Resolutions finer than milliseconds will result in
// imprecise round-trips.
d0 := time.Duration(rand.Int31()) * time.Millisecond
s := d0.String()
d1, err := approx.ParseDuration(s)
if err != nil || d0 != d1 {
t.Errorf("round-trip failed: %d => %q => %d, %v", d0, s, d1, err)
}
}
}