-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatarithms_test.go
155 lines (123 loc) · 2.85 KB
/
datarithms_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
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
package datarithms_test
import (
"testing"
"github.com/COSI-Lab/datarithms"
)
// Test the queue
func TestQueue(t *testing.T) {
// Create a new queue
q := datarithms.CircularQueueInit[int](5)
if q.Capacity() != 5 {
t.Error("Capacity is not 5")
}
// Push some elements
q.Push(1)
q.Push(2)
q.Push(3)
// Check the length
if q.Len() != 3 {
t.Error("Expected 3, got", q.Len())
}
var element int
var err error
// Pop the first element
if element, err = q.Pop(); err == nil && element != 1 {
t.Error("Expected 1, got", element)
}
// Check the length
if q.Len() != 2 {
t.Error("Expected 2, got", q.Len())
}
// Pop the second element
if element, err = q.Pop(); err == nil && element != 2 {
t.Error("Expected 2, got", element)
}
// Check the length
if q.Len() != 1 {
t.Error("Expected 1, got", q.Len())
}
// Pop the third element
if element, err = q.Pop(); err == nil && element != 3 {
t.Error("Expected 3, got", element)
}
// Check the length
if q.Len() != 0 {
t.Error("Expected 0, got", q.Len())
}
// Pop the fourth element
if element, err = q.Pop(); err == nil && element == 0 {
t.Error("Expected nil, got", element)
}
// Check the length
if q.Len() != 0 {
t.Error("Expected 0, got", q.Len())
}
// Push more elements than capacity
for i := 0; i < 10; i++ {
q.Push(i)
}
// Check the length
if q.Len() != 5 {
t.Error("Expected 5, got", q.Len())
}
// Pop the first element
if element, err = q.Pop(); err != nil && element != 5 {
t.Error("Expected 5, got", element)
}
// Check the length
if q.Len() != 4 {
t.Error("Expected 4, got", q.Len())
}
// Pop the second element
if element, err = q.Pop(); err != nil && element != 6 {
t.Error("Expected 6, got", element)
}
// Check the length
if q.Len() != 3 {
t.Error("Expected 3, got", q.Len())
}
// Pop the third element
if element, err = q.Pop(); err != nil && element != 7 {
t.Error("Expected 7, got", element)
}
// Check the length
if q.Len() != 2 {
t.Error("Expected 2, got", q.Len())
}
// Pop the fourth element
if element, err = q.Pop(); err != nil && element != 8 {
t.Error("Expected 8, got", element)
}
// Check the length
if q.Len() != 1 {
t.Error("Expected 1, got", q.Len())
}
// Pop the fifth element
if element, err = q.Pop(); err != nil && element != 9 {
t.Error("Expected 9, got", element)
}
// Check the length
if q.Len() != 0 {
t.Error("Expected 0, got", q.Len())
}
}
func TestSchedule(t *testing.T) {
// Create tasks
tasks := []datarithms.Task{
{Short: "a", Syncs: 1},
{Short: "b", Syncs: 2},
{Short: "c", Syncs: 4},
{Short: "d", Syncs: 8},
}
sched := datarithms.BuildSchedule(tasks)
t.Log(sched)
verify := datarithms.Verify(sched, tasks)
if !verify {
t.Error("Schedule is invalid")
}
// Next task is in the future
_, dt := sched.NextJob()
if dt < 0 {
t.Error("Next task is in the past")
}
}