src: add assert_noexcept()
This commit is contained in:
+136
-120
@@ -196,7 +196,7 @@ struct CheckIntegral {
|
||||
static constexpr U z[2] = {};
|
||||
};
|
||||
template <class U>
|
||||
static void checkU(void) {
|
||||
static void checkU(void) noexcept {
|
||||
{
|
||||
U a = {};
|
||||
const U b = {};
|
||||
@@ -204,21 +204,21 @@ struct CheckIntegral {
|
||||
U x[2] = {};
|
||||
const U y[2] = {};
|
||||
constexpr U z[2] = {};
|
||||
assert(a == 0);
|
||||
assert(b == 0);
|
||||
assert(c == 0);
|
||||
assert(x[0] == 0 && x[1] == 0);
|
||||
assert(y[0] == 0 && y[1] == 0);
|
||||
assert(z[0] == 0 && z[1] == 0);
|
||||
assert_noexcept(a == 0);
|
||||
assert_noexcept(b == 0);
|
||||
assert_noexcept(c == 0);
|
||||
assert_noexcept(x[0] == 0 && x[1] == 0);
|
||||
assert_noexcept(y[0] == 0 && y[1] == 0);
|
||||
assert_noexcept(z[0] == 0 && z[1] == 0);
|
||||
}
|
||||
{
|
||||
TestU<U> t;
|
||||
assert(t.a == 0);
|
||||
assert(t.b == 0);
|
||||
assert(t.c == 0);
|
||||
assert(t.x[0] == 0 && t.x[1] == 0);
|
||||
assert(t.y[0] == 0 && t.y[1] == 0);
|
||||
assert(t.z[0] == 0 && t.z[1] == 0);
|
||||
assert_noexcept(t.a == 0);
|
||||
assert_noexcept(t.b == 0);
|
||||
assert_noexcept(t.c == 0);
|
||||
assert_noexcept(t.x[0] == 0 && t.x[1] == 0);
|
||||
assert_noexcept(t.y[0] == 0 && t.y[1] == 0);
|
||||
assert_noexcept(t.z[0] == 0 && t.z[1] == 0);
|
||||
}
|
||||
#if __cplusplus < 202002L
|
||||
COMPILE_TIME_ASSERT(std::is_pod<U>::value) // std::is_pod is deprecated in C++20
|
||||
@@ -233,27 +233,27 @@ struct CheckIntegral {
|
||||
COMPILE_TIME_ASSERT(upx_is_integral<U>::value)
|
||||
COMPILE_TIME_ASSERT(upx_is_integral_v<U>)
|
||||
}
|
||||
static void check(void) {
|
||||
static void check(void) noexcept {
|
||||
{
|
||||
TestT t = {};
|
||||
assert(t.a == 0);
|
||||
assert(t.x[0] == 0 && t.x[1] == 0);
|
||||
assert_noexcept(t.a == 0);
|
||||
assert_noexcept(t.x[0] == 0 && t.x[1] == 0);
|
||||
}
|
||||
{
|
||||
const TestT t = {};
|
||||
assert(t.a == 0);
|
||||
assert(t.x[0] == 0 && t.x[1] == 0);
|
||||
assert_noexcept(t.a == 0);
|
||||
assert_noexcept(t.x[0] == 0 && t.x[1] == 0);
|
||||
}
|
||||
{
|
||||
constexpr TestT t = {};
|
||||
assert(t.a == 0);
|
||||
assert(t.x[0] == 0 && t.x[1] == 0);
|
||||
assert_noexcept(t.a == 0);
|
||||
assert_noexcept(t.x[0] == 0 && t.x[1] == 0);
|
||||
}
|
||||
{
|
||||
TestT t;
|
||||
mem_clear(&t);
|
||||
assert(t.a == 0);
|
||||
assert(t.x[0] == 0 && t.x[1] == 0);
|
||||
assert_noexcept(t.a == 0);
|
||||
assert_noexcept(t.x[0] == 0 && t.x[1] == 0);
|
||||
}
|
||||
checkU<T>();
|
||||
checkU<typename std::add_const<T>::type>();
|
||||
@@ -261,7 +261,7 @@ struct CheckIntegral {
|
||||
};
|
||||
template <class T>
|
||||
struct CheckAlignment {
|
||||
static void check(void) {
|
||||
static void check(void) noexcept {
|
||||
COMPILE_TIME_ASSERT_ALIGNED1(T)
|
||||
struct alignas(1) Test1 {
|
||||
char a;
|
||||
@@ -285,12 +285,12 @@ struct CheckAlignment {
|
||||
};
|
||||
template <class T>
|
||||
struct TestBELE {
|
||||
static noinline bool test(void) {
|
||||
static noinline bool test(void) noexcept {
|
||||
CheckIntegral<T>::check();
|
||||
CheckAlignment<T>::check();
|
||||
// arithmetic checks
|
||||
T allbits = {};
|
||||
assert(allbits == 0);
|
||||
assert_noexcept(allbits == 0);
|
||||
allbits += 1;
|
||||
allbits -= 2;
|
||||
T v1;
|
||||
@@ -300,22 +300,22 @@ struct TestBELE {
|
||||
v1 -= 1;
|
||||
T v2;
|
||||
v2 = 1;
|
||||
assert((v1 == v2));
|
||||
assert(!(v1 != v2));
|
||||
assert((v1 <= v2));
|
||||
assert((v1 >= v2));
|
||||
assert(!(v1 < v2));
|
||||
assert(!(v1 > v2));
|
||||
assert_noexcept((v1 == v2));
|
||||
assert_noexcept(!(v1 != v2));
|
||||
assert_noexcept((v1 <= v2));
|
||||
assert_noexcept((v1 >= v2));
|
||||
assert_noexcept(!(v1 < v2));
|
||||
assert_noexcept(!(v1 > v2));
|
||||
v2 ^= allbits;
|
||||
assert(!(v1 == v2));
|
||||
assert((v1 != v2));
|
||||
assert((v1 <= v2));
|
||||
assert(!(v1 >= v2));
|
||||
assert((v1 < v2));
|
||||
assert(!(v1 > v2));
|
||||
assert_noexcept(!(v1 == v2));
|
||||
assert_noexcept((v1 != v2));
|
||||
assert_noexcept((v1 <= v2));
|
||||
assert_noexcept(!(v1 >= v2));
|
||||
assert_noexcept((v1 < v2));
|
||||
assert_noexcept(!(v1 > v2));
|
||||
v2 += 2;
|
||||
assert(v1 == 1);
|
||||
assert(v2 == 0);
|
||||
assert_noexcept(v1 == 1);
|
||||
assert_noexcept(v2 == 0);
|
||||
v1 <<= 1;
|
||||
v1 |= v2;
|
||||
v1 >>= 1;
|
||||
@@ -324,8 +324,8 @@ struct TestBELE {
|
||||
v2 *= v1;
|
||||
v1 += v2;
|
||||
v1 -= v2;
|
||||
assert(v1 == 1);
|
||||
assert(v2 == 0);
|
||||
assert_noexcept(v1 == 1);
|
||||
assert_noexcept(v2 == 0);
|
||||
if ((v1 ^ v2) != 1)
|
||||
return false;
|
||||
return true;
|
||||
@@ -358,7 +358,7 @@ struct TestIntegerWrap {
|
||||
#undef ACCCHK_ASSERT
|
||||
#include "../miniacc.h"
|
||||
|
||||
void upx_compiler_sanity_check(void) {
|
||||
void upx_compiler_sanity_check(void) noexcept {
|
||||
#define ACC_WANT_ACC_CHK_CH 1
|
||||
#undef ACCCHK_ASSERT
|
||||
#define ACCCHK_ASSERT(expr) ACC_COMPILE_TIME_ASSERT(expr)
|
||||
@@ -402,31 +402,32 @@ void upx_compiler_sanity_check(void) {
|
||||
COMPILE_TIME_ASSERT(sizeof(*((charptr) nullptr)) == 1)
|
||||
|
||||
COMPILE_TIME_ASSERT(sizeof(UPX_VERSION_STRING4) == 4 + 1)
|
||||
assert(strlen(UPX_VERSION_STRING4) == 4);
|
||||
assert_noexcept(strlen(UPX_VERSION_STRING4) == 4);
|
||||
COMPILE_TIME_ASSERT(sizeof(UPX_VERSION_YEAR) == 4 + 1)
|
||||
assert(strlen(UPX_VERSION_YEAR) == 4);
|
||||
assert(memcmp(UPX_VERSION_DATE_ISO, UPX_VERSION_YEAR, 4) == 0);
|
||||
assert(memcmp(&UPX_VERSION_DATE[sizeof(UPX_VERSION_DATE) - 1 - 4], UPX_VERSION_YEAR, 4) == 0);
|
||||
assert_noexcept(strlen(UPX_VERSION_YEAR) == 4);
|
||||
assert_noexcept(memcmp(UPX_VERSION_DATE_ISO, UPX_VERSION_YEAR, 4) == 0);
|
||||
assert_noexcept(
|
||||
memcmp(&UPX_VERSION_DATE[sizeof(UPX_VERSION_DATE) - 1 - 4], UPX_VERSION_YEAR, 4) == 0);
|
||||
if (gitrev[0]) {
|
||||
size_t revlen = strlen(gitrev);
|
||||
if (strncmp(gitrev, "ERROR", 5) == 0) {
|
||||
assert(revlen == 5 || revlen == 6);
|
||||
assert_noexcept(revlen == 5 || revlen == 6);
|
||||
} else {
|
||||
assert(revlen == 12 || revlen == 13);
|
||||
assert_noexcept(revlen == 12 || revlen == 13);
|
||||
}
|
||||
if (revlen == 6 || revlen == 13) {
|
||||
assert(gitrev[revlen - 1] == '+');
|
||||
assert_noexcept(gitrev[revlen - 1] == '+');
|
||||
}
|
||||
}
|
||||
assert(UPX_RSIZE_MAX_MEM == 805306368);
|
||||
assert_noexcept(UPX_RSIZE_MAX_MEM == 805306368);
|
||||
|
||||
#if DEBUG || 1
|
||||
assert(TestBELE<LE16>::test());
|
||||
assert(TestBELE<LE32>::test());
|
||||
assert(TestBELE<LE64>::test());
|
||||
assert(TestBELE<BE16>::test());
|
||||
assert(TestBELE<BE32>::test());
|
||||
assert(TestBELE<BE64>::test());
|
||||
assert_noexcept(TestBELE<LE16>::test());
|
||||
assert_noexcept(TestBELE<LE32>::test());
|
||||
assert_noexcept(TestBELE<LE64>::test());
|
||||
assert_noexcept(TestBELE<BE16>::test());
|
||||
assert_noexcept(TestBELE<BE32>::test());
|
||||
assert_noexcept(TestBELE<BE64>::test());
|
||||
{
|
||||
alignas(16) static const byte dd[32] = {
|
||||
0, 0, 0, 0, 0, 0, 0, 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0,
|
||||
@@ -434,51 +435,51 @@ void upx_compiler_sanity_check(void) {
|
||||
const byte *d;
|
||||
const N_BELE_RTP::AbstractPolicy *bele;
|
||||
d = dd + 7;
|
||||
assert(upx_adler32(d, 4) == 0x09f003f7);
|
||||
assert(upx_adler32(d, 4, 0) == 0x09ec03f6);
|
||||
assert(upx_adler32(d, 4, 1) == 0x09f003f7);
|
||||
assert_noexcept(upx_adler32(d, 4) == 0x09f003f7);
|
||||
assert_noexcept(upx_adler32(d, 4, 0) == 0x09ec03f6);
|
||||
assert_noexcept(upx_adler32(d, 4, 1) == 0x09f003f7);
|
||||
bele = &N_BELE_RTP::be_policy;
|
||||
assert(get_be16(d) == 0xfffe);
|
||||
assert(bele->get16(d) == 0xfffe);
|
||||
assert(get_be16_signed(d) == -2);
|
||||
assert(get_be24(d) == 0xfffefd);
|
||||
assert(bele->get24(d) == 0xfffefd);
|
||||
assert(get_be24_signed(d) == -259);
|
||||
assert(get_be32(d) == 0xfffefdfc);
|
||||
assert(bele->get32(d) == 0xfffefdfc);
|
||||
assert(get_be32_signed(d) == -66052);
|
||||
assert_noexcept(get_be16(d) == 0xfffe);
|
||||
assert_noexcept(bele->get16(d) == 0xfffe);
|
||||
assert_noexcept(get_be16_signed(d) == -2);
|
||||
assert_noexcept(get_be24(d) == 0xfffefd);
|
||||
assert_noexcept(bele->get24(d) == 0xfffefd);
|
||||
assert_noexcept(get_be24_signed(d) == -259);
|
||||
assert_noexcept(get_be32(d) == 0xfffefdfc);
|
||||
assert_noexcept(bele->get32(d) == 0xfffefdfc);
|
||||
assert_noexcept(get_be32_signed(d) == -66052);
|
||||
bele = &N_BELE_RTP::le_policy;
|
||||
assert(get_le16(d) == 0xfeff);
|
||||
assert(bele->get16(d) == 0xfeff);
|
||||
assert(get_le16_signed(d) == -257);
|
||||
assert(get_le24(d) == 0xfdfeff);
|
||||
assert(bele->get24(d) == 0xfdfeff);
|
||||
assert(get_le24_signed(d) == -131329);
|
||||
assert(get_le32(d) == 0xfcfdfeff);
|
||||
assert(bele->get32(d) == 0xfcfdfeff);
|
||||
assert(get_le32_signed(d) == -50462977);
|
||||
assert(get_le64_signed(d) == -506097522914230529LL);
|
||||
assert(find_be16(d, 2, 0xfffe) == 0);
|
||||
assert(find_le16(d, 2, 0xfeff) == 0);
|
||||
assert(find_be32(d, 4, 0xfffefdfc) == 0);
|
||||
assert(find_le32(d, 4, 0xfcfdfeff) == 0);
|
||||
assert_noexcept(get_le16(d) == 0xfeff);
|
||||
assert_noexcept(bele->get16(d) == 0xfeff);
|
||||
assert_noexcept(get_le16_signed(d) == -257);
|
||||
assert_noexcept(get_le24(d) == 0xfdfeff);
|
||||
assert_noexcept(bele->get24(d) == 0xfdfeff);
|
||||
assert_noexcept(get_le24_signed(d) == -131329);
|
||||
assert_noexcept(get_le32(d) == 0xfcfdfeff);
|
||||
assert_noexcept(bele->get32(d) == 0xfcfdfeff);
|
||||
assert_noexcept(get_le32_signed(d) == -50462977);
|
||||
assert_noexcept(get_le64_signed(d) == -506097522914230529LL);
|
||||
assert_noexcept(find_be16(d, 2, 0xfffe) == 0);
|
||||
assert_noexcept(find_le16(d, 2, 0xfeff) == 0);
|
||||
assert_noexcept(find_be32(d, 4, 0xfffefdfc) == 0);
|
||||
assert_noexcept(find_le32(d, 4, 0xfcfdfeff) == 0);
|
||||
d += 12;
|
||||
assert(get_be16_signed(d) == 32638);
|
||||
assert(get_be24_signed(d) == 8355453);
|
||||
assert(get_be32_signed(d) == 2138996092);
|
||||
assert(get_be64_signed(d) == 9186918263483431288LL);
|
||||
assert_noexcept(get_be16_signed(d) == 32638);
|
||||
assert_noexcept(get_be24_signed(d) == 8355453);
|
||||
assert_noexcept(get_be32_signed(d) == 2138996092);
|
||||
assert_noexcept(get_be64_signed(d) == 9186918263483431288LL);
|
||||
}
|
||||
{
|
||||
unsigned dd;
|
||||
void *const d = ⅆ
|
||||
dd = ne32_to_le32(0xf7f6f5f4);
|
||||
assert(get_le26(d) == 0x03f6f5f4);
|
||||
assert_noexcept(get_le26(d) == 0x03f6f5f4);
|
||||
set_le26(d, 0);
|
||||
assert(get_le26(d) == 0);
|
||||
assert(dd == ne32_to_le32(0xf4000000));
|
||||
assert_noexcept(get_le26(d) == 0);
|
||||
assert_noexcept(dd == ne32_to_le32(0xf4000000));
|
||||
set_le26(d, 0xff020304);
|
||||
assert(get_le26(d) == 0x03020304);
|
||||
assert(dd == ne32_to_le32(0xf7020304));
|
||||
assert_noexcept(get_le26(d) == 0x03020304);
|
||||
assert_noexcept(dd == ne32_to_le32(0xf7020304));
|
||||
}
|
||||
{
|
||||
upx_uint16_t a = 0;
|
||||
@@ -487,12 +488,12 @@ void upx_compiler_sanity_check(void) {
|
||||
set_ne16(&a, 0x04030201); // ignore upper bits
|
||||
set_ne32(&b, 0x04030201);
|
||||
set_ne64(&c, 0x0807060504030201ull);
|
||||
assert(a == 0x0201);
|
||||
assert(b == 0x04030201);
|
||||
assert(c == 0x0807060504030201ull);
|
||||
assert(get_ne16(&a) == 0x0201);
|
||||
assert(get_ne32(&b) == 0x04030201);
|
||||
assert(get_ne64(&c) == 0x0807060504030201ull);
|
||||
assert_noexcept(a == 0x0201);
|
||||
assert_noexcept(b == 0x04030201);
|
||||
assert_noexcept(c == 0x0807060504030201ull);
|
||||
assert_noexcept(get_ne16(&a) == 0x0201);
|
||||
assert_noexcept(get_ne32(&b) == 0x04030201);
|
||||
assert_noexcept(get_ne64(&c) == 0x0807060504030201ull);
|
||||
}
|
||||
#endif // DEBUG
|
||||
union {
|
||||
@@ -507,38 +508,53 @@ void upx_compiler_sanity_check(void) {
|
||||
LE32 l32;
|
||||
LE64 l64;
|
||||
} u;
|
||||
assert(testNoAliasing(&u.v_short, &u.b32));
|
||||
assert(testNoAliasing(&u.v_short, &u.l32));
|
||||
assert(testNoAliasing(&u.v_int, &u.b64));
|
||||
assert(testNoAliasing(&u.v_int, &u.l64));
|
||||
assert_noexcept(testNoAliasing(&u.v_short, &u.b32));
|
||||
assert_noexcept(testNoAliasing(&u.v_short, &u.l32));
|
||||
assert_noexcept(testNoAliasing(&u.v_int, &u.b64));
|
||||
assert_noexcept(testNoAliasing(&u.v_int, &u.l64));
|
||||
// check working -fno-strict-aliasing
|
||||
assert(testNoAliasing(&u.v_short, &u.v_int));
|
||||
assert(testNoAliasing(&u.v_int, &u.v_long));
|
||||
assert(testNoAliasing(&u.v_int, &u.v_llong));
|
||||
assert(testNoAliasing(&u.v_long, &u.v_llong));
|
||||
assert_noexcept(testNoAliasing(&u.v_short, &u.v_int));
|
||||
assert_noexcept(testNoAliasing(&u.v_int, &u.v_long));
|
||||
assert_noexcept(testNoAliasing(&u.v_int, &u.v_llong));
|
||||
assert_noexcept(testNoAliasing(&u.v_long, &u.v_llong));
|
||||
|
||||
assert(TestIntegerWrap<unsigned>::inc_gt(0));
|
||||
assert(!TestIntegerWrap<unsigned>::inc_gt(UINT_MAX));
|
||||
assert(TestIntegerWrap<unsigned>::dec_lt(1));
|
||||
assert(!TestIntegerWrap<unsigned>::dec_lt(0));
|
||||
assert(TestIntegerWrap<unsigned>::neg_eq(0));
|
||||
assert(!TestIntegerWrap<unsigned>::neg_eq(1));
|
||||
assert(!TestIntegerWrap<unsigned>::neg_eq(UINT_MAX));
|
||||
assert_noexcept(TestIntegerWrap<unsigned>::inc_gt(0));
|
||||
assert_noexcept(!TestIntegerWrap<unsigned>::inc_gt(UINT_MAX));
|
||||
assert_noexcept(TestIntegerWrap<unsigned>::dec_lt(1));
|
||||
assert_noexcept(!TestIntegerWrap<unsigned>::dec_lt(0));
|
||||
assert_noexcept(TestIntegerWrap<unsigned>::neg_eq(0));
|
||||
assert_noexcept(!TestIntegerWrap<unsigned>::neg_eq(1));
|
||||
assert_noexcept(!TestIntegerWrap<unsigned>::neg_eq(UINT_MAX));
|
||||
// check working -fno-strict-overflow
|
||||
assert(TestIntegerWrap<int>::inc_gt(0));
|
||||
assert(!TestIntegerWrap<int>::inc_gt(INT_MAX));
|
||||
assert(TestIntegerWrap<int>::dec_lt(0));
|
||||
assert(!TestIntegerWrap<int>::dec_lt(INT_MIN));
|
||||
assert(TestIntegerWrap<int>::neg_eq(0));
|
||||
assert(!TestIntegerWrap<int>::neg_eq(1));
|
||||
assert(!TestIntegerWrap<int>::neg_eq(INT_MAX));
|
||||
assert(TestIntegerWrap<int>::neg_eq(INT_MIN)); // !!
|
||||
assert_noexcept(TestIntegerWrap<int>::inc_gt(0));
|
||||
assert_noexcept(!TestIntegerWrap<int>::inc_gt(INT_MAX));
|
||||
assert_noexcept(TestIntegerWrap<int>::dec_lt(0));
|
||||
assert_noexcept(!TestIntegerWrap<int>::dec_lt(INT_MIN));
|
||||
assert_noexcept(TestIntegerWrap<int>::neg_eq(0));
|
||||
assert_noexcept(!TestIntegerWrap<int>::neg_eq(1));
|
||||
assert_noexcept(!TestIntegerWrap<int>::neg_eq(INT_MAX));
|
||||
assert_noexcept(TestIntegerWrap<int>::neg_eq(INT_MIN)); // !!
|
||||
}
|
||||
|
||||
/*************************************************************************
|
||||
// some doctest test cases
|
||||
**************************************************************************/
|
||||
|
||||
TEST_CASE("assert_noexcept") {
|
||||
// just to make sure that our assert macros don't generate any warnings
|
||||
byte dummy = 0;
|
||||
byte *ptr1 = &dummy;
|
||||
const byte *const ptr2 = &dummy;
|
||||
assert(true);
|
||||
assert(1);
|
||||
assert(ptr1);
|
||||
assert(ptr2);
|
||||
assert_noexcept(true);
|
||||
assert_noexcept(1);
|
||||
assert_noexcept(ptr1);
|
||||
assert_noexcept(ptr2);
|
||||
}
|
||||
|
||||
TEST_CASE("noncopyable") {
|
||||
struct Test : private noncopyable {
|
||||
int v = 1;
|
||||
|
||||
Reference in New Issue
Block a user