aboutsummaryrefslogtreecommitdiff
path: root/testes
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2019-05-17 11:11:44 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2019-05-17 11:11:44 -0300
commitd9f40e3f6fb61650240c47d548bee69b24b07859 (patch)
treeab01022b3e3bc6bdb800423c97095a9423e0a798 /testes
parent347d6961ac14213264c7176e3d125c9ba8475b01 (diff)
downloadlua-d9f40e3f6fb61650240c47d548bee69b24b07859.tar.gz
lua-d9f40e3f6fb61650240c47d548bee69b24b07859.tar.bz2
lua-d9f40e3f6fb61650240c47d548bee69b24b07859.zip
First implementation for 'const' variables
A variable can be declared const, which means it cannot be assigned to, with the syntax 'local <const> name = exp'.
Diffstat (limited to 'testes')
-rw-r--r--testes/constructs.lua61
-rw-r--r--testes/files.lua8
-rw-r--r--testes/math.lua6
3 files changed, 66 insertions, 9 deletions
diff --git a/testes/constructs.lua b/testes/constructs.lua
index a83df79e..b91e0979 100644
--- a/testes/constructs.lua
+++ b/testes/constructs.lua
@@ -59,6 +59,41 @@ assert((x>y) and x or y == 2);
59 59
60assert(1234567890 == tonumber('1234567890') and 1234567890+1 == 1234567891) 60assert(1234567890 == tonumber('1234567890') and 1234567890+1 == 1234567891)
61 61
62do -- testing operators with diffent kinds of constants
63 -- operands to consider:
64 -- * fit in register
65 -- * constant doesn't fit in register
66 -- * floats with integral values
67 local operand = {3, 100, 5.0, -10, -5.0, 10000, -10000}
68 local operator = {"+", "-", "*", "/", "//", "%", "^",
69 "&", "|", "^", "<<", ">>",
70 "==", "~=", "<", ">", "<=", ">=",}
71 for _, op in ipairs(operator) do
72 local f = assert(load(string.format([[return function (x,y)
73 return x %s y
74 end]], op)))();
75 for _, o1 in ipairs(operand) do
76 for _, o2 in ipairs(operand) do
77 local gab = f(o1, o2)
78
79 _ENV.XX = o1
80 code = string.format("return XX %s %s", op, o2)
81 res = assert(load(code))()
82 assert(res == gab)
83
84 _ENV.XX = o2
85 local code = string.format("return (%s) %s XX", o1, op)
86 local res = assert(load(code))()
87 assert(res == gab)
88
89 code = string.format("return (%s) %s %s", o1, op, o2)
90 res = assert(load(code))()
91 assert(res == gab)
92 end
93 end
94 end
95end
96
62 97
63-- silly loops 98-- silly loops
64repeat until 1; repeat until true; 99repeat until 1; repeat until true;
@@ -175,6 +210,28 @@ assert(a==1 and b==nil)
175 210
176print'+'; 211print'+';
177 212
213do -- testing constants
214 local <const> prog = [[local <XXX> x = 10]]
215 checkload(prog, "unknown attribute 'XXX'")
216
217 checkload([[local <const> xxx = 20; xxx = 10]],
218 ":1: assignment to const variable 'xxx'")
219
220 checkload([[
221 local xx;
222 local <const> xxx = 20;
223 local yyy;
224 local function foo ()
225 local abc = xx + yyy + xxx;
226 return function () return function () xxx = yyy end end
227 end
228 ]], ":6: assignment to const variable 'xxx'")
229
230 checkload([[
231 local <toclose> x = nil
232 x = io.open()
233 ]], ":2: assignment to const variable 'x'")
234end
178 235
179f = [[ 236f = [[
180return function ( a , b , c , d , e ) 237return function ( a , b , c , d , e )
@@ -245,12 +302,12 @@ print('testing short-circuit optimizations (' .. _ENV.GLOB1 .. ')')
245 302
246 303
247-- operators with their respective values 304-- operators with their respective values
248local binops = { 305local <const> binops = {
249 {" and ", function (a,b) if not a then return a else return b end end}, 306 {" and ", function (a,b) if not a then return a else return b end end},
250 {" or ", function (a,b) if a then return a else return b end end}, 307 {" or ", function (a,b) if a then return a else return b end end},
251} 308}
252 309
253local cases = {} 310local <const> cases = {}
254 311
255-- creates all combinations of '(cases[i] op cases[n-i])' plus 312-- creates all combinations of '(cases[i] op cases[n-i])' plus
256-- 'not(cases[i] op cases[n-i])' (syntax + value) 313-- 'not(cases[i] op cases[n-i])' (syntax + value)
diff --git a/testes/files.lua b/testes/files.lua
index eb100fe1..54931c14 100644
--- a/testes/files.lua
+++ b/testes/files.lua
@@ -144,7 +144,7 @@ do
144 f:write(string.format("0x%X\n", -maxint)) 144 f:write(string.format("0x%X\n", -maxint))
145 f:write("-0xABCp-3", '\n') 145 f:write("-0xABCp-3", '\n')
146 assert(f:close()) 146 assert(f:close())
147 f = assert(io.open(file, "r")) 147 local <toclose> f = assert(io.open(file, "r"))
148 assert(f:read("n") == maxint) 148 assert(f:read("n") == maxint)
149 assert(f:read("n") == maxint) 149 assert(f:read("n") == maxint)
150 assert(f:read("n") == 0xABCp-3) 150 assert(f:read("n") == 0xABCp-3)
@@ -170,18 +170,18 @@ three
170]] 170]]
171 local l1, l2, l3, l4, n1, n2, c, dummy 171 local l1, l2, l3, l4, n1, n2, c, dummy
172 assert(f:close()) 172 assert(f:close())
173 f = assert(io.open(file, "r")) 173 local <toclose> f = assert(io.open(file, "r"))
174 l1, l2, n1, n2, dummy = f:read("l", "L", "n", "n") 174 l1, l2, n1, n2, dummy = f:read("l", "L", "n", "n")
175 assert(l1 == "a line" and l2 == "another line\n" and 175 assert(l1 == "a line" and l2 == "another line\n" and
176 n1 == 1234 and n2 == 3.45 and dummy == nil) 176 n1 == 1234 and n2 == 3.45 and dummy == nil)
177 assert(f:close()) 177 assert(f:close())
178 f = assert(io.open(file, "r")) 178 local <toclose> f = assert(io.open(file, "r"))
179 l1, l2, n1, n2, c, l3, l4, dummy = f:read(7, "l", "n", "n", 1, "l", "l") 179 l1, l2, n1, n2, c, l3, l4, dummy = f:read(7, "l", "n", "n", 1, "l", "l")
180 assert(l1 == "a line\n" and l2 == "another line" and c == '\n' and 180 assert(l1 == "a line\n" and l2 == "another line" and c == '\n' and
181 n1 == 1234 and n2 == 3.45 and l3 == "one" and l4 == "two" 181 n1 == 1234 and n2 == 3.45 and l3 == "one" and l4 == "two"
182 and dummy == nil) 182 and dummy == nil)
183 assert(f:close()) 183 assert(f:close())
184 f = assert(io.open(file, "r")) 184 local <toclose> f = assert(io.open(file, "r"))
185 -- second item failing 185 -- second item failing
186 l1, n1, n2, dummy = f:read("l", "n", "n", "l") 186 l1, n1, n2, dummy = f:read("l", "n", "n", "l")
187 assert(l1 == "a line" and n1 == nil) 187 assert(l1 == "a line" and n1 == nil)
diff --git a/testes/math.lua b/testes/math.lua
index b010ff6c..c45a91ad 100644
--- a/testes/math.lua
+++ b/testes/math.lua
@@ -3,10 +3,10 @@
3 3
4print("testing numbers and math lib") 4print("testing numbers and math lib")
5 5
6local minint = math.mininteger 6local <const> minint = math.mininteger
7local maxint = math.maxinteger 7local <const> maxint = math.maxinteger
8 8
9local intbits = math.floor(math.log(maxint, 2) + 0.5) + 1 9local <const> intbits = math.floor(math.log(maxint, 2) + 0.5) + 1
10assert((1 << intbits) == 0) 10assert((1 << intbits) == 0)
11 11
12assert(minint == 1 << (intbits - 1)) 12assert(minint == 1 << (intbits - 1))