Lutok: test_utils.hpp Source File

Lutok 0.4

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Macros
test_utils.hpp
Go to the documentation of this file.
1 // Copyright 2011 Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
35
36 #if !defined(LUTOK_TEST_UTILS_HPP)
37 # define LUTOK_TEST_UTILS_HPP
38 #else
39 # error "test_utils.hpp can only be included once"
40 #endif
41
42 #include <atf-c++.hpp>
43
44 #include "c_gate.hpp"
45 #include "exceptions.hpp"
46 #include "state.hpp"
47
48
49 namespace {
50
51
61 #define REQUIRE_API_ERROR(exp_api_function, statement) \
62 do { \
63 try { \
64 statement; \
65 ATF_FAIL("api_error not raised by " #statement); \
66 } catch (const lutok::api_error& api_error) { \
67 ATF_REQUIRE_EQ(exp_api_function, api_error.api_function()); \
68 } \
69 } while (0)
70
71
79 static inline lua_State*
80 raw(lutok::state& state)
81 {
82 return lutok::state_c_gate(state).c_state();
83 }
84
85
92 class stack_balance_checker {
94 lutok::state& _state;
95
97 bool _with_sentinel;
98
100 unsigned int _old_count;
101
102 public:
110 stack_balance_checker(lutok::state& state_,
111 const bool with_sentinel_ = true) :
112 _state(state_),
113 _with_sentinel(with_sentinel_),
114 _old_count(_state.get_top())
115 {
116 if (_with_sentinel)
117 _state.push_integer(987654321);
118 }
119
124 ~stack_balance_checker(void)
125 {
126 if (_with_sentinel) {
127 if (!_state.is_number(-1) || _state.to_integer(-1) != 987654321)
128 ATF_FAIL("Stack corrupted: sentinel not found");
129 _state.pop(1);
130 }
131
132 unsigned int new_count = _state.get_top();
133 if (_old_count != new_count)
134 //ATF_FAIL(F("Stack not balanced: before %d, after %d") %
135 // _old_count % new_count);
136 ATF_FAIL("Stack not balanced");
137 }
138 };
139
140
141 } // anonymous namespace