Import backtrace-cpp and integrate it into our main executable
Upstream release https://github.com/bombela/backward-cpp/archive/v1.3.tar.gz
This commit is contained in:
parent
5a1728c8d4
commit
e6a0821cfc
25 changed files with 3606 additions and 1 deletions
121
external/backtrace-cpp/test/_test_main.cpp
vendored
Normal file
121
external/backtrace-cpp/test/_test_main.cpp
vendored
Normal file
|
|
@ -0,0 +1,121 @@
|
|||
/*
|
||||
* _test_main.cpp
|
||||
* Copyright 2013 Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "test.hpp"
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <error.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
test::test_registry_t test::test_registry;
|
||||
using namespace test;
|
||||
|
||||
bool run_test(TestBase& test) {
|
||||
printf("-- running test case: %s\n", test.name);
|
||||
|
||||
fflush(stdout);
|
||||
pid_t child_pid = fork();
|
||||
if (child_pid == 0) {
|
||||
exit(static_cast<int>(test.run()));
|
||||
}
|
||||
if (child_pid == -1) {
|
||||
error(EXIT_FAILURE, 0, "unable to fork");
|
||||
}
|
||||
|
||||
int child_status = 0;
|
||||
waitpid(child_pid, &child_status, 0);
|
||||
|
||||
test::TestStatus status;
|
||||
|
||||
if (WIFEXITED(child_status)) {
|
||||
int exit_status = WEXITSTATUS(child_status);
|
||||
if (exit_status & ~test::STATUS_MASK) {
|
||||
status = test::FAILED;
|
||||
} else {
|
||||
status = static_cast<test::TestStatus>(exit_status);
|
||||
}
|
||||
} else if (WIFSIGNALED(child_status)) {
|
||||
const int signum = WTERMSIG(child_status);
|
||||
printf("!! signal (%d) %s\n", signum, strsignal(signum));
|
||||
switch (signum) {
|
||||
case SIGABRT:
|
||||
status = test::SIGNAL_ABORT; break;
|
||||
case SIGSEGV:
|
||||
case SIGBUS:
|
||||
status = test::SIGNAL_SEGFAULT; break;
|
||||
case SIGFPE:
|
||||
status = test::SIGNAL_DIVZERO; break;
|
||||
default:
|
||||
status = test::SIGNAL_UNCAUGHT;
|
||||
}
|
||||
} else {
|
||||
status = test::SUCCESS;
|
||||
}
|
||||
|
||||
if (test.expected_status == test::FAILED) {
|
||||
return (status & test::FAILED);
|
||||
}
|
||||
|
||||
if (test.expected_status == test::SIGNAL_UNCAUGHT) {
|
||||
return (status & test::SIGNAL_UNCAUGHT);
|
||||
}
|
||||
|
||||
return status == test.expected_status;
|
||||
}
|
||||
|
||||
int main(int argc, const char* const argv[]) {
|
||||
|
||||
size_t success_cnt = 0;
|
||||
size_t total_cnt = 0;
|
||||
for (test_registry_t::iterator it = test_registry.begin();
|
||||
it != test_registry.end(); ++it) {
|
||||
TestBase& test = **it;
|
||||
|
||||
bool consider_test = (argc <= 1);
|
||||
for (int i = 1; i < argc; ++i) {
|
||||
if (strcasecmp(argv[i], test.name) == 0) {
|
||||
consider_test = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (not consider_test) {
|
||||
continue;
|
||||
}
|
||||
|
||||
total_cnt += 1;
|
||||
if (run_test(test)) {
|
||||
printf("-- test case success: %s\n", test.name);
|
||||
success_cnt += 1;
|
||||
} else {
|
||||
printf("** test case FAILED : %s\n", test.name);
|
||||
}
|
||||
}
|
||||
printf("-- tests passing: %lu/%lu", success_cnt, total_cnt);
|
||||
if (total_cnt) {
|
||||
printf(" (%lu%%)\n", success_cnt * 100 / total_cnt);
|
||||
} else {
|
||||
printf("\n");
|
||||
}
|
||||
return (success_cnt == total_cnt) ? EXIT_SUCCESS : EXIT_FAILURE;
|
||||
}
|
||||
99
external/backtrace-cpp/test/rectrace.cpp
vendored
Normal file
99
external/backtrace-cpp/test/rectrace.cpp
vendored
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
/*
|
||||
* test/rectrace.cpp
|
||||
* Copyright 2013 Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "backward.hpp"
|
||||
#include <stdio.h>
|
||||
#include "test/test.hpp"
|
||||
|
||||
using namespace backward;
|
||||
|
||||
typedef StackTrace stacktrace_t;
|
||||
|
||||
void end_of_our_journey(stacktrace_t& st) {
|
||||
if (not st.size()) {
|
||||
st.load_here();
|
||||
}
|
||||
}
|
||||
|
||||
int rec(stacktrace_t& st, int level) {
|
||||
if (level <= 1) {
|
||||
end_of_our_journey(st);
|
||||
return 0;
|
||||
}
|
||||
return rec(st, level - 1);
|
||||
}
|
||||
|
||||
namespace toto {
|
||||
|
||||
namespace titi {
|
||||
|
||||
struct foo {
|
||||
|
||||
union bar {
|
||||
__attribute__((noinline))
|
||||
static int trampoline(stacktrace_t& st, int level) {
|
||||
return rec(st, level);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
} // namespace titi
|
||||
|
||||
} // namespace toto
|
||||
|
||||
TEST (recursion) {
|
||||
{ // lexical scope.
|
||||
stacktrace_t st;
|
||||
const int input = 3;
|
||||
int r = toto::titi::foo::bar::trampoline(st, input);
|
||||
|
||||
std::cout << "rec(" << input << ") == " << r << std::endl;
|
||||
|
||||
Printer printer;
|
||||
// printer.address = true;
|
||||
printer.object = true;
|
||||
printer.print(st, stdout);
|
||||
}
|
||||
}
|
||||
|
||||
int fib(StackTrace& st, int level) {
|
||||
if (level == 2) {
|
||||
return 1;
|
||||
}
|
||||
if (level <= 1) {
|
||||
end_of_our_journey(st);
|
||||
return 0;
|
||||
}
|
||||
return fib(st, level - 1) + fib(st, level - 2);
|
||||
}
|
||||
|
||||
TEST (fibrecursive) {
|
||||
StackTrace st;
|
||||
const int input = 6;
|
||||
int r = fib(st, input);
|
||||
|
||||
std::cout << "fib(" << input << ") == " << r << std::endl;
|
||||
|
||||
Printer printer;
|
||||
printer.print(st, stdout);
|
||||
}
|
||||
50
external/backtrace-cpp/test/select_signals.cpp
vendored
Normal file
50
external/backtrace-cpp/test/select_signals.cpp
vendored
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
/*
|
||||
* test/segfault.cpp
|
||||
* Copyright 2013 Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "backward.hpp"
|
||||
|
||||
#include <stdio.h>
|
||||
#include "test/test.hpp"
|
||||
|
||||
using namespace backward;
|
||||
|
||||
void badass_function() {
|
||||
char* ptr = (char*)42;
|
||||
*ptr = 42;
|
||||
}
|
||||
|
||||
TEST_SEGFAULT (pprint_sigsev) {
|
||||
std::vector<int> signals;
|
||||
signals.push_back(SIGSEGV);
|
||||
SignalHandling sh(signals);
|
||||
std::cout << std::boolalpha << "sh.loaded() == " << sh.loaded() << std::endl;
|
||||
badass_function();
|
||||
}
|
||||
|
||||
TEST_SEGFAULT (wont_pprint) {
|
||||
std::vector<int> signals;
|
||||
signals.push_back(SIGABRT);
|
||||
SignalHandling sh(signals);
|
||||
std::cout << std::boolalpha << "sh.loaded() == " << sh.loaded() << std::endl;
|
||||
badass_function();
|
||||
}
|
||||
65
external/backtrace-cpp/test/stacktrace.cpp
vendored
Normal file
65
external/backtrace-cpp/test/stacktrace.cpp
vendored
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
/*
|
||||
* test/stacktrace.cpp
|
||||
* Copyright 2013 Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "backward.hpp"
|
||||
#include <cstdio>
|
||||
#include "test/test.hpp"
|
||||
|
||||
using namespace backward;
|
||||
|
||||
void collect_trace(StackTrace& st) {
|
||||
st.load_here();
|
||||
}
|
||||
|
||||
TEST (minitrace) {
|
||||
StackTrace st;
|
||||
collect_trace(st);
|
||||
|
||||
Printer printer;
|
||||
printer.print(st, stdout);
|
||||
}
|
||||
|
||||
void d(StackTrace& st) {
|
||||
st.load_here();
|
||||
}
|
||||
|
||||
void c(StackTrace& st) {
|
||||
return d(st);
|
||||
}
|
||||
|
||||
void b(StackTrace& st) {
|
||||
return c(st);
|
||||
}
|
||||
|
||||
__attribute__ ((noinline))
|
||||
void a(StackTrace& st) {
|
||||
return b(st);
|
||||
}
|
||||
|
||||
TEST (smalltrace) {
|
||||
StackTrace st;
|
||||
a(st);
|
||||
|
||||
Printer printer;
|
||||
printer.print(st, stdout);
|
||||
}
|
||||
93
external/backtrace-cpp/test/suicide.cpp
vendored
Normal file
93
external/backtrace-cpp/test/suicide.cpp
vendored
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
/*
|
||||
* test/suicide.cpp
|
||||
* Copyright 2013 Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "backward.hpp"
|
||||
|
||||
#include <cstdio>
|
||||
#include <sys/resource.h>
|
||||
#include "test/test.hpp"
|
||||
|
||||
using namespace backward;
|
||||
|
||||
void badass_function()
|
||||
{
|
||||
char* ptr = (char*)42;
|
||||
*ptr = 42;
|
||||
}
|
||||
|
||||
TEST_SEGFAULT (invalid_write)
|
||||
{
|
||||
badass_function();
|
||||
}
|
||||
|
||||
int you_shall_not_pass()
|
||||
{
|
||||
char* ptr = (char*)42;
|
||||
int v = *ptr;
|
||||
return v;
|
||||
}
|
||||
|
||||
TEST_SEGFAULT(invalid_read)
|
||||
{
|
||||
int v = you_shall_not_pass();
|
||||
std::cout << "v=" << v << std::endl;
|
||||
}
|
||||
|
||||
void abort_abort_I_repeat_abort_abort()
|
||||
{
|
||||
std::cout << "Jumping off the boat!" << std::endl;
|
||||
abort();
|
||||
}
|
||||
|
||||
TEST_ABORT (calling_abort)
|
||||
{
|
||||
abort_abort_I_repeat_abort_abort();
|
||||
}
|
||||
|
||||
volatile int zero = 0;
|
||||
|
||||
int divide_by_zero()
|
||||
{
|
||||
std::cout << "And the wild black hole appears..." << std::endl;
|
||||
int v = 42 / zero;
|
||||
return v;
|
||||
}
|
||||
|
||||
TEST_DIVZERO (divide_by_zero)
|
||||
{
|
||||
int v = divide_by_zero();
|
||||
std::cout << "v=" << v << std::endl;
|
||||
}
|
||||
|
||||
int bye_bye_stack(int i) {
|
||||
return bye_bye_stack(i + 1) + bye_bye_stack(i * 2);
|
||||
}
|
||||
|
||||
TEST_SEGFAULT(stackoverflow)
|
||||
{
|
||||
struct rlimit limit;
|
||||
limit.rlim_max = 8096;
|
||||
setrlimit(RLIMIT_STACK, &limit);
|
||||
int r = bye_bye_stack(42);
|
||||
std::cout << "r=" << r << std::endl;
|
||||
}
|
||||
75
external/backtrace-cpp/test/test.cpp
vendored
Normal file
75
external/backtrace-cpp/test/test.cpp
vendored
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
/*
|
||||
* test/test.cpp
|
||||
* Copyright 2013 Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <cstdlib>
|
||||
#include "test/test.hpp"
|
||||
|
||||
TEST (empty_test) { }
|
||||
|
||||
TEST_FAIL_ASSERT (fail_assert) {
|
||||
ASSERT(1 == 2);
|
||||
}
|
||||
|
||||
TEST_FAIL_ASSERT (fail_assert_ge) {
|
||||
ASSERT_GE(4, 5);
|
||||
}
|
||||
|
||||
TEST_UNCAUGHT_EXCEPTION (uncaught_exception) {
|
||||
throw std::runtime_error("some random runtime error");
|
||||
}
|
||||
|
||||
TEST_UNCAUGHT_EXCEPTION (uncaught_exception_int) {
|
||||
throw 42;
|
||||
}
|
||||
|
||||
TEST_SEGFAULT (segfault) {
|
||||
char* a = 0;
|
||||
char b = a[42];
|
||||
std::cout << "result: " << b << std::endl;
|
||||
}
|
||||
|
||||
TEST_ABORT (abort) {
|
||||
abort();
|
||||
}
|
||||
|
||||
TEST (catch_int) {
|
||||
ASSERT_THROW({throw 42;}, int);
|
||||
}
|
||||
|
||||
TEST_FAIL_ASSERT (fail_catch_int) {
|
||||
ASSERT_THROW({}, int);
|
||||
}
|
||||
|
||||
TEST_FAIL_ASSERT (fail_no_throw) {
|
||||
ASSERT_NO_THROW({throw 42;});
|
||||
}
|
||||
|
||||
TEST (any_throw) {
|
||||
ASSERT_ANY_THROW({throw 42;});
|
||||
}
|
||||
|
||||
TEST_FAIL_ASSERT (fail_any_throw) {
|
||||
ASSERT_ANY_THROW({});
|
||||
}
|
||||
170
external/backtrace-cpp/test/test.hpp
vendored
Normal file
170
external/backtrace-cpp/test/test.hpp
vendored
Normal file
|
|
@ -0,0 +1,170 @@
|
|||
/*
|
||||
* test/test.hpp
|
||||
* Copyright 2013 Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#ifndef H_54E531F7_9154_454B_BEB9_257408429470
|
||||
#define H_54E531F7_9154_454B_BEB9_257408429470
|
||||
|
||||
#include <exception>
|
||||
#include <vector>
|
||||
#include <cstring>
|
||||
#include <cstdio>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
namespace test {
|
||||
|
||||
struct AssertFailedError: std::exception {
|
||||
~AssertFailedError() throw() {}
|
||||
|
||||
AssertFailedError(const char* filename, int line, const char* errmsg):
|
||||
basename(_basename(filename)), line(line), errmsg(errmsg) {}
|
||||
|
||||
const char* what() const throw() {
|
||||
if (not _what.size()) {
|
||||
std::ostringstream ss;
|
||||
ss << "assertion failed (" << basename << ":" << line;
|
||||
ss << ") " << errmsg;
|
||||
_what = ss.str();
|
||||
}
|
||||
return _what.c_str();
|
||||
}
|
||||
|
||||
const char* basename;
|
||||
int line;
|
||||
const char* errmsg;
|
||||
|
||||
mutable std::string _what;
|
||||
|
||||
static const char* _basename(const char* filename) {
|
||||
const char* basename = filename + strlen(filename);
|
||||
while (basename != filename && *basename != '/') {
|
||||
basename -= 1;
|
||||
}
|
||||
return basename + 1;
|
||||
}
|
||||
};
|
||||
|
||||
enum TestStatus {
|
||||
SUCCESS = 0<<0,
|
||||
FAILED = 1<<0,
|
||||
|
||||
ASSERT_FAIL = FAILED | 1<<1,
|
||||
EXCEPTION_UNCAUGHT = FAILED | 2<<1,
|
||||
SIGNAL_UNCAUGHT = FAILED | 3<<1,
|
||||
SIGNAL_SEGFAULT = SIGNAL_UNCAUGHT | 1<<3,
|
||||
SIGNAL_ABORT = SIGNAL_UNCAUGHT | 2<<3,
|
||||
SIGNAL_DIVZERO = SIGNAL_UNCAUGHT | 2<<3,
|
||||
|
||||
STATUS_MASK = 0x1F
|
||||
};
|
||||
|
||||
struct TestBase {
|
||||
const char* name;
|
||||
TestStatus expected_status;
|
||||
|
||||
virtual ~TestBase() {}
|
||||
TestBase(const char*, TestStatus);
|
||||
virtual void do_test() = 0;
|
||||
|
||||
TestStatus run() {
|
||||
try {
|
||||
do_test();
|
||||
return SUCCESS;
|
||||
} catch(const AssertFailedError& e) {
|
||||
printf("!! %s\n", e.what());
|
||||
return ASSERT_FAIL;
|
||||
} catch(const std::exception& e) {
|
||||
printf("!! exception: %s\n", e.what());
|
||||
return EXCEPTION_UNCAUGHT;
|
||||
} catch(...) {
|
||||
printf("!! unknown exception\n");
|
||||
return EXCEPTION_UNCAUGHT;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
typedef std::vector<TestBase*> test_registry_t;
|
||||
extern test_registry_t test_registry;
|
||||
|
||||
TestBase::TestBase(const char* n, TestStatus s): name(n), expected_status(s) {
|
||||
test_registry.push_back(this);
|
||||
}
|
||||
|
||||
} // namespace test
|
||||
|
||||
#define _TEST_STATUS(name, status) \
|
||||
struct TEST_##name: ::test::TestBase { \
|
||||
TEST_##name(): TestBase(#name, status) {} \
|
||||
void do_test(); \
|
||||
} TEST_##name; \
|
||||
void TEST_##name::do_test()
|
||||
|
||||
#define TEST(name) _TEST_STATUS(name, ::test::SUCCESS)
|
||||
#define TEST_FAIL(name) _TEST_STATUS(name, ::test::FAILED)
|
||||
#define TEST_FAIL_ASSERT(name) _TEST_STATUS(name, ::test::ASSERT_FAIL)
|
||||
#define TEST_UNCAUGHT_EXCEPTION(name) _TEST_STATUS(name, ::test::EXCEPTION_UNCAUGHT)
|
||||
#define TEST_UNCAUGHT_SIGNAL(name) _TEST_STATUS(name, ::test::SIGNAL_UNCAUGHT)
|
||||
#define TEST_SEGFAULT(name) _TEST_STATUS(name, ::test::SIGNAL_SEGFAULT)
|
||||
#define TEST_ABORT(name) _TEST_STATUS(name, ::test::SIGNAL_ABORT)
|
||||
#define TEST_DIVZERO(name) _TEST_STATUS(name, ::test::SIGNAL_DIVZERO)
|
||||
|
||||
#define ASSERT(expr) \
|
||||
(expr) ? static_cast<void>(0) \
|
||||
: throw ::test::AssertFailedError( \
|
||||
__FILE__, __LINE__, #expr)
|
||||
|
||||
#define _ASSERT_BINOP(a, b, cmp) \
|
||||
(not (a cmp b)) ? static_cast<void>(0) \
|
||||
: throw ::test::AssertFailedError( \
|
||||
__FILE__, __LINE__, "because " #a " " #cmp " " #b)
|
||||
|
||||
#define ASSERT_EQ(a, b) _ASSERT_BINOP(a, b, !=)
|
||||
#define ASSERT_NE(a, b) _ASSERT_BINOP(a, b, ==)
|
||||
#define ASSERT_LT(a, b) _ASSERT_BINOP(a, b, >=)
|
||||
#define ASSERT_LE(a, b) _ASSERT_BINOP(a, b, >)
|
||||
#define ASSERT_GT(a, b) _ASSERT_BINOP(a, b, <=)
|
||||
#define ASSERT_GE(a, b) _ASSERT_BINOP(a, b, <)
|
||||
|
||||
#define ASSERT_THROW(expr, e_type) \
|
||||
do { try { expr } \
|
||||
catch (const e_type&) { break; } \
|
||||
throw ::test::AssertFailedError( \
|
||||
__FILE__, __LINE__, "expected exception " #e_type); \
|
||||
} while(0)
|
||||
|
||||
#define ASSERT_ANY_THROW(expr) \
|
||||
do { try { expr } \
|
||||
catch (...) { break; } \
|
||||
throw ::test::AssertFailedError( \
|
||||
__FILE__, __LINE__, "expected any exception"); \
|
||||
} while(0)
|
||||
|
||||
#define ASSERT_NO_THROW(expr) \
|
||||
try { expr } \
|
||||
catch (...) { \
|
||||
throw ::test::AssertFailedError( \
|
||||
__FILE__, __LINE__, "no exception expected"); \
|
||||
}
|
||||
|
||||
#endif /* H_GUARD */
|
||||
Loading…
Add table
Add a link
Reference in a new issue