From facb8d1d07a0e16b9ae1476cc622b068c03526e9 Mon Sep 17 00:00:00 2001 From: James Roseborough Date: Wed, 31 Oct 2007 19:03:18 +0000 Subject: [PATCH] Unit test for table package functions --- src/test/res/table.lua | 68 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/test/res/table.lua diff --git a/src/test/res/table.lua b/src/test/res/table.lua new file mode 100644 index 00000000..facaeb79 --- /dev/null +++ b/src/test/res/table.lua @@ -0,0 +1,68 @@ +-- concat +print( '-- concat tests' ) +function tryconcat(t) + print( table.concat(t) ) + print( table.concat(t,'--') ) + print( table.concat(t,',',2) ) + print( table.concat(t,',',2,2) ) + print( table.concat(t,',',5,2) ) +end +tryconcat( { "one", "two", "three", a='aaa', b='bbb', c='ccc' } ) +tryconcat( { "one", "two", "three", "four", "five" } ) +function tryconcat(t) + print( table.concat(t) ) + print( table.concat(t,'--') ) + print( table.concat(t,',',2) ) +end +tryconcat( { a='aaa', b='bbb', c='ccc', d='ddd', e='eee' } ) +tryconcat( { [501]="one", [502]="two", [503]="three", [504]="four", [505]="five" } ) +tryconcat( {} ) + +-- insert, maxn +print( '-- insert, maxn tests' ) +local t = { "one", "two", "three", a='aaa', b='bbb', c='ccc' } +print( table.concat(t,'-'), table.maxn(t), #t ) +table.insert(t,'six') +print( table.concat(t,'-'), table.maxn(t), #t ) +table.insert(t,1,'seven') +print( table.concat(t,'-'), table.maxn(t), #t ) +table.insert(t,4,'eight') +print( table.concat(t,'-'), table.maxn(t), #t ) +table.insert(t,7,'nine') +print( table.concat(t,'-'), table.maxn(t), #t ) +table.insert(t,10,'ten') +print( table.concat(t,'-'), table.maxn(t), #t ) +print( t[10] ) +print( table.maxn({}), #{} ) + +-- remove +print( '-- remove tests' ) +t = { "one", "two", "three", "four", "five", "six", "seven", [10]="ten", a='aaa', b='bbb', c='ccc' } +print( table.concat(t,'-'), table.maxn(t), #t ) +table.remove(t) +print( table.concat(t,'-'), table.maxn(t) ) +table.remove(t,1) +print( table.concat(t,'-'), table.maxn(t) ) +table.remove(t,3) +print( table.concat(t,'-'), table.maxn(t) ) +table.remove(t,5) +print( table.concat(t,'-'), table.maxn(t), t[10] ) +table.remove(t,10) +print( table.concat(t,'-'), table.maxn(t), t[10] ) +table.remove(t,-1) +print( table.concat(t,'-'), table.maxn(t), t[10] ) +table.remove(t,-1) +print( table.concat(t,'-'), table.maxn(t), t[10] ) + +-- sort +print( '-- sort tests' ) +t = { "one", "two", "three", a='aaa', b='bbb', c='ccc' } +print( table.concat(t,'-'), table.maxn(t), #t ) +table.sort(t) +print( table.concat(t,'-'), table.maxn(t), #t ) +t = { "zzz", "yyy", "xxx", "www", "vvv", "uuu", "ttt", "sss" } +print( table.concat(t,'-'), table.maxn(t), #t ) +table.sort(t) +print( table.concat(t,'-'), table.maxn(t), #t ) +table.sort(t,function(a,b) return b