-
Notifications
You must be signed in to change notification settings - Fork 1
/
jtime_test.go
64 lines (55 loc) · 1.28 KB
/
jtime_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
package jalali
import (
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestMonthDayToYear(t *testing.T) {
for y := 0; y < 366; y++ {
m, d, err := dayToMonth(y)
require.NoError(t, err)
y2, err := monthDayToYear(m, d)
require.NoError(t, err)
assert.Equal(t, y, y2)
}
}
func TestWeekDay(t *testing.T) {
base := time.Time{}
for i := -1000; i < 1000; i++ {
day := base.Add(time.Duration(i*24) * time.Hour)
since := int(day.Unix() / secInDay)
wd := dayToWeekday(since)
require.Equal(t, day.Weekday(), wd)
}
}
func TestDayToJTime(t *testing.T) {
// This test is not reliable. it depends on the same code and the leap year
// function which can be wrong
month := jEpocMonth
day := jEpocDay
for i := 0; i < 100; i++ {
if IsLeap(i + jEpocYear - 1) {
day--
if day <= 0 {
month--
day = jMonthLen[month-1]
}
}
y, m, d, err := dayToJTime(i * 365)
require.NoError(t, err)
assert.Equal(t, jEpocYear+i, y)
assert.Equal(t, month, m, fmt.Sprintf("%d - %d - %d", i, month, d))
assert.Equal(t, day, d)
}
current := jEpocDiff
for i := 0; i < 300; i++ {
if IsLeap(jEpocYear - i + 1) {
current++
}
y, d := dayToYear(-i * 365)
assert.Equal(t, jEpocYear-i, y)
assert.Equal(t, current, d)
}
}