1
0
Fork 0
mirror of https://github.com/Atmosphere-NX/Atmosphere.git synced 2024-11-29 23:32:23 +00:00

os: add unit tests for ExpandUnsignedValueToAscii

This commit is contained in:
Michael Scire 2021-09-29 13:55:40 -07:00
parent 15ff64e03a
commit 8fc7d715a5

View file

@ -18,25 +18,87 @@
namespace ams::os::impl { namespace ams::os::impl {
void ExpandUnsignedValueToAscii(char *dst, u64 value) { namespace {
/* Determine size needed. */
constexpr size_t SizeNeeded = (BITSIZEOF(value) / 4) * sizeof(char);
/* Write null-terminator. */ constexpr void ExpandUnsignedValueToAsciiImpl(char *dst, u64 value) {
dst[SizeNeeded] = '\x00'; /* Determine size needed. */
constexpr size_t SizeNeeded = (BITSIZEOF(value) / 4) * sizeof(char);
/* Write characters. */ /* Write null-terminator. */
for (size_t i = 0; i < SizeNeeded; ++i) { dst[SizeNeeded] = '\x00';
/* Determine current character. */
const auto nybble = (value & 0xF);
const char cur_c = (nybble < 10) ? ('0' + nybble) : ('A' + nybble - 10);
/* Write current character. */ /* Write characters. */
dst[SizeNeeded - 1 - i] = cur_c; for (size_t i = 0; i < SizeNeeded; ++i) {
/* Determine current character. */
const auto nybble = (value & 0xF);
const char cur_c = (nybble < 10) ? ('0' + nybble) : ('A' + nybble - 10);
/* Shift to next nybble. */ /* Write current character. */
value >>= 4; dst[SizeNeeded - 1 - i] = cur_c;
/* Shift to next nybble. */
value >>= 4;
}
} }
} }
void ExpandUnsignedValueToAscii(char *dst, u64 value) {
return ExpandUnsignedValueToAsciiImpl(dst, value);
}
/* Test correctness. */
#if 0
namespace {
consteval bool TestExpandUnsignedValueToAsciiImpl(const char *expected, u64 value) {
/* Create buffer. */
char buffer[17] = {
'\xCC', '\xCC', '\xCC', '\xCC', '\xCC', '\xCC', '\xCC', '\xCC', '\xCC', '\xCC', '\xCC', '\xCC', '\xCC', '\xCC', '\xCC', '\xCC', '\xCC'
};
/* Validate buffer is initially garbage. */
for (size_t i = 0; i < util::size(buffer); ++i) {
if (buffer[i] != '\xCC') {
return false;
}
}
/* Expand the value into the buffer. */
ExpandUnsignedValueToAsciiImpl(buffer, value);
/* Verify the result. */
for (size_t i = 0; i < util::size(buffer); ++i) {
if (buffer[i] != expected[i]) {
return false;
}
}
/* Verify null-termination. */
if (buffer[util::size(buffer) - 1] != 0) {
return false;
}
return true;
}
#define DO_TEST_EXPAND_UNSIGNED_VALUE_TO_ASCII_IMPL(v) static_assert(TestExpandUnsignedValueToAsciiImpl( #v , UINT64_C(0x##v)))
DO_TEST_EXPAND_UNSIGNED_VALUE_TO_ASCII_IMPL(0000000000000000);
DO_TEST_EXPAND_UNSIGNED_VALUE_TO_ASCII_IMPL(FFFFFFFFFFFFFFFF);
DO_TEST_EXPAND_UNSIGNED_VALUE_TO_ASCII_IMPL(FFFFFFFF00000000);
DO_TEST_EXPAND_UNSIGNED_VALUE_TO_ASCII_IMPL(00000000FFFFFFFF);
DO_TEST_EXPAND_UNSIGNED_VALUE_TO_ASCII_IMPL(FFFF0000FFFF0000);
DO_TEST_EXPAND_UNSIGNED_VALUE_TO_ASCII_IMPL(0000FFFF0000FFFF);
DO_TEST_EXPAND_UNSIGNED_VALUE_TO_ASCII_IMPL(F0F0F0F0F0F0F0F0);
DO_TEST_EXPAND_UNSIGNED_VALUE_TO_ASCII_IMPL(0F0F0F0F0F0F0F0F);
DO_TEST_EXPAND_UNSIGNED_VALUE_TO_ASCII_IMPL(0123456789ABCDEF);
DO_TEST_EXPAND_UNSIGNED_VALUE_TO_ASCII_IMPL(FEDCBA9876543210);
DO_TEST_EXPAND_UNSIGNED_VALUE_TO_ASCII_IMPL(A5A5A5A5A5A5A5A5);
#undef DO_TEST_EXPAND_UNSIGNED_VALUE_TO_ASCII_IMPL
}
#endif
} }