Debian Codegraph @

closure-library/closure/goog/structs/priorityqueue_test.js

raw
1 // Copyright 2006 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 goog.provide('goog.structs.PriorityQueueTest');
16 goog.setTestOnly('goog.structs.PriorityQueueTest');
17
18 goog.require('goog.structs');
19 goog.require('goog.structs.PriorityQueue');
20 goog.require('goog.testing.jsunit');
21
22 function getPriorityQueue() {
23 var p = new goog.structs.PriorityQueue();
24 p.enqueue(0, 'a');
25 p.enqueue(1, 'b');
26 p.enqueue(2, 'c');
27 p.enqueue(3, 'd');
28 return p;
29 }
30
31
32 function getPriorityQueue2() {
33 var p = new goog.structs.PriorityQueue();
34 p.insert(1, 'b');
35 p.insert(3, 'd');
36 p.insert(0, 'a');
37 p.insert(2, 'c');
38 return p;
39 }
40
41
42 function testGetCount1() {
43 var p = getPriorityQueue();
44 assertEquals('count, should be 4', p.getCount(), 4);
45 p.dequeue();
46 assertEquals('count, should be 3', p.getCount(), 3);
47 }
48
49
50 function testGetCount2() {
51 var p = getPriorityQueue();
52 assertEquals('count, should be 4', p.getCount(), 4);
53 p.dequeue();
54 assertEquals('count, should be 3', p.getCount(), 3);
55 }
56
57
58 function testGetCount3() {
59 var p = getPriorityQueue();
60 p.dequeue();
61 p.dequeue();
62 p.dequeue();
63 p.dequeue();
64 assertEquals('count, should be 0', p.getCount(), 0);
65 }
66
67
68 function testKeys() {
69 var p = getPriorityQueue();
70 var keys = p.getKeys();
71 for (var i = 0; i < 4; i++) {
72 assertTrue('getKeys, key ' + i + ' found', goog.structs.contains(keys, i));
73 }
74 assertEquals('getKeys, Should be 4 keys', goog.structs.getCount(keys), 4);
75 }
76
77
78 function testValues() {
79 var p = getPriorityQueue();
80 var values = p.getValues();
81
82 assertTrue('getKeys, value "a" found', goog.structs.contains(values, 'a'));
83 assertTrue('getKeys, value "b" found', goog.structs.contains(values, 'b'));
84 assertTrue('getKeys, value "c" found', goog.structs.contains(values, 'c'));
85 assertTrue('getKeys, value "d" found', goog.structs.contains(values, 'd'));
86 assertEquals('getKeys, Should be 4 keys', goog.structs.getCount(values), 4);
87 }
88
89
90 function testClear() {
91 var p = getPriorityQueue();
92 p.clear();
93 assertTrue('cleared so it should be empty', p.isEmpty());
94 }
95
96
97 function testIsEmpty() {
98 var p = getPriorityQueue();
99 assertFalse('4 values so should not be empty', p.isEmpty());
100
101 p.dequeue();
102 p.dequeue();
103 p.dequeue();
104 assertFalse('1 values so should not be empty', p.isEmpty());
105
106 p.dequeue();
107 assertTrue('0 values so should be empty', p.isEmpty());
108 }
109
110
111 function testPeek1() {
112 var p = getPriorityQueue();
113 assertEquals('peek, Should be "a"', p.peek(), 'a');
114 }
115
116
117 function testPeek2() {
118 var p = getPriorityQueue2();
119 assertEquals('peek, Should be "a"', p.peek(), 'a');
120 }
121
122
123 function testPeek3() {
124 var p = getPriorityQueue();
125 p.clear();
126 assertEquals('peek, Should be "a"', p.peek(), undefined);
127 }
128
129
130 function testDequeue1() {
131 var p = getPriorityQueue();
132
133 assertEquals('dequeue, Should be "a"', p.dequeue(), 'a');
134 assertEquals('dequeue, Should be "b"', p.dequeue(), 'b');
135 assertEquals('dequeue, Should be "c"', p.dequeue(), 'c');
136 assertEquals('dequeue, Should be "d"', p.dequeue(), 'd');
137 }
138
139
140 function testDequeue2() {
141 var p = getPriorityQueue2();
142
143 assertEquals('dequeue, Should be "a"', p.dequeue(), 'a');
144 assertEquals('dequeue, Should be "b"', p.dequeue(), 'b');
145 assertEquals('dequeue, Should be "c"', p.dequeue(), 'c');
146 assertEquals('dequeue, Should be "d"', p.dequeue(), 'd');
147 }
148
149
150 function testEnqueuePeek1() {
151 var p = new goog.structs.PriorityQueue();
152
153 p.enqueue(3, 'd');
154 assertEquals('peak, Should be "d"', p.peek(), 'd');
155 p.enqueue(2, 'c');
156 assertEquals('peak, Should be "c"', p.peek(), 'c');
157 p.enqueue(1, 'b');
158 assertEquals('peak, Should be "b"', p.peek(), 'b');
159 p.enqueue(0, 'a');
160 assertEquals('peak, Should be "a"', p.peek(), 'a');
161 }
162
163
164 function testEnqueuePeek2() {
165 var p = new goog.structs.PriorityQueue();
166
167 p.enqueue(1, 'b');
168 assertEquals('peak, Should be "b"', p.peek(), 'b');
169 p.enqueue(3, 'd');
170 assertEquals('peak, Should be "b"', p.peek(), 'b');
171 p.enqueue(0, 'a');
172 assertEquals('peak, Should be "a"', p.peek(), 'a');
173 p.enqueue(2, 'c');
174 assertEquals('peak, Should be "a"', p.peek(), 'a');
175 }