queue_test.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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.QueueTest');
  15. goog.setTestOnly('goog.structs.QueueTest');
  16. goog.require('goog.structs.Queue');
  17. goog.require('goog.testing.jsunit');
  18. function stringifyQueue(q) {
  19. var values = q.getValues();
  20. var s = '';
  21. for (var i = 0; i < values.length; i++) {
  22. s += values[i];
  23. }
  24. return s;
  25. }
  26. function createQueue() {
  27. var q = new goog.structs.Queue();
  28. q.enqueue('a');
  29. q.enqueue('b');
  30. q.enqueue('c');
  31. q.enqueue('a');
  32. q.dequeue();
  33. q.enqueue('b');
  34. q.enqueue('c');
  35. // q is now: bcabc
  36. return q;
  37. }
  38. function testConstructor() {
  39. var q = new goog.structs.Queue();
  40. assertTrue('testConstructor(), queue should be empty initially', q.isEmpty());
  41. assertEquals('testConstructor(), count should be 0', q.getCount(), 0);
  42. assertEquals(
  43. 'testConstructor(), head element should be undefined', q.peek(),
  44. undefined);
  45. }
  46. function testCount() {
  47. var q = createQueue();
  48. assertEquals('testCount(), count should be 5', q.getCount(), 5);
  49. q.enqueue('d');
  50. assertEquals('testCount(), count should be 6', q.getCount(), 6);
  51. q.dequeue();
  52. assertEquals('testCount(), count should be 5', q.getCount(), 5);
  53. q.clear();
  54. assertEquals('testCount(), count should be 0', q.getCount(), 0);
  55. }
  56. function testEnqueue() {
  57. var q = new goog.structs.Queue();
  58. q.enqueue('a');
  59. assertEquals('testEnqueue(), count should be 1', q.getCount(), 1);
  60. q.enqueue('b');
  61. assertEquals('testEnqueue(), count should be 2', q.getCount(), 2);
  62. assertEquals('testEnqueue(), head element should be a', q.peek(), 'a');
  63. q.dequeue();
  64. assertEquals('testEnqueue(), count should be 1', q.getCount(), 1);
  65. assertEquals('testEnqueue(), head element should be b', q.peek(), 'b');
  66. }
  67. function testDequeue() {
  68. var q = createQueue();
  69. assertEquals('testDequeue(), should return b', q.dequeue(), 'b');
  70. assertEquals('testDequeue(), should return b', q.dequeue(), 'c');
  71. assertEquals('testDequeue(), should return b', q.dequeue(), 'a');
  72. assertEquals('testDequeue(), should return b', q.dequeue(), 'b');
  73. assertEquals('testDequeue(), should return b', q.dequeue(), 'c');
  74. assertTrue('testDequeue(), queue should be empty', q.isEmpty());
  75. assertEquals(
  76. 'testDequeue(), should return undefined for empty queue', q.dequeue(),
  77. undefined);
  78. }
  79. function testPeek() {
  80. var q = createQueue();
  81. assertEquals('testPeek(), should return b', q.peek(), 'b');
  82. assertEquals(
  83. 'testPeek(), dequeue should return peek() result', q.dequeue(), 'b');
  84. assertEquals('testPeek(), should return b', q.peek(), 'c');
  85. q.clear();
  86. assertEquals(
  87. 'testPeek(), should return undefined for empty queue', q.peek(),
  88. undefined);
  89. }
  90. function testClear() {
  91. var q = createQueue();
  92. q.clear();
  93. assertTrue('testClear(), queue should be empty', q.isEmpty());
  94. }
  95. function testQueue() {
  96. var q = createQueue();
  97. assertEquals(
  98. 'testQueue(), contents must be bcabc', stringifyQueue(q), 'bcabc');
  99. }
  100. function testRemove() {
  101. var q = createQueue();
  102. assertEquals(
  103. 'testRemove(), contents must be bcabc', stringifyQueue(q), 'bcabc');
  104. q.dequeue();
  105. assertEquals(
  106. 'testRemove(), contents must be cabc', stringifyQueue(q), 'cabc');
  107. q.enqueue('a');
  108. assertEquals(
  109. 'testRemove(), contents must be cabca', stringifyQueue(q), 'cabca');
  110. assertTrue('testRemove(), remove should have returned true', q.remove('c'));
  111. assertEquals(
  112. 'testRemove(), contents must be abca', stringifyQueue(q), 'abca');
  113. assertTrue('testRemove(), remove should have returned true', q.remove('b'));
  114. assertEquals('testRemove(), contents must be aca', stringifyQueue(q), 'aca');
  115. assertFalse('testRemove(), remove should have returned false', q.remove('b'));
  116. assertEquals('testRemove(), contents must be aca', stringifyQueue(q), 'aca');
  117. assertTrue('testRemove(), remove should have returned true', q.remove('a'));
  118. assertEquals('testRemove(), contents must be ca', stringifyQueue(q), 'ca');
  119. assertTrue('testRemove(), remove should have returned true', q.remove('a'));
  120. assertEquals('testRemove(), contents must be c', stringifyQueue(q), 'c');
  121. assertTrue('testRemove(), remove should have returned true', q.remove('c'));
  122. assertEquals('testRemove(), contents must be empty', stringifyQueue(q), '');
  123. q.enqueue('a');
  124. q.enqueue('b');
  125. q.enqueue('c');
  126. q.enqueue('a');
  127. q.dequeue();
  128. q.enqueue('b');
  129. q.enqueue('c');
  130. assertEquals(
  131. 'testRemove(), contents must be bcabc', stringifyQueue(q), 'bcabc');
  132. assertTrue('testRemove(), remove should have returned true', q.remove('c'));
  133. assertEquals(
  134. 'testRemove(), contents must be babc', stringifyQueue(q), 'babc');
  135. }
  136. function testContains() {
  137. var q = createQueue();
  138. assertTrue(
  139. 'testContains(), contains should have returned true', q.contains('a'));
  140. assertFalse(
  141. 'testContains(), contains should have returned false',
  142. q.contains('foobar'));
  143. }