-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_cache.lua
More file actions
91 lines (76 loc) · 2.19 KB
/
Copy pathtest_cache.lua
File metadata and controls
91 lines (76 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
local function table_length(t)
local length = 0
for _ in pairs(t) do
length = length + 1
end
return length
end
local CACHE = require "cache"
do
local cache = CACHE:new("id", "type")
cache:add {id = 1, type = "type_1", x = 128, y = 129, name = "obj_1"}
cache:add {id = 2, type = "type_1", x = 128, y = 130, name = "obj_2"}
cache:add {id = 3, type = "type_2", x = 128, y = 131, name = "obj_3"}
-- select index
do
local c = {}
for k, v in cache:select("type", "type_1") do
c[k] = v
end
assert( table_length(c) == 2 )
assert( c[1].name == "obj_1" )
assert( c[2].name == "obj_2" )
end
-- key
do
local obj = assert(cache:selectkey(1))
assert( obj.name == "obj_1" )
end
-- change index value & select
do
local obj_3 = assert(cache:selectkey(3))
obj_3.type = "type_1"
cache:sync(obj_3, "type")
local c = {}
for k, v in cache:select("type", "type_1") do
c[k] = v
end
assert( table_length(c) == 3 )
assert( c[1].name == "obj_1" )
assert( c[2].name == "obj_2" )
assert( c[3].name == "obj_3" )
end
end
-------------------------------------------------------------------------------------------------
do
-- remove
local cache = CACHE:new("id", "type")
cache:add {id = 1, type = "type_1", x = 128, y = 129, name = "obj_1"}
cache:add {id = 2, type = "type_1", x = 128, y = 130, name = "obj_2"}
cache:add {id = 3, type = "type_2", x = 128, y = 131, name = "obj_3"}
cache:remove(1)
assert(cache:selectkey(1) == nil)
local c = {}
for k, v in cache:select("type", "type_1") do
c[k] = v
end
assert( table_length(c) == 1 )
assert( c[2].name == "obj_2" )
end
-------------------------------------------------------------------------------------------------
do
-- empty values
local cache = CACHE:new("id", "type", "tag")
cache:add {id = 1, type = "type_1", x = 128, y = 129, name = "obj_1"}
cache:add {id = 2, type = "type_1", x = 128, y = 130, name = "obj_2"}
cache:add {id = 3, type = "type_2", x = 128, y = 131, name = "obj_3"}
local obj_3 = assert(cache:selectkey(3))
obj_3.tag = false
cache:sync(obj_3, "tag")
local c = {}
for k, v in cache:select("tag", false) do
c[k] = v
end
assert( table_length(c) == 1 )
assert( c[3].name == "obj_3" )
end