-
Notifications
You must be signed in to change notification settings - Fork 8
/
unit_parse_hours_test.go
102 lines (81 loc) · 2.43 KB
/
unit_parse_hours_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
package main
import (
"testing"
)
const FLOATING_POINT_THRESHOLD = 0.000001
func TestParseOlderThanToHoursInvalidFormat(t *testing.T) {
t.Parallel()
_, err := parseOlderThanToHours("not-a-valid-format")
if err == nil {
t.Fatal("Expected to get an error when parsing an invalid format, but got nil")
}
}
func TestParseOlderThanToHoursNegativeHours(t *testing.T) {
t.Parallel()
_, err := parseOlderThanToHours("-15h")
if err == nil {
t.Fatal("Expected to get an error when parsing a negative value, but got nil")
}
}
func TestParseOlderThanToHoursZeroHours(t *testing.T) {
t.Parallel()
testParseOlderThan("0h", 0, t)
}
func TestParseOlderThanToHoursOneHour(t *testing.T) {
t.Parallel()
testParseOlderThan("1h", 1, t)
}
func TestParseOlderThanToHoursTenHours(t *testing.T) {
t.Parallel()
testParseOlderThan("10h", 10, t)
}
func TestParseOlderThanToHoursNineHundredNinetyNineHours(t *testing.T) {
t.Parallel()
testParseOlderThan("999h", 999, t)
}
func TestParseOlderThanToHoursZeroMinutes(t *testing.T) {
t.Parallel()
testParseOlderThan("0m", 0, t)
}
func TestParseOlderThanToHoursOneMinute(t *testing.T) {
t.Parallel()
testParseOlderThan("1m", 0.01666666666667, t)
}
func TestParseOlderThanToHoursTenMinutes(t *testing.T) {
t.Parallel()
testParseOlderThan("10m", 0.16666666666667, t)
}
func TestParseOlderThanToHoursSixtyMinutes(t *testing.T) {
t.Parallel()
testParseOlderThan("60m", 1, t)
}
func TestParseOlderThanToHoursNineHundredNinetyNineMinutes(t *testing.T) {
t.Parallel()
testParseOlderThan("999m", 16.65, t)
}
func TestParseOlderThanToHoursZeroDays(t *testing.T) {
t.Parallel()
testParseOlderThan("0d", 0, t)
}
func TestParseOlderThanToHoursOneDay(t *testing.T) {
t.Parallel()
testParseOlderThan("1d", 24, t)
}
func TestParseOlderThanToHoursTenDays(t *testing.T) {
t.Parallel()
testParseOlderThan("10d", 240, t)
}
func TestParseOlderThanToHoursNineHundredNinetyNineDays(t *testing.T) {
t.Parallel()
testParseOlderThan("999d", 23976, t)
}
func testParseOlderThan(timeFormat string, expectedHours float64, t *testing.T) {
hours, err := parseOlderThanToHours(timeFormat)
if err != nil {
t.Fatalf("Unexpected error parsing a valid time format '%s': %s", timeFormat, err.Error())
}
diff := expectedHours - hours
if diff > FLOATING_POINT_THRESHOLD {
t.Fatalf("Expected %9f but got %9f. The difference %9f is greater than the floating point threshold %9f.", expectedHours, hours, diff, FLOATING_POINT_THRESHOLD)
}
}