ds 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (117) hide show
  1. data/.gitignore +2 -0
  2. data/README.rdoc +490 -0
  3. data/Rakefile +1 -1
  4. data/lib/ds.rb +17 -19
  5. data/lib/ds/ext/array_x.rb +35 -0
  6. data/lib/ds/graphs/graph.rb +36 -32
  7. data/lib/ds/graphs/graph_as_matrix.rb +22 -23
  8. data/lib/ds/graphs/graph_as_tri_matrix.rb +1 -2
  9. data/lib/ds/lists/list.rb +18 -21
  10. data/lib/ds/matrixes/array_2d.rb +4 -0
  11. data/lib/ds/pair.rb +14 -0
  12. data/lib/ds/queues/priority_queue.rb +36 -0
  13. data/lib/ds/queues/queue.rb +1 -1
  14. data/lib/ds/sets/ordered_set.rb +32 -0
  15. data/lib/ds/trees/binary_heap.rb +45 -14
  16. data/lib/ds/trees/binary_search_tree.rb +3 -1
  17. data/lib/ds/trees/binary_tree.rb +11 -5
  18. data/lib/ds/trees/complete_binary_tree.rb +24 -16
  19. data/lib/ds/trees/tree.rb +3 -3
  20. data/lib/ds/trees/tree_walker.rb +7 -6
  21. data/lib/ds/trees/trie.rb +14 -20
  22. data/lib/ds/version.rb +1 -1
  23. data/test/help.rb +12 -3
  24. data/test/test_array2d.rb +14 -4
  25. data/test/test_array_x.rb +27 -7
  26. data/test/test_binary_heap.rb +19 -1
  27. data/test/test_binary_search_tree.rb +8 -1
  28. data/test/test_binary_tree.rb +7 -1
  29. data/test/test_complete_binary_tree.rb +34 -6
  30. data/test/test_digraph.rb +1 -1
  31. data/test/test_graph.rb +10 -1
  32. data/test/test_list.rb +59 -2
  33. data/test/test_ordered_set.rb +28 -0
  34. data/test/test_pair.rb +26 -0
  35. data/test/test_priority_queue.rb +134 -0
  36. data/test/test_queue.rb +3 -3
  37. data/test/test_stack.rb +0 -2
  38. data/test/test_tree.rb +32 -9
  39. data/test/test_tree_walker.rb +22 -0
  40. data/test/test_trie.rb +20 -2
  41. metadata +13 -81
  42. data/doc/Array.html +0 -264
  43. data/doc/DS.html +0 -292
  44. data/doc/DS/Array2D.html +0 -345
  45. data/doc/DS/BinaryHeap.html +0 -493
  46. data/doc/DS/BinarySearchTree.html +0 -313
  47. data/doc/DS/BinaryTree.html +0 -433
  48. data/doc/DS/CompleteBinaryTree.html +0 -550
  49. data/doc/DS/CyclicList.html +0 -234
  50. data/doc/DS/Digraph.html +0 -299
  51. data/doc/DS/Edge.html +0 -283
  52. data/doc/DS/ExpandableArray.html +0 -316
  53. data/doc/DS/Graph.html +0 -739
  54. data/doc/DS/GraphAsList.html +0 -361
  55. data/doc/DS/GraphAsMatrix.html +0 -633
  56. data/doc/DS/GraphAsTriMatrix.html +0 -274
  57. data/doc/DS/List.html +0 -1263
  58. data/doc/DS/ListElement.html +0 -344
  59. data/doc/DS/Queue.html +0 -517
  60. data/doc/DS/Ring.html +0 -323
  61. data/doc/DS/Stack.html +0 -407
  62. data/doc/DS/Tree.html +0 -770
  63. data/doc/DS/TreeWalker.html +0 -561
  64. data/doc/DS/TriMatrix.html +0 -338
  65. data/doc/created.rid +0 -25
  66. data/doc/ds/graphs/digraph_rb.html +0 -52
  67. data/doc/ds/graphs/edge_rb.html +0 -52
  68. data/doc/ds/graphs/graph_as_list_rb.html +0 -52
  69. data/doc/ds/graphs/graph_as_matrix_rb.html +0 -52
  70. data/doc/ds/graphs/graph_as_tri_matrix_rb.html +0 -52
  71. data/doc/ds/graphs/graph_rb.html +0 -52
  72. data/doc/ds/lists/cyclic_list_rb.html +0 -52
  73. data/doc/ds/lists/list_element_rb.html +0 -52
  74. data/doc/ds/lists/list_rb.html +0 -52
  75. data/doc/ds/lists/ring_rb.html +0 -52
  76. data/doc/ds/matrixes/array_2d_rb.html +0 -52
  77. data/doc/ds/matrixes/expandable_array_rb.html +0 -52
  78. data/doc/ds/matrixes/tri_matrix_rb.html +0 -52
  79. data/doc/ds/queues/queue_rb.html +0 -52
  80. data/doc/ds/stacks/stack_rb.html +0 -52
  81. data/doc/ds/trees/binary_heap_rb.html +0 -52
  82. data/doc/ds/trees/binary_search_tree_rb.html +0 -52
  83. data/doc/ds/trees/binary_tree_rb.html +0 -52
  84. data/doc/ds/trees/complete_binary_tree_rb.html +0 -52
  85. data/doc/ds/trees/tree_rb.html +0 -52
  86. data/doc/ds/trees/tree_walker_rb.html +0 -52
  87. data/doc/ds/version_rb.html +0 -52
  88. data/doc/ds_rb.html +0 -98
  89. data/doc/ext/ext_rb.html +0 -52
  90. data/doc/images/brick.png +0 -0
  91. data/doc/images/brick_link.png +0 -0
  92. data/doc/images/bug.png +0 -0
  93. data/doc/images/bullet_black.png +0 -0
  94. data/doc/images/bullet_toggle_minus.png +0 -0
  95. data/doc/images/bullet_toggle_plus.png +0 -0
  96. data/doc/images/date.png +0 -0
  97. data/doc/images/find.png +0 -0
  98. data/doc/images/loadingAnimation.gif +0 -0
  99. data/doc/images/macFFBgHack.png +0 -0
  100. data/doc/images/package.png +0 -0
  101. data/doc/images/page_green.png +0 -0
  102. data/doc/images/page_white_text.png +0 -0
  103. data/doc/images/page_white_width.png +0 -0
  104. data/doc/images/plugin.png +0 -0
  105. data/doc/images/ruby.png +0 -0
  106. data/doc/images/tag_green.png +0 -0
  107. data/doc/images/wrench.png +0 -0
  108. data/doc/images/wrench_orange.png +0 -0
  109. data/doc/images/zoom.png +0 -0
  110. data/doc/index.html +0 -375
  111. data/doc/js/darkfish.js +0 -116
  112. data/doc/js/jquery.js +0 -32
  113. data/doc/js/quicksearch.js +0 -114
  114. data/doc/js/thickbox-compressed.js +0 -10
  115. data/doc/rdoc.css +0 -763
  116. data/lib/ds/ext/ext.rb +0 -36
  117. data/lib/ds/graphs/graph_as_list.rb +0 -48
