heap_test.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. goog.provide('goog.structs.HeapTest');
  15. goog.setTestOnly('goog.structs.HeapTest');
  16. goog.require('goog.structs');
  17. goog.require('goog.structs.Heap');
  18. goog.require('goog.testing.jsunit');
  19. /**
  20. * Constructs a heap from key-value pairs passed as arguments
  21. * @param {...!Array} var_args List of length-2 arrays [key, value]
  22. * @return {goog.structs.Heap} Heap constructed from passed in key-value pairs
  23. */
  24. function makeHeap(var_args) {
  25. var h = new goog.structs.Heap();
  26. var key, value;
  27. for (var i = 0; i < arguments.length; i++) {
  28. key = arguments[i][0];
  29. value = arguments[i][1];
  30. h.insert(key, value);
  31. }
  32. return h;
  33. }
  34. function testGetCount1() {
  35. var h = makeHeap([0, 'a'], [1, 'b'], [2, 'c'], [3, 'd']);
  36. assertEquals('count, should be 4', 4, h.getCount());
  37. h.remove();
  38. assertEquals('count, should be 3', 3, h.getCount());
  39. }
  40. function testGetCount2() {
  41. var h = makeHeap([0, 'a'], [1, 'b'], [2, 'c'], [3, 'd']);
  42. h.remove();
  43. h.remove();
  44. h.remove();
  45. h.remove();
  46. assertEquals('count, should be 0', 0, h.getCount());
  47. }
  48. function testKeys() {
  49. var h = makeHeap([0, 'a'], [1, 'b'], [2, 'c'], [3, 'd']);
  50. var keys = h.getKeys();
  51. for (var i = 0; i < 4; i++) {
  52. assertTrue('getKeys, key ' + i + ' found', goog.structs.contains(keys, i));
  53. }
  54. assertEquals('getKeys, Should be 4 keys', 4, goog.structs.getCount(keys));
  55. }
  56. function testValues() {
  57. var h = makeHeap([0, 'a'], [1, 'b'], [2, 'c'], [3, 'd']);
  58. var values = h.getValues();
  59. assertTrue('getKeys, value "a" found', goog.structs.contains(values, 'a'));
  60. assertTrue('getKeys, value "b" found', goog.structs.contains(values, 'b'));
  61. assertTrue('getKeys, value "c" found', goog.structs.contains(values, 'c'));
  62. assertTrue('getKeys, value "d" found', goog.structs.contains(values, 'd'));
  63. assertEquals('getKeys, Should be 4 keys', 4, goog.structs.getCount(values));
  64. }
  65. function testContainsKey() {
  66. var h = makeHeap([0, 'a'], [1, 'b'], [2, 'c'], [3, 'd']);
  67. for (var i = 0; i < 4; i++) {
  68. assertTrue('containsKey, key ' + i + ' found', h.containsKey(i));
  69. }
  70. assertFalse('containsKey, value 4 not found', h.containsKey(4));
  71. }
  72. function testContainsValue() {
  73. var h = makeHeap([0, 'a'], [1, 'b'], [2, 'c'], [3, 'd']);
  74. assertTrue('containsValue, value "a" found', h.containsValue('a'));
  75. assertTrue('containsValue, value "b" found', h.containsValue('b'));
  76. assertTrue('containsValue, value "c" found', h.containsValue('c'));
  77. assertTrue('containsValue, value "d" found', h.containsValue('d'));
  78. assertFalse('containsValue, value "e" not found', h.containsValue('e'));
  79. }
  80. function testClone() {
  81. var h = makeHeap([0, 'a'], [1, 'b'], [2, 'c'], [3, 'd']);
  82. var h2 = h.clone();
  83. assertTrue('clone so it should not be empty', !h2.isEmpty());
  84. assertTrue('clone so it should contain key 0', h2.containsKey(0));
  85. assertTrue('clone so it should contain value "a"', h2.containsValue('a'));
  86. }
  87. function testClear() {
  88. var h = makeHeap([0, 'a'], [1, 'b'], [2, 'c'], [3, 'd']);
  89. h.clear();
  90. assertTrue('cleared so it should be empty', h.isEmpty());
  91. }
  92. function testIsEmpty() {
  93. var h = makeHeap([0, 'a'], [1, 'b'], [2, 'c'], [3, 'd']);
  94. assertFalse('4 values so should not be empty', h.isEmpty());
  95. h.remove();
  96. h.remove();
  97. h.remove();
  98. assertFalse('1 values so should not be empty', h.isEmpty());
  99. h.remove();
  100. assertTrue('0 values so should be empty', h.isEmpty());
  101. }
  102. function testPeek1() {
  103. var h = makeHeap([0, 'a'], [1, 'b'], [2, 'c'], [3, 'd']);
  104. assertEquals('peek, Should be "a"', 'a', h.peek());
  105. }
  106. function testPeek2() {
  107. var h = makeHeap([1, 'b'], [3, 'd'], [0, 'a'], [2, 'c']);
  108. assertEquals('peek, Should be "a"', 'a', h.peek());
  109. }
  110. function testPeek3() {
  111. var h = makeHeap([0, 'a'], [1, 'b'], [2, 'c'], [3, 'd']);
  112. h.clear();
  113. assertEquals('peek, Should be "undefined"', undefined, h.peek());
  114. }
  115. function testPeekKey1() {
  116. var h = makeHeap([0, 'a'], [1, 'b'], [2, 'c'], [3, 'd']);
  117. assertEquals('peekKey, Should be "0"', 0, h.peekKey());
  118. }
  119. function testPeekKey2() {
  120. var h = makeHeap([1, 'b'], [3, 'd'], [0, 'a'], [2, 'c']);
  121. assertEquals('peekKey, Should be "0"', 0, h.peekKey());
  122. }
  123. function testPeekKey3() {
  124. var h = makeHeap([0, 'a'], [1, 'b'], [2, 'c'], [3, 'd']);
  125. h.clear();
  126. assertEquals('peekKey, Should be "undefined"', undefined, h.peekKey());
  127. }
  128. function testRemove1() {
  129. var h = makeHeap([0, 'a'], [1, 'b'], [2, 'c'], [3, 'd']);
  130. assertEquals('remove, Should be "a"', 'a', h.remove());
  131. assertEquals('remove, Should be "b"', 'b', h.remove());
  132. assertEquals('remove, Should be "c"', 'c', h.remove());
  133. assertEquals('remove, Should be "d"', 'd', h.remove());
  134. }
  135. function testRemove2() {
  136. var h = makeHeap([1, 'b'], [3, 'd'], [0, 'a'], [2, 'c']);
  137. assertEquals('remove, Should be "a"', 'a', h.remove());
  138. assertEquals('remove, Should be "b"', 'b', h.remove());
  139. assertEquals('remove, Should be "c"', 'c', h.remove());
  140. assertEquals('remove, Should be "d"', 'd', h.remove());
  141. }
  142. function testInsertPeek1() {
  143. var h = makeHeap();
  144. h.insert(3, 'd');
  145. assertEquals('peek, Should be "d"', 'd', h.peek());
  146. h.insert(2, 'c');
  147. assertEquals('peek, Should be "c"', 'c', h.peek());
  148. h.insert(1, 'b');
  149. assertEquals('peek, Should be "b"', 'b', h.peek());
  150. h.insert(0, 'a');
  151. assertEquals('peek, Should be "a"', 'a', h.peek());
  152. }
  153. function testInsertPeek2() {
  154. var h = makeHeap();
  155. h.insert(1, 'b');
  156. assertEquals('peek, Should be "b"', 'b', h.peek());
  157. h.insert(3, 'd');
  158. assertEquals('peek, Should be "b"', 'b', h.peek());
  159. h.insert(0, 'a');
  160. assertEquals('peek, Should be "a"', 'a', h.peek());
  161. h.insert(2, 'c');
  162. assertEquals('peek, Should be "a"', 'a', h.peek());
  163. }
  164. function testInsertAllPeek1() {
  165. var h1 = makeHeap([1, 'e']);
  166. var h2 = makeHeap([0, 'a'], [1, 'b'], [2, 'c'], [3, 'd']);
  167. h1.insertAll(h2);
  168. assertEquals('peek, should be "a"', 'a', h1.peek());
  169. }
  170. function testInsertAllPeek2() {
  171. var h1 = makeHeap([-1, 'z']);
  172. var h2 = makeHeap([0, 'a'], [1, 'b'], [2, 'c'], [3, 'd']);
  173. h1.insertAll(h2);
  174. assertEquals('peek, should be "z"', 'z', h1.peek());
  175. }
  176. function testInsertAllPeek3() {
  177. var h1 = makeHeap();
  178. var h2 = makeHeap([0, 'a'], [1, 'b'], [2, 'c'], [3, 'd']);
  179. h1.insertAll(h2);
  180. assertEquals('peek, should be "a"', 'a', h1.peek());
  181. }