priorityqueue_test.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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.PriorityQueueTest');
  15. goog.setTestOnly('goog.structs.PriorityQueueTest');
  16. goog.require('goog.structs');
  17. goog.require('goog.structs.PriorityQueue');
  18. goog.require('goog.testing.jsunit');
  19. function getPriorityQueue() {
  20. var p = new goog.structs.PriorityQueue();
  21. p.enqueue(0, 'a');
  22. p.enqueue(1, 'b');
  23. p.enqueue(2, 'c');
  24. p.enqueue(3, 'd');
  25. return p;
  26. }
  27. function getPriorityQueue2() {
  28. var p = new goog.structs.PriorityQueue();
  29. p.insert(1, 'b');
  30. p.insert(3, 'd');
  31. p.insert(0, 'a');
  32. p.insert(2, 'c');
  33. return p;
  34. }
  35. function testGetCount1() {
  36. var p = getPriorityQueue();
  37. assertEquals('count, should be 4', p.getCount(), 4);
  38. p.dequeue();
  39. assertEquals('count, should be 3', p.getCount(), 3);
  40. }
  41. function testGetCount2() {
  42. var p = getPriorityQueue();
  43. assertEquals('count, should be 4', p.getCount(), 4);
  44. p.dequeue();
  45. assertEquals('count, should be 3', p.getCount(), 3);
  46. }
  47. function testGetCount3() {
  48. var p = getPriorityQueue();
  49. p.dequeue();
  50. p.dequeue();
  51. p.dequeue();
  52. p.dequeue();
  53. assertEquals('count, should be 0', p.getCount(), 0);
  54. }
  55. function testKeys() {
  56. var p = getPriorityQueue();
  57. var keys = p.getKeys();
  58. for (var i = 0; i < 4; i++) {
  59. assertTrue('getKeys, key ' + i + ' found', goog.structs.contains(keys, i));
  60. }
  61. assertEquals('getKeys, Should be 4 keys', goog.structs.getCount(keys), 4);
  62. }
  63. function testValues() {
  64. var p = getPriorityQueue();
  65. var values = p.getValues();
  66. assertTrue('getKeys, value "a" found', goog.structs.contains(values, 'a'));
  67. assertTrue('getKeys, value "b" found', goog.structs.contains(values, 'b'));
  68. assertTrue('getKeys, value "c" found', goog.structs.contains(values, 'c'));
  69. assertTrue('getKeys, value "d" found', goog.structs.contains(values, 'd'));
  70. assertEquals('getKeys, Should be 4 keys', goog.structs.getCount(values), 4);
  71. }
  72. function testClear() {
  73. var p = getPriorityQueue();
  74. p.clear();
  75. assertTrue('cleared so it should be empty', p.isEmpty());
  76. }
  77. function testIsEmpty() {
  78. var p = getPriorityQueue();
  79. assertFalse('4 values so should not be empty', p.isEmpty());
  80. p.dequeue();
  81. p.dequeue();
  82. p.dequeue();
  83. assertFalse('1 values so should not be empty', p.isEmpty());
  84. p.dequeue();
  85. assertTrue('0 values so should be empty', p.isEmpty());
  86. }
  87. function testPeek1() {
  88. var p = getPriorityQueue();
  89. assertEquals('peek, Should be "a"', p.peek(), 'a');
  90. }
  91. function testPeek2() {
  92. var p = getPriorityQueue2();
  93. assertEquals('peek, Should be "a"', p.peek(), 'a');
  94. }
  95. function testPeek3() {
  96. var p = getPriorityQueue();
  97. p.clear();
  98. assertEquals('peek, Should be "a"', p.peek(), undefined);
  99. }
  100. function testDequeue1() {
  101. var p = getPriorityQueue();
  102. assertEquals('dequeue, Should be "a"', p.dequeue(), 'a');
  103. assertEquals('dequeue, Should be "b"', p.dequeue(), 'b');
  104. assertEquals('dequeue, Should be "c"', p.dequeue(), 'c');
  105. assertEquals('dequeue, Should be "d"', p.dequeue(), 'd');
  106. }
  107. function testDequeue2() {
  108. var p = getPriorityQueue2();
  109. assertEquals('dequeue, Should be "a"', p.dequeue(), 'a');
  110. assertEquals('dequeue, Should be "b"', p.dequeue(), 'b');
  111. assertEquals('dequeue, Should be "c"', p.dequeue(), 'c');
  112. assertEquals('dequeue, Should be "d"', p.dequeue(), 'd');
  113. }
  114. function testEnqueuePeek1() {
  115. var p = new goog.structs.PriorityQueue();
  116. p.enqueue(3, 'd');
  117. assertEquals('peak, Should be "d"', p.peek(), 'd');
  118. p.enqueue(2, 'c');
  119. assertEquals('peak, Should be "c"', p.peek(), 'c');
  120. p.enqueue(1, 'b');
  121. assertEquals('peak, Should be "b"', p.peek(), 'b');
  122. p.enqueue(0, 'a');
  123. assertEquals('peak, Should be "a"', p.peek(), 'a');
  124. }
  125. function testEnqueuePeek2() {
  126. var p = new goog.structs.PriorityQueue();
  127. p.enqueue(1, 'b');
  128. assertEquals('peak, Should be "b"', p.peek(), 'b');
  129. p.enqueue(3, 'd');
  130. assertEquals('peak, Should be "b"', p.peek(), 'b');
  131. p.enqueue(0, 'a');
  132. assertEquals('peak, Should be "a"', p.peek(), 'a');
  133. p.enqueue(2, 'c');
  134. assertEquals('peak, Should be "a"', p.peek(), 'a');
  135. }