depq 0.2 → 0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. data/README +59 -47
  2. data/depq.rb +452 -470
  3. data/test-depq.rb +599 -368
  4. metadata +1 -1
@@ -29,16 +29,16 @@ require 'test/unit'
29
29
 
30
30
  class Depq
31
31
  module SimpleHeap
32
- def validation(pd, ary)
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?(pd, ary, i, j)
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?(pd, ary, i, k)
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(pd, ary)
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(pd, ary, imin, j) > 0
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(pd, ary, imax, j) < 0
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(pd, ary, imin, j) == 0 && scmp(pd, ary, imin, j) > 0
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(pd, ary, imax, j) == 0 && scmp(pd, ary, imax, j) > 0
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(pd, ary, jmin, j) == 0 && scmp(pd, ary, jmin, j) > 0
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.pdeque
87
- raise "pdeque mismatch"
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
- pd = Depq.new
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
- pd.insert(r)
114
+ q.insert(r)
115
115
  if incremental
116
- pd.send find
117
- pd.validation
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 << pd.send(delete)
124
- pd.validation
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
- pd = Depq.new
139
+ q = Depq.new
140
140
  a1.each {|v|
141
- pd.insert v
141
+ q.insert v
142
142
  if incremental
143
- pd.find_min
144
- pd.validation
143
+ q.find_min
144
+ q.validation
145
145
  end
146
146
  }
147
- pd.find_min
148
- pd.validation
147
+ q.find_min
148
+ q.validation
149
149
  a0.each {|v|
150
- assert_equal(v, pd.delete_min)
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
- pd = Depq.new
167
+ q = Depq.new
168
168
  a1.each_with_index {|v,i|
169
- pd.insert v
169
+ q.insert v
170
170
  #log << v
171
171
  if n[i] != 0
172
- pd.find_min
173
- pd.validation
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, pd.delete_min)
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
- pd = Depq.new
193
- pd.insert "a", 0
194
- pd.insert "b", 0
195
- pd.insert "c", 0
196
- assert_equal("a", pd.delete_min)
197
- assert_equal("b", pd.delete_min)
198
- assert_equal("c", pd.delete_min)
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
- pd = Depq.new
203
- pd.insert "a", 0
204
- pd.insert "b", 0
205
- pd.insert "c", 0
206
- assert_equal("a", pd.delete_max)
207
- assert_equal("b", pd.delete_max)
208
- assert_equal("c", pd.delete_max)
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
- pd = Depq.new
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
- pd.insert_locator loc1
220
- pd.insert_locator loc2
221
- assert_equal(loc1, pd.delete_min_locator)
222
- assert_equal(loc2, pd.delete_min_locator)
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
- pd = Depq.new
233
- loc2 = pd.insert(Object.new, 2)
234
- loc1 = pd.insert(Object.new, 1)
235
- loc3 = pd.insert(Object.new, 3)
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
- pd.delete_locator(loc1)
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
- pd = Depq.new
245
- a = pd.insert("a", 2)
246
- b = pd.insert("b", 1)
247
- c = pd.insert("c", 3)
248
- assert_equal(b, pd.find_min_locator)
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, pd.find_min_locator)
256
+ assert_equal(a, q.find_min_locator)
252
257
  a.update("e", 10)
253
- assert_equal("b", pd.delete_min)
254
- assert_equal("c", pd.delete_min)
255
- assert_equal("e", pd.delete_min)
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
- pd = Depq.new
263
- a = pd.insert("a", 1, 0)
264
- b = pd.insert("b", 2, 1)
265
- c = pd.insert("c", 1, 2)
266
- d = pd.insert("d", 2, 3)
267
- e = pd.insert("e", 1, 4)
268
- f = pd.insert("f", 2, 5)
269
- assert_equal(a, pd.find_min_locator)
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, pd.find_min_locator)
276
+ assert_equal(c, q.find_min_locator)
272
277
  a.update("aa", 1, 1)
