Dec 27 22:09 2013 lazyList2Test.luam Page 1 --[[ Test Code for Lazy Cell-Based List Module -- LuaMacro Version Functional Programming Example in Lua H. Conrad Cunningham, Professor Computer and Information Science University of Mississippi Developed for CSci 658, Software Language Engineering, Fall 2013 1234567890123456789012345678901234567890123456789012345678901234567890 2013-09-26: Developed prototype for lazy lists from eager cell lists 2013-11-03: Updated definition and comments for use of LuaMacro 2 2013-12-27: Added testing of deepCopy This is a test driver for LuaMacro version of the Cell-Based List Module. --]] -- Load LuaMacro definition for Cons require_ 'lazyListMacros' -- Require Lazy Cell List Module -- (after luam -o lazyList2x.lua lazyList2.lua) local cl = require "lazyList2" -- As a convenience, introduce local names for values from the module local Nil, eCons = cl.Nil, cl.eCons local head, tail = cl.head, cl.tail local isList, isNil = cl.isList, cl.isNil local isValidList, copy = cl.isValidList, cl.copy local deepCopy = cl.deepCopy local take, drop = cl.take, cl.drop local length, append = cl.length, cl.append local traverse = cl.traverse local map, filter = cl.map, cl.filter local foldr, foldl = cl.foldr, cl.foldl local toList, fromList = cl.toList, cl.fromList local listToString = cl.listToString local show_data = cl.show_data local memoize = cl.memoize -- Function "treeConcat" takes a recursive list of lists in argument -- "t" and returns the corresponding parenthesized traversal string. -- A list is stored in the low positive integer indices of an -- array-style Lua table. local function treeConcat(t) if type(t) ~= "table" then return tostring(t) end local res = {} for i = 1, #t do res[i] = treeConcat(t[i]) Dec 27 22:09 2013 lazyList2Test.luam Page 2 end return "(" .. table.concat(res," ") .. ")" end -- Procedure "printTree" prints the corresponding parenthesized -- traversal string for its argument "t". local function printTree(t) print(treeConcat(t)) end -- Do partial testing local function test1() local function odd(x) return x % 2 == 1 end local function square(x) return x * x end local function add(x,y) return x + y end local function mul(x,y) return x * y end print("Test Cons, Nil, head, and tail.") print("isNil(Nil()) => " .. tostring(isNil(Nil()))) print("isList(Nil()) => " .. tostring(isList(Nil()))) local testCons = Cons("HEAD",Nil()) print("isNil(testCons => " .. tostring(isNil(testCons))) print("isList(testCons => " .. tostring(isList(testCons))) print("head(testCons) => " .. tostring(head(testCons))) print("tail(testCons) => " .. listToString(tail(testCons))) local fiveT = {1,2,3,4,5} print("Try toList(fiveT) of table " .. treeConcat(fiveT)) local fiveL = toList(fiveT) print("head(fiveL) => " .. tostring(head(fiveL))) print("head(tail(fiveL)) => " .. tostring(head(tail(fiveL)))) print("listToString(fiveL) => " .. listToString(fiveL)) print("length(fiveL) => " .. length(fiveL)) print("tofrom test.") local fiveTR = fromList(fiveL) for i = 1, #fiveTR do print("fiveTR[" .. i .. "] = " .. fiveTR[i] ) end print("Take/drop tests.") print("take(0,fiveL) => " .. listToString(take(0,fiveL))) print("take(1,fiveL) => " .. listToString(take(1,fiveL))) print("take(3,fiveL) => " .. listToString(take(3,fiveL))) print("take(5,fiveL) => " .. listToString(take(5,fiveL))) print("take(6,fiveL) => " .. listToString(take(6,fiveL))) print("drop(0,fiveL) => " .. listToString(drop(0,fiveL))) print("drop(1,fiveL) => " .. listToString(drop(1,fiveL))) Dec 27 22:09 2013 lazyList2Test.luam Page 3 print("drop(3,fiveL) => " .. listToString(drop(3,fiveL))) print("drop(5,fiveL) => " .. listToString(drop(5,fiveL))) print("drop(6,fiveL) => " .. listToString(drop(6,fiveL))) print("Append test.") print("append(fiveL,Nil()) = " .. listToString(append(fiveL,Nil()))) print("append(Nil(),fiveL) = " .. listToString(append(Nil(),fiveL))) local app = append(fiveL,fiveL) local fivefiveT = fromList(app) for i = 1, #fivefiveT do print("app[" .. i .. "] = " .. fivefiveT[i]) end print("Square fiveL.") print("map(square,Nil()) = " .. listToString(map(square,Nil()))) local sqfiveL = map(square,fiveL) local sqfiveT = fromList(sqfiveL) for i = 1, #sqfiveT do print("sqfiveL[" .. i .. "] = " .. sqfiveT[i]) end print("Filter test...Odd fivefive.") print("filter(even,Nil()) = " .. listToString(filter(even,Nil()))) local oddfivefive = filter(odd,app) local oddfivefiveT = fromList(oddfivefive) for i = 1, #oddfivefiveT do print("oddfivefive[" .. i .. "] = " .. oddfivefiveT[i]) end print("foldr test.") local result = foldr(add,0,fiveL) print("foldr(add,0,fiveL) = " .. result) print("foldl test.") local result = foldl(mul,1,fiveL) print("foldl(mul,1,fiveL) = " .. result) print("test copy") local new55 = copy(app) print("copy(fivefive) = " .. listToString(new55)) print("iterator test") for _, el in traverse(app) do print(tostring(el)) end print("iterator test") for _, el in traverse(app) do print(tostring(el)) end print("iterator over Nil test") for c, el in traverse(Nil()) do Dec 27 22:09 2013 lazyList2Test.luam Page 4 print(listToString(c),tostring(el)) end print("end iterator over Nil test") print("another iterator test.") local names = toList{"Conrad", "Dawn", "Tobin", "Carrie"} for _, el in traverse(names) do print(el) end -- test deepCopy print("test deepCopy.") local listdc = Cons("start",Cons(fiveL,fiveL)) local dc = deepCopy(listdc) print(listToString(dc)) print("same list? " .. tostring(listdc == dc)) local function intsfrom(i) return Cons(i,intsfrom(i+1)) end local ten = take(10,intsfrom(1)) print("ten = " .. show_data(ten)) print("ten = " .. listToString(ten)) end -- Call testing function test1()