From fc79b2ae7a85af1b892a103340b9465274153c60 Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Fri, 24 Feb 2017 14:01:02 +0100 Subject: table.sort Using Array.prototype.sort for now --- tests/ltablib.js | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 76 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/ltablib.js b/tests/ltablib.js index 990b1f8..fa15d6a 100644 --- a/tests/ltablib.js +++ b/tests/ltablib.js @@ -17,6 +17,16 @@ const linit = require('../src/linit.js'); const lstate = require('../src/lstate.js'); const CT = lua.constant_types; +const inttable2array = function(t) { + let a = []; + + t.forEach(function (v, k) { + if (typeof k === 'number') + a[k] = v; + }); + + return a.map(e => e.value); +}; test('table.concat', function (t) { let luaCode = ` @@ -171,7 +181,7 @@ test('table.remove', function (t) { linit.luaL_openlibs(L); - lapi.lua_load(L, bc, "test-table.insert"); + lapi.lua_load(L, bc, "test-table.remove"); lapi.lua_call(L, 0, -1); @@ -204,7 +214,7 @@ test('table.move', function (t) { linit.luaL_openlibs(L); - lapi.lua_load(L, bc, "test-table.insert"); + lapi.lua_load(L, bc, "test-table.move"); lapi.lua_call(L, 0, -1); @@ -217,4 +227,68 @@ test('table.move', function (t) { [1, 2, 3, 4, 5, 6], "Correct element(s) on the stack" ); +}); + + +test('table.sort (<)', function (t) { + let luaCode = ` + local t = {3, 1, 5, ['just'] = 'tofuckitup', 2, 4} + table.sort(t) + return t + `, L; + + t.plan(2); + + t.doesNotThrow(function () { + + let bc = toByteCode(luaCode).dataView; + + L = lauxlib.luaL_newstate(); + + linit.luaL_openlibs(L); + + lapi.lua_load(L, bc, "test-table.sort"); + + lapi.lua_call(L, 0, -1); + + }, "JS Lua program ran without error"); + + t.deepEqual( + inttable2array(lapi.lua_topointer(L, -1)), + [1, 2, 3, 4, 5], + "Correct element(s) on the stack" + ); +}); + + +test('table.sort with cmp function', function (t) { + let luaCode = ` + local t = {3, 1, 5, ['just'] = 'tofuckitup', 2, 4} + table.sort(t, function (a, b) + return a > b + end) + return t + `, L; + + t.plan(2); + + t.doesNotThrow(function () { + + let bc = toByteCode(luaCode).dataView; + + L = lauxlib.luaL_newstate(); + + linit.luaL_openlibs(L); + + lapi.lua_load(L, bc, "test-table.sort"); + + lapi.lua_call(L, 0, -1); + + }, "JS Lua program ran without error"); + + t.deepEqual( + inttable2array(lapi.lua_topointer(L, -1)), + [5, 4, 3, 2, 1], + "Correct element(s) on the stack" + ); }); \ No newline at end of file -- cgit v1.2.3-54-g00ecf