diff options
Diffstat (limited to 'testes/locals.lua')
| -rw-r--r-- | testes/locals.lua | 181 |
1 files changed, 181 insertions, 0 deletions
diff --git a/testes/locals.lua b/testes/locals.lua new file mode 100644 index 00000000..f0780a03 --- /dev/null +++ b/testes/locals.lua | |||
| @@ -0,0 +1,181 @@ | |||
| 1 | -- $Id: locals.lua,v 1.41 2018/06/19 12:25:39 roberto Exp $ | ||
| 2 | -- See Copyright Notice in file all.lua | ||
| 3 | |||
| 4 | print('testing local variables and environments') | ||
| 5 | |||
| 6 | local debug = require"debug" | ||
| 7 | |||
| 8 | |||
| 9 | -- bug in 5.1: | ||
| 10 | |||
| 11 | local function f(x) x = nil; return x end | ||
| 12 | assert(f(10) == nil) | ||
| 13 | |||
| 14 | local function f() local x; return x end | ||
| 15 | assert(f(10) == nil) | ||
| 16 | |||
| 17 | local function f(x) x = nil; local y; return x, y end | ||
| 18 | assert(f(10) == nil and select(2, f(20)) == nil) | ||
| 19 | |||
| 20 | do | ||
| 21 | local i = 10 | ||
| 22 | do local i = 100; assert(i==100) end | ||
| 23 | do local i = 1000; assert(i==1000) end | ||
| 24 | assert(i == 10) | ||
| 25 | if i ~= 10 then | ||
| 26 | local i = 20 | ||
| 27 | else | ||
| 28 | local i = 30 | ||
| 29 | assert(i == 30) | ||
| 30 | end | ||
| 31 | end | ||
| 32 | |||
| 33 | |||
| 34 | |||
| 35 | f = nil | ||
| 36 | |||
| 37 | local f | ||
| 38 | x = 1 | ||
| 39 | |||
| 40 | a = nil | ||
| 41 | load('local a = {}')() | ||
| 42 | assert(a == nil) | ||
| 43 | |||
| 44 | function f (a) | ||
| 45 | local _1, _2, _3, _4, _5 | ||
| 46 | local _6, _7, _8, _9, _10 | ||
| 47 | local x = 3 | ||
| 48 | local b = a | ||
| 49 | local c,d = a,b | ||
| 50 | if (d == b) then | ||
| 51 | local x = 'q' | ||
| 52 | x = b | ||
| 53 | assert(x == 2) | ||
| 54 | else | ||
| 55 | assert(nil) | ||
| 56 | end | ||
| 57 | assert(x == 3) | ||
| 58 | local f = 10 | ||
| 59 | end | ||
| 60 | |||
| 61 | local b=10 | ||
| 62 | local a; repeat local b; a,b=1,2; assert(a+1==b); until a+b==3 | ||
| 63 | |||
| 64 | |||
| 65 | assert(x == 1) | ||
| 66 | |||
| 67 | f(2) | ||
| 68 | assert(type(f) == 'function') | ||
| 69 | |||
| 70 | |||
| 71 | local function getenv (f) | ||
| 72 | local a,b = debug.getupvalue(f, 1) | ||
| 73 | assert(a == '_ENV') | ||
| 74 | return b | ||
| 75 | end | ||
| 76 | |||
| 77 | -- test for global table of loaded chunks | ||
| 78 | assert(getenv(load"a=3") == _G) | ||
| 79 | local c = {}; local f = load("a = 3", nil, nil, c) | ||
| 80 | assert(getenv(f) == c) | ||
| 81 | assert(c.a == nil) | ||
| 82 | f() | ||
| 83 | assert(c.a == 3) | ||
| 84 | |||
| 85 | -- old test for limits for special instructions (now just a generic test) | ||
| 86 | do | ||
| 87 | local i = 2 | ||
| 88 | local p = 4 -- p == 2^i | ||
| 89 | repeat | ||
| 90 | for j=-3,3 do | ||
| 91 | assert(load(string.format([[local a=%s; | ||
| 92 | a=a+%s; | ||
| 93 | assert(a ==2^%s)]], j, p-j, i), '')) () | ||
| 94 | assert(load(string.format([[local a=%s; | ||
| 95 | a=a-%s; | ||
| 96 | assert(a==-2^%s)]], -j, p-j, i), '')) () | ||
| 97 | assert(load(string.format([[local a,b=0,%s; | ||
| 98 | a=b-%s; | ||
| 99 | assert(a==-2^%s)]], -j, p-j, i), '')) () | ||
| 100 | end | ||
| 101 | p = 2 * p; i = i + 1 | ||
| 102 | until p <= 0 | ||
| 103 | end | ||
| 104 | |||
| 105 | print'+' | ||
| 106 | |||
| 107 | |||
| 108 | if rawget(_G, "T") then | ||
| 109 | -- testing clearing of dead elements from tables | ||
| 110 | collectgarbage("stop") -- stop GC | ||
| 111 | local a = {[{}] = 4, [3] = 0, alo = 1, | ||
| 112 | a1234567890123456789012345678901234567890 = 10} | ||
| 113 | |||
| 114 | local t = T.querytab(a) | ||
| 115 | |||
| 116 | for k,_ in pairs(a) do a[k] = undef end | ||
| 117 | collectgarbage() -- restore GC and collect dead fiels in `a' | ||
| 118 | for i=0,t-1 do | ||
| 119 | local k = querytab(a, i) | ||
| 120 | assert(k == nil or type(k) == 'number' or k == 'alo') | ||
| 121 | end | ||
| 122 | |||
| 123 | -- testing allocation errors during table insertions | ||
| 124 | local a = {} | ||
| 125 | local function additems () | ||
| 126 | a.x = true; a.y = true; a.z = true | ||
| 127 | a[1] = true | ||
| 128 | a[2] = true | ||
| 129 | end | ||
| 130 | for i = 1, math.huge do | ||
| 131 | T.alloccount(i) | ||
| 132 | local st, msg = pcall(additems) | ||
| 133 | T.alloccount() | ||
| 134 | local count = 0 | ||
| 135 | for k, v in pairs(a) do | ||
| 136 | assert(a[k] == v) | ||
| 137 | count = count + 1 | ||
| 138 | end | ||
| 139 | if st then assert(count == 5); break end | ||
| 140 | end | ||
| 141 | end | ||
| 142 | |||
| 143 | |||
| 144 | -- testing lexical environments | ||
| 145 | |||
| 146 | assert(_ENV == _G) | ||
| 147 | |||
| 148 | do | ||
| 149 | local dummy | ||
| 150 | local _ENV = (function (...) return ... end)(_G, dummy) -- { | ||
| 151 | |||
| 152 | do local _ENV = {assert=assert}; assert(true) end | ||
| 153 | mt = {_G = _G} | ||
| 154 | local foo,x | ||
| 155 | A = false -- "declare" A | ||
| 156 | do local _ENV = mt | ||
| 157 | function foo (x) | ||
| 158 | A = x | ||
| 159 | do local _ENV = _G; A = 1000 end | ||
| 160 | return function (x) return A .. x end | ||
| 161 | end | ||
| 162 | end | ||
| 163 | assert(getenv(foo) == mt) | ||
| 164 | x = foo('hi'); assert(mt.A == 'hi' and A == 1000) | ||
| 165 | assert(x('*') == mt.A .. '*') | ||
| 166 | |||
| 167 | do local _ENV = {assert=assert, A=10}; | ||
| 168 | do local _ENV = {assert=assert, A=20}; | ||
| 169 | assert(A==20);x=A | ||
| 170 | end | ||
| 171 | assert(A==10 and x==20) | ||
| 172 | end | ||
| 173 | assert(x==20) | ||
| 174 | |||
| 175 | |||
| 176 | print('OK') | ||
| 177 | |||
| 178 | return 5,f | ||
| 179 | |||
| 180 | end -- } | ||
| 181 | |||
