-
Notifications
You must be signed in to change notification settings - Fork 7
/
tipsettimestamp_test.go
90 lines (74 loc) · 2.26 KB
/
tipsettimestamp_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
package f3
import (
"testing"
"time"
"github.com/filecoin-project/go-f3/gpbft"
"github.com/stretchr/testify/assert"
)
type tipset struct {
genesis time.Time
period time.Duration
epoch int64
}
func (ts *tipset) String() string {
panic("not implemented")
}
func (ts *tipset) Key() gpbft.TipSetKey {
panic("not implemented")
}
func (ts *tipset) Beacon() []byte {
panic("not implemented")
}
func (ts *tipset) Epoch() int64 {
return ts.epoch
}
func (ts *tipset) Timestamp() time.Time {
return ts.genesis.Add(time.Duration(ts.epoch) * ts.period)
}
func tipsetGenerator(genesis time.Time, period time.Duration) func(epoch int64) *tipset {
return func(epoch int64) *tipset {
return &tipset{
genesis: genesis,
period: period,
epoch: epoch,
}
}
}
func TestComputeTipsetTimestampAtEpoch(t *testing.T) {
genesis := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC)
period := 30 * time.Second
generateTipset := tipsetGenerator(genesis, period)
tipset := generateTipset(10)
t.Run("Basic Functionality", func(t *testing.T) {
targetEpoch := int64(15)
expected := generateTipset(targetEpoch).Timestamp()
actual := computeTipsetTimestampAtEpoch(tipset, targetEpoch, period)
assert.Equal(t, expected, actual)
})
t.Run("Zero Epoch", func(t *testing.T) {
targetEpoch := int64(0)
expected := generateTipset(targetEpoch).Timestamp()
actual := computeTipsetTimestampAtEpoch(tipset, targetEpoch, period)
assert.Equal(t, expected, actual)
})
t.Run("Large Epoch", func(t *testing.T) {
largeEpoch := int64(1e6)
expected := generateTipset(largeEpoch).Timestamp()
actual := computeTipsetTimestampAtEpoch(tipset, largeEpoch, period)
assert.Equal(t, expected, actual)
})
t.Run("Boundary Condition", func(t *testing.T) {
boundaryEpoch := int64(1e3)
expected := generateTipset(boundaryEpoch).Timestamp()
actual := computeTipsetTimestampAtEpoch(tipset, boundaryEpoch, period)
assert.Equal(t, expected, actual)
})
t.Run("Consistency", func(t *testing.T) {
targetEpoch := int64(20)
expected := generateTipset(targetEpoch).Timestamp()
actual1 := computeTipsetTimestampAtEpoch(tipset, targetEpoch, period)
actual2 := computeTipsetTimestampAtEpoch(tipset, targetEpoch, period)
assert.Equal(t, expected, actual1)
assert.Equal(t, expected, actual2)
})
}