summaryrefslogtreecommitdiff
path: root/tests/ltablib.js
diff options
context:
space:
mode:
authorBenoit Giannangeli <benoit.giannangeli@boursorama.fr>2017-02-24 14:01:02 +0100
committerBenoit Giannangeli <benoit.giannangeli@boursorama.fr>2017-02-24 14:01:02 +0100
commitfc79b2ae7a85af1b892a103340b9465274153c60 (patch)
tree5e3dc8cab20bbbe994390eda53007c08068feeff /tests/ltablib.js
parent2f9fe378bc341921e1ae259a2fec049663100738 (diff)
downloadfengari-fc79b2ae7a85af1b892a103340b9465274153c60.tar.gz
fengari-fc79b2ae7a85af1b892a103340b9465274153c60.tar.bz2
fengari-fc79b2ae7a85af1b892a103340b9465274153c60.zip
table.sort
Using Array.prototype.sort for now
Diffstat (limited to 'tests/ltablib.js')
-rw-r--r--tests/ltablib.js78
1 files changed, 76 insertions, 2 deletions
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