]> git.proxmox.com Git - extjs.git/blame - extjs/packages/core/test/specs/util/LruCache.js
bump version to 7.0.0-4
[extjs.git] / extjs / packages / core / test / specs / util / LruCache.js
CommitLineData
947f0963
TL
1topSuite("Ext.util.LruCache", function() {
2 var cache,
3 obj1 = { objIdx: 1 },
4 obj2 = { objIdx: 2 },
5 obj3 = { objIdx: 3 },
6 obj4 = { objIdx: 4 },
7 obj5 = { objIdx: 5 },
8 obj6 = { objIdx: 6 },
9 obj7 = { objIdx: 7 },
10 obj8 = { objIdx: 8 },
11 obj9 = { objIdx: 9 },
12 obj10 = { objIdx: 10 };
13
14 function createCache(config) {
15 cache = new Ext.util.LruCache(config);
16 }
17
18 describe("Adding", function() {
19 it("should create an empty cache", function() {
20 createCache();
21 expect(cache.length).toBe(0);
22 expect(cache.first).toBeNull();
23 expect(cache.last).toBeNull();
24 expect(cache.getValues()).toEqual([]);
25 expect(cache.getKeys()).toEqual([]);
26 });
27
28 it("should contain 1 entry", function() {
29 createCache();
30 cache.add(1, obj1);
31 expect(cache.length).toEqual(1);
32 expect(cache.first.value).toBe(obj1);
33 expect(cache.last.value).toBe(obj1);
34 expect(cache.getValues()).toEqual([obj1]);
35 expect(cache.getKeys()).toEqual([1]);
36 });
37
38 it("should contain 2 entries", function() {
39 createCache();
40 cache.add(1, obj1);
41 cache.add(2, obj2);
42 expect(cache.length).toEqual(2);
43 expect(cache.first.value).toBe(obj1);
44 expect(cache.last.value).toBe(obj2);
45 expect(cache.getValues()).toEqual([obj1, obj2]);
46 expect(cache.getKeys()).toEqual([1, 2]);
47 });
48
49 it("should be able to add existing keys", function() {
50 createCache();
51 cache.add(1, obj1);
52 cache.add(2, obj2);
53 cache.add(1, obj3);
54 expect(cache.length).toEqual(2);
55 expect(cache.first.value).toBe(obj2);
56 expect(cache.last.value).toBe(obj3);
57 expect(cache.getValues()).toEqual([obj2, obj3]);
58 expect(cache.getKeys()).toEqual([2, 1]);
59 });
60 });
61
62 describe("Sort on access", function() {
63 it("should move accessed items to the end", function() {
64 createCache();
65 cache.add(1, obj1);
66 cache.add(2, obj2);
67 expect(cache.getValues()).toEqual([obj1, obj2]);
68 expect(cache.getKeys()).toEqual([1, 2]);
69 cache.get(1);
70 expect(cache.getValues()).toEqual([obj2, obj1]);
71 expect(cache.getKeys()).toEqual([2, 1]);
72 });
73 });
74
75 describe("Inserting", function() {
76 it("should insert at the requested point", function() {
77 createCache();
78 cache.add(1, obj1);
79 cache.insertBefore(2, obj2, obj1);
80 expect(cache.getValues()).toEqual([obj2, obj1]);
81 expect(cache.getKeys()).toEqual([2, 1]);
82 });
83 });
84
85 describe("Iterating", function() {
86 it("should iterate in order", function() {
87 var result = [];
88
89 createCache();
90 cache.add(1, obj1);
91 cache.add(2, obj2);
92 cache.each(function(key, value, length) {
93 result.push(key, value);
94 });
95 expect(result).toEqual([1, obj1, 2, obj2]);
96 });
97 it("should iterate in reverse order", function() {
98 var result = [];
99
100 createCache();
101 cache.add(1, obj1);
102 cache.add(2, obj2);
103 cache.each(function(key, value, length) {
104 result.push(key, value);
105 }, null, true);
106 expect(result).toEqual([2, obj2, 1, obj1]);
107 });
108 });
109
110 describe("Removing", function() {
111 it("should remove by key and re-link", function() {
112 createCache();
113 cache.add(1, obj1);
114 cache.add(2, obj2);
115 cache.add(3, obj3);
116 cache.removeAtKey(2);
117 expect(cache.getValues()).toEqual([obj1, obj3]);
118 expect(cache.getKeys()).toEqual([1, 3]);
119 });
120 it("should remove by value and re-link", function() {
121 createCache();
122 cache.add(1, obj1);
123 cache.add(2, obj2);
124 cache.add(3, obj3);
125 cache.remove(obj2);
126 expect(cache.getValues()).toEqual([obj1, obj3]);
127 expect(cache.getKeys()).toEqual([1, 3]);
128 });
129 });
130
131 describe("Clearing", function() {
132 it("should remove all", function() {
133 createCache();
134 cache.add(1, obj1);
135 cache.add(2, obj2);
136 cache.clear();
137 expect(cache.getValues()).toEqual([]);
138 expect(cache.getKeys()).toEqual([]);
139 });
140 });
141
142 describe("Purging", function() {
143 it("should only contain the last 5 added", function() {
144 createCache({
145 maxSize: 5
146 });
147 cache.add(1, obj1);
148 cache.add(2, obj2);
149 cache.add(3, obj3);
150 cache.add(4, obj4);
151 cache.add(5, obj5);
152 expect(cache.getValues()).toEqual([obj1, obj2, obj3, obj4, obj5]);
153 expect(cache.getKeys()).toEqual([1, 2, 3, 4, 5]);
154 cache.add(6, obj6);
155 expect(cache.getValues()).toEqual([obj2, obj3, obj4, obj5, obj6]);
156 expect(cache.getKeys()).toEqual([2, 3, 4, 5, 6]);
157 cache.add(7, obj7);
158 expect(cache.getValues()).toEqual([obj3, obj4, obj5, obj6, obj7]);
159 expect(cache.getKeys()).toEqual([3, 4, 5, 6, 7]);
160 cache.add(8, obj8);
161 expect(cache.getValues()).toEqual([obj4, obj5, obj6, obj7, obj8]);
162 expect(cache.getKeys()).toEqual([4, 5, 6, 7, 8]);
163 cache.add(9, obj9);
164 expect(cache.getValues()).toEqual([obj5, obj6, obj7, obj8, obj9]);
165 expect(cache.getKeys()).toEqual([5, 6, 7, 8, 9]);
166 cache.add(10, obj10);
167 expect(cache.getValues()).toEqual([obj6, obj7, obj8, obj9, obj10]);
168 expect(cache.getKeys()).toEqual([6, 7, 8, 9, 10]);
169 });
170 });
171});