summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Silverstone <dsilvers@digital-scurf.org>2012-09-08 14:13:00 +0100
committerDaniel Silverstone <dsilvers@digital-scurf.org>2012-09-08 14:14:39 +0100
commit3c5a5bc87cccf5d4a99f155e449bbb7a98c4262f (patch)
tree0e8e09dde1615325c6eed522f341ef0b480d4bb3
parentd1c919e95956c4c883aa052dbdbacbe1b6129387 (diff)
downloadgall-3c5a5bc87cccf5d4a99f155e449bbb7a98c4262f.tar.gz
GALL.UTIL: Start to test deep_copy
-rw-r--r--Makefile2
-rw-r--r--lib/gall/util.lua5
-rw-r--r--test/test-gall.util.lua91
3 files changed, 95 insertions, 3 deletions
diff --git a/Makefile b/Makefile
index 1ac9023..71479f2 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
all: test
-MODULES := gall
+MODULES := gall gall.util
LUA_VER := 5.1
INST_BASE := /usr/local
diff --git a/lib/gall/util.lua b/lib/gall/util.lua
index d6c5297..48181be 100644
--- a/lib/gall/util.lua
+++ b/lib/gall/util.lua
@@ -10,14 +10,15 @@ local function deep_copy(t, memo)
if not memo then memo = {} end
if memo[t] then return memo[t] end
local ret = {}
+ memo[t] = ret
local kk, vv
for k, v in pairs(t) do
kk, vv = k, v
if type(k) == "table" then
- kk = _deep_copy(k)
+ kk = deep_copy(k, memo)
end
if type(v) == "table" then
- vv = _deep_copy(v)
+ vv = deep_copy(v, memo)
end
ret[kk] = vv
end
diff --git a/test/test-gall.util.lua b/test/test-gall.util.lua
new file mode 100644
index 0000000..908e114
--- /dev/null
+++ b/test/test-gall.util.lua
@@ -0,0 +1,91 @@
+-- test/test-gall.util.lua
+--
+-- Git Abstraction layer for Lua - Utility function tests
+--
+-- Copyright 2012 Daniel Silverstone <dsilvers@digital-scurf.org>
+--
+-- For Licence terms, see COPYING
+--
+
+-- Step one, start coverage
+
+local luacov = require 'luacov'
+
+local util = require 'gall.util'
+
+local testnames = {}
+
+local real_assert = assert
+local total_asserts = 0
+local function assert(...)
+ local retval = real_assert(...)
+ total_asserts = total_asserts + 1
+ return retval
+end
+
+local function add_test(suite, name, value)
+ rawset(suite, name, value)
+ testnames[#testnames+1] = name
+end
+
+local suite = setmetatable({}, {__newindex = add_test})
+
+function suite.test_deep_copy_simple()
+ local intab = { "a", "b" }
+ local outtab = util.deep_copy(intab)
+ assert(intab ~= outtab)
+ assert(outtab[1] == "a")
+ assert(outtab[2] == "b")
+end
+
+function suite.test_deep_copy_recurse()
+ local intab = { { "a" } }
+ local outtab = util.deep_copy(intab)
+ assert(intab ~= outtab)
+ assert(intab[1] ~= outtab[1])
+ assert(outtab[1][1] == "a")
+end
+
+function suite.test_deep_copy_recurse_key()
+ local intab = { [{"a"}] = {"b"} }
+ local outtab = util.deep_copy(intab)
+ assert(intab ~= outtab)
+ local inkey, inval = next(intab)
+ local outkey, outval = next(outtab)
+ assert(inkey ~= outkey)
+ assert(outkey[1] == "a")
+ assert(inval ~= outval)
+ assert(outval[1] == "b")
+end
+
+function suite.test_deep_copy_memo()
+ local intab = { a = {} }
+ intab.b = intab.a
+ local outtab = util.deep_copy(intab)
+ assert(intab ~= outtab)
+ assert(outtab.a == outtab.b)
+end
+
+function suite.test_deep_copy_memo_loop()
+ local intab = {}
+ intab.a = intab
+ local outtab = util.deep_copy(intab)
+ assert(intab ~= outtab)
+ assert(outtab == outtab.a)
+end
+
+local count_ok = 0
+for _, testname in ipairs(testnames) do
+-- print("Run: " .. testname)
+ local ok, err = xpcall(suite[testname], debug.traceback)
+ if not ok then
+ print(err)
+ print()
+ else
+ count_ok = count_ok + 1
+ end
+end
+
+print(tostring(count_ok) .. "/" .. tostring(#testnames) .. " [" .. tostring(total_asserts) .. "] OK")
+
+os.exit(count_ok == #testnames and 0 or 1)