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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
|
package rfc3339
import (
"encoding/json"
"fmt"
"reflect"
"testing"
)
func TestIsLeapYear(t *testing.T) {
tests := []struct {
val uint16
exp bool
} {
{ 1800, false },
{ 1900, false },
{ 1996, true },
{ 1997, false },
{ 1998, false },
{ 1999, false },
{ 2000, true },
{ 2001, false },
{ 2002, false },
{ 2003, false },
{ 2004, true },
{ 2005, false },
{ 2006, false },
{ 2007, false },
{ 2008, true },
{ 2009, false },
{ 2010, false },
{ 2011, false },
{ 2012, true },
{ 2013, false },
{ 2014, false },
{ 2015, false },
{ 2016, true },
{ 2017, false },
{ 2018, false },
{ 2019, false },
{ 2020, true },
{ 2021, false },
{ 2022, false },
{ 2023, false },
{ 2024, true },
{ 2100, false },
{ 2400, true },
}
for _, test := range(tests) {
t.Run(fmt.Sprintf("%d", test.val), func(t *testing.T) {
got := isLeapYear(test.val)
if got != test.exp {
t.Errorf("got %v, exp %v", got, test.exp)
}
})
}
}
func TestGetMaxMonthDay(t *testing.T) {
tests := []struct {
y uint16
m uint16
exp uint16
} {
{ 2000, 2, 29 }, // test leap year
{ 2001, 2, 28 }, // test non-leap year
{ 2022, 1, 31 }, // test full non-leap year
{ 2022, 2, 28 },
{ 2022, 3, 31 },
{ 2022, 4, 30 },
{ 2022, 5, 31 },
{ 2022, 6, 30 },
{ 2022, 7, 31 },
{ 2022, 8, 31 },
{ 2022, 9, 30 },
{ 2022, 10, 31 },
{ 2022, 11, 30 },
{ 2022, 12, 31 },
}
for _, test := range(tests) {
t.Run(fmt.Sprintf("%04d-%02d", test.y, test.m), func(t *testing.T) {
got := getMaxMonthDay(test.y, test.m)
if got != test.exp {
t.Errorf("got %v, exp %v", got, test.exp)
}
})
}
}
func TestParseDateComponent(t *testing.T) {
tests := []struct {
name string
val string
min uint16
max uint16
exp bool
} {
{ "bad-uint", "abcd", 2000, 2001, false },
{ "y-pass", "2000", 2000, 2001, true },
{ "y-fail-lo", "1999", 2000, 2001, false },
{ "y-fail-hi", "2002", 2000, 2001, false },
{ "m-pass-1", "1", 1, 12, true },
{ "m-pass-2", "2", 1, 12, true },
{ "m-pass-3", "3", 1, 12, true },
{ "m-pass-4", "4", 1, 12, true },
{ "m-pass-5", "5", 1, 12, true },
{ "m-pass-6", "6", 1, 12, true },
{ "m-pass-7", "7", 1, 12, true },
{ "m-pass-8", "8", 1, 12, true },
{ "m-pass-9", "9", 1, 12, true },
{ "m-pass-10", "10", 1, 12, true },
{ "m-pass-11", "11", 1, 12, true },
{ "m-pass-12", "12", 1, 12, true },
{ "m-fail-lo", "0", 1, 12, false },
{ "m-fail-hi", "13", 1, 12, false },
{ "d-pass-1", "1", 1, 31, true },
{ "d-pass-10", "10", 1, 31, true },
{ "d-pass-30", "10", 1, 31, true },
{ "d-fail-lo", "0", 1, 31, false },
{ "d-fail-hi", "32", 1, 31, false },
}
for _, test := range(tests) {
t.Run(test.name, func(t *testing.T) {
got, err := parseDateComponent("a", []byte(test.val), test.min, test.max)
if err != nil && test.exp == true {
t.Error(err)
} else if err == nil && test.exp == false {
t.Errorf("got %v, exp error", got)
}
})
}
}
func TestNewDate(t *testing.T) {
tests := []struct {
name string
val string
exp bool
} {
{ "pass", "2000-01-03", true },
{ "pass-lo", "1999-01-01", true },
{ "pass-hi", "2126-12-31", true },
{ "fail", "asdf", false },
{ "fail-y-lo", "1998-01-03", false },
{ "fail-y-hi", "2128-01-03", false },
{ "fail-m-lo", "2126-00-03", false },
{ "fail-m-hi", "2126-13-03", false },
{ "fail-d-lo", "2126-01-00", false },
{ "fail-d-hi", "2126-01-32", false },
{ "fail-d-hi", "2126-02-29", false },
{ "fail-d-hi", "2126-03-32", false },
{ "fail-d-hi", "2126-04-31", false },
{ "fail-d-hi", "2126-05-32", false },
{ "fail-d-hi", "2126-06-31", false },
}
for _, test := range(tests) {
t.Run(test.name, func(t *testing.T) {
if got, err := NewDate([]byte(test.val)); err != nil && test.exp {
t.Error(err)
} else if err == nil && !test.exp {
t.Errorf("got %v, exp error", got)
}
})
}
}
func TestGetComponents(t *testing.T) {
type date struct {
y uint16
m uint16
d uint16
}
tests := []struct {
val string
exp date
} {
{ "2022-10-31", date { 2022, 10, 31 } },
}
for _, test := range(tests) {
t.Run(test.val, func(t *testing.T) {
// create date
dt, err := NewDate([]byte(test.val))
if err != nil {
t.Error(err)
return
}
// get components
y, m, d := dt.GetComponents()
got := date { y, m, d }
// check components
if !reflect.DeepEqual(got, test.exp) {
t.Errorf("got %v, exp %v", got, test.exp)
}
})
}
}
func TestYear(t *testing.T) {
tests := []struct {
val string
exp uint16
} {
{ "2022-10-31", 2022 },
}
for _, test := range(tests) {
t.Run(test.val, func(t *testing.T) {
// create date
dt, err := NewDate([]byte(test.val))
if err != nil {
t.Error(err)
return
}
// get year
got := dt.Year()
// check components
if got != test.exp {
t.Errorf("got year %d, exp %d", got, test.exp)
}
})
}
}
func TestMonth(t *testing.T) {
tests := []struct {
val string
exp uint16
} {
{ "2022-10-31", 10 },
}
for _, test := range(tests) {
t.Run(test.val, func(t *testing.T) {
// create date
dt, err := NewDate([]byte(test.val))
if err != nil {
t.Error(err)
return
}
// get month
got := dt.Month()
// check components
if got != test.exp {
t.Errorf("got month %d, exp %d", got, test.exp)
}
})
}
}
func TestDay(t *testing.T) {
tests := []struct {
val string
exp uint16
} {
{ "2022-10-31", 31 },
}
for _, test := range(tests) {
t.Run(test.val, func(t *testing.T) {
// create date
dt, err := NewDate([]byte(test.val))
if err != nil {
t.Error(err)
return
}
// get day
got := dt.Day()
// check components
if got != test.exp {
t.Errorf("got day %d, exp %d", got, test.exp)
}
})
}
}
func TestString(t *testing.T) {
tests := []string {
"2022-10-31",
}
for _, test := range(tests) {
t.Run(test, func(t *testing.T) {
// create date
dt, err := NewDate([]byte(test))
if err != nil {
t.Error(err)
return
}
// get/check string
got := dt.String()
if got != test {
t.Errorf("got %s, exp %s", got, test)
}
})
}
}
func TestDateUnmarshalJSON(t *testing.T) {
passTests := []struct {
val string
exp string
} {
{ `"2022-02-03"`, "2022-02-03" },
}
for _, test := range(passTests) {
var got Date
if err := json.Unmarshal([]byte(test.val), &got); err != nil {
t.Error(err)
} else if got.String() != test.exp {
t.Errorf("got \"%s\", exp \"%s\"", got.String(), test.exp)
}
}
failTests := []struct {
name string
val string
} {
{ "fail-str", "asdf" },
{ "fail-parse", "\"asdf\"" },
}
for _, test := range(failTests) {
t.Run(test.name, func(t *testing.T) {
var got Date
if err := got.UnmarshalJSON([]byte(test.val)); err == nil {
t.Errorf("got \"%v\" exp error", got)
}
})
}
}
func TestDateMarshalJSON(t *testing.T) {
tests := []struct {
val string
exp string
} {
{ "2022-10-31", `"2022-10-31"` },
}
for _, test := range(tests) {
t.Run(test.val, func(t *testing.T) {
// create date
dt, err := NewDate([]byte(test.val))
if err != nil {
t.Error(err)
return
}
// get/check string
if got, err := dt.MarshalJSON(); err != nil {
t.Error(err)
} else if string(got) != test.exp {
t.Errorf("got \"%s\", exp \"%s\"", got, test.exp)
}
})
}
}
|