diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-07-12 16:13:50 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-07-12 16:13:50 -0300 |
commit | 1fb4d539254b67e7e35ed698250c66d1edff0e08 (patch) | |
tree | 8f48b7ca736a7fb02834bcfac1415cd43307f529 /testes | |
parent | f6aab3ec1f111cd8d968bdcb7ca800e93b819d24 (diff) | |
download | lua-1fb4d539254b67e7e35ed698250c66d1edff0e08.tar.gz lua-1fb4d539254b67e7e35ed698250c66d1edff0e08.tar.bz2 lua-1fb4d539254b67e7e35ed698250c66d1edff0e08.zip |
OP_NEWTABLE keeps exact size of arrays
OP_NEWTABLE is followed by an OP_EXTRAARG, so that it can keep
the exact size of the array part of the table to be created.
(Functions 'luaO_int2fb'/'luaO_fb2int' were removed.)
Diffstat (limited to 'testes')
-rw-r--r-- | testes/code.lua | 6 | ||||
-rw-r--r-- | testes/nextvar.lua | 52 |
2 files changed, 23 insertions, 35 deletions
diff --git a/testes/code.lua b/testes/code.lua index 49d682f8..b2702c61 100644 --- a/testes/code.lua +++ b/testes/code.lua | |||
@@ -93,11 +93,13 @@ end | |||
93 | -- some basic instructions | 93 | -- some basic instructions |
94 | check(function () -- function does not create upvalues | 94 | check(function () -- function does not create upvalues |
95 | (function () end){f()} | 95 | (function () end){f()} |
96 | end, 'CLOSURE', 'NEWTABLE', 'GETTABUP', 'CALL', 'SETLIST', 'CALL', 'RETURN0') | 96 | end, 'CLOSURE', 'NEWTABLE', 'EXTRAARG', 'GETTABUP', 'CALL', |
97 | 'SETLIST', 'CALL', 'RETURN0') | ||
97 | 98 | ||
98 | check(function (x) -- function creates upvalues | 99 | check(function (x) -- function creates upvalues |
99 | (function () return x end){f()} | 100 | (function () return x end){f()} |
100 | end, 'CLOSURE', 'NEWTABLE', 'GETTABUP', 'CALL', 'SETLIST', 'CALL', 'RETURN') | 101 | end, 'CLOSURE', 'NEWTABLE', 'EXTRAARG', 'GETTABUP', 'CALL', |
102 | 'SETLIST', 'CALL', 'RETURN') | ||
101 | 103 | ||
102 | 104 | ||
103 | -- sequence of LOADNILs | 105 | -- sequence of LOADNILs |
diff --git a/testes/nextvar.lua b/testes/nextvar.lua index 87a6bfa8..bdc9fc29 100644 --- a/testes/nextvar.lua +++ b/testes/nextvar.lua | |||
@@ -49,33 +49,13 @@ if not T then | |||
49 | else --[ | 49 | else --[ |
50 | -- testing table sizes | 50 | -- testing table sizes |
51 | 51 | ||
52 | local function log2 (x) return math.log(x, 2) end | ||
53 | 52 | ||
54 | local function mp2 (n) -- minimum power of 2 >= n | 53 | local function mp2 (n) -- minimum power of 2 >= n |
55 | local mp = 2^math.ceil(log2(n)) | 54 | local mp = 2^math.ceil(math.log(n, 2)) |
56 | assert(n == 0 or (mp/2 < n and n <= mp)) | 55 | assert(n == 0 or (mp/2 < n and n <= mp)) |
57 | return mp | 56 | return mp |
58 | end | 57 | end |
59 | 58 | ||
60 | local function fb (n) | ||
61 | local r, nn = T.int2fb(n) | ||
62 | assert(r < 256) | ||
63 | return nn | ||
64 | end | ||
65 | |||
66 | -- test fb function | ||
67 | for a = 1, 10000 do -- all numbers up to 10^4 | ||
68 | local n = fb(a) | ||
69 | assert(a <= n and n <= a*1.125) | ||
70 | end | ||
71 | local a = 1024 -- plus a few up to 2 ^30 | ||
72 | local lim = 2^30 | ||
73 | while a < lim do | ||
74 | local n = fb(a) | ||
75 | assert(a <= n and n <= a*1.125) | ||
76 | a = math.ceil(a*1.3) | ||
77 | end | ||
78 | |||
79 | 59 | ||
80 | local function check (t, na, nh) | 60 | local function check (t, na, nh) |
81 | local a, h = T.querytab(t) | 61 | local a, h = T.querytab(t) |
@@ -95,24 +75,30 @@ end | |||
95 | 75 | ||
96 | 76 | ||
97 | -- testing constructor sizes | 77 | -- testing constructor sizes |
98 | local lim = 40 | 78 | local sizes = {0, 1, 2, 3, 4, 5, 7, 8, 9, 15, 16, 17, |
99 | local s = 'return {' | 79 | 30, 31, 32, 33, 34, 500, 1000} |
100 | for i=1,lim do | 80 | |
101 | s = s..i..',' | 81 | for _, sa in ipairs(sizes) do -- 'sa' is size of the array part |
102 | local s = s | 82 | local arr = {"return {"} |
103 | for k=0,lim do | 83 | -- array part |
104 | local t = load(s..'}', '')() | 84 | for i = 1, sa do arr[1 + i] = "1," end |
105 | assert(#t == i) | 85 | for _, sh in ipairs(sizes) do -- 'sh' is size of the hash part |
106 | check(t, fb(i), mp2(k)) | 86 | for j = 1, sh do -- hash part |
107 | s = string.format('%sa%d=%d,', s, k, k) | 87 | arr[1 + sa + j] = string.format('k%x=%d,', j, j) |
88 | end | ||
89 | arr[1 + sa + sh + 1] = "}" | ||
90 | local prog = table.concat(arr) | ||
91 | local t = assert(load(prog))() | ||
92 | assert(#t == sa) | ||
93 | check(t, sa, mp2(sh)) | ||
108 | end | 94 | end |
109 | end | 95 | end |
110 | 96 | ||
111 | 97 | ||
112 | -- tests with unknown number of elements | 98 | -- tests with unknown number of elements |
113 | local a = {} | 99 | local a = {} |
114 | for i=1,lim do a[i] = i end -- build auxiliary table | 100 | for i=1,sizes[#sizes] do a[i] = i end -- build auxiliary table |
115 | for k=0,lim do | 101 | for k in ipairs(sizes) do |
116 | local a = {table.unpack(a,1,k)} | 102 | local a = {table.unpack(a,1,k)} |
117 | assert(#a == k) | 103 | assert(#a == k) |
118 | check(a, k, 0) | 104 | check(a, k, 0) |