--[[ Test Code for Cell-Based List Module using Function Closures for Cells 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-12-27: Developed from basic cellList test code --]] -- Require Cons List Module local cl = require "cellListFunct" -- As a convenience, introduce local names for values from the module local Nil, Cons = cl.Nil, cl.Cons 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 -- 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]) 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.") local testCons = Cons("HEAD",Nil()) 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))) 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 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)) end -- Call testing function test1()