diff options
author | Philipp Janda <siffiejoe@gmx.net> | 2015-01-17 20:36:32 +0100 |
---|---|---|
committer | Philipp Janda <siffiejoe@gmx.net> | 2015-01-17 20:36:32 +0100 |
commit | 4c062846ae003dd747dfcd3eca91493c7eee4f50 (patch) | |
tree | 583f36eab79954b8bb354d81f4f08cf1472be2b5 /tests | |
parent | 64783408a4a108812f22268c12f71c75f5399d81 (diff) | |
download | lua-compat-5.3-4c062846ae003dd747dfcd3eca91493c7eee4f50.tar.gz lua-compat-5.3-4c062846ae003dd747dfcd3eca91493c7eee4f50.tar.bz2 lua-compat-5.3-4c062846ae003dd747dfcd3eca91493c7eee4f50.zip |
table library (except table.sort for now) respects metamethods
Diffstat (limited to 'tests')
-rwxr-xr-x | tests/test.lua | 116 |
1 files changed, 115 insertions, 1 deletions
diff --git a/tests/test.lua b/tests/test.lua index ab25820..7e1c9da 100755 --- a/tests/test.lua +++ b/tests/test.lua | |||
@@ -1,6 +1,6 @@ | |||
1 | #!/usr/bin/env lua | 1 | #!/usr/bin/env lua |
2 | 2 | ||
3 | local F, ___ | 3 | local F, tproxy, ___ |
4 | do | 4 | do |
5 | local type, unpack = type, table.unpack or unpack | 5 | local type, unpack = type, table.unpack or unpack |
6 | function F(...) | 6 | function F(...) |
@@ -13,6 +13,13 @@ do | |||
13 | end | 13 | end |
14 | return unpack(args, 1, n) | 14 | return unpack(args, 1, n) |
15 | end | 15 | end |
16 | function tproxy(t) | ||
17 | return setmetatable({}, { | ||
18 | __index = t, | ||
19 | __newindex = t, | ||
20 | __len = function() return #t end, | ||
21 | }), t | ||
22 | end | ||
16 | local sep = ("="):rep(70) | 23 | local sep = ("="):rep(70) |
17 | function ___() | 24 | function ___() |
18 | print(sep) | 25 | print(sep) |
@@ -33,6 +40,113 @@ end | |||
33 | 40 | ||
34 | 41 | ||
35 | ___'' | 42 | ___'' |
43 | do | ||
44 | local p, t = tproxy{ "a", "b", "c" } | ||
45 | print("table.concat", table.concat(p)) | ||
46 | print("table.concat", table.concat(p, ",", 2)) | ||
47 | print("table.concat", table.concat(p, ".", 1, 2)) | ||
48 | print("table.concat", table.concat(t)) | ||
49 | print("table.concat", table.concat(t, ",", 2)) | ||
50 | print("table.concat", table.concat(t, ".", 1, 2)) | ||
51 | end | ||
52 | |||
53 | |||
54 | ___'' | ||
55 | do | ||
56 | local p, t = tproxy{ "a", "b", "c" } | ||
57 | table.insert(p, "d") | ||
58 | print("table.insert", next(p), t[4]) | ||
59 | table.insert(p, 1, "z") | ||
60 | print("table.insert", next(p), t[1], t[2]) | ||
61 | table.insert(p, 2, "y") | ||
62 | print("table.insert", next(p), t[1], t[2], p[3]) | ||
63 | t = { "a", "b", "c" } | ||
64 | table.insert(t, "d") | ||
65 | print("table.insert", t[1], t[2], t[3], t[4]) | ||
66 | table.insert(t, 1, "z") | ||
67 | print("table.insert", t[1], t[2], t[3], t[4], t[5]) | ||
68 | table.insert(t, 2, "y") | ||
69 | print("table.insert", t[1], t[2], t[3], t[4], t[5]) | ||
70 | end | ||
71 | |||
72 | |||
73 | ___'' | ||
74 | do | ||
75 | local ps, s = tproxy{ "a", "b", "c", "d" } | ||
76 | local pd, d = tproxy{ "A", "B", "C", "D" } | ||
77 | table.move(ps, 1, 4, 1, pd) | ||
78 | print("table.move", next(pd), d[1], d[2], d[3], d[4]) | ||
79 | pd, d = tproxy{ "A", "B", "C", "D" } | ||
80 | table.move(ps, 2, 4, 1, pd) | ||
81 | print("table.move", next(pd), d[1], d[2], d[3], d[4]) | ||
82 | pd, d = tproxy{ "A", "B", "C", "D" } | ||
83 | table.move(ps, 2, 3, 4, pd) | ||
84 | print("table.move", next(pd), d[1], d[2], d[3], d[4], d[5]) | ||
85 | table.move(ps, 2, 4, 1) | ||
86 | print("table.move", next(ps), s[1], s[2], s[3], s[4]) | ||
87 | ps, s = tproxy{ "a", "b", "c", "d" } | ||
88 | table.move(ps, 2, 3, 4) | ||
89 | print("table.move", next(ps), s[1], s[2], s[3], s[4], s[5]) | ||
90 | s = { "a", "b", "c", "d" } | ||
91 | d = { "A", "B", "C", "D" } | ||
92 | table.move(s, 1, 4, 1, d) | ||
93 | print("table.move", d[1], d[2], d[3], d[4]) | ||
94 | d = { "A", "B", "C", "D" } | ||
95 | table.move(s, 2, 4, 1, d) | ||
96 | print("table.move", d[1], d[2], d[3], d[4]) | ||
97 | d = { "A", "B", "C", "D" } | ||
98 | table.move(s, 2, 3, 4, d) | ||
99 | print("table.move", d[1], d[2], d[3], d[4], d[5]) | ||
100 | table.move(s, 2, 4, 1) | ||
101 | print("table.move", s[1], s[2], s[3], s[4]) | ||
102 | s = { "a", "b", "c", "d" } | ||
103 | table.move(s, 2, 3, 4) | ||
104 | print("table.move", s[1], s[2], s[3], s[4], s[5]) | ||
105 | end | ||
106 | |||
107 | |||
108 | ___'' | ||
109 | do | ||
110 | local p, t = tproxy{ "a", "b", "c", "d", "e" } | ||
111 | print("table.remove", table.remove(p)) | ||
112 | print("table.remove", next(p), t[1], t[2], t[3], t[4], t[5]) | ||
113 | print("table.remove", table.remove(p, 1)) | ||
114 | print("table.remove", next(p), t[1], t[2], t[3], t[4]) | ||
115 | print("table.remove", table.remove(p, 2)) | ||
116 | print("table.remove", next(p), t[1], t[2], t[3]) | ||
117 | print("table.remove", table.remove(p, 3)) | ||
118 | print("table.remove", next(p), t[1], t[2], t[3]) | ||
119 | p, t = tproxy{} | ||
120 | print("table.remove", table.remove(p)) | ||
121 | print("table.remove", next(p), next(t)) | ||
122 | t = { "a", "b", "c", "d", "e" } | ||
123 | print("table.remove", table.remove(t)) | ||
124 | print("table.remove", t[1], t[2], t[3], t[4], t[5]) | ||
125 | print("table.remove", table.remove(t, 1)) | ||
126 | print("table.remove", t[1], t[2], t[3], t[4]) | ||
127 | print("table.remove", table.remove(t, 2)) | ||
128 | print("table.remove", t[1], t[2], t[3]) | ||
129 | print("table.remove", table.remove(t, 3)) | ||
130 | print("table.remove", t[1], t[2], t[3]) | ||
131 | t = {} | ||
132 | print("table.remove", table.remove(t)) | ||
133 | print("table.remove", next(t)) | ||
134 | end | ||
135 | |||
136 | |||
137 | ___'' | ||
138 | do | ||
139 | local p, t = tproxy{ "a", "b", "c" } | ||
140 | print("table.unpack", table.unpack(p)) | ||
141 | print("table.unpack", table.unpack(p, 2)) | ||
142 | print("table.unpack", table.unpack(p, 1, 2)) | ||
143 | print("table.unpack", table.unpack(t)) | ||
144 | print("table.unpack", table.unpack(t, 2)) | ||
145 | print("table.unpack", table.unpack(t, 1, 2)) | ||
146 | end | ||
147 | |||
148 | |||
149 | ___'' | ||
36 | print("math.maxinteger", math.maxinteger+1 > math.maxinteger) | 150 | print("math.maxinteger", math.maxinteger+1 > math.maxinteger) |
37 | print("math.mininteger", math.mininteger-1 < math.mininteger) | 151 | print("math.mininteger", math.mininteger-1 < math.mininteger) |
38 | 152 | ||