273
- assert_equal(a, pd.find_min_locator)
274
- pd.delete_locator a
275
- assert_equal(c, pd.find_min_locator)
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
- pd = Depq.new
284
- a = pd.insert("a", 1, 0)
285
- b = pd.insert("b", 2, 1)
286
- c = pd.insert("c", 1, 2)
287
- d = pd.insert("d", 2, 3)
288
- e = pd.insert("e", 1, 4)
289
- f = pd.insert("f", 2, 5)
290
- assert_equal(b, pd.find_max_locator)
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, pd.find_max_locator)
297
+ assert_equal(d, q.find_max_locator)
293
298
  b.update("bb", 2, 0)
294
- assert_equal(b, pd.find_max_locator)
295
- pd.delete_locator b
296
- assert_equal(d, pd.find_max_locator)
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
- pd = Depq.new
305
- a = pd.insert("a", 2)
306
- b = pd.insert("b", 1)
307
- c = pd.insert("c", 3)
308
- assert_equal(c, pd.find_max_locator)
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, pd.find_max_locator)
316
+ assert_equal(b, q.find_max_locator)
312
317
  b.update("e", 0)
313
- assert_equal("c", pd.delete_max)
314
- assert_equal("a", pd.delete_max)
315
- assert_equal("e", pd.delete_max)
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
- pd = Depq.new
320
- loc = pd.insert 1, 2, 3
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
- pd = Depq.new
332
- loc = pd.insert 1, 2, 3
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
- pd.delete_min
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
- pd = Depq.new
360
- loc1 = pd.insert(Object.new, 1, 11)
361
- loc2 = pd.insert(Object.new, 2, 12)
362
- loc3 = pd.insert(Object.new, 3, 13)
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
- pd.delete_locator(loc1)
385
+ q.delete_locator(loc1)
370
386
  assert_equal(11, loc1.subpriority)
371
387
  end
372
388
 
373
389
  def test_new
