mirror of
https://github.com/keymanapp/keyman.git
synced 2026-08-05 08:25:32 +00:00
Had a real yak shave this morning with disabling assertions in release
builds in our C/C++ code. It turns out that our unit tests use
`assert()` which we intended to use from `test_assert.h`, but in some
cases `cassert` or `assert.h` had been #included after `test_assert.h`,
overriding our special `assert()` macro. The chain of includes is
somewhat hard to puzzle out -- it's often buried several levels deep.
This meant that a release build would drop all test assertions, meaning
most tests passed, unsurprisingly, as there were no assertions left to
fail ... but some tests failed with crashes because we optimized out
important lines such as `assert(some_important_function())`.
I was quite unhappy with this fragility, so I have opted to rename
`assert()` to `test_assert()` in all of our home-grown C/C++ unit tests,
which further highlighted unit tests which were only using the C/C++
`assert()` and not ours, so then had to figure out which unit test
executables needed to have `test_assert` added, and then ... then ...
discovered a bug in `test_color.h`, where we were #including
`io.h`/`unistd.h` inside a `namespace console_color {}` block, which
just happened to be the first ref to those beautiful headers, and thus
(because `#pragma once`) meant that useful little functions like
`access()` were no longer accessible to us in the global namespace.
I have also audited Every Single Call to `assert()` to verify that we do
not do Important Work inside the parentheses, and, apart from those
offending unit tests, now resolved with `test_assert()`, it looks like
all is good.
I would like to present one very well-shaved yak in this commit.
Fixes: #12619
75 lines
2 KiB
C++
75 lines
2 KiB
C++
/*
|
|
* Keyman is copyright (C) SIL International. MIT License.
|
|
*
|
|
* Keyman Core - test assertion macros
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <iostream>
|
|
#include <string>
|
|
#include "test_color.h"
|
|
|
|
#ifdef _test_assert_failed
|
|
#undef _test_assert_failed
|
|
#endif
|
|
#define _test_assert_failed(result, exprText) { \
|
|
std::wcerr << console_color::fg(console_color::BRIGHT_RED) \
|
|
<< "Test failed with " << (result) \
|
|
<< " at " << __FILE__ << ":" << __LINE__ << ":" \
|
|
<< console_color::reset() \
|
|
<< std::endl \
|
|
<< " " << (exprText) << std::endl; \
|
|
std::exit(EXIT_FAILURE); \
|
|
}
|
|
|
|
#ifdef try_status
|
|
#undef try_status
|
|
#endif
|
|
#define try_status(expr) { \
|
|
auto __s = (expr); \
|
|
if (__s != KM_CORE_STATUS_OK) { \
|
|
_test_assert_failed(__s, u ## #expr); \
|
|
} \
|
|
}
|
|
|
|
#ifdef test_assert
|
|
#undef test_assert
|
|
#endif
|
|
#define test_assert(expr) { \
|
|
if (!(expr)) { \
|
|
_test_assert_failed(0, u ## #expr); \
|
|
} \
|
|
}
|
|
|
|
#ifdef test_assert_equal
|
|
#undef test_assert_equal
|
|
#endif
|
|
#define test_assert_equal(actual, expected) { \
|
|
if ((actual) != (expected)) { \
|
|
std::wcerr << console_color::fg(console_color::BRIGHT_RED) \
|
|
<< "Test failed at " << __FILE__ << ":" << __LINE__ << ":" \
|
|
<< console_color::reset() \
|
|
<< std::endl \
|
|
<< "expected: " << (expected) << std::endl \
|
|
<< "actual: " << (actual) << std::endl; \
|
|
std::exit(EXIT_FAILURE); \
|
|
} \
|
|
}
|
|
|
|
#ifdef test_assert_string_equal
|
|
#undef test_assert_string_equal
|
|
#endif
|
|
#define test_assert_string_equal(actual, expected) { \
|
|
if (u16cmp((actual), (expected)) != 0) { \
|
|
std::wcerr << console_color::fg(console_color::BRIGHT_RED) \
|
|
<< "Test failed at " << __FILE__ << ":" << __LINE__ << ":" \
|
|
<< console_color::reset() \
|
|
<< std::endl \
|
|
<< "expected: " << Debug_UnicodeString((PKMX_WCHAR)(expected)) << std::endl \
|
|
<< "actual: " << Debug_UnicodeString((PKMX_WCHAR)(actual)) << std::endl; \
|
|
std::exit(EXIT_FAILURE); \
|
|
} \
|
|
}
|