aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/structs/heap_test.html
blob: de7e093051f454280960e1ee218e5b56580e0332 (plain)
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
<!DOCTYPE html>
<html>
<!--
Copyright 2006 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Closure Unit Tests - goog.structs.Heap</title>
<script src="../base.js"></script>
<script>
  goog.require('goog.structs');
  goog.require('goog.structs.Heap');
  goog.require('goog.testing.jsunit');
</script>
</head>
<body>
<script>

function getHeap() {
  var h = new goog.structs.Heap();
  h.insert(0, 'a');
  h.insert(1, 'b');
  h.insert(2, 'c');
  h.insert(3, 'd');
  return h;
}


function getHeap2() {
  var h = new goog.structs.Heap();
  h.insert(1, 'b');
  h.insert(3, 'd');
  h.insert(0, 'a');
  h.insert(2, 'c');
  return h;
}


function testGetCount1() {
  var h = getHeap();
  assertEquals('count, should be 4', h.getCount(), 4);
  h.remove();
  assertEquals('count, should be 3', h.getCount(), 3);
}

function testGetCount2() {
  var h = getHeap();
  h.remove();
  h.remove();
  h.remove();
  h.remove();
  assertEquals('count, should be 0', h.getCount(), 0);
}


function testKeys() {
  var h = getHeap();
  var keys = h.getKeys();
  for (var i = 0; i < 4; i++) {
    assertTrue('getKeys, key ' + i + ' found', goog.structs.contains(keys, i));
  }
  assertEquals('getKeys, Should be 4 keys', goog.structs.getCount(keys), 4);
}


function testValues() {
  var h = getHeap();
  var values = h.getValues();

  assertTrue('getKeys, value "a" found', goog.structs.contains(values, 'a'));
  assertTrue('getKeys, value "b" found', goog.structs.contains(values, 'b'));
  assertTrue('getKeys, value "c" found', goog.structs.contains(values, 'c'));
  assertTrue('getKeys, value "d" found', goog.structs.contains(values, 'd'));
  assertEquals('getKeys, Should be 4 keys', goog.structs.getCount(values), 4);
}


function testContainsKey() {
  var h = getHeap();

  for (var i = 0; i < 4; i++) {
    assertTrue('containsKey, key ' + i + ' found', h.containsKey(i));
  }
  assertFalse('containsKey, value 4 not found', h.containsKey(4));
}


function testContainsValue() {
  var h = getHeap();

  assertTrue('containsValue, value "a" found', h.containsValue('a'));
  assertTrue('containsValue, value "b" found', h.containsValue('b'));
  assertTrue('containsValue, value "c" found', h.containsValue('c'));
  assertTrue('containsValue, value "d" found', h.containsValue('d'));
  assertFalse('containsValue, value "e" not found', h.containsValue('e'));
}


function testClone() {
  var h = getHeap();
  var h2 = h.clone();
  assertTrue('clone so it should not be empty', !h2.isEmpty());
  assertTrue('clone so it should contain key 0', h2.containsKey(0));
  assertTrue('clone so it should contain value "a"', h2.containsValue('a'));
}


function testClear() {
  var h = getHeap();
  h.clear();
  assertTrue('cleared so it should be empty', h.isEmpty());
}


function testIsEmpty() {
  var h = getHeap();
  assertFalse('4 values so should not be empty', h.isEmpty());

  h.remove();
  h.remove();
  h.remove();
  assertFalse('1 values so should not be empty', h.isEmpty());

  h.remove();
  assertTrue('0 values so should be empty', h.isEmpty());
}


function testPeek1() {
  var h = getHeap();
  assertEquals('peek, Should be "a"', h.peek(), 'a');
}


function testPeek2() {
  var h = getHeap2();
  assertEquals('peek, Should be "a"', h.peek(), 'a');
}


function testPeek3() {
  var h = getHeap();
  h.clear();
  assertEquals('peek, Should be "undefined"', h.peek(), undefined);
}


function testPeekKey1() {
  var h = getHeap();
  assertEquals('peekKey, Should be "0"', h.peekKey(), 0);
}


function testPeekKey2() {
  var h = getHeap2();
  assertEquals('peekKey, Should be "0"', h.peekKey(), 0);
}


function testPeekKey3() {
  var h = getHeap();
  h.clear();
  assertEquals('peekKey, Should be "undefined"', h.peekKey(), undefined);
}


function testRemove1() {
  var h = getHeap();

  assertEquals('remove, Should be "a"', h.remove(), 'a');
  assertEquals('remove, Should be "b"', h.remove(), 'b');
  assertEquals('remove, Should be "c"', h.remove(), 'c');
  assertEquals('remove, Should be "d"', h.remove(), 'd');
}


function testRemove2() {
  var h = getHeap2();

  assertEquals('remove, Should be "a"', h.remove(), 'a');
  assertEquals('remove, Should be "b"', h.remove(), 'b');
  assertEquals('remove, Should be "c"', h.remove(), 'c');
  assertEquals('remove, Should be "d"', h.remove(), 'd');
}


function testInsertPeek1() {
  var h = new goog.structs.Heap();

  h.insert(3, 'd');
  assertEquals('peek, Should be "d"', h.peek(), 'd');
  h.insert(2, 'c');
  assertEquals('peek, Should be "c"', h.peek(), 'c');
  h.insert(1, 'b');
  assertEquals('peek, Should be "b"', h.peek(), 'b');
  h.insert(0, 'a');
  assertEquals('peek, Should be "a"', h.peek(), 'a');
}


function testInsertPeek2() {
  var h = new goog.structs.Heap();

  h.insert(1, 'b');
  assertEquals('peak, Should be "b"', h.peek(), 'b');
  h.insert(3, 'd');
  assertEquals('peak, Should be "b"', h.peek(), 'b');
  h.insert(0, 'a');
  assertEquals('peak, Should be "a"', h.peek(), 'a');
  h.insert(2, 'c');
  assertEquals('peak, Should be "a"', h.peek(), 'a');
}


</script>
</body>
</html>