freelist_test.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2015 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.async.FreeListTest');
  15. goog.setTestOnly('goog.async.FreeListTest');
  16. goog.require('goog.async.FreeList');
  17. goog.require('goog.testing.jsunit');
  18. var id = 0;
  19. var list = null;
  20. function setUp() {
  21. var id = 0;
  22. var data = 1;
  23. list = new goog.async.FreeList(
  24. function() {
  25. data *= 2;
  26. return {id: id++, data: data, next: null};
  27. },
  28. function(item) { item.data = null; },
  29. 2); // max occupancy
  30. }
  31. function tearDown() {
  32. list = null;
  33. }
  34. function testItemsCreatedAsNeeded() {
  35. assertEquals(0, list.occupants());
  36. var item1 = list.get();
  37. assertNotNullNorUndefined(item1);
  38. var item2 = list.get();
  39. assertNotNullNorUndefined(item2);
  40. assertNotEquals(item1, item2);
  41. assertEquals(0, list.occupants());
  42. }
  43. function testMaxOccupancy() {
  44. assertEquals(0, list.occupants());
  45. var item1 = list.get();
  46. var item2 = list.get();
  47. var item3 = list.get();
  48. list.put(item1);
  49. list.put(item2);
  50. list.put(item3);
  51. assertEquals(2, list.occupants());
  52. }
  53. function testRecycling() {
  54. assertEquals(0, list.occupants());
  55. var item1 = list.get();
  56. assertNotNull(item1.data);
  57. list.put(item1);
  58. var item2 = list.get();
  59. // Item recycled
  60. assertEquals(item1, item2);
  61. // reset method called
  62. assertNull(item2.data);
  63. }