374
- pd = Depq.new
375
- pd.insert "Foo"
376
- pd.insert "bar"
377
- assert_equal("Foo", pd.delete_min)
378
- assert_equal("bar", pd.delete_min)
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
- pd = Depq.new(:casecmp)
381
- pd.insert "Foo"
382
- pd.insert "bar"
383
- assert_equal("bar", pd.delete_min)
384
- assert_equal("Foo", pd.delete_min)
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
- pd = Depq.new(lambda {|a,b| a.casecmp(b) })
387
- pd.insert "Foo"
388
- pd.insert "bar"
389
- assert_equal("bar", pd.delete_min)
390
- assert_equal("Foo", pd.delete_min)
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
- pd = Depq.new
395
- pd.insert 1
396
- pd2 = pd.dup
397
- pd.validation
398
- pd2.validation
399
- pd.insert 2
400
- pd2.validation
401
- assert_equal(1, pd2.delete_min)
402
- pd.validation
403
- pd2.validation
404
- assert_equal(nil, pd2.delete_min)
405
- pd.validation
406
- pd2.validation
407
- assert_equal(1, pd.delete_min)
408
- pd.validation
409
- pd2.validation
410
- assert_equal(2, pd.delete_min)
411
- assert_equal(nil, pd.delete_min)
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
- pd = Depq.new
416
- pd.insert 1
417
- pd2 = Marshal.load(Marshal.dump(pd))
418
- pd.validation
419
- pd2.validation
420
- pd.insert 2
421
- pd2.validation
422
- assert_equal(1, pd2.delete_min)
423
- pd.validation
424
- pd2.validation
425
- assert_equal(nil, pd2.delete_min)
426
- pd.validation
427
- pd2.validation
428
- assert_equal(1, pd.delete_min)
429
- pd.validation
430
- pd2.validation
431
- assert_equal(2, pd.delete_min)
432
- assert_equal(nil, pd.delete_min)
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
- pd = Depq.new
437
- assert_operator(pd.compare_priority("a", "b"), :<, 0)
438
- assert_operator(pd.compare_priority("a", "a"), :==, 0)
439
- assert_operator(pd.compare_priority("b", "a"), :>, 0)
440
- pd = Depq.new(:casecmp)
441
- assert_operator(pd.compare_priority("a", "b"), :<, 0)
442
- assert_operator(pd.compare_priority("a", "B"), :<, 0)
443
- assert_operator(pd.compare_priority("A", "b"), :<, 0)
444
- assert_operator(pd.compare_priority("A", "B"), :<, 0)
445
- assert_operator(pd.compare_priority("a", "a"), :==, 0)
446
- assert_operator(pd.compare_priority("a", "A"), :==, 0)
447
- assert_operator(pd.compare_priority("A", "a"), :==, 0)
448
- assert_operator(pd.compare_priority("A", "A"), :==, 0)
449
- assert_operator(pd.compare_priority("b", "a"), :>, 0)
450
- assert_operator(pd.compare_priority("b", "A"), :>, 0)
451
- assert_operator(pd.compare_priority("B", "a"), :>, 0)
452
- assert_operator(pd.compare_priority("B", "A"), :>, 0)
453
- pd = Depq.new(lambda {|a,b| [a[1],a[0]] <=> [b[1],b[0]]})
454
- assert_operator(pd.compare_priority([0,0], [0,0]), :==, 0)
455
- assert_operator(pd.compare_priority([0,0], [0,1]), :<, 0)
456
- assert_operator(pd.compare_priority([0,0], [1,0]), :<, 0)
457
- assert_operator(pd.compare_priority([0,0], [1,1]), :<, 0)
458
- assert_operator(pd.compare_priority([0,1], [0,0]), :>, 0)
459
- assert_operator(pd.compare_priority([0,1], [0,1]), :==, 0)
460
- assert_operator(pd.compare_priority([0,1], [1,0]), :>, 0)
461
- assert_operator(pd.compare_priority([0,1], [1,1]), :<, 0)
462
- assert_operator(pd.compare_priority([1,0], [0,0]), :>, 0)
463
- assert_operator(pd.compare_priority([1,0], [0,1]), :<, 0)
464
- assert_operator(pd.compare_priority([1,0], [1,0]), :==, 0)
465
- assert_operator(pd.compare_priority([1,0], [1,1]), :<, 0)
466
- assert_operator(pd.compare_priority([1,1], [0,0]), :>, 0)
467
- assert_operator(pd.compare_priority([1,1], [0,1]), :>, 0)
468
- assert_operator(pd.compare_priority([1,1], [1,0]), :>, 0)
469
- assert_operator(pd.compare_priority([1,1], [1,1]), :==, 0)
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
- pd = Depq.new
474
- assert(pd.empty?)
475
- pd.insert 1
476
- assert(!pd.empty?)
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
- pd = Depq.new
481
- pd.insert 1
482
- assert_equal(1, pd.size)
483
- pd.insert 10
484
- assert_equal(2, pd.size)
485
- pd.insert 2
486
- assert_equal(3, pd.size)
487
- pd.delete_max
488
- assert_equal(2, pd.size)
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
- pd = Depq.new
493
- assert_equal(0, pd.totalcount)
494
- pd.insert 1
495
- assert_equal(1, pd.totalcount)
496
- pd.insert 2
497
- assert_equal(2, pd.totalcount)
498
- pd.delete_min
499
- assert_equal(2, pd.totalcount)
500
- pd.insert 4
501
- assert_equal(3, pd.totalcount)
502
- pd.insert 3
503
- assert_equal(4, pd.totalcount)
504
- pd.insert 0
505
- assert_equal(5, pd.totalcount)
506
- pd.delete_min
507
- assert_equal(5, pd.totalcount)
508
- pd.insert 2
509
- assert_equal(6, pd.totalcount)
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
- pd = Depq.new
514
- pd.insert 1
515
- assert(!pd.empty?)
516
- pd.clear
517
- assert(pd.empty?)
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
- pd = Depq.new
522
- pd.insert 1
523
- pd.insert 10
524
- pd.insert 2
525
- assert_equal(1, pd.delete_min)
526
- assert_equal(2, pd.delete_min)
527
- assert_equal(10, pd.delete_min)
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
- pd = Depq.new
532
- pd.insert_all [3,1,2]
533
- assert_equal(1, pd.delete_min)
534
- assert_equal(2, pd.delete_min)
535
- assert_equal(3, pd.delete_min)
536
- assert_equal(nil, pd.delete_min)
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
- pd = Depq.new
541
- pd.insert 1
542
- loc = pd.find_min_locator
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(pd, loc.pdeque)
546
- assert_equal(1, pd.delete_min)
547
- assert_equal(nil, loc.pdeque)
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
- pd = Depq.new
552
- pd.insert 1
553
- assert_equal(1, pd.find_min)
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
- pd = Depq.new
558
- pd.insert "a", 1
559
- assert_equal(["a", 1], pd.find_min_priority)
560
- pd.delete_min
561
- assert_equal(nil, pd.find_min_priority)
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
- pd = Depq.new
566
- pd.insert 1
567
- loc = pd.find_max_locator
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(pd, loc.pdeque)
586
+ assert_equal(q, loc.depq)
571
587
  end