@@ -5,6 +5,8 @@ describe ArrayX do
5
5
  before do
6
6
  @asc = [1,3,6,9,10,13].extend(ArrayX)
7
7
  @desc = [9,8,7,7,4,3,2].extend(ArrayX)
8
+ @short = [1,2].extend(ArrayX)
9
+
8
10
  @empty = [].extend(ArrayX)
9
11
  @one = [3].extend(ArrayX)
10
12
  @not_sorted = [3,4,9,1,1,2,8].extend(ArrayX)
@@ -15,16 +17,34 @@ describe ArrayX do
15
17
  assert @asc.sorted?
16
18
  assert @empty.sorted?
17
19
  assert @one.sorted?
20
+ assert @short.sorted?
18
21
  refute @not_sorted.sorted?
19
- end
22
+ assert @desc.sorted?(:desc)
23
+ refute @desc.sorted?(:asc)
24
+ assert @asc.sorted?(:asc)
25
+ refute @asc.sorted?(:desc)
26
+ refute @not_sorted.sorted?(:des)
27
+ refute @not_sorted.sorted?(:asc)
28
+ assert @short.sorted?(:asc)
29
+ refute @short.sorted?(:desc)
30
+
31
+ assert @one.sorted?(:asc)
32
+ assert @one.sorted?(:asc)
20
33
 
