circularbuffer_test.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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.CircularBufferTest');
  15. goog.setTestOnly('goog.structs.CircularBufferTest');
  16. goog.require('goog.structs.CircularBuffer');
  17. goog.require('goog.testing.jsunit');
  18. function testCircularBuffer() {
  19. var buff = new goog.structs.CircularBuffer(2);
  20. assertUndefined(buff.add('first'));
  21. assertEquals(1, buff.getCount());
  22. assertEquals('first', buff.get(0));
  23. assertEquals('first', buff.getLast());
  24. assertUndefined(buff.add('second'));
  25. assertEquals(2, buff.getCount());
  26. assertEquals('first', buff.get(0));
  27. assertEquals('second', buff.get(1));
  28. assertEquals('second', buff.getLast());
  29. assertEquals('first', buff.add('third'));
  30. assertEquals(2, buff.getCount());
  31. assertEquals('second', buff.get(0));
  32. assertEquals('third', buff.get(1));
  33. assertEquals('third', buff.getLast());
  34. }
  35. function testIsEmpty() {
  36. var buff = new goog.structs.CircularBuffer(2);
  37. assertTrue('initially empty', buff.isEmpty());
  38. assertUndefined(buff.add('first'));
  39. assertFalse('not empty after add empty', buff.isEmpty());
  40. }
  41. function testClear() {
  42. var buff = new goog.structs.CircularBuffer(2);
  43. assertUndefined(buff.add('first'));
  44. buff.clear();
  45. assertTrue('should be empty after clear', buff.isEmpty());
  46. }
  47. function testGetValues() {
  48. var buff = new goog.structs.CircularBuffer(2);
  49. assertUndefined(buff.add('first'));
  50. assertUndefined(buff.add('second'));
  51. assertArrayEquals(['first', 'second'], buff.getValues());
  52. }
  53. function testGetNewestValues() {
  54. var buff = new goog.structs.CircularBuffer(5);
  55. assertUndefined(buff.add('first'));
  56. assertUndefined(buff.add('second'));
  57. assertUndefined(buff.add('third'));
  58. assertUndefined(buff.add('fourth'));
  59. assertUndefined(buff.add('fifth'));
  60. assertArrayEquals(['fourth', 'fifth'], buff.getNewestValues(2));
  61. }
  62. function testGetKeys() {
  63. var buff = new goog.structs.CircularBuffer(2);
  64. assertUndefined(buff.add('first'));
  65. assertUndefined(buff.add('second'));
  66. assertArrayEquals([0, 1], buff.getKeys());
  67. }
  68. function testContainsValue() {
  69. var buff = new goog.structs.CircularBuffer(2);
  70. assertUndefined(buff.add('first'));
  71. assertUndefined(buff.add('second'));
  72. assertTrue(buff.containsValue('first'));
  73. assertTrue(buff.containsValue('second'));
  74. assertFalse(buff.containsValue('third'));
  75. }
  76. function testContainsKey() {
  77. var buff = new goog.structs.CircularBuffer(3);
  78. assertUndefined(buff.add('first'));
  79. assertUndefined(buff.add('second'));
  80. assertUndefined(buff.add('third'));
  81. assertTrue(buff.containsKey(0));
  82. assertTrue(buff.containsKey('0'));
  83. assertTrue(buff.containsKey(1));
  84. assertTrue(buff.containsKey('1'));
  85. assertTrue(buff.containsKey(2));
  86. assertTrue(buff.containsKey('2'));
  87. assertFalse(buff.containsKey(3));
  88. assertFalse(buff.containsKey('3'));
  89. }