diff options
author | moteus <mimir@newmail.ru> | 2013-12-26 12:00:41 +0400 |
---|---|---|
committer | moteus <mimir@newmail.ru> | 2013-12-26 12:00:41 +0400 |
commit | febf9da1af8ba4cf0f8cc64b6af2adb0dcf9b354 (patch) | |
tree | 735ef634f8ebeab2101aa21897fd4e4e2f826f32 /test/test_table_copy.lua | |
parent | 46ed59584e5407c49a02f1ea6bede6487259a92e (diff) | |
download | lua-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.lua | 134 |
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 | |||
21 | local llthreads = require"llthreads" | ||
22 | |||
23 | local sleep | ||
24 | local status, socket = pcall(require,"socket") | ||
25 | if status then | ||
26 | sleep = function(secs) | ||
27 | return socket.sleep(secs) | ||
28 | end | ||
29 | else | ||
30 | sleep = function(secs) | ||
31 | os.execute("sleep " .. tonumber(secs)) | ||
32 | end | ||
33 | end | ||
34 | |||
35 | local dump_code = [==[ | ||
36 | local 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) .. "}" | ||
68 | end | ||
69 | |||
70 | local obj = ... | ||
71 | local seen = {} | ||
72 | return dump_recur(seen, obj, 0) | ||
73 | ]==] | ||
74 | |||
75 | local dump = (loadstring or load)(dump_code) | ||
76 | |||
77 | local child_code = [==[ | ||
78 | local dump = (loadstring or load)[[ | ||
79 | ]==] .. dump_code .. [==[ | ||
80 | ]] | ||
81 | local args = ... | ||
82 | |||
83 | print("Child thread args:", dump(args)) | ||
84 | |||
85 | -- return all values. | ||
86 | return ... | ||
87 | ]==] | ||
88 | |||
89 | local 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)) | ||
98 | end | ||
99 | |||
100 | -- create some tables. | ||
101 | local a1 = { "a1" } | ||
102 | local a2 = { "a2" } | ||
103 | local a3 = { "a3" } | ||
104 | local a4 = { "a4" } | ||
105 | local b1 = { a1, a2, a3, a4 } | ||
106 | local b2 = { a1=a1, a2=a2, a3=a3, a4=a4 } | ||
107 | |||
108 | -- | ||
109 | -- no loops | ||
110 | -- | ||
111 | test_thread_value_copying(b1, b2) | ||
112 | |||
113 | local top = {} | ||
114 | -- self reference. | ||
115 | top.top = top | ||
116 | top[top] = top | ||
117 | -- nested reference. | ||
118 | top.sub1 = { sub2 = { sub3 = { top } } } | ||
119 | |||
120 | -- | ||
121 | -- loops | ||
122 | -- | ||
123 | test_thread_value_copying(top) | ||
124 | |||
125 | -- | ||
126 | -- Test max depth | ||
127 | -- | ||
128 | local outer = {} | ||
129 | for n=1,100 do | ||
130 | outer = {outer} | ||
131 | end | ||
132 | local status, err = pcall(test_thread_value_copying,outer) | ||
133 | assert(not status, "Assertion failed: max depth test failed.") | ||
134 | |||