1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
|
#!/usr/bin/env ruby
require 'spec_helper'
require 'puppet/graph'
describe Puppet::Graph::RbTreeMap do
describe "#push" do
it "should allow a new element to be added" do
subject[5] = 'foo'
subject.size.should == 1
subject[5].should == 'foo'
end
it "should replace the old value if the key is in tree already" do
subject[0] = 10
subject[0] = 20
subject[0].should == 20
subject.size.should == 1
end
it "should be able to add a large number of elements" do
(1..1000).each {|i| subject[i] = i.to_s}
subject.size.should == 1000
end
it "should create a root node if the tree was empty" do
subject.instance_variable_get(:@root).should be_nil
subject[5] = 'foo'
subject.instance_variable_get(:@root).should be_a(Puppet::Graph::RbTreeMap::Node)
end
end
describe "#size" do
it "should be 0 for en empty tree" do
subject.size.should == 0
end
it "should correctly report the size for a non-empty tree" do
(1..10).each {|i| subject[i] = i.to_s}
subject.size.should == 10
end
end
describe "#has_key?" do
it "should be true if the tree contains the key" do
subject[1] = 2
subject.should be_has_key(1)
end
it "should be true if the tree contains the key and its value is nil" do
subject[0] = nil
subject.should be_has_key(0)
end
it "should be false if the tree does not contain the key" do
subject[1] = 2
subject.should_not be_has_key(2)
end
it "should be false if the tree is empty" do
subject.should_not be_has_key(5)
end
end
describe "#get" do
it "should return the value at the key" do
subject[1] = 2
subject[3] = 4
subject.get(1).should == 2
subject.get(3).should == 4
end
it "should return nil if the tree is empty" do
subject[1].should be_nil
end
it "should return nil if the key is not in the tree" do
subject[1] = 2
subject[3].should be_nil
end
it "should return nil if the value at the key is nil" do
subject[1] = nil
subject[1].should be_nil
end
end
describe "#min_key" do
it "should return the smallest key in the tree" do
[4,8,12,3,6,2,-4,7].each do |i|
subject[i] = i.to_s
end
subject.min_key.should == -4
end
it "should return nil if the tree is empty" do
subject.min_key.should be_nil
end
end
describe "#max_key" do
it "should return the largest key in the tree" do
[4,8,12,3,6,2,-4,7].each do |i|
subject[i] = i.to_s
end
subject.max_key.should == 12
end
it "should return nil if the tree is empty" do
subject.max_key.should be_nil
end
end
describe "#delete" do
before :each do
subject[1] = '1'
subject[0] = '0'
subject[2] = '2'
end
it "should return the value at the key deleted" do
subject.delete(0).should == '0'
subject.delete(1).should == '1'
subject.delete(2).should == '2'
subject.size.should == 0
end
it "should be able to delete the last node" do
tree = described_class.new
tree[1] = '1'
tree.delete(1).should == '1'
tree.should be_empty
end
it "should be able to delete the root node" do
subject.delete(1).should == '1'
subject.size.should == 2
subject.to_hash.should == {
:node => {
:key => 2,
:value => '2',
:color => :black,
},
:left => {
:node => {
:key => 0,
:value => '0',
:color => :red,
}
}
}
end
it "should be able to delete the left child" do
subject.delete(0).should == '0'
subject.size.should == 2
subject.to_hash.should == {
:node => {
:key => 2,
:value => '2',
:color => :black,
},
:left => {
:node => {
:key => 1,
:value => '1',
:color => :red,
}
}
}
end
it "should be able to delete the right child" do
subject.delete(2).should == '2'
subject.size.should == 2
subject.to_hash.should == {
:node => {
:key => 1,
:value => '1',
:color => :black,
},
:left => {
:node => {
:key => 0,
:value => '0',
:color => :red,
}
}
}
end
it "should be able to delete the left child if it is a subtree" do
(3..6).each {|i| subject[i] = i.to_s}
subject.delete(1).should == '1'
subject.to_hash.should == {
:node => {
:key => 5,
:value => '5',
:color => :black,
},
:left => {
:node => {
:key => 3,
:value => '3',
:color => :red,
},
:left => {
:node => {
:key => 2,
:value => '2',
:color => :black,
},
:left => {
:node => {
:key => 0,
:value => '0',
:color => :red,
},
},
},
:right => {
:node => {
:key => 4,
:value => '4',
:color => :black,
},
},
},
:right => {
:node => {
:key => 6,
:value => '6',
:color => :black,
},
},
}
end
it "should be able to delete the right child if it is a subtree" do
(3..6).each {|i| subject[i] = i.to_s}
subject.delete(5).should == '5'
subject.to_hash.should == {
:node => {
:key => 3,
:value => '3',
:color => :black,
},
:left => {
:node => {
:key => 1,
:value => '1',
:color => :red,
},
:left => {
:node => {
:key => 0,
:value => '0',
:color => :black,
},
},
:right => {
:node => {
:key => 2,
:value => '2',
:color => :black,
},
},
},
:right => {
:node => {
:key => 6,
:value => '6',
:color => :black,
},
:left => {
:node => {
:key => 4,
:value => '4',
:color => :red,
},
},
},
}
end
it "should return nil if the tree is empty" do
tree = described_class.new
tree.delete(14).should be_nil
tree.size.should == 0
end
it "should return nil if the key is not in the tree" do
(0..4).each {|i| subject[i] = i.to_s}
subject.delete(2.5).should be_nil
subject.size.should == 5
end
it "should return nil if the key is larger than the maximum key" do
subject.delete(100).should be_nil
subject.size.should == 3
end
it "should return nil if the key is smaller than the minimum key" do
subject.delete(-1).should be_nil
subject.size.should == 3
end
end
describe "#empty?" do
it "should return true if the tree is empty" do
subject.should be_empty
end
it "should return false if the tree is not empty" do
subject[5] = 10
subject.should_not be_empty
end
end
describe "#delete_min" do
it "should delete the smallest element of the tree" do
(1..15).each {|i| subject[i] = i.to_s}
subject.delete_min.should == '1'
subject.size.should == 14
end
it "should return nil if the tree is empty" do
subject.delete_min.should be_nil
end
end
describe "#delete_max" do
it "should delete the largest element of the tree" do
(1..15).each {|i| subject[i] = i.to_s}
subject.delete_max.should == '15'
subject.size.should == 14
end
it "should return nil if the tree is empty" do
subject.delete_max.should be_nil
end
end
describe "#each" do
it "should yield each pair in the tree in order if a block is provided" do
# Insert in reverse to demonstrate they aren't being yielded in insertion order
(1..5).to_a.reverse.each {|i| subject[i] = i.to_s}
nodes = []
subject.each do |key,value|
nodes << [key,value]
end
nodes.should == (1..5).map {|i| [i, i.to_s]}
end
it "should do nothing if the tree is empty" do
subject.each do |key,value|
raise "each on an empty tree incorrectly yielded #{key}, #{value}"
end
end
end
describe "#isred" do
it "should return true if the node is red" do
node = Puppet::Graph::RbTreeMap::Node.new(1,2)
node.color = :red
subject.send(:isred, node).should == true
end
it "should return false if the node is black" do
node = Puppet::Graph::RbTreeMap::Node.new(1,2)
node.color = :black
subject.send(:isred, node).should == false
end
it "should return false if the node is nil" do
subject.send(:isred, nil).should == false
end
end
end
describe Puppet::Graph::RbTreeMap::Node do
let(:tree) { Puppet::Graph::RbTreeMap.new }
let(:subject) { tree.instance_variable_get(:@root) }
before :each do
(1..3).each {|i| tree[i] = i.to_s}
end
describe "#red?" do
it "should return true if the node is red" do
subject.color = :red
subject.should be_red
end
it "should return false if the node is black" do
subject.color = :black
subject.should_not be_red
end
end
describe "#colorflip" do
it "should switch the color of the node and its children" do
subject.color.should == :black
subject.left.color.should == :black
subject.right.color.should == :black
subject.colorflip
subject.color.should == :red
subject.left.color.should == :red
subject.right.color.should == :red
end
end
describe "#rotate_left" do
it "should rotate the tree once to the left" do
(4..7).each {|i| tree[i] = i.to_s}
root = tree.instance_variable_get(:@root)
root.rotate_left
tree.to_hash.should == {
:node => {
:key => 6,
:value => '6',
:color => :black,
},
:left => {
:node => {
:key => 4,
:value => '4',
:color => :red,
},
:left => {
:node => {
:key => 2,
:value => '2',
:color => :black,
},
:left => {
:node => {
:key => 1,
:value => '1',
:color => :black,
},
},
:right => {
:node => {
:key => 3,
:value => '3',
:color => :black,
},
},
},
:right => {
:node => {
:key => 5,
:value => '5',
:color => :black,
},
},
},
:right => {
:node => {
:key => 7,
:value => '7',
:color => :black,
},
},
}
end
end
describe "#rotate_right" do
it "should rotate the tree once to the right" do
(4..7).each {|i| tree[i] = i.to_s}
root = tree.instance_variable_get(:@root)
root.rotate_right
tree.to_hash.should == {
:node => {
:key => 2,
:value => '2',
:color => :black,
},
:left => {
:node => {
:key => 1,
:value => '1',
:color => :black,
},
},
:right => {
:node => {
:key => 4,
:value => '4',
:color => :red,
},
:left => {
:node => {
:key => 3,
:value => '3',
:color => :black,
},
},
:right => {
:node => {
:key => 6,
:value => '6',
:color => :black,
},
:left => {
:node => {
:key => 5,
:value => '5',
:color => :black,
},
},
:right => {
:node => {
:key => 7,
:value => '7',
:color => :black,
},
},
},
},
}
end
end
end
|