diff options
Diffstat (limited to 'testes/nextvar.lua')
| -rw-r--r-- | testes/nextvar.lua | 52 |
1 files changed, 19 insertions, 33 deletions
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) |
