github.com/hirochachacha/plua@v0.0.0-20170217012138-c82f520cc725/testdata/lua-5.3.3-tests/verybig.lua (about)

     1  -- $Id: verybig.lua,v 1.24 2014/12/26 17:20:53 roberto Exp $
     2  
     3  print "testing RK"
     4  
     5  -- testing opcodes with RK arguments larger than K limit
     6  local function foo ()
     7    local dummy = {
     8       -- fill first 256 entries in table of constants
     9       1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
    10       17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
    11       33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
    12       49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64,
    13       65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
    14       81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96,
    15       97, 98, 99, 100, 101, 102, 103, 104,
    16       105, 106, 107, 108, 109, 110, 111, 112,
    17       113, 114, 115, 116, 117, 118, 119, 120,
    18       121, 122, 123, 124, 125, 126, 127, 128,
    19       129, 130, 131, 132, 133, 134, 135, 136,
    20       137, 138, 139, 140, 141, 142, 143, 144,
    21       145, 146, 147, 148, 149, 150, 151, 152,
    22       153, 154, 155, 156, 157, 158, 159, 160,
    23       161, 162, 163, 164, 165, 166, 167, 168,
    24       169, 170, 171, 172, 173, 174, 175, 176,
    25       177, 178, 179, 180, 181, 182, 183, 184,
    26       185, 186, 187, 188, 189, 190, 191, 192,
    27       193, 194, 195, 196, 197, 198, 199, 200,
    28       201, 202, 203, 204, 205, 206, 207, 208,
    29       209, 210, 211, 212, 213, 214, 215, 216,
    30       217, 218, 219, 220, 221, 222, 223, 224,
    31       225, 226, 227, 228, 229, 230, 231, 232,
    32       233, 234, 235, 236, 237, 238, 239, 240,
    33       241, 242, 243, 244, 245, 246, 247, 248,
    34       249, 250, 251, 252, 253, 254, 255, 256,
    35    }
    36    assert(24.5 + 0.6 == 25.1)
    37    local t = {foo = function (self, x) return x + self.x end, x = 10}
    38    t.t = t
    39    assert(t:foo(1.5) == 11.5)
    40    assert(t.t:foo(0.5) == 10.5)   -- bug in 5.2 alpha
    41    assert(24.3 == 24.3)
    42    assert((function () return t.x end)() == 10)
    43  end
    44  
    45  
    46  foo()
    47  foo = nil
    48  
    49  if _soft then return 10 end
    50  
    51  print "testing large programs (>64k)"
    52  
    53  -- template to create a very big test file
    54  prog = [[$
    55  
    56  local a,b
    57  
    58  b = {$1$
    59    b30009 = 65534,
    60    b30010 = 65535,
    61    b30011 = 65536,
    62    b30012 = 65537,
    63    b30013 = 16777214,
    64    b30014 = 16777215,
    65    b30015 = 16777216,
    66    b30016 = 16777217,
    67    b30017 = 0x7fffff,
    68    b30018 = -0x7fffff,
    69    b30019 = 0x1ffffff,
    70    b30020 = -0x1ffffd,
    71    b30021 = -65534,
    72    b30022 = -65535,
    73    b30023 = -65536,
    74    b30024 = -0xffffff,
    75    b30025 = 15012.5,
    76    $2$
    77  };
    78  
    79  assert(b.a50008 == 25004 and b["a11"] == -5.5)
    80  assert(b.a33007 == -16503.5 and b.a50009 == -25004.5)
    81  assert(b["b"..30024] == -0xffffff)
    82  
    83  function b:xxx (a,b) return a+b end
    84  assert(b:xxx(10, 12) == 22)   -- pushself with non-constant index
    85  b.xxx = nil
    86  
    87  s = 0; n=0
    88  for a,b in pairs(b) do s=s+b; n=n+1 end
    89  -- with 32-bit floats, exact value of 's' depends on summation order
    90  assert(81800000.0 < s and s < 81860000 and n == 70001)
    91  
    92  a = nil; b = nil
    93  print'+'
    94  
    95  function f(x) b=x end
    96  
    97  a = f{$3$} or 10
    98  
    99  assert(a==10)
   100  assert(b[1] == "a10" and b[2] == 5 and b[#b-1] == "a50009")
   101  
   102  
   103  function xxxx (x) return b[x] end
   104  
   105  assert(xxxx(3) == "a11")
   106  
   107  a = nil; b=nil
   108  xxxx = nil
   109  
   110  return 10
   111  
   112  ]]
   113  
   114  -- functions to fill in the $n$
   115  
   116  local function sig (x)
   117    return (x % 2 == 0) and '' or '-'
   118  end
   119  
   120  F = {
   121  function ()   -- $1$
   122    for i=10,50009 do
   123      io.write('a', i, ' = ', sig(i), 5+((i-10)/2), ',\n')
   124    end
   125  end,
   126  
   127  function ()   -- $2$
   128    for i=30026,50009 do
   129      io.write('b', i, ' = ', sig(i), 15013+((i-30026)/2), ',\n')
   130    end
   131  end,
   132  
   133  function ()   -- $3$
   134    for i=10,50009 do
   135      io.write('"a', i, '", ', sig(i), 5+((i-10)/2), ',\n')
   136    end
   137  end,
   138  }
   139  
   140  file = os.tmpname()
   141  io.output(file)
   142  for s in string.gmatch(prog, "$([^$]+)") do
   143    local n = tonumber(s)
   144    if not n then io.write(s) else F[n]() end
   145  end
   146  io.close()
   147  result = dofile(file)
   148  assert(os.remove(file))
   149  print'OK'
   150  return result
   151