diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-05-17 11:11:44 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-05-17 11:11:44 -0300 |
| commit | d9f40e3f6fb61650240c47d548bee69b24b07859 (patch) | |
| tree | ab01022b3e3bc6bdb800423c97095a9423e0a798 /testes | |
| parent | 347d6961ac14213264c7176e3d125c9ba8475b01 (diff) | |
| download | lua-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.lua | 61 | ||||
| -rw-r--r-- | testes/files.lua | 8 | ||||
| -rw-r--r-- | testes/math.lua | 6 |
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 | ||
| 60 | assert(1234567890 == tonumber('1234567890') and 1234567890+1 == 1234567891) | 60 | assert(1234567890 == tonumber('1234567890') and 1234567890+1 == 1234567891) |
| 61 | 61 | ||
| 62 | do -- 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 | ||
| 95 | end | ||
| 96 | |||
| 62 | 97 | ||
| 63 | -- silly loops | 98 | -- silly loops |
| 64 | repeat until 1; repeat until true; | 99 | repeat until 1; repeat until true; |
| @@ -175,6 +210,28 @@ assert(a==1 and b==nil) | |||
| 175 | 210 | ||
| 176 | print'+'; | 211 | print'+'; |
| 177 | 212 | ||
| 213 | do -- 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'") | ||
| 234 | end | ||
| 178 | 235 | ||
| 179 | f = [[ | 236 | f = [[ |
| 180 | return function ( a , b , c , d , e ) | 237 | return 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 |
| 248 | local binops = { | 305 | local <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 | ||
| 253 | local cases = {} | 310 | local <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 | ||
| 4 | print("testing numbers and math lib") | 4 | print("testing numbers and math lib") |
| 5 | 5 | ||
| 6 | local minint = math.mininteger | 6 | local <const> minint = math.mininteger |
| 7 | local maxint = math.maxinteger | 7 | local <const> maxint = math.maxinteger |
| 8 | 8 | ||
| 9 | local intbits = math.floor(math.log(maxint, 2) + 0.5) + 1 | 9 | local <const> intbits = math.floor(math.log(maxint, 2) + 0.5) + 1 |
| 10 | assert((1 << intbits) == 0) | 10 | assert((1 << intbits) == 0) |
| 11 | 11 | ||
| 12 | assert(minint == 1 << (intbits - 1)) | 12 | assert(minint == 1 << (intbits - 1)) |