21
- it "#push_uniq should push element only if it is not already in array." do
22
- @asc.size.must_equal 6
23
- @asc.push_uniq(3).must_equal 1
24
- @asc.size.must_equal 6
34
+ end
35
+
36
+ it "#duplications? should check if array has duplicates." do
37
+ refute @asc.duplications?
38
+ assert @desc.duplications?
39
+ assert @not_sorted.duplications?
40
+ refute @empty.duplications?
41
+ end
25
42
 
26
- @empty.push_uniq(3).must_equal 0
27
- @empty.must_equal [3]
43
+ it "#tail should return array without first element." do
44
+ @asc.tail.must_equal [3,6,9,10,13]
45
+ @short.tail.must_equal [2]
46
+ @one.tail.must_equal []
47
+ @empty.tail.must_be_nil
28
48
  end
29
49
  end
30
50
 
@@ -6,7 +6,7 @@ describe BinaryHeap do
6
6
  @arr = [9,8,4,5,11,6]
7
7
  @arr2 = [4,2,9,11,3,5,1]
8
8
  @heap = BinaryHeap.new(*@arr)
9
- @heap2 = BinaryHeap.new(*@arr2)
9
+ @heap2 = BinaryHeap.max(*@arr2)
10
10
  end
11
11
 
12
12
  it "should create heap from array" do
@@ -18,18 +18,36 @@ describe BinaryHeap do
18
18
  @heap.insert(13).to_a.must_equal [13,9,11,5,8,4,6]
19
19
  end
20
20
 
21
+ it "#shift should remove element from heap maintaining heap relation." do
22
+ @heap.shift.must_equal 11
23
+ @heap.to_a.must_equal [9,8,6,5,4]
24
+ @heap.shift.must_equal 9
25
+ @heap.to_a.must_equal [8,5,6,4]
26
+ @heap.shift.must_equal 8
27
+ @heap.to_a.must_equal [6,5,4]
28
+ @heap.shift.must_equal 6
29
+ @heap.to_a.must_equal [5,4]
30
+ @heap.shift.must_equal 5
31
+ @heap.to_a.must_equal [4]
32
+ @heap.shift.must_equal 4
33
+ @heap.shift.must_be_nil
34
+ end
35
+
21
36
  describe "Minimal Heap" do
22
37
 
23
38
  before do
24
39
  @min_heap = BinaryHeap.new(*@arr){|parent,child| parent < child}
40
+ @min_heap2 = BinaryHeap.min(*@arr)
25
41
  end
26
42
 
27
43
  it "should create heap from array." do
28
44
  @min_heap.to_a.must_equal [4,5,6,8,11,9]
45
+ @min_heap2.to_a.must_equal [4,5,6,8,11,9]
29
46
  end
30
47
 
31
48
  it "#insert should insert new element to the heap maintaining heap relation." do
32
49
  @min_heap.insert(3).to_a.must_equal [3,5,4,8,11,9,6]
50
+ @min_heap2.insert(3).to_a.must_equal [3,5,4,8,11,9,6]
33
51
  end
34
52
  end
35
53
  end
@@ -3,6 +3,8 @@ require 'help'
3
3
  describe BinarySearchTree do
4
4
 
5
5
  before do
6
+
7
+
6
8
  t = BinarySearchTree.new(7)
