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
|
-- Verify the functionality of result_view.
result_view = require 'result_view'
print('alphabet')
alphabet = "abcdefghijklmnopqrstuvwxyz"
assert(#alphabet == 26)
alphabits = string.split(alphabet, '')
print('function slice()')
function slice(t, index, count)
return table.move(t, index, index + count - 1, 1, {})
end
print('verify slice()')
-- verify that slice() does what we expect
assert(table.concat(slice(alphabits, 4, 3)) == "def")
assert(table.concat(slice(alphabits, 14, 3)) == "nop")
assert(table.concat(slice(alphabits, 25, 3)) == "yz")
print('function fetch()')
function fetch(key, index)
-- fetch function is defined to be 0-relative: fix for Lua data
-- constrain view of alphabits to slices of at most 3 elements
return slice(alphabits, index+1, 3), index
end
print('result_view()')
-- for test purposes, key is irrelevant, so just 'key'
view = result_view({'key', #alphabits}, fetch)
print('function check_iter()')
function check_iter(...)
result = {}
for k, v in ... do
table.insert(result, v)
end
assert(table.concat(result) == alphabet)
end
print('check_iter(pairs(view))')
check_iter(pairs(view))
print('check_iter(ipairs(view))')
check_iter(ipairs(view))
print('check_iter(view)')
check_iter(view)
print('raw index access')
assert(view[5] == 'e')
assert(view[10] == 'j')
assert(view[15] == 'o')
assert(view[20] == 't')
assert(view[25] == 'y')
print('Success!')
|