-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_test.go
106 lines (99 loc) · 1.99 KB
/
convert_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
package hdur
import (
"math"
"testing"
)
func TestDuration_Conversion(t *testing.T) {
tests := []struct {
name string
d Duration
}{
{
name: "simple duration",
d: Duration{Days: 1, Hours: 2, Minutes: 30},
},
{
name: "complex duration",
d: Duration{Years: 1, Months: 2, Days: 3, Hours: 4},
},
{
name: "negative duration",
d: Duration{Days: -1, Hours: -12},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
std := tt.d.ToStandard()
got := FromStandard(std)
// Compare using nanoseconds since direct comparison might fail due to month/year approximation
if got.InNanoseconds() != tt.d.InNanoseconds() {
t.Errorf("ToStandard/FromStandard roundtrip failed: got %v, want %v", got, tt.d)
}
})
}
}
func TestDurationConversions(t *testing.T) {
tests := []struct {
name string
d Duration
unit string
expected float64
delta float64
}{
{
name: "hours simple",
d: Hours(2),
unit: "hours",
expected: 2,
delta: 0.001,
},
{
name: "minutes simple",
d: Minutes(120),
unit: "minutes",
expected: 120,
delta: 0.001,
},
{
name: "seconds simple",
d: Seconds(3600),
unit: "seconds",
expected: 3600,
delta: 0.001,
},
{
name: "months simple",
d: Months(2),
unit: "months",
expected: 2,
delta: 0.001,
},
{
name: "years simple",
d: Years(1),
unit: "years",
expected: 1,
delta: 0.001,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var got float64
switch tt.unit {
case "hours":
got = tt.d.InHours()
case "minutes":
got = tt.d.InMinutes()
case "seconds":
got = tt.d.InSeconds()
case "months":
got = tt.d.InMonths()
case "years":
got = tt.d.InYears()
}
if math.Abs(got-tt.expected) > tt.delta {
t.Errorf("got %v, want %v (±%v)", got, tt.expected, tt.delta)
}
})
}
}