572
588
 
573
589
  def test_find_max
574
- pd = Depq.new
575
- pd.insert 1
576
- assert_equal(1, pd.find_max)
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
- pd = Depq.new
581
- pd.insert "a", 1
582
- assert_equal(["a", 1], pd.find_max_priority)
583
- pd.delete_max
584
- assert_equal(nil, pd.find_max_priority)
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
- pd = Depq.new
589
- assert_equal([nil, nil], pd.find_minmax_locator)
590
- loc3 = pd.insert 3
591
- loc1 = pd.insert 1
592
- pd.insert 2
593
- res = pd.find_minmax_locator
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
- pd = Depq.new
599
- assert_equal([nil, nil], pd.find_minmax)
600
- pd.insert 3
601
- pd.insert 1
602
- pd.insert 2
603
- res = pd.find_minmax
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
- pd = Depq.new
609
- assert_equal([nil, nil], pd.find_minmax)
610
- pd.insert 3
611
- pd.insert 1
612
- pd.insert 2
613
- assert_equal(1, pd.min)
614
- res = pd.find_minmax
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
- pd = Depq.new
620
- assert_equal([nil, nil], pd.find_minmax)
621
- pd.insert 3
622
- pd.insert 1
623
- pd.insert 2
624
- assert_equal(3, pd.max)
625
- res = pd.find_minmax
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
- pd = Depq.new
631
- loc = pd.insert 1
632
- pd.delete_locator loc
633
- assert(pd.empty?)
634
- pd = Depq.new
635
- loc = pd.insert 2
636
- pd.insert 3
637
- pd.insert 1
638
- assert_equal(1, pd.find_min)
639
- pd.delete_locator(loc)
640
- assert_equal(1, pd.delete_min)
641
- assert_equal(3, pd.delete_min)
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
- pd = Depq.new
646
- pd.insert 1
647
- pd.insert 2
648
- pd.insert 0
649
- assert_equal(0, pd.delete_min)
650
- assert_equal(1, pd.delete_min)
651
- assert_equal(2, pd.delete_min)
652
- assert_equal(nil, pd.delete_min)
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
- pd = Depq.new
657
- loc1 = pd.insert 1
658
- loc2 = pd.insert 2
659
- loc0 = pd.insert 0
660
- assert_equal(loc0, pd.delete_min_locator)
661
- assert_equal(loc1, pd.delete_min_locator)
662
- assert_equal(loc2, pd.delete_min_locator)
663
- assert_equal(nil, pd.delete_min_locator)
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
- pd = Depq.new
668
- pd.insert 1
669
- pd.insert 2
670
- pd.insert 0
671
- assert_equal(2, pd.delete_max)
672
- assert_equal(1, pd.delete_max)
673
- assert_equal(0, pd.delete_max)
674
- assert_equal(nil, pd.delete_max)
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
- pd = Depq.new
679
- loc1 = pd.insert 1
680
- loc2 = pd.insert 2
681
- loc0 = pd.insert 0
682
- assert_equal(loc2, pd.delete_max_locator)
683
- assert_equal(loc1, pd.delete_max_locator)
684
- assert_equal(loc0, pd.delete_max_locator)
685
- assert_equal(nil, pd.delete_max)
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
- pd = Depq.new
907
+ q = Depq.new
690
908
  a1 = [1,2,0]
