mirror of
https://github.com/QuasarApp/backward-cpp.git
synced 2025-04-26 10:44:31 +00:00
clang-format
This commit is contained in:
parent
db6adef8ab
commit
9fb93a08aa
2
.clang-format
Normal file
2
.clang-format
Normal file
@ -0,0 +1,2 @@
|
||||
---
|
||||
BasedOnStyle: LLVM
|
5870
backward.hpp
5870
backward.hpp
File diff suppressed because it is too large
Load Diff
@ -24,8 +24,8 @@
|
||||
#include "test.hpp"
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <unistd.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#if defined(__has_include) && __has_include(<error.h>)
|
||||
#include <error.h>
|
||||
@ -40,113 +40,116 @@
|
||||
#endif
|
||||
|
||||
void error(int status, int errnum, const char *format, ...) {
|
||||
fflush(stdout);
|
||||
fprintf(stderr, "%s: ", getprogname());
|
||||
fflush(stdout);
|
||||
fprintf(stderr, "%s: ", getprogname());
|
||||
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
vfprintf(stderr, format, args);
|
||||
va_end(args);
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
vfprintf(stderr, format, args);
|
||||
va_end(args);
|
||||
|
||||
if (errnum != 0) {
|
||||
fprintf(stderr, ": %s\n", strerror(errnum));
|
||||
} else {
|
||||
fprintf(stderr, "\n");
|
||||
}
|
||||
if (status != 0) {
|
||||
exit(status);
|
||||
}
|
||||
if (errnum != 0) {
|
||||
fprintf(stderr, ": %s\n", strerror(errnum));
|
||||
} else {
|
||||
fprintf(stderr, "\n");
|
||||
}
|
||||
if (status != 0) {
|
||||
exit(status);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
test::test_registry_t test::test_registry;
|
||||
using namespace test;
|
||||
|
||||
bool run_test(TestBase& test) {
|
||||
printf("-- running test case: %s\n", test.name);
|
||||
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");
|
||||
}
|
||||
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);
|
||||
int child_status = 0;
|
||||
waitpid(child_pid, &child_status, 0);
|
||||
|
||||
test::TestStatus status;
|
||||
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 (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::FAILED) {
|
||||
return (status & test::FAILED);
|
||||
}
|
||||
|
||||
if (test.expected_status == test::SIGNAL_UNCAUGHT) {
|
||||
return (status & test::SIGNAL_UNCAUGHT);
|
||||
}
|
||||
if (test.expected_status == test::SIGNAL_UNCAUGHT) {
|
||||
return (status & test::SIGNAL_UNCAUGHT);
|
||||
}
|
||||
|
||||
return status == test.expected_status;
|
||||
return status == test.expected_status;
|
||||
}
|
||||
|
||||
int main(int argc, const char* const argv[]) {
|
||||
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;
|
||||
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;
|
||||
}
|
||||
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;
|
||||
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;
|
||||
}
|
||||
|
@ -22,78 +22,78 @@
|
||||
*/
|
||||
|
||||
#include "backward.hpp"
|
||||
#include <stdio.h>
|
||||
#include "test/test.hpp"
|
||||
#include <stdio.h>
|
||||
|
||||
using namespace backward;
|
||||
|
||||
typedef StackTrace stacktrace_t;
|
||||
|
||||
void end_of_our_journey(stacktrace_t& st) {
|
||||
if (not st.size()) {
|
||||
st.load_here();
|
||||
}
|
||||
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);
|
||||
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 {
|
||||
struct foo {
|
||||
|
||||
union bar {
|
||||
__attribute__((noinline))
|
||||
static int trampoline(stacktrace_t& st, int level) {
|
||||
return rec(st, level);
|
||||
}
|
||||
};
|
||||
};
|
||||
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);
|
||||
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;
|
||||
std::cout << "rec(" << input << ") == " << r << std::endl;
|
||||
|
||||
Printer printer;
|
||||
// printer.address = true;
|
||||
printer.object = true;
|
||||
printer.print(st, stdout);
|
||||
}
|
||||
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);
|
||||
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);
|
||||
TEST(fibrecursive) {
|
||||
StackTrace st;
|
||||
const int input = 6;
|
||||
int r = fib(st, input);
|
||||
|
||||
std::cout << "fib(" << input << ") == " << r << std::endl;
|
||||
std::cout << "fib(" << input << ") == " << r << std::endl;
|
||||
|
||||
Printer printer;
|
||||
printer.print(st, stdout);
|
||||
Printer printer;
|
||||
printer.print(st, stdout);
|
||||
}
|
||||
|
@ -23,29 +23,29 @@
|
||||
|
||||
#include "backward.hpp"
|
||||
|
||||
#include "test/test.hpp"
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include "test/test.hpp"
|
||||
|
||||
using namespace backward;
|
||||
|
||||
void badass_function() {
|
||||
char* ptr = (char*)42;
|
||||
*ptr = 42;
|
||||
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(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();
|
||||
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();
|
||||
}
|
||||
|
@ -22,44 +22,33 @@
|
||||
*/
|
||||
|
||||
#include "backward.hpp"
|
||||
#include <cstdio>
|
||||
#include "test/test.hpp"
|
||||
#include <cstdio>
|
||||
|
||||
using namespace backward;
|
||||
|
||||
void collect_trace(StackTrace& st) {
|
||||
st.load_here();
|
||||
void collect_trace(StackTrace &st) { st.load_here(); }
|
||||
|
||||
TEST(minitrace) {
|
||||
StackTrace st;
|
||||
collect_trace(st);
|
||||
|
||||
Printer printer;
|
||||
printer.print(st, stdout);
|
||||
}
|
||||
|
||||
TEST (minitrace) {
|
||||
StackTrace st;
|
||||
collect_trace(st);
|
||||
void d(StackTrace &st) { st.load_here(); }
|
||||
|
||||
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);
|
||||
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);
|
||||
}
|
||||
|
@ -23,77 +23,62 @@
|
||||
|
||||
#include "backward.hpp"
|
||||
|
||||
#include "test/test.hpp"
|
||||
#include <cstdio>
|
||||
#include <sys/resource.h>
|
||||
#include "test/test.hpp"
|
||||
|
||||
using namespace backward;
|
||||
|
||||
void badass_function()
|
||||
{
|
||||
char* ptr = (char*)42;
|
||||
*ptr = 42;
|
||||
void badass_function() {
|
||||
char *ptr = (char *)42;
|
||||
*ptr = 42;
|
||||
}
|
||||
|
||||
TEST_SEGFAULT (invalid_write)
|
||||
{
|
||||
badass_function();
|
||||
TEST_SEGFAULT(invalid_write) { badass_function(); }
|
||||
|
||||
int you_shall_not_pass() {
|
||||
char *ptr = (char *)42;
|
||||
int v = *ptr;
|
||||
return v;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
TEST_ABORT(calling_abort) { abort_abort_I_repeat_abort_abort(); }
|
||||
|
||||
// aarch64 does not trap Division by zero
|
||||
#ifndef __aarch64__
|
||||
volatile int zero = 0;
|
||||
|
||||
int divide_by_zero()
|
||||
{
|
||||
std::cout << "And the wild black hole appears..." << std::endl;
|
||||
int v = 42 / zero;
|
||||
return v;
|
||||
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;
|
||||
TEST_DIVZERO(divide_by_zero) {
|
||||
int v = divide_by_zero();
|
||||
std::cout << "v=" << v << std::endl;
|
||||
}
|
||||
#endif
|
||||
|
||||
// Darwin does not allow RLIMIT_STACK to be reduced
|
||||
#ifndef __APPLE__
|
||||
int bye_bye_stack(int i) {
|
||||
return bye_bye_stack(i + 1) + bye_bye_stack(i * 2);
|
||||
}
|
||||
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;
|
||||
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;
|
||||
}
|
||||
#endif
|
||||
|
@ -21,55 +21,43 @@
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "test/test.hpp"
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <cstdlib>
|
||||
#include "test/test.hpp"
|
||||
|
||||
TEST (empty_test) { }
|
||||
TEST(empty_test) {}
|
||||
|
||||
TEST_FAIL_ASSERT (fail_assert) {
|
||||
ASSERT(1 == 2);
|
||||
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_FAIL_ASSERT (fail_assert_ge) {
|
||||
ASSERT_GE(4, 5);
|
||||
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_UNCAUGHT_EXCEPTION (uncaught_exception) {
|
||||
throw std::runtime_error("some random runtime error");
|
||||
TEST_ABORT(abort) { abort(); }
|
||||
|
||||
TEST(catch_int) {
|
||||
ASSERT_THROW({ throw 42; }, int);
|
||||
}
|
||||
|
||||
TEST_UNCAUGHT_EXCEPTION (uncaught_exception_int) {
|
||||
throw 42;
|
||||
TEST_FAIL_ASSERT(fail_catch_int) { ASSERT_THROW({}, int); }
|
||||
|
||||
TEST_FAIL_ASSERT(fail_no_throw) {
|
||||
ASSERT_NO_THROW({ throw 42; });
|
||||
}
|
||||
|
||||
TEST_SEGFAULT (segfault) {
|
||||
char* a = 0;
|
||||
char b = a[42];
|
||||
std::cout << "result: " << b << std::endl;
|
||||
TEST(any_throw) {
|
||||
ASSERT_ANY_THROW({ throw 42; });
|
||||
}
|
||||
|
||||
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({});
|
||||
}
|
||||
TEST_FAIL_ASSERT(fail_any_throw) { ASSERT_ANY_THROW({}); }
|
||||
|
193
test/test.hpp
193
test/test.hpp
@ -25,119 +25,119 @@
|
||||
#ifndef H_54E531F7_9154_454B_BEB9_257408429470
|
||||
#define H_54E531F7_9154_454B_BEB9_257408429470
|
||||
|
||||
#include <exception>
|
||||
#include <vector>
|
||||
#include <cstring>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <exception>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace test {
|
||||
|
||||
struct AssertFailedError: std::exception {
|
||||
~AssertFailedError() throw() {}
|
||||
struct AssertFailedError : std::exception {
|
||||
~AssertFailedError() throw() {}
|
||||
|
||||
AssertFailedError(const char* filename, int _line, const char* _errmsg):
|
||||
basename(_basename(filename)), line(_line), errmsg(_errmsg) {}
|
||||
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 *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;
|
||||
const char *basename;
|
||||
int line;
|
||||
const char *errmsg;
|
||||
|
||||
mutable std::string _what;
|
||||
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;
|
||||
}
|
||||
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,
|
||||
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,
|
||||
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
|
||||
STATUS_MASK = 0x1F
|
||||
};
|
||||
|
||||
struct TestBase {
|
||||
const char* name;
|
||||
TestStatus expected_status;
|
||||
const char *name;
|
||||
TestStatus expected_status;
|
||||
|
||||
virtual ~TestBase() {}
|
||||
TestBase(const char*, TestStatus);
|
||||
virtual void do_test() = 0;
|
||||
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;
|
||||
}
|
||||
}
|
||||
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;
|
||||
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);
|
||||
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_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_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(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_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, ==)
|
||||
@ -146,25 +146,34 @@ TestBase::TestBase(const char* n, TestStatus s): name(n), expected_status(s) {
|
||||
#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_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_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"); \
|
||||
}
|
||||
#define ASSERT_NO_THROW(expr) \
|
||||
try { \
|
||||
expr \
|
||||
} catch (...) { \
|
||||
throw ::test::AssertFailedError(__FILE__, __LINE__, \
|
||||
"no exception expected"); \
|
||||
}
|
||||
|
||||
#endif /* H_GUARD */
|
||||
|
@ -1,60 +1,46 @@
|
||||
#include <backward/backward.hpp>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
|
||||
using namespace backward;
|
||||
|
||||
class TracedException : public std::runtime_error
|
||||
{
|
||||
class TracedException : public std::runtime_error {
|
||||
public:
|
||||
TracedException() :
|
||||
std::runtime_error(_get_trace())
|
||||
{}
|
||||
TracedException() : std::runtime_error(_get_trace()) {}
|
||||
|
||||
private:
|
||||
std::string _get_trace()
|
||||
{
|
||||
std::ostringstream ss;
|
||||
std::string _get_trace() {
|
||||
std::ostringstream ss;
|
||||
|
||||
StackTrace stackTrace;
|
||||
TraceResolver resolver;
|
||||
stackTrace.load_here();
|
||||
resolver.load_stacktrace(stackTrace);
|
||||
StackTrace stackTrace;
|
||||
TraceResolver resolver;
|
||||
stackTrace.load_here();
|
||||
resolver.load_stacktrace(stackTrace);
|
||||
|
||||
for(std::size_t i = 0; i < stackTrace.size(); ++i)
|
||||
{
|
||||
const ResolvedTrace trace = resolver.resolve(stackTrace[i]);
|
||||
for (std::size_t i = 0; i < stackTrace.size(); ++i) {
|
||||
const ResolvedTrace trace = resolver.resolve(stackTrace[i]);
|
||||
|
||||
ss << "#" << i << " at " << trace.object_function << "\n";
|
||||
}
|
||||
|
||||
return ss.str();
|
||||
ss << "#" << i << " at " << trace.object_function << "\n";
|
||||
}
|
||||
|
||||
return ss.str();
|
||||
}
|
||||
};
|
||||
|
||||
void f(int i)
|
||||
{
|
||||
if(i >= 42)
|
||||
{
|
||||
throw TracedException();
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "i=" << i << "\n";
|
||||
f(i + 1);
|
||||
}
|
||||
void f(int i) {
|
||||
if (i >= 42) {
|
||||
throw TracedException();
|
||||
} else {
|
||||
std::cout << "i=" << i << "\n";
|
||||
f(i + 1);
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
try
|
||||
{
|
||||
f(0);
|
||||
} catch (const TracedException& ex)
|
||||
{
|
||||
std::cout << ex.what();
|
||||
}
|
||||
int main() {
|
||||
try {
|
||||
f(0);
|
||||
} catch (const TracedException &ex) {
|
||||
std::cout << ex.what();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user