-
Notifications
You must be signed in to change notification settings - Fork 66
/
test.lua
68 lines (54 loc) · 1.31 KB
/
test.lua
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
local zset = require "zset"
local total = 100
local all = {}
for i=1, total do
all[#all + 1] = i
end
local function random_choose(t)
if #t == 0 then
return
end
local i = math.random(#t)
return table.remove(t, i)
end
local function delete_handler(member)
print("delete", member)
end
local zs = zset.new(delete_handler)
while true do
local score = random_choose(all)
if not score then
break
end
local name = "a" .. score
zs:add(score, name)
end
assert(total == zs:count())
print("rank 28:", zs:rank("a28"))
print("rev rank 28:", zs:rev_rank("a28"))
local t = zs:range(1, 10)
print("rank 1-10:")
for _, name in ipairs(t) do
print(name)
end
local t = zs:rev_range(1, 10)
print("rev rank 1-10:")
for _, name in ipairs(t) do
print(name)
end
print("------------------ dump ------------------")
zs:dump()
print("------------------ dump after limit 10 ------------------")
zs:limit(10)
zs:dump()
print("------------------ dump after rev limit 5 ------------------")
zs:rev_limit(5)
zs:dump()
print("------------------ member_by_rank ------------------")
for rank = 1, 10 do
print("rank", rank, zs:member_by_rank(rank))
end
print("------------------ member_by_rev_rank ------------------")
for rank = 1, 10 do
print("rank", rank, zs:member_by_rev_rank(rank))
end