Debian Codegraph @

closure-library/closure/goog/async/freelist_test.js

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