xref: /minix3/external/bsd/lutok/dist/operations_test.cpp (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc // Copyright 2011 Google Inc.
2*11be35a1SLionel Sambuc // All rights reserved.
3*11be35a1SLionel Sambuc //
4*11be35a1SLionel Sambuc // Redistribution and use in source and binary forms, with or without
5*11be35a1SLionel Sambuc // modification, are permitted provided that the following conditions are
6*11be35a1SLionel Sambuc // met:
7*11be35a1SLionel Sambuc //
8*11be35a1SLionel Sambuc // * Redistributions of source code must retain the above copyright
9*11be35a1SLionel Sambuc //   notice, this list of conditions and the following disclaimer.
10*11be35a1SLionel Sambuc // * Redistributions in binary form must reproduce the above copyright
11*11be35a1SLionel Sambuc //   notice, this list of conditions and the following disclaimer in the
12*11be35a1SLionel Sambuc //   documentation and/or other materials provided with the distribution.
13*11be35a1SLionel Sambuc // * Neither the name of Google Inc. nor the names of its contributors
14*11be35a1SLionel Sambuc //   may be used to endorse or promote products derived from this software
15*11be35a1SLionel Sambuc //   without specific prior written permission.
16*11be35a1SLionel Sambuc //
17*11be35a1SLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*11be35a1SLionel Sambuc // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*11be35a1SLionel Sambuc // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*11be35a1SLionel Sambuc // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*11be35a1SLionel Sambuc // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*11be35a1SLionel Sambuc // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*11be35a1SLionel Sambuc // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*11be35a1SLionel Sambuc // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*11be35a1SLionel Sambuc // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*11be35a1SLionel Sambuc // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*11be35a1SLionel Sambuc // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*11be35a1SLionel Sambuc 
29*11be35a1SLionel Sambuc #include "operations.hpp"
30*11be35a1SLionel Sambuc 
31*11be35a1SLionel Sambuc #include <fstream>
32*11be35a1SLionel Sambuc 
33*11be35a1SLionel Sambuc #include <atf-c++.hpp>
34*11be35a1SLionel Sambuc 
35*11be35a1SLionel Sambuc #include "exceptions.hpp"
36*11be35a1SLionel Sambuc #include "state.ipp"
37*11be35a1SLionel Sambuc #include "test_utils.hpp"
38*11be35a1SLionel Sambuc 
39*11be35a1SLionel Sambuc 
40*11be35a1SLionel Sambuc namespace {
41*11be35a1SLionel Sambuc 
42*11be35a1SLionel Sambuc 
43*11be35a1SLionel Sambuc /// Addition function for injection into Lua.
44*11be35a1SLionel Sambuc ///
45*11be35a1SLionel Sambuc /// \pre stack(-2) The first summand.
46*11be35a1SLionel Sambuc /// \pre stack(-1) The second summand.
47*11be35a1SLionel Sambuc /// \post stack(-1) The result of the sum.
48*11be35a1SLionel Sambuc ///
49*11be35a1SLionel Sambuc /// \param state The Lua state.
50*11be35a1SLionel Sambuc ///
51*11be35a1SLionel Sambuc /// \return The number of results (1).
52*11be35a1SLionel Sambuc static int
hook_add(lutok::state & state)53*11be35a1SLionel Sambuc hook_add(lutok::state& state)
54*11be35a1SLionel Sambuc {
55*11be35a1SLionel Sambuc     state.push_integer(state.to_integer(-1) + state.to_integer(-2));
56*11be35a1SLionel Sambuc     return 1;
57*11be35a1SLionel Sambuc }
58*11be35a1SLionel Sambuc 
59*11be35a1SLionel Sambuc 
60*11be35a1SLionel Sambuc /// Multiplication function for injection into Lua.
61*11be35a1SLionel Sambuc ///
62*11be35a1SLionel Sambuc /// \pre stack(-2) The first factor.
63*11be35a1SLionel Sambuc /// \pre stack(-1) The second factor.
64*11be35a1SLionel Sambuc /// \post stack(-1) The product.
65*11be35a1SLionel Sambuc ///
66*11be35a1SLionel Sambuc /// \param state The Lua state.
67*11be35a1SLionel Sambuc ///
68*11be35a1SLionel Sambuc /// \return The number of results (1).
69*11be35a1SLionel Sambuc static int
hook_multiply(lutok::state & state)70*11be35a1SLionel Sambuc hook_multiply(lutok::state& state)
71*11be35a1SLionel Sambuc {
72*11be35a1SLionel Sambuc     state.push_integer(state.to_integer(-1) * state.to_integer(-2));
73*11be35a1SLionel Sambuc     return 1;
74*11be35a1SLionel Sambuc }
75*11be35a1SLionel Sambuc 
76*11be35a1SLionel Sambuc 
77*11be35a1SLionel Sambuc }  // anonymous namespace
78*11be35a1SLionel Sambuc 
79*11be35a1SLionel Sambuc 
80*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(create_module__empty);
ATF_TEST_CASE_BODY(create_module__empty)81*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(create_module__empty)
82*11be35a1SLionel Sambuc {
83*11be35a1SLionel Sambuc     lutok::state state;
84*11be35a1SLionel Sambuc     std::map< std::string, lutok::cxx_function > members;
85*11be35a1SLionel Sambuc     lutok::create_module(state, "my_math", members);
86*11be35a1SLionel Sambuc 
87*11be35a1SLionel Sambuc     state.open_base();
88*11be35a1SLionel Sambuc     lutok::do_string(state, "return next(my_math) == nil", 1);
89*11be35a1SLionel Sambuc     ATF_REQUIRE(state.to_boolean());
90*11be35a1SLionel Sambuc     state.pop(1);
91*11be35a1SLionel Sambuc }
92*11be35a1SLionel Sambuc 
93*11be35a1SLionel Sambuc 
94*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(create_module__one);
ATF_TEST_CASE_BODY(create_module__one)95*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(create_module__one)
96*11be35a1SLionel Sambuc {
97*11be35a1SLionel Sambuc     lutok::state state;
98*11be35a1SLionel Sambuc     std::map< std::string, lutok::cxx_function > members;
99*11be35a1SLionel Sambuc     members["add"] = hook_add;
100*11be35a1SLionel Sambuc     lutok::create_module(state, "my_math", members);
101*11be35a1SLionel Sambuc 
102*11be35a1SLionel Sambuc     lutok::do_string(state, "return my_math.add(10, 20)", 1);
103*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(30, state.to_integer());
104*11be35a1SLionel Sambuc     state.pop(1);
105*11be35a1SLionel Sambuc }
106*11be35a1SLionel Sambuc 
107*11be35a1SLionel Sambuc 
108*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(create_module__many);
ATF_TEST_CASE_BODY(create_module__many)109*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(create_module__many)
110*11be35a1SLionel Sambuc {
111*11be35a1SLionel Sambuc     lutok::state state;
112*11be35a1SLionel Sambuc     std::map< std::string, lutok::cxx_function > members;
113*11be35a1SLionel Sambuc     members["add"] = hook_add;
114*11be35a1SLionel Sambuc     members["multiply"] = hook_multiply;
115*11be35a1SLionel Sambuc     members["add2"] = hook_add;
116*11be35a1SLionel Sambuc     lutok::create_module(state, "my_math", members);
117*11be35a1SLionel Sambuc 
118*11be35a1SLionel Sambuc     lutok::do_string(state, "return my_math.add(10, 20)", 1);
119*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(30, state.to_integer());
120*11be35a1SLionel Sambuc     lutok::do_string(state, "return my_math.multiply(10, 20)", 1);
121*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(200, state.to_integer());
122*11be35a1SLionel Sambuc     lutok::do_string(state, "return my_math.add2(20, 30)", 1);
123*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(50, state.to_integer());
124*11be35a1SLionel Sambuc     state.pop(3);
125*11be35a1SLionel Sambuc }
126*11be35a1SLionel Sambuc 
127*11be35a1SLionel Sambuc 
128*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(do_file__any_results);
ATF_TEST_CASE_BODY(do_file__any_results)129*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(do_file__any_results)
130*11be35a1SLionel Sambuc {
131*11be35a1SLionel Sambuc     std::ofstream output("test.lua");
132*11be35a1SLionel Sambuc     output << "return 10, 20, 30\n";
133*11be35a1SLionel Sambuc     output.close();
134*11be35a1SLionel Sambuc 
135*11be35a1SLionel Sambuc     lutok::state state;
136*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(3, lutok::do_file(state, "test.lua", -1));
137*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(3, state.get_top());
138*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(10, state.to_integer(-3));
139*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(20, state.to_integer(-2));
140*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(30, state.to_integer(-1));
141*11be35a1SLionel Sambuc     state.pop(3);
142*11be35a1SLionel Sambuc }
143*11be35a1SLionel Sambuc 
144*11be35a1SLionel Sambuc 
145*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(do_file__no_results);
ATF_TEST_CASE_BODY(do_file__no_results)146*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(do_file__no_results)
147*11be35a1SLionel Sambuc {
148*11be35a1SLionel Sambuc     std::ofstream output("test.lua");
149*11be35a1SLionel Sambuc     output << "return 10, 20, 30\n";
150*11be35a1SLionel Sambuc     output.close();
151*11be35a1SLionel Sambuc 
152*11be35a1SLionel Sambuc     lutok::state state;
153*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(0, lutok::do_file(state, "test.lua"));
154*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(0, state.get_top());
155*11be35a1SLionel Sambuc }
156*11be35a1SLionel Sambuc 
157*11be35a1SLionel Sambuc 
158*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(do_file__many_results);
ATF_TEST_CASE_BODY(do_file__many_results)159*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(do_file__many_results)
160*11be35a1SLionel Sambuc {
161*11be35a1SLionel Sambuc     std::ofstream output("test.lua");
162*11be35a1SLionel Sambuc     output << "return 10, 20, 30\n";
163*11be35a1SLionel Sambuc     output.close();
164*11be35a1SLionel Sambuc 
165*11be35a1SLionel Sambuc     lutok::state state;
166*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(2, lutok::do_file(state, "test.lua", 2));
167*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(2, state.get_top());
168*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(10, state.to_integer(-2));
169*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(20, state.to_integer(-1));
170*11be35a1SLionel Sambuc     state.pop(2);
171*11be35a1SLionel Sambuc }
172*11be35a1SLionel Sambuc 
173*11be35a1SLionel Sambuc 
174*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(do_file__not_found);
ATF_TEST_CASE_BODY(do_file__not_found)175*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(do_file__not_found)
176*11be35a1SLionel Sambuc {
177*11be35a1SLionel Sambuc     lutok::state state;
178*11be35a1SLionel Sambuc     stack_balance_checker checker(state);
179*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW_RE(lutok::file_not_found_error, "missing.lua",
180*11be35a1SLionel Sambuc                          lutok::do_file(state, "missing.lua"));
181*11be35a1SLionel Sambuc }
182*11be35a1SLionel Sambuc 
183*11be35a1SLionel Sambuc 
184*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(do_file__error);
ATF_TEST_CASE_BODY(do_file__error)185*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(do_file__error)
186*11be35a1SLionel Sambuc {
187*11be35a1SLionel Sambuc     std::ofstream output("test.lua");
188*11be35a1SLionel Sambuc     output << "a b c\n";
189*11be35a1SLionel Sambuc     output.close();
190*11be35a1SLionel Sambuc 
191*11be35a1SLionel Sambuc     lutok::state state;
192*11be35a1SLionel Sambuc     stack_balance_checker checker(state);
193*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW_RE(lutok::error, "Failed to load Lua file 'test.lua'",
194*11be35a1SLionel Sambuc                          lutok::do_file(state, "test.lua"));
195*11be35a1SLionel Sambuc }
196*11be35a1SLionel Sambuc 
197*11be35a1SLionel Sambuc 
198*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(do_string__any_results);
ATF_TEST_CASE_BODY(do_string__any_results)199*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(do_string__any_results)
200*11be35a1SLionel Sambuc {
201*11be35a1SLionel Sambuc     lutok::state state;
202*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(3, lutok::do_string(state, "return 10, 20, 30", -1));
203*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(3, state.get_top());
204*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(10, state.to_integer(-3));
205*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(20, state.to_integer(-2));
206*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(30, state.to_integer(-1));
207*11be35a1SLionel Sambuc     state.pop(3);
208*11be35a1SLionel Sambuc }
209*11be35a1SLionel Sambuc 
210*11be35a1SLionel Sambuc 
211*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(do_string__no_results);
ATF_TEST_CASE_BODY(do_string__no_results)212*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(do_string__no_results)
213*11be35a1SLionel Sambuc {
214*11be35a1SLionel Sambuc     lutok::state state;
215*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(0, lutok::do_string(state, "return 10, 20, 30"));
216*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(0, state.get_top());
217*11be35a1SLionel Sambuc }
218*11be35a1SLionel Sambuc 
219*11be35a1SLionel Sambuc 
220*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(do_string__many_results);
ATF_TEST_CASE_BODY(do_string__many_results)221*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(do_string__many_results)
222*11be35a1SLionel Sambuc {
223*11be35a1SLionel Sambuc     lutok::state state;
224*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(2, lutok::do_string(state, "return 10, 20, 30", 2));
225*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(2, state.get_top());
226*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(10, state.to_integer(-2));
227*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(20, state.to_integer(-1));
228*11be35a1SLionel Sambuc     state.pop(2);
229*11be35a1SLionel Sambuc }
230*11be35a1SLionel Sambuc 
231*11be35a1SLionel Sambuc 
232*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(do_string__error);
ATF_TEST_CASE_BODY(do_string__error)233*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(do_string__error)
234*11be35a1SLionel Sambuc {
235*11be35a1SLionel Sambuc     lutok::state state;
236*11be35a1SLionel Sambuc     stack_balance_checker checker(state);
237*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW_RE(lutok::error, "Failed to process Lua string 'a b c'",
238*11be35a1SLionel Sambuc                          lutok::do_string(state, "a b c"));
239*11be35a1SLionel Sambuc }
240*11be35a1SLionel Sambuc 
241*11be35a1SLionel Sambuc 
242*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(eval__one_result);
ATF_TEST_CASE_BODY(eval__one_result)243*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(eval__one_result)
244*11be35a1SLionel Sambuc {
245*11be35a1SLionel Sambuc     lutok::state state;
246*11be35a1SLionel Sambuc     stack_balance_checker checker(state);
247*11be35a1SLionel Sambuc     lutok::eval(state, "3 + 10");
248*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(13, state.to_integer());
249*11be35a1SLionel Sambuc     state.pop(1);
250*11be35a1SLionel Sambuc }
251*11be35a1SLionel Sambuc 
252*11be35a1SLionel Sambuc 
253*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(eval__many_results);
ATF_TEST_CASE_BODY(eval__many_results)254*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(eval__many_results)
255*11be35a1SLionel Sambuc {
256*11be35a1SLionel Sambuc     lutok::state state;
257*11be35a1SLionel Sambuc     stack_balance_checker checker(state);
258*11be35a1SLionel Sambuc     lutok::eval(state, "5, 8, 10", 3);
259*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(5, state.to_integer(-3));
260*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(8, state.to_integer(-2));
261*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(10, state.to_integer(-1));
262*11be35a1SLionel Sambuc     state.pop(3);
263*11be35a1SLionel Sambuc }
264*11be35a1SLionel Sambuc 
265*11be35a1SLionel Sambuc 
266*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(eval__error);
ATF_TEST_CASE_BODY(eval__error)267*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(eval__error)
268*11be35a1SLionel Sambuc {
269*11be35a1SLionel Sambuc     lutok::state state;
270*11be35a1SLionel Sambuc     stack_balance_checker checker(state);
271*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW(lutok::error,
272*11be35a1SLionel Sambuc                       lutok::eval(state, "non_existent.method()"));
273*11be35a1SLionel Sambuc }
274*11be35a1SLionel Sambuc 
275*11be35a1SLionel Sambuc 
ATF_INIT_TEST_CASES(tcs)276*11be35a1SLionel Sambuc ATF_INIT_TEST_CASES(tcs)
277*11be35a1SLionel Sambuc {
278*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, create_module__empty);
279*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, create_module__one);
280*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, create_module__many);
281*11be35a1SLionel Sambuc 
282*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, do_file__any_results);
283*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, do_file__no_results);
284*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, do_file__many_results);
285*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, do_file__not_found);
286*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, do_file__error);
287*11be35a1SLionel Sambuc 
288*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, do_string__any_results);
289*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, do_string__no_results);
290*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, do_string__many_results);
291*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, do_string__error);
292*11be35a1SLionel Sambuc 
293*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, eval__one_result);
294*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, eval__many_results);
295*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, eval__error);
296*11be35a1SLionel Sambuc }
297