Lutok  0.2
 All Classes Namespaces Files Functions Variables Defines
test_utils.hpp
Go to the documentation of this file.
00001 // Copyright 2011 Google Inc.
00002 // All rights reserved.
00003 //
00004 // Redistribution and use in source and binary forms, with or without
00005 // modification, are permitted provided that the following conditions are
00006 // met:
00007 //
00008 // * Redistributions of source code must retain the above copyright
00009 //   notice, this list of conditions and the following disclaimer.
00010 // * Redistributions in binary form must reproduce the above copyright
00011 //   notice, this list of conditions and the following disclaimer in the
00012 //   documentation and/or other materials provided with the distribution.
00013 // * Neither the name of Google Inc. nor the names of its contributors
00014 //   may be used to endorse or promote products derived from this software
00015 //   without specific prior written permission.
00016 //
00017 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00018 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00019 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00020 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00021 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00022 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00023 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00024 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00025 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00026 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00027 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028 
00035 
00036 #if !defined(LUTOK_TEST_UTILS_HPP)
00037 #   define LUTOK_TEST_UTILS_HPP
00038 #else
00039 #   error "test_utils.hpp can only be included once"
00040 #endif
00041 
00042 #include <atf-c++.hpp>
00043 
00044 #include "c_gate.hpp"
00045 #include "exceptions.hpp"
00046 #include "state.hpp"
00047 
00048 
00049 namespace {
00050 
00051 
00061 #define REQUIRE_API_ERROR(exp_api_function, statement) \
00062     do { \
00063         try { \
00064             statement; \
00065             ATF_FAIL("api_error not raised by " #statement); \
00066         } catch (const lutok::api_error& api_error) { \
00067             ATF_REQUIRE_EQ(exp_api_function, api_error.api_function()); \
00068         } \
00069     } while (0)
00070 
00071 
00079 static inline lua_State*
00080 raw(lutok::state& state)
00081 {
00082     return lutok::state_c_gate(state).c_state();
00083 }
00084 
00085 
00092 class stack_balance_checker {
00094     lutok::state& _state;
00095 
00097     bool _with_sentinel;
00098 
00100     unsigned int _old_count;
00101 
00102 public:
00110     stack_balance_checker(lutok::state& state_,
00111                           const bool with_sentinel_ = true) :
00112         _state(state_),
00113         _with_sentinel(with_sentinel_),
00114         _old_count(_state.get_top())
00115     {
00116         if (_with_sentinel)
00117             _state.push_integer(987654321);
00118     }
00119 
00124     ~stack_balance_checker(void)
00125     {
00126         if (_with_sentinel) {
00127             if (!_state.is_number() || _state.to_integer() != 987654321)
00128                 ATF_FAIL("Stack corrupted: sentinel not found");
00129             _state.pop(1);
00130         }
00131 
00132         unsigned int new_count = _state.get_top();
00133         if (_old_count != new_count)
00134             //ATF_FAIL(F("Stack not balanced: before %d, after %d") %
00135             //         _old_count % new_count);
00136             ATF_FAIL("Stack not balanced");
00137     }
00138 };
00139 
00140 
00141 }  // anonymous namespace