-
Notifications
You must be signed in to change notification settings - Fork 18
/
operations_test.cpp
372 lines (308 loc) · 10.9 KB
/
operations_test.cpp
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
// Copyright 2011 Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of Google Inc. nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "operations.hpp"
#include <fstream>
#include <atf-c++.hpp>
#include "exceptions.hpp"
#include "state.ipp"
#include "test_utils.hpp"
namespace {
/// Addition function for injection into Lua.
///
/// \pre stack(-2) The first summand.
/// \pre stack(-1) The second summand.
/// \post stack(-1) The result of the sum.
///
/// \param state The Lua state.
///
/// \return The number of results (1).
static int
hook_add(lutok::state& state)
{
state.push_integer(state.to_integer(-1) + state.to_integer(-2));
return 1;
}
/// Multiplication function for injection into Lua.
///
/// \pre stack(-2) The first factor.
/// \pre stack(-1) The second factor.
/// \post stack(-1) The product.
///
/// \param state The Lua state.
///
/// \return The number of results (1).
static int
hook_multiply(lutok::state& state)
{
state.push_integer(state.to_integer(-1) * state.to_integer(-2));
return 1;
}
} // anonymous namespace
ATF_TEST_CASE_WITHOUT_HEAD(create_module__empty);
ATF_TEST_CASE_BODY(create_module__empty)
{
lutok::state state;
std::map< std::string, lutok::cxx_function > members;
lutok::create_module(state, "my_math", members);
state.open_base();
lutok::do_string(state, "return next(my_math) == nil", 0, 1, 0);
ATF_REQUIRE(state.to_boolean(-1));
state.pop(1);
}
ATF_TEST_CASE_WITHOUT_HEAD(create_module__one);
ATF_TEST_CASE_BODY(create_module__one)
{
lutok::state state;
std::map< std::string, lutok::cxx_function > members;
members["add"] = hook_add;
lutok::create_module(state, "my_math", members);
lutok::do_string(state, "return my_math.add(10, 20)", 0, 1, 0);
ATF_REQUIRE_EQ(30, state.to_integer(-1));
state.pop(1);
}
ATF_TEST_CASE_WITHOUT_HEAD(create_module__many);
ATF_TEST_CASE_BODY(create_module__many)
{
lutok::state state;
std::map< std::string, lutok::cxx_function > members;
members["add"] = hook_add;
members["multiply"] = hook_multiply;
members["add2"] = hook_add;
lutok::create_module(state, "my_math", members);
lutok::do_string(state, "return my_math.add(10, 20)", 0, 1, 0);
ATF_REQUIRE_EQ(30, state.to_integer(-1));
lutok::do_string(state, "return my_math.multiply(10, 20)", 0, 1, 0);
ATF_REQUIRE_EQ(200, state.to_integer(-1));
lutok::do_string(state, "return my_math.add2(20, 30)", 0, 1, 0);
ATF_REQUIRE_EQ(50, state.to_integer(-1));
state.pop(3);
}
ATF_TEST_CASE_WITHOUT_HEAD(do_file__some_args);
ATF_TEST_CASE_BODY(do_file__some_args)
{
std::ofstream output("test.lua");
output << "local a1, a2 = ...\nreturn a1 * 2, a2 * 2\n";
output.close();
lutok::state state;
state.push_integer(456);
state.push_integer(3);
state.push_integer(5);
state.push_integer(123);
ATF_REQUIRE_EQ(2, lutok::do_file(state, "test.lua", 3, -1, 0));
ATF_REQUIRE_EQ(3, state.get_top());
ATF_REQUIRE_EQ(456, state.to_integer(-3));
ATF_REQUIRE_EQ(6, state.to_integer(-2));
ATF_REQUIRE_EQ(10, state.to_integer(-1));
state.pop(3);
}
ATF_TEST_CASE_WITHOUT_HEAD(do_file__any_results);
ATF_TEST_CASE_BODY(do_file__any_results)
{
std::ofstream output("test.lua");
output << "return 10, 20, 30\n";
output.close();
lutok::state state;
ATF_REQUIRE_EQ(3, lutok::do_file(state, "test.lua", 0, -1, 0));
ATF_REQUIRE_EQ(3, state.get_top());
ATF_REQUIRE_EQ(10, state.to_integer(-3));
ATF_REQUIRE_EQ(20, state.to_integer(-2));
ATF_REQUIRE_EQ(30, state.to_integer(-1));
state.pop(3);
}
ATF_TEST_CASE_WITHOUT_HEAD(do_file__no_results);
ATF_TEST_CASE_BODY(do_file__no_results)
{
std::ofstream output("test.lua");
output << "return 10, 20, 30\n";
output.close();
lutok::state state;
ATF_REQUIRE_EQ(0, lutok::do_file(state, "test.lua", 0, 0, 0));
ATF_REQUIRE_EQ(0, state.get_top());
}
ATF_TEST_CASE_WITHOUT_HEAD(do_file__many_results);
ATF_TEST_CASE_BODY(do_file__many_results)
{
std::ofstream output("test.lua");
output << "return 10, 20, 30\n";
output.close();
lutok::state state;
ATF_REQUIRE_EQ(2, lutok::do_file(state, "test.lua", 0, 2, 0));
ATF_REQUIRE_EQ(2, state.get_top());
ATF_REQUIRE_EQ(10, state.to_integer(-2));
ATF_REQUIRE_EQ(20, state.to_integer(-1));
state.pop(2);
}
ATF_TEST_CASE_WITHOUT_HEAD(do_file__not_found);
ATF_TEST_CASE_BODY(do_file__not_found)
{
lutok::state state;
stack_balance_checker checker(state);
ATF_REQUIRE_THROW_RE(lutok::file_not_found_error, "missing.lua",
lutok::do_file(state, "missing.lua", 0, 0, 0));
}
ATF_TEST_CASE_WITHOUT_HEAD(do_file__error);
ATF_TEST_CASE_BODY(do_file__error)
{
std::ofstream output("test.lua");
output << "a b c\n";
output.close();
lutok::state state;
stack_balance_checker checker(state);
ATF_REQUIRE_THROW_RE(lutok::error, "Failed to load Lua file 'test.lua'",
lutok::do_file(state, "test.lua", 0, 0, 0));
}
ATF_TEST_CASE_WITHOUT_HEAD(do_file__error_with_errfunc);
ATF_TEST_CASE_BODY(do_file__error_with_errfunc)
{
std::ofstream output("test.lua");
output << "unknown_function()\n";
output.close();
lutok::state state;
lutok::eval(state, "function(message) return 'This is an error!' end", 1);
{
stack_balance_checker checker(state);
ATF_REQUIRE_THROW_RE(lutok::error, "This is an error!",
lutok::do_file(state, "test.lua", 0, 0, -2));
}
state.pop(1);
}
ATF_TEST_CASE_WITHOUT_HEAD(do_string__some_args);
ATF_TEST_CASE_BODY(do_string__some_args)
{
lutok::state state;
state.push_integer(456);
state.push_integer(3);
state.push_integer(5);
state.push_integer(123);
ATF_REQUIRE_EQ(2, lutok::do_string(
state, "local a1, a2 = ...\nreturn a1 * 2, a2 * 2\n", 3, -1, 0));
ATF_REQUIRE_EQ(3, state.get_top());
ATF_REQUIRE_EQ(456, state.to_integer(-3));
ATF_REQUIRE_EQ(6, state.to_integer(-2));
ATF_REQUIRE_EQ(10, state.to_integer(-1));
state.pop(3);
}
ATF_TEST_CASE_WITHOUT_HEAD(do_string__any_results);
ATF_TEST_CASE_BODY(do_string__any_results)
{
lutok::state state;
ATF_REQUIRE_EQ(3, lutok::do_string(state, "return 10, 20, 30", 0, -1, 0));
ATF_REQUIRE_EQ(3, state.get_top());
ATF_REQUIRE_EQ(10, state.to_integer(-3));
ATF_REQUIRE_EQ(20, state.to_integer(-2));
ATF_REQUIRE_EQ(30, state.to_integer(-1));
state.pop(3);
}
ATF_TEST_CASE_WITHOUT_HEAD(do_string__no_results);
ATF_TEST_CASE_BODY(do_string__no_results)
{
lutok::state state;
ATF_REQUIRE_EQ(0, lutok::do_string(state, "return 10, 20, 30", 0, 0, 0));
ATF_REQUIRE_EQ(0, state.get_top());
}
ATF_TEST_CASE_WITHOUT_HEAD(do_string__many_results);
ATF_TEST_CASE_BODY(do_string__many_results)
{
lutok::state state;
ATF_REQUIRE_EQ(2, lutok::do_string(state, "return 10, 20, 30", 0, 2, 0));
ATF_REQUIRE_EQ(2, state.get_top());
ATF_REQUIRE_EQ(10, state.to_integer(-2));
ATF_REQUIRE_EQ(20, state.to_integer(-1));
state.pop(2);
}
ATF_TEST_CASE_WITHOUT_HEAD(do_string__error);
ATF_TEST_CASE_BODY(do_string__error)
{
lutok::state state;
stack_balance_checker checker(state);
ATF_REQUIRE_THROW_RE(lutok::error, "Failed to process Lua string 'a b c'",
lutok::do_string(state, "a b c", 0, 0, 0));
}
ATF_TEST_CASE_WITHOUT_HEAD(do_string__error_with_errfunc);
ATF_TEST_CASE_BODY(do_string__error_with_errfunc)
{
lutok::state state;
lutok::eval(state, "function(message) return 'This is an error!' end", 1);
{
stack_balance_checker checker(state);
ATF_REQUIRE_THROW_RE(lutok::error, "This is an error!",
lutok::do_string(state, "unknown_function()",
0, 0, -2));
}
state.pop(1);
}
ATF_TEST_CASE_WITHOUT_HEAD(eval__one_result);
ATF_TEST_CASE_BODY(eval__one_result)
{
lutok::state state;
stack_balance_checker checker(state);
lutok::eval(state, "3 + 10", 1);
ATF_REQUIRE_EQ(13, state.to_integer(-1));
state.pop(1);
}
ATF_TEST_CASE_WITHOUT_HEAD(eval__many_results);
ATF_TEST_CASE_BODY(eval__many_results)
{
lutok::state state;
stack_balance_checker checker(state);
lutok::eval(state, "5, 8, 10", 3);
ATF_REQUIRE_EQ(5, state.to_integer(-3));
ATF_REQUIRE_EQ(8, state.to_integer(-2));
ATF_REQUIRE_EQ(10, state.to_integer(-1));
state.pop(3);
}
ATF_TEST_CASE_WITHOUT_HEAD(eval__error);
ATF_TEST_CASE_BODY(eval__error)
{
lutok::state state;
stack_balance_checker checker(state);
ATF_REQUIRE_THROW(lutok::error,
lutok::eval(state, "non_existent.method()", 1));
}
ATF_INIT_TEST_CASES(tcs)
{
ATF_ADD_TEST_CASE(tcs, create_module__empty);
ATF_ADD_TEST_CASE(tcs, create_module__one);
ATF_ADD_TEST_CASE(tcs, create_module__many);
ATF_ADD_TEST_CASE(tcs, do_file__some_args);
ATF_ADD_TEST_CASE(tcs, do_file__any_results);
ATF_ADD_TEST_CASE(tcs, do_file__no_results);
ATF_ADD_TEST_CASE(tcs, do_file__many_results);
ATF_ADD_TEST_CASE(tcs, do_file__not_found);
ATF_ADD_TEST_CASE(tcs, do_file__error);
ATF_ADD_TEST_CASE(tcs, do_file__error_with_errfunc);
ATF_ADD_TEST_CASE(tcs, do_string__some_args);
ATF_ADD_TEST_CASE(tcs, do_string__any_results);
ATF_ADD_TEST_CASE(tcs, do_string__no_results);
ATF_ADD_TEST_CASE(tcs, do_string__many_results);
ATF_ADD_TEST_CASE(tcs, do_string__error);
ATF_ADD_TEST_CASE(tcs, do_string__error_with_errfunc);
ATF_ADD_TEST_CASE(tcs, eval__one_result);
ATF_ADD_TEST_CASE(tcs, eval__many_results);
ATF_ADD_TEST_CASE(tcs, eval__error);
}