depq 0.2 → 0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README +59 -47
- data/depq.rb +452 -470
- data/test-depq.rb +599 -368
- metadata +1 -1
data/test-depq.rb
CHANGED
@@ -29,16 +29,16 @@ require 'test/unit'
|
|
29
29
|
|
30
30
|
class Depq
|
31
31
|
module SimpleHeap
|
32
|
-
def validation(
|
32
|
+
def validation(q, ary)
|
33
33
|
0.upto(size(ary)-1) {|i|
|
34
34
|
_, x = get_entry(ary, i)
|
35
35
|
j = i*2+1
|
36
36
|
k = i*2+2
|
37
|
-
if j < size(ary) && !upper?(
|
37
|
+
if j < size(ary) && !upper?(q, ary, i, j)
|
38
38
|
_, y = get_entry(ary, j)
|
39
39
|
raise "wrong binary heap: pri[#{i}]=#{x.inspect} > #{y.inspect}=pri[#{j}]"
|
40
40
|
end
|
41
|
-
if k < size(ary) && !upper?(
|
41
|
+
if k < size(ary) && !upper?(q, ary, i, k)
|
42
42
|
_, z = get_entry(ary, k)
|
43
43
|
raise "wrong binary heap: pri[#{i}]=#{x.inspect} > #{z.inspect}=pri[#{k}]"
|
44
44
|
end
|
@@ -46,25 +46,25 @@ class Depq
|
|
46
46
|
end
|
47
47
|
end
|
48
48
|
|
49
|
-
def IntervalHeap.validation(
|
49
|
+
def IntervalHeap.validation(q, ary)
|
50
50
|
range=0...size(ary)
|
51
51
|
range.each {|j|
|
52
52
|
imin = parent_minside(j)
|
53
53
|
imax = parent_maxside(j)
|
54
54
|
jmin = minside(j)
|
55
|
-
if minside?(j) && range.include?(imin) && pcmp(
|
55
|
+
if minside?(j) && range.include?(imin) && pcmp(q, ary, imin, j) > 0
|
56
56
|
raise "ary[#{imin}].priority > ary[#{j}].priority "
|
57
57
|
end
|
58
|
-
if maxside?(j) && range.include?(imax) && pcmp(
|
58
|
+
if maxside?(j) && range.include?(imax) && pcmp(q, ary, imax, j) < 0
|
59
59
|
raise "ary[#{imax}].priority < ary[#{j}].priority "
|
60
60
|
end
|
61
|
-
if range.include?(imin) && pcmp(
|
61
|
+
if range.include?(imin) && pcmp(q, ary, imin, j) == 0 && scmp(q, ary, imin, j) > 0
|
62
62
|
raise "ary[#{imin}].subpriority < ary[#{j}].subpriority "
|
63
63
|
end
|
64
|
-
if range.include?(imax) && pcmp(
|
64
|
+
if range.include?(imax) && pcmp(q, ary, imax, j) == 0 && scmp(q, ary, imax, j) > 0
|
65
65
|
raise "ary[#{imax}].subpriority < ary[#{j}].subpriority "
|
66
66
|
end
|
67
|
-
if maxside?(j) && range.include?(jmin) && pcmp(
|
67
|
+
if maxside?(j) && range.include?(jmin) && pcmp(q, ary, jmin, j) == 0 && scmp(q, ary, jmin, j) > 0
|
68
68
|
raise "ary[#{jmin}].subpriority < ary[#{j}].subpriority "
|
69
69
|
end
|
70
70
|
}
|
@@ -83,8 +83,8 @@ class Depq
|
|
83
83
|
if loc.send(:index) != i
|
84
84
|
raise "index mismatch"
|
85
85
|
end
|
86
|
-
unless self.equal? loc.
|
87
|
-
raise "
|
86
|
+
unless self.equal? loc.depq
|
87
|
+
raise "depq mismatch"
|
88
88
|
end
|
89
89
|
i += 1
|
90
90
|
}
|
@@ -105,23 +105,23 @@ class TestDepq < Test::Unit::TestCase
|
|
105
105
|
else
|
106
106
|
raise "wrong mode"
|
107
107
|
end
|
108
|
-
|
108
|
+
q = Depq.new
|
109
109
|
n = 10
|
110
110
|
a1 = []
|
111
111
|
n.times {
|
112
112
|
r = rand(n)
|
113
113
|
a1 << r
|
114
|
-
|
114
|
+
q.insert(r)
|
115
115
|
if incremental
|
116
|
-
|
117
|
-
|
116
|
+
q.send find
|
117
|
+
q.validation
|
118
118
|
end
|
119
119
|
}
|
120
120
|
a1.sort!(&cmp)
|
121
121
|
a2 = []
|
122
122
|
n.times {
|
123
|
-
a2 <<
|
124
|
-
|
123
|
+
a2 << q.send(delete)
|
124
|
+
q.validation
|
125
125
|
}
|
126
126
|
assert_equal(a1, a2)
|
127
127
|
end
|
@@ -136,18 +136,18 @@ class TestDepq < Test::Unit::TestCase
|
|
136
136
|
def perm_test(ary, incremental)
|
137
137
|
a0 = ary.to_a.sort
|
138
138
|
a0.permutation {|a1|
|
139
|
-
|
139
|
+
q = Depq.new
|
140
140
|
a1.each {|v|
|
141
|
-
|
141
|
+
q.insert v
|
142
142
|
if incremental
|
143
|
-
|
144
|
-
|
143
|
+
q.find_min
|
144
|
+
q.validation
|
145
145
|
end
|
146
146
|
}
|
147
|
-
|
148
|
-
|
147
|
+
q.find_min
|
148
|
+
q.validation
|
149
149
|
a0.each {|v|
|
150
|
-
assert_equal(v,
|
150
|
+
assert_equal(v, q.delete_min)
|
151
151
|
}
|
152
152
|
}
|
153
153
|
end
|
@@ -164,19 +164,19 @@ class TestDepq < Test::Unit::TestCase
|
|
164
164
|
a0.permutation {|a1|
|
165
165
|
0.upto(2**(a1.length-1)-1) {|n|
|
166
166
|
#log = []; p [:n, n, 2**(a1.length-1)-1]
|
167
|
-
|
167
|
+
q = Depq.new
|
168
168
|
a1.each_with_index {|v,i|
|
169
|
-
|
169
|
+
q.insert v
|
170
170
|
#log << v
|
171
171
|
if n[i] != 0
|
172
|
-
|
173
|
-
|
172
|
+
q.find_min
|
173
|
+
q.validation
|
174
174
|
#log << :find_min
|
175
175
|
end
|
176
176
|
}
|
177
177
|
#p log
|
178
178
|
a0.each {|v|
|
179
|
-
assert_equal(v,
|
179
|
+
assert_equal(v, q.delete_min)
|
180
180
|
}
|
181
181
|
}
|
182
182
|
}
|
@@ -189,37 +189,42 @@ class TestDepq < Test::Unit::TestCase
|
|
189
189
|
end
|
190
190
|
|
191
191
|
def test_stable_min
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
assert_equal("a",
|
197
|
-
assert_equal("b",
|
198
|
-
assert_equal("c",
|
192
|
+
q = Depq.new
|
193
|
+
q.insert "a", 0
|
194
|
+
q.insert "b", 0
|
195
|
+
q.insert "c", 0
|
196
|
+
assert_equal("a", q.delete_min)
|
197
|
+
assert_equal("b", q.delete_min)
|
198
|
+
assert_equal("c", q.delete_min)
|
199
199
|
end
|
200
200
|
|
201
201
|
def test_stable_max
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
assert_equal("a",
|
207
|
-
assert_equal("b",
|
208
|
-
assert_equal("c",
|
202
|
+
q = Depq.new
|
203
|
+
q.insert "a", 0
|
204
|
+
q.insert "b", 0
|
205
|
+
q.insert "c", 0
|
206
|
+
assert_equal("a", q.delete_max)
|
207
|
+
assert_equal("b", q.delete_max)
|
208
|
+
assert_equal("c", q.delete_max)
|
209
209
|
end
|
210
210
|
|
211
211
|
def test_locator_new
|
212
|
-
|
212
|
+
q = Depq.new
|
213
213
|
loc1 = Depq::Locator.new(1)
|
214
214
|
loc2 = Depq::Locator.new(2, 3)
|
215
215
|
assert_equal(1, loc1.value)
|
216
216
|
assert_equal(1, loc1.priority)
|
217
217
|
assert_equal(2, loc2.value)
|
218
218
|
assert_equal(3, loc2.priority)
|
219
|
-
|
220
|
-
|
221
|
-
assert_equal(loc1,
|
222
|
-
assert_equal(loc2,
|
219
|
+
q.insert_locator loc1
|
220
|
+
q.insert_locator loc2
|
221
|
+
assert_equal(loc1, q.delete_min_locator)
|
222
|
+
assert_equal(loc2, q.delete_min_locator)
|
223
|
+
end
|
224
|
+
|
225
|
+
def test_locator_dup
|
226
|
+
loc = Depq::Locator.new(1)
|
227
|
+
assert_raise(TypeError) { loc.dup }
|
223
228
|
end
|
224
229
|
|
225
230
|
def test_locator_eql
|
@@ -229,50 +234,50 @@ class TestDepq < Test::Unit::TestCase
|
|
229
234
|
end
|
230
235
|
|
231
236
|
def test_locator_priority
|
232
|
-
|
233
|
-
loc2 =
|
234
|
-
loc1 =
|
235
|
-
loc3 =
|
237
|
+
q = Depq.new
|
238
|
+
loc2 = q.insert(Object.new, 2)
|
239
|
+
loc1 = q.insert(Object.new, 1)
|
240
|
+
loc3 = q.insert(Object.new, 3)
|
236
241
|
assert_equal(1, loc1.priority)
|
237
242
|
assert_equal(2, loc2.priority)
|
238
243
|
assert_equal(3, loc3.priority)
|
239
|
-
|
244
|
+
q.delete_locator(loc1)
|
240
245
|
assert_equal(1, loc1.priority)
|
241
246
|
end
|
242
247
|
|
243
248
|
def test_locator_update_min
|
244
|
-
|
245
|
-
a =
|
246
|
-
b =
|
247
|
-
c =
|
248
|
-
assert_equal(b,
|
249
|
+
q = Depq.new
|
250
|
+
a = q.insert("a", 2)
|
251
|
+
b = q.insert("b", 1)
|
252
|
+
c = q.insert("c", 3)
|
253
|
+
assert_equal(b, q.find_min_locator)
|
249
254
|
a.update("d", 0)
|
250
255
|
assert_equal("d", a.value)
|
251
|
-
assert_equal(a,
|
256
|
+
assert_equal(a, q.find_min_locator)
|
252
257
|
a.update("e", 10)
|
253
|
-
assert_equal("b",
|
254
|
-
assert_equal("c",
|
255
|
-
assert_equal("e",
|
258
|
+
assert_equal("b", q.delete_min)
|
259
|
+
assert_equal("c", q.delete_min)
|
260
|
+
assert_equal("e", q.delete_min)
|
256
261
|
a.update "z", 20
|
257
262
|
assert_equal("z", a.value)
|
258
263
|
assert_equal(20, a.priority)
|
259
264
|
end
|
260
265
|
|
261
266
|
def test_locator_update_subpriority_min
|
262
|
-
|
263
|
-
a =
|
264
|
-
b =
|
265
|
-
c =
|
266
|
-
d =
|
267
|
-
e =
|
268
|
-
f =
|
269
|
-
assert_equal(a,
|
267
|
+
q = Depq.new
|
268
|
+
a = q.insert("a", 1, 0)
|
269
|
+
b = q.insert("b", 2, 1)
|
270
|
+
c = q.insert("c", 1, 2)
|
271
|
+
d = q.insert("d", 2, 3)
|
272
|
+
e = q.insert("e", 1, 4)
|
273
|
+
f = q.insert("f", 2, 5)
|
274
|
+
assert_equal(a, q.find_min_locator)
|
270
275
|
a.update("A", 1, 10)
|
271
|
-
assert_equal(c,
|
276
|
+
assert_equal(c, q.find_min_locator)
|
272
277
|
a.update("aa", 1, 1)
|
273
|
-
assert_equal(a,
|
274
|
-
|
275
|
-
assert_equal(c,
|
278
|
+
assert_equal(a, q.find_min_locator)
|
279
|
+
q.delete_locator a
|
280
|
+
assert_equal(c, q.find_min_locator)
|
276
281
|
a.update("aaa", 10, 20)
|
277
282
|
assert_equal("aaa", a.value)
|
278
283
|
assert_equal(10, a.priority)
|
@@ -280,20 +285,20 @@ class TestDepq < Test::Unit::TestCase
|
|
280
285
|
end
|
281
286
|
|
282
287
|
def test_locator_update_subpriority_max
|
283
|
-
|
284
|
-
a =
|
285
|
-
b =
|
286
|
-
c =
|
287
|
-
d =
|
288
|
-
e =
|
289
|
-
f =
|
290
|
-
assert_equal(b,
|
288
|
+
q = Depq.new
|
289
|
+
a = q.insert("a", 1, 0)
|
290
|
+
b = q.insert("b", 2, 1)
|
291
|
+
c = q.insert("c", 1, 2)
|
292
|
+
d = q.insert("d", 2, 3)
|
293
|
+
e = q.insert("e", 1, 4)
|
294
|
+
f = q.insert("f", 2, 5)
|
295
|
+
assert_equal(b, q.find_max_locator)
|
291
296
|
b.update("B", 2, 6)
|
292
|
-
assert_equal(d,
|
297
|
+
assert_equal(d, q.find_max_locator)
|
293
298
|
b.update("bb", 2, 0)
|
294
|
-
assert_equal(b,
|
295
|
-
|
296
|
-
assert_equal(d,
|
299
|
+
assert_equal(b, q.find_max_locator)
|
300
|
+
q.delete_locator b
|
301
|
+
assert_equal(d, q.find_max_locator)
|
297
302
|
b.update("bbb", -1, -2)
|
298
303
|
assert_equal("bbb", b.value)
|
299
304
|
assert_equal(-1, b.priority)
|
@@ -301,23 +306,23 @@ class TestDepq < Test::Unit::TestCase
|
|
301
306
|
end
|
302
307
|
|
303
308
|
def test_locator_update_max
|
304
|
-
|
305
|
-
a =
|
306
|
-
b =
|
307
|
-
c =
|
308
|
-
assert_equal(c,
|
309
|
+
q = Depq.new
|
310
|
+
a = q.insert("a", 2)
|
311
|
+
b = q.insert("b", 1)
|
312
|
+
c = q.insert("c", 3)
|
313
|
+
assert_equal(c, q.find_max_locator)
|
309
314
|
b.update("d", 10)
|
310
315
|
assert_equal("d", b.value)
|
311
|
-
assert_equal(b,
|
316
|
+
assert_equal(b, q.find_max_locator)
|
312
317
|
b.update("e", 0)
|
313
|
-
assert_equal("c",
|
314
|
-
assert_equal("a",
|
315
|
-
assert_equal("e",
|
318
|
+
assert_equal("c", q.delete_max)
|
319
|
+
assert_equal("a", q.delete_max)
|
320
|
+
assert_equal("e", q.delete_max)
|
316
321
|
end
|
317
322
|
|
318
323
|
def test_locator_update_value
|
319
|
-
|
320
|
-
loc =
|
324
|
+
q = Depq.new
|
325
|
+
loc = q.insert 1, 2, 3
|
321
326
|
assert_equal(1, loc.value)
|
322
327
|
assert_equal(2, loc.priority)
|
323
328
|
assert_equal(3, loc.subpriority)
|
@@ -328,8 +333,8 @@ class TestDepq < Test::Unit::TestCase
|
|
328
333
|
end
|
329
334
|
|
330
335
|
def test_locator_update_priority
|
331
|
-
|
332
|
-
loc =
|
336
|
+
q = Depq.new
|
337
|
+
loc = q.insert 1, 2, 3
|
333
338
|
assert_equal(1, loc.value)
|
334
339
|
assert_equal(2, loc.priority)
|
335
340
|
assert_equal(3, loc.subpriority)
|
@@ -345,7 +350,7 @@ class TestDepq < Test::Unit::TestCase
|
|
345
350
|
assert_equal(1, loc.value)
|
346
351
|
assert_equal(40, loc.priority)
|
347
352
|
assert_equal(30, loc.subpriority)
|
348
|
-
|
353
|
+
q.delete_min
|
349
354
|
assert_equal(1, loc.value)
|
350
355
|
assert_equal(40, loc.priority)
|
351
356
|
assert_equal(30, loc.subpriority)
|
@@ -355,410 +360,623 @@ class TestDepq < Test::Unit::TestCase
|
|
355
360
|
assert_equal(nil, loc.subpriority)
|
356
361
|
end
|
357
362
|
|
363
|
+
def test_locator_minmax_update_priority
|
364
|
+
q = Depq.new
|
365
|
+
loc = q.insert 1, 2, 3
|
366
|
+
q.insert 4, 5, 6
|
367
|
+
q.insert 2, 3, 4
|
368
|
+
q.insert 3, 4, 5
|
369
|
+
assert_equal([1, 4], q.minmax)
|
370
|
+
loc.update 7, 8, 9
|
371
|
+
assert_equal([2, 7], q.minmax)
|
372
|
+
end
|
373
|
+
|
358
374
|
def test_locator_subpriority
|
359
|
-
|
360
|
-
loc1 =
|
361
|
-
loc2 =
|
362
|
-
loc3 =
|
375
|
+
q = Depq.new
|
376
|
+
loc1 = q.insert(Object.new, 1, 11)
|
377
|
+
loc2 = q.insert(Object.new, 2, 12)
|
378
|
+
loc3 = q.insert(Object.new, 3, 13)
|
363
379
|
assert_equal(1, loc1.priority)
|
364
380
|
assert_equal(11, loc1.subpriority)
|
365
381
|
assert_equal(2, loc2.priority)
|
366
382
|
assert_equal(12, loc2.subpriority)
|
367
383
|
assert_equal(3, loc3.priority)
|
368
384
|
assert_equal(13, loc3.subpriority)
|
369
|
-
|
385
|
+
q.delete_locator(loc1)
|
370
386
|
assert_equal(11, loc1.subpriority)
|
371
387
|
end
|
372
388
|
|
373
389
|
def test_new
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
assert_equal("Foo",
|
378
|
-
assert_equal("bar",
|
390
|
+
q = Depq.new
|
391
|
+
q.insert "Foo"
|
392
|
+
q.insert "bar"
|
393
|
+
assert_equal("Foo", q.delete_min)
|
394
|
+
assert_equal("bar", q.delete_min)
|
379
395
|
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
assert_equal("bar",
|
384
|
-
assert_equal("Foo",
|
396
|
+
q = Depq.new(:casecmp)
|
397
|
+
q.insert "Foo"
|
398
|
+
q.insert "bar"
|
399
|
+
assert_equal("bar", q.delete_min)
|
400
|
+
assert_equal("Foo", q.delete_min)
|
385
401
|
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
assert_equal("bar",
|
390
|
-
assert_equal("Foo",
|
402
|
+
q = Depq.new(lambda {|a,b| a.casecmp(b) })
|
403
|
+
q.insert "Foo"
|
404
|
+
q.insert "bar"
|
405
|
+
assert_equal("bar", q.delete_min)
|
406
|
+
assert_equal("Foo", q.delete_min)
|
391
407
|
end
|
392
408
|
|
393
409
|
def test_dup
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
assert_equal(1,
|
402
|
-
|
403
|
-
|
404
|
-
assert_equal(nil,
|
405
|
-
|
406
|
-
|
407
|
-
assert_equal(1,
|
408
|
-
|
409
|
-
|
410
|
-
assert_equal(2,
|
411
|
-
assert_equal(nil,
|
410
|
+
q = Depq.new
|
411
|
+
q.insert 1
|
412
|
+
q2 = q.dup
|
413
|
+
q.validation
|
414
|
+
q2.validation
|
415
|
+
q.insert 2
|
416
|
+
q2.validation
|
417
|
+
assert_equal(1, q2.delete_min)
|
418
|
+
q.validation
|
419
|
+
q2.validation
|
420
|
+
assert_equal(nil, q2.delete_min)
|
421
|
+
q.validation
|
422
|
+
q2.validation
|
423
|
+
assert_equal(1, q.delete_min)
|
424
|
+
q.validation
|
425
|
+
q2.validation
|
426
|
+
assert_equal(2, q.delete_min)
|
427
|
+
assert_equal(nil, q.delete_min)
|
412
428
|
end
|
413
429
|
|
414
430
|
def test_marshal
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
assert_equal(1,
|
423
|
-
|
424
|
-
|
425
|
-
assert_equal(nil,
|
426
|
-
|
427
|
-
|
428
|
-
assert_equal(1,
|
429
|
-
|
430
|
-
|
431
|
-
assert_equal(2,
|
432
|
-
assert_equal(nil,
|
431
|
+
q = Depq.new
|
432
|
+
q.insert 1
|
433
|
+
q2 = Marshal.load(Marshal.dump(q))
|
434
|
+
q.validation
|
435
|
+
q2.validation
|
436
|
+
q.insert 2
|
437
|
+
q2.validation
|
438
|
+
assert_equal(1, q2.delete_min)
|
439
|
+
q.validation
|
440
|
+
q2.validation
|
441
|
+
assert_equal(nil, q2.delete_min)
|
442
|
+
q.validation
|
443
|
+
q2.validation
|
444
|
+
assert_equal(1, q.delete_min)
|
445
|
+
q.validation
|
446
|
+
q2.validation
|
447
|
+
assert_equal(2, q.delete_min)
|
448
|
+
assert_equal(nil, q.delete_min)
|
433
449
|
end
|
434
450
|
|
435
451
|
def test_compare_priority
|
436
|
-
|
437
|
-
assert_operator(
|
438
|
-
assert_operator(
|
439
|
-
assert_operator(
|
440
|
-
|
441
|
-
assert_operator(
|
442
|
-
assert_operator(
|
443
|
-
assert_operator(
|
444
|
-
assert_operator(
|
445
|
-
assert_operator(
|
446
|
-
assert_operator(
|
447
|
-
assert_operator(
|
448
|
-
assert_operator(
|
449
|
-
assert_operator(
|
450
|
-
assert_operator(
|
451
|
-
assert_operator(
|
452
|
-
assert_operator(
|
453
|
-
|
454
|
-
assert_operator(
|
455
|
-
assert_operator(
|
456
|
-
assert_operator(
|
457
|
-
assert_operator(
|
458
|
-
assert_operator(
|
459
|
-
assert_operator(
|
460
|
-
assert_operator(
|
461
|
-
assert_operator(
|
462
|
-
assert_operator(
|
463
|
-
assert_operator(
|
464
|
-
assert_operator(
|
465
|
-
assert_operator(
|
466
|
-
assert_operator(
|
467
|
-
assert_operator(
|
468
|
-
assert_operator(
|
469
|
-
assert_operator(
|
452
|
+
q = Depq.new
|
453
|
+
assert_operator(q.compare_priority("a", "b"), :<, 0)
|
454
|
+
assert_operator(q.compare_priority("a", "a"), :==, 0)
|
455
|
+
assert_operator(q.compare_priority("b", "a"), :>, 0)
|
456
|
+
q = Depq.new(:casecmp)
|
457
|
+
assert_operator(q.compare_priority("a", "b"), :<, 0)
|
458
|
+
assert_operator(q.compare_priority("a", "B"), :<, 0)
|
459
|
+
assert_operator(q.compare_priority("A", "b"), :<, 0)
|
460
|
+
assert_operator(q.compare_priority("A", "B"), :<, 0)
|
461
|
+
assert_operator(q.compare_priority("a", "a"), :==, 0)
|
462
|
+
assert_operator(q.compare_priority("a", "A"), :==, 0)
|
463
|
+
assert_operator(q.compare_priority("A", "a"), :==, 0)
|
464
|
+
assert_operator(q.compare_priority("A", "A"), :==, 0)
|
465
|
+
assert_operator(q.compare_priority("b", "a"), :>, 0)
|
466
|
+
assert_operator(q.compare_priority("b", "A"), :>, 0)
|
467
|
+
assert_operator(q.compare_priority("B", "a"), :>, 0)
|
468
|
+
assert_operator(q.compare_priority("B", "A"), :>, 0)
|
469
|
+
q = Depq.new(lambda {|a,b| [a[1],a[0]] <=> [b[1],b[0]]})
|
470
|
+
assert_operator(q.compare_priority([0,0], [0,0]), :==, 0)
|
471
|
+
assert_operator(q.compare_priority([0,0], [0,1]), :<, 0)
|
472
|
+
assert_operator(q.compare_priority([0,0], [1,0]), :<, 0)
|
473
|
+
assert_operator(q.compare_priority([0,0], [1,1]), :<, 0)
|
474
|
+
assert_operator(q.compare_priority([0,1], [0,0]), :>, 0)
|
475
|
+
assert_operator(q.compare_priority([0,1], [0,1]), :==, 0)
|
476
|
+
assert_operator(q.compare_priority([0,1], [1,0]), :>, 0)
|
477
|
+
assert_operator(q.compare_priority([0,1], [1,1]), :<, 0)
|
478
|
+
assert_operator(q.compare_priority([1,0], [0,0]), :>, 0)
|
479
|
+
assert_operator(q.compare_priority([1,0], [0,1]), :<, 0)
|
480
|
+
assert_operator(q.compare_priority([1,0], [1,0]), :==, 0)
|
481
|
+
assert_operator(q.compare_priority([1,0], [1,1]), :<, 0)
|
482
|
+
assert_operator(q.compare_priority([1,1], [0,0]), :>, 0)
|
483
|
+
assert_operator(q.compare_priority([1,1], [0,1]), :>, 0)
|
484
|
+
assert_operator(q.compare_priority([1,1], [1,0]), :>, 0)
|
485
|
+
assert_operator(q.compare_priority([1,1], [1,1]), :==, 0)
|
470
486
|
end
|
471
487
|
|
472
488
|
def test_empty?
|
473
|
-
|
474
|
-
assert(
|
475
|
-
|
476
|
-
assert(!
|
489
|
+
q = Depq.new
|
490
|
+
assert(q.empty?)
|
491
|
+
q.insert 1
|
492
|
+
assert(!q.empty?)
|
477
493
|
end
|
478
494
|
|
479
495
|
def test_size
|
480
|
-
|
481
|
-
|
482
|
-
assert_equal(1,
|
483
|
-
|
484
|
-
assert_equal(2,
|
485
|
-
|
486
|
-
assert_equal(3,
|
487
|
-
|
488
|
-
assert_equal(2,
|
496
|
+
q = Depq.new
|
497
|
+
q.insert 1
|
498
|
+
assert_equal(1, q.size)
|
499
|
+
q.insert 10
|
500
|
+
assert_equal(2, q.size)
|
501
|
+
q.insert 2
|
502
|
+
assert_equal(3, q.size)
|
503
|
+
q.delete_max
|
504
|
+
assert_equal(2, q.size)
|
489
505
|
end
|
490
506
|
|
491
507
|
def test_totalcount
|
492
|
-
|
493
|
-
assert_equal(0,
|
494
|
-
|
495
|
-
assert_equal(1,
|
496
|
-
|
497
|
-
assert_equal(2,
|
498
|
-
|
499
|
-
assert_equal(2,
|
500
|
-
|
501
|
-
assert_equal(3,
|
502
|
-
|
503
|
-
assert_equal(4,
|
504
|
-
|
505
|
-
assert_equal(5,
|
506
|
-
|
507
|
-
assert_equal(5,
|
508
|
-
|
509
|
-
assert_equal(6,
|
508
|
+
q = Depq.new
|
509
|
+
assert_equal(0, q.totalcount)
|
510
|
+
q.insert 1
|
511
|
+
assert_equal(1, q.totalcount)
|
512
|
+
q.insert 2
|
513
|
+
assert_equal(2, q.totalcount)
|
514
|
+
q.delete_min
|
515
|
+
assert_equal(2, q.totalcount)
|
516
|
+
q.insert 4
|
517
|
+
assert_equal(3, q.totalcount)
|
518
|
+
q.insert 3
|
519
|
+
assert_equal(4, q.totalcount)
|
520
|
+
q.insert 0
|
521
|
+
assert_equal(5, q.totalcount)
|
522
|
+
q.delete_min
|
523
|
+
assert_equal(5, q.totalcount)
|
524
|
+
q.insert 2
|
525
|
+
assert_equal(6, q.totalcount)
|
510
526
|
end
|
511
527
|
|
512
528
|
def test_clear
|
513
|
-
|
514
|
-
|
515
|
-
assert(!
|
516
|
-
|
517
|
-
assert(
|
529
|
+
q = Depq.new
|
530
|
+
q.insert 1
|
531
|
+
assert(!q.empty?)
|
532
|
+
q.clear
|
533
|
+
assert(q.empty?)
|
518
534
|
end
|
519
535
|
|
520
536
|
def test_insert
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
assert_equal(1,
|
526
|
-
assert_equal(2,
|
527
|
-
assert_equal(10,
|
537
|
+
q = Depq.new
|
538
|
+
q.insert 1
|
539
|
+
q.insert 10
|
540
|
+
q.insert 2
|
541
|
+
assert_equal(1, q.delete_min)
|
542
|
+
assert_equal(2, q.delete_min)
|
543
|
+
assert_equal(10, q.delete_min)
|
528
544
|
end
|
529
545
|
|
530
546
|
def test_insert_all
|
531
|
-
|
532
|
-
|
533
|
-
assert_equal(1,
|
534
|
-
assert_equal(2,
|
535
|
-
assert_equal(3,
|
536
|
-
assert_equal(nil,
|
547
|
+
q = Depq.new
|
548
|
+
q.insert_all [3,1,2]
|
549
|
+
assert_equal(1, q.delete_min)
|
550
|
+
assert_equal(2, q.delete_min)
|
551
|
+
assert_equal(3, q.delete_min)
|
552
|
+
assert_equal(nil, q.delete_min)
|
537
553
|
end
|
538
554
|
|
539
555
|
def test_find_min_locator
|
540
|
-
|
541
|
-
|
542
|
-
loc =
|
556
|
+
q = Depq.new
|
557
|
+
q.insert 1
|
558
|
+
loc = q.find_min_locator
|
543
559
|
assert_equal(1, loc.value)
|
544
560
|
assert_equal(1, loc.priority)
|
545
|
-
assert_equal(
|
546
|
-
assert_equal(1,
|
547
|
-
assert_equal(nil, loc.
|
561
|
+
assert_equal(q, loc.depq)
|
562
|
+
assert_equal(1, q.delete_min)
|
563
|
+
assert_equal(nil, loc.depq)
|
548
564
|
end
|
549
565
|
|
550
566
|
def test_find_min
|
551
|
-
|
552
|
-
|
553
|
-
assert_equal(1,
|
567
|
+
q = Depq.new
|
568
|
+
q.insert 1
|
569
|
+
assert_equal(1, q.find_min)
|
554
570
|
end
|
555
571
|
|
556
572
|
def test_find_min_priority
|
557
|
-
|
558
|
-
|
559
|
-
assert_equal(["a", 1],
|
560
|
-
|
561
|
-
assert_equal(nil,
|
573
|
+
q = Depq.new
|
574
|
+
q.insert "a", 1
|
575
|
+
assert_equal(["a", 1], q.find_min_priority)
|
576
|
+
q.delete_min
|
577
|
+
assert_equal(nil, q.find_min_priority)
|
562
578
|
end
|
563
579
|
|
564
580
|
def test_find_max_locator
|
565
|
-
|
566
|
-
|
567
|
-
loc =
|
581
|
+
q = Depq.new
|
582
|
+
q.insert 1
|
583
|
+
loc = q.find_max_locator
|
568
584
|
assert_equal(1, loc.value)
|
569
585
|
assert_equal(1, loc.priority)
|
570
|
-
assert_equal(
|
586
|
+
assert_equal(q, loc.depq)
|
571
587
|
end
|
572
588
|
|
573
589
|
def test_find_max
|
574
|
-
|
575
|
-
|
576
|
-
assert_equal(1,
|
590
|
+
q = Depq.new
|
591
|
+
q.insert 1
|
592
|
+
assert_equal(1, q.find_max)
|
577
593
|
end
|
578
594
|
|
579
595
|
def test_find_max_priority
|
580
|
-
|
581
|
-
|
582
|
-
assert_equal(["a", 1],
|
583
|
-
|
584
|
-
assert_equal(nil,
|
596
|
+
q = Depq.new
|
597
|
+
q.insert "a", 1
|
598
|
+
assert_equal(["a", 1], q.find_max_priority)
|
599
|
+
q.delete_max
|
600
|
+
assert_equal(nil, q.find_max_priority)
|
585
601
|
end
|
586
602
|
|
587
603
|
def test_find_minmax_locator
|
588
|
-
|
589
|
-
assert_equal([nil, nil],
|
590
|
-
loc3 =
|
591
|
-
|
592
|
-
|
593
|
-
|
604
|
+
q = Depq.new
|
605
|
+
assert_equal([nil, nil], q.find_minmax_locator)
|
606
|
+
loc3 = q.insert 3
|
607
|
+
assert_equal([loc3, loc3], q.find_minmax_locator)
|
608
|
+
loc1 = q.insert 1
|
609
|
+
q.insert 2
|
610
|
+
res = q.find_minmax_locator
|
594
611
|
assert_equal([loc1, loc3], res)
|
595
612
|
end
|
596
613
|
|
614
|
+
def test_find_minmax_locator2
|
615
|
+
q = Depq.new
|
616
|
+
loc1 = q.insert 10
|
617
|
+
assert_equal([loc1, loc1], q.find_minmax_locator)
|
618
|
+
loc2 = q.insert 10
|
619
|
+
assert_equal([loc1, loc1], q.find_minmax_locator)
|
620
|
+
end
|
621
|
+
|
597
622
|
def test_find_minmax
|
598
|
-
|
599
|
-
assert_equal([nil, nil],
|
600
|
-
|
601
|
-
|
602
|
-
|
603
|
-
res =
|
623
|
+
q = Depq.new
|
624
|
+
assert_equal([nil, nil], q.find_minmax)
|
625
|
+
q.insert 3
|
626
|
+
q.insert 1
|
627
|
+
q.insert 2
|
628
|
+
res = q.find_minmax
|
604
629
|
assert_equal([1, 3], res)
|
605
630
|
end
|
606
631
|
|
607
632
|
def test_find_minmax_after_min
|
608
|
-
|
609
|
-
assert_equal([nil, nil],
|
610
|
-
|
611
|
-
|
612
|
-
|
613
|
-
assert_equal(1,
|
614
|
-
res =
|
633
|
+
q = Depq.new
|
634
|
+
assert_equal([nil, nil], q.find_minmax)
|
635
|
+
q.insert 3
|
636
|
+
q.insert 1
|
637
|
+
q.insert 2
|
638
|
+
assert_equal(1, q.min)
|
639
|
+
res = q.find_minmax
|
615
640
|
assert_equal([1, 3], res)
|
616
641
|
end
|
617
642
|
|
618
643
|
def test_find_minmax_after_max
|
619
|
-
|
620
|
-
assert_equal([nil, nil],
|
621
|
-
|
622
|
-
|
623
|
-
|
624
|
-
assert_equal(3,
|
625
|
-
res =
|
644
|
+
q = Depq.new
|
645
|
+
assert_equal([nil, nil], q.find_minmax)
|
646
|
+
q.insert 3
|
647
|
+
q.insert 1
|
648
|
+
q.insert 2
|
649
|
+
assert_equal(3, q.max)
|
650
|
+
res = q.find_minmax
|
626
651
|
assert_equal([1, 3], res)
|
627
652
|
end
|
628
653
|
|
629
654
|
def test_delete_locator
|
630
|
-
|
631
|
-
loc =
|
632
|
-
|
633
|
-
assert(
|
634
|
-
|
635
|
-
loc =
|
636
|
-
|
637
|
-
|
638
|
-
assert_equal(1,
|
639
|
-
|
640
|
-
assert_equal(1,
|
641
|
-
assert_equal(3,
|
655
|
+
q = Depq.new
|
656
|
+
loc = q.insert 1
|
657
|
+
q.delete_locator loc
|
658
|
+
assert(q.empty?)
|
659
|
+
q = Depq.new
|
660
|
+
loc = q.insert 2
|
661
|
+
q.insert 3
|
662
|
+
q.insert 1
|
663
|
+
assert_equal(1, q.find_min)
|
664
|
+
q.delete_locator(loc)
|
665
|
+
assert_equal(1, q.delete_min)
|
666
|
+
assert_equal(3, q.delete_min)
|
667
|
+
end
|
668
|
+
|
669
|
+
def test_delete_locator_err
|
670
|
+
q = Depq.new
|
671
|
+
loc = q.insert 1
|
672
|
+
q2 = Depq.new
|
673
|
+
assert_raise(ArgumentError) { q2.delete_locator(loc) }
|
642
674
|
end
|
643
675
|
|
644
676
|
def test_delete_min
|
645
|
-
|
646
|
-
|
647
|
-
|
648
|
-
|
649
|
-
assert_equal(0,
|
650
|
-
assert_equal(1,
|
651
|
-
assert_equal(2,
|
652
|
-
assert_equal(nil,
|
677
|
+
q = Depq.new
|
678
|
+
q.insert 1
|
679
|
+
q.insert 2
|
680
|
+
q.insert 0
|
681
|
+
assert_equal(0, q.delete_min)
|
682
|
+
assert_equal(1, q.delete_min)
|
683
|
+
assert_equal(2, q.delete_min)
|
684
|
+
assert_equal(nil, q.delete_min)
|
685
|
+
end
|
686
|
+
|
687
|
+
def test_delete_min_priority
|
688
|
+
q = Depq.new
|
689
|
+
q.insert "apple", 1
|
690
|
+
q.insert "durian", 2
|
691
|
+
q.insert "banana", 0
|
692
|
+
assert_equal(["banana", 0], q.delete_min_priority)
|
693
|
+
assert_equal(["apple", 1], q.delete_min_priority)
|
694
|
+
assert_equal(["durian", 2], q.delete_min_priority)
|
695
|
+
assert_equal(nil, q.delete_min_priority)
|
653
696
|
end
|
654
697
|
|
655
698
|
def test_delete_min_locator
|
656
|
-
|
657
|
-
loc1 =
|
658
|
-
loc2 =
|
659
|
-
loc0 =
|
660
|
-
assert_equal(loc0,
|
661
|
-
assert_equal(loc1,
|
662
|
-
assert_equal(loc2,
|
663
|
-
assert_equal(nil,
|
699
|
+
q = Depq.new
|
700
|
+
loc1 = q.insert 1
|
701
|
+
loc2 = q.insert 2
|
702
|
+
loc0 = q.insert 0
|
703
|
+
assert_equal(loc0, q.delete_min_locator)
|
704
|
+
assert_equal(loc1, q.delete_min_locator)
|
705
|
+
assert_equal(loc2, q.delete_min_locator)
|
706
|
+
assert_equal(nil, q.delete_min_locator)
|
664
707
|
end
|
665
708
|
|
666
709
|
def test_delete_max
|
667
|
-
|
668
|
-
|
669
|
-
|
670
|
-
|
671
|
-
assert_equal(2,
|
672
|
-
assert_equal(1,
|
673
|
-
assert_equal(0,
|
674
|
-
assert_equal(nil,
|
710
|
+
q = Depq.new
|
711
|
+
q.insert 1
|
712
|
+
q.insert 2
|
713
|
+
q.insert 0
|
714
|
+
assert_equal(2, q.delete_max)
|
715
|
+
assert_equal(1, q.delete_max)
|
716
|
+
assert_equal(0, q.delete_max)
|
717
|
+
assert_equal(nil, q.delete_max)
|
718
|
+
end
|
719
|
+
|
720
|
+
def test_delete_max_priority
|
721
|
+
q = Depq.new
|
722
|
+
q.insert "apple", 1
|
723
|
+
q.insert "durian", 2
|
724
|
+
q.insert "banana", 0
|
725
|
+
assert_equal(["durian", 2], q.delete_max_priority)
|
726
|
+
assert_equal(["apple", 1], q.delete_max_priority)
|
727
|
+
assert_equal(["banana", 0], q.delete_max_priority)
|
728
|
+
assert_equal(nil, q.delete_max)
|
675
729
|
end
|
676
730
|
|
677
731
|
def test_delete_max_locator
|
678
|
-
|
679
|
-
loc1 =
|
680
|
-
loc2 =
|
681
|
-
loc0 =
|
682
|
-
assert_equal(loc2,
|
683
|
-
assert_equal(loc1,
|
684
|
-
assert_equal(loc0,
|
685
|
-
assert_equal(nil,
|
732
|
+
q = Depq.new
|
733
|
+
loc1 = q.insert 1
|
734
|
+
loc2 = q.insert 2
|
735
|
+
loc0 = q.insert 0
|
736
|
+
assert_equal(loc2, q.delete_max_locator)
|
737
|
+
assert_equal(loc1, q.delete_max_locator)
|
738
|
+
assert_equal(loc0, q.delete_max_locator)
|
739
|
+
assert_equal(nil, q.delete_max)
|
740
|
+
end
|
741
|
+
|
742
|
+
def test_delete_max_after_insert
|
743
|
+
q = Depq.new
|
744
|
+
q.insert 1
|
745
|
+
q.insert 2
|
746
|
+
q.insert 0
|
747
|
+
assert_equal(2, q.delete_max)
|
748
|
+
q.insert 3
|
749
|
+
assert_equal(3, q.delete_max)
|
750
|
+
end
|
751
|
+
|
752
|
+
def test_minmax_after_insert
|
753
|
+
q = Depq.new
|
754
|
+
q.insert 2
|
755
|
+
q.insert 3
|
756
|
+
q.insert 1
|
757
|
+
assert_equal([1,3], q.minmax)
|
758
|
+
q.insert 10
|
759
|
+
q.insert 0
|
760
|
+
assert_equal([0,10], q.minmax)
|
761
|
+
end
|
762
|
+
|
763
|
+
def test_stable_minmax
|
764
|
+
q = Depq.new
|
765
|
+
q.insert :a, 2
|
766
|
+
q.insert :b, 2
|
767
|
+
q.insert :c, 1
|
768
|
+
q.insert :d, 1
|
769
|
+
q.insert :e, 2
|
770
|
+
q.insert :f, 1
|
771
|
+
q.insert :g, 2
|
772
|
+
q.insert :h, 1
|
773
|
+
assert_equal([:c, :a], q.minmax)
|
774
|
+
end
|
775
|
+
|
776
|
+
def test_stable_minmax2
|
777
|
+
q = Depq.new
|
778
|
+
q.insert :a, 1, 0
|
779
|
+
q.insert :b, 2, 1
|
780
|
+
q.insert :c, 2, 2
|
781
|
+
assert_equal([:a, :b], q.minmax)
|
782
|
+
q.insert :d, 3, 3
|
783
|
+
assert_equal([:a, :d], q.minmax)
|
784
|
+
end
|
785
|
+
|
786
|
+
def test_minmax_upheap_minside
|
787
|
+
q = Depq.new
|
788
|
+
q.insert :a, 1, 0
|
789
|
+
q.insert :b, 2, 1
|
790
|
+
assert_equal([:a, :b], q.minmax)
|
791
|
+
q.insert :c, 0, 2
|
792
|
+
assert_equal([:c, :b], q.minmax)
|
793
|
+
end
|
794
|
+
|
795
|
+
def test_minmax_upheap_minside2
|
796
|
+
q = Depq.new
|
797
|
+
q.insert :a, 1, 0
|
798
|
+
q.insert :b, 2, 1
|
799
|
+
assert_equal([:a, :b], q.minmax)
|
800
|
+
q.insert :c, 2, 2
|
801
|
+
assert_equal([:a, :b], q.minmax)
|
802
|
+
end
|
803
|
+
|
804
|
+
def test_minmax_downheap_minside
|
805
|
+
q = Depq.new
|
806
|
+
q.insert :a, 1
|
807
|
+
q.insert :b, 9
|
808
|
+
q.insert :c, 2
|
809
|
+
q.insert :d, 5
|
810
|
+
q.insert :e, 6
|
811
|
+
q.insert :f, 7
|
812
|
+
assert_equal([:a, :b], q.minmax)
|
813
|
+
assert_equal(:a, q.delete_min)
|
814
|
+
assert_equal(:c, q.delete_min)
|
815
|
+
assert_equal(:d, q.delete_min)
|
816
|
+
assert_equal(:e, q.delete_min)
|
817
|
+
assert_equal(:f, q.delete_min)
|
818
|
+
assert_equal(:b, q.delete_min)
|
819
|
+
assert_equal(nil, q.delete_min)
|
820
|
+
end
|
821
|
+
|
822
|
+
def test_minmax_downheap_minside2
|
823
|
+
q = Depq.new
|
824
|
+
q.insert :a, 1
|
825
|
+
q.insert :b, 9
|
826
|
+
q.insert :c, 2, 10
|
827
|
+
q.insert :d, 5
|
828
|
+
q.insert :e, 2, 9
|
829
|
+
q.insert :f, 7
|
830
|
+
assert_equal([:a, :b], q.minmax)
|
831
|
+
assert_equal(:a, q.delete_min)
|
832
|
+
assert_equal(:e, q.delete_min)
|
833
|
+
assert_equal(:c, q.delete_min)
|
834
|
+
assert_equal(:d, q.delete_min)
|
835
|
+
assert_equal(:f, q.delete_min)
|
836
|
+
assert_equal(:b, q.delete_min)
|
837
|
+
assert_equal(nil, q.delete_min)
|
838
|
+
end
|
839
|
+
|
840
|
+
def test_minmax_downheap_maxside
|
841
|
+
q = Depq.new
|
842
|
+
q.insert :a, 1
|
843
|
+
q.insert :b, 9
|
844
|
+
q.insert :c, 2
|
845
|
+
q.insert :d, 7
|
846
|
+
q.insert :e, 6
|
847
|
+
q.insert :f, 5
|
848
|
+
assert_equal([:a, :b], q.minmax)
|
849
|
+
assert_equal(:b, q.delete_max)
|
850
|
+
assert_equal(:d, q.delete_max)
|
851
|
+
assert_equal(:e, q.delete_max)
|
852
|
+
assert_equal(:f, q.delete_max)
|
853
|
+
assert_equal(:c, q.delete_max)
|
854
|
+
assert_equal(:a, q.delete_max)
|
855
|
+
assert_equal(nil, q.delete_max)
|
856
|
+
end
|
857
|
+
|
858
|
+
def test_minmax_downheap_maxside2
|
859
|
+
q = Depq.new
|
860
|
+
q.insert :a, 1
|
861
|
+
q.insert :b, 9
|
862
|
+
q.insert :c, 2
|
863
|
+
q.insert :d, 7
|
864
|
+
q.insert :e, 6
|
865
|
+
q.insert :f, 7
|
866
|
+
assert_equal([:a, :b], q.minmax)
|
867
|
+
assert_equal(:b, q.delete_max)
|
868
|
+
assert_equal(:d, q.delete_max)
|
869
|
+
assert_equal(:f, q.delete_max)
|
870
|
+
assert_equal(:e, q.delete_max)
|
871
|
+
assert_equal(:c, q.delete_max)
|
872
|
+
assert_equal(:a, q.delete_max)
|
873
|
+
assert_equal(nil, q.delete_max)
|
874
|
+
end
|
875
|
+
|
876
|
+
def test_minmax_upheap_sub
|
877
|
+
q = Depq.new
|
878
|
+
q.insert :a, 1
|
879
|
+
q.insert :b, 2
|
880
|
+
assert_equal([:a, :b], q.minmax)
|
881
|
+
q.insert :c, 1
|
882
|
+
assert_equal([:a, :b], q.minmax)
|
883
|
+
end
|
884
|
+
|
885
|
+
def test_minmax_downheap_sub
|
886
|
+
q = Depq.new
|
887
|
+
q.insert :a, 1
|
888
|
+
q.insert :b, 5
|
889
|
+
q.insert :c, 2
|
890
|
+
q.insert :d, 3
|
891
|
+
q.insert :e, 5
|
892
|
+
q.insert :f, 5
|
893
|
+
assert_equal([:a, :b], q.minmax)
|
894
|
+
end
|
895
|
+
|
896
|
+
def test_minmax_adjust
|
897
|
+
q = Depq.new
|
898
|
+
q.insert :a, 1
|
899
|
+
q.insert :b, 5
|
900
|
+
q.insert :c, 2
|
901
|
+
assert_equal([:a, :b], q.minmax)
|
902
|
+
q.insert :d, 1
|
903
|
+
assert_equal([:a, :b], q.minmax)
|
686
904
|
end
|
687
905
|
|
688
906
|
def test_delete_unspecified
|
689
|
-
|
907
|
+
q = Depq.new
|
690
908
|
a1 = [1,2,0]
|
691
909
|
a1.each {|v|
|
692
|
-
|
910
|
+
q.insert v
|
693
911
|
}
|
694
912
|
a2 = []
|
695
913
|
a1.length.times {
|
696
|
-
a2 <<
|
914
|
+
a2 << q.delete_unspecified
|
697
915
|
}
|
698
916
|
assert_equal(a1.sort, a2.sort)
|
699
|
-
assert_equal(nil,
|
917
|
+
assert_equal(nil, q.delete_unspecified_locator)
|
700
918
|
end
|
701
919
|
|
702
920
|
def test_delete_unspecified_priority
|
703
|
-
|
921
|
+
q = Depq.new
|
704
922
|
a1 = [[1,8],[2,3],[0,5]]
|
705
923
|
a1.each {|val, priority|
|
706
|
-
|
924
|
+
q.insert val, priority
|
707
925
|
}
|
708
926
|
a2 = []
|
709
927
|
a1.length.times {
|
710
|
-
a2 <<
|
928
|
+
a2 << q.delete_unspecified_priority
|
711
929
|
}
|
712
930
|
assert_equal(a1.sort, a2.sort)
|
713
|
-
assert_equal(nil,
|
931
|
+
assert_equal(nil, q.delete_unspecified_locator)
|
714
932
|
end
|
715
933
|
|
716
934
|
def test_delete_unspecified_locator
|
717
|
-
|
935
|
+
q = Depq.new
|
718
936
|
a1 = [1,2,0]
|
719
937
|
a1.each {|v|
|
720
|
-
|
938
|
+
q.insert v
|
721
939
|
}
|
722
940
|
a2 = []
|
723
941
|
a1.length.times {
|
724
|
-
a2 <<
|
942
|
+
a2 << q.delete_unspecified_locator.value
|
725
943
|
}
|
726
944
|
assert_equal(a1.sort, a2.sort)
|
727
|
-
assert_equal(nil,
|
945
|
+
assert_equal(nil, q.delete_unspecified_locator)
|
728
946
|
end
|
729
947
|
|
730
948
|
def test_each
|
731
|
-
|
949
|
+
q = Depq.new
|
732
950
|
a = [1,2,0]
|
733
951
|
a.each {|v|
|
734
|
-
|
952
|
+
q.insert v
|
735
953
|
}
|
736
|
-
|
954
|
+
q.each {|v|
|
737
955
|
assert(a.include? v)
|
738
956
|
}
|
739
957
|
end
|
740
958
|
|
741
959
|
def test_each_with_priority
|
742
|
-
|
960
|
+
q = Depq.new
|
743
961
|
h = {}
|
744
962
|
h["durian"] = 1
|
745
963
|
h["banana"] = 3
|
746
964
|
h["melon"] = 2
|
747
965
|
h.each {|val, prio|
|
748
|
-
|
966
|
+
q.insert val, prio
|
749
967
|
}
|
750
|
-
|
968
|
+
q.each_with_priority {|val, prio|
|
751
969
|
assert_equal(h[val], prio)
|
752
970
|
}
|
753
971
|
end
|
754
972
|
|
755
973
|
def test_each_locator
|
756
|
-
|
974
|
+
q = Depq.new
|
757
975
|
a = [1,2,0]
|
758
976
|
a.each {|v|
|
759
|
-
|
977
|
+
q.insert v
|
760
978
|
}
|
761
|
-
|
979
|
+
q.each_locator {|loc|
|
762
980
|
assert(a.include? loc.value)
|
763
981
|
}
|
764
982
|
end
|
@@ -794,25 +1012,38 @@ class TestDepq < Test::Unit::TestCase
|
|
794
1012
|
end
|
795
1013
|
|
796
1014
|
def test_merge_enumerator
|
797
|
-
|
798
|
-
assert_equal(1,
|
799
|
-
assert_equal(2,
|
800
|
-
assert_equal(3,
|
801
|
-
assert_equal(3,
|
802
|
-
assert_equal(4,
|
803
|
-
assert_equal(4,
|
804
|
-
assert_equal(5,
|
805
|
-
assert_equal(6,
|
806
|
-
assert_raise(StopIteration) {
|
1015
|
+
e = Depq.merge(1..4, 3..6)
|
1016
|
+
assert_equal(1, e.next)
|
1017
|
+
assert_equal(2, e.next)
|
1018
|
+
assert_equal(3, e.next)
|
1019
|
+
assert_equal(3, e.next)
|
1020
|
+
assert_equal(4, e.next)
|
1021
|
+
assert_equal(4, e.next)
|
1022
|
+
assert_equal(5, e.next)
|
1023
|
+
assert_equal(6, e.next)
|
1024
|
+
assert_raise(StopIteration) { e.next }
|
807
1025
|
end
|
808
1026
|
|
809
1027
|
def test_merge_enumerator2
|
810
|
-
|
1028
|
+
e = Depq.merge(1..4, 3..6)
|
811
1029
|
a = []
|
812
|
-
|
1030
|
+
e.each_slice(2) {|x|
|
813
1031
|
a << x
|
814
1032
|
}
|
815
1033
|
assert_equal([[1,2],[3,3],[4,4],[5,6]], a)
|
816
1034
|
end
|
817
1035
|
|
1036
|
+
def test_merge_empty
|
1037
|
+
e = Depq.merge(1..4, 2...2, 3..6)
|
1038
|
+
assert_equal(1, e.next)
|
1039
|
+
assert_equal(2, e.next)
|
1040
|
+
assert_equal(3, e.next)
|
1041
|
+
assert_equal(3, e.next)
|
1042
|
+
assert_equal(4, e.next)
|
1043
|
+
assert_equal(4, e.next)
|
1044
|
+
assert_equal(5, e.next)
|
1045
|
+
assert_equal(6, e.next)
|
1046
|
+
assert_raise(StopIteration) { e.next }
|
1047
|
+
end
|
1048
|
+
|
818
1049
|
end
|