Fix sorting with custom comparator, and add unit tests to regress
This commit is contained in:
@@ -645,7 +645,7 @@ public class LTable extends LValue {
|
|||||||
vm.pushlvalue(m_vector[i]);
|
vm.pushlvalue(m_vector[i]);
|
||||||
vm.pushlvalue(m_vector[j]);
|
vm.pushlvalue(m_vector[j]);
|
||||||
vm.call(2, 1);
|
vm.call(2, 1);
|
||||||
boolean result = vm.toboolean(1);
|
boolean result = vm.toboolean(-1);
|
||||||
vm.resettop();
|
vm.resettop();
|
||||||
return result;
|
return result;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -148,6 +148,10 @@ public class LuaJTest extends TestCase {
|
|||||||
runTest( "strlib" );
|
runTest( "strlib" );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testSort() throws IOException, InterruptedException {
|
||||||
|
runTest( "sort" );
|
||||||
|
}
|
||||||
|
|
||||||
public void testTable() throws IOException, InterruptedException {
|
public void testTable() throws IOException, InterruptedException {
|
||||||
runTest( "table" );
|
runTest( "table" );
|
||||||
}
|
}
|
||||||
|
|||||||
27
src/test/res/sort.lua
Normal file
27
src/test/res/sort.lua
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
-- concat
|
||||||
|
print( '-- sort tests' )
|
||||||
|
local function tryall(cmp)
|
||||||
|
local function try(t)
|
||||||
|
print( table.concat(t,'-') )
|
||||||
|
if pcall( table.sort, t, cmp ) then
|
||||||
|
print( table.concat(t,'-') )
|
||||||
|
else
|
||||||
|
print( 'sort failed' )
|
||||||
|
end
|
||||||
|
end
|
||||||
|
try{ 2, 4, 6, 8, 1, 3, 5, 7 }
|
||||||
|
try{ 333, 222, 111 }
|
||||||
|
try{ "www", "xxx", "yyy", "aaa", "bbb", "ccc" }
|
||||||
|
try{ 21, 23, "25", 27, 22, "24", 26, 28 }
|
||||||
|
end
|
||||||
|
local function comparator(a,b)
|
||||||
|
return tonumber(a)<tonumber(b)
|
||||||
|
end
|
||||||
|
tryall()
|
||||||
|
tryall(comparator)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1 +1 @@
|
|||||||
version: 0.22
|
version: 0.23
|
||||||
|
|||||||
Reference in New Issue
Block a user