From 45d0cbb4c722d84eb178515a205f58d4b4434c87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Fri, 26 Jun 2015 19:37:11 +0200 Subject: [PATCH] add assert_always() macro for release build assertions --- src/mbgl/util/assert.hpp | 13 +++++++++++++ test/miscellaneous/assert.cpp | 9 +++++++++ test/test.gypi | 1 + 3 files changed, 23 insertions(+) create mode 100644 src/mbgl/util/assert.hpp create mode 100644 test/miscellaneous/assert.cpp diff --git a/src/mbgl/util/assert.hpp b/src/mbgl/util/assert.hpp new file mode 100644 index 000000000..0c2f63f8f --- /dev/null +++ b/src/mbgl/util/assert.hpp @@ -0,0 +1,13 @@ +#ifndef MBGL_UTIL_ASSERT +#define MBGL_UTIL_ASSERT + +#include + +// Based on FreeBSD's src/include/assert.h +// Licensed under the original BSD license +#define assert_always(expr) \ + ((void)((expr) ? ((void)0) : ((void)fprintf(stderr, "%s:%u: failed assertion `%s'\n", \ + __FILE__, __LINE__, #expr), \ + abort()))) + +#endif diff --git a/test/miscellaneous/assert.cpp b/test/miscellaneous/assert.cpp new file mode 100644 index 000000000..f3eba009f --- /dev/null +++ b/test/miscellaneous/assert.cpp @@ -0,0 +1,9 @@ +#include "../fixtures/util.hpp" + +#include + +using namespace mbgl; + +TEST(Assert, Always) { + EXPECT_DEATH(assert_always(true == false), "failed assertion `true == false'"); +} diff --git a/test/test.gypi b/test/test.gypi index 374e81344..f3d6976dd 100644 --- a/test/test.gypi +++ b/test/test.gypi @@ -41,6 +41,7 @@ 'headless/headless.cpp', + 'miscellaneous/assert.cpp', 'miscellaneous/clip_ids.cpp', 'miscellaneous/binpack.cpp', 'miscellaneous/bilinear.cpp',