7
9
  c1 = t.insert 5
8
10
  c2 = t.insert 8
@@ -16,13 +18,18 @@ describe BinarySearchTree do
16
18
 
17
19
  @bin_tree = BinaryTree.new
18
20
  [2,5,8,9,11,12,14].each{|x| @bin_tree.insert(x)}
19
-
21
+
20
22
  end
21
23
 
22
24
  it "should be valid Binary Search Tree." do
23
25
  assert BinarySearchTree.valid?(@bst)
24
26
  end
25
27
 
28
+ it "#from_array should create new valid BinarySearchTree." do
29
+ b = BinarySearchTree.from_array([8,1,5,2,7,6,3])
30
+ assert BinarySearchTree.valid?(b)
31
+ TreeWalker.new(b).traverse(:inorder).must_equal [1,2,3,5,6,7,8]
32
+ end
26
33
 
27
34
  it "not every Binary Tree is valid Binary Search Tree." do
28
35
  refute BinarySearchTree.valid?(@bin_tree)
@@ -10,6 +10,10 @@ describe BinaryTree do
10
10
  @izo_tree = BinaryTree.new
11
11
  [2,7,8,3,11,12,14].each{|x| @izo_tree.insert(x)}
12
12
 
13
+ @small_tree = BinaryTree.new(2)
14
+ @small_tree << BinaryTree.new(7)
15
+ @small_tree << BinaryTree.new(8)
16
+
13
17
  @bin_walker = TreeWalker.new(@bin_tree)
14
18
 
15
19
  end
@@ -35,7 +39,7 @@ describe BinaryTree do
35
39
  end
36
40
 
37
41
  it "#lowest_height should return node which lies closest to the root." do
38
- assert @bin_tree.lowest_height.must_equal 9
42
+ assert @bin_tree.lowest_height.data.must_equal 9
39
43
  end
40
44
 
41
45
  it "#mirror should mirror tree." do
@@ -45,6 +49,8 @@ describe BinaryTree do
45
49
 
46
50
  it "#isometric should check if tree is isometric to antother." do
47
51
  assert @bin_tree.izometric?(@izo_tree)
52
+ assert @izo_tree.izometric?(@bin_tree)
53
+ refute @bin_tree.izometric?(@small_tree)
48
54
  end
49
55
 
50
56
  end
@@ -4,27 +4,55 @@ describe CompleteBinaryTree do
4
4
 
5
5
  before do
6
6
  @tree = CompleteBinaryTree.new(1,2,3,4,5,6,7)
7
+ @empty_tree = CompleteBinaryTree.new
8
+ end
9
+
10
+ it "#<< should add new element." do
11
+ @empty_tree << 1
12
+ @empty_tree << 2
13
+ @empty_tree.root.must_equal 1
7
14
  end
8
15
 
9
16
  it "#root should return root of the tree." do
10
17
  @tree.root.must_equal 1
11
18
  end
12
19
 
20
+ it "#left_index should return index of left child" do
21
+ @tree.left_index(0).must_equal 1
22
+ @tree.left_index(1).must_equal 3
23
+ end
24
+
25
+ it "#right_index should return index of left child" do
26
+ @tree.right_index(0).must_equal 2
27
+ @tree.right_index(1).must_equal 4
28
+ end
29
+
30
+ it "#parent_index should return index of left child" do
31
+ @tree.parent_index(1).must_equal 0
32
+ @tree.parent_index(3).must_equal 1
33
+ end
34
+
13
35
  it "#left should return left child" do
14
- @tree.left(1).must_equal 2
15
- @tree.left(2).must_equal 4
36
+ @tree.left(0).must_equal 2
37
+ @tree.left(1).must_equal 4
16
38
  end
17
39
 
18
40
  it "#right should return right child" do
19
- @tree.right(1).must_equal 3
20
- @tree.right(2).must_equal 5
41
+ @tree.right(0).must_equal 3
42
+ @tree.right(1).must_equal 5
21
43
  end
22
44
 
