-
Notifications
You must be signed in to change notification settings - Fork 1
/
schedule_test.go
197 lines (158 loc) · 4.23 KB
/
schedule_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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
* Shift Scheduler
* Copyright (C) 2018 Ritchie Borja
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package schedule
import (
"testing"
)
func initialTestData() *Shift {
jack := &Shift{Start: 0,
End: 6,
Employee: Employee{"Jack", 17},
}
jill := &Shift{
Start: 6,
End: 10,
Employee: Employee{"Jill", 12},
}
john := &Shift{
Start: 10,
End: 19,
Employee: Employee{"John", 7},
}
john2 := &Shift{
Start: 19,
End: 23,
Employee: Employee{"John", 7},
}
jim := &Shift{
Start: 26,
End: 48,
Employee: Employee{"Jim", 4},
}
jack.next = jill
jill.next = john
john.next = john2
john2.next = jim
jill.prev = jack
john.prev = jill
john2.prev = john
jim.prev = john2
return jack
}
func TestShiftGapNothingOverlapped(t *testing.T) {
start, end := TimeToNumeric(11, 30), TimeToNumeric(13, 0)
if initialTestData().Overlaps(Interval(&Shift{Start: start, End: end})) {
t.Fail()
}
}
func TestShiftGapOverlappedLeftBound(t *testing.T) {
start, end := TimeToNumeric(11, 00), TimeToNumeric(13, 0)
if !initialTestData().Overlaps(Interval(&Shift{Start: start, End: end})) {
t.Fail()
}
}
func TestShiftGapOverlappedRightBound(t *testing.T) {
start, end := TimeToNumeric(11, 30), TimeToNumeric(13, 30)
if !initialTestData().Overlaps(Interval(&Shift{Start: start, End: end})) {
t.Fail()
}
}
func TestShiftGapOverlappedBothBound(t *testing.T) {
start, end := TimeToNumeric(11, 00), TimeToNumeric(13, 30)
if !initialTestData().Overlaps(Interval(&Shift{Start: start, End: end})) {
t.Fail()
}
}
func TestAddWorkerToShiftSuccessful(t *testing.T) {
start, end := TimeToNumeric(11, 30), TimeToNumeric(13, 0)
ritchie := &Shift{Start: start, End: end, Employee: Employee{"Ritchie", 666}}
shift := initialTestData()
Add(shift, ritchie)
var success bool
for i := shift; i != nil; i = i.next {
success = success || i == ritchie
}
// Test fails when "ritchie" is not in the shift list after adding to the shift
if !success {
t.Log("Employee's shift wasn't added")
t.FailNow()
}
}
func TestAddWorkerToFirstShift(t *testing.T) {
shift := initialTestData()
newFirst := shift.next
// Delete first shift
shift.next = nil
newFirst.prev = nil
newFirst, _ = Add(newFirst, shift)
success := false
for i := newFirst; i != nil; i = i.next {
success = success || i == shift
}
if newFirst.next == nil {
t.Log("First shift doesn't have next shifts")
t.Fail()
}
if !success {
// Test fails when "ritchie" is not in the shift list after adding to the shift
t.Log("Employee's shift wasn't added")
t.FailNow()
}
}
func TestAddWorkerToLastShift(t *testing.T) {
shift := initialTestData()
var last *Shift
for i := shift; i != nil; i = i.next {
last = i
}
last.prev.next = nil
last.prev = nil
Add(shift, last)
success := false
for i := shift; i != nil; i = i.next {
success = success || i == last
}
if !success {
// Test fails when "ritchie" is not in the shift list after adding to the shift
t.Log("Employee's shift wasn't added")
t.FailNow()
}
}
func TestTimeToNumeric(t *testing.T) {
hour, min := 11, 30
numeric := TimeToNumeric(hour, min)
if numeric != 23 {
t.Log("11:30 should be equivalent to 23")
t.Fail()
}
}
func TestTimeToNumericInvalidHours(t *testing.T) {
hour, min := 24, 0
numeric := TimeToNumeric(hour, min)
if numeric != -1 {
t.Log("24:00 should be an invalid time for any day")
t.Fail()
}
}
func TestReverseInterval(t *testing.T) {
shift := &Shift{Start: 48, End: 0}
if (&Shift{}).Add(shift) != false {
t.Error("Shift should not be added if an interval is reversed")
}
}