-
Notifications
You must be signed in to change notification settings - Fork 59
/
tc_temp.rb
406 lines (346 loc) · 7.44 KB
/
tc_temp.rb
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
require './test_common'
class BasicTemp
include Bud
state do
scratch :out, [:val]
scratch :inski
end
bloom :rules do
temp :tmpy <= inski
out <= tmpy {|t| [t.val]}
end
end
class DupTemp < BasicTemp
bloom :rules do
temp :tmpy <= inski
temp :tmpy <= inski
out <= tmpy {|t| [t.val]}
end
end
class ReuseTemp < BasicTemp
bloom :rules do
temp :hemp <= inski
hemp <= [[1,2]]
end
end
class TempNext
include Bud
state do
scratch :inski, [:c1, :c2] => [:c3, :c4]
scratch :wait, [:c1, :c2] => [:c3, :c4]
scratch :out, [:c1, :c2] => [:c3, :c4]
end
bloom do
temp :tmpy <= inski
temp :waity <+ wait
out <= tmpy
out <= waity
end
end
class TempNoSchema
include Bud
bloom do
temp :out <= [[1,2], [3, 4]]
end
end
class SimpleTempTest
include Bud
state do
table :t1
table :t2
scratch :t3
scratch :t4
scratch :t5
end
bootstrap do
t1 << [5, 10]
t1 << [10, 20]
t2 << [50, 100]
t2 << [75, 125]
end
bloom do
temp :k <= (t1 * t2)
t3 <= k.map {|a, b| [a.key + b.key, a.val + b.val]}
t4 <= k.map {|a, b| [a.key + b.key, a.val + b.val]}
t5 <= k.map {|a, b| a if b.val > 100}
end
end
class TempRefTemp
include Bud
state do
table :t1
scratch :t2
end
bootstrap do
t1 << [100, 200]
end
bloom do
temp :a <= t1.map {|t| [t.key + 10, t.val + 10]}
temp :b <= a.map {|t| [t[0] + 20, t[1] + 20]}
temp :c <= b.map {|t| [t[0] - 50, t[1] - 100]}
temp :d <= b.map {|t| [t[0] - 50, t[1] - 100]} # unreferenced
t2 <= c
end
end
class TempShadow
include Bud
state do
table :t1
table :t2
table :t3
table :t4
end
bootstrap do
t1 << [20,40]
t1 << [40,60]
end
bloom do
temp :k <= t1.map {|t| [t.key + 10, t.val + 20]}
t2 <= k
t2 <= t1.map {|k| [k.key, k.val]}
t3 <= (t1 * t2).pairs(:key => :key) {|j,k| [j.key + 20, k.val + 20]}
t4 <= t3.map {|t| [t.key, t.val] unless k.include? t}
end
end
class SimpleTempNoMapTest
include Bud
state do
table :t1
table :t2
scratch :t3
scratch :t4
scratch :t5
end
bootstrap do
t1 << [5, 10]
t1 << [10, 20]
t2 << [50, 100]
t2 << [75, 125]
end
bloom do
temp :k <= (t1 * t2)
t3 <= k {|a, b| [a.key + b.key, a.val + b.val]}
t4 <= k {|a, b| [a.key + b.key, a.val + b.val]}
t5 <= k {|a, b| a if b.val > 100}
end
end
class TempNoMapRefTempNoMap
include Bud
state do
table :t1
scratch :t2
end
bootstrap do
t1 << [100, 200]
end
bloom do
temp :a <= t1 {|t| [t.key + 10, t.val + 10]}
temp :b <= a {|t| [t[0] + 20, t[1] + 20]}
temp :c <= b {|t| [t[0] - 50, t[1] - 100]}
temp :d <= b {|t| [t[0] - 50, t[1] - 100]} # unreferenced
t2 <= c
end
end
class TempNoMapShadow
include Bud
state do
table :t1
table :t2
table :t3
table :t4
end
bootstrap do
t1 << [20,40]
t1 << [40,60]
end
bloom do
temp :k <= t1 {|t| [t.key + 10, t.val + 20]}
t2 <= k
t2 <= t1 {|k| [k.key, k.val]}
t3 <= (t1 * t2).pairs(:key => :key) {|j,k| [j.key + 20, k.val + 20]}
t4 <= t3 {|t| [t.key, t.val] unless k.include? t}
end
end
# Check that schema inference works when many of the initial inputs are nil
class TempWithPredicate
include Bud
state do
table :t1
table :t2
end
bootstrap do
t1 << [1, 11]
t1 << [2, 11]
t1 << [3, 11]
t1 << [4, 11]
t1 << [5, 11]
t1 << [6, 11]
t1 << [7, 11]
end
bloom do
temp :xyz <= t1 {|t| t if t.key == 7}
t2 <= xyz
end
end
# Check that schema inference works for <+ rules
class TempAtNext
include Bud
state do
table :t1
table :t2
end
bootstrap do
t1 << [5, 10]
end
bloom do
temp :xyz <+ t1 {|t| [t.key + 1, t.val + 1]}
t2 <= xyz
end
end
class TestTemps < Minitest::Test
def test_basic_temp
p = BasicTemp.new
p.run_bg
p.sync_do{p.inski <+ [[1,1], [2,2], [3,3]]}
p.stop
assert_equal(3, p.out.length)
assert_equal([[1], [2], [3]], p.out.map{|o| [o.val]}.sort)
end
def test_retemp
p = BasicTemp.new
p.run_bg
p.sync_do{p.inski <+ [[1,1], [2,2], [3,3]]}
p.stop
assert_equal(3, p.out.length)
assert_equal([[1], [2], [3]], p.out.map{|o| [o.val]}.sort)
end
def test_temp_next
p = TempNext.new
p.run_bg
p.sync_do do
p.inski <+ [[1,1,2,2],
[2,2,3,3],
[3,3,4,4]]
p.wait <+ [[5,5,6,6],
[7,7,8,8],
[9,9,9,9]]
end
assert_equal(3, p.out.length)
assert_equal([[1], [2], [3]], p.out.map{|o| [o.c1]}.sort)
p.sync_do
assert_equal(3, p.out.length)
assert_equal([[5], [7], [9]], p.out.map{|o| [o.c1]}.sort)
p.stop
end
def test_dup_tmp
assert_raises(Bud::CompileError) {DupTemp.new}
end
def test_reuse_tmp
p = ReuseTemp.new
p.tick
assert_equal(1, p.hemp.length)
end
def test_no_schema
p = TempNoSchema.new
p.tick
end
def test_simple_temp
p = SimpleTempTest.new
p.tick
assert_equal(p.t3.to_a.sort, [[55, 110], [60, 120], [80, 135], [85, 145]])
assert_equal(p.t4.to_a.sort, [[55, 110], [60, 120], [80, 135], [85, 145]])
assert_equal(p.t5.to_a.sort, [[5, 10], [10, 20]])
end
def test_temp_pred_schema_infer
p = TempWithPredicate.new
p.tick
assert_equal(p.t2.to_a.sort, [[7, 11]])
end
def test_test_atnext_schema_infer
p = TempAtNext.new
p.tick
assert_equal(p.t2.to_a.sort, [])
p.tick
assert_equal(p.t2.to_a.sort, [[6, 11]])
end
def test_temp_in_temp
p = TempRefTemp.new
p.tick
assert_equal(p.t2.to_a.sort, [[80, 130]])
end
def test_temp_shadow
p = TempShadow.new
p.tick
assert_equal(p.k.to_a.sort, [[30, 60], [50, 80]])
assert_equal(p.t2.to_a.sort, [[20, 40], [30, 60], [40, 60], [50, 80]])
assert_equal(p.t3.to_a.sort, [[40, 60], [60, 80]])
assert_equal(p.t3.to_a.sort, p.t4.to_a.sort)
end
class Issue132
include Bud
state do
table :foo
table :bar
end
bootstrap do
foo << [10, 10]
end
bloom do
temp :baz <= foo do |f|
[f.key, f.val + 1]
end
bar <= baz
end
end
def test_issue132
i = Issue132.new
i.tick
assert_equal(i.bar.to_a.sort, [[10, 11]])
end
end
class TestTempNoMaps < Minitest::Test
def test_simple_test_nomap
p = SimpleTempNoMapTest.new
p.tick
assert_equal(p.t3.to_a.sort, [[55, 110], [60, 120], [80, 135], [85, 145]])
assert_equal(p.t4.to_a.sort, [[55, 110], [60, 120], [80, 135], [85, 145]])
assert_equal(p.t5.to_a.sort, [[5, 10], [10, 20]])
end
def test_temp_nomap_in_temp_nomap
p = TempNoMapRefTempNoMap.new
p.tick
assert_equal(p.t2.to_a.sort, [[80, 130]])
end
def test_temp_nomap_shadow
p = TempNoMapShadow.new
p.tick
assert_equal(p.k.to_a.sort, [[30, 60], [50, 80]])
assert_equal(p.t2.to_a.sort, [[20, 40], [30, 60], [40, 60], [50, 80]])
assert_equal(p.t3.to_a.sort, [[40, 60], [60, 80]])
assert_equal(p.t3.to_a.sort, p.t4.to_a.sort)
end
end
module TestDefModule
state do
table :t1
table :t2
end
bootstrap do
t1 << [10, 10]
end
bloom do
temp :t3 <= t1 {|t| [t.key + 10, t.val + 20]}
t2 <= t3 {|t| [t[0] + 20, t[1] + 40]}
end
end
class TestModuleUser
include Bud
include TestDefModule
end
class TestModuleTemp < Minitest::Test
def test_simple
c = TestModuleUser.new
c.tick
assert_equal(c.t2.to_a.sort, [[40, 70]])
end
end