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