-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.lua
216 lines (183 loc) · 6.09 KB
/
test.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
---@diagnostic disable: lowercase-global
-- Because the Lua Test Adapter for VS Code runs the test from
-- the workspace folder / respository root,
-- this file should be kept in this directory.
-- (Maybe one can pass arguments to the lua interpreter used by the Test Adapter.)
package.path = "./test/?.lua;./src/?.lua;"..package.path
local mp_aux = require("lomp-aux")
local msg = require("lomp-msg")
local mpn = require("lomp-mpn")
lu = require("luaunit")
-- set WIDTH == 15 for (simulating) 32-bit systems
-- set WIDTH == 31 for 64-bit systems
-- I recommend WIDTH == 8 to test simulate multiple limb integers.
mpn.__set_limb_width(8, "testing")
local function logf(fmt, ...)
return print("test Log: "..string.format(fmt, ...))
end
local function num_tostring(num)
if type(num) == "number" then
if math.type(num) == "float" then
return string.format("%f", num)
else
return string.format("%d", num)
end
else
return tostring(num)
end
end
local GROUP_WIDTH_HEX = mpn.get_count_of_hex_digits_per_limb()
function hex(num)
return mp_aux.grouped_hex_from_int(num, GROUP_WIDTH_HEX)
end
function dec(num)
return mp_aux.grouped_dec_from_int(num)
end
-- ########## deep coping ##########
local __dp = {} -- table needed because of pairwise recurisve call :-(
-- returns a deep_copy of a value
--
-- If the value is a simple type (including nil and strings),
-- then it simply returns the value (therefore making a copy)
--
-- If the value is a complex type
-- (currently tables are the only supported complex type)
-- and it has a metamethod __deep_copy,
-- the it calls this as __deep_copy(self_table)
--
-- Note: Functions can only be "copied" as reference.
--
-- If the value is a complex type
-- and does not have a metamethod __deep_copy,
-- then it uses pairs() to make copies of all keys and values
-- This default __deep_copy function passes the metatable as reference
-- to the deep copy
-- For almost all data structures
-- this default __deep_copy function is not sufficent.
function __dp.deep_copy(v)
local mt = getmetatable(v)
if not rawequal(mt, nil) then
local deep_copy_func = mt.__deep_copy
if type(deep_copy_func) == "function" then
return deep_copy_func(v)
end
end
---@diagnostic disable-next-line: undefined-global
return __dp.deep_copy_default(v)
end
-- deep copied a table
function __dp.deep_copy_default(obj)
local type_str = type(obj)
if rawequal(type_str, "table") then
local dct = setmetatable({}, __dp.deep_copy(getmetatable(obj)))
for k, v in pairs(obj) do
dct[__dp.deep_copy(k)] = __dp.deep_copy(v)
end
return dct
elseif rawequal(type_str, "thread") or rawequal(type_str,"userdata") then
msg.errorf("__dp.deep_copy_default(value) does not support values"
.." of the (complex) type %s", type_str)
return
end
return obj
end
if false then
local __TEST_MSG_INFOS = {}
__TEST_MSG_INFOS.backup_test_datum_objs = {}
__TEST_MSG_INFOS.test_datum_objs = {}
function TEST_OP_NAME(op_name)
assert( type(op_name) == "string" or rawequal(op_name, nil))
__TEST_MSG_INFOS.op_name = op_name
end
function BEGIN_TEST_DATA()
local cur_test_datum_objs = __TEST_MSG_INFOS.test_datum_objs
assert( type(cur_test_datum_objs) == "table" )
cur_test_datum_objs = __dp.deep_copy(cur_test_datum_objs)
assert( type(cur_test_datum_objs) == "table" )
table.insert(__TEST_MSG_INFOS.backup_test_datum_objs,
cur_test_datum_objs)
end
function TEST_DATUM(k, v)
assert( type(k) == "string" )
local test_datum_obj = { k = k ; v = v }
table.insert(__TEST_MSG_INFOS.test_datum_objs, test_datum_obj)
end
function END_TEST_DATA()
local backups = __TEST_MSG_INFOS.backup_test_datum_objs
local backup_size = #backups
assert( backup_size >= 1 )
__TEST_MSG_INFOS.test_datum_objs = backups[backup_size]
table.remove(backups)
assert( #backups == backup_size - 1 )
end
local function __sprint_test_data()
local str
for _, test_datum_obj in ipairs(__TEST_MSG_INFOS.test_datum_objs) do
if rawequal(str, nil) then
str = ""
else
str = str.."\n"
end
str = str..string.format("%s == %s", test_datum_obj.k,
tostring(test_datum_obj.v))
end
return str
end
__TEST_MSG_INFOS.on = false
function TEST_MSG_ON()
__TEST_MSG_INFOS.on = true
end
function BEGIN_TEST_CASE()
if __TEST_MSG_INFOS.on then
local suffix = ""
local op_name = __TEST_MSG_INFOS.op_name
if not rawequal(op_name, nil) then
suffix = "on operation "..op_name
end
if not rawequal(suffix, "") then
suffix = suffix.." "
end
print(string.rep("=", 10).." test case "..suffix..string.rep("=", 30))
print("test data:")
print(__sprint_test_data())
print(string.rep("-", 25).." begin test routine "..string.rep("-", 55))
end
end
function BEGIN_TEST_OP()
if __TEST_MSG_INFOS.on then
print(string.rep("-", 35)
.." begin operation to test "
..string.rep("-", 40))
end
end
function END_TEST_OP()
if __TEST_MSG_INFOS.on then
print(string.rep("-", 35)
.." end operation to test "
..string.rep("-", 40))
end
end
function END_TEST_CASE()
if __TEST_MSG_INFOS.on then
print(string.rep("-", 25)
.." end test routine "
..string.rep("-", 55))
__TEST_MSG_INFOS.on = false
end
end
end -- end if false
local function __get_msg()
return __sprint_test_data()
end
-- THE FOLLOWING ASSERTATION FUNCTIONS REQUIRE luaunit-patched.lua !!
-- like lu.assert_equals() but prints an auto-generated informative message
-- about the test data, of the test case when the test case failed.
-- function assert_equals(res, exp)
-- lu.assert_equals(res, exp, __get_msg)
-- end
--
-- function assert_true(bool)
-- lu.assert_true(bool, __get_msg)
-- end
assert_equals = lu.assert_equals
assert_true = lu.assert_true