summaryrefslogtreecommitdiff
path: root/test/test-suite/sort.test.js
blob: cc33c0ca664c6006d887b163d95c6a71207a4f7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
"use strict";

const lua = require('../../src/lua.js');
const lauxlib = require('../../src/lauxlib.js');
const lualib = require('../../src/lualib.js');
const {to_luastring} = require("../../src/fengaricore.js");

const prefix = `
    local unpack = table.unpack

    local maxI = math.maxinteger
    local minI = math.mininteger


    local function checkerror (msg, f, ...)
      local s, err = pcall(f, ...)
      assert(not s and string.find(err, msg))
    end

    function timesort (a, n, func, msg, pre)
      local x = os.clock()
      table.sort(a, func)
      x = (os.clock() - x) * 1000
      pre = pre or ""
      -- print(string.format("%ssorting %d %s elements in %.2f msec.", pre, n, msg, x))
      check(a, func)
    end

    limit = 50000
    if _soft then limit = 5000 end
`;

test("[test-suite] sort: testing unpack", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        checkerror("wrong number of arguments", table.insert, {}, 2, 3, 4)

        local x,y,z,a,n
        a = {}; lim = _soft and 200 or 2000
        for i=1, lim do a[i]=i end
        assert(select(lim, unpack(a)) == lim and select('#', unpack(a)) == lim)
        x = unpack(a)
        assert(x == 1)
        x = {unpack(a)}
        assert(#x == lim and x[1] == 1 and x[lim] == lim)
        x = {unpack(a, lim-2)}
        assert(#x == 3 and x[1] == lim-2 and x[3] == lim)
        x = {unpack(a, 10, 6)}
        assert(next(x) == nil)   -- no elements
        x = {unpack(a, 11, 10)}
        assert(next(x) == nil)   -- no elements
        x,y = unpack(a, 10, 10)
        assert(x == 10 and y == nil)
        x,y,z = unpack(a, 10, 11)
        assert(x == 10 and y == 11 and z == nil)
        a,x = unpack{1}
        assert(a==1 and x==nil)
        a,x = unpack({1,2}, 1, 1)
        assert(a==1 and x==nil)
    `;
    lualib.luaL_openlibs(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


test("[test-suite] sort: testing unpack", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        do
          local maxi = (1 << 31) - 1          -- maximum value for an int (usually)
          local mini = -(1 << 31)             -- minimum value for an int (usually)
          checkerror("too many results", unpack, {}, 0, maxi)
          checkerror("too many results", unpack, {}, 1, maxi)
          checkerror("too many results", unpack, {}, 0, maxI)
          checkerror("too many results", unpack, {}, 1, maxI)
          checkerror("too many results", unpack, {}, mini, maxi)
          checkerror("too many results", unpack, {}, -maxi, maxi)
          checkerror("too many results", unpack, {}, minI, maxI)
          unpack({}, maxi, 0)
          unpack({}, maxi, 1)
          unpack({}, maxI, minI)
          pcall(unpack, {}, 1, maxi + 1)
          local a, b = unpack({[maxi] = 20}, maxi, maxi)
          assert(a == 20 and b == nil)
          a, b = unpack({[maxi] = 20}, maxi - 1, maxi)
          assert(a == nil and b == 20)
          local t = {[maxI - 1] = 12, [maxI] = 23}
          a, b = unpack(t, maxI - 1, maxI); assert(a == 12 and b == 23)
          a, b = unpack(t, maxI, maxI); assert(a == 23 and b == nil)
          a, b = unpack(t, maxI, maxI - 1); assert(a == nil and b == nil)
          t = {[minI] = 12.3, [minI + 1] = 23.5}
          a, b = unpack(t, minI, minI + 1); assert(a == 12.3 and b == 23.5)
          a, b = unpack(t, minI, minI); assert(a == 12.3 and b == nil)
          a, b = unpack(t, minI + 1, minI); assert(a == nil and b == nil)
        end
    `;
    lualib.luaL_openlibs(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


test("[test-suite] sort: testing unpack", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        do   -- length is not an integer
          local t = setmetatable({}, {__len = function () return 'abc' end})
          assert(#t == 'abc')
          checkerror("object length is not an integer", table.insert, t, 1)
        end
    `;
    lualib.luaL_openlibs(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


test("[test-suite] sort: testing pack", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        a = table.pack()
        assert(a[1] == nil and a.n == 0)

        a = table.pack(table)
        assert(a[1] == table and a.n == 1)

        a = table.pack(nil, nil, nil, nil)
        assert(a[1] == nil and a.n == 4)
    `;
    lualib.luaL_openlibs(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


test("[test-suite] sort: testing move", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        do

          checkerror("table expected", table.move, 1, 2, 3, 4)

          local function eqT (a, b)
            for k, v in pairs(a) do assert(b[k] == v) end
            for k, v in pairs(b) do assert(a[k] == v) end
          end

          local a = table.move({10,20,30}, 1, 3, 2)  -- move forward
          eqT(a, {10,10,20,30})

          -- move forward with overlap of 1
          a = table.move({10, 20, 30}, 1, 3, 3)
          eqT(a, {10, 20, 10, 20, 30})

          -- moving to the same table (not being explicit about it)
          a = {10, 20, 30, 40}
          table.move(a, 1, 4, 2, a)
          eqT(a, {10, 10, 20, 30, 40})

          a = table.move({10,20,30}, 2, 3, 1)   -- move backward
          eqT(a, {20,30,30})

          a = {}   -- move to new table
          assert(table.move({10,20,30}, 1, 3, 1, a) == a)
          eqT(a, {10,20,30})

          a = {}
          assert(table.move({10,20,30}, 1, 0, 3, a) == a)  -- empty move (no move)
          eqT(a, {})

          a = table.move({10,20,30}, 1, 10, 1)   -- move to the same place
          eqT(a, {10,20,30})

          -- moving on the fringes
          a = table.move({[maxI - 2] = 1, [maxI - 1] = 2, [maxI] = 3},
                         maxI - 2, maxI, -10, {})
          eqT(a, {[-10] = 1, [-9] = 2, [-8] = 3})

          a = table.move({[minI] = 1, [minI + 1] = 2, [minI + 2] = 3},
                         minI, minI + 2, -10, {})
          eqT(a, {[-10] = 1, [-9] = 2, [-8] = 3})

          a = table.move({45}, 1, 1, maxI)
          eqT(a, {45, [maxI] = 45})

          a = table.move({[maxI] = 100}, maxI, maxI, minI)
          eqT(a, {[minI] = 100, [maxI] = 100})

          a = table.move({[minI] = 100}, minI, minI, maxI)
          eqT(a, {[minI] = 100, [maxI] = 100})

          a = setmetatable({}, {
                __index = function (_,k) return k * 10 end,
                __newindex = error})
          local b = table.move(a, 1, 10, 3, {})
          eqT(a, {})
          eqT(b, {nil,nil,10,20,30,40,50,60,70,80,90,100})

          b = setmetatable({""}, {
                __index = error,
                __newindex = function (t,k,v)
                  t[1] = string.format("%s(%d,%d)", t[1], k, v)
              end})
          table.move(a, 10, 13, 3, b)
          assert(b[1] == "(3,100)(4,110)(5,120)(6,130)")
          local stat, msg = pcall(table.move, b, 10, 13, 3, b)
          assert(not stat and msg == b)
        end
    `;
    lualib.luaL_openlibs(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


test("[test-suite] sort: testing long move", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        do
          -- for very long moves, just check initial accesses and interrupt
          -- move with an error
          local function checkmove (f, e, t, x, y)
            local pos1, pos2
            local a = setmetatable({}, {
                        __index = function (_,k) pos1 = k end,
                        __newindex = function (_,k) pos2 = k; error() end, })
            local st, msg = pcall(table.move, a, f, e, t)
            assert(not st and not msg and pos1 == x and pos2 == y)
          end
          checkmove(1, maxI, 0, 1, 0)
          checkmove(0, maxI - 1, 1, maxI - 1, maxI)
          checkmove(minI, -2, -5, -2, maxI - 6)
          checkmove(minI + 1, -1, -2, -1, maxI - 3)
          checkmove(minI, -2, 0, minI, 0)  -- non overlapping
          checkmove(minI + 1, -1, 1, minI + 1, 1)  -- non overlapping
        end

        checkerror("too many", table.move, {}, 0, maxI, 1)
        checkerror("too many", table.move, {}, -1, maxI - 1, 1)
        checkerror("too many", table.move, {}, minI, -1, 1)
        checkerror("too many", table.move, {}, minI, maxI, 1)
        checkerror("wrap around", table.move, {}, 1, maxI, 2)
        checkerror("wrap around", table.move, {}, 1, 2, maxI)
        checkerror("wrap around", table.move, {}, minI, -2, 2)
    `;
    lualib.luaL_openlibs(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


test("[test-suite] sort: testing sort, strange lengths", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        local a = setmetatable({}, {__len = function () return -1 end})
        assert(#a == -1)
        table.sort(a, error)    -- should not compare anything
        a = setmetatable({}, {__len = function () return maxI end})
        checkerror("too big", table.sort, a)
    `;
    lualib.luaL_openlibs(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


test("[test-suite] sort: test checks for invalid order functions", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        local function check (t)
          local function f(a, b) assert(a and b); return true end
          checkerror("invalid order function", table.sort, t, f)
        end

        check{1,2,3,4}
        check{1,2,3,4,5}
        check{1,2,3,4,5,6}
    `;
    lualib.luaL_openlibs(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


test("[test-suite] sort: sort alpha", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        function check (a, f)
          f = f or function (x,y) return x<y end;
          for n = #a, 2, -1 do
            assert(not f(a[n], a[n-1]))
          end
        end

        a = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
             "Oct", "Nov", "Dec"}

        table.sort(a)
        check(a)
    `;
    lualib.luaL_openlibs(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


test("[test-suite] sort: sort perm", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        function check (a, f)
          f = f or function (x,y) return x<y end;
          for n = #a, 2, -1 do
            assert(not f(a[n], a[n-1]))
          end
        end

        function perm (s, n)
          n = n or #s
          if n == 1 then
            local t = {unpack(s)}
            table.sort(t)
            check(t)
          else
            for i = 1, n do
              s[i], s[n] = s[n], s[i]
              perm(s, n - 1)
              s[i], s[n] = s[n], s[i]
            end
          end
        end

        perm{}
        perm{1}
        perm{1,2}
        perm{1,2,3}
        perm{1,2,3,4}
        perm{2,2,3,4}
        perm{1,2,3,4,5}
        perm{1,2,3,3,5}
        perm{1,2,3,4,5,6}
        perm{2,2,3,3,5,6}
    `;
    lualib.luaL_openlibs(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


test("[test-suite] sort: Invert-sorting", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        function check (a, f)
          f = f or function (x,y) return x<y end;
          for n = #a, 2, -1 do
            assert(not f(a[n], a[n-1]))
          end
        end

        a = {}
        for i=1,limit do
          a[i] = math.random()
        end

        x = os.clock(); i=0
        table.sort(a, function(x,y) i=i+1; return y<x end)
        x = (os.clock() - x) * 1000
        -- print(string.format("Invert-sorting other %d elements in %.2f msec., with %i comparisons",
        --       limit, x, i))
        check(a, function(x,y) return y<x end)

        table.sort{}  -- empty array

        for i=1,limit do a[i] = false end
        timesort(a, limit,  function(x,y) return nil end, "equal")

        for i,v in pairs(a) do assert(v == false) end
    `;
    lualib.luaL_openlibs(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


test("[test-suite] sort: sorting", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        function check (a, f)
          f = f or function (x,y) return x<y end;
          for n = #a, 2, -1 do
            assert(not f(a[n], a[n-1]))
          end
        end

        A = {"álo", "\\0first :-)", "alo", "then this one", "45", "and a new"}
        table.sort(A)
        check(A)

        table.sort(A, function (x, y)
                  load(string.format("A[%q] = ''", x), "")()
                  -- collectgarbage()
                  return x<y
                end)


        tt = {__lt = function (a,b) return a.val < b.val end}
        a = {}
        for i=1,10 do  a[i] = {val=math.random(100)}; setmetatable(a[i], tt); end
        table.sort(a)
        check(a, tt.__lt)
        check(a)
    `;
    lualib.luaL_openlibs(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});