--[[ Carrie's Candy Bowl Test Driver CSci 658: Software Language Engineering, Fall 2013 Exam 1, Problem 4 H. Conrad Cunningham, Professor Computer and Information Science University of Mississippi Developed for CSci 658, Software Language Engineering, Fall 2013 1234567890123456789012345678901234567890123456789012345678901234567890 2013-11-17: Experimental version before exam given 2013-11-25: Modified, documented, and separated from candybowl module --]] -- 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 -- Tests local function runTests(ccbm) local CandyBowl = ccbm.CandyBowl local put, put2, take, take2 = ccbm.put, ccbm.put2, ccbm.take, ccbm.take2 local combine, combine2 = ccbm.combine, ccbm.combine2 local inventory, toBowl = ccbm.inventory, ccbm.toBowl local has, howMany, isEmpty = ccbm.has, ccbm.howMany, ccbm.isEmpty local cb = CandyBowl() local eb = CandyBowl() print("cb and eb are both new empty bowls)") print("howMany(cb) = " .. howMany(cb)) print("howMany(cb,'snickers') = " .. howMany(cb,'snickers')) print("howMany(cb,'kisses') = " .. howMany(cb,'kisses')) print("howMany(cb,'tootsie') = " .. howMany(cb,'tootsie')) print("isEmpty(cb) = " .. tostring(isEmpty(cb))) print("inventory(cb) = " .. treeConcat(inventory(cb))) print("has(cb,'snickers') = " .. tostring(has(cb,'snickers'))) print("has(cb,'kisses') = " .. tostring(has(cb,'kisses'))) print("has(cb,'tootsie') = " .. tostring(has(cb,'tootsie'))) print("combine(cb,eb) = " .. treeConcat(combine(cb,eb))) print("combine(cb,eb) = " .. treeConcat(inventory(combine(cb,eb)))) print("\nAdd 1 snickers to cb") cb = put(cb,'snickers') print("howMany(cb) = " .. howMany(cb)) print("howMany(cb,'snickers') = " .. howMany(cb,'snickers')) print("howMany(cb,'kisses') = " .. howMany(cb,'kisses')) print("howMany(cb,'tootsie') = " .. howMany(cb,'tootsie')) print("isEmpty(cb) = " .. tostring(isEmpty(cb))) print("inventory(cb) = " .. treeConcat(inventory(cb))) print("has(cb,'snickers') = " .. tostring(has(cb,'snickers'))) print("has(cb,'kisses') = " .. tostring(has(cb,'kisses'))) print("has(cb,'tootsie') = " .. tostring(has(cb,'tootsie'))) print("combine(cb,eb) = " .. treeConcat(inventory(combine(cb,eb)))) print("combine(eb,cb) = " .. treeConcat(inventory(combine(eb,cb)))) print("combine(cb,cb) = " .. treeConcat(inventory(combine(cb,cb)))) print("combine2(cb,eb) = " .. treeConcat(inventory(combine2(cb,eb)))) print("combine2(eb,cb) = " .. treeConcat(inventory(combine2(eb,cb)))) print("combine2(cb,cb) = " .. treeConcat(inventory(combine2(cb,cb)))) print("\nAdding second snickers to cb") cb = put(cb,'snickers') print("howMany(cb) = " .. howMany(cb)) print("howMany(cb,'snickers') = " .. howMany(cb,'snickers')) print("howMany(cb,'kisses') = " .. howMany(cb,'kisses')) print("howMany(cb,'tootsie') = " .. howMany(cb,'tootsie')) print("isEmpty(cb) = " .. tostring(isEmpty(cb))) print("isEmpty(cb) = " .. tostring(isEmpty(cb))) print("inventory(cb) = " .. treeConcat(inventory(cb))) print("has(cb,'snickers') = " .. tostring(has(cb,'snickers'))) print("has(cb,'kisses') = " .. tostring(has(cb,'kisses'))) print("has(cb,'tootsie') = " .. tostring(has(cb,'tootsie'))) print("combine(cb,eb) = " .. treeConcat(inventory(combine(cb,eb)))) print("combine(eb,cb) = " .. treeConcat(inventory(combine(eb,cb)))) print("combine(cb,cb) = " .. treeConcat(inventory(combine(cb,cb)))) print("combine2(cb,eb) = " .. treeConcat(inventory(combine2(cb,eb)))) print("combine2(eb,cb) = " .. treeConcat(inventory(combine2(eb,cb)))) print("combine2(cb,cb) = " .. treeConcat(inventory(combine2(cb,cb)))) print("\nAdding a kiss to cb") cb = put(cb,'kisses') print("howMany(cb) = " .. howMany(cb)) print("isEmpty(cb) = " .. tostring(isEmpty(cb))) print("howMany(cb,'snickers') = " .. howMany(cb,'snickers')) print("howMany(cb,'kisses') = " .. howMany(cb,'kisses')) print("howMany(cb,'tootsie') = " .. howMany(cb,'tootsie')) print("inventory(cb) = " .. treeConcat(inventory(cb))) print("has(cb,'snickers') = " .. tostring(has(cb,'snickers'))) print("has(cb,'kisses') = " .. tostring(has(cb,'kisses'))) print("has(cb,'tootsie') = " .. tostring(has(cb,'tootsie'))) print("combine(cb,eb) = " .. treeConcat(inventory(combine(cb,eb)))) print("combine(eb,cb) = " .. treeConcat(inventory(combine(eb,cb)))) print("combine(cb,cb) = " .. treeConcat(inventory(combine(cb,cb)))) print("combine2(cb,eb) = " .. treeConcat(inventory(combine2(cb,eb)))) print("combine2(eb,cb) = " .. treeConcat(inventory(combine2(eb,cb)))) print("combine2(cb,cb) = " .. treeConcat(inventory(combine2(cb,cb)))) print("\nAdding gum to cb") cb = put(cb,'gum') print("howMany(cb) = " .. howMany(cb)) print("isEmpty(cb) = " .. tostring(isEmpty(cb))) print("howMany(cb,'snickers') = " .. howMany(cb,'snickers')) print("howMany(cb,'kisses') = " .. howMany(cb,'kisses')) print("howMany(cb,'tootsie') = " .. howMany(cb,'tootsie')) print("inventory(cb) = " .. treeConcat(inventory(cb))) print("has(cb,'snickers') = " .. tostring(has(cb,'snickers'))) print("has(cb,'kisses') = " .. tostring(has(cb,'kisses'))) print("has(cb,'tootsie') = " .. tostring(has(cb,'tootsie'))) print("combine(cb,eb) = " .. treeConcat(inventory(combine(cb,eb)))) print("combine(eb,cb) = " .. treeConcat(inventory(combine(eb,cb)))) print("combine(cb,cb) = " .. treeConcat(inventory(combine(cb,cb)))) print("combine2(cb,eb) = " .. treeConcat(inventory(combine2(cb,eb)))) print("combine2(eb,cb) = " .. treeConcat(inventory(combine2(eb,cb)))) print("combine2(cb,cb) = " .. treeConcat(inventory(combine2(cb,cb)))) print("\nRemoving a snickers from cb") cb = take(cb,'snickers') print("howMany(cb) = " .. howMany(cb)) print("isEmpty(cb) = " .. tostring(isEmpty(cb))) print("howMany(cb,'snickers') = " .. howMany(cb,'snickers')) print("howMany(cb,'kisses') = " .. howMany(cb,'kisses')) print("howMany(cb,'tootsie') = " .. howMany(cb,'tootsie')) print("howMany(cb,'gum') = " .. howMany(cb,'gum')) print("inventory(cb) = " .. treeConcat(inventory(cb))) print("has(cb,'snickers') = " .. tostring(has(cb,'snickers'))) print("has(cb,'kisses') = " .. tostring(has(cb,'kisses'))) print("has(cb,'tootsie') = " .. tostring(has(cb,'tootsie'))) print("has(cb,'gum') = " .. tostring(has(cb,'gum'))) print("combine(cb,eb) = " .. treeConcat(inventory(combine(cb,eb)))) print("combine(eb,cb) = " .. treeConcat(inventory(combine(eb,cb)))) print("combine(cb,cb) = " .. treeConcat(inventory(combine(cb,cb)))) print("combine2(cb,eb) = " .. treeConcat(inventory(combine2(cb,eb)))) print("combine2(eb,cb) = " .. treeConcat(inventory(combine2(eb,cb)))) print("combine2(cb,cb) = " .. treeConcat(inventory(combine2(cb,cb)))) print("\nRemoving a kiss from cb") cb = take(cb,'kisses') print("howMany(cb) = " .. howMany(cb)) print("isEmpty(cb) = " .. tostring(isEmpty(cb))) print("howMany(cb,'snickers') = " .. howMany(cb,'snickers')) print("howMany(cb,'kisses') = " .. howMany(cb,'kisses')) print("howMany(cb,'tootsie') = " .. howMany(cb,'tootsie')) print("howMany(cb,'gum') = " .. howMany(cb,'gum')) print("inventory(cb) = " .. treeConcat(inventory(cb))) print("has(cb,'snickers') = " .. tostring(has(cb,'snickers'))) print("has(cb,'kisses') = " .. tostring(has(cb,'kisses'))) print("has(cb,'tootsie') = " .. tostring(has(cb,'tootsie'))) print("has(cb,'gum') = " .. tostring(has(cb,'gum'))) print("combine(cb,eb) = " .. treeConcat(inventory(combine(cb,eb)))) print("combine(eb,cb) = " .. treeConcat(inventory(combine(eb,cb)))) print("combine(cb,cb) = " .. treeConcat(inventory(combine(cb,cb)))) print("combine2(cb,eb) = " .. treeConcat(inventory(combine2(cb,eb)))) print("combine2(eb,cb) = " .. treeConcat(inventory(combine2(eb,cb)))) print("combine2(cb,cb) = " .. treeConcat(inventory(combine2(cb,cb)))) print("\nRemoving a snickers from cb") cb = take(cb,'snickers') print("howMany(cb) = " .. howMany(cb)) print("isEmpty(cb) = " .. tostring(isEmpty(cb))) print("howMany(cb,'snickers') = " .. howMany(cb,'snickers')) print("howMany(cb,'kisses') = " .. howMany(cb,'kisses')) print("howMany(cb,'tootsie') = " .. howMany(cb,'tootsie')) print("howMany(cb,'gum') = " .. howMany(cb,'gum')) print("inventory(cb) = " .. treeConcat(inventory(cb))) print("has(cb,'snickers') = " .. tostring(has(cb,'snickers'))) print("has(cb,'kisses') = " .. tostring(has(cb,'kisses'))) print("has(cb,'tootsie') = " .. tostring(has(cb,'tootsie'))) print("has(cb,'gum') = " .. tostring(has(cb,'gum'))) print("combine(cb,eb) = " .. treeConcat(inventory(combine(cb,eb)))) print("combine(eb,cb) = " .. treeConcat(inventory(combine(eb,cb)))) print("combine(cb,cb) = " .. treeConcat(inventory(combine(cb,cb)))) print("combine2(cb,eb) = " .. treeConcat(inventory(combine2(cb,eb)))) print("combine2(eb,cb) = " .. treeConcat(inventory(combine2(eb,cb)))) print("combine2(cb,cb) = " .. treeConcat(inventory(combine2(cb,cb)))) print("\nRemoving gum from cb") cb = take(cb,'gum') print("howMany(cb) = " .. howMany(cb)) print("isEmpty(cb) = " .. tostring(isEmpty(cb))) print("howMany(cb,'snickers') = " .. howMany(cb,'snickers')) print("howMany(cb,'kisses') = " .. howMany(cb,'kisses')) print("howMany(cb,'tootsie') = " .. howMany(cb,'tootsie')) print("howMany(cb,'gum') = " .. howMany(cb,'gum')) print("inventory(cb) = " .. treeConcat(inventory(cb))) print("has(cb,'snickers') = " .. tostring(has(cb,'snickers'))) print("has(cb,'kisses') = " .. tostring(has(cb,'kisses'))) print("has(cb,'tootsie') = " .. tostring(has(cb,'tootsie'))) print("has(cb,'gum') = " .. tostring(has(cb,'gum'))) print("combine(cb,eb) = " .. treeConcat(inventory(combine(cb,eb)))) print("combine(eb,cb) = " .. treeConcat(inventory(combine(eb,cb)))) print("combine(cb,cb) = " .. treeConcat(inventory(combine(cb,cb)))) print("combine2(cb,eb) = " .. treeConcat(inventory(combine2(cb,eb)))) print("combine2(eb,cb) = " .. treeConcat(inventory(combine2(eb,cb)))) print("combine2(cb,cb) = " .. treeConcat(inventory(combine2(cb,cb)))) print("\nRemoving illegal candyType from cb--force error") local takeok, takemsg = pcall(function() cb = take(cb,'gum') end) if takeok then print("inventory(cb) = " .. treeConcat(inventory(cb))) print("This test should not succeed.") else print("take failed: " .. takemsg) print("This test should fail.") end end -- Run tests on candybowl modules local ccbm = require "candybowl_hash" print("\nBegin tests of candybowl_hash") runTests(ccbm) print("\nEnd tests of hashed candybowl") local ccbm = require "candybowl_list" print("\nBegin tests of list candybowl") runTests(ccbm) print("\nEnd tests of candybowl_list")