-
Notifications
You must be signed in to change notification settings - Fork 4
/
model_expression_time_dependent_test.go
319 lines (272 loc) · 8.39 KB
/
model_expression_time_dependent_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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// © 2019-present nextmv.io inc
package nextroute_test
import (
"testing"
"time"
"github.com/nextmv-io/nextroute"
)
func TestTimeDependentDurationExpression_SetExpression(t *testing.T) {
model, err := nextroute.NewModel()
if err != nil {
t.Fatal(err)
}
defaultExpression := nextroute.NewConstantDurationExpression("default", 0*time.Hour)
c1 := nextroute.NewConstantDurationExpression("c1", 1*time.Hour)
c2 := nextroute.NewConstantDurationExpression("c2", 2*time.Hour)
c3 := nextroute.NewConstantDurationExpression("c3", 3*time.Hour)
c4 := nextroute.NewConstantDurationExpression("c4", 4*time.Hour)
c5 := nextroute.NewConstantDurationExpression("c5", 5*time.Hour)
timeDependentExpression, err := nextroute.NewTimeDependentDurationExpression(
model,
defaultExpression,
)
if err != nil {
t.Fatal(err)
}
expression := timeDependentExpression.ExpressionAtTime(time.Now())
if expression == nil {
t.Error("expression should not be nil")
}
if expression != defaultExpression {
t.Error("expression should be defaultExpression")
}
if timeDependentExpression.IsDependentOnTime() {
t.Error("expression should not be dependent on time")
}
if len(timeDependentExpression.Expressions()) != 0 {
t.Error("expression should have no time dependent expressions")
}
s1 := time.Date(2020, 1, 1, 9, 30, 0, 0, time.UTC)
e1 := time.Date(2020, 1, 1, 10, 30, 0, 0, time.UTC)
err = timeDependentExpression.SetExpression(
s1,
e1,
c1,
)
if err != nil {
t.Fatal(err)
}
if !timeDependentExpression.IsDependentOnTime() {
t.Error("expression should be dependent on time")
}
if len(timeDependentExpression.Expressions()) != 1 {
t.Error("expression should have one time dependent expression")
}
if timeDependentExpression.ExpressionAtTime(s1.Add(-time.Second)) != defaultExpression {
t.Error("expression before s1 should be defaultExpression")
}
if timeDependentExpression.ExpressionAtTime(s1) != c1 {
t.Error("expression at s1 should be c1")
}
if timeDependentExpression.ExpressionAtTime(e1) != defaultExpression {
t.Error("expression at e1 or later should be defaultExpression")
}
s2 := time.Date(2020, 1, 1, 11, 30, 0, 0, time.UTC)
e2 := time.Date(2020, 1, 1, 12, 30, 0, 0, time.UTC)
err = timeDependentExpression.SetExpression(
s2,
e2,
c2,
)
if err != nil {
t.Fatal(err)
}
if !timeDependentExpression.IsDependentOnTime() {
t.Error("expression should be dependent on time")
}
if len(timeDependentExpression.Expressions()) != 2 {
t.Error("expression should have two time dependent expression")
}
if timeDependentExpression.ExpressionAtTime(s1.Add(-time.Second)) != defaultExpression {
t.Error("expression before s1 should be defaultExpression")
}
if timeDependentExpression.ExpressionAtTime(s1) != c1 {
t.Error("expression at s1 should be c1")
}
if timeDependentExpression.ExpressionAtTime(e1) != defaultExpression {
t.Error("expression at e1 or later should be defaultExpression")
}
if timeDependentExpression.ExpressionAtTime(s2) != c2 {
t.Error("expression at s2 should be c2")
}
if timeDependentExpression.ExpressionAtTime(e2) != defaultExpression {
t.Error("expression at e2 should be defaultExpression")
}
s3 := time.Date(2020, 1, 1, 4, 30, 0, 0, time.UTC)
e3 := time.Date(2020, 1, 1, 5, 30, 0, 0, time.UTC)
err = timeDependentExpression.SetExpression(
s3,
e3,
c3,
)
if err != nil {
t.Fatal(err)
}
if timeDependentExpression.ExpressionAtTime(s1.Add(-time.Second)) != defaultExpression {
t.Error("expression before s1 should be defaultExpression")
}
if timeDependentExpression.ExpressionAtTime(s1) != c1 {
t.Error("expression at s1 should be c1")
}
if timeDependentExpression.ExpressionAtTime(e1) != defaultExpression {
t.Error("expression at e1 or later should be defaultExpression")
}
if timeDependentExpression.ExpressionAtTime(s2) != c2 {
t.Error("expression at s2 should be c2")
}
if timeDependentExpression.ExpressionAtTime(e2) != defaultExpression {
t.Error("expression at e2 should be defaultExpression")
}
if timeDependentExpression.ExpressionAtTime(s3.Add(-time.Second)) != defaultExpression {
t.Error("expression before s3 should be defaultExpression")
}
if timeDependentExpression.ExpressionAtTime(s3) != c3 {
t.Error("expression at s3 should be c3")
}
if timeDependentExpression.ExpressionAtTime(e3) != defaultExpression {
t.Error("expression at e3 should be defaultExpression")
}
s4 := time.Date(2020, 1, 1, 5, 0o0, 0, 0, time.UTC)
e4 := time.Date(2020, 1, 1, 11, 0o0, 0, 0, time.UTC)
err = timeDependentExpression.SetExpression(
s4,
e4,
c4,
)
if err == nil {
t.Fatal("should not be able to set expression that overlaps with existing expression")
}
s5 := time.Date(2020, 1, 1, 12, 30, 0, 0, time.UTC)
e5 := time.Date(2020, 1, 1, 13, 0o0, 0, 0, time.UTC)
err = timeDependentExpression.SetExpression(
s5,
e5,
c5,
)
if err != nil {
t.Fatal("should be able to set expression that starts at the end of an existing expression")
}
if len(timeDependentExpression.Expressions()) != 5 {
t.Error("expression should have five time dependent expression")
}
s6 := time.Date(2020, 1, 1, 4, 0o0, 0, 0, time.UTC)
e6 := time.Date(2020, 1, 1, 4, 30, 0, 0, time.UTC)
err = timeDependentExpression.SetExpression(
s6,
e6,
c5,
)
if err != nil {
t.Fatal("should be able to set expression that ends at the start of an existing expression")
}
if len(timeDependentExpression.Expressions()) != 5 {
t.Error("expression should have five time dependent expression")
}
}
func TestTimeDependentDurationExpression_ValueAtTime1(t *testing.T) {
model, err := nextroute.NewModel()
if err != nil {
t.Fatal(err)
}
defaultExpression := nextroute.NewConstantDurationExpression("default", 1*time.Hour)
c1 := nextroute.NewConstantDurationExpression("c1", 30*time.Minute)
timeDependentExpression, err := nextroute.NewTimeDependentDurationExpression(
model,
defaultExpression,
)
if err != nil {
t.Fatal(err)
}
s1 := time.Date(2020, 1, 1, 8, 30, 0, 0, time.UTC)
e1 := time.Date(2020, 1, 1, 8, 45, 0, 0, time.UTC)
value := timeDependentExpression.ValueAtTime(s1, nil, nil, nil)
if value != 3600 {
t.Fatal("value should be 3600, 1 hour from defaultExpression")
}
err = timeDependentExpression.SetExpression(
s1,
e1,
c1,
)
if err != nil {
t.Fatal(err)
}
value = timeDependentExpression.ValueAtTime(s1, nil, nil, nil)
if value != 2700 {
t.Error("value should be 2700, 1800 from defaultExpression and 900 from c1")
}
value = timeDependentExpression.ValueAtTime(e1, nil, nil, nil)
if value != 3600 {
t.Fatalf(
"value should be 3600, 1 hour from defaultExpression, it is %v", value)
}
}
func TestTimeDependentDurationExpression_ValueAtTime2(t *testing.T) {
model, err := nextroute.NewModel()
if err != nil {
t.Fatal(err)
}
defaultExpression := nextroute.NewConstantDurationExpression("default", 6*time.Hour)
c1 := nextroute.NewConstantDurationExpression("c1", 12*time.Hour)
c2 := nextroute.NewConstantDurationExpression("c2", 3*time.Hour)
timeDependentExpression, err := nextroute.NewTimeDependentDurationExpression(
model,
defaultExpression,
)
if err != nil {
t.Fatal(err)
}
s1 := time.Date(2020, 1, 1, 8, 0o0, 0, 0, time.UTC)
e1 := time.Date(2020, 1, 1, 9, 0o0, 0, 0, time.UTC)
err = timeDependentExpression.SetExpression(
s1,
e1,
c1,
)
if err != nil {
t.Fatal(err)
}
s2 := time.Date(2020, 1, 1, 9, 0o0, 0, 0, time.UTC)
e2 := time.Date(2020, 1, 1, 11, 0o0, 0, 0, time.UTC)
err = timeDependentExpression.SetExpression(
s2,
e2,
c2,
)
if err != nil {
t.Fatal(err)
}
s3 := time.Date(2020, 1, 1, 12, 0o0, 0, 0, time.UTC)
e3 := time.Date(2020, 1, 1, 12, 0o0, 0, 0, time.UTC)
err = timeDependentExpression.SetExpression(
s3,
e3,
c2,
)
if err != nil {
t.Fatal(err)
}
value := timeDependentExpression.ValueAtTime(s1.Add(-1*time.Hour), nil, nil, nil)
if value != 16200 {
t.Error("value should be 16200")
}
value = timeDependentExpression.ValueAtTime(s2, nil, nil, nil)
if int(value) != 14400 {
t.Error("value should be 14400 but is: ", value)
}
value = timeDependentExpression.ValueAtTime(e2, nil, nil, nil)
if value != 21600 {
t.Error("value should be 21600 but is: ", value)
}
handlePanic := func() {
r := recover()
if r != nil {
t.Fatalf("should not panic, but did: %v", r)
}
}
defer handlePanic()
value = timeDependentExpression.ValueAtTime(s2.Add(-30*time.Minute), nil, nil, nil)
if int(value) != 15300 {
t.Error("value should be 15300 but is: ", value)
}
}