Fix sorting with custom comparator, and add unit tests to regress

This commit is contained in:
James Roseborough
2008-04-03 03:58:15 +00:00
parent faaee456ad
commit 35ed9dca3c
4 changed files with 33 additions and 2 deletions

View File

@@ -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 {

View File

@@ -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
View 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)

View File

@@ -1 +1 @@
version: 0.22 version: 0.23