From 09771147cfc3e882b090a4299e26413cfb4151c9 Mon Sep 17 00:00:00 2001 From: James Roseborough Date: Wed, 9 Apr 2008 12:46:05 +0000 Subject: [PATCH] Improve unit test for "next()" function --- src/test/res/next.lua | 40 +++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/src/test/res/next.lua b/src/test/res/next.lua index 36de2f0b..36465713 100644 --- a/src/test/res/next.lua +++ b/src/test/res/next.lua @@ -1,14 +1,28 @@ + +-- call with arg 'true' to turn on error messages +local messages = select(1,...) +function mpcall(...) + if messages then return pcall(...) + else return (pcall(...)) end +end + -- unit tests for the next() function -function donexts(tag,table,count) - local index = nil - for i = 1,count do - index,value = next(table,index) - print( tag, index, value ) +function checkvalues(tag, tbl) + local values = {} + local index,value = next(tbl,nil) + while index do + table.insert( values, tostring(index).."="..tostring(value) ) + index,value = next(tbl,index) end - print( tag, '--- -1', 'pcall( next, table,-1 )', pcall( next, table,-1 ) ) - print( tag, '--- 0', 'pcall( next, table,0 )', pcall( next, table,0 ) ) - print( tag, '---"a"', 'pcall( next, table,"a" )', pcall( next, table,"a" ) ) - print( tag, '--- 10', 'pcall( next, table, 10 )', pcall( next, table, 10 ) ) + table.sort( values ) + print( tag, "values: {"..table.concat(values,",").."}" ) +end +function donexts(tag,tbl,count) + checkvalues(tag,tbl) + print( tag, '--- -1', 'pcall( next, tbl,-1 )', mpcall( next, tbl,-1 ) ) + print( tag, '--- 0', 'pcall( next, tbl,0 )', mpcall( next, tbl,0 ) ) + print( tag, '---"a"', 'pcall( next, tbl,"a" )', mpcall( next, tbl,"a" ) ) + print( tag, '--- 10', 'pcall( next, tbl, 10 )', mpcall( next, tbl, 10 ) ) end donexts( 'next1', {}, 2 ) donexts( 'next2', {'one', 'two', 'three' }, 5 ) @@ -16,8 +30,8 @@ donexts( 'next3', { aa='aaa', bb='bbb', cc='ccc', [20]='20', [30]='30'}, 7 ) donexts( 'next4', {'one', 'two', 'three', aa='aaa', bb='bbb', cc='ccc', [20]='20', [30]='30'}, 9 ) donexts( 'next5', {'one', 'two', 'three', [-1]='minus-one', [0]='zero' }, 7 ) -print( 'pcall(next)', pcall(next) ) -print( 'pcall(next,nil)', pcall(next,nil) ) -print( 'pcall(next,"a")', pcall(next,"a") ) -print( 'pcall(next,1)', pcall(next,1) ) +print( 'pcall(next)', mpcall(next) ) +print( 'pcall(next,nil)', mpcall(next,nil) ) +print( 'pcall(next,"a")', mpcall(next,"a") ) +print( 'pcall(next,1)', mpcall(next,1) )