23
45
  it "#parent should return parent." do
46
+ @tree.parent(3).must_equal 2
24
47
  @tree.parent(4).must_equal 2
25
- @tree.parent(5).must_equal 2
26
- @tree.parent(2).must_equal 1
48
+ @tree.parent(1).must_equal 1
49
+ end
50
+
51
+ it "#children should return children." do
52
+ @tree.children(0).must_equal [2,3]
27
53
  end
28
54
 
55
+
56
+
29
57
  end
30
58
 
@@ -63,7 +63,7 @@ describe Digraph do
63
63
 
64
64
  it "#add_edge should add new edge." do
65
65
  @digraph.add_edges([Edge.new("Marc","Kate")])
66
- @digraph.vertex_size.must_equal 6
66
+ @digraph.vertex_size.must_equal 5
67
67
  assert @digraph.edge?("Marc","Kate")
68
68
  refute @digraph.edge?("Kate","Marc")
69
69
  refute @digraph.edge?("Tom","Kate")
@@ -37,9 +37,18 @@ describe Graph do
37
37
  refute @graph.edge?("Tom","Kate")
38
38
  end
39
39
 
40
+ it "#add should add new edge to graph." do
41
+ @graph.add('Jack','Lukas')
42
+ @graph.vertex_size.must_equal 4
43
+ assert @graph.edge?("Jack","Lukas")
44
+ assert @graph.edge?("Lukas","Jack")
45
+ end
46
+
47
+
48
+
40
49
  it "#remove_edge should remove edge." do
41
50
  @graph.remove("Marc","Jack")
42
- @graph.vertex_size.must_equal 3
51
+ @graph.vertex_size.must_equal 4
43
52
  refute @graph.edge?("Marc","Jack")
44
53
  refute @graph.edge?("Jack","Marc")
45
54
  end
@@ -5,6 +5,9 @@ describe List do
5
5
  before do
6
6
  @list = List.from_array([1,2,3,4])
7
7
  @list2 = List.from_array([4,5,7])
8
+
9
+ @numbers = List.from_array((1..10).to_a)
10
+ @even_numbers = List.from_array([2,4,6,8,10])
8
11
  end
9
12
 
10
13
  it "#from_array should transform array to list." do
@@ -49,12 +52,17 @@ describe List do
49
52
  @list.insert_before(7,@list.tail)
50
53
  @list.to_a.must_equal [8,1,9,2,3,7,4]
51
54
 
55
+ proc { @list.insert_before(1,nil)}.must_raise ListError
52
56
  end
53
57
 
54
58
  it "#insert_after should insert new element after another." do
55
59
  second = @list.head.next
56
60
  @list.insert_after(9,second)
57
61
  @list.to_a.must_equal [1,2,9,3,4]
62
+
63
+ tail = @list.tail
64
+ @list.insert_after(11,tail)
65
+ @list.to_a.must_equal [1,2,9,3,4,11]
58
66
  end
59
67
 
60
68
 
@@ -100,6 +108,11 @@ describe List do
100
108
  @list2.merge(@list).to_a.must_equal [1,2,3,4,4,5,7]
101
109
  end
102
110
 
111
+ it "#merge should be symetric operation." do
112
+ @list.merge(@list2).to_a.must_equal [1,2,3,4,4,5,7]
113
+ end
114
+
115
+
103
116
  it "#orderize should order elements by evaluating block." do
104
117
  not_sorted_list = List.from_array([3,-1,0,-8,2,0,1])
105
118
  not_sorted_list.orderize{|elem| elem <=> 0}.to_a.must_equal [-1,-8,0,0,3,2,1]
@@ -107,6 +120,11 @@ describe List do
107
120
 
108
121
  it "#remove! should remove all elements that occur on the other list." do
109
122
  @list.remove!(@list2).to_a.must_equal [1,2,3]