691
909
  a1.each {|v|
692
- pd.insert v
910
+ q.insert v
693
911
  }
694
912
  a2 = []
695
913
  a1.length.times {
696
- a2 << pd.delete_unspecified
914
+ a2 << q.delete_unspecified
697
915
  }
698
916
  assert_equal(a1.sort, a2.sort)
699
- assert_equal(nil, pd.delete_unspecified_locator)
917
+ assert_equal(nil, q.delete_unspecified_locator)
700
918
  end
701
919
 
702
920
  def test_delete_unspecified_priority
703
- pd = Depq.new
921
+ q = Depq.new
704
922
  a1 = [[1,8],[2,3],[0,5]]
705
923
  a1.each {|val, priority|
706
- pd.insert val, priority
924
+ q.insert val, priority
707
925
  }
708
926
  a2 = []
709
927
  a1.length.times {
710
- a2 << pd.delete_unspecified_priority
928
+ a2 << q.delete_unspecified_priority
711
929
  }
712
930
  assert_equal(a1.sort, a2.sort)
713
- assert_equal(nil, pd.delete_unspecified_locator)
931
+ assert_equal(nil, q.delete_unspecified_locator)
714
932
  end
715
933
 
716
934
  def test_delete_unspecified_locator
717
- pd = Depq.new
935
+ q = Depq.new
718
936
  a1 = [1,2,0]
719
937
  a1.each {|v|
720
- pd.insert v
938
+ q.insert v
721
939
  }
722
940
  a2 = []
723
941
  a1.length.times {
724
- a2 << pd.delete_unspecified_locator.value
942
+ a2 << q.delete_unspecified_locator.value
725
943
  }
726
944
  assert_equal(a1.sort, a2.sort)
727
- assert_equal(nil, pd.delete_unspecified_locator)
945
+ assert_equal(nil, q.delete_unspecified_locator)
728
946
  end
729
947
 
730
948
  def test_each
731
- pd = Depq.new
949
+ q = Depq.new
732
950
  a = [1,2,0]
733
951
  a.each {|v|
734
- pd.insert v
952
+ q.insert v
735
953
  }
736
- pd.each {|v|
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
- pd = Depq.new
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
- pd.insert val, prio
966
+ q.insert val, prio
749
967
  }
750
- pd.each_with_priority {|val, prio|
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
- pd = Depq.new
974
+ q = Depq.new
757
975
  a = [1,2,0]
758
976
  a.each {|v|
759
- pd.insert v
977
+ q.insert v
760
978
  }
761
- pd.each_locator {|loc|
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
- loc = Depq.merge(1..4, 3..6)
798
- assert_equal(1, loc.next)
799
- assert_equal(2, loc.next)
800
- assert_equal(3, loc.next)
801
- assert_equal(3, loc.next)
802
- assert_equal(4, loc.next)
803
- assert_equal(4, loc.next)
804
- assert_equal(5, loc.next)
805
- assert_equal(6, loc.next)
806
- assert_raise(StopIteration) { loc.next }
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
- loc = Depq.merge(1..4, 3..6)
1028
+ e = Depq.merge(1..4, 3..6)
811
1029
  a = []
812
- loc.each_slice(2) {|x|
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