2021-07-01 12:02:39 +00:00
|
|
|
// -------------------------------------------------------------------------------------------------
|
|
|
|
// NOTE: Preprocessor Config
|
|
|
|
// -------------------------------------------------------------------------------------------------
|
|
|
|
/*
|
2021-09-24 13:41:58 +00:00
|
|
|
#define DQN_TEST_WITH_MAIN Define this to enable the main function and allow standalone compiling
|
|
|
|
and running of the file.
|
|
|
|
#define DQN_TEST_NO_COLORS Define this to disable any ANSI terminal color codes from output
|
2021-07-01 12:02:39 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#if defined(DQN_TEST_WITH_MAIN)
|
|
|
|
#define DQN_IMPLEMENTATION
|
2021-08-15 13:57:18 +00:00
|
|
|
#define DQN_WITH_CRT_ALLOCATOR // Dqn_CRTAllocator
|
|
|
|
#define DQN_WITH_DSMAP // Dqn_DSMap
|
|
|
|
#define DQN_WITH_FIXED_ARRAY // Dqn_FixedArray
|
|
|
|
#define DQN_WITH_FIXED_STRING // Dqn_FixedString
|
|
|
|
#define DQN_WITH_HEX // Dqn_Hex and friends ...
|
|
|
|
#define DQN_WITH_JSON_WRITER // Dqn_JsonWriter
|
|
|
|
#define DQN_WITH_MAP // Dqn_Map
|
|
|
|
#define DQN_WITH_MATH // Dqn_V2/3/4/Mat4 and friends ...
|
|
|
|
#define DQN_WITH_THREAD_CONTEXT // Dqn_ThreadContext and friends ...
|
2021-09-19 11:49:56 +00:00
|
|
|
#include "dqn.h"
|
|
|
|
|
|
|
|
#define DQN_KECCAK_IMPLEMENTATION
|
|
|
|
#include "dqn_keccak.h"
|
2021-07-01 12:02:39 +00:00
|
|
|
#endif
|
|
|
|
|
2021-09-19 11:49:56 +00:00
|
|
|
#include "dqn_tests_helpers.cpp"
|
|
|
|
|
2021-09-24 13:41:58 +00:00
|
|
|
#if defined(DQN_TEST_NO_COLORS)
|
|
|
|
#define DQN_TEST_COLOR_RED
|
|
|
|
#define DQN_TEST_COLOR_GREEN
|
|
|
|
#define DQN_TEST_COLOR_YELLOW
|
|
|
|
#define DQN_TEST_COLOR_BLUE
|
|
|
|
#define DQN_TEST_COLOR_MAGENTA
|
|
|
|
#define DQN_TEST_COLOR_CYAN
|
|
|
|
#define DQN_TEST_COLOR_RESET
|
2021-07-01 12:02:39 +00:00
|
|
|
#else
|
2021-09-24 13:41:58 +00:00
|
|
|
#define DQN_TEST_COLOR_RED "\x1b[31m"
|
|
|
|
#define DQN_TEST_COLOR_GREEN "\x1b[32m"
|
|
|
|
#define DQN_TEST_COLOR_YELLOW "\x1b[33m"
|
|
|
|
#define DQN_TEST_COLOR_BLUE "\x1b[34m"
|
|
|
|
#define DQN_TEST_COLOR_MAGENTA "\x1b[35m"
|
|
|
|
#define DQN_TEST_COLOR_CYAN "\x1b[36m"
|
|
|
|
#define DQN_TEST_COLOR_RESET "\x1b[0m"
|
2021-07-01 12:02:39 +00:00
|
|
|
#endif
|
|
|
|
|
2021-09-24 13:41:58 +00:00
|
|
|
#define DQN_TEST_GROUP(test, fmt, ...) \
|
|
|
|
fprintf(stdout, fmt "\n", ##__VA_ARGS__); \
|
2019-08-24 01:39:12 +00:00
|
|
|
DQN_DEFER \
|
|
|
|
{ \
|
2021-09-24 13:41:58 +00:00
|
|
|
bool all_clear = test.num_tests_ok_in_group == test.num_tests_in_group; \
|
|
|
|
fprintf(stdout, \
|
|
|
|
"%s\n %02d/%02d tests passed -- %s\n\n" DQN_TEST_COLOR_RESET, \
|
|
|
|
all_clear ? DQN_TEST_COLOR_GREEN : DQN_TEST_COLOR_RED, \
|
|
|
|
test.num_tests_ok_in_group, \
|
|
|
|
test.num_tests_in_group, \
|
|
|
|
all_clear ? "OK" : "FAILED"); \
|
2019-08-24 01:39:12 +00:00
|
|
|
}
|
|
|
|
|
2021-09-24 13:41:58 +00:00
|
|
|
#define DQN_TEST(test, fmt, ...) \
|
|
|
|
Dqn_TestBegin(&test, fmt, ##__VA_ARGS__); \
|
|
|
|
DQN_DEFER \
|
2019-08-24 01:39:12 +00:00
|
|
|
{ \
|
2021-09-24 13:41:58 +00:00
|
|
|
if (!test.failed) \
|
|
|
|
{ \
|
|
|
|
fprintf(stdout, DQN_TEST_COLOR_GREEN " OK" DQN_TEST_COLOR_RESET "\n"); \
|
|
|
|
test.num_tests_ok_in_group++; \
|
|
|
|
} \
|
|
|
|
};
|
2019-08-24 01:39:12 +00:00
|
|
|
|
2021-09-24 13:41:58 +00:00
|
|
|
#define DQN_TEST_ASSERT_MSG(test, expr, fmt, ...) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
if (!(expr)) \
|
|
|
|
{ \
|
|
|
|
if (!test.failed) \
|
|
|
|
{ \
|
|
|
|
fprintf(stdout, DQN_TEST_COLOR_RED " FAILED" DQN_TEST_COLOR_RESET "\n"); \
|
|
|
|
test.failed = true; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
fprintf(stderr, \
|
|
|
|
" File: %s:%d\n" \
|
|
|
|
" Expression: [" #expr "]\n" \
|
|
|
|
" Reason: " fmt "\n\n", \
|
|
|
|
__FILE__, \
|
|
|
|
__LINE__, \
|
|
|
|
##__VA_ARGS__); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define DQN_TEST_ASSERT(test, expr) DQN_TEST_ASSERT_MSG(test, expr, "")
|
|
|
|
|
|
|
|
struct Dqn_Test
|
2019-08-24 01:39:12 +00:00
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
int num_tests_in_group;
|
|
|
|
int num_tests_ok_in_group;
|
|
|
|
int failed;
|
|
|
|
};
|
2019-08-24 01:39:12 +00:00
|
|
|
|
2021-09-24 13:41:58 +00:00
|
|
|
void Dqn_TestBegin(Dqn_Test *state, char const *fmt, ...)
|
2019-08-24 01:39:12 +00:00
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
state->num_tests_in_group++;
|
|
|
|
state->failed = false;
|
2019-08-24 01:39:12 +00:00
|
|
|
|
2021-09-24 13:41:58 +00:00
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
2019-08-24 01:39:12 +00:00
|
|
|
|
2021-09-24 13:41:58 +00:00
|
|
|
int size_required = 0;
|
2019-08-24 01:39:12 +00:00
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
va_list args_copy;
|
|
|
|
va_copy(args_copy, args);
|
|
|
|
size_required = vsnprintf(nullptr, 0, fmt, args_copy);
|
|
|
|
va_end(args_copy);
|
2019-08-24 01:39:12 +00:00
|
|
|
}
|
2021-09-24 13:41:58 +00:00
|
|
|
|
|
|
|
printf(" ");
|
|
|
|
vprintf(fmt, args);
|
|
|
|
int const PAD_LENGTH = 90;
|
|
|
|
for (int pad = size_required; pad < PAD_LENGTH; pad++)
|
|
|
|
putc('.', stdout);
|
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
Dqn_Test Dqn_Test_Array()
|
2021-07-22 09:59:27 +00:00
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
Dqn_Test test = {};
|
|
|
|
DQN_TEST_GROUP(test, "Dqn_Array");
|
2021-09-24 12:29:34 +00:00
|
|
|
// NOTE: Dqn_ArrayInitWithMemory
|
2019-08-24 01:39:12 +00:00
|
|
|
{
|
2021-07-22 09:59:27 +00:00
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Fixed Memory: Test add single item and can't allocate more");
|
2021-07-22 09:59:27 +00:00
|
|
|
int memory[4] = {};
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_Array<int> array = Dqn_ArrayInitWithMemory(memory, Dqn_ArrayCount(memory), 0 /*size*/);
|
|
|
|
Dqn_ArrayAdd(&array, 1);
|
|
|
|
Dqn_ArrayAdd(&array, 2);
|
|
|
|
Dqn_ArrayAdd(&array, 3);
|
|
|
|
Dqn_ArrayAdd(&array, 4);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, array.data[0] == 1, "array.data %d", array.data[0]);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, array.data[1] == 2, "array.data %d", array.data[1]);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, array.data[2] == 3, "array.data %d", array.data[2]);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, array.data[3] == 4, "array.data %d", array.data[3]);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, array.size == 4, "array.size: %zu", array.size);
|
2021-07-22 09:59:27 +00:00
|
|
|
|
2021-09-24 12:29:34 +00:00
|
|
|
int *added_item = Dqn_ArrayAdd(&array, 5);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT(test, added_item == nullptr);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, array.size == 4, "array.size: %zu", array.size);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, array.max == 4, "array.max: %zu", array.max);
|
2021-07-22 09:59:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Fixed Memory: Test add array of items");
|
2021-07-22 09:59:27 +00:00
|
|
|
int memory[4] = {};
|
|
|
|
int DATA[] = {1, 2, 3};
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_Array<int> array = Dqn_ArrayInitWithMemory(memory, Dqn_ArrayCount(memory), 0 /*size*/);
|
|
|
|
Dqn_ArrayAddArray(&array, DATA, Dqn_ArrayCount(DATA));
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, array.data[0] == 1, "array.data %d", array.data[0]);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, array.data[1] == 2, "array.data %d", array.data[1]);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, array.data[2] == 3, "array.data %d", array.data[2]);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, array.size == 3, "array.size: %zu", array.size);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, array.max == 4, "array.max: %zu", array.max);
|
2021-07-22 09:59:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Fixed Memory: Test clear and clear with memory zeroed");
|
2021-07-22 09:59:27 +00:00
|
|
|
int memory[4] = {};
|
|
|
|
int DATA[] = {1, 2, 3};
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_Array<int> array = Dqn_ArrayInitWithMemory(memory, Dqn_ArrayCount(memory), 0 /*size*/);
|
|
|
|
Dqn_ArrayAddArray(&array, DATA, Dqn_ArrayCount(DATA));
|
|
|
|
Dqn_ArrayClear(&array, Dqn_ZeroMem::No);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, array.size == 0, "array.size: %zu", array.size);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, array.max == 4, "array.max: %zu", array.max);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, array.data[0] == 1, "array.data %d. Clear but don't zero memory so old values should still remain", array.data[0]);
|
2021-07-22 09:59:27 +00:00
|
|
|
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_ArrayClear(&array, Dqn_ZeroMem::Yes);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, array.data[0] == 0, "array.data %d. Clear but zero memory old values should not remain", array.data[0]);
|
2021-07-22 09:59:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Fixed Memory: Test erase stable and erase unstable");
|
2021-07-22 09:59:27 +00:00
|
|
|
int memory[4] = {};
|
|
|
|
int DATA[] = {1, 2, 3, 4};
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_Array<int> array = Dqn_ArrayInitWithMemory(memory, Dqn_ArrayCount(memory), 0 /*size*/);
|
|
|
|
Dqn_ArrayAddArray(&array, DATA, Dqn_ArrayCount(DATA));
|
|
|
|
Dqn_ArrayEraseUnstable(&array, 1);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, array.data[0] == 1, "array.data %d", array.data[0]);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, array.data[1] == 4, "array.data %d", array.data[1]);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, array.data[2] == 3, "array.data %d", array.data[2]);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, array.size == 3, "array.size: %zu", array.size);
|
2021-07-22 09:59:27 +00:00
|
|
|
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_ArrayEraseStable(&array, 0);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, array.data[0] == 4, "array.data: %d", array.data[0]);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, array.data[1] == 3, "array.data: %d", array.data[1]);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, array.size == 2, "array.size: %zu", array.size);
|
2021-07-22 09:59:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Fixed Memory: Test array pop and peek");
|
2021-07-22 09:59:27 +00:00
|
|
|
int memory[4] = {};
|
|
|
|
int DATA[] = {1, 2, 3};
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_Array<int> array = Dqn_ArrayInitWithMemory(memory, Dqn_ArrayCount(memory), 0 /*size*/);
|
|
|
|
Dqn_ArrayAddArray(&array, DATA, Dqn_ArrayCount(DATA));
|
|
|
|
Dqn_ArrayPop(&array, 2);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, array.data[0] == 1, "array.data: %d", array.data[0]);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, array.size == 1, "array.size: %zu", array.size);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, array.max == 4, "array.max: %zu", array.max);
|
2021-07-22 09:59:27 +00:00
|
|
|
|
2021-09-24 12:29:34 +00:00
|
|
|
int *peek_item = Dqn_ArrayPeek(&array);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, *peek_item == 1, "peek: %d", *peek_item);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, array.size == 1, "array.size: %zu", array.size);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, array.max == 4, "array.max: %zu", array.max);
|
2021-07-22 09:59:27 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-01 12:02:39 +00:00
|
|
|
|
2021-07-22 09:59:27 +00:00
|
|
|
// NOTE: Dynamic Memory: Dqn_Array
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Dynamic Memory: Reserve and check over commit reallocates");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_Arena arena = {};
|
2021-07-28 11:10:25 +00:00
|
|
|
Dqn_Array<int> array = {};
|
|
|
|
array.arena = &arena;
|
|
|
|
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_ArrayReserve(&array, 4);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, array.size == 0, "array.size: %zu", array.size);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, array.max == 4, "array.max: %zu", array.max);
|
2021-07-28 11:10:25 +00:00
|
|
|
|
|
|
|
int DATA[] = {1, 2, 3, 4};
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_ArrayAddArray(&array, DATA, Dqn_ArrayCount(DATA));
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, array.data[0] == 1, "array.data: %d", array.data[0]);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, array.data[1] == 2, "array.data: %d", array.data[1]);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, array.data[2] == 3, "array.data: %d", array.data[2]);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, array.data[3] == 4, "array.data: %d", array.data[3]);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, array.size == 4, "array.size: %zu", array.size);
|
2021-07-28 11:10:25 +00:00
|
|
|
|
2021-09-24 12:29:34 +00:00
|
|
|
int *added_item = Dqn_ArrayAdd(&array, 5);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, *added_item == 5, "added_item: %d", *added_item);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, array.data[4] == 5, "array.data: %d", array.data[4]);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, array.size == 5, "array.size: %zu", array.size);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, array.max >= 5, "array.max: %zu", array.max);
|
2021-07-28 11:10:25 +00:00
|
|
|
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_ArenaFree(&arena);
|
2021-07-22 09:59:27 +00:00
|
|
|
}
|
2021-09-24 13:41:58 +00:00
|
|
|
return test;
|
2021-07-22 09:59:27 +00:00
|
|
|
}
|
2019-08-24 01:39:12 +00:00
|
|
|
|
2021-09-24 13:41:58 +00:00
|
|
|
Dqn_Test Dqn_Test_File()
|
2021-08-15 13:57:18 +00:00
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
Dqn_Test test = {};
|
|
|
|
DQN_TEST_GROUP(test, "Dqn_File");
|
2021-08-15 13:57:18 +00:00
|
|
|
{
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_Arena arena = {};
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Make directory recursive \"abcd/efgh\"");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_b32 success = Dqn_FileMakeDir(DQN_STRING("abcd/efgh"), &arena);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT(test, success);
|
|
|
|
DQN_TEST_ASSERT(test, Dqn_FileDirExists(DQN_STRING("abcd")));
|
|
|
|
DQN_TEST_ASSERT(test, Dqn_FileDirExists(DQN_STRING("abcd/efgh")));
|
|
|
|
DQN_TEST_ASSERT_MSG(test, Dqn_FileExists(DQN_STRING("abcd")) == false, "This function should only return true for files");
|
|
|
|
DQN_TEST_ASSERT_MSG(test, Dqn_FileExists(DQN_STRING("abcd/efgh")) == false, "This function should only return true for files");
|
|
|
|
|
|
|
|
DQN_TEST_ASSERT(test, Dqn_FileDelete(DQN_STRING("abcd/efgh")));
|
|
|
|
DQN_TEST_ASSERT_MSG(test, Dqn_FileDelete(DQN_STRING("abcd")), "Failed to cleanup directory");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_ArenaFree(&arena);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// NOTE: Write step
|
|
|
|
Dqn_String const SRC_FILE = DQN_STRING("dqn_test_file");
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Write file, read it, copy it, move it and delete it");
|
2021-12-16 13:07:33 +00:00
|
|
|
Dqn_b32 write_result = Dqn_FileWriteFile(SRC_FILE.str, SRC_FILE.size, "test", 4);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT(test, write_result);
|
|
|
|
DQN_TEST_ASSERT(test, Dqn_FileExists(SRC_FILE));
|
2021-08-15 13:57:18 +00:00
|
|
|
|
|
|
|
// NOTE: Read step
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_Arena arena = {};
|
2021-12-16 13:07:33 +00:00
|
|
|
Dqn_String read_file = Dqn_FileArenaReadToString(SRC_FILE.str, SRC_FILE.size, &arena);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT(test, Dqn_StringIsValid(read_file));
|
|
|
|
DQN_TEST_ASSERT(test, read_file.size == 4);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, Dqn_StringEq(read_file, DQN_STRING("test")), "read(%zu): %.*s", read_file.size, DQN_STRING_FMT(read_file));
|
2021-08-15 13:57:18 +00:00
|
|
|
|
|
|
|
// NOTE: Copy step
|
|
|
|
Dqn_String const COPY_FILE = DQN_STRING("dqn_test_file_copy");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_b32 copy_result = Dqn_FileCopy(SRC_FILE, COPY_FILE, true /*overwrite*/);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT(test, copy_result);
|
|
|
|
DQN_TEST_ASSERT(test, Dqn_FileExists(COPY_FILE));
|
2021-08-15 13:57:18 +00:00
|
|
|
|
|
|
|
// NOTE: Move step
|
|
|
|
Dqn_String const MOVE_FILE = DQN_STRING("dqn_test_file_move");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_b32 move_result = Dqn_FileMove(COPY_FILE, MOVE_FILE, true /*overwrite*/);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT(test, move_result);
|
|
|
|
DQN_TEST_ASSERT(test, Dqn_FileExists(MOVE_FILE));
|
|
|
|
DQN_TEST_ASSERT_MSG(test, Dqn_FileExists(COPY_FILE) == false, "Moving a file should remove the original");
|
2021-08-15 13:57:18 +00:00
|
|
|
|
|
|
|
// NOTE: Delete step
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_b32 delete_src_file = Dqn_FileDelete(SRC_FILE);
|
|
|
|
Dqn_b32 delete_moved_file = Dqn_FileDelete(MOVE_FILE);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT(test, delete_src_file);
|
|
|
|
DQN_TEST_ASSERT(test, delete_moved_file);
|
2021-08-15 13:57:18 +00:00
|
|
|
|
|
|
|
// NOTE: Deleting non-existent file fails
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_b32 delete_non_existent_src_file = Dqn_FileDelete(SRC_FILE);
|
|
|
|
Dqn_b32 delete_non_existent_moved_file = Dqn_FileDelete(MOVE_FILE);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT(test, delete_non_existent_moved_file == false);
|
|
|
|
DQN_TEST_ASSERT(test, delete_non_existent_src_file == false);
|
2021-08-15 13:57:18 +00:00
|
|
|
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_ArenaFree(&arena);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
2021-09-24 13:41:58 +00:00
|
|
|
return test;
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
2021-09-24 13:41:58 +00:00
|
|
|
Dqn_Test Dqn_Test_FixedArray()
|
2021-07-22 09:59:27 +00:00
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
Dqn_Test test = {};
|
2021-08-15 13:57:18 +00:00
|
|
|
#if defined(DQN_WITH_FIXED_ARRAY)
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_GROUP(test, "Dqn_FixedArray");
|
2021-07-22 09:59:27 +00:00
|
|
|
// NOTE: Dqn_FixedArray_Init
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Initialise from raw array");
|
2021-07-22 09:59:27 +00:00
|
|
|
int raw_array[] = {1, 2};
|
|
|
|
auto array = Dqn_FixedArray_Init<int, 4>(raw_array, (int)Dqn_ArrayCount(raw_array));
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT(test, array.size == 2);
|
|
|
|
DQN_TEST_ASSERT(test, array[0] == 1);
|
|
|
|
DQN_TEST_ASSERT(test, array[1] == 2);
|
2021-07-22 09:59:27 +00:00
|
|
|
}
|
2021-07-01 12:02:39 +00:00
|
|
|
|
2021-07-22 09:59:27 +00:00
|
|
|
// NOTE: Dqn_FixedArray_EraseStable
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Erase stable 1 element from array");
|
2021-07-22 09:59:27 +00:00
|
|
|
int raw_array[] = {1, 2, 3};
|
|
|
|
auto array = Dqn_FixedArray_Init<int, 4>(raw_array, (int)Dqn_ArrayCount(raw_array));
|
|
|
|
Dqn_FixedArray_EraseStable(&array, 1);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT(test, array.size == 2);
|
|
|
|
DQN_TEST_ASSERT(test, array[0] == 1);
|
|
|
|
DQN_TEST_ASSERT(test, array[1] == 3);
|
2021-07-22 09:59:27 +00:00
|
|
|
}
|
2019-08-24 01:39:12 +00:00
|
|
|
|
2021-07-22 09:59:27 +00:00
|
|
|
// NOTE: Dqn_FixedArray_EraseUnstable
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Erase unstable 1 element from array");
|
2021-07-22 09:59:27 +00:00
|
|
|
int raw_array[] = {1, 2, 3};
|
|
|
|
auto array = Dqn_FixedArray_Init<int, 4>(raw_array, (int)Dqn_ArrayCount(raw_array));
|
|
|
|
Dqn_FixedArray_EraseUnstable(&array, 0);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT(test, array.size == 2);
|
|
|
|
DQN_TEST_ASSERT(test, array[0] == 3);
|
|
|
|
DQN_TEST_ASSERT(test, array[1] == 2);
|
2021-07-22 09:59:27 +00:00
|
|
|
}
|
2019-08-24 03:38:58 +00:00
|
|
|
|
2021-07-22 09:59:27 +00:00
|
|
|
// NOTE: Dqn_FixedArray_Add
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Add 1 element to array");
|
2021-07-22 09:59:27 +00:00
|
|
|
int const ITEM = 2;
|
|
|
|
int raw_array[] = {1};
|
|
|
|
auto array = Dqn_FixedArray_Init<int, 4>(raw_array, (int)Dqn_ArrayCount(raw_array));
|
|
|
|
Dqn_FixedArray_Add(&array, ITEM);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT(test, array.size == 2);
|
|
|
|
DQN_TEST_ASSERT(test, array[0] == 1);
|
|
|
|
DQN_TEST_ASSERT(test, array[1] == ITEM);
|
2021-07-22 09:59:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: Dqn_FixedArray_Clear
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Clear array");
|
2021-07-22 09:59:27 +00:00
|
|
|
int raw_array[] = {1};
|
|
|
|
auto array = Dqn_FixedArray_Init<int, 4>(raw_array, (int)Dqn_ArrayCount(raw_array));
|
|
|
|
Dqn_FixedArray_Clear(&array);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT(test, array.size == 0);
|
2021-07-22 09:59:27 +00:00
|
|
|
}
|
2021-08-15 13:57:18 +00:00
|
|
|
#endif // DQN_WITH_FIXED_ARRAY
|
2021-09-24 13:41:58 +00:00
|
|
|
return test;
|
2021-07-22 09:59:27 +00:00
|
|
|
}
|
2021-07-01 12:02:39 +00:00
|
|
|
|
2021-09-24 13:41:58 +00:00
|
|
|
Dqn_Test Dqn_Test_FixedString()
|
2021-07-22 09:59:27 +00:00
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
Dqn_Test test = {};
|
2021-08-15 13:57:18 +00:00
|
|
|
#if defined(DQN_WITH_FIXED_STRING)
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_GROUP(test, "Dqn_FixedString");
|
2019-08-24 03:38:58 +00:00
|
|
|
|
2021-09-24 12:29:34 +00:00
|
|
|
// NOTE: Dqn_FixedStringAppend
|
2021-07-22 09:59:27 +00:00
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Append too much fails");
|
2021-07-22 09:59:27 +00:00
|
|
|
Dqn_FixedString<4> str = {};
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, Dqn_FixedStringAppend(&str, "abcd") == false, "We need space for the null-terminator");
|
2019-08-24 01:39:12 +00:00
|
|
|
}
|
|
|
|
|
2021-09-24 12:29:34 +00:00
|
|
|
// NOTE: Dqn_FixedStringAppendFmt
|
2021-07-22 09:59:27 +00:00
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Append format string too much fails");
|
2021-07-22 09:59:27 +00:00
|
|
|
Dqn_FixedString<4> str = {};
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, Dqn_FixedStringAppendFmt(&str, "abcd") == false, "We need space for the null-terminator");
|
2021-07-22 09:59:27 +00:00
|
|
|
}
|
2021-08-15 13:57:18 +00:00
|
|
|
#endif // DQN_WITH_FIXED_STRING
|
2021-09-24 13:41:58 +00:00
|
|
|
return test;
|
2021-07-22 09:59:27 +00:00
|
|
|
}
|
|
|
|
|
2021-09-24 13:41:58 +00:00
|
|
|
Dqn_Test Dqn_Test_Hex()
|
2021-08-06 14:54:53 +00:00
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
Dqn_Test test = {};
|
2021-08-15 13:57:18 +00:00
|
|
|
#if defined(DQN_WITH_HEX)
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_GROUP(test, "Dqn_Hex");
|
2021-08-06 14:54:53 +00:00
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Convert 0x123");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_u64 result = Dqn_HexStringToU64(DQN_STRING("0x123"));
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, result == 0x123, "result: %zu", result);
|
2021-08-06 14:54:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Convert 0xFFFF");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_u64 result = Dqn_HexStringToU64(DQN_STRING("0xFFFF"));
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, result == 0xFFFF, "result: %zu", result);
|
2021-08-06 14:54:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Convert FFFF");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_u64 result = Dqn_HexStringToU64(DQN_STRING("FFFF"));
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, result == 0xFFFF, "result: %zu", result);
|
2021-08-06 14:54:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Convert abCD");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_u64 result = Dqn_HexStringToU64(DQN_STRING("abCD"));
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, result == 0xabCD, "result: %zu", result);
|
2021-08-06 14:54:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Convert 0xabCD");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_u64 result = Dqn_HexStringToU64(DQN_STRING("0xabCD"));
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, result == 0xabCD, "result: %zu", result);
|
2021-08-06 14:54:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Convert 0x");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_u64 result = Dqn_HexStringToU64(DQN_STRING("0x"));
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, result == 0x0, "result: %zu", result);
|
2021-08-06 14:54:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Convert 0X");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_u64 result = Dqn_HexStringToU64(DQN_STRING("0X"));
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, result == 0x0, "result: %zu", result);
|
2021-08-06 14:54:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Convert 3");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_u64 result = Dqn_HexStringToU64(DQN_STRING("3"));
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, result == 3, "result: %zu", result);
|
2021-08-06 14:54:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Convert f");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_u64 result = Dqn_HexStringToU64(DQN_STRING("f"));
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, result == 0xf, "result: %zu", result);
|
2021-08-06 14:54:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Convert g");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_u64 result = Dqn_HexStringToU64(DQN_STRING("g"));
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, result == 0, "result: %zu", result);
|
2021-08-06 14:54:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Convert -0x3");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_u64 result = Dqn_HexStringToU64(DQN_STRING("-0x3"));
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, result == 0, "result: %zu", result);
|
2021-08-06 14:54:53 +00:00
|
|
|
}
|
2021-08-15 13:57:18 +00:00
|
|
|
#endif // DQN_WITH_HEX
|
2021-09-24 13:41:58 +00:00
|
|
|
return test;
|
2021-08-06 14:54:53 +00:00
|
|
|
}
|
|
|
|
|
2021-09-24 13:41:58 +00:00
|
|
|
Dqn_Test Dqn_Test_M4()
|
2021-07-22 09:59:27 +00:00
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
Dqn_Test test = {};
|
2021-08-15 13:57:18 +00:00
|
|
|
#if defined(DQN_WITH_MATH)
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_GROUP(test, "Dqn_M4");
|
2021-07-22 09:59:27 +00:00
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Simple translate and scale matrix");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_M4 translate = Dqn_M4TranslateF(1, 2, 3);
|
|
|
|
Dqn_M4 scale = Dqn_M4ScaleF(2, 2, 2);
|
|
|
|
Dqn_M4 result = Dqn_M4Mul(translate, scale);
|
2021-07-22 09:59:27 +00:00
|
|
|
|
|
|
|
const Dqn_M4 EXPECT = {{
|
|
|
|
{2, 0, 0, 0},
|
|
|
|
{0, 2, 0, 0},
|
|
|
|
{0, 0, 2, 0},
|
|
|
|
{1, 2, 3, 1},
|
|
|
|
}};
|
|
|
|
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test,
|
2021-07-22 09:59:27 +00:00
|
|
|
memcmp(result.columns, EXPECT.columns, sizeof(EXPECT)) == 0,
|
|
|
|
"\nresult =\n%s\nexpected =\n%s",
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_M4ColumnMajorString(result).str,
|
|
|
|
Dqn_M4ColumnMajorString(EXPECT).str);
|
2021-07-22 09:59:27 +00:00
|
|
|
}
|
2021-08-15 13:57:18 +00:00
|
|
|
#endif // DQN_WITH_MATH
|
2021-09-24 13:41:58 +00:00
|
|
|
return test;
|
2021-07-22 09:59:27 +00:00
|
|
|
}
|
|
|
|
|
2021-09-24 13:41:58 +00:00
|
|
|
Dqn_Test Dqn_Test_DSMap()
|
2021-08-06 14:54:53 +00:00
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
Dqn_Test test = {};
|
2021-08-15 13:57:18 +00:00
|
|
|
#if defined(DQN_WITH_DSMAP)
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_GROUP(test, "Dqn_DSMap");
|
2021-08-06 14:54:53 +00:00
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Add r-value item to map");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_DSMap<int> map = Dqn_DSMapInit<int>(128);
|
|
|
|
Dqn_DSMapEntry<int> *entry = Dqn_DSMapAddCopy(&map, 3 /*hash*/, 5 /*value*/);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, map.size == 128, "size: %I64d", map.size);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, map.count == 1, "count: %zu", map.count);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, entry->hash == 3, "hash: %zu", entry->hash);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, entry->value == 5, "value: %d", entry->value);
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_DSMapFree(&map);
|
2021-08-06 14:54:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Add l-value item to map");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_DSMap<int> map = Dqn_DSMapInit<int>(128);
|
2021-08-06 14:54:53 +00:00
|
|
|
int value = 5;
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_DSMapEntry<int> *entry = Dqn_DSMapAdd(&map, 3 /*hash*/, value);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, map.size == 128, "size: %I64d", map.size);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, map.count == 1, "count: %zu", map.count);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, entry->hash == 3, "hash: %zu", entry->hash);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, entry->value == 5, "value: %d", entry->value);
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_DSMapFree(&map);
|
2021-08-06 14:54:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Get item from map");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_DSMap<int> map = Dqn_DSMapInit<int>(128);
|
|
|
|
Dqn_DSMapEntry<int> *entry = Dqn_DSMapAddCopy(&map, 3 /*hash*/, 5 /*value*/);
|
|
|
|
Dqn_DSMapEntry<int> *get_entry = Dqn_DSMapGet(&map, 3 /*hash*/);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, get_entry == entry, "get_entry: %p, entry: %p", get_entry, entry);
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_DSMapFree(&map);
|
2021-08-06 14:54:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Get non-existent item from map");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_DSMap<int> map = Dqn_DSMapInit<int>(128);
|
|
|
|
Dqn_DSMapEntry<int> *entry = Dqn_DSMapGet(&map, 3 /*hash*/);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT(test, entry == nullptr);
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_DSMapFree(&map);
|
2021-08-06 14:54:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Erase item from map");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_DSMap<int> map = Dqn_DSMapInit<int>(128);
|
|
|
|
Dqn_DSMapAddCopy(&map, 3 /*hash*/, 5 /*value*/);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, map.count == 1, "count: %I64d", map.count);
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_DSMapErase(&map, 3 /*hash*/, Dqn_ZeroMem::No);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, map.count == 0, "count: %I64d", map.count);
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_DSMapFree(&map);
|
2021-08-06 14:54:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Erase non-existent item from map");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_DSMap<int> map = Dqn_DSMapInit<int>(128);
|
|
|
|
Dqn_DSMapErase(&map, 3 /*hash*/, Dqn_ZeroMem::No);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, map.count == 0, "count: %I64d", map.count);
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_DSMapFree(&map);
|
2021-08-06 14:54:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Test resize on maximum load");
|
2021-08-06 14:54:53 +00:00
|
|
|
const Dqn_isize INIT_SIZE = 4;
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_DSMap<int> map = Dqn_DSMapInit<int>(INIT_SIZE);
|
|
|
|
Dqn_DSMapAddCopy(&map, 0 /*hash*/, 5 /*value*/);
|
|
|
|
Dqn_DSMapAddCopy(&map, 1 /*hash*/, 5 /*value*/);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, map.count == 2, "count: %I64d", map.count);
|
2021-08-06 14:54:53 +00:00
|
|
|
|
|
|
|
// This *should* cause a resize because 3/4 slots filled is 75% load
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_DSMapAddCopy(&map, 6 /*hash*/, 5 /*value*/);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, map.count == 3, "count: %I64d", map.count);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, map.size == INIT_SIZE * 2, "size: %I64d", map.size);
|
2021-08-06 14:54:53 +00:00
|
|
|
|
|
|
|
// Check that the elements are rehashed where we expected them to be
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT (test, map.slots[0].occupied == DQN_CAST(Dqn_u8)true);
|
|
|
|
DQN_TEST_ASSERT (test, map.slots[1].occupied == DQN_CAST(Dqn_u8)true);
|
|
|
|
DQN_TEST_ASSERT (test, map.slots[2].occupied == DQN_CAST(Dqn_u8)false);
|
|
|
|
DQN_TEST_ASSERT (test, map.slots[3].occupied == DQN_CAST(Dqn_u8)false);
|
|
|
|
DQN_TEST_ASSERT (test, map.slots[4].occupied == DQN_CAST(Dqn_u8)false);
|
|
|
|
DQN_TEST_ASSERT (test, map.slots[5].occupied == DQN_CAST(Dqn_u8)false);
|
|
|
|
DQN_TEST_ASSERT (test, map.slots[6].occupied == DQN_CAST(Dqn_u8)true);
|
|
|
|
DQN_TEST_ASSERT (test, map.slots[7].occupied == DQN_CAST(Dqn_u8)false);
|
|
|
|
|
|
|
|
DQN_TEST_ASSERT_MSG(test, map.slots[0].value == 5, "value: %d", map.slots[0].value);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, map.slots[1].value == 5, "value: %d", map.slots[1].value);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, map.slots[6].value == 5, "value: %d", map.slots[6].value);
|
2021-08-06 14:54:53 +00:00
|
|
|
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_DSMapFree(&map);
|
2021-08-06 14:54:53 +00:00
|
|
|
}
|
2021-08-15 13:57:18 +00:00
|
|
|
#endif // DQN_WITH_DSMAP
|
2021-09-24 13:41:58 +00:00
|
|
|
return test;
|
2021-08-06 14:54:53 +00:00
|
|
|
}
|
|
|
|
|
2021-09-24 13:41:58 +00:00
|
|
|
Dqn_Test Dqn_Test_Map()
|
2021-07-28 11:10:25 +00:00
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
Dqn_Test test = {};
|
2021-08-15 13:57:18 +00:00
|
|
|
#if defined(DQN_WITH_MAP)
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_GROUP(test, "Dqn_Map");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_Arena arena = {};
|
2021-07-28 11:10:25 +00:00
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Add r-value item to map");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_Map<int> map = Dqn_MapInitWithArena<int>(&arena, 1);
|
|
|
|
Dqn_MapEntry<int> *entry = Dqn_MapAddCopy(&map, 3 /*hash*/, 5 /*value*/, Dqn_MapCollideRule::Overwrite);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, map.size == 1, "size: %I64d", map.size);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, map.count == 1, "count: %zu", map.count);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, map.chain_count == 0, "chain_count: %zu", map.chain_count);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, map.free_list == nullptr, "free_list: %p", map.free_list);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, entry->hash == 3, "hash: %zu", entry->hash);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, entry->value == 5, "value: %d", entry->value);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, entry->next == nullptr, "next: %p", entry->next);
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_ArenaFree(&arena);
|
2021-07-28 11:10:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Add l-value item to map");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_Map<int> map = Dqn_MapInitWithArena<int>(&arena, 1);
|
2021-07-28 11:10:25 +00:00
|
|
|
int value = 5;
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_MapEntry<int> *entry = Dqn_MapAdd(&map, 3 /*hash*/, value, Dqn_MapCollideRule::Overwrite);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, map.size == 1, "size: %I64d", map.size);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, map.count == 1, "count: %zu", map.count);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, map.chain_count == 0, "chain_count: %zu", map.chain_count);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, map.free_list == nullptr, "free_list: %p", map.free_list);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, entry->hash == 3, "hash: %zu", entry->hash);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, entry->value == 5, "value: %d", entry->value);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, entry->next == nullptr, "next: %p", entry->next);
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_ArenaFree(&arena);
|
2021-07-28 11:10:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Add r-value item and overwrite on collision");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_Map<int> map = Dqn_MapInitWithArena<int>(&arena, 1);
|
|
|
|
Dqn_MapEntry<int> *entry_a = Dqn_MapAddCopy(&map, 3 /*hash*/, 5, Dqn_MapCollideRule::Overwrite);
|
|
|
|
Dqn_MapEntry<int> *entry_b = Dqn_MapAddCopy(&map, 4 /*hash*/, 6, Dqn_MapCollideRule::Overwrite);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, map.size == 1, "size: %zu", map.size);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, map.count == 1, "count: %zu", map.count);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, map.chain_count == 0, "chain_count: %zu", map.chain_count);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, map.free_list == nullptr, "free_list: %p", map.free_list);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, entry_a == entry_b, "Expected entry to be overwritten");
|
|
|
|
DQN_TEST_ASSERT_MSG(test, entry_b->hash == 4, "hash: %zu", entry_b->hash);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, entry_b->value == 6, "value: %d", entry_b->value);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, entry_b->next == nullptr, "next: %p", entry_b->next);
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_ArenaFree(&arena);
|
2021-07-28 11:10:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Add r-value item and fail on collision");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_Map<int> map = Dqn_MapInitWithArena<int>(&arena, 1);
|
|
|
|
Dqn_MapAddCopy(&map, 3 /*hash*/, 5, Dqn_MapCollideRule::Overwrite);
|
|
|
|
Dqn_MapEntry<int> *entry_b = Dqn_MapAddCopy(&map, 4 /*hash*/, 6, Dqn_MapCollideRule::Fail);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, entry_b == nullptr, "Expected entry to be overwritten");
|
|
|
|
DQN_TEST_ASSERT_MSG(test, map.size == 1, "size: %zu", map.size);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, map.count == 1, "count: %zu", map.count);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, map.chain_count == 0, "chain_count: %zu", map.chain_count);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, map.free_list == nullptr, "free_list: %p", map.free_list);
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_ArenaFree(&arena);
|
2021-07-28 11:10:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Add r-value item and chain on collision");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_Map<int> map = Dqn_MapInitWithArena<int>(&arena, 1);
|
|
|
|
Dqn_MapEntry<int> *entry_a = Dqn_MapAddCopy(&map, 3 /*hash*/, 5, Dqn_MapCollideRule::Overwrite);
|
|
|
|
Dqn_MapEntry<int> *entry_b = Dqn_MapAddCopy(&map, 4 /*hash*/, 6, Dqn_MapCollideRule::Chain);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, map.size == 1, "size: %zu", map.size);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, map.count == 1, "count: %zu", map.count);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, map.chain_count == 1, "chain_count: %zu", map.chain_count);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, map.free_list == nullptr, "free_list: %p", map.free_list);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, entry_a != entry_b, "Expected colliding entry to be chained");
|
|
|
|
DQN_TEST_ASSERT_MSG(test, entry_a->next == entry_b, "Expected chained entry to be next to our first map entry");
|
|
|
|
DQN_TEST_ASSERT_MSG(test, entry_b->hash == 4, "hash: %zu", entry_b->hash);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, entry_b->value == 6, "value: %d", entry_b->value);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, entry_b->next == nullptr, "next: %p", entry_b->next);
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_ArenaFree(&arena);
|
2021-07-28 11:10:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Add r-value item and get them back out again");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_Map<int> map = Dqn_MapInitWithArena<int>(&arena, 1);
|
|
|
|
Dqn_MapEntry<int> *entry_a = Dqn_MapAddCopy(&map, 3 /*hash*/, 5, Dqn_MapCollideRule::Overwrite);
|
|
|
|
Dqn_MapEntry<int> *entry_b = Dqn_MapAddCopy(&map, 4 /*hash*/, 6, Dqn_MapCollideRule::Chain);
|
2021-07-28 11:10:25 +00:00
|
|
|
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_MapEntry<int> *entry_a_copy = Dqn_MapGet(&map, 3 /*hash*/);
|
|
|
|
Dqn_MapEntry<int> *entry_b_copy = Dqn_MapGet(&map, 4 /*hash*/);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, map.size == 1, "size: %zu", map.size);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, map.count == 1, "count: %zu", map.count);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, map.chain_count == 1, "chain_count: %zu", map.chain_count);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, map.free_list == nullptr, "free_list: %p", map.free_list);
|
|
|
|
DQN_TEST_ASSERT(test, entry_a_copy == entry_a);
|
|
|
|
DQN_TEST_ASSERT(test, entry_b_copy == entry_b);
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_ArenaFree(&arena);
|
2021-07-28 11:10:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Add r-value item and erase it");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_Map<int> map = Dqn_MapInitWithArena<int>(&arena, 1);
|
|
|
|
Dqn_MapAddCopy(&map, 3 /*hash*/, 5, Dqn_MapCollideRule::Overwrite);
|
|
|
|
Dqn_MapAddCopy(&map, 4 /*hash*/, 6, Dqn_MapCollideRule::Chain);
|
|
|
|
Dqn_MapGet(&map, 3 /*hash*/);
|
2021-07-28 11:10:25 +00:00
|
|
|
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_MapErase(&map, 3 /*hash*/, Dqn_ZeroMem::No);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, map.size == 1, "size: %zu", map.size);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, map.count == 1, "count: %zu", map.count);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, map.chain_count == 0, "chain_count: %zu", map.chain_count);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, map.free_list != nullptr, "free_list: %p", map.free_list);
|
2021-07-28 11:10:25 +00:00
|
|
|
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, map.free_list->hash == 3, "Entry should not be zeroed out on erase");
|
|
|
|
DQN_TEST_ASSERT_MSG(test, map.free_list->value == 5, "Entry should not be zeroed out on erase");
|
|
|
|
DQN_TEST_ASSERT_MSG(test, map.free_list->next == nullptr, "This should be the first and only entry in the free list");
|
2021-07-28 11:10:25 +00:00
|
|
|
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_MapEntry<int> *entry = Dqn_MapGet(&map, 4 /*hash*/);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, entry->hash == 4, "hash: %zu", entry->hash);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, entry->value == 6, "value: %d", entry->value);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, entry->next == nullptr, "next: %p", entry->next);
|
2021-07-28 11:10:25 +00:00
|
|
|
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_ArenaFree(&arena);
|
2021-07-28 11:10:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Add r-value item and erase it, zeroing the memory out");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_Map<int> map = Dqn_MapInitWithArena<int>(&arena, 1);
|
|
|
|
Dqn_MapAddCopy(&map, 3 /*hash*/, 5, Dqn_MapCollideRule::Overwrite);
|
|
|
|
Dqn_MapAddCopy(&map, 4 /*hash*/, 6, Dqn_MapCollideRule::Chain);
|
|
|
|
Dqn_MapGet(&map, 3 /*hash*/);
|
2021-07-28 11:10:25 +00:00
|
|
|
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_MapErase(&map, 3 /*hash*/, Dqn_ZeroMem::Yes);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, map.size == 1, "size: %zu", map.size);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, map.count == 1, "count: %zu", map.count);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, map.chain_count == 0, "chain_count: %zu", map.chain_count);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, map.free_list != nullptr, "free_list: %p", map.free_list);
|
2021-07-28 11:10:25 +00:00
|
|
|
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, map.free_list->hash == 0, "Entry should be zeroed out on erase");
|
|
|
|
DQN_TEST_ASSERT_MSG(test, map.free_list->value == 0, "Entry should be zeroed out on erase");
|
|
|
|
DQN_TEST_ASSERT_MSG(test, map.free_list->next == nullptr, "This should be the first and only entry in the free list");
|
2021-07-28 11:10:25 +00:00
|
|
|
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_MapEntry<int> *entry = Dqn_MapGet(&map, 4 /*hash*/);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, entry->hash == 4, "hash: %zu", entry->hash);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, entry->value == 6, "value: %d", entry->value);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, entry->next == nullptr, "next: %p", entry->next);
|
2021-07-28 11:10:25 +00:00
|
|
|
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_ArenaFree(&arena);
|
2021-07-28 11:10:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(dqn): Test free list is chained correctly
|
|
|
|
// TODO(dqn): Test deleting 'b' from the list in the situation [map] - [a]->[b], we currently only test deleting a
|
2021-08-15 13:57:18 +00:00
|
|
|
#endif // DQN_WITH_MAP
|
2021-09-24 13:41:58 +00:00
|
|
|
return test;
|
2021-07-28 11:10:25 +00:00
|
|
|
}
|
|
|
|
|
2021-09-24 13:41:58 +00:00
|
|
|
Dqn_Test Dqn_Test_Intrinsics()
|
2021-07-22 15:35:08 +00:00
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
Dqn_Test test = {};
|
2021-07-28 11:10:25 +00:00
|
|
|
// TODO(dqn): We don't have meaningful tests here, but since
|
2021-07-22 15:35:08 +00:00
|
|
|
// atomics/intrinsics are implemented using macros we ensure the macro was
|
|
|
|
// written properly with these tests.
|
|
|
|
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_GROUP(test, "Dqn_Atomic");
|
2021-07-22 15:35:08 +00:00
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Dqn_AtomicAddU32");
|
2021-07-22 15:35:08 +00:00
|
|
|
Dqn_u32 val = 0;
|
|
|
|
Dqn_AtomicAddU32(&val, 1);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, val == 1, "val: %u", val);
|
2021-07-22 15:35:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Dqn_AtomicAddU64");
|
2021-07-22 15:35:08 +00:00
|
|
|
Dqn_u64 val = 0;
|
|
|
|
Dqn_AtomicAddU64(&val, 1);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, val == 1, "val: %zu", val);
|
2021-07-22 15:35:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Dqn_AtomicSubU32");
|
2021-07-22 15:35:08 +00:00
|
|
|
Dqn_u32 val = 1;
|
|
|
|
Dqn_AtomicSubU32(&val, 1);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, val == 0, "val: %u", val);
|
2021-07-22 15:35:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Dqn_AtomicSubU64");
|
2021-07-22 15:35:08 +00:00
|
|
|
Dqn_u64 val = 1;
|
|
|
|
Dqn_AtomicSubU64(&val, 1);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, val == 0, "val: %zu", val);
|
2021-07-22 15:35:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Dqn_AtomicSetValue32");
|
2021-07-22 15:35:08 +00:00
|
|
|
long a = 0;
|
|
|
|
long b = 111;
|
|
|
|
Dqn_AtomicSetValue32(&a, b);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, a == b, "a: %lu, b: %lu", a, b);
|
2021-07-22 15:35:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Dqn_AtomicSetValue64");
|
2021-07-22 15:35:08 +00:00
|
|
|
Dqn_i64 a = 0;
|
|
|
|
Dqn_i64 b = 111;
|
|
|
|
Dqn_AtomicSetValue64(&a, b);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, a == b, "a: %I64i, b: %I64i", a, b);
|
2021-07-22 15:35:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Dqn_CPUClockCycle");
|
2021-07-22 15:35:08 +00:00
|
|
|
Dqn_CPUClockCycle();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Dqn_CompilerReadBarrierAndCPUReadFence");
|
2021-07-22 15:35:08 +00:00
|
|
|
Dqn_CompilerReadBarrierAndCPUReadFence;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Dqn_CompilerWriteBarrierAndCPUWriteFence");
|
2021-07-22 15:35:08 +00:00
|
|
|
Dqn_CompilerWriteBarrierAndCPUWriteFence;
|
|
|
|
}
|
2021-09-24 13:41:58 +00:00
|
|
|
return test;
|
2021-07-22 15:35:08 +00:00
|
|
|
}
|
|
|
|
|
2021-09-24 13:41:58 +00:00
|
|
|
Dqn_Test Dqn_Test_Rect()
|
2021-07-22 09:59:27 +00:00
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
Dqn_Test test = {};
|
2021-08-15 13:57:18 +00:00
|
|
|
#if defined(DQN_WITH_MATH)
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_GROUP(test, "Dqn_Rect");
|
2021-09-24 12:29:34 +00:00
|
|
|
// NOTE: Dqn_RectIntersection
|
2019-08-24 01:39:12 +00:00
|
|
|
{
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "No intersection");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_Rect a = Dqn_RectInitFromPosAndSize(Dqn_V2(0, 0), Dqn_V2(100, 100));
|
|
|
|
Dqn_Rect b = Dqn_RectInitFromPosAndSize(Dqn_V2(200, 0), Dqn_V2(200, 200));
|
|
|
|
Dqn_Rect ab = Dqn_RectIntersection(a, b);
|
2021-07-22 09:59:27 +00:00
|
|
|
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test,
|
2021-07-22 09:59:27 +00:00
|
|
|
ab.min.x == 0 && ab.min.y == 0 && ab.max.x == 0 && ab.max.y == 0,
|
|
|
|
"ab = { min.x = %.2f, min.y = %.2f, max.x = %.2f. max.y = %.2f }",
|
|
|
|
ab.min.x,
|
|
|
|
ab.min.y,
|
|
|
|
ab.max.x,
|
|
|
|
ab.max.y);
|
2019-08-24 01:39:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "A's min intersects B");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_Rect a = Dqn_RectInitFromPosAndSize(Dqn_V2(50, 50), Dqn_V2(100, 100));
|
|
|
|
Dqn_Rect b = Dqn_RectInitFromPosAndSize(Dqn_V2( 0, 0), Dqn_V2(100, 100));
|
|
|
|
Dqn_Rect ab = Dqn_RectIntersection(a, b);
|
2021-07-22 09:59:27 +00:00
|
|
|
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test,
|
2021-07-22 09:59:27 +00:00
|
|
|
ab.min.x == 50 && ab.min.y == 50 && ab.max.x == 100 && ab.max.y == 100,
|
|
|
|
"ab = { min.x = %.2f, min.y = %.2f, max.x = %.2f. max.y = %.2f }",
|
|
|
|
ab.min.x,
|
|
|
|
ab.min.y,
|
|
|
|
ab.max.x,
|
|
|
|
ab.max.y);
|
2019-08-24 01:39:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "B's min intersects A");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_Rect a = Dqn_RectInitFromPosAndSize(Dqn_V2( 0, 0), Dqn_V2(100, 100));
|
|
|
|
Dqn_Rect b = Dqn_RectInitFromPosAndSize(Dqn_V2(50, 50), Dqn_V2(100, 100));
|
|
|
|
Dqn_Rect ab = Dqn_RectIntersection(a, b);
|
2021-07-22 09:59:27 +00:00
|
|
|
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test,
|
2021-07-22 09:59:27 +00:00
|
|
|
ab.min.x == 50 && ab.min.y == 50 && ab.max.x == 100 && ab.max.y == 100,
|
|
|
|
"ab = { min.x = %.2f, min.y = %.2f, max.x = %.2f. max.y = %.2f }",
|
|
|
|
ab.min.x,
|
|
|
|
ab.min.y,
|
|
|
|
ab.max.x,
|
|
|
|
ab.max.y);
|
2019-08-24 01:39:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "A's max intersects B");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_Rect a = Dqn_RectInitFromPosAndSize(Dqn_V2(-50, -50), Dqn_V2(100, 100));
|
|
|
|
Dqn_Rect b = Dqn_RectInitFromPosAndSize(Dqn_V2( 0, 0), Dqn_V2(100, 100));
|
|
|
|
Dqn_Rect ab = Dqn_RectIntersection(a, b);
|
2021-07-22 09:59:27 +00:00
|
|
|
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test,
|
2021-07-22 09:59:27 +00:00
|
|
|
ab.min.x == 0 && ab.min.y == 0 && ab.max.x == 50 && ab.max.y == 50,
|
|
|
|
"ab = { min.x = %.2f, min.y = %.2f, max.x = %.2f. max.y = %.2f }",
|
|
|
|
ab.min.x,
|
|
|
|
ab.min.y,
|
|
|
|
ab.max.x,
|
|
|
|
ab.max.y);
|
2019-08-24 01:39:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "B's max intersects A");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_Rect a = Dqn_RectInitFromPosAndSize(Dqn_V2( 0, 0), Dqn_V2(100, 100));
|
|
|
|
Dqn_Rect b = Dqn_RectInitFromPosAndSize(Dqn_V2(-50, -50), Dqn_V2(100, 100));
|
|
|
|
Dqn_Rect ab = Dqn_RectIntersection(a, b);
|
2021-07-22 09:59:27 +00:00
|
|
|
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test,
|
2021-07-22 09:59:27 +00:00
|
|
|
ab.min.x == 0 && ab.min.y == 0 && ab.max.x == 50 && ab.max.y == 50,
|
|
|
|
"ab = { min.x = %.2f, min.y = %.2f, max.x = %.2f. max.y = %.2f }",
|
|
|
|
ab.min.x,
|
|
|
|
ab.min.y,
|
|
|
|
ab.max.x,
|
|
|
|
ab.max.y);
|
2019-08-24 01:39:12 +00:00
|
|
|
}
|
2019-09-23 13:21:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "B contains A");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_Rect a = Dqn_RectInitFromPosAndSize(Dqn_V2(25, 25), Dqn_V2( 25, 25));
|
|
|
|
Dqn_Rect b = Dqn_RectInitFromPosAndSize(Dqn_V2( 0, 0), Dqn_V2(100, 100));
|
|
|
|
Dqn_Rect ab = Dqn_RectIntersection(a, b);
|
2021-07-22 09:59:27 +00:00
|
|
|
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test,
|
2021-07-22 09:59:27 +00:00
|
|
|
ab.min.x == 25 && ab.min.y == 25 && ab.max.x == 50 && ab.max.y == 50,
|
|
|
|
"ab = { min.x = %.2f, min.y = %.2f, max.x = %.2f. max.y = %.2f }",
|
|
|
|
ab.min.x,
|
|
|
|
ab.min.y,
|
|
|
|
ab.max.x,
|
|
|
|
ab.max.y);
|
2019-09-23 13:21:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "A contains B");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_Rect a = Dqn_RectInitFromPosAndSize(Dqn_V2( 0, 0), Dqn_V2(100, 100));
|
|
|
|
Dqn_Rect b = Dqn_RectInitFromPosAndSize(Dqn_V2(25, 25), Dqn_V2( 25, 25));
|
|
|
|
Dqn_Rect ab = Dqn_RectIntersection(a, b);
|
2021-07-22 09:59:27 +00:00
|
|
|
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test,
|
2021-07-22 09:59:27 +00:00
|
|
|
ab.min.x == 25 && ab.min.y == 25 && ab.max.x == 50 && ab.max.y == 50,
|
|
|
|
"ab = { min.x = %.2f, min.y = %.2f, max.x = %.2f. max.y = %.2f }",
|
|
|
|
ab.min.x,
|
|
|
|
ab.min.y,
|
|
|
|
ab.max.x,
|
|
|
|
ab.max.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "A equals B");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_Rect a = Dqn_RectInitFromPosAndSize(Dqn_V2(0, 0), Dqn_V2(100, 100));
|
2021-07-22 09:59:27 +00:00
|
|
|
Dqn_Rect b = a;
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_Rect ab = Dqn_RectIntersection(a, b);
|
2021-07-22 09:59:27 +00:00
|
|
|
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test,
|
2021-07-22 09:59:27 +00:00
|
|
|
ab.min.x == 0 && ab.min.y == 0 && ab.max.x == 100 && ab.max.y == 100,
|
|
|
|
"ab = { min.x = %.2f, min.y = %.2f, max.x = %.2f. max.y = %.2f }",
|
|
|
|
ab.min.x,
|
|
|
|
ab.min.y,
|
|
|
|
ab.max.x,
|
|
|
|
ab.max.y);
|
2019-09-23 13:21:14 +00:00
|
|
|
}
|
2019-08-24 01:39:12 +00:00
|
|
|
}
|
2021-08-15 13:57:18 +00:00
|
|
|
#endif // DQN_WITH_MATH
|
2021-09-24 13:41:58 +00:00
|
|
|
return test;
|
2021-07-22 09:59:27 +00:00
|
|
|
}
|
2019-08-24 01:39:12 +00:00
|
|
|
|
2021-09-24 13:41:58 +00:00
|
|
|
Dqn_Test Dqn_Test_PerfCounter()
|
2021-08-09 07:47:13 +00:00
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
Dqn_Test test = {};
|
|
|
|
DQN_TEST_GROUP(test, "Dqn_PerfCounter");
|
2021-08-09 07:47:13 +00:00
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Dqn_PerfCounterNow");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_u64 result = Dqn_PerfCounterNow();
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT(test, result != 0);
|
2021-08-09 07:47:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Consecutive ticks are ordered");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_u64 a = Dqn_PerfCounterNow();
|
|
|
|
Dqn_u64 b = Dqn_PerfCounterNow();
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, b >= a, "a: %zu, b: %zu", a, b);
|
2021-08-09 07:47:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Ticks to time are a correct order of magnitude");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_u64 a = Dqn_PerfCounterNow();
|
|
|
|
Dqn_u64 b = Dqn_PerfCounterNow();
|
2021-08-09 07:47:13 +00:00
|
|
|
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_f64 s = Dqn_PerfCounterS(a, b);
|
|
|
|
Dqn_f64 ms = Dqn_PerfCounterMs(a, b);
|
|
|
|
Dqn_f64 micro_s = Dqn_PerfCounterMicroS(a, b);
|
|
|
|
Dqn_f64 ns = Dqn_PerfCounterNs(a, b);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, s <= ms, "s: %f, ms: %f", s, ms);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, ms <= micro_s, "ms: %f, micro_s: %f", ms, micro_s);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, micro_s <= ns, "micro_s: %f, ns: %f", micro_s, ns);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
2021-09-24 13:41:58 +00:00
|
|
|
return test;
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
2021-09-24 13:41:58 +00:00
|
|
|
Dqn_Test Dqn_Test_OS()
|
2021-08-15 13:57:18 +00:00
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
Dqn_Test test = {};
|
|
|
|
DQN_TEST_GROUP(test, "Dqn_OS");
|
2021-08-15 13:57:18 +00:00
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Generate secure RNG bytes with nullptr");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_b32 result = Dqn_OSSecureRNGBytes(nullptr, 1);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT(test, result == false);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Generate secure RNG 32 bytes");
|
2021-08-15 13:57:18 +00:00
|
|
|
char const ZERO[32] = {};
|
|
|
|
char buf[32] = {};
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_b32 result = Dqn_OSSecureRNGBytes(buf, Dqn_ArrayCountI(buf));
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT(test, result);
|
|
|
|
DQN_TEST_ASSERT(test, DQN_MEMCMP(buf, ZERO, Dqn_ArrayCount(buf)) != 0);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Generate secure RNG 0 bytes");
|
2021-08-15 13:57:18 +00:00
|
|
|
char buf[32] = {};
|
|
|
|
buf[0] = 'Z';
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_b32 result = Dqn_OSSecureRNGBytes(buf, 0);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT(test, result);
|
|
|
|
DQN_TEST_ASSERT(test, buf[0] == 'Z');
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Query executable directory");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_Arena arena = {};
|
|
|
|
Dqn_String result = Dqn_OSExecutableDirectory(&arena);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT(test, Dqn_StringIsValid(result));
|
|
|
|
DQN_TEST_ASSERT_MSG(test, Dqn_FileDirExists(result), "result(%zu): %.*s", result.size, DQN_STRING_FMT(result));
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_ArenaFree(&arena);
|
2021-08-09 07:47:13 +00:00
|
|
|
}
|
2021-09-24 13:41:58 +00:00
|
|
|
return test;
|
2021-08-09 07:47:13 +00:00
|
|
|
}
|
|
|
|
|
2021-09-24 13:41:58 +00:00
|
|
|
Dqn_Test Dqn_Test_Str()
|
2021-07-22 09:59:27 +00:00
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
Dqn_Test test = {};
|
|
|
|
DQN_TEST_GROUP(test, "Dqn_Str");
|
2019-08-24 01:39:12 +00:00
|
|
|
// ---------------------------------------------------------------------------------------------
|
2021-09-24 12:29:34 +00:00
|
|
|
// NOTE: Dqn_CStringToI64
|
2019-08-24 01:39:12 +00:00
|
|
|
// ---------------------------------------------------------------------------------------------
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "To I64: Convert nullptr");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_i64 result = Dqn_CStringToI64(nullptr);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT(test, result == 0);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
2019-08-24 01:39:12 +00:00
|
|
|
|
2021-08-15 13:57:18 +00:00
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "To I64: Convert empty string");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_i64 result = Dqn_CStringToI64("");
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT(test, result == 0);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
2019-08-24 01:39:12 +00:00
|
|
|
|
2021-08-15 13:57:18 +00:00
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "To I64: Convert \"1\"");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_i64 result = Dqn_CStringToI64("1");
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT(test, result == 1);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
2019-08-24 01:39:12 +00:00
|
|
|
|
2021-08-15 13:57:18 +00:00
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "To I64: Convert \"-0\"");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_i64 result = Dqn_CStringToI64("-0");
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT(test, result == 0);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
2019-08-24 01:39:12 +00:00
|
|
|
|
2021-08-15 13:57:18 +00:00
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "To I64: Convert \"-1\"");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_i64 result = Dqn_CStringToI64("-1");
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT(test, result == -1);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
2019-08-24 01:39:12 +00:00
|
|
|
|
2021-08-15 13:57:18 +00:00
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "To I64: Convert \"1.2\"");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_i64 result = Dqn_CStringToI64("1.2");
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT(test, result == 1);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
2019-08-24 01:39:12 +00:00
|
|
|
|
2021-08-15 13:57:18 +00:00
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "To I64: Convert \"1,234\"");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_i64 result = Dqn_CStringToI64("1,234");
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT(test, result == 1234);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
2019-08-24 01:39:12 +00:00
|
|
|
|
2021-08-15 13:57:18 +00:00
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "To I64: Convert \"1,2\"");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_i64 result = Dqn_CStringToI64("1,2");
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT(test, result == 12);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
2019-08-24 01:39:12 +00:00
|
|
|
|
2021-08-15 13:57:18 +00:00
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "To I64: Convert \"12a3\"");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_i64 result = Dqn_CStringToI64("12a3");
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT(test, result == 12);
|
2019-08-24 01:39:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------------------------
|
2021-09-24 12:29:34 +00:00
|
|
|
// NOTE: Dqn_CStringToU64
|
2019-08-24 01:39:12 +00:00
|
|
|
// ---------------------------------------------------------------------------------------------
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "To U64: Convert nullptr");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_u64 result = Dqn_CStringToU64(nullptr);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, result == 0, "result: %zu", result);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
2019-08-24 01:39:12 +00:00
|
|
|
|
2021-08-15 13:57:18 +00:00
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "To U64: Convert empty string");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_u64 result = Dqn_CStringToU64("");
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, result == 0, "result: %zu", result);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
2019-08-24 01:39:12 +00:00
|
|
|
|
2021-08-15 13:57:18 +00:00
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "To U64: Convert \"1\"");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_u64 result = Dqn_CStringToU64("1");
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, result == 1, "result: %zu", result);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
2019-08-24 01:39:12 +00:00
|
|
|
|
2021-08-15 13:57:18 +00:00
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "To U64: Convert \"-0\"");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_u64 result = Dqn_CStringToU64("-0");
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, result == 0, "result: %zu", result);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
2019-08-24 01:39:12 +00:00
|
|
|
|
2021-08-15 13:57:18 +00:00
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "To U64: Convert \"-1\"");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_u64 result = Dqn_CStringToU64("-1");
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, result == 0, "result: %zu", result);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
2019-08-24 01:39:12 +00:00
|
|
|
|
2021-08-15 13:57:18 +00:00
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "To U64: Convert \"1.2\"");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_u64 result = Dqn_CStringToU64("1.2");
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, result == 1, "result: %zu", result);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
2019-08-24 01:39:12 +00:00
|
|
|
|
2021-08-15 13:57:18 +00:00
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "To U64: Convert \"1,234\"");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_u64 result = Dqn_CStringToU64("1,234");
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, result == 1234, "result: %zu", result);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
2019-08-24 01:39:12 +00:00
|
|
|
|
2021-08-15 13:57:18 +00:00
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "To U64: Convert \"1,2\"");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_u64 result = Dqn_CStringToU64("1,2");
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, result == 12, "result: %zu", result);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
2019-08-24 01:39:12 +00:00
|
|
|
|
2021-08-15 13:57:18 +00:00
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "To U64: Convert \"12a3\"");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_u64 result = Dqn_CStringToU64("12a3");
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, result == 12, "result: %zu", result);
|
2019-08-24 01:39:12 +00:00
|
|
|
}
|
2020-01-17 15:01:25 +00:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------------------------
|
2021-09-24 12:29:34 +00:00
|
|
|
// NOTE: Dqn_CStringFind
|
2020-01-17 15:01:25 +00:00
|
|
|
// ---------------------------------------------------------------------------------------------
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Find: String (char) is not in buffer");
|
2021-08-15 13:57:18 +00:00
|
|
|
char const buf[] = "836a35becd4e74b66a0d6844d51f1a63018c7ebc44cf7e109e8e4bba57eefb55";
|
|
|
|
char const find[] = "2";
|
2021-09-24 12:29:34 +00:00
|
|
|
char const *result = Dqn_CStringFind(buf, find, Dqn_CharCountI(buf), Dqn_CharCountI(find));
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT(test, result == nullptr);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
2020-01-17 15:01:25 +00:00
|
|
|
|
2021-08-15 13:57:18 +00:00
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Find: String (char) is in buffer");
|
2021-08-15 13:57:18 +00:00
|
|
|
char const buf[] = "836a35becd4e74b66a0d6844d51f1a63018c7ebc44cf7e109e8e4bba57eefb55";
|
|
|
|
char const find[] = "6";
|
2021-09-24 12:29:34 +00:00
|
|
|
char const *result = Dqn_CStringFind(buf, find, Dqn_CharCountI(buf), Dqn_CharCountI(find));
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT(test, result != nullptr);
|
|
|
|
DQN_TEST_ASSERT(test, result[0] == '6' && result[1] == 'a');
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------------------------
|
2021-09-24 12:29:34 +00:00
|
|
|
// NOTE: Dqn_CStringFileNameFromPath
|
2021-08-15 13:57:18 +00:00
|
|
|
// ---------------------------------------------------------------------------------------------
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "File name from Windows path");
|
2021-08-15 13:57:18 +00:00
|
|
|
Dqn_isize file_name_size = 0;
|
|
|
|
char const buf[] = "C:\\ABC\\test.exe";
|
2021-09-24 12:29:34 +00:00
|
|
|
char const *result = Dqn_CStringFileNameFromPath(buf, Dqn_CharCountI(buf), &file_name_size);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, file_name_size == 8, "size: %I64d", file_name_size);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, Dqn_StringInit(result, file_name_size) == DQN_STRING("test.exe"), "%.*s", (int)file_name_size, result);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "File name from Linux path");
|
2021-08-15 13:57:18 +00:00
|
|
|
Dqn_isize file_name_size = 0;
|
|
|
|
char const buf[] = "/ABC/test.exe";
|
2021-09-24 12:29:34 +00:00
|
|
|
char const *result = Dqn_CStringFileNameFromPath(buf, Dqn_CharCountI(buf), &file_name_size);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, file_name_size == 8, "size: %I64d", file_name_size);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, Dqn_StringInit(result, file_name_size) == DQN_STRING("test.exe"), "%.*s", (int)file_name_size, result);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------------------------
|
2021-09-24 12:29:34 +00:00
|
|
|
// NOTE: Dqn_CStringTrimPrefix
|
2021-08-15 13:57:18 +00:00
|
|
|
// ---------------------------------------------------------------------------------------------
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Trim prefix");
|
2021-08-15 13:57:18 +00:00
|
|
|
char const prefix[] = "@123";
|
|
|
|
char const buf[] = "@123string";
|
|
|
|
Dqn_isize trimmed_size = 0;
|
2021-09-24 12:29:34 +00:00
|
|
|
char const *result = Dqn_CStringTrimPrefix(buf, Dqn_CharCountI(buf), prefix, Dqn_CharCountI(prefix), &trimmed_size);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, trimmed_size == 6, "size: %I64d", trimmed_size);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, Dqn_StringInit(result, trimmed_size) == DQN_STRING("string"), "%.*s", (int)trimmed_size, result);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Trim prefix, nullptr trimmed size");
|
2021-08-15 13:57:18 +00:00
|
|
|
char const prefix[] = "@123";
|
|
|
|
char const buf[] = "@123string";
|
2021-09-24 12:29:34 +00:00
|
|
|
char const *result = Dqn_CStringTrimPrefix(buf, Dqn_CharCountI(buf), prefix, Dqn_CharCountI(prefix), nullptr);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT(test, result);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------------------------
|
2021-09-24 12:29:34 +00:00
|
|
|
// NOTE: Dqn_CStringIsAllDigits
|
2021-08-15 13:57:18 +00:00
|
|
|
// ---------------------------------------------------------------------------------------------
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Is all digits fails on non-digit string");
|
2021-08-15 13:57:18 +00:00
|
|
|
char const buf[] = "@123string";
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_b32 result = Dqn_CStringIsAllDigits(buf, Dqn_CharCountI(buf));
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT(test, result == false);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Is all digits fails on nullptr");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_b32 result = Dqn_CStringIsAllDigits(nullptr, 0);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT(test, result == false);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Is all digits fails on nullptr w/ size");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_b32 result = Dqn_CStringIsAllDigits(nullptr, 1);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT(test, result == false);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Is all digits fails on 0 size w/ string");
|
2021-08-15 13:57:18 +00:00
|
|
|
char const buf[] = "@123string";
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_b32 result = Dqn_CStringIsAllDigits(buf, 0);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT(test, result == false);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Is all digits success");
|
2021-08-15 13:57:18 +00:00
|
|
|
char const buf[] = "23";
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_b32 result = Dqn_CStringIsAllDigits(buf, Dqn_CharCountI(buf));
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT(test, DQN_CAST(bool)result == true);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Is all digits fails on whitespace");
|
2021-08-15 13:57:18 +00:00
|
|
|
char const buf[] = "23 ";
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_b32 result = Dqn_CStringIsAllDigits(buf, Dqn_CharCountI(buf));
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT(test, DQN_CAST(bool)result == false);
|
2021-07-01 12:02:39 +00:00
|
|
|
}
|
2021-09-24 13:41:58 +00:00
|
|
|
return test;
|
2021-07-22 09:59:27 +00:00
|
|
|
}
|
2021-07-01 12:02:39 +00:00
|
|
|
|
2021-09-24 13:41:58 +00:00
|
|
|
Dqn_Test Dqn_Test_String()
|
2021-07-28 11:10:25 +00:00
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
Dqn_Test test = {};
|
|
|
|
DQN_TEST_GROUP(test, "Dqn_String");
|
2021-07-28 11:10:25 +00:00
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Initialise with string literal w/ macro");
|
2021-07-28 11:10:25 +00:00
|
|
|
Dqn_String string = DQN_STRING("AB");
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, string.size == 2, "size: %I64d", string.size);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, string.cap == 2, "cap: %I64d", string.cap);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, string.str[0] == 'A', "string[0]: %c", string.str[0]);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, string.str[1] == 'B', "string[1]: %c", string.str[1]);
|
2021-07-28 11:10:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Initialise with format string");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_Arena arena = {};
|
|
|
|
Dqn_String string = Dqn_StringFmt(&arena, "%s", "AB");
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, string.size == 2, "size: %I64d", string.size);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, string.cap == 2, "cap: %I64d", string.cap);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, string.str[0] == 'A', "string[0]: %c", string.str[0]);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, string.str[1] == 'B', "string[1]: %c", string.str[1]);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, string.str[2] == 0, "string[2]: %c", string.str[2]);
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_ArenaFree(&arena);
|
2021-07-28 11:10:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Copy string");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_Arena arena = {};
|
2021-07-28 11:10:25 +00:00
|
|
|
Dqn_String string = DQN_STRING("AB");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_String copy = Dqn_StringCopy(string, &arena);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, copy.size == 2, "size: %I64d", copy.size);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, copy.cap == 2, "cap: %I64d", copy.cap);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, copy.str[0] == 'A', "copy[0]: %c", copy.str[0]);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, copy.str[1] == 'B', "copy[1]: %c", copy.str[1]);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, copy.str[2] == 0, "copy[2]: %c", copy.str[2]);
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_ArenaFree(&arena);
|
2021-07-28 11:10:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Trim whitespace around string");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_String string = Dqn_StringTrimWhitespaceAround(DQN_STRING(" AB "));
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, string.size == 2, "size: %I64d", string.size);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, string.cap == 2, "cap: %I64d", string.cap);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, string.str[0] == 'A', "string[0]: %c", string.str[0]);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, string.str[1] == 'B', "string[1]: %c", string.str[1]);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, string.str[2] == ' ', "string[1]: %c", string.str[1]);
|
2021-07-28 11:10:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Allocate string from arena");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_Arena arena = {};
|
|
|
|
Dqn_String string = Dqn_StringAllocate(&arena, 2, Dqn_ZeroMem::No);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, string.size == 0, "size: %I64d", string.size);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, string.cap == 2, "cap: %I64d", string.cap);
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_ArenaFree(&arena);
|
2021-07-28 11:10:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Append to allocated string");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_Arena arena = {};
|
|
|
|
Dqn_String string = Dqn_StringAllocate(&arena, 2, Dqn_ZeroMem::No);
|
|
|
|
Dqn_StringAppendFmt(&string, "%c", 'A');
|
|
|
|
Dqn_StringAppendFmt(&string, "%c", 'B');
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, string.size == 2, "size: %I64d", string.size);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, string.cap == 2, "cap: %I64d", string.cap);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, string.str[0] == 'A', "string[0]: %c", string.str[0]);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, string.str[1] == 'B', "string[1]: %c", string.str[1]);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, string.str[2] == 0, "string[2]: %c", string.str[2]);
|
2021-07-28 11:10:25 +00:00
|
|
|
}
|
2021-08-15 13:57:18 +00:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------------------------
|
2021-09-24 12:29:34 +00:00
|
|
|
// NOTE: Dqn_CStringTrim[Prefix/Suffix]
|
2021-08-15 13:57:18 +00:00
|
|
|
// ---------------------------------------------------------------------------------------------
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Trim prefix with matching prefix");
|
2021-08-15 13:57:18 +00:00
|
|
|
Dqn_String input = DQN_STRING("nft/abc");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_String result = Dqn_StringTrimPrefix(input, DQN_STRING("nft/"));
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, result == DQN_STRING("abc"), "%.*s", DQN_STRING_FMT(result));
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Trim prefix with non matching prefix");
|
2021-08-15 13:57:18 +00:00
|
|
|
Dqn_String input = DQN_STRING("nft/abc");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_String result = Dqn_StringTrimPrefix(input, DQN_STRING(" ft/"));
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, result == input, "%.*s", DQN_STRING_FMT(result));
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Trim suffix with matching suffix");
|
2021-08-15 13:57:18 +00:00
|
|
|
Dqn_String input = DQN_STRING("nft/abc");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_String result = Dqn_StringTrimSuffix(input, DQN_STRING("abc"));
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, result == DQN_STRING("nft/"), "%.*s", DQN_STRING_FMT(result));
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Trim suffix with non matching suffix");
|
2021-08-15 13:57:18 +00:00
|
|
|
Dqn_String input = DQN_STRING("nft/abc");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_String result = Dqn_StringTrimSuffix(input, DQN_STRING("ab"));
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, result == input, "%.*s", DQN_STRING_FMT(result));
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------------------------
|
2021-09-24 12:29:34 +00:00
|
|
|
// NOTE: Dqn_StringIsAllDigits
|
2021-08-15 13:57:18 +00:00
|
|
|
// ---------------------------------------------------------------------------------------------
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Is all digits fails on non-digit string");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_b32 result = Dqn_StringIsAllDigits(DQN_STRING("@123string"));
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT(test, result == false);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Is all digits fails on nullptr");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_b32 result = Dqn_StringIsAllDigits(Dqn_StringInit(nullptr, 0));
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT(test, result == false);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Is all digits fails on nullptr w/ size");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_b32 result = Dqn_StringIsAllDigits(Dqn_StringInit(nullptr, 1));
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT(test, result == false);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Is all digits fails on string w/ 0 size");
|
2021-08-15 13:57:18 +00:00
|
|
|
char const buf[] = "@123string";
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_b32 result = Dqn_StringIsAllDigits(Dqn_StringInit(buf, 0));
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT(test, result == false);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Is all digits success");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_b32 result = Dqn_StringIsAllDigits(DQN_STRING("23"));
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT(test, DQN_CAST(bool)result == true);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Is all digits fails on whitespace");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_b32 result = Dqn_StringIsAllDigits(DQN_STRING("23 "));
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT(test, DQN_CAST(bool)result == false);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
2021-09-24 13:41:58 +00:00
|
|
|
return test;
|
2021-07-28 11:10:25 +00:00
|
|
|
}
|
|
|
|
|
2021-09-24 13:41:58 +00:00
|
|
|
Dqn_Test Dqn_Test_TicketMutex()
|
2021-07-22 09:59:27 +00:00
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
Dqn_Test test = {};
|
|
|
|
DQN_TEST_GROUP(test, "Dqn_TicketMutex");
|
2021-07-22 09:59:27 +00:00
|
|
|
{
|
|
|
|
// TODO: We don't have a meaningful test but since atomics are
|
|
|
|
// implemented with a macro this ensures that we test that they are
|
|
|
|
// written correctly.
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Ticket mutex start and stop");
|
2021-07-22 09:59:27 +00:00
|
|
|
Dqn_TicketMutex mutex = {};
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_TicketMutexBegin(&mutex);
|
|
|
|
Dqn_TicketMutexEnd(&mutex);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT(test, mutex.ticket == mutex.serving);
|
2021-07-22 09:59:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Ticket mutex start and stop w/ advanced API");
|
2021-07-22 09:59:27 +00:00
|
|
|
Dqn_TicketMutex mutex = {};
|
2021-09-24 12:29:34 +00:00
|
|
|
unsigned int ticket_a = Dqn_TicketMutexMakeTicket(&mutex);
|
|
|
|
unsigned int ticket_b = Dqn_TicketMutexMakeTicket(&mutex);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT(test, DQN_CAST(bool)Dqn_TicketMutexCanLock(&mutex, ticket_b) == false);
|
|
|
|
DQN_TEST_ASSERT(test, DQN_CAST(bool)Dqn_TicketMutexCanLock(&mutex, ticket_a) == true);
|
2021-07-22 09:59:27 +00:00
|
|
|
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_TicketMutexBeginTicket(&mutex, ticket_a);
|
|
|
|
Dqn_TicketMutexEnd(&mutex);
|
|
|
|
Dqn_TicketMutexBeginTicket(&mutex, ticket_b);
|
|
|
|
Dqn_TicketMutexEnd(&mutex);
|
2021-07-22 09:59:27 +00:00
|
|
|
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT(test, mutex.ticket == mutex.serving);
|
|
|
|
DQN_TEST_ASSERT(test, mutex.ticket == ticket_b + 1);
|
2021-07-22 09:59:27 +00:00
|
|
|
}
|
2021-09-24 13:41:58 +00:00
|
|
|
return test;
|
2021-07-22 09:59:27 +00:00
|
|
|
}
|
|
|
|
|
2021-09-24 13:41:58 +00:00
|
|
|
Dqn_Test Dqn_Test_Win()
|
2021-08-15 13:57:18 +00:00
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
Dqn_Test test = {};
|
2021-08-15 13:57:18 +00:00
|
|
|
#if defined(DQN_OS_WIN32)
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_GROUP(test, "Dqn_Win");
|
2021-08-15 13:57:18 +00:00
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "UTF8 to wide character size calculation");
|
2021-09-24 12:29:34 +00:00
|
|
|
int result = Dqn_WinUTF8ToWCharSizeRequired(DQN_STRING("String"));
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, result == 6, "Size returned: %d. This size should be including the null-terminator", result);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "UTF8 to wide character");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_Arena arena = {};
|
2021-08-15 13:57:18 +00:00
|
|
|
Dqn_String const INPUT = DQN_STRING("String");
|
2021-09-24 12:29:34 +00:00
|
|
|
int size_required = Dqn_WinUTF8ToWCharSizeRequired(INPUT);
|
|
|
|
wchar_t *string = Dqn_ArenaNewArray(&arena, wchar_t, size_required + 1, Dqn_ZeroMem::No);
|
2021-08-15 13:57:18 +00:00
|
|
|
|
|
|
|
// Fill the string with error sentinels, which ensures the string is zero terminated
|
|
|
|
DQN_MEMSET(string, 'Z', size_required + 1);
|
|
|
|
|
2021-09-24 12:29:34 +00:00
|
|
|
int size_returned = Dqn_WinUTF8ToWChar(INPUT, string, size_required + 1);
|
2021-08-15 13:57:18 +00:00
|
|
|
wchar_t const EXPECTED[] = {L'S', L't', L'r', L'i', L'n', L'g', 0};
|
|
|
|
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, size_required == size_returned, "string_size: %d, result: %d", size_required, size_returned);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, size_returned == Dqn_ArrayCount(EXPECTED) - 1, "string_size: %d, expected: %zu", size_returned, sizeof(EXPECTED));
|
|
|
|
DQN_TEST_ASSERT(test, DQN_MEMCMP(EXPECTED, string, sizeof(EXPECTED)) == 0);
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_ArenaFree(&arena);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Wide char to UTF8 size calculation");
|
2021-09-24 12:29:34 +00:00
|
|
|
int result = Dqn_WinWCharToUTF8SizeRequired(DQN_STRINGW(L"String"));
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, result == 6, "Size returned: %d. This size should be including the null-terminator", result);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "Wide char to UTF8");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_Arena arena = {};
|
|
|
|
Dqn_StringW const INPUT = DQN_STRINGW(L"String");
|
|
|
|
int size_required = Dqn_WinWCharToUTF8SizeRequired(INPUT);
|
|
|
|
char *string = Dqn_ArenaNewArray(&arena, char, size_required + 1, Dqn_ZeroMem::No);
|
2021-08-15 13:57:18 +00:00
|
|
|
|
|
|
|
// Fill the string with error sentinels, which ensures the string is zero terminated
|
|
|
|
DQN_MEMSET(string, 'Z', size_required + 1);
|
|
|
|
|
2021-09-24 12:29:34 +00:00
|
|
|
int size_returned = Dqn_WinWCharToUTF8(INPUT, string, size_required + 1);
|
2021-08-15 13:57:18 +00:00
|
|
|
char const EXPECTED[] = {'S', 't', 'r', 'i', 'n', 'g', 0};
|
|
|
|
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG(test, size_required == size_returned, "string_size: %d, result: %d", size_required, size_returned);
|
|
|
|
DQN_TEST_ASSERT_MSG(test, size_returned == Dqn_ArrayCount(EXPECTED) - 1, "string_size: %d, expected: %zu", size_returned, sizeof(EXPECTED));
|
|
|
|
DQN_TEST_ASSERT(test, DQN_MEMCMP(EXPECTED, string, sizeof(EXPECTED)) == 0);
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_ArenaFree(&arena);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
#endif // DQN_OS_WIN32
|
2021-09-24 13:41:58 +00:00
|
|
|
|
|
|
|
return test;
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
2021-09-19 11:49:56 +00:00
|
|
|
#define DQN_TESTS_HASH_X_MACRO \
|
|
|
|
DQN_TESTS_HASH_X_ENTRY(SHA3_224, "SHA3-224") \
|
|
|
|
DQN_TESTS_HASH_X_ENTRY(SHA3_256, "SHA3-256") \
|
|
|
|
DQN_TESTS_HASH_X_ENTRY(SHA3_384, "SHA3-384") \
|
|
|
|
DQN_TESTS_HASH_X_ENTRY(SHA3_512, "SHA3-512") \
|
|
|
|
DQN_TESTS_HASH_X_ENTRY(Keccak_224, "Keccak-224") \
|
|
|
|
DQN_TESTS_HASH_X_ENTRY(Keccak_256, "Keccak-256") \
|
|
|
|
DQN_TESTS_HASH_X_ENTRY(Keccak_384, "Keccak-384") \
|
|
|
|
DQN_TESTS_HASH_X_ENTRY(Keccak_512, "Keccak-512") \
|
|
|
|
DQN_TESTS_HASH_X_ENTRY(Count, "Keccak-512")
|
|
|
|
|
|
|
|
enum Dqn_Tests__HashType
|
|
|
|
{
|
|
|
|
#define DQN_TESTS_HASH_X_ENTRY(enum_val, string) Hash_##enum_val,
|
|
|
|
DQN_TESTS_HASH_X_MACRO
|
|
|
|
#undef DQN_TESTS_HASH_X_ENTRY
|
|
|
|
};
|
|
|
|
|
|
|
|
Dqn_String const DQN_TESTS__HASH_STRING[] =
|
|
|
|
{
|
|
|
|
#define DQN_TESTS_HASH_X_ENTRY(enum_val, string) DQN_STRING(string),
|
|
|
|
DQN_TESTS_HASH_X_MACRO
|
|
|
|
#undef DQN_TESTS_HASH_X_ENTRY
|
|
|
|
};
|
|
|
|
|
2021-09-24 13:41:58 +00:00
|
|
|
void Dqn_Test__KeccakDispatch(Dqn_Test *test, int hash_type, Dqn_String input)
|
2021-09-19 11:49:56 +00:00
|
|
|
{
|
|
|
|
#if defined(DQN_KECCAK_H)
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_ThreadScratch scratch = Dqn_ThreadGetScratch();
|
|
|
|
Dqn_String input_hex = Dqn_HexBytesToHexStringArena(input.str, input.size, scratch.arena);
|
2021-09-19 11:49:56 +00:00
|
|
|
|
|
|
|
switch(hash_type)
|
|
|
|
{
|
|
|
|
case Hash_SHA3_224:
|
|
|
|
{
|
2021-12-16 13:07:33 +00:00
|
|
|
Dqn_KeccakBytes28 hash = Dqn_SHA3_224StringToBytes28(input);
|
2021-09-19 11:49:56 +00:00
|
|
|
Dqn_KeccakBytes28 expect;
|
|
|
|
FIPS202_SHA3_224(DQN_CAST(u8 *)input.str, input.size, (u8 *)expect.data);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG((*test),
|
2021-12-16 13:07:33 +00:00
|
|
|
Dqn_KeccakBytes28Equals(&hash, &expect),
|
2021-09-19 11:49:56 +00:00
|
|
|
"\ninput: %.*s"
|
|
|
|
"\nhash: %.*s"
|
|
|
|
"\nexpect: %.*s"
|
|
|
|
,
|
|
|
|
DQN_STRING_FMT(input_hex),
|
2021-12-16 13:07:33 +00:00
|
|
|
DQN_KECCAK_STRING56_FMT(Dqn_KeccakBytes28ToHex(&hash).str),
|
|
|
|
DQN_KECCAK_STRING56_FMT(Dqn_KeccakBytes28ToHex(&expect).str));
|
2021-09-19 11:49:56 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Hash_SHA3_256:
|
|
|
|
{
|
2021-12-16 13:07:33 +00:00
|
|
|
Dqn_KeccakBytes32 hash = Dqn_SHA3_256StringToBytes32(input);
|
2021-09-19 11:49:56 +00:00
|
|
|
Dqn_KeccakBytes32 expect;
|
|
|
|
FIPS202_SHA3_256(DQN_CAST(u8 *)input.str, input.size, (u8 *)expect.data);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG((*test),
|
2021-12-16 13:07:33 +00:00
|
|
|
Dqn_KeccakBytes32Equals(&hash, &expect),
|
2021-09-19 11:49:56 +00:00
|
|
|
"\ninput: %.*s"
|
|
|
|
"\nhash: %.*s"
|
|
|
|
"\nexpect: %.*s"
|
|
|
|
,
|
|
|
|
DQN_STRING_FMT(input_hex),
|
2021-12-16 13:07:33 +00:00
|
|
|
DQN_KECCAK_STRING64_FMT(Dqn_KeccakBytes32ToHex(&hash).str),
|
|
|
|
DQN_KECCAK_STRING64_FMT(Dqn_KeccakBytes32ToHex(&expect).str));
|
2021-09-19 11:49:56 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Hash_SHA3_384:
|
|
|
|
{
|
2021-12-16 13:07:33 +00:00
|
|
|
Dqn_KeccakBytes48 hash = Dqn_SHA3_384StringToBytes48(input);
|
2021-09-19 11:49:56 +00:00
|
|
|
Dqn_KeccakBytes48 expect;
|
|
|
|
FIPS202_SHA3_384(DQN_CAST(u8 *)input.str, input.size, (u8 *)expect.data);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG((*test),
|
2021-12-16 13:07:33 +00:00
|
|
|
Dqn_KeccakBytes48Equals(&hash, &expect),
|
2021-09-19 11:49:56 +00:00
|
|
|
"\ninput: %.*s"
|
|
|
|
"\nhash: %.*s"
|
|
|
|
"\nexpect: %.*s"
|
|
|
|
,
|
|
|
|
DQN_STRING_FMT(input_hex),
|
2021-12-16 13:07:33 +00:00
|
|
|
DQN_KECCAK_STRING96_FMT(Dqn_KeccakBytes48ToHex(&hash).str),
|
|
|
|
DQN_KECCAK_STRING96_FMT(Dqn_KeccakBytes48ToHex(&expect).str));
|
2021-09-19 11:49:56 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Hash_SHA3_512:
|
|
|
|
{
|
2021-12-16 13:07:33 +00:00
|
|
|
Dqn_KeccakBytes64 hash = Dqn_SHA3_512StringToBytes64(input);
|
2021-09-19 11:49:56 +00:00
|
|
|
Dqn_KeccakBytes64 expect;
|
|
|
|
FIPS202_SHA3_512(DQN_CAST(u8 *)input.str, input.size, (u8 *)expect.data);
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG((*test),
|
2021-12-16 13:07:33 +00:00
|
|
|
Dqn_KeccakBytes64Equals(&hash, &expect),
|
2021-09-19 11:49:56 +00:00
|
|
|
"\ninput: %.*s"
|
|
|
|
"\nhash: %.*s"
|
|
|
|
"\nexpect: %.*s"
|
|
|
|
,
|
|
|
|
DQN_STRING_FMT(input_hex),
|
2021-12-16 13:07:33 +00:00
|
|
|
DQN_KECCAK_STRING128_FMT(Dqn_KeccakBytes64ToHex(&hash).str),
|
|
|
|
DQN_KECCAK_STRING128_FMT(Dqn_KeccakBytes64ToHex(&expect).str));
|
2021-09-19 11:49:56 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Hash_Keccak_224:
|
|
|
|
{
|
2021-12-16 13:07:33 +00:00
|
|
|
Dqn_KeccakBytes28 hash = Dqn_Keccak224StringToBytes28(input);
|
2021-09-19 11:49:56 +00:00
|
|
|
Dqn_KeccakBytes28 expect;
|
|
|
|
Keccak(1152, 448, DQN_CAST(u8 *)input.str, input.size, 0x01, (u8 *)expect.data, sizeof(expect));
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG((*test),
|
2021-12-16 13:07:33 +00:00
|
|
|
Dqn_KeccakBytes28Equals(&hash, &expect),
|
2021-09-19 11:49:56 +00:00
|
|
|
"\ninput: %.*s"
|
|
|
|
"\nhash: %.*s"
|
|
|
|
"\nexpect: %.*s"
|
|
|
|
,
|
|
|
|
DQN_STRING_FMT(input_hex),
|
2021-12-16 13:07:33 +00:00
|
|
|
DQN_KECCAK_STRING56_FMT(Dqn_KeccakBytes28ToHex(&hash).str),
|
|
|
|
DQN_KECCAK_STRING56_FMT(Dqn_KeccakBytes28ToHex(&expect).str));
|
2021-09-19 11:49:56 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Hash_Keccak_256:
|
|
|
|
{
|
2021-12-16 13:07:33 +00:00
|
|
|
Dqn_KeccakBytes32 hash = Dqn_Keccak256StringToBytes32(input);
|
2021-09-19 11:49:56 +00:00
|
|
|
Dqn_KeccakBytes32 expect;
|
|
|
|
Keccak(1088, 512, DQN_CAST(u8 *)input.str, input.size, 0x01, (u8 *)expect.data, sizeof(expect));
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG((*test),
|
2021-12-16 13:07:33 +00:00
|
|
|
Dqn_KeccakBytes32Equals(&hash, &expect),
|
2021-09-19 11:49:56 +00:00
|
|
|
"\ninput: %.*s"
|
|
|
|
"\nhash: %.*s"
|
|
|
|
"\nexpect: %.*s"
|
|
|
|
,
|
|
|
|
DQN_STRING_FMT(input_hex),
|
2021-12-16 13:07:33 +00:00
|
|
|
DQN_KECCAK_STRING64_FMT(Dqn_KeccakBytes32ToHex(&hash).str),
|
|
|
|
DQN_KECCAK_STRING64_FMT(Dqn_KeccakBytes32ToHex(&expect).str));
|
2021-09-19 11:49:56 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Hash_Keccak_384:
|
|
|
|
{
|
2021-12-16 13:07:33 +00:00
|
|
|
Dqn_KeccakBytes48 hash = Dqn_Keccak384StringToBytes48(input);
|
2021-09-19 11:49:56 +00:00
|
|
|
Dqn_KeccakBytes48 expect;
|
|
|
|
Keccak(832, 768, DQN_CAST(u8 *)input.str, input.size, 0x01, (u8 *)expect.data, sizeof(expect));
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG((*test),
|
2021-12-16 13:07:33 +00:00
|
|
|
Dqn_KeccakBytes48Equals(&hash, &expect),
|
2021-09-19 11:49:56 +00:00
|
|
|
"\ninput: %.*s"
|
|
|
|
"\nhash: %.*s"
|
|
|
|
"\nexpect: %.*s"
|
|
|
|
,
|
|
|
|
DQN_STRING_FMT(input_hex),
|
2021-12-16 13:07:33 +00:00
|
|
|
DQN_KECCAK_STRING96_FMT(Dqn_KeccakBytes48ToHex(&hash).str),
|
|
|
|
DQN_KECCAK_STRING96_FMT(Dqn_KeccakBytes48ToHex(&expect).str));
|
2021-09-19 11:49:56 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Hash_Keccak_512:
|
|
|
|
{
|
2021-12-16 13:07:33 +00:00
|
|
|
Dqn_KeccakBytes64 hash = Dqn_Keccak512StringToBytes64(input);
|
2021-09-19 11:49:56 +00:00
|
|
|
Dqn_KeccakBytes64 expect;
|
|
|
|
Keccak(576, 1024, DQN_CAST(u8 *)input.str, input.size, 0x01, (u8 *)expect.data, sizeof(expect));
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_ASSERT_MSG((*test),
|
2021-12-16 13:07:33 +00:00
|
|
|
Dqn_KeccakBytes64Equals(&hash, &expect),
|
2021-09-19 11:49:56 +00:00
|
|
|
"\ninput: %.*s"
|
|
|
|
"\nhash: %.*s"
|
|
|
|
"\nexpect: %.*s"
|
|
|
|
,
|
|
|
|
DQN_STRING_FMT(input_hex),
|
2021-12-16 13:07:33 +00:00
|
|
|
DQN_KECCAK_STRING128_FMT(Dqn_KeccakBytes64ToHex(&hash).str),
|
|
|
|
DQN_KECCAK_STRING128_FMT(Dqn_KeccakBytes64ToHex(&expect).str));
|
2021-09-19 11:49:56 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
#endif // DQN_KECCAK_H
|
|
|
|
}
|
|
|
|
|
2021-09-24 13:41:58 +00:00
|
|
|
Dqn_Test Dqn_Test_Keccak()
|
2021-09-19 11:49:56 +00:00
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
Dqn_Test test = {};
|
2021-09-19 11:49:56 +00:00
|
|
|
#if defined(DQN_KECCAK_H)
|
|
|
|
Dqn_String const INPUTS[] = {
|
|
|
|
DQN_STRING("abc"),
|
|
|
|
DQN_STRING(""),
|
|
|
|
DQN_STRING("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"),
|
|
|
|
DQN_STRING("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmno"
|
|
|
|
"pqrstnopqrstu"),
|
|
|
|
};
|
|
|
|
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST_GROUP(test, "Dqn_Keccak");
|
2021-09-19 11:49:56 +00:00
|
|
|
for (int hash_type = 0; hash_type < Hash_Count; hash_type++)
|
|
|
|
{
|
|
|
|
pcg32_random_t rng = {};
|
|
|
|
pcg32_srandom_r(&rng, 0xd48e'be21'2af8'733d, 0x3f89'3bd2'd6b0'4eef);
|
|
|
|
|
|
|
|
for (Dqn_String input : INPUTS)
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "%.*s - Input: %.*s", DQN_STRING_FMT(DQN_TESTS__HASH_STRING[hash_type]), DQN_MIN(input.size, 54), input.str);
|
|
|
|
Dqn_Test__KeccakDispatch(&test, hash_type, input);
|
2021-09-19 11:49:56 +00:00
|
|
|
}
|
|
|
|
|
2021-09-24 13:41:58 +00:00
|
|
|
DQN_TEST(test, "%.*s - Deterministic random inputs", DQN_STRING_FMT(DQN_TESTS__HASH_STRING[hash_type]));
|
2021-09-19 11:49:56 +00:00
|
|
|
for (int index = 0; index < 128; index++)
|
|
|
|
{
|
|
|
|
char src[4096] = {};
|
|
|
|
Dqn_u32 src_size = pcg32_boundedrand_r(&rng, sizeof(src));
|
|
|
|
|
|
|
|
for (int src_index = 0; src_index < src_size; src_index++)
|
|
|
|
src[src_index] = pcg32_boundedrand_r(&rng, 255);
|
|
|
|
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_String input = Dqn_StringInit(src, src_size);
|
2021-09-24 13:41:58 +00:00
|
|
|
Dqn_Test__KeccakDispatch(&test, hash_type, input);
|
2021-09-19 11:49:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // DQN_KECCAK_H
|
2021-09-24 13:41:58 +00:00
|
|
|
return test;
|
2021-09-19 11:49:56 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 09:59:27 +00:00
|
|
|
void Dqn_Test_RunSuite()
|
|
|
|
{
|
2021-09-24 13:41:58 +00:00
|
|
|
Dqn_Test tests[]
|
|
|
|
{
|
|
|
|
Dqn_Test_Array(),
|
|
|
|
Dqn_Test_File(),
|
|
|
|
Dqn_Test_FixedArray(),
|
|
|
|
Dqn_Test_FixedString(),
|
|
|
|
Dqn_Test_Hex(),
|
|
|
|
Dqn_Test_Intrinsics(),
|
|
|
|
Dqn_Test_M4(),
|
|
|
|
Dqn_Test_DSMap(),
|
|
|
|
Dqn_Test_Map(),
|
|
|
|
Dqn_Test_Rect(),
|
|
|
|
Dqn_Test_PerfCounter(),
|
|
|
|
Dqn_Test_OS(),
|
|
|
|
Dqn_Test_Keccak(),
|
|
|
|
Dqn_Test_Str(),
|
|
|
|
Dqn_Test_String(),
|
|
|
|
Dqn_Test_TicketMutex(),
|
|
|
|
Dqn_Test_Win(),
|
|
|
|
};
|
|
|
|
|
|
|
|
int total_tests = 0;
|
|
|
|
int total_good_tests = 0;
|
|
|
|
for (Dqn_Test &test : tests)
|
|
|
|
{
|
|
|
|
total_tests += test.num_tests_in_group;
|
|
|
|
total_good_tests += test.num_tests_ok_in_group;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stdout, "Summary: %d/%d tests succeeded\n", total_good_tests, total_tests);
|
2019-08-24 01:39:12 +00:00
|
|
|
}
|
|
|
|
|
2021-07-01 12:02:39 +00:00
|
|
|
#if defined(DQN_TEST_WITH_MAIN)
|
2019-09-20 06:03:09 +00:00
|
|
|
int main(int argc, char *argv[])
|
2019-08-24 01:39:12 +00:00
|
|
|
{
|
|
|
|
(void)argv; (void)argc;
|
2021-07-22 09:59:27 +00:00
|
|
|
Dqn_Test_RunSuite();
|
2019-08-24 01:39:12 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2021-07-01 12:02:39 +00:00
|
|
|
#endif
|
2019-08-24 01:39:12 +00:00
|
|
|
|