123
+ @numbers.remove!(@even_numbers).to_a.must_equal [1,3,5,7,9]
124
+ end
125
+
126
+ it "#remove! should remove all elements that occur on the other list#2." do
127
+ @list2.remove!(@list).to_a.must_equal [5,7]
110
128
  end
111
129
 
112
130
  it "#looped? should check if list has cycle." do
@@ -114,9 +132,16 @@ describe List do
114
132
  refute @list.looped?
115
133
  end
116
134
 
135
+ it "#each_with_index should iterate list." do
136
+ h = {}
137
+ @list.each_with_index{|e,i| h[i] = e.data}
138
+ h.must_equal({0 => 1, 1 => 2, 2 => 3, 3 => 4})
139
+ end
140
+
117
141
  it "should include Enumerable methods." do
118
- @list.map{ |e| e }.must_equal [1,2,3,4]
119
- @list.inject(0){ |mem, var| mem = mem + var }.must_equal 10
142
+ @list.map{ |e| e.data }.must_equal [1,2,3,4]
143
+ @list.inject(0){ |mem, var| mem = mem + var.data }.must_equal 10
144
+ @list.find { |e| e.data == 1.0 }.data.must_equal 1
120
145
  end
121
146
 
122
147
 
@@ -134,5 +159,37 @@ describe List do
134
159
 
135
160
  end
136
161
 
162
+ if ENV['BENCH']
163
+ describe "performance" do
164
+
165
+ before do
166
+ @arr = (1..10_000).to_a.sort_by{rand}
167
+
168
+ 10000.times do |n|
169
+ @list.append 4
170
+ end
171
+ end
172
+
173
+
174
+ bench_performance_constant "#append should be const operation.", 0.999 do |n|
175
+ n.times do
176
+ @list.append 3
177
+ end
178
+ end
179
+
180
+ bench_performance_constant "#prepend should be const operation.", 0.999 do |n|
181
+ n.times do
182
+ @list.prepend 3
183
+ end
184
+ end
185
+
186
+ bench_performance_linear "#reverse! should be linear operation.", 0.999 do |n|
187
+ list =List.from_array(@arr[0..n])
188
+ list.reverse!
189
+ end
190
+
191
+ end
137
192
  end
138
193
 
