aboutsummaryrefslogtreecommitdiff
path: root/test/test_table_copy.lua
diff options
context:
space:
mode:
authormoteus <mimir@newmail.ru>2013-12-26 12:00:41 +0400
committermoteus <mimir@newmail.ru>2013-12-26 12:00:41 +0400
commitfebf9da1af8ba4cf0f8cc64b6af2adb0dcf9b354 (patch)
tree735ef634f8ebeab2101aa21897fd4e4e2f826f32 /test/test_table_copy.lua
parent46ed59584e5407c49a02f1ea6bede6487259a92e (diff)
downloadlua-llthreads2-febf9da1af8ba4cf0f8cc64b6af2adb0dcf9b354.tar.gz
lua-llthreads2-febf9da1af8ba4cf0f8cc64b6af2adb0dcf9b354.tar.bz2
lua-llthreads2-febf9da1af8ba4cf0f8cc64b6af2adb0dcf9b354.zip
First commit.
Diffstat (limited to 'test/test_table_copy.lua')
-rw-r--r--test/test_table_copy.lua134
1 files changed, 134 insertions, 0 deletions
diff --git a/test/test_table_copy.lua b/test/test_table_copy.lua
new file mode 100644
index 0000000..0408ad3
--- /dev/null
+++ b/test/test_table_copy.lua
@@ -0,0 +1,134 @@
1-- Copyright (c) 2011 by Robert G. Jakabosky <bobby@sharedrealm.com>
2--
3-- Permission is hereby granted, free of charge, to any person obtaining a copy
4-- of this software and associated documentation files (the "Software"), to deal
5-- in the Software without restriction, including without limitation the rights
6-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7-- copies of the Software, and to permit persons to whom the Software is
8-- furnished to do so, subject to the following conditions:
9--
10-- The above copyright notice and this permission notice shall be included in
11-- all copies or substantial portions of the Software.
12--
13-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19-- THE SOFTWARE.
20
21local llthreads = require"llthreads"
22
23local sleep
24local status, socket = pcall(require,"socket")
25if status then
26 sleep = function(secs)
27 return socket.sleep(secs)
28 end
29else
30 sleep = function(secs)
31 os.execute("sleep " .. tonumber(secs))
32 end
33end
34
35local dump_code = [==[
36local function dump_recur(seen, obj, depth)
37 local out
38 local t = type(obj)
39 -- if not a table just convert to string.
40 if t ~= "table" then
41 if t == "string" then
42 return '"' .. obj .. '"'
43 end
44 return tostring(obj)
45 end
46 -- check if this table has been seen already.
47 if seen[obj] then
48 return "Already dumped " .. tostring(obj)
49 end
50 seen[obj] = true
51 -- restrict max depth.
52 if depth >= 10 then
53 return "{... max depth reached ...}"
54 end
55 depth = depth + 1
56 -- output table key/value pairs
57 local tabs = string.rep(" ",depth)
58 local out = "{\n"
59 for k,v in pairs(obj) do
60 if type(k) ~= "number" then
61 out = out .. tabs .. '[' .. dump_recur(seen, k, depth) .. '] = ' ..
62 dump_recur(seen, v, depth) .. ',\n'
63 else
64 out = out .. tabs .. '[' .. k .. '] = ' .. dump_recur(seen, v, depth) .. ',\n'
65 end
66 end
67 return out .. tabs:sub(1,-3) .. "}"
68end
69
70local obj = ...
71local seen = {}
72return dump_recur(seen, obj, 0)
73]==]
74
75local dump = (loadstring or load)(dump_code)
76
77local child_code = [==[
78local dump = (loadstring or load)[[
79]==] .. dump_code .. [==[
80]]
81local args = ...
82
83print("Child thread args:", dump(args))
84
85-- return all values.
86return ...
87]==]
88
89local function test_thread_value_copying(...)
90 local args = {...}
91 print("Main thread args:", dump(args))
92 local thread = llthreads.new(child_code, args)
93 -- start joinable thread
94 assert(thread:start())
95
96 local status, results = thread:join()
97 print("Main thread results:", dump(results))
98end
99
100-- create some tables.
101local a1 = { "a1" }
102local a2 = { "a2" }
103local a3 = { "a3" }
104local a4 = { "a4" }
105local b1 = { a1, a2, a3, a4 }
106local b2 = { a1=a1, a2=a2, a3=a3, a4=a4 }
107
108--
109-- no loops
110--
111test_thread_value_copying(b1, b2)
112
113local top = {}
114-- self reference.
115top.top = top
116top[top] = top
117-- nested reference.
118top.sub1 = { sub2 = { sub3 = { top } } }
119
120--
121-- loops
122--
123test_thread_value_copying(top)
124
125--
126-- Test max depth
127--
128local outer = {}
129for n=1,100 do
130 outer = {outer}
131end
132local status, err = pcall(test_thread_value_copying,outer)
133assert(not status, "Assertion failed: max depth test failed.")
134