akr-depq 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README +181 -0
  2. data/depq.rb +1520 -0
  3. data/test-depq.rb +772 -0
  4. metadata +55 -0
@@ -0,0 +1,772 @@
1
+ # test-depq.rb - test for depq.rb
2
+ #
3
+ # Copyright (C) 2009 Tanaka Akira <akr@fsij.org>
4
+ #
5
+ # Redistribution and use in source and binary forms, with or without
6
+ # modification, are permitted provided that the following conditions are met:
7
+ #
8
+ # 1. Redistributions of source code must retain the above copyright notice, this
9
+ # list of conditions and the following disclaimer.
10
+ # 2. Redistributions in binary form must reproduce the above copyright notice,
11
+ # this list of conditions and the following disclaimer in the documentation
12
+ # and/or other materials provided with the distribution.
13
+ # 3. The name of the author may not be used to endorse or promote products
14
+ # derived from this software without specific prior written permission.
15
+ #
16
+ # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17
+ # WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18
+ # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19
+ # EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20
+ # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21
+ # OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22
+ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23
+ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24
+ # IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
25
+ # OF SUCH DAMAGE.
26
+
27
+ require './depq'
28
+ require 'test/unit'
29
+
30
+ class Depq
31
+ module SimpleHeap
32
+ def validation(pd, ary)
33
+ 0.upto(size(ary)-1) {|i|
34
+ _, x = get_entry(ary, i)
35
+ j = i*2+1
36
+ k = i*2+2
37
+ if j < size(ary) && !upper?(pd, ary, i, j)
38
+ _, y = get_entry(ary, j)
39
+ raise "wrong binary heap: pri[#{i}]=#{x.inspect} > #{y.inspect}=pri[#{j}]"
40
+ end
41
+ if k < size(ary) && !upper?(pd, ary, i, k)
42
+ _, z = get_entry(ary, k)
43
+ raise "wrong binary heap: pri[#{i}]=#{x.inspect} > #{z.inspect}=pri[#{k}]"
44
+ end
45
+ }
46
+ end
47
+ end
48
+
49
+ def validation
50
+ @mode.validation(self, @ary) if @mode
51
+ if @ary.length % ARY_SLICE_SIZE != 0
52
+ raise "wrong length"
53
+ end
54
+ i = 0
55
+ each_entry {|loc, priority|
56
+ if !loc.in_queue?
57
+ raise "wrongly deleted"
58
+ end
59
+ if loc.send(:index) != i
60
+ raise "index mismatch"
61
+ end
62
+ unless self.equal? loc.pdeque
63
+ raise "pdeque mismatch"
64
+ end
65
+ i += 1
66
+ }
67
+ end
68
+ end
69
+
70
+ class TestDepq < Test::Unit::TestCase
71
+ def random_test(mode, incremental)
72
+ case mode
73
+ when :min
74
+ find = :find_min
75
+ delete = :delete_min
76
+ cmp = lambda {|a, b| a <=> b }
77
+ when :max
78
+ find = :find_max
79
+ delete = :delete_max
80
+ cmp = lambda {|a, b| b <=> a }
81
+ else
82
+ raise "wrong mode"
83
+ end
84
+ pd = Depq.new
85
+ n = 10
86
+ a1 = []
87
+ n.times {
88
+ r = rand(n)
89
+ a1 << r
90
+ pd.insert(r)
91
+ if incremental
92
+ pd.send find
93
+ pd.validation
94
+ end
95
+ }
96
+ a1.sort!(&cmp)
97
+ a2 = []
98
+ n.times {
99
+ a2 << pd.send(delete)
100
+ pd.validation
101
+ }
102
+ assert_equal(a1, a2)
103
+ end
104
+
105
+ def test_random100_minmode
106
+ 100.times { random_test(:min, false) }
107
+ 100.times { random_test(:min, true) }
108
+ 100.times { random_test(:max, false) }
109
+ 100.times { random_test(:max, true) }
110
+ end
111
+
112
+ def perm_test(ary, incremental)
113
+ a0 = ary.to_a.sort
114
+ a0.permutation {|a1|
115
+ pd = Depq.new
116
+ a1.each {|v|
117
+ pd.insert v
118
+ if incremental
119
+ pd.find_min
120
+ pd.validation
121
+ end
122
+ }
123
+ pd.find_min
124
+ pd.validation
125
+ a0.each {|v|
126
+ assert_equal(v, pd.delete_min)
127
+ }
128
+ }
129
+ end
130
+
131
+ def test_permutation
132
+ 5.times {|n|
133
+ perm_test(0..n, true)
134
+ perm_test(0..n, false)
135
+ }
136
+ end
137
+
138
+ def perm_test2(ary)
139
+ a0 = ary.to_a.sort
140
+ a0.permutation {|a1|
141
+ 0.upto(2**(a1.length-1)-1) {|n|
142
+ #log = []; p [:n, n, 2**(a1.length-1)-1]
143
+ pd = Depq.new
144
+ a1.each_with_index {|v,i|
145
+ pd.insert v
146
+ #log << v
147
+ if n[i] != 0
148
+ pd.find_min
149
+ pd.validation
150
+ #log << :find_min
151
+ end
152
+ }
153
+ #p log
154
+ a0.each {|v|
155
+ assert_equal(v, pd.delete_min)
156
+ }
157
+ }
158
+ }
159
+ end
160
+
161
+ def test_permutation2
162
+ 5.times {|n|
163
+ perm_test2(0..n)
164
+ }
165
+ end
166
+
167
+ def test_stable_min
168
+ pd = Depq.new
169
+ pd.insert "a", 0
170
+ pd.insert "b", 0
171
+ pd.insert "c", 0
172
+ assert_equal("a", pd.delete_min)
173
+ assert_equal("b", pd.delete_min)
174
+ assert_equal("c", pd.delete_min)
175
+ end
176
+
177
+ def test_stable_max
178
+ pd = Depq.new
179
+ pd.insert "a", 0
180
+ pd.insert "b", 0
181
+ pd.insert "c", 0
182
+ assert_equal("a", pd.delete_max)
183
+ assert_equal("b", pd.delete_max)
184
+ assert_equal("c", pd.delete_max)
185
+ end
186
+
187
+ def test_locator_new
188
+ pd = Depq.new
189
+ loc1 = Depq::Locator.new(1)
190
+ loc2 = Depq::Locator.new(2, 3)
191
+ assert_equal(1, loc1.value)
192
+ assert_equal(1, loc1.priority)
193
+ assert_equal(2, loc2.value)
194
+ assert_equal(3, loc2.priority)
195
+ pd.insert_locator loc1
196
+ pd.insert_locator loc2
197
+ assert_equal(loc1, pd.delete_min_locator)
198
+ assert_equal(loc2, pd.delete_min_locator)
199
+ end
200
+
201
+ def test_locator_eql
202
+ loc1 = Depq::Locator.new(1,2,3)
203
+ loc2 = Depq::Locator.new(1,2,3)
204
+ assert(!loc1.eql?(loc2))
205
+ end
206
+
207
+ def test_locator_priority
208
+ pd = Depq.new
209
+ loc2 = pd.insert(Object.new, 2)
210
+ loc1 = pd.insert(Object.new, 1)
211
+ loc3 = pd.insert(Object.new, 3)
212
+ assert_equal(1, loc1.priority)
213
+ assert_equal(2, loc2.priority)
214
+ assert_equal(3, loc3.priority)
215
+ pd.delete_locator(loc1)
216
+ assert_equal(1, loc1.priority)
217
+ end
218
+
219
+ def test_locator_update_min
220
+ pd = Depq.new
221
+ a = pd.insert("a", 2)
222
+ b = pd.insert("b", 1)
223
+ c = pd.insert("c", 3)
224
+ assert_equal(b, pd.find_min_locator)
225
+ a.update("d", 0)
226
+ assert_equal("d", a.value)
227
+ assert_equal(a, pd.find_min_locator)
228
+ a.update("e", 10)
229
+ assert_equal("b", pd.delete_min)
230
+ assert_equal("c", pd.delete_min)
231
+ assert_equal("e", pd.delete_min)
232
+ a.update "z", 20
233
+ assert_equal("z", a.value)
234
+ assert_equal(20, a.priority)
235
+ end
236
+
237
+ def test_locator_update_subpriority_min
238
+ pd = Depq.new
239
+ a = pd.insert("a", 1, 0)
240
+ b = pd.insert("b", 2, 1)
241
+ c = pd.insert("c", 1, 2)
242
+ d = pd.insert("d", 2, 3)
243
+ e = pd.insert("e", 1, 4)
244
+ f = pd.insert("f", 2, 5)
245
+ assert_equal(a, pd.find_min_locator)
246
+ a.update("A", 1, 10)
247
+ assert_equal(c, pd.find_min_locator)
248
+ a.update("aa", 1, 1)
249
+ assert_equal(a, pd.find_min_locator)
250
+ pd.delete_locator a
251
+ assert_equal(c, pd.find_min_locator)
252
+ a.update("aaa", 10, 20)
253
+ assert_equal("aaa", a.value)
254
+ assert_equal(10, a.priority)
255
+ assert_equal(20, a.subpriority)
256
+ end
257
+
258
+ def test_locator_update_subpriority_max
259
+ pd = Depq.new
260
+ a = pd.insert("a", 1, 0)
261
+ b = pd.insert("b", 2, 1)
262
+ c = pd.insert("c", 1, 2)
263
+ d = pd.insert("d", 2, 3)
264
+ e = pd.insert("e", 1, 4)
265
+ f = pd.insert("f", 2, 5)
266
+ assert_equal(b, pd.find_max_locator)
267
+ b.update("B", 2, 6)
268
+ assert_equal(d, pd.find_max_locator)
269
+ b.update("bb", 2, 0)
270
+ assert_equal(b, pd.find_max_locator)
271
+ pd.delete_locator b
272
+ assert_equal(d, pd.find_max_locator)
273
+ b.update("bbb", -1, -2)
274
+ assert_equal("bbb", b.value)
275
+ assert_equal(-1, b.priority)
276
+ assert_equal(-2, b.subpriority)
277
+ end
278
+
279
+ def test_locator_update_max
280
+ pd = Depq.new
281
+ a = pd.insert("a", 2)
282
+ b = pd.insert("b", 1)
283
+ c = pd.insert("c", 3)
284
+ assert_equal(c, pd.find_max_locator)
285
+ b.update("d", 10)
286
+ assert_equal("d", b.value)
287
+ assert_equal(b, pd.find_max_locator)
288
+ b.update("e", 0)
289
+ assert_equal("c", pd.delete_max)
290
+ assert_equal("a", pd.delete_max)
291
+ assert_equal("e", pd.delete_max)
292
+ end
293
+
294
+ def test_locator_update_value
295
+ pd = Depq.new
296
+ loc = pd.insert 1, 2, 3
297
+ assert_equal(1, loc.value)
298
+ assert_equal(2, loc.priority)
299
+ assert_equal(3, loc.subpriority)
300
+ loc.update_value 10
301
+ assert_equal(10, loc.value)
302
+ assert_equal(2, loc.priority)
303
+ assert_equal(3, loc.subpriority)
304
+ end
305
+
306
+ def test_locator_update_priority
307
+ pd = Depq.new
308
+ loc = pd.insert 1, 2, 3
309
+ assert_equal(1, loc.value)
310
+ assert_equal(2, loc.priority)
311
+ assert_equal(3, loc.subpriority)
312
+ loc.update_priority 10
313
+ assert_equal(1, loc.value)
314
+ assert_equal(10, loc.priority)
315
+ assert_equal(3, loc.subpriority)
316
+ loc.update_priority 20, 30
317
+ assert_equal(1, loc.value)
318
+ assert_equal(20, loc.priority)
319
+ assert_equal(30, loc.subpriority)
320
+ loc.update_priority 40, nil
321
+ assert_equal(1, loc.value)
322
+ assert_equal(40, loc.priority)
323
+ assert_equal(30, loc.subpriority)
324
+ pd.delete_min
325
+ assert_equal(1, loc.value)
326
+ assert_equal(40, loc.priority)
327
+ assert_equal(30, loc.subpriority)
328
+ loc.update_priority 50, nil
329
+ assert_equal(1, loc.value)
330
+ assert_equal(50, loc.priority)
331
+ assert_equal(nil, loc.subpriority)
332
+ end
333
+
334
+ def test_locator_subpriority
335
+ pd = Depq.new
336
+ loc1 = pd.insert(Object.new, 1, 11)
337
+ loc2 = pd.insert(Object.new, 2, 12)
338
+ loc3 = pd.insert(Object.new, 3, 13)
339
+ assert_equal(1, loc1.priority)
340
+ assert_equal(11, loc1.subpriority)
341
+ assert_equal(2, loc2.priority)
342
+ assert_equal(12, loc2.subpriority)
343
+ assert_equal(3, loc3.priority)
344
+ assert_equal(13, loc3.subpriority)
345
+ pd.delete_locator(loc1)
346
+ assert_equal(11, loc1.subpriority)
347
+ end
348
+
349
+ def test_new
350
+ pd = Depq.new
351
+ pd.insert "Foo"
352
+ pd.insert "bar"
353
+ assert_equal("Foo", pd.delete_min)
354
+ assert_equal("bar", pd.delete_min)
355
+
356
+ pd = Depq.new(:casecmp)
357
+ pd.insert "Foo"
358
+ pd.insert "bar"
359
+ assert_equal("bar", pd.delete_min)
360
+ assert_equal("Foo", pd.delete_min)
361
+
362
+ pd = Depq.new(lambda {|a,b| a.casecmp(b) })
363
+ pd.insert "Foo"
364
+ pd.insert "bar"
365
+ assert_equal("bar", pd.delete_min)
366
+ assert_equal("Foo", pd.delete_min)
367
+ end
368
+
369
+ def test_dup
370
+ pd = Depq.new
371
+ pd.insert 1
372
+ pd2 = pd.dup
373
+ pd.validation
374
+ pd2.validation
375
+ pd.insert 2
376
+ pd2.validation
377
+ assert_equal(1, pd2.delete_min)
378
+ pd.validation
379
+ pd2.validation
380
+ assert_equal(nil, pd2.delete_min)
381
+ pd.validation
382
+ pd2.validation
383
+ assert_equal(1, pd.delete_min)
384
+ pd.validation
385
+ pd2.validation
386
+ assert_equal(2, pd.delete_min)
387
+ assert_equal(nil, pd.delete_min)
388
+ end
389
+
390
+ def test_marshal
391
+ pd = Depq.new
392
+ pd.insert 1
393
+ pd2 = Marshal.load(Marshal.dump(pd))
394
+ pd.validation
395
+ pd2.validation
396
+ pd.insert 2
397
+ pd2.validation
398
+ assert_equal(1, pd2.delete_min)
399
+ pd.validation
400
+ pd2.validation
401
+ assert_equal(nil, pd2.delete_min)
402
+ pd.validation
403
+ pd2.validation
404
+ assert_equal(1, pd.delete_min)
405
+ pd.validation
406
+ pd2.validation
407
+ assert_equal(2, pd.delete_min)
408
+ assert_equal(nil, pd.delete_min)
409
+ end
410
+
411
+ def test_compare_priority
412
+ pd = Depq.new
413
+ assert_operator(pd.compare_priority("a", "b"), :<, 0)
414
+ assert_operator(pd.compare_priority("a", "a"), :==, 0)
415
+ assert_operator(pd.compare_priority("b", "a"), :>, 0)
416
+ pd = Depq.new(:casecmp)
417
+ assert_operator(pd.compare_priority("a", "b"), :<, 0)
418
+ assert_operator(pd.compare_priority("a", "B"), :<, 0)
419
+ assert_operator(pd.compare_priority("A", "b"), :<, 0)
420
+ assert_operator(pd.compare_priority("A", "B"), :<, 0)
421
+ assert_operator(pd.compare_priority("a", "a"), :==, 0)
422
+ assert_operator(pd.compare_priority("a", "A"), :==, 0)
423
+ assert_operator(pd.compare_priority("A", "a"), :==, 0)
424
+ assert_operator(pd.compare_priority("A", "A"), :==, 0)
425
+ assert_operator(pd.compare_priority("b", "a"), :>, 0)
426
+ assert_operator(pd.compare_priority("b", "A"), :>, 0)
427
+ assert_operator(pd.compare_priority("B", "a"), :>, 0)
428
+ assert_operator(pd.compare_priority("B", "A"), :>, 0)
429
+ pd = Depq.new(lambda {|a,b| [a[1],a[0]] <=> [b[1],b[0]]})
430
+ assert_operator(pd.compare_priority([0,0], [0,0]), :==, 0)
431
+ assert_operator(pd.compare_priority([0,0], [0,1]), :<, 0)
432
+ assert_operator(pd.compare_priority([0,0], [1,0]), :<, 0)
433
+ assert_operator(pd.compare_priority([0,0], [1,1]), :<, 0)
434
+ assert_operator(pd.compare_priority([0,1], [0,0]), :>, 0)
435
+ assert_operator(pd.compare_priority([0,1], [0,1]), :==, 0)
436
+ assert_operator(pd.compare_priority([0,1], [1,0]), :>, 0)
437
+ assert_operator(pd.compare_priority([0,1], [1,1]), :<, 0)
438
+ assert_operator(pd.compare_priority([1,0], [0,0]), :>, 0)
439
+ assert_operator(pd.compare_priority([1,0], [0,1]), :<, 0)
440
+ assert_operator(pd.compare_priority([1,0], [1,0]), :==, 0)
441
+ assert_operator(pd.compare_priority([1,0], [1,1]), :<, 0)
442
+ assert_operator(pd.compare_priority([1,1], [0,0]), :>, 0)
443
+ assert_operator(pd.compare_priority([1,1], [0,1]), :>, 0)
444
+ assert_operator(pd.compare_priority([1,1], [1,0]), :>, 0)
445
+ assert_operator(pd.compare_priority([1,1], [1,1]), :==, 0)
446
+ end
447
+
448
+ def test_empty?
449
+ pd = Depq.new
450
+ assert(pd.empty?)
451
+ pd.insert 1
452
+ assert(!pd.empty?)
453
+ end
454
+
455
+ def test_size
456
+ pd = Depq.new
457
+ pd.insert 1
458
+ assert_equal(1, pd.size)
459
+ pd.insert 10
460
+ assert_equal(2, pd.size)
461
+ pd.insert 2
462
+ assert_equal(3, pd.size)
463
+ pd.delete_max
464
+ assert_equal(2, pd.size)
465
+ end
466
+
467
+ def test_totalcount
468
+ pd = Depq.new
469
+ assert_equal(0, pd.totalcount)
470
+ pd.insert 1
471
+ assert_equal(1, pd.totalcount)
472
+ pd.insert 2
473
+ assert_equal(2, pd.totalcount)
474
+ pd.delete_min
475
+ assert_equal(2, pd.totalcount)
476
+ pd.insert 4
477
+ assert_equal(3, pd.totalcount)
478
+ pd.insert 3
479
+ assert_equal(4, pd.totalcount)
480
+ pd.insert 0
481
+ assert_equal(5, pd.totalcount)
482
+ pd.delete_min
483
+ assert_equal(5, pd.totalcount)
484
+ pd.insert 2
485
+ assert_equal(6, pd.totalcount)
486
+ end
487
+
488
+ def test_clear
489
+ pd = Depq.new
490
+ pd.insert 1
491
+ assert(!pd.empty?)
492
+ pd.clear
493
+ assert(pd.empty?)
494
+ end
495
+
496
+ def test_insert
497
+ pd = Depq.new
498
+ pd.insert 1
499
+ pd.insert 10
500
+ pd.insert 2
501
+ assert_equal(1, pd.delete_min)
502
+ assert_equal(2, pd.delete_min)
503
+ assert_equal(10, pd.delete_min)
504
+ end
505
+
506
+ def test_insert_all
507
+ pd = Depq.new
508
+ pd.insert_all [3,1,2]
509
+ assert_equal(1, pd.delete_min)
510
+ assert_equal(2, pd.delete_min)
511
+ assert_equal(3, pd.delete_min)
512
+ assert_equal(nil, pd.delete_min)
513
+ end
514
+
515
+ def test_find_min_locator
516
+ pd = Depq.new
517
+ pd.insert 1
518
+ loc = pd.find_min_locator
519
+ assert_equal(1, loc.value)
520
+ assert_equal(1, loc.priority)
521
+ assert_equal(pd, loc.pdeque)
522
+ assert_equal(1, pd.delete_min)
523
+ assert_equal(nil, loc.pdeque)
524
+ end
525
+
526
+ def test_find_min
527
+ pd = Depq.new
528
+ pd.insert 1
529
+ assert_equal(1, pd.find_min)
530
+ end
531
+
532
+ def test_find_min_priority
533
+ pd = Depq.new
534
+ pd.insert "a", 1
535
+ assert_equal(["a", 1], pd.find_min_priority)
536
+ pd.delete_min
537
+ assert_equal(nil, pd.find_min_priority)
538
+ end
539
+
540
+ def test_find_max_locator
541
+ pd = Depq.new
542
+ pd.insert 1
543
+ loc = pd.find_max_locator
544
+ assert_equal(1, loc.value)
545
+ assert_equal(1, loc.priority)
546
+ assert_equal(pd, loc.pdeque)
547
+ end
548
+
549
+ def test_find_max
550
+ pd = Depq.new
551
+ pd.insert 1
552
+ assert_equal(1, pd.find_max)
553
+ end
554
+
555
+ def test_find_max_priority
556
+ pd = Depq.new
557
+ pd.insert "a", 1
558
+ assert_equal(["a", 1], pd.find_max_priority)
559
+ pd.delete_max
560
+ assert_equal(nil, pd.find_max_priority)
561
+ end
562
+
563
+ def test_find_minmax_locator
564
+ pd = Depq.new
565
+ assert_equal([nil, nil], pd.find_minmax_locator)
566
+ loc3 = pd.insert 3
567
+ loc1 = pd.insert 1
568
+ pd.insert 2
569
+ res = pd.find_minmax_locator
570
+ assert_equal([loc1, loc3], res)
571
+ end
572
+
573
+ def test_find_minmax
574
+ pd = Depq.new
575
+ assert_equal([nil, nil], pd.find_minmax)
576
+ pd.insert 3
577
+ pd.insert 1
578
+ pd.insert 2
579
+ res = pd.find_minmax
580
+ assert_equal([1, 3], res)
581
+ end
582
+
583
+ def test_delete_locator
584
+ pd = Depq.new
585
+ loc = pd.insert 1
586
+ pd.delete_locator loc
587
+ assert(pd.empty?)
588
+ pd = Depq.new
589
+ loc = pd.insert 2
590
+ pd.insert 3
591
+ pd.insert 1
592
+ assert_equal(1, pd.find_min)
593
+ pd.delete_locator(loc)
594
+ assert_equal(1, pd.delete_min)
595
+ assert_equal(3, pd.delete_min)
596
+ end
597
+
598
+ def test_delete_min
599
+ pd = Depq.new
600
+ pd.insert 1
601
+ pd.insert 2
602
+ pd.insert 0
603
+ assert_equal(0, pd.delete_min)
604
+ assert_equal(1, pd.delete_min)
605
+ assert_equal(2, pd.delete_min)
606
+ assert_equal(nil, pd.delete_min)
607
+ end
608
+
609
+ def test_delete_min_locator
610
+ pd = Depq.new
611
+ loc1 = pd.insert 1
612
+ loc2 = pd.insert 2
613
+ loc0 = pd.insert 0
614
+ assert_equal(loc0, pd.delete_min_locator)
615
+ assert_equal(loc1, pd.delete_min_locator)
616
+ assert_equal(loc2, pd.delete_min_locator)
617
+ assert_equal(nil, pd.delete_min_locator)
618
+ end
619
+
620
+ def test_delete_max
621
+ pd = Depq.new
622
+ pd.insert 1
623
+ pd.insert 2
624
+ pd.insert 0
625
+ assert_equal(2, pd.delete_max)
626
+ assert_equal(1, pd.delete_max)
627
+ assert_equal(0, pd.delete_max)
628
+ assert_equal(nil, pd.delete_max)
629
+ end
630
+
631
+ def test_delete_max_locator
632
+ pd = Depq.new
633
+ loc1 = pd.insert 1
634
+ loc2 = pd.insert 2
635
+ loc0 = pd.insert 0
636
+ assert_equal(loc2, pd.delete_max_locator)
637
+ assert_equal(loc1, pd.delete_max_locator)
638
+ assert_equal(loc0, pd.delete_max_locator)
639
+ assert_equal(nil, pd.delete_max)
640
+ end
641
+
642
+ def test_delete_unspecified
643
+ pd = Depq.new
644
+ a1 = [1,2,0]
645
+ a1.each {|v|
646
+ pd.insert v
647
+ }
648
+ a2 = []
649
+ a1.length.times {
650
+ a2 << pd.delete_unspecified
651
+ }
652
+ assert_equal(a1.sort, a2.sort)
653
+ assert_equal(nil, pd.delete_unspecified_locator)
654
+ end
655
+
656
+ def test_delete_unspecified_priority
657
+ pd = Depq.new
658
+ a1 = [[1,8],[2,3],[0,5]]
659
+ a1.each {|val, priority|
660
+ pd.insert val, priority
661
+ }
662
+ a2 = []
663
+ a1.length.times {
664
+ a2 << pd.delete_unspecified_priority
665
+ }
666
+ assert_equal(a1.sort, a2.sort)
667
+ assert_equal(nil, pd.delete_unspecified_locator)
668
+ end
669
+
670
+ def test_delete_unspecified_locator
671
+ pd = Depq.new
672
+ a1 = [1,2,0]
673
+ a1.each {|v|
674
+ pd.insert v
675
+ }
676
+ a2 = []
677
+ a1.length.times {
678
+ a2 << pd.delete_unspecified_locator.value
679
+ }
680
+ assert_equal(a1.sort, a2.sort)
681
+ assert_equal(nil, pd.delete_unspecified_locator)
682
+ end
683
+
684
+ def test_each
685
+ pd = Depq.new
686
+ a = [1,2,0]
687
+ a.each {|v|
688
+ pd.insert v
689
+ }
690
+ pd.each {|v|
691
+ assert(a.include? v)
692
+ }
693
+ end
694
+
695
+ def test_each_with_priority
696
+ pd = Depq.new
697
+ h = {}
698
+ h["durian"] = 1
699
+ h["banana"] = 3
700
+ h["melon"] = 2
701
+ h.each {|val, prio|
702
+ pd.insert val, prio
703
+ }
704
+ pd.each_with_priority {|val, prio|
705
+ assert_equal(h[val], prio)
706
+ }
707
+ end
708
+
709
+ def test_each_locator
710
+ pd = Depq.new
711
+ a = [1,2,0]
712
+ a.each {|v|
713
+ pd.insert v
714
+ }
715
+ pd.each_locator {|loc|
716
+ assert(a.include? loc.value)
717
+ }
718
+ end
719
+
720
+ def test_nlargest
721
+ a = Depq.nlargest(3, [5, 1, 3, 2, 4, 6, 7])
722
+ assert_equal([5, 6, 7], a)
723
+
724
+ assert_equal([1,2], Depq.nlargest(3, [1,2]))
725
+
726
+ a = []
727
+ 2000.times { a << rand }
728
+ b = a.sort
729
+ assert_equal(b[-30..-1], Depq.nlargest(30, a))
730
+ end
731
+
732
+ def test_nsmallest
733
+ a = Depq.nsmallest(5, [5, 2, 3, 1, 4, 6, 7])
734
+ assert_equal([1, 2, 3, 4, 5], a)
735
+
736
+ assert_equal([1,2], Depq.nsmallest(3, [1,2]))
737
+
738
+ a = []
739
+ 2000.times { a << rand }
740
+ b = a.sort
741
+ assert_equal(b[0, 30], Depq.nsmallest(30, a))
742
+ end
743
+
744
+ def test_merge
745
+ a = []
746
+ Depq.merge(1..4, 3..6) {|v| a << v }
747
+ assert_equal([1,2,3,3,4,4,5,6], a)
748
+ end
749
+
750
+ def test_merge_enumerator
751
+ loc = Depq.merge(1..4, 3..6)
752
+ assert_equal(1, loc.next)
753
+ assert_equal(2, loc.next)
754
+ assert_equal(3, loc.next)
755
+ assert_equal(3, loc.next)
756
+ assert_equal(4, loc.next)
757
+ assert_equal(4, loc.next)
758
+ assert_equal(5, loc.next)
759
+ assert_equal(6, loc.next)
760
+ assert_raise(StopIteration) { loc.next }
761
+ end
762
+
763
+ def test_merge_enumerator2
764
+ loc = Depq.merge(1..4, 3..6)
765
+ a = []
766
+ loc.each_slice(2) {|x|
767
+ a << x
768
+ }
769
+ assert_equal([[1,2],[3,3],[4,4],[5,6]], a)
770
+ end
771
+
772
+ end