194
+ end
195
+
@@ -0,0 +1,28 @@
1
+ require 'help'
2
+
3
+ describe "OrderedSet" do
4
+
5
+ before do
6
+ @set = OrderedSet.new
7
+ @set.push(:first).must_equal 0
8
+ end
9
+
10
+ it "#push should add new element to set and return its index." do
11
+ @set.push(:second).must_equal 1
12
+ @set.push(:first).must_equal 0
13
+ end
14
+
15
+ it "#index should return element index." do
16
+ @set.index(:first).must_equal 0
17
+ @set.push(:second)
18
+ @set.index(:second).must_equal 1
19
+ @set.index(:non_exist).must_be_nil
20
+ end
21
+
22
+ it "#to_a should return array." do
23
+ @set.push(:second)
24
+ @set.to_a.must_equal [:first,:second]
25
+ end
26
+
27
+ end
28
+
@@ -0,0 +1,26 @@
1
+ require 'help'
2
+
3
+ describe Pair do
4
+
5
+ before do
6
+ @pair = Pair.new(1,2)
7
+ end
8
+
9
+ it "#first should return first element of a pair" do
10
+ @pair.first.must_equal 1
11
+ end
12
+
13
+ it "#key should return first element of a pair" do
14
+ @pair.key.must_equal 1
15
+ end
16
+
17
+ it "#second should return second element of a pair" do
18
+ @pair.second.must_equal 2
19
+ end
20
+
21
+ it "#value should return second element of a pair" do
22
+ @pair.value.must_equal 2
23
+ end
24
+
25
+ end
26
+
@@ -0,0 +1,134 @@
1
+ require 'help'
2
+
3
+ describe PriorityQueue do
4
+
5
+ describe "Empty Priority Queue" do
6
+
7
+ before do
8
+ @empty_queue = PriorityQueue.new
9
+ end
10
+
11
+ it "should be empty." do
12
+ assert @empty_queue.empty?
13
+ end
14
+
15
+ it "should have zero elements." do
16
+ @empty_queue.length.must_equal 0
17
+ end
18
+ end
19
+
20
+
21
+ describe "Not empty priority queue" do
22
+
23
+ before do
24
+ @queue = PriorityQueue.new
25
+ @queue.enqueue :important, 2
26
+ @queue.enqueue :not_important, 1
27
+ end
28
+
29
+ it "should not be empty." do
30
+ refute @queue.empty?
31
+ @queue.length.must_equal 2
32
+
33
+ end
34
+
35
+ it "#peek should return element with highest priority." do
36
+ @queue.peek.must_equal :important
37
+ end
38
+
39
+
40
+ it "#enqueue and #push should add element to priority queue." do
41
+ @queue.enqueue :very_important, 5
42
+ @queue.push :nevermind, 0
43
+ @queue.length.must_equal 4
44
+ end
45
+
46
+ it "#dequeue and #shift should remove element with highest priority." do
47
+ x = @queue.dequeue
48
+ @queue.length.must_equal 1
49
+ x.must_equal :important
50
+ @queue.dequeue.must_equal :not_important
51
+ @queue.length.must_equal 0
52
+ @queue.dequeue.must_be_nil
53
+ end
54
+ end
55
+
56
+ describe "Priority Queue with duplications" do
57
+
58
+ before do
59
+ @dup_queue = PriorityQueue.new
60
+ @dup_queue.enqueue :same_important, 2
61
+ @dup_queue.enqueue :important, 2
62
+ @dup_queue.enqueue :not_important, 1
63
+ end
64
+
65
+ it "should not be empty" do
66
+ refute @dup_queue.empty?
67
+ @dup_queue.length.must_equal 3
68
+ end
69
+
70
+ it "#peek should return element with highest priority." do
71
+ assert [:important,:same_important].include? @dup_queue.peek
72
+ @dup_queue.dequeue
73
+ assert [:important,:same_important].include? @dup_queue.peek
74
+ @dup_queue.dequeue
75
+ @dup_queue.peek.must_equal :not_important
76
+ end
77
+
78
+ it "#enqueue and #push should add element to priority queue." do
79
+ @dup_queue.push :nevermind, 0
80
+ @dup_queue.push :another_important, 2
81
+ @dup_queue.length.must_equal 5
82
+ end
83
+
84
+ it "#dequeue and #shift should remove element with highest priority." do
85
+ x = @dup_queue.dequeue
86
+ assert [:important,:same_important].include? x
87
+ @dup_queue.length.must_equal 2
88
+
89
+ @dup_queue.enqueue :important, 2
90
+ @dup_queue.length.must_equal 3
91
+ @dup_queue.dequeue.must_equal :important
92
+ @dup_queue.dequeue.must_equal :important
93
+ end
94
+ end
95
+
96
+
97
+ if ENV['BENCH']
98
+ describe "performance" do
99
+
100
+ before do
101
+
102
+ @queue = PriorityQueue.new
103
+ 10000.times do |n|
104
+ @queue.push :elem, rand(10)
105
+ end
106
+
107
+ @empty_queue = PriorityQueue.new
108
+ end
109
+
110
+
111
+ bench_performance_constant "#accessing max element should be const operation.", 0.999 do |n|
112
+ n.times do
113
+ @queue.peek
114
+ end
115
+ end
116
+
117
+
118
+ bench_performance_constant "#removing max element should be log operation.", 0.999 do |n|
119
+ n.times do
120
+ @queue.dequeue
121
+ end
122
+ end
123
+
124
+ bench_performance_constant "creating new priority queue should be linear operation.", 0.999 do |n|
125
+ n.times do
126
+ @empty_queue.push :elem, rand(10)
127
+ end
128
+ end
129
+
130
+
131
+
132
+ end
133
+ end
134
+ end