all: improve tests

This commit is contained in:
Markus F.X.J. Oberhumer
2024-04-16 15:20:56 +02:00
parent 35c6a200ad
commit 1d2b276425
11 changed files with 249 additions and 216 deletions
+17 -2
View File
@@ -248,10 +248,21 @@ TEST_CASE("upx::ObjectDeleter 1") {
LE16 *o = nullptr; // object
LE32 *a = nullptr; // array
{
const upx::ObjectDeleter<LE16 **> o_deleter{&o, 1};
upx::ObjectDeleter<LE16 **> o_deleter{&o, 1};
o = new LE16;
assert(o != nullptr);
const upx::ArrayDeleter<LE32 **> a_deleter{&a, 1};
upx::ArrayDeleter<LE32 **> a_deleter{&a, 1};
a = New(LE32, 1);
assert(a != nullptr);
}
assert(o == nullptr);
assert(a == nullptr);
// test "const" versions
{
const upx::ObjectDeleter<LE16 **const> o_deleter{&o, 1};
o = new LE16;
assert(o != nullptr);
const upx::ArrayDeleter<LE32 **const> a_deleter{&a, 1};
a = New(LE32, 1);
assert(a != nullptr);
}
@@ -355,6 +366,10 @@ struct TestTriBool {
static_assert(alignof(typename T::value_type) == alignof(typename T::underlying_type));
#if (ACC_ARCH_M68K && ACC_OS_TOS && ACC_CC_GNUC) && defined(__MINT__)
// broken compiler or broken ABI
#elif __GNUC__ == 7 && defined(__i386__) && !defined(__clang__)
static_assert(sizeof(T) == sizeof(typename T::underlying_type));
// gcc-7 "long long" enum align bug/ABI problem on i386
static_assert(alignof(T) <= alignof(typename T::underlying_type));
#else
static_assert(sizeof(T) == sizeof(typename T::underlying_type));
static_assert(alignof(T) == alignof(typename T::underlying_type));
+1 -1
View File
@@ -195,7 +195,7 @@ void throwAssertFailed(const char *expr, const char *file, int line, const char
}
}
const char *prettyName(const char *n) noexcept {
const char *prettyExceptionName(const char *n) noexcept {
if (n == nullptr)
return "(null)";
while (*n) {
+1 -1
View File
@@ -178,7 +178,7 @@ public:
// util
**************************************************************************/
const char *prettyName(const char *n) noexcept;
const char *prettyExceptionName(const char *n) noexcept;
noreturn void throwCantPack(const char *msg) may_throw;
noreturn void throwCantPackExact() may_throw;
+2 -2
View File
@@ -102,7 +102,7 @@ void printErr(const char *iname, const Throwable &e) noexcept {
char buf[1024];
size_t l;
upx_safe_snprintf(buf, sizeof(buf), "%s", prettyName(typeid(e).name()));
upx_safe_snprintf(buf, sizeof(buf), "%s", prettyExceptionName(typeid(e).name()));
l = strlen(buf);
if (l < sizeof(buf) && e.getMsg())
upx_safe_snprintf(buf + l, sizeof(buf) - l, ": %s", e.getMsg());
@@ -144,7 +144,7 @@ void printWarn(const char *iname, const char *format, ...) noexcept {
void printUnhandledException(const char *iname, const std::exception *e) noexcept {
if (e != nullptr)
printErr(iname, "unhandled exception: %s\n", prettyName(e->what()));
printErr(iname, "unhandled exception: %s\n", prettyExceptionName(e->what()));
else
printErr(iname, "internal error: unhandled exception!\n");
if (opt->cmd != CMD_COMPRESS) {
+5 -5
View File
@@ -512,10 +512,10 @@ void PeFile32::processRelocs() // pass1
infoWarning("skipping unsupported relocation type %d (%d)", ic, counts[ic]);
LE32 *fix[4];
upx::ArrayDeleter<LE32 **> fixdel{fix, 0}; // don't leak memory
upx::ArrayDeleter<LE32 **const> fix_deleter{fix, 0}; // don't leak memory
for (unsigned ic = 0; ic <= IMAGE_REL_BASED_HIGHLOW; ic++) {
fix[ic] = New(LE32, counts[ic]);
fixdel.count += 1;
fix_deleter.count += 1;
}
unsigned xcounts[4];
@@ -614,10 +614,10 @@ void PeFile64::processRelocs() // pass1
infoWarning("skipping unsupported relocation type %d (%d)", ic, counts[ic]);
LE32 *fix[16];
upx::ArrayDeleter<LE32 **> fixdel{fix, 0}; // don't leak memory
upx::ArrayDeleter<LE32 **const> fix_deleter{fix, 0}; // don't leak memory
for (unsigned ic = 0; ic < 16; ic++) {
fix[ic] = New(LE32, counts[ic]);
fixdel.count += 1;
fix_deleter.count += 1;
}
unsigned xcounts[16];
@@ -1924,7 +1924,7 @@ void PeFile::processResources(Resource *res) {
SPAN_S_VAR(byte, ores, oresources + res->dirsize());
char *keep_icons = nullptr; // icon ids in the first icon group
upx::ArrayDeleter<char **> keep_icons_deleter{&keep_icons, 1}; // don't leak memory
upx::ArrayDeleter<char **const> keep_icons_deleter{&keep_icons, 1}; // don't leak memory
unsigned iconsin1stdir = 0;
if (opt->win32_pe.compress_icons == 2)
while (res->next()) // there is no rewind() in Resource
+7 -7
View File
@@ -919,15 +919,15 @@ bool makebakname(char *ofilename, size_t size, const char *ifilename, bool force
**************************************************************************/
unsigned get_ratio(upx_uint64_t u_len, upx_uint64_t c_len) {
constexpr unsigned n = 1000 * 1000;
constexpr unsigned N = 1000 * 1000;
if (u_len == 0)
return c_len == 0 ? 0 : n;
upx_uint64_t x = c_len * n;
assert(x / n == c_len);
return c_len == 0 ? 0 : N;
upx_uint64_t x = c_len * N;
assert(x / N == c_len); // sanity check
x /= u_len;
x += 50; // rounding
if (x >= 10 * n) // >= "1000%"
x = 10 * n - 1;
x += 50; // rounding; cannot overflow
if (x >= 10 * N) // >= "1000%"
x = 10 * N - 1;
return ACC_ICONV(unsigned, x);
}