spiegel-keyman/core/tests/unit/kmnkbd/debug_items.hpp
Marc Durdin d71cb56ca7 fix(core): rename assert() to test_assert() in unit tests
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
2024-11-28 09:46:01 +07:00

190 lines
6.3 KiB
C++

#include <iostream>
#include "keyman_core.h"
#include "kmx/kmx_base.h"
#include "kmx/kmx_xstring.h"
#include <test_assert.h>
using namespace km::core::kmx;
namespace {
// TODO: consider using same const approach in kmx_base.h
// but this ripples through a whole lot of code
typedef struct tagDEBUG_GROUP
{
KMX_WCHAR const * dpName;
KEY const * dpKeyArray; // [LPKEY] address of first item in key array
KMX_WCHAR const * dpMatch;
KMX_WCHAR const * dpNoMatch;
KMX_DWORD cxKeyArray; // in array entries
KMX_BOOL fUsingKeys; // group(xx) [using keys] <-- specified or not
} DEBUG_GROUP, *LPDEBUG_GROUP;
typedef struct tagDEBUG_KEY
{
KMX_WCHAR Key;
KMX_DWORD Line;
KMX_DWORD ShiftFlags;
KMX_WCHAR const * dpOutput;
KMX_WCHAR const * dpContext;
} DEBUG_KEY, *LPDEBUG_KEY;
typedef struct tagDEBUG_STORE
{
KMX_DWORD dwSystemID;
KMX_WCHAR const * dpName;
KMX_WCHAR const * dpValue;
} DEBUG_STORE, *LPDEBUG_STORE;
const char *debug_item_types[] = {
"KM_CORE_DEBUG_BEGIN", // = 0,
"//KM_CORE_DEBUG_BEGIN_ANSI", // = 1, // not supported; instead rewrite ansi keyboards to Unicode with mcompile
"KM_CORE_DEBUG_GROUP_ENTER", // = 2,
"KM_CORE_DEBUG_GROUP_EXIT", // = 3,
"KM_CORE_DEBUG_RULE_ENTER", // = 4,
"KM_CORE_DEBUG_RULE_EXIT", // = 5,
"KM_CORE_DEBUG_MATCH_ENTER", // = 6,
"KM_CORE_DEBUG_MATCH_EXIT", // = 7,
"KM_CORE_DEBUG_NOMATCH_ENTER", // = 8,
"KM_CORE_DEBUG_NOMATCH_EXIT", // = 9,
"KM_CORE_DEBUG_END", // = 10,
"KM_CORE_DEBUG_SET_OPTION", // = 11
};
void print_debug_item(const char *title, km_core_state_debug_item const & item) {
LPGROUP gp = static_cast<LPGROUP>(item.kmx_info.group);
LPKEY rule = static_cast<LPKEY>(item.kmx_info.rule);
LPSTORE store = static_cast<LPSTORE>(item.kmx_info.option.store);
std::cout
<< "debug_item " << title << std::endl
<< " type: " << debug_item_types[item.type] << std::endl
<< " flags: " << item.flags << std::endl
<< " key_info: (vk: " << item.key_info.vk
<< " mod: " << item.key_info.modifier_state
<< " char: " << item.key_info.character << ")" << std::endl;
std::cout
<< " kmx_info: "<< std::endl
<< " context: '" << item.kmx_info.context << "'" << std::endl
<< " action: " << item.kmx_info.first_action << std::endl;
if(gp) std::cout
<< " group: " << gp->dpName << std::endl;
if(rule) std::cout
<< " rule: " << std::endl
<< " key: " << rule->Key << std::endl
<< " shift: " << rule->ShiftFlags << std::endl
<< " line: " << rule->Line << std::endl;
std::cout
<< " offsets: ";
std::copy(
std::begin(item.kmx_info.store_offsets),
std::end(item.kmx_info.store_offsets),
std::ostream_iterator<uint16_t>(std::cout, " ")
);
std::cout << std::endl;
if(store) std::cout
<< " option.store: " << std::endl
<< " name: " << store->dpName << std::endl
<< " value: " << store->dpString << std::endl;
if(store) std::cout
<< " option.value: " << item.kmx_info.option.value << std::endl;
}
bool are_store_offsets_equal(const uint16_t (&lhs)[DEBUG_STORE_OFFSETS_SIZE], const uint16_t (&rhs)[DEBUG_STORE_OFFSETS_SIZE]) {
return std::equal(std::begin(lhs), std::end(lhs), std::begin(rhs));
// !memcmp(lhs, rhs, sizeof(lhs));
}
bool operator==(
km_core_state_debug_item const & lhs,
km_core_state_debug_item const & rhs
) {
auto result = (lhs.type == rhs.type && lhs.flags == rhs.flags && lhs.kmx_info.first_action == rhs.kmx_info.first_action);
if(result) {
LPGROUP lgp = static_cast<LPGROUP>(lhs.kmx_info.group), rgp = static_cast<LPGROUP>(rhs.kmx_info.group);
LPKEY lrule = static_cast<LPKEY>(lhs.kmx_info.rule), rrule = static_cast<LPKEY>(rhs.kmx_info.rule);
LPSTORE loption_store = static_cast<LPSTORE>(lhs.kmx_info.option.store), roption_store = static_cast<LPSTORE>(rhs.kmx_info.option.store);
switch(lhs.type) {
case KM_CORE_DEBUG_BEGIN:
result = lhs.key_info.character == rhs.key_info.character &&
lhs.key_info.modifier_state == rhs.key_info.modifier_state &&
lhs.key_info.vk == rhs.key_info.vk;
break;
case KM_CORE_DEBUG_END:
break;
case KM_CORE_DEBUG_GROUP_ENTER:
case KM_CORE_DEBUG_GROUP_EXIT:
case KM_CORE_DEBUG_MATCH_ENTER:
case KM_CORE_DEBUG_MATCH_EXIT:
case KM_CORE_DEBUG_NOMATCH_ENTER:
case KM_CORE_DEBUG_NOMATCH_EXIT:
test_assert(lgp != nullptr);
test_assert(rgp != nullptr);
test_assert(lgp->dpName != nullptr);
test_assert(rgp->dpName != nullptr);
result = u16cmp(lgp->dpName, rgp->dpName) == 0;
break;
case KM_CORE_DEBUG_RULE_ENTER:
case KM_CORE_DEBUG_RULE_EXIT:
test_assert(lgp != nullptr);
test_assert(rgp != nullptr);
test_assert(lgp->dpName != nullptr);
test_assert(rgp->dpName != nullptr);
test_assert(lrule != nullptr);
test_assert(rrule != nullptr);
result = u16cmp(lgp->dpName, rgp->dpName) == 0 &&
lrule->Line == rrule->Line &&
lrule->Key == rrule->Key &&
lrule->ShiftFlags == rrule->ShiftFlags &&
u16cmp(lhs.kmx_info.context, rhs.kmx_info.context) == 0 &&
are_store_offsets_equal(lhs.kmx_info.store_offsets, rhs.kmx_info.store_offsets);
break;
case KM_CORE_DEBUG_SET_OPTION:
test_assert(loption_store != nullptr && roption_store != nullptr);
result =
u16cmp(loption_store->dpName, roption_store->dpName) == 0 &&
u16cmp(lhs.kmx_info.option.value, rhs.kmx_info.option.value) == 0;
break;
default:
test_assert(false);
result = false;
}
}
if(!result) {
print_debug_item("actual", lhs);
print_debug_item("expected", rhs);
}
return result;
}
bool debug_items(
km_core_state const * state,
std::initializer_list<km_core_state_debug_item> const & expected
) {
size_t n = 0;
auto act = km_core_state_debug_items(state, &n);
for (auto &rhs: expected) {
if ((int)--n < 0) {
std::cout << "expected longer than actual" << std::endl;
print_debug_item("next expected item:", rhs);
return false;
}
if (!(*act++ == rhs)) return false;
}
if(n != 0) {
std::cout << "actual longer than expected" << std::endl;
print_debug_item("next actual item:", *act);
return false;
}
return true;
}
};