2021-07-01 12:02:39 +00:00
|
|
|
// -------------------------------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// NOTE: Preprocessor Config
|
|
|
|
//
|
|
|
|
// -------------------------------------------------------------------------------------------------
|
|
|
|
/*
|
|
|
|
#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_ANSI_COLORS Define this to disable any ANSI terminal color codes from output
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if defined(DQN_TEST_WITH_MAIN)
|
|
|
|
#define DQN_IMPLEMENTATION
|
|
|
|
#include "Dqn.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct Dqn_TestState
|
2019-08-24 01:39:12 +00:00
|
|
|
{
|
2020-02-22 14:19:59 +00:00
|
|
|
int indent_level;
|
|
|
|
Dqn_String name;
|
|
|
|
Dqn_String fail_expr;
|
|
|
|
Dqn_String fail_msg;
|
|
|
|
bool scope_started;
|
2019-08-24 01:39:12 +00:00
|
|
|
};
|
|
|
|
|
2021-07-01 12:02:39 +00:00
|
|
|
struct Dqn_TestingState
|
2019-08-24 01:39:12 +00:00
|
|
|
{
|
2020-07-16 14:12:27 +00:00
|
|
|
int num_tests_in_group;
|
|
|
|
int num_tests_ok_in_group;
|
2021-07-01 12:02:39 +00:00
|
|
|
Dqn_TestState test;
|
|
|
|
Dqn_ArenaAllocator arena;
|
2019-08-24 01:39:12 +00:00
|
|
|
};
|
|
|
|
|
2021-07-28 11:10:25 +00:00
|
|
|
static int g_dqn_test_total_good_tests;
|
|
|
|
static int g_dqn_test_total_tests;
|
|
|
|
|
2021-07-01 12:02:39 +00:00
|
|
|
#if defined(DQN_TEST_NO_ANSI_COLORS)
|
|
|
|
#define DQN_TEST_ANSI_COLOR_RED
|
|
|
|
#define DQN_TEST_ANSI_COLOR_GREEN
|
|
|
|
#define DQN_TEST_ANSI_COLOR_YELLOW
|
|
|
|
#define DQN_TEST_ANSI_COLOR_BLUE
|
|
|
|
#define DQN_TEST_ANSI_COLOR_MAGENTA
|
|
|
|
#define DQN_TEST_ANSI_COLOR_CYAN
|
|
|
|
#define DQN_TEST_ANSI_COLOR_RESET
|
|
|
|
#else
|
|
|
|
#define DQN_TEST_ANSI_COLOR_RED "\x1b[31m"
|
|
|
|
#define DQN_TEST_ANSI_COLOR_GREEN "\x1b[32m"
|
|
|
|
#define DQN_TEST_ANSI_COLOR_YELLOW "\x1b[33m"
|
|
|
|
#define DQN_TEST_ANSI_COLOR_BLUE "\x1b[34m"
|
|
|
|
#define DQN_TEST_ANSI_COLOR_MAGENTA "\x1b[35m"
|
|
|
|
#define DQN_TEST_ANSI_COLOR_CYAN "\x1b[36m"
|
|
|
|
#define DQN_TEST_ANSI_COLOR_RESET "\x1b[0m"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define DQN_TEST_START_SCOPE(testing_state, test_name) \
|
2019-08-24 01:39:12 +00:00
|
|
|
DQN_DEFER \
|
|
|
|
{ \
|
2020-07-17 14:49:21 +00:00
|
|
|
if (testing_state.test.fail_expr.size == 0) testing_state.num_tests_ok_in_group++; \
|
2021-07-01 12:02:39 +00:00
|
|
|
Dqn_TestState_PrintResult(&testing_state.test); \
|
|
|
|
Dqn_ArenaAllocator_ResetUsage(&testing_state.arena, Dqn_ZeroMem::No); \
|
|
|
|
testing_state.test = {}; \
|
2019-08-24 01:39:12 +00:00
|
|
|
}; \
|
2021-07-01 12:02:39 +00:00
|
|
|
testing_state.test.name = DQN_STRING(test_name); \
|
2019-08-24 01:39:12 +00:00
|
|
|
testing_state.test.scope_started = true; \
|
|
|
|
testing_state.num_tests_in_group++
|
|
|
|
|
2021-07-01 12:02:39 +00:00
|
|
|
#define DQN_TEST_DECLARE_GROUP_SCOPED(testing_state, name) \
|
|
|
|
fprintf(stdout, name "\n"); \
|
2019-08-24 01:39:12 +00:00
|
|
|
DQN_DEFER \
|
|
|
|
{ \
|
2021-07-28 11:10:25 +00:00
|
|
|
g_dqn_test_total_good_tests += testing_state.num_tests_ok_in_group; \
|
|
|
|
g_dqn_test_total_tests += testing_state.num_tests_in_group; \
|
2021-07-01 12:02:39 +00:00
|
|
|
Dqn_TestingState_PrintGroupResult(&testing_state); \
|
2019-08-24 01:39:12 +00:00
|
|
|
testing_state = {}; \
|
2021-07-01 12:02:39 +00:00
|
|
|
fprintf(stdout, "\n\n"); \
|
2019-08-24 01:39:12 +00:00
|
|
|
}
|
|
|
|
|
2021-07-01 12:02:39 +00:00
|
|
|
#define DQN_TEST_EXPECT_MSG(testing_state, expr, msg, ...) \
|
2019-08-24 01:39:12 +00:00
|
|
|
DQN_ASSERT(testing_state.test.scope_started); \
|
|
|
|
if (!(expr)) \
|
|
|
|
{ \
|
2021-07-01 12:02:39 +00:00
|
|
|
testing_state.test.fail_expr = DQN_STRING(#expr); \
|
2021-08-06 14:54:53 +00:00
|
|
|
testing_state.test.fail_msg = Dqn_String_Fmt(&testing_state.arena, msg, ##__VA_ARGS__); \
|
2019-08-24 01:39:12 +00:00
|
|
|
}
|
|
|
|
|
2021-07-01 12:02:39 +00:00
|
|
|
#define DQN_TEST_EXPECT(testing_state, expr) DQN_TEST_EXPECT_MSG(testing_state, expr, "")
|
2019-08-24 01:39:12 +00:00
|
|
|
|
2021-07-01 12:02:39 +00:00
|
|
|
void Dqn_TestingState_PrintGroupResult(Dqn_TestingState const *result)
|
2019-08-24 01:39:12 +00:00
|
|
|
{
|
2021-07-01 12:02:39 +00:00
|
|
|
int const DESIRED_LEN = 72;
|
|
|
|
char const STATUS_OK[] = "OK";
|
|
|
|
char const STATUS_FAIL[] = "FAIL";
|
2019-08-24 01:39:12 +00:00
|
|
|
|
|
|
|
bool all_tests_passed = (result->num_tests_ok_in_group == result->num_tests_in_group);
|
|
|
|
char buf[256] = {};
|
2020-07-17 14:49:21 +00:00
|
|
|
int size = snprintf(buf, Dqn_ArrayCount(buf), "%02d/%02d Tests Passed ", result->num_tests_ok_in_group, result->num_tests_in_group);
|
2021-05-18 11:15:36 +00:00
|
|
|
Dqn_isize remaining_size = DESIRED_LEN - size;
|
2020-07-17 14:49:21 +00:00
|
|
|
remaining_size = (all_tests_passed) ? remaining_size - Dqn_CharCount(STATUS_OK) : remaining_size - Dqn_CharCount(STATUS_FAIL);
|
2021-07-01 12:02:39 +00:00
|
|
|
remaining_size = DQN_M_MAX(remaining_size, 0);
|
2020-07-17 14:49:21 +00:00
|
|
|
DQN_FOR_EACH(i, remaining_size) fprintf(stdout, " ");
|
2019-08-24 01:39:12 +00:00
|
|
|
|
|
|
|
fprintf(stdout, "%s", buf);
|
|
|
|
if (result->num_tests_ok_in_group == result->num_tests_in_group)
|
2021-07-01 12:02:39 +00:00
|
|
|
fprintf(stdout, DQN_TEST_ANSI_COLOR_GREEN "%s" DQN_TEST_ANSI_COLOR_RESET, STATUS_OK);
|
2019-08-24 01:39:12 +00:00
|
|
|
else
|
2021-07-01 12:02:39 +00:00
|
|
|
fprintf(stdout, DQN_TEST_ANSI_COLOR_RED "%s" DQN_TEST_ANSI_COLOR_RESET, STATUS_FAIL);
|
2019-08-24 01:39:12 +00:00
|
|
|
}
|
|
|
|
|
2021-07-01 12:02:39 +00:00
|
|
|
void Dqn_TestState_PrintResult(Dqn_TestState const *result)
|
2019-08-24 01:39:12 +00:00
|
|
|
{
|
2021-07-01 12:02:39 +00:00
|
|
|
char const INDENT[] = " ";
|
|
|
|
int const DESIRED_LEN = 72;
|
2019-08-24 01:39:12 +00:00
|
|
|
fprintf(stdout, "%s%s", INDENT, result->name.str);
|
|
|
|
|
|
|
|
char const STATUS_OK[] = "OK";
|
|
|
|
char const STATUS_FAIL[] = "FAIL";
|
|
|
|
|
2020-07-17 14:49:21 +00:00
|
|
|
Dqn_isize remaining_size = DESIRED_LEN - result->name.size - Dqn_CharCount(INDENT);
|
2021-07-01 12:02:39 +00:00
|
|
|
remaining_size = (result->fail_expr.str) ? remaining_size - Dqn_CharCount(STATUS_FAIL) : remaining_size - Dqn_CharCount(STATUS_OK);
|
|
|
|
remaining_size = DQN_M_MAX(remaining_size, 0);
|
2019-08-24 01:39:12 +00:00
|
|
|
|
2020-07-17 14:49:21 +00:00
|
|
|
DQN_FOR_EACH(i, remaining_size) fprintf(stdout, ".");
|
2019-08-24 01:39:12 +00:00
|
|
|
if (result->fail_expr.str)
|
|
|
|
{
|
2021-07-01 12:02:39 +00:00
|
|
|
fprintf(stdout, DQN_TEST_ANSI_COLOR_RED "%s" DQN_TEST_ANSI_COLOR_RESET "\n", STATUS_FAIL);
|
2021-05-18 11:15:36 +00:00
|
|
|
fprintf(stdout, "%s%sReason: Expression failed (%s) %s\n", INDENT, INDENT, result->fail_expr.str, result->fail_msg.str);
|
2019-08-24 01:39:12 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-07-01 12:02:39 +00:00
|
|
|
fprintf(stdout, DQN_TEST_ANSI_COLOR_GREEN "%s" DQN_TEST_ANSI_COLOR_RESET "\n", STATUS_OK);
|
2019-08-24 01:39:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-22 09:59:27 +00:00
|
|
|
void Dqn_Test_Array()
|
|
|
|
{
|
|
|
|
Dqn_TestingState testing_state = {};
|
|
|
|
DQN_TEST_DECLARE_GROUP_SCOPED(testing_state, "Dqn_Array");
|
|
|
|
// NOTE: Dqn_Array_InitWithMemory
|
2019-08-24 01:39:12 +00:00
|
|
|
{
|
2021-07-22 09:59:27 +00:00
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Fixed Memory: Test add single item and can't allocate more");
|
|
|
|
int memory[4] = {};
|
|
|
|
Dqn_Array<int> array = Dqn_Array_InitWithMemory(memory, Dqn_ArrayCount(memory), 0 /*size*/);
|
|
|
|
Dqn_Array_Add(&array, 1);
|
|
|
|
Dqn_Array_Add(&array, 2);
|
|
|
|
Dqn_Array_Add(&array, 3);
|
|
|
|
Dqn_Array_Add(&array, 4);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, array.data[0] == 1, "array.data %d", array.data[0]);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, array.data[1] == 2, "array.data %d", array.data[1]);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, array.data[2] == 3, "array.data %d", array.data[2]);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, array.data[3] == 4, "array.data %d", array.data[3]);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, array.size == 4, "array.size: %d", array.size);
|
|
|
|
|
|
|
|
int *added_item = Dqn_Array_Add(&array, 5);
|
|
|
|
DQN_TEST_EXPECT(testing_state, added_item == nullptr);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, array.size == 4, "array.size: %d", array.size);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, array.max == 4, "array.max: %d", array.max);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Fixed Memory: Test add array of items");
|
|
|
|
int memory[4] = {};
|
|
|
|
int DATA[] = {1, 2, 3};
|
|
|
|
Dqn_Array<int> array = Dqn_Array_InitWithMemory(memory, Dqn_ArrayCount(memory), 0 /*size*/);
|
|
|
|
Dqn_Array_AddArray(&array, DATA, Dqn_ArrayCount(DATA));
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, array.data[0] == 1, "array.data %d", array.data[0]);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, array.data[1] == 2, "array.data %d", array.data[1]);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, array.data[2] == 3, "array.data %d", array.data[2]);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, array.size == 3, "array.size: %d", array.size);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, array.max == 4, "array.max: %d", array.max);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Fixed Memory: Test clear and clear with memory zeroed");
|
|
|
|
int memory[4] = {};
|
|
|
|
int DATA[] = {1, 2, 3};
|
|
|
|
Dqn_Array<int> array = Dqn_Array_InitWithMemory(memory, Dqn_ArrayCount(memory), 0 /*size*/);
|
|
|
|
Dqn_Array_AddArray(&array, DATA, Dqn_ArrayCount(DATA));
|
|
|
|
Dqn_Array_Clear(&array, Dqn_ZeroMem::No);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, array.size == 0, "array.size: %d", array.size);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, array.max == 4, "array.max: %d", array.max);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, array.data[0] == 1, "array.data %d. Clear but don't zero memory so old values should still remain", array.data[0]);
|
|
|
|
|
|
|
|
Dqn_Array_Clear(&array, Dqn_ZeroMem::Yes);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, array.data[0] == 0, "array.data %d. Clear but zero memory old values should not remain", array.data[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Fixed Memory: Test erase stable and erase unstable");
|
|
|
|
int memory[4] = {};
|
|
|
|
int DATA[] = {1, 2, 3, 4};
|
|
|
|
Dqn_Array<int> array = Dqn_Array_InitWithMemory(memory, Dqn_ArrayCount(memory), 0 /*size*/);
|
|
|
|
Dqn_Array_AddArray(&array, DATA, Dqn_ArrayCount(DATA));
|
|
|
|
Dqn_Array_EraseUnstable(&array, 1);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, array.data[0] == 1, "array.data %d", array.data[0]);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, array.data[1] == 4, "array.data %d", array.data[1]);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, array.data[2] == 3, "array.data %d", array.data[2]);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, array.size == 3, "array.size: %d", array.size);
|
|
|
|
|
|
|
|
Dqn_Array_EraseStable(&array, 0);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, array.data[0] == 4, "array.data: %d", array.data[0]);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, array.data[1] == 3, "array.data: %d", array.data[1]);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, array.size == 2, "array.size: %d", array.size);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Fixed Memory: Test array pop and peek");
|
|
|
|
int memory[4] = {};
|
|
|
|
int DATA[] = {1, 2, 3};
|
|
|
|
Dqn_Array<int> array = Dqn_Array_InitWithMemory(memory, Dqn_ArrayCount(memory), 0 /*size*/);
|
|
|
|
Dqn_Array_AddArray(&array, DATA, Dqn_ArrayCount(DATA));
|
|
|
|
Dqn_Array_Pop(&array, 2);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, array.data[0] == 1, "array.data: %d", array.data[0]);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, array.size == 1, "array.size: %d", array.size);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, array.max == 4, "array.max: %d", array.max);
|
|
|
|
|
|
|
|
int *peek_item = Dqn_Array_Peek(&array);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, *peek_item == 1, "peek: %d", *peek_item);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, array.size == 1, "array.size: %d", array.size);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, array.max == 4, "array.max: %d", array.max);
|
|
|
|
}
|
|
|
|
}
|
2021-07-01 12:02:39 +00:00
|
|
|
|
2021-07-22 09:59:27 +00:00
|
|
|
// NOTE: Dynamic Memory: Dqn_Array
|
|
|
|
{
|
2021-07-28 11:10:25 +00:00
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Dynamic Memory: Reserve and check over commit reallocates");
|
|
|
|
Dqn_ArenaAllocator arena = {};
|
|
|
|
Dqn_Array<int> array = {};
|
|
|
|
array.arena = &arena;
|
|
|
|
|
|
|
|
Dqn_Array_Reserve(&array, 4);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, array.size == 0, "array.size: %d", array.size);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, array.max == 4, "array.max: %d", array.max);
|
|
|
|
|
|
|
|
int DATA[] = {1, 2, 3, 4};
|
|
|
|
Dqn_Array_AddArray(&array, DATA, Dqn_ArrayCount(DATA));
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, array.data[0] == 1, "array.data: %d", array.data[0]);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, array.data[1] == 2, "array.data: %d", array.data[1]);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, array.data[2] == 3, "array.data: %d", array.data[2]);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, array.data[3] == 4, "array.data: %d", array.data[3]);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, array.size == 4, "array.size: %d", array.size);
|
|
|
|
|
|
|
|
int *added_item = Dqn_Array_Add(&array, 5);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, *added_item == 5, "added_item: %d", *added_item);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, array.data[4] == 5, "array.data: %d", array.data[4]);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, array.size == 5, "array.size: %d", array.size);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, array.max >= 5, "array.max: %d", array.max);
|
|
|
|
|
|
|
|
Dqn_ArenaAllocator_Free(&arena);
|
2021-07-22 09:59:27 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-24 01:39:12 +00:00
|
|
|
|
2021-07-22 09:59:27 +00:00
|
|
|
void Dqn_Test_FixedArray()
|
|
|
|
{
|
|
|
|
Dqn_TestingState testing_state = {};
|
|
|
|
DQN_TEST_DECLARE_GROUP_SCOPED(testing_state, "Dqn_FixedArray");
|
|
|
|
// NOTE: Dqn_FixedArray_Init
|
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Initialise from raw array");
|
|
|
|
int raw_array[] = {1, 2};
|
|
|
|
auto array = Dqn_FixedArray_Init<int, 4>(raw_array, (int)Dqn_ArrayCount(raw_array));
|
|
|
|
DQN_TEST_EXPECT(testing_state, array.size == 2);
|
|
|
|
DQN_TEST_EXPECT(testing_state, array[0] == 1);
|
|
|
|
DQN_TEST_EXPECT(testing_state, array[1] == 2);
|
|
|
|
}
|
2021-07-01 12:02:39 +00:00
|
|
|
|
2021-07-22 09:59:27 +00:00
|
|
|
// NOTE: Dqn_FixedArray_EraseStable
|
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Erase stable 1 element from array");
|
|
|
|
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);
|
|
|
|
DQN_TEST_EXPECT(testing_state, array.size == 2);
|
|
|
|
DQN_TEST_EXPECT(testing_state, array[0] == 1);
|
|
|
|
DQN_TEST_EXPECT(testing_state, array[1] == 3);
|
|
|
|
}
|
2019-08-24 01:39:12 +00:00
|
|
|
|
2021-07-22 09:59:27 +00:00
|
|
|
// NOTE: Dqn_FixedArray_EraseUnstable
|
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Erase unstable 1 element from array");
|
|
|
|
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);
|
|
|
|
DQN_TEST_EXPECT(testing_state, array.size == 2);
|
|
|
|
DQN_TEST_EXPECT(testing_state, array[0] == 3);
|
|
|
|
DQN_TEST_EXPECT(testing_state, array[1] == 2);
|
|
|
|
}
|
2019-08-24 03:38:58 +00:00
|
|
|
|
2021-07-22 09:59:27 +00:00
|
|
|
// NOTE: Dqn_FixedArray_Add
|
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Add 1 element to array");
|
|
|
|
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);
|
|
|
|
DQN_TEST_EXPECT(testing_state, array.size == 2);
|
|
|
|
DQN_TEST_EXPECT(testing_state, array[0] == 1);
|
|
|
|
DQN_TEST_EXPECT(testing_state, array[1] == ITEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: Dqn_FixedArray_Clear
|
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Clear array");
|
|
|
|
int raw_array[] = {1};
|
|
|
|
auto array = Dqn_FixedArray_Init<int, 4>(raw_array, (int)Dqn_ArrayCount(raw_array));
|
|
|
|
Dqn_FixedArray_Clear(&array);
|
|
|
|
DQN_TEST_EXPECT(testing_state, array.size == 0);
|
|
|
|
}
|
|
|
|
}
|
2021-07-01 12:02:39 +00:00
|
|
|
|
2021-07-22 09:59:27 +00:00
|
|
|
void Dqn_Test_FixedString()
|
|
|
|
{
|
|
|
|
Dqn_TestingState testing_state = {};
|
|
|
|
DQN_TEST_DECLARE_GROUP_SCOPED(testing_state, "Dqn_FixedString");
|
2019-08-24 03:38:58 +00:00
|
|
|
|
2021-07-22 09:59:27 +00:00
|
|
|
// NOTE: Dqn_FixedString_Append
|
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Append too much fails");
|
|
|
|
Dqn_FixedString<4> str = {};
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, Dqn_FixedString_Append(&str, "abcd") == false, "We need space for the null-terminator");
|
2019-08-24 01:39:12 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 09:59:27 +00:00
|
|
|
// NOTE: Dqn_FixedString_AppendFmt
|
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Append format string too much fails");
|
|
|
|
Dqn_FixedString<4> str = {};
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, Dqn_FixedString_AppendFmt(&str, "abcd") == false, "We need space for the null-terminator");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-06 14:54:53 +00:00
|
|
|
void Dqn_Test_Hex()
|
|
|
|
{
|
|
|
|
Dqn_TestingState testing_state = {};
|
|
|
|
DQN_TEST_DECLARE_GROUP_SCOPED(testing_state, "Dqn_Hex");
|
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Convert 0x123");
|
|
|
|
Dqn_u64 result = Dqn_Hex_StringToU64(DQN_STRING("0x123"));
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, result == 0x123, "result: %I64u", result);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Convert 0xFFFF");
|
|
|
|
Dqn_u64 result = Dqn_Hex_StringToU64(DQN_STRING("0xFFFF"));
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, result == 0xFFFF, "result: %I64u", result);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Convert FFFF");
|
|
|
|
Dqn_u64 result = Dqn_Hex_StringToU64(DQN_STRING("FFFF"));
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, result == 0xFFFF, "result: %I64u", result);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Convert abCD");
|
|
|
|
Dqn_u64 result = Dqn_Hex_StringToU64(DQN_STRING("abCD"));
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, result == 0xabCD, "result: %I64u", result);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Convert 0xabCD");
|
|
|
|
Dqn_u64 result = Dqn_Hex_StringToU64(DQN_STRING("0xabCD"));
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, result == 0xabCD, "result: %I64u", result);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Convert 0x");
|
|
|
|
Dqn_u64 result = Dqn_Hex_StringToU64(DQN_STRING("0x"));
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, result == 0x0, "result: %I64u", result);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Convert 0X");
|
|
|
|
Dqn_u64 result = Dqn_Hex_StringToU64(DQN_STRING("0X"));
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, result == 0x0, "result: %I64u", result);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Convert 3");
|
|
|
|
Dqn_u64 result = Dqn_Hex_StringToU64(DQN_STRING("3"));
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, result == 3, "result: %I64u", result);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Convert f");
|
|
|
|
Dqn_u64 result = Dqn_Hex_StringToU64(DQN_STRING("f"));
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, result == 0xf, "result: %I64u", result);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Convert g");
|
|
|
|
Dqn_u64 result = Dqn_Hex_StringToU64(DQN_STRING("g"));
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, result == 0, "result: %I64u", result);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Convert -0x3");
|
|
|
|
Dqn_u64 result = Dqn_Hex_StringToU64(DQN_STRING("-0x3"));
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, result == 0, "result: %I64u", result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-22 09:59:27 +00:00
|
|
|
void Dqn_Test_M4()
|
|
|
|
{
|
|
|
|
Dqn_TestingState testing_state = {};
|
|
|
|
DQN_TEST_DECLARE_GROUP_SCOPED(testing_state, "Dqn_M4");
|
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Simple translate and scale matrix");
|
|
|
|
Dqn_M4 translate = Dqn_M4_TranslateF(1, 2, 3);
|
|
|
|
Dqn_M4 scale = Dqn_M4_ScaleF(2, 2, 2);
|
|
|
|
Dqn_M4 result = Dqn_M4_Mul(translate, scale);
|
|
|
|
|
|
|
|
const Dqn_M4 EXPECT = {{
|
|
|
|
{2, 0, 0, 0},
|
|
|
|
{0, 2, 0, 0},
|
|
|
|
{0, 0, 2, 0},
|
|
|
|
{1, 2, 3, 1},
|
|
|
|
}};
|
|
|
|
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state,
|
|
|
|
memcmp(result.columns, EXPECT.columns, sizeof(EXPECT)) == 0,
|
|
|
|
"\nresult =\n%s\nexpected =\n%s",
|
|
|
|
Dqn_M4_ColumnMajorString(result).str,
|
|
|
|
Dqn_M4_ColumnMajorString(EXPECT).str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-06 14:54:53 +00:00
|
|
|
void Dqn_Test_DSMap()
|
|
|
|
{
|
|
|
|
Dqn_TestingState testing_state = {};
|
|
|
|
DQN_TEST_DECLARE_GROUP_SCOPED(testing_state, "Dqn_DSMap");
|
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Add r-value item to map");
|
|
|
|
Dqn_DSMap<int> map = Dqn_DSMap_Init<int>(128);
|
|
|
|
Dqn_DSMapEntry<int> *entry = Dqn_DSMap_AddCopy(&map, 3 /*hash*/, 5 /*value*/);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, map.size == 128, "size: %I64d", map.size);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, map.count == 1, "count: %I64u", map.count);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, entry->hash == 3, "hash: %I64u", entry->hash);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, entry->value == 5, "value: %d", entry->value);
|
|
|
|
Dqn_DSMap_Free(&map);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Add l-value item to map");
|
|
|
|
Dqn_DSMap<int> map = Dqn_DSMap_Init<int>(128);
|
|
|
|
int value = 5;
|
|
|
|
Dqn_DSMapEntry<int> *entry = Dqn_DSMap_Add(&map, 3 /*hash*/, value);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, map.size == 128, "size: %I64d", map.size);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, map.count == 1, "count: %I64u", map.count);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, entry->hash == 3, "hash: %I64u", entry->hash);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, entry->value == 5, "value: %d", entry->value);
|
|
|
|
Dqn_DSMap_Free(&map);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Get item from map");
|
|
|
|
Dqn_DSMap<int> map = Dqn_DSMap_Init<int>(128);
|
|
|
|
Dqn_DSMapEntry<int> *entry = Dqn_DSMap_AddCopy(&map, 3 /*hash*/, 5 /*value*/);
|
|
|
|
Dqn_DSMapEntry<int> *get_entry = Dqn_DSMap_Get(&map, 3 /*hash*/);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, get_entry == entry, "get_entry: %p, entry: %p", get_entry, entry);
|
|
|
|
Dqn_DSMap_Free(&map);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Get non-existent item from map");
|
|
|
|
Dqn_DSMap<int> map = Dqn_DSMap_Init<int>(128);
|
|
|
|
Dqn_DSMapEntry<int> *entry = Dqn_DSMap_Get(&map, 3 /*hash*/);
|
|
|
|
DQN_TEST_EXPECT(testing_state, entry == nullptr);
|
|
|
|
Dqn_DSMap_Free(&map);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Erase item from map");
|
|
|
|
Dqn_DSMap<int> map = Dqn_DSMap_Init<int>(128);
|
|
|
|
Dqn_DSMap_AddCopy(&map, 3 /*hash*/, 5 /*value*/);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, map.count == 1, "count: %I64d", map.count);
|
|
|
|
Dqn_DSMap_Erase(&map, 3 /*hash*/, Dqn_ZeroMem::No);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, map.count == 0, "count: %I64d", map.count);
|
|
|
|
Dqn_DSMap_Free(&map);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Erase non-existent item from map");
|
|
|
|
Dqn_DSMap<int> map = Dqn_DSMap_Init<int>(128);
|
|
|
|
Dqn_DSMap_Erase(&map, 3 /*hash*/, Dqn_ZeroMem::No);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, map.count == 0, "count: %I64d", map.count);
|
|
|
|
Dqn_DSMap_Free(&map);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Test resize on maximum load");
|
|
|
|
const Dqn_isize INIT_SIZE = 4;
|
|
|
|
Dqn_DSMap<int> map = Dqn_DSMap_Init<int>(INIT_SIZE);
|
|
|
|
Dqn_DSMap_AddCopy(&map, 0 /*hash*/, 5 /*value*/);
|
|
|
|
Dqn_DSMap_AddCopy(&map, 1 /*hash*/, 5 /*value*/);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, map.count == 2, "count: %I64d", map.count);
|
|
|
|
|
|
|
|
// This *should* cause a resize because 3/4 slots filled is 75% load
|
|
|
|
Dqn_DSMap_AddCopy(&map, 6 /*hash*/, 5 /*value*/);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, map.count == 3, "count: %I64d", map.count);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, map.size == INIT_SIZE * 2, "size: %I64d", map.size);
|
|
|
|
|
|
|
|
// Check that the elements are rehashed where we expected them to be
|
|
|
|
DQN_TEST_EXPECT (testing_state, map.slots[0].occupied == DQN_CAST(Dqn_u8)true);
|
|
|
|
DQN_TEST_EXPECT (testing_state, map.slots[1].occupied == DQN_CAST(Dqn_u8)true);
|
|
|
|
DQN_TEST_EXPECT (testing_state, map.slots[2].occupied == DQN_CAST(Dqn_u8)false);
|
|
|
|
DQN_TEST_EXPECT (testing_state, map.slots[3].occupied == DQN_CAST(Dqn_u8)false);
|
|
|
|
DQN_TEST_EXPECT (testing_state, map.slots[4].occupied == DQN_CAST(Dqn_u8)false);
|
|
|
|
DQN_TEST_EXPECT (testing_state, map.slots[5].occupied == DQN_CAST(Dqn_u8)false);
|
|
|
|
DQN_TEST_EXPECT (testing_state, map.slots[6].occupied == DQN_CAST(Dqn_u8)true);
|
|
|
|
DQN_TEST_EXPECT (testing_state, map.slots[7].occupied == DQN_CAST(Dqn_u8)false);
|
|
|
|
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, map.slots[0].value == 5, "value: %d", map.slots[0].value);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, map.slots[1].value == 5, "value: %d", map.slots[1].value);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, map.slots[6].value == 5, "value: %d", map.slots[6].value);
|
|
|
|
|
|
|
|
Dqn_DSMap_Free(&map);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-28 11:10:25 +00:00
|
|
|
void Dqn_Test_Map()
|
|
|
|
{
|
|
|
|
Dqn_TestingState testing_state = {};
|
|
|
|
DQN_TEST_DECLARE_GROUP_SCOPED(testing_state, "Dqn_Map");
|
|
|
|
Dqn_ArenaAllocator arena = {};
|
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Add r-value item to map");
|
|
|
|
Dqn_Map<int> map = Dqn_Map_InitWithArena<int>(&arena, 1);
|
|
|
|
Dqn_MapEntry<int> *entry = Dqn_Map_AddCopy(&map, 3 /*hash*/, 5 /*value*/, Dqn_MapCollideRule::Overwrite);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, map.size == 1, "size: %I64d", map.size);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, map.count == 1, "count: %I64u", map.count);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, map.chain_count == 0, "chain_count: %I64u", map.chain_count);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, map.free_list == nullptr, "free_list: %p", map.free_list);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, entry->hash == 3, "hash: %I64u", entry->hash);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, entry->value == 5, "value: %d", entry->value);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, entry->next == nullptr, "next: %p", entry->next);
|
|
|
|
Dqn_ArenaAllocator_Free(&arena);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Add l-value item to map");
|
|
|
|
Dqn_Map<int> map = Dqn_Map_InitWithArena<int>(&arena, 1);
|
|
|
|
int value = 5;
|
|
|
|
Dqn_MapEntry<int> *entry = Dqn_Map_Add(&map, 3 /*hash*/, value, Dqn_MapCollideRule::Overwrite);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, map.size == 1, "size: %I64d", map.size);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, map.count == 1, "count: %I64u", map.count);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, map.chain_count == 0, "chain_count: %I64u", map.chain_count);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, map.free_list == nullptr, "free_list: %p", map.free_list);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, entry->hash == 3, "hash: %I64u", entry->hash);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, entry->value == 5, "value: %d", entry->value);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, entry->next == nullptr, "next: %p", entry->next);
|
|
|
|
Dqn_ArenaAllocator_Free(&arena);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Add r-value item and overwrite on collision");
|
|
|
|
Dqn_Map<int> map = Dqn_Map_InitWithArena<int>(&arena, 1);
|
|
|
|
Dqn_MapEntry<int> *entry_a = Dqn_Map_AddCopy(&map, 3 /*hash*/, 5, Dqn_MapCollideRule::Overwrite);
|
|
|
|
Dqn_MapEntry<int> *entry_b = Dqn_Map_AddCopy(&map, 4 /*hash*/, 6, Dqn_MapCollideRule::Overwrite);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, map.size == 1, "size: %I64u", map.size);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, map.count == 1, "count: %I64u", map.count);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, map.chain_count == 0, "chain_count: %I64u", map.chain_count);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, map.free_list == nullptr, "free_list: %p", map.free_list);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, entry_a == entry_b, "Expected entry to be overwritten");
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, entry_b->hash == 4, "hash: %I64u", entry_b->hash);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, entry_b->value == 6, "value: %d", entry_b->value);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, entry_b->next == nullptr, "next: %p", entry_b->next);
|
|
|
|
Dqn_ArenaAllocator_Free(&arena);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Add r-value item and fail on collision");
|
|
|
|
Dqn_Map<int> map = Dqn_Map_InitWithArena<int>(&arena, 1);
|
|
|
|
Dqn_Map_AddCopy(&map, 3 /*hash*/, 5, Dqn_MapCollideRule::Overwrite);
|
|
|
|
Dqn_MapEntry<int> *entry_b = Dqn_Map_AddCopy(&map, 4 /*hash*/, 6, Dqn_MapCollideRule::Fail);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, entry_b == nullptr, "Expected entry to be overwritten");
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, map.size == 1, "size: %I64u", map.size);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, map.count == 1, "count: %I64u", map.count);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, map.chain_count == 0, "chain_count: %I64u", map.chain_count);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, map.free_list == nullptr, "free_list: %p", map.free_list);
|
|
|
|
Dqn_ArenaAllocator_Free(&arena);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Add r-value item and chain on collision");
|
|
|
|
Dqn_Map<int> map = Dqn_Map_InitWithArena<int>(&arena, 1);
|
|
|
|
Dqn_MapEntry<int> *entry_a = Dqn_Map_AddCopy(&map, 3 /*hash*/, 5, Dqn_MapCollideRule::Overwrite);
|
|
|
|
Dqn_MapEntry<int> *entry_b = Dqn_Map_AddCopy(&map, 4 /*hash*/, 6, Dqn_MapCollideRule::Chain);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, map.size == 1, "size: %I64u", map.size);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, map.count == 1, "count: %I64u", map.count);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, map.chain_count == 1, "chain_count: %I64u", map.chain_count);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, map.free_list == nullptr, "free_list: %p", map.free_list);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, entry_a != entry_b, "Expected colliding entry to be chained");
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, entry_a->next == entry_b, "Expected chained entry to be next to our first map entry");
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, entry_b->hash == 4, "hash: %I64u", entry_b->hash);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, entry_b->value == 6, "value: %d", entry_b->value);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, entry_b->next == nullptr, "next: %p", entry_b->next);
|
|
|
|
Dqn_ArenaAllocator_Free(&arena);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Add r-value item and get them back out again");
|
|
|
|
Dqn_Map<int> map = Dqn_Map_InitWithArena<int>(&arena, 1);
|
|
|
|
Dqn_MapEntry<int> *entry_a = Dqn_Map_AddCopy(&map, 3 /*hash*/, 5, Dqn_MapCollideRule::Overwrite);
|
|
|
|
Dqn_MapEntry<int> *entry_b = Dqn_Map_AddCopy(&map, 4 /*hash*/, 6, Dqn_MapCollideRule::Chain);
|
|
|
|
|
|
|
|
Dqn_MapEntry<int> *entry_a_copy = Dqn_Map_Get(&map, 3 /*hash*/);
|
|
|
|
Dqn_MapEntry<int> *entry_b_copy = Dqn_Map_Get(&map, 4 /*hash*/);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, map.size == 1, "size: %I64u", map.size);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, map.count == 1, "count: %I64u", map.count);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, map.chain_count == 1, "chain_count: %I64u", map.chain_count);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, map.free_list == nullptr, "free_list: %p", map.free_list);
|
|
|
|
DQN_TEST_EXPECT(testing_state, entry_a_copy == entry_a);
|
|
|
|
DQN_TEST_EXPECT(testing_state, entry_b_copy == entry_b);
|
|
|
|
Dqn_ArenaAllocator_Free(&arena);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Add r-value item and erase it");
|
|
|
|
Dqn_Map<int> map = Dqn_Map_InitWithArena<int>(&arena, 1);
|
|
|
|
Dqn_Map_AddCopy(&map, 3 /*hash*/, 5, Dqn_MapCollideRule::Overwrite);
|
|
|
|
Dqn_Map_AddCopy(&map, 4 /*hash*/, 6, Dqn_MapCollideRule::Chain);
|
|
|
|
Dqn_Map_Get(&map, 3 /*hash*/);
|
|
|
|
|
|
|
|
Dqn_Map_Erase(&map, 3 /*hash*/, Dqn_ZeroMem::No);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, map.size == 1, "size: %I64u", map.size);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, map.count == 1, "count: %I64u", map.count);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, map.chain_count == 0, "chain_count: %I64u", map.chain_count);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, map.free_list != nullptr, "free_list: %p", map.free_list);
|
|
|
|
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, map.free_list->hash == 3, "Entry should not be zeroed out on erase");
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, map.free_list->value == 5, "Entry should not be zeroed out on erase");
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, map.free_list->next == nullptr, "This should be the first and only entry in the free list");
|
|
|
|
|
|
|
|
Dqn_MapEntry<int> *entry = Dqn_Map_Get(&map, 4 /*hash*/);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, entry->hash == 4, "hash: %I64u", entry->hash);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, entry->value == 6, "value: %d", entry->value);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, entry->next == nullptr, "next: %p", entry->next);
|
|
|
|
|
|
|
|
Dqn_ArenaAllocator_Free(&arena);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Add r-value item and erase it, zeroing the memory out");
|
|
|
|
Dqn_Map<int> map = Dqn_Map_InitWithArena<int>(&arena, 1);
|
|
|
|
Dqn_Map_AddCopy(&map, 3 /*hash*/, 5, Dqn_MapCollideRule::Overwrite);
|
|
|
|
Dqn_Map_AddCopy(&map, 4 /*hash*/, 6, Dqn_MapCollideRule::Chain);
|
|
|
|
Dqn_Map_Get(&map, 3 /*hash*/);
|
|
|
|
|
|
|
|
Dqn_Map_Erase(&map, 3 /*hash*/, Dqn_ZeroMem::Yes);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, map.size == 1, "size: %I64u", map.size);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, map.count == 1, "count: %I64u", map.count);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, map.chain_count == 0, "chain_count: %I64u", map.chain_count);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, map.free_list != nullptr, "free_list: %p", map.free_list);
|
|
|
|
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, map.free_list->hash == 0, "Entry should be zeroed out on erase");
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, map.free_list->value == 0, "Entry should be zeroed out on erase");
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, map.free_list->next == nullptr, "This should be the first and only entry in the free list");
|
|
|
|
|
|
|
|
Dqn_MapEntry<int> *entry = Dqn_Map_Get(&map, 4 /*hash*/);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, entry->hash == 4, "hash: %I64u", entry->hash);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, entry->value == 6, "value: %d", entry->value);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, entry->next == nullptr, "next: %p", entry->next);
|
|
|
|
|
|
|
|
Dqn_ArenaAllocator_Free(&arena);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-07-22 15:35:08 +00:00
|
|
|
void Dqn_Test_Intrinsics()
|
|
|
|
{
|
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.
|
|
|
|
|
|
|
|
Dqn_TestingState testing_state = {};
|
|
|
|
DQN_TEST_DECLARE_GROUP_SCOPED(testing_state, "Dqn_Atomic");
|
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Dqn_AtomicAddU32");
|
|
|
|
Dqn_u32 val = 0;
|
|
|
|
Dqn_AtomicAddU32(&val, 1);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, val == 1, "val: %I64u", val);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Dqn_AtomicAddU64");
|
|
|
|
Dqn_u64 val = 0;
|
|
|
|
Dqn_AtomicAddU64(&val, 1);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, val == 1, "val: %I64u", val);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Dqn_AtomicSubU32");
|
|
|
|
Dqn_u32 val = 1;
|
|
|
|
Dqn_AtomicSubU32(&val, 1);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, val == 0, "val: %I64u", val);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Dqn_AtomicSubU64");
|
|
|
|
Dqn_u64 val = 1;
|
|
|
|
Dqn_AtomicSubU64(&val, 1);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, val == 0, "val: %I64u", val);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Dqn_AtomicSetValue32");
|
|
|
|
long a = 0;
|
|
|
|
long b = 111;
|
|
|
|
Dqn_AtomicSetValue32(&a, b);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, a == b, "a: %I64d, b: %I64d", a, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Dqn_AtomicSetValue64");
|
|
|
|
Dqn_i64 a = 0;
|
|
|
|
Dqn_i64 b = 111;
|
|
|
|
Dqn_AtomicSetValue64(&a, b);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, a == b, "a: %I64i, b: %I64i", a, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Dqn_CPUClockCycle");
|
|
|
|
Dqn_CPUClockCycle();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Dqn_CompilerReadBarrierAndCPUReadFence");
|
|
|
|
Dqn_CompilerReadBarrierAndCPUReadFence;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Dqn_CompilerWriteBarrierAndCPUWriteFence");
|
|
|
|
Dqn_CompilerWriteBarrierAndCPUWriteFence;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-22 09:59:27 +00:00
|
|
|
void Dqn_Test_Rect()
|
|
|
|
{
|
|
|
|
Dqn_TestingState testing_state = {};
|
|
|
|
DQN_TEST_DECLARE_GROUP_SCOPED(testing_state, "Dqn_Rect");
|
|
|
|
// NOTE: Dqn_Rect_Intersection
|
2019-08-24 01:39:12 +00:00
|
|
|
{
|
|
|
|
{
|
2021-07-22 09:59:27 +00:00
|
|
|
DQN_TEST_START_SCOPE(testing_state, "No intersection");
|
|
|
|
Dqn_Rect a = Dqn_Rect_InitFromPosAndSize(Dqn_V2(0, 0), Dqn_V2(100, 100));
|
|
|
|
Dqn_Rect b = Dqn_Rect_InitFromPosAndSize(Dqn_V2(200, 0), Dqn_V2(200, 200));
|
|
|
|
Dqn_Rect ab = Dqn_Rect_Intersection(a, b);
|
|
|
|
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state,
|
|
|
|
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-07-22 09:59:27 +00:00
|
|
|
DQN_TEST_START_SCOPE(testing_state, "A's min intersects B");
|
|
|
|
Dqn_Rect a = Dqn_Rect_InitFromPosAndSize(Dqn_V2(50, 50), Dqn_V2(100, 100));
|
|
|
|
Dqn_Rect b = Dqn_Rect_InitFromPosAndSize(Dqn_V2( 0, 0), Dqn_V2(100, 100));
|
|
|
|
Dqn_Rect ab = Dqn_Rect_Intersection(a, b);
|
|
|
|
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state,
|
|
|
|
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-07-22 09:59:27 +00:00
|
|
|
DQN_TEST_START_SCOPE(testing_state, "B's min intersects A");
|
|
|
|
Dqn_Rect a = Dqn_Rect_InitFromPosAndSize(Dqn_V2( 0, 0), Dqn_V2(100, 100));
|
|
|
|
Dqn_Rect b = Dqn_Rect_InitFromPosAndSize(Dqn_V2(50, 50), Dqn_V2(100, 100));
|
|
|
|
Dqn_Rect ab = Dqn_Rect_Intersection(a, b);
|
|
|
|
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state,
|
|
|
|
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-07-22 09:59:27 +00:00
|
|
|
DQN_TEST_START_SCOPE(testing_state, "A's max intersects B");
|
|
|
|
Dqn_Rect a = Dqn_Rect_InitFromPosAndSize(Dqn_V2(-50, -50), Dqn_V2(100, 100));
|
|
|
|
Dqn_Rect b = Dqn_Rect_InitFromPosAndSize(Dqn_V2( 0, 0), Dqn_V2(100, 100));
|
|
|
|
Dqn_Rect ab = Dqn_Rect_Intersection(a, b);
|
|
|
|
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state,
|
|
|
|
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-07-22 09:59:27 +00:00
|
|
|
DQN_TEST_START_SCOPE(testing_state, "B's max intersects A");
|
|
|
|
Dqn_Rect a = Dqn_Rect_InitFromPosAndSize(Dqn_V2( 0, 0), Dqn_V2(100, 100));
|
|
|
|
Dqn_Rect b = Dqn_Rect_InitFromPosAndSize(Dqn_V2(-50, -50), Dqn_V2(100, 100));
|
|
|
|
Dqn_Rect ab = Dqn_Rect_Intersection(a, b);
|
|
|
|
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state,
|
|
|
|
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-07-22 09:59:27 +00:00
|
|
|
DQN_TEST_START_SCOPE(testing_state, "B contains A");
|
|
|
|
Dqn_Rect a = Dqn_Rect_InitFromPosAndSize(Dqn_V2(25, 25), Dqn_V2( 25, 25));
|
|
|
|
Dqn_Rect b = Dqn_Rect_InitFromPosAndSize(Dqn_V2( 0, 0), Dqn_V2(100, 100));
|
|
|
|
Dqn_Rect ab = Dqn_Rect_Intersection(a, b);
|
|
|
|
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state,
|
|
|
|
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-07-22 09:59:27 +00:00
|
|
|
DQN_TEST_START_SCOPE(testing_state, "A contains B");
|
|
|
|
Dqn_Rect a = Dqn_Rect_InitFromPosAndSize(Dqn_V2( 0, 0), Dqn_V2(100, 100));
|
|
|
|
Dqn_Rect b = Dqn_Rect_InitFromPosAndSize(Dqn_V2(25, 25), Dqn_V2( 25, 25));
|
|
|
|
Dqn_Rect ab = Dqn_Rect_Intersection(a, b);
|
|
|
|
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state,
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "A equals B");
|
|
|
|
Dqn_Rect a = Dqn_Rect_InitFromPosAndSize(Dqn_V2(0, 0), Dqn_V2(100, 100));
|
|
|
|
Dqn_Rect b = a;
|
|
|
|
Dqn_Rect ab = Dqn_Rect_Intersection(a, b);
|
|
|
|
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state,
|
|
|
|
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-07-22 09:59:27 +00:00
|
|
|
}
|
2019-08-24 01:39:12 +00:00
|
|
|
|
2021-07-22 09:59:27 +00:00
|
|
|
void Dqn_Test_Str()
|
|
|
|
{
|
|
|
|
Dqn_TestingState testing_state = {};
|
2019-08-24 01:39:12 +00:00
|
|
|
// ---------------------------------------------------------------------------------------------
|
2019-09-03 13:19:03 +00:00
|
|
|
// NOTE: Dqn_Str_ToI64
|
2019-08-24 01:39:12 +00:00
|
|
|
// ---------------------------------------------------------------------------------------------
|
|
|
|
{
|
2021-07-01 12:02:39 +00:00
|
|
|
DQN_TEST_DECLARE_GROUP_SCOPED(testing_state, "Dqn_Str_ToI64");
|
2019-08-24 01:39:12 +00:00
|
|
|
{
|
2021-07-01 12:02:39 +00:00
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Convert nullptr");
|
2020-04-14 14:40:12 +00:00
|
|
|
Dqn_i64 result = Dqn_Str_ToI64(nullptr);
|
2021-07-01 12:02:39 +00:00
|
|
|
DQN_TEST_EXPECT(testing_state, result == 0);
|
2019-08-24 01:39:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-07-01 12:02:39 +00:00
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Convert empty string");
|
2020-04-14 14:40:12 +00:00
|
|
|
Dqn_i64 result = Dqn_Str_ToI64("");
|
2021-07-01 12:02:39 +00:00
|
|
|
DQN_TEST_EXPECT(testing_state, result == 0);
|
2019-08-24 01:39:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-07-01 12:02:39 +00:00
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Convert \"1\"");
|
2020-04-14 14:40:12 +00:00
|
|
|
Dqn_i64 result = Dqn_Str_ToI64("1");
|
2021-07-01 12:02:39 +00:00
|
|
|
DQN_TEST_EXPECT(testing_state, result == 1);
|
2019-08-24 01:39:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-07-01 12:02:39 +00:00
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Convert \"-0\"");
|
2020-04-14 14:40:12 +00:00
|
|
|
Dqn_i64 result = Dqn_Str_ToI64("-0");
|
2021-07-01 12:02:39 +00:00
|
|
|
DQN_TEST_EXPECT(testing_state, result == 0);
|
2019-08-24 01:39:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-07-01 12:02:39 +00:00
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Convert \"-1\"");
|
2020-04-14 14:40:12 +00:00
|
|
|
Dqn_i64 result = Dqn_Str_ToI64("-1");
|
2021-07-01 12:02:39 +00:00
|
|
|
DQN_TEST_EXPECT(testing_state, result == -1);
|
2019-08-24 01:39:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-07-01 12:02:39 +00:00
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Convert \"1.2\"");
|
2020-04-14 14:40:12 +00:00
|
|
|
Dqn_i64 result = Dqn_Str_ToI64("1.2");
|
2021-07-01 12:02:39 +00:00
|
|
|
DQN_TEST_EXPECT(testing_state, result == 1);
|
2019-08-24 01:39:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-07-01 12:02:39 +00:00
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Convert \"1,234\"");
|
2020-04-14 14:40:12 +00:00
|
|
|
Dqn_i64 result = Dqn_Str_ToI64("1,234");
|
2021-07-01 12:02:39 +00:00
|
|
|
DQN_TEST_EXPECT(testing_state, result == 1234);
|
2019-08-24 01:39:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-07-01 12:02:39 +00:00
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Convert \"1,2\"");
|
2020-04-14 14:40:12 +00:00
|
|
|
Dqn_i64 result = Dqn_Str_ToI64("1,2");
|
2021-07-01 12:02:39 +00:00
|
|
|
DQN_TEST_EXPECT(testing_state, result == 12);
|
2019-08-24 01:39:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-07-01 12:02:39 +00:00
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Convert \"12a3\"");
|
2020-04-14 14:40:12 +00:00
|
|
|
Dqn_i64 result = Dqn_Str_ToI64("12a3");
|
2021-07-01 12:02:39 +00:00
|
|
|
DQN_TEST_EXPECT(testing_state, result == 12);
|
2019-08-24 01:39:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------------------------
|
2019-09-03 13:19:03 +00:00
|
|
|
// NOTE: Dqn_Str_ToU64
|
2019-08-24 01:39:12 +00:00
|
|
|
// ---------------------------------------------------------------------------------------------
|
|
|
|
{
|
2021-07-01 12:02:39 +00:00
|
|
|
DQN_TEST_DECLARE_GROUP_SCOPED(testing_state, "Dqn_Str_ToU64");
|
2019-08-24 01:39:12 +00:00
|
|
|
{
|
2021-07-01 12:02:39 +00:00
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Convert nullptr");
|
2020-04-14 14:40:12 +00:00
|
|
|
Dqn_u64 result = Dqn_Str_ToU64(nullptr);
|
2021-07-01 12:02:39 +00:00
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, result == 0, "result: %zu", result);
|
2019-08-24 01:39:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-07-01 12:02:39 +00:00
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Convert empty string");
|
2020-04-14 14:40:12 +00:00
|
|
|
Dqn_u64 result = Dqn_Str_ToU64("");
|
2021-07-01 12:02:39 +00:00
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, result == 0, "result: %zu", result);
|
2019-08-24 01:39:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-07-01 12:02:39 +00:00
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Convert \"1\"");
|
2020-04-14 14:40:12 +00:00
|
|
|
Dqn_u64 result = Dqn_Str_ToU64("1");
|
2021-07-01 12:02:39 +00:00
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, result == 1, "result: %zu", result);
|
2019-08-24 01:39:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-07-01 12:02:39 +00:00
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Convert \"-0\"");
|
2020-04-14 14:40:12 +00:00
|
|
|
Dqn_u64 result = Dqn_Str_ToU64("-0");
|
2021-07-01 12:02:39 +00:00
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, result == 0, "result: %zu", result);
|
2019-08-24 01:39:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-07-01 12:02:39 +00:00
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Convert \"-1\"");
|
2020-04-14 14:40:12 +00:00
|
|
|
Dqn_u64 result = Dqn_Str_ToU64("-1");
|
2021-07-01 12:02:39 +00:00
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, result == 0, "result: %zu", result);
|
2019-08-24 01:39:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-07-01 12:02:39 +00:00
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Convert \"1.2\"");
|
2020-04-14 14:40:12 +00:00
|
|
|
Dqn_u64 result = Dqn_Str_ToU64("1.2");
|
2021-07-01 12:02:39 +00:00
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, result == 1, "result: %zu", result);
|
2019-08-24 01:39:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-07-01 12:02:39 +00:00
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Convert \"1,234\"");
|
2020-04-14 14:40:12 +00:00
|
|
|
Dqn_u64 result = Dqn_Str_ToU64("1,234");
|
2021-07-01 12:02:39 +00:00
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, result == 1234, "result: %zu", result);
|
2019-08-24 01:39:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-07-01 12:02:39 +00:00
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Convert \"1,2\"");
|
2020-04-14 14:40:12 +00:00
|
|
|
Dqn_u64 result = Dqn_Str_ToU64("1,2");
|
2021-07-01 12:02:39 +00:00
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, result == 12, "result: %zu", result);
|
2019-08-24 01:39:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-07-01 12:02:39 +00:00
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Convert \"12a3\"");
|
2020-04-14 14:40:12 +00:00
|
|
|
Dqn_u64 result = Dqn_Str_ToU64("12a3");
|
2021-07-01 12:02:39 +00:00
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, result == 12, "result: %zu", result);
|
2019-08-24 01:39:12 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-17 15:01:25 +00:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------------------------
|
|
|
|
// NOTE: Dqn_Str_Find
|
|
|
|
// ---------------------------------------------------------------------------------------------
|
|
|
|
{
|
2021-07-01 12:02:39 +00:00
|
|
|
DQN_TEST_DECLARE_GROUP_SCOPED(testing_state, "Dqn_Str_Find");
|
2020-01-17 15:01:25 +00:00
|
|
|
{
|
2021-07-01 12:02:39 +00:00
|
|
|
DQN_TEST_START_SCOPE(testing_state, "String (char) is not in buffer");
|
2020-01-17 15:01:25 +00:00
|
|
|
char const buf[] = "836a35becd4e74b66a0d6844d51f1a63018c7ebc44cf7e109e8e4bba57eefb55";
|
|
|
|
char const find[] = "2";
|
|
|
|
char const *result = Dqn_Str_Find(buf, find, Dqn_CharCountI(buf), Dqn_CharCountI(find));
|
2021-07-01 12:02:39 +00:00
|
|
|
DQN_TEST_EXPECT(testing_state, result == nullptr);
|
2020-01-17 15:01:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-07-01 12:02:39 +00:00
|
|
|
DQN_TEST_START_SCOPE(testing_state, "String (char) is in buffer");
|
2020-01-17 15:01:25 +00:00
|
|
|
char const buf[] = "836a35becd4e74b66a0d6844d51f1a63018c7ebc44cf7e109e8e4bba57eefb55";
|
|
|
|
char const find[] = "6";
|
|
|
|
char const *result = Dqn_Str_Find(buf, find, Dqn_CharCountI(buf), Dqn_CharCountI(find));
|
2021-07-01 12:02:39 +00:00
|
|
|
DQN_TEST_EXPECT(testing_state, result != nullptr);
|
|
|
|
DQN_TEST_EXPECT(testing_state, result[0] == '6' && result[1] == 'a');
|
|
|
|
}
|
|
|
|
}
|
2021-07-22 09:59:27 +00:00
|
|
|
}
|
2021-07-01 12:02:39 +00:00
|
|
|
|
2021-07-28 11:10:25 +00:00
|
|
|
void Dqn_Test_String()
|
|
|
|
{
|
|
|
|
Dqn_TestingState testing_state = {};
|
|
|
|
DQN_TEST_DECLARE_GROUP_SCOPED(testing_state, "Dqn_String");
|
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Initialise with string literal w/ macro");
|
|
|
|
Dqn_String string = DQN_STRING("AB");
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, string.size == 2, "size: %I64d", string.size);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, string.cap == 2, "cap: %I64d", string.cap);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, string.str[0] == 'A', "string[0]: %c", string.str[0]);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, string.str[1] == 'B', "string[1]: %c", string.str[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Initialise with format string");
|
|
|
|
Dqn_ArenaAllocator arena = {};
|
|
|
|
Dqn_String string = Dqn_String_Fmt(&arena, "%s", "AB");
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, string.size == 2, "size: %I64d", string.size);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, string.cap == 2, "cap: %I64d", string.cap);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, string.str[0] == 'A', "string[0]: %c", string.str[0]);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, string.str[1] == 'B', "string[1]: %c", string.str[1]);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, string.str[2] == 0, "string[2]: %c", string.str[2]);
|
|
|
|
Dqn_ArenaAllocator_Free(&arena);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Copy string");
|
|
|
|
Dqn_ArenaAllocator arena = {};
|
|
|
|
Dqn_String string = DQN_STRING("AB");
|
|
|
|
Dqn_String copy = Dqn_String_Copy(string, &arena);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, copy.size == 2, "size: %I64d", copy.size);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, copy.cap == 2, "cap: %I64d", copy.cap);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, copy.str[0] == 'A', "copy[0]: %c", copy.str[0]);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, copy.str[1] == 'B', "copy[1]: %c", copy.str[1]);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, copy.str[2] == 0, "copy[2]: %c", copy.str[2]);
|
|
|
|
Dqn_ArenaAllocator_Free(&arena);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Trim whitespace around string");
|
|
|
|
Dqn_String string = Dqn_String_TrimWhitespaceAround(DQN_STRING(" AB "));
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, string.size == 2, "size: %I64d", string.size);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, string.cap == 2, "cap: %I64d", string.cap);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, string.str[0] == 'A', "string[0]: %c", string.str[0]);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, string.str[1] == 'B', "string[1]: %c", string.str[1]);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, string.str[2] == ' ', "string[1]: %c", string.str[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Allocate string from arena");
|
|
|
|
Dqn_ArenaAllocator arena = {};
|
|
|
|
Dqn_String string = Dqn_String_Allocate(&arena, 2, Dqn_ZeroMem::No);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, string.size == 0, "size: %I64d", string.size);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, string.cap == 2, "cap: %I64d", string.cap);
|
|
|
|
Dqn_ArenaAllocator_Free(&arena);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Append to allocated string");
|
|
|
|
Dqn_ArenaAllocator arena = {};
|
|
|
|
Dqn_String string = Dqn_String_Allocate(&arena, 2, Dqn_ZeroMem::No);
|
|
|
|
Dqn_String_AppendFmt(&string, "%c", 'A');
|
|
|
|
Dqn_String_AppendFmt(&string, "%c", 'B');
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, string.size == 2, "size: %I64d", string.size);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, string.cap == 2, "cap: %I64d", string.cap);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, string.str[0] == 'A', "string[0]: %c", string.str[0]);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, string.str[1] == 'B', "string[1]: %c", string.str[1]);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, string.str[2] == 0, "string[2]: %c", string.str[2]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-22 09:59:27 +00:00
|
|
|
void Dqn_Test_StringBuilder()
|
|
|
|
{
|
|
|
|
Dqn_TestingState testing_state = {};
|
|
|
|
DQN_TEST_DECLARE_GROUP_SCOPED(testing_state, "Dqn_StringBuilder");
|
2021-07-28 11:10:25 +00:00
|
|
|
Dqn_ArenaAllocator arena = {};
|
2021-07-22 09:59:27 +00:00
|
|
|
// NOTE: Dqn_StringBuilder_Append
|
2021-07-01 12:02:39 +00:00
|
|
|
{
|
|
|
|
{
|
2021-07-28 11:10:25 +00:00
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Append variable size strings and build using heap arena");
|
2021-07-22 09:59:27 +00:00
|
|
|
Dqn_StringBuilder<> builder = {};
|
|
|
|
Dqn_StringBuilder_Append(&builder, "Abc", 1);
|
|
|
|
Dqn_StringBuilder_Append(&builder, "cd");
|
|
|
|
Dqn_isize size = 0;
|
2021-07-28 11:10:25 +00:00
|
|
|
char *result = Dqn_StringBuilder_Build(&builder, &arena, &size);
|
|
|
|
DQN_DEFER { Dqn_ArenaAllocator_Free(&arena); };
|
2021-07-01 12:02:39 +00:00
|
|
|
|
2021-07-22 09:59:27 +00:00
|
|
|
char const EXPECT_STR[] = "Acd";
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, size == Dqn_CharCountI(EXPECT_STR), "size: %zd", size);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, strncmp(result, EXPECT_STR, size) == 0, "result: %s", result);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-07-28 11:10:25 +00:00
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Append empty string and build using heap arena");
|
2021-07-22 09:59:27 +00:00
|
|
|
Dqn_StringBuilder<> builder = {};
|
|
|
|
Dqn_StringBuilder_Append(&builder, "");
|
|
|
|
Dqn_StringBuilder_Append(&builder, "");
|
|
|
|
Dqn_isize size = 0;
|
2021-07-28 11:10:25 +00:00
|
|
|
char *result = Dqn_StringBuilder_Build(&builder, &arena, &size);
|
|
|
|
DQN_DEFER { Dqn_ArenaAllocator_Free(&arena); };
|
2021-07-22 09:59:27 +00:00
|
|
|
|
|
|
|
char const EXPECT_STR[] = "";
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, size == Dqn_CharCountI(EXPECT_STR), "size: %zd", size);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, strncmp(result, EXPECT_STR, size) == 0, "result: %s", result);
|
2020-01-17 15:01:25 +00:00
|
|
|
}
|
2021-07-22 09:59:27 +00:00
|
|
|
|
|
|
|
{
|
2021-07-28 11:10:25 +00:00
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Append empty string onto string and build using heap arena");
|
2021-07-22 09:59:27 +00:00
|
|
|
Dqn_StringBuilder<> builder = {};
|
|
|
|
Dqn_StringBuilder_Append(&builder, "Acd");
|
|
|
|
Dqn_StringBuilder_Append(&builder, "");
|
|
|
|
Dqn_isize size = 0;
|
2021-07-28 11:10:25 +00:00
|
|
|
char *result = Dqn_StringBuilder_Build(&builder, &arena, &size);
|
|
|
|
DQN_DEFER { Dqn_ArenaAllocator_Free(&arena); };
|
2021-07-22 09:59:27 +00:00
|
|
|
|
|
|
|
char const EXPECT_STR[] = "Acd";
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, size == Dqn_CharCountI(EXPECT_STR), "size: %zd", size);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, strncmp(result, EXPECT_STR, size) == 0, "result: %s", result);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-07-28 11:10:25 +00:00
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Append nullptr and build using heap arena");
|
2021-07-22 09:59:27 +00:00
|
|
|
Dqn_StringBuilder<> builder = {};
|
|
|
|
Dqn_StringBuilder_Append(&builder, nullptr, 5);
|
|
|
|
Dqn_isize size = 0;
|
2021-07-28 11:10:25 +00:00
|
|
|
char *result = Dqn_StringBuilder_Build(&builder, &arena, &size);
|
|
|
|
DQN_DEFER { Dqn_ArenaAllocator_Free(&arena); };
|
2021-07-22 09:59:27 +00:00
|
|
|
|
|
|
|
char const EXPECT_STR[] = "";
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, size == Dqn_CharCountI(EXPECT_STR), "size: %zd", size);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, strncmp(result, EXPECT_STR, size) == 0, "result: %s", result);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-07-28 11:10:25 +00:00
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Append and require new linked buffer and build using heap arena");
|
2021-07-22 09:59:27 +00:00
|
|
|
Dqn_StringBuilder<2> builder = {};
|
2021-07-28 11:10:25 +00:00
|
|
|
Dqn_StringBuilder_InitWithArena(&builder, &arena);
|
2021-07-22 09:59:27 +00:00
|
|
|
Dqn_StringBuilder_Append(&builder, "A");
|
|
|
|
Dqn_StringBuilder_Append(&builder, "z"); // Should force a new memory block
|
|
|
|
Dqn_StringBuilder_Append(&builder, "tec");
|
|
|
|
Dqn_isize size = 0;
|
2021-07-28 11:10:25 +00:00
|
|
|
char *result = Dqn_StringBuilder_Build(&builder, &arena, &size);
|
|
|
|
DQN_DEFER { Dqn_ArenaAllocator_Free(&arena); };
|
2021-07-22 09:59:27 +00:00
|
|
|
|
|
|
|
char const EXPECT_STR[] = "Aztec";
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, size == Dqn_CharCountI(EXPECT_STR), "size: %zd", size);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, strncmp(result, EXPECT_STR, size) == 0, "result: %s", result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: Dqn_StringBuilder_AppendChar
|
|
|
|
{
|
2021-07-28 11:10:25 +00:00
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Append char and build using heap arena");
|
2021-07-22 09:59:27 +00:00
|
|
|
Dqn_StringBuilder<> builder = {};
|
|
|
|
Dqn_StringBuilder_AppendChar(&builder, 'a');
|
|
|
|
Dqn_StringBuilder_AppendChar(&builder, 'b');
|
|
|
|
Dqn_isize size = 0;
|
2021-07-28 11:10:25 +00:00
|
|
|
char *result = Dqn_StringBuilder_Build(&builder, &arena, &size);
|
|
|
|
DQN_DEFER { Dqn_ArenaAllocator_Free(&arena); };
|
2021-07-22 09:59:27 +00:00
|
|
|
|
|
|
|
char const EXPECT_STR[] = "ab";
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, size == Dqn_CharCountI(EXPECT_STR), "size: %zd", size);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, strncmp(result, EXPECT_STR, size) == 0, "result: %s", result);
|
2020-01-17 15:01:25 +00:00
|
|
|
}
|
2021-07-22 09:59:27 +00:00
|
|
|
|
|
|
|
// NOTE: Dqn_StringBuilder_AppendFmt
|
|
|
|
{
|
|
|
|
{
|
2021-07-28 11:10:25 +00:00
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Append format string and build using heap arena");
|
2021-07-22 09:59:27 +00:00
|
|
|
Dqn_StringBuilder<> builder = {};
|
|
|
|
Dqn_StringBuilder_AppendFmt(&builder, "Number: %d, String: %s, ", 4, "Hello Sailor");
|
|
|
|
Dqn_StringBuilder_AppendFmt(&builder, "Extra Stuff");
|
|
|
|
Dqn_isize size = 0;
|
2021-07-28 11:10:25 +00:00
|
|
|
char *result = Dqn_StringBuilder_Build(&builder, &arena, &size);
|
|
|
|
DQN_DEFER { Dqn_ArenaAllocator_Free(&arena); };
|
2021-07-22 09:59:27 +00:00
|
|
|
|
|
|
|
char const EXPECT_STR[] = "Number: 4, String: Hello Sailor, Extra Stuff";
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, size == Dqn_CharCountI(EXPECT_STR), "size: %zd", size);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, strncmp(result, EXPECT_STR, size) == 0, "result: %s", result);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-07-28 11:10:25 +00:00
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Append nullptr format string and build using heap arena");
|
2021-07-22 09:59:27 +00:00
|
|
|
Dqn_StringBuilder<> builder = {};
|
|
|
|
Dqn_StringBuilder_AppendFmt(&builder, nullptr);
|
|
|
|
Dqn_isize size = 0;
|
2021-07-28 11:10:25 +00:00
|
|
|
char *result = Dqn_StringBuilder_Build(&builder, &arena, &size);
|
|
|
|
DQN_DEFER { Dqn_ArenaAllocator_Free(&arena); };
|
2021-07-22 09:59:27 +00:00
|
|
|
|
|
|
|
char const EXPECT_STR[] = "";
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, size == Dqn_CharCountI(EXPECT_STR), "size: %zd", size);
|
|
|
|
DQN_TEST_EXPECT_MSG(testing_state, strncmp(result, EXPECT_STR, size) == 0, "result: %s", result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Dqn_Test_TicketMutex()
|
|
|
|
{
|
|
|
|
Dqn_TestingState testing_state = {};
|
|
|
|
DQN_TEST_DECLARE_GROUP_SCOPED(testing_state, "Dqn_TicketMutex");
|
|
|
|
{
|
|
|
|
// 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.
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Ticket mutex start and stop");
|
|
|
|
Dqn_TicketMutex mutex = {};
|
|
|
|
Dqn_TicketMutex_Begin(&mutex);
|
|
|
|
Dqn_TicketMutex_End(&mutex);
|
2021-07-22 15:35:08 +00:00
|
|
|
DQN_TEST_EXPECT(testing_state, mutex.ticket == mutex.serving);
|
2021-07-22 09:59:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
DQN_TEST_START_SCOPE(testing_state, "Ticket mutex start and stop w/ advanced API");
|
|
|
|
Dqn_TicketMutex mutex = {};
|
|
|
|
unsigned int ticket_a = Dqn_TicketMutex_MakeTicket(&mutex);
|
|
|
|
unsigned int ticket_b = Dqn_TicketMutex_MakeTicket(&mutex);
|
|
|
|
DQN_TEST_EXPECT(testing_state, DQN_CAST(bool)Dqn_TicketMutex_CanLock(&mutex, ticket_b) == false);
|
|
|
|
DQN_TEST_EXPECT(testing_state, DQN_CAST(bool)Dqn_TicketMutex_CanLock(&mutex, ticket_a) == true);
|
|
|
|
|
|
|
|
Dqn_TicketMutex_BeginTicket(&mutex, ticket_a);
|
|
|
|
Dqn_TicketMutex_End(&mutex);
|
|
|
|
Dqn_TicketMutex_BeginTicket(&mutex, ticket_b);
|
|
|
|
Dqn_TicketMutex_End(&mutex);
|
|
|
|
|
2021-07-22 15:35:08 +00:00
|
|
|
DQN_TEST_EXPECT(testing_state, mutex.ticket == mutex.serving);
|
2021-07-22 09:59:27 +00:00
|
|
|
DQN_TEST_EXPECT(testing_state, mutex.ticket == ticket_b + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Dqn_Test_RunSuite()
|
|
|
|
{
|
|
|
|
Dqn_Test_Array();
|
|
|
|
Dqn_Test_FixedArray();
|
|
|
|
Dqn_Test_FixedString();
|
2021-08-06 14:54:53 +00:00
|
|
|
Dqn_Test_Hex();
|
2021-07-22 15:35:08 +00:00
|
|
|
Dqn_Test_Intrinsics();
|
2021-07-22 09:59:27 +00:00
|
|
|
Dqn_Test_M4();
|
2021-08-06 14:54:53 +00:00
|
|
|
Dqn_Test_DSMap();
|
2021-07-28 11:10:25 +00:00
|
|
|
Dqn_Test_Map();
|
2021-07-22 09:59:27 +00:00
|
|
|
Dqn_Test_Rect();
|
|
|
|
Dqn_Test_Str();
|
2021-07-28 11:10:25 +00:00
|
|
|
Dqn_Test_String();
|
2021-07-22 09:59:27 +00:00
|
|
|
Dqn_Test_StringBuilder();
|
|
|
|
Dqn_Test_TicketMutex();
|
2021-08-06 14:54:53 +00:00
|
|
|
|
2021-07-28 11:10:25 +00:00
|
|
|
fprintf(stdout, "Summary: %d/%d tests succeeded\n", g_dqn_test_total_good_tests, g_dqn_test_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
|
|
|
|