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.
|
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_DSMAP // Dqn_DSMap
|
2022-11-24 13:35:53 +00:00
|
|
|
#define DQN_WITH_FIXED_ARRAY // Dqn_FArray
|
|
|
|
#define DQN_WITH_FIXED_STRING // Dqn_FString8
|
2021-08-15 13:57:18 +00:00
|
|
|
#define DQN_WITH_HEX // Dqn_Hex and friends ...
|
2022-11-23 13:12:21 +00:00
|
|
|
#define DQN_WITH_JSON_BUILDER // Dqn_JSONBuilder
|
2021-08-15 13:57:18 +00:00
|
|
|
#define DQN_WITH_MAP // Dqn_Map
|
|
|
|
#define DQN_WITH_MATH // Dqn_V2/3/4/Mat4 and friends ...
|
2022-11-24 13:35:53 +00:00
|
|
|
#define DQN_WITH_WIN_NET //
|
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"
|
|
|
|
|
2022-02-17 10:01:31 +00:00
|
|
|
#define DQN_TESTER_IMPLEMENTATION
|
|
|
|
#include "dqn_tester.h"
|
2019-08-24 01:39:12 +00:00
|
|
|
|
2022-02-17 10:01:31 +00:00
|
|
|
Dqn_Tester Dqn_Test_File()
|
2021-08-15 13:57:18 +00:00
|
|
|
{
|
2022-02-17 10:01:31 +00:00
|
|
|
Dqn_Tester test = {};
|
2022-10-09 09:51:23 +00:00
|
|
|
DQN_TESTER_BEGIN_GROUP("Dqn_Fs");
|
2021-08-15 13:57:18 +00:00
|
|
|
{
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_Arena arena = {};
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_Allocator allocator = Dqn_Arena_Allocator(&arena);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Make directory recursive \"abcd/efgh\"");
|
2022-10-12 08:17:51 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, Dqn_Fs_MakeDir(DQN_STRING8("abcd/efgh")), "Failed to make directory");
|
|
|
|
DQN_TESTER_ASSERTF(&test, Dqn_Fs_DirExists(DQN_STRING8("abcd")), "Directory was not made");
|
|
|
|
DQN_TESTER_ASSERTF(&test, Dqn_Fs_DirExists(DQN_STRING8("abcd/efgh")), "Subdirectory was not made");
|
|
|
|
DQN_TESTER_ASSERTF(&test, Dqn_Fs_Exists(DQN_STRING8("abcd")) == false, "This function should only return true for files");
|
|
|
|
DQN_TESTER_ASSERTF(&test, Dqn_Fs_Exists(DQN_STRING8("abcd/efgh")) == false, "This function should only return true for files");
|
|
|
|
DQN_TESTER_ASSERTF(&test, Dqn_Fs_Delete(DQN_STRING8("abcd/efgh")), "Failed to delete directory");
|
|
|
|
DQN_TESTER_ASSERTF(&test, Dqn_Fs_Delete(DQN_STRING8("abcd")), "Failed to cleanup directory");
|
2023-03-11 14:28:17 +00:00
|
|
|
Dqn_Arena_Free(&arena, Dqn_ZeroMem_No);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// NOTE: Write step
|
2022-10-09 09:51:23 +00:00
|
|
|
Dqn_String8 const SRC_FILE = DQN_STRING8("dqn_test_file");
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Write file, read it, copy it, move it and delete it");
|
2023-03-11 14:28:17 +00:00
|
|
|
Dqn_b32 write_result = Dqn_Fs_WriteCString8(SRC_FILE.data, SRC_FILE.size, "test", 4);
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERT(&test, write_result);
|
2022-10-12 08:17:51 +00:00
|
|
|
DQN_TESTER_ASSERT(&test, Dqn_Fs_Exists(SRC_FILE));
|
2021-08-15 13:57:18 +00:00
|
|
|
|
|
|
|
// NOTE: Read step
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_Arena arena = {};
|
2023-03-11 14:28:17 +00:00
|
|
|
Dqn_String8 read_file = Dqn_Fs_ReadString8(SRC_FILE, Dqn_Arena_Allocator(&arena));
|
2022-10-12 08:17:51 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, Dqn_String8_IsValid(read_file), "Failed to load file");
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, read_file.size == 4, "File read wrong amount of bytes");
|
2022-10-12 08:17:51 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, Dqn_String8_Eq(read_file, DQN_STRING8("test")), "read(%zu): %.*s", read_file.size, DQN_STRING_FMT(read_file));
|
2021-08-15 13:57:18 +00:00
|
|
|
|
|
|
|
// NOTE: Copy step
|
2022-10-09 09:51:23 +00:00
|
|
|
Dqn_String8 const COPY_FILE = DQN_STRING8("dqn_test_file_copy");
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_b32 copy_result = Dqn_Fs_Copy(SRC_FILE, COPY_FILE, true /*overwrite*/);
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERT(&test, copy_result);
|
2022-10-12 08:17:51 +00:00
|
|
|
DQN_TESTER_ASSERT(&test, Dqn_Fs_Exists(COPY_FILE));
|
2021-08-15 13:57:18 +00:00
|
|
|
|
|
|
|
// NOTE: Move step
|
2022-10-09 09:51:23 +00:00
|
|
|
Dqn_String8 const MOVE_FILE = DQN_STRING8("dqn_test_file_move");
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_b32 move_result = Dqn_Fs_Move(COPY_FILE, MOVE_FILE, true /*overwrite*/);
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERT(&test, move_result);
|
2022-10-12 08:17:51 +00:00
|
|
|
DQN_TESTER_ASSERT(&test, Dqn_Fs_Exists(MOVE_FILE));
|
|
|
|
DQN_TESTER_ASSERTF(&test, Dqn_Fs_Exists(COPY_FILE) == false, "Moving a file should remove the original");
|
2021-08-15 13:57:18 +00:00
|
|
|
|
|
|
|
// NOTE: Delete step
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_b32 delete_src_file = Dqn_Fs_Delete(SRC_FILE);
|
|
|
|
Dqn_b32 delete_moved_file = Dqn_Fs_Delete(MOVE_FILE);
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERT(&test, delete_src_file);
|
|
|
|
DQN_TESTER_ASSERT(&test, delete_moved_file);
|
2021-08-15 13:57:18 +00:00
|
|
|
|
|
|
|
// NOTE: Deleting non-existent file fails
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_b32 delete_non_existent_src_file = Dqn_Fs_Delete(SRC_FILE);
|
|
|
|
Dqn_b32 delete_non_existent_moved_file = Dqn_Fs_Delete(MOVE_FILE);
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERT(&test, delete_non_existent_moved_file == false);
|
|
|
|
DQN_TESTER_ASSERT(&test, delete_non_existent_src_file == false);
|
2021-08-15 13:57:18 +00:00
|
|
|
|
2023-03-11 14:28:17 +00:00
|
|
|
Dqn_Arena_Free(&arena, Dqn_ZeroMem_No);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_END_GROUP(&test);
|
2021-09-24 13:41:58 +00:00
|
|
|
return test;
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
2022-02-17 10:01:31 +00:00
|
|
|
Dqn_Tester Dqn_Test_FixedArray()
|
2021-07-22 09:59:27 +00:00
|
|
|
{
|
2022-02-17 10:01:31 +00:00
|
|
|
Dqn_Tester test = {};
|
2021-08-15 13:57:18 +00:00
|
|
|
#if defined(DQN_WITH_FIXED_ARRAY)
|
2022-11-24 13:35:53 +00:00
|
|
|
DQN_TESTER_BEGIN_GROUP("Dqn_FArray");
|
|
|
|
// NOTE: Dqn_FArray_Init
|
2021-07-22 09:59:27 +00:00
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Initialise from raw array");
|
2021-07-22 09:59:27 +00:00
|
|
|
int raw_array[] = {1, 2};
|
2022-11-24 13:35:53 +00:00
|
|
|
auto array = Dqn_FArray_Init<int, 4>(raw_array, (int)Dqn_CArray_Count(raw_array));
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERT(&test, array.size == 2);
|
2022-11-24 13:35:53 +00:00
|
|
|
DQN_TESTER_ASSERT(&test, array.data[0] == 1);
|
|
|
|
DQN_TESTER_ASSERT(&test, array.data[1] == 2);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-07-22 09:59:27 +00:00
|
|
|
}
|
2021-07-01 12:02:39 +00:00
|
|
|
|
2022-11-24 13:35:53 +00:00
|
|
|
// NOTE: Dqn_FArray_EraseStable
|
2021-07-22 09:59:27 +00:00
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Erase stable 1 element from array");
|
2021-07-22 09:59:27 +00:00
|
|
|
int raw_array[] = {1, 2, 3};
|
2022-11-24 13:35:53 +00:00
|
|
|
auto array = Dqn_FArray_Init<int, 4>(raw_array, (int)Dqn_CArray_Count(raw_array));
|
|
|
|
Dqn_FArray_EraseStable(&array, 1);
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERT(&test, array.size == 2);
|
2022-11-24 13:35:53 +00:00
|
|
|
DQN_TESTER_ASSERT(&test, array.data[0] == 1);
|
|
|
|
DQN_TESTER_ASSERT(&test, array.data[1] == 3);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-07-22 09:59:27 +00:00
|
|
|
}
|
2019-08-24 01:39:12 +00:00
|
|
|
|
2022-11-24 13:35:53 +00:00
|
|
|
// NOTE: Dqn_FArray_EraseUnstable
|
2021-07-22 09:59:27 +00:00
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Erase unstable 1 element from array");
|
2021-07-22 09:59:27 +00:00
|
|
|
int raw_array[] = {1, 2, 3};
|
2022-11-24 13:35:53 +00:00
|
|
|
auto array = Dqn_FArray_Init<int, 4>(raw_array, (int)Dqn_CArray_Count(raw_array));
|
|
|
|
DQN_TESTER_ASSERT(&test, Dqn_FArray_EraseUnstable(&array, 0));
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERT(&test, array.size == 2);
|
2022-11-24 13:35:53 +00:00
|
|
|
DQN_TESTER_ASSERT(&test, array.data[0] == 3);
|
|
|
|
DQN_TESTER_ASSERT(&test, array.data[1] == 2);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-07-22 09:59:27 +00:00
|
|
|
}
|
2019-08-24 03:38:58 +00:00
|
|
|
|
2022-11-24 13:35:53 +00:00
|
|
|
// NOTE: Dqn_FArray_Add
|
2021-07-22 09:59:27 +00:00
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Add 1 element to array");
|
2021-07-22 09:59:27 +00:00
|
|
|
int const ITEM = 2;
|
|
|
|
int raw_array[] = {1};
|
2022-11-24 13:35:53 +00:00
|
|
|
auto array = Dqn_FArray_Init<int, 4>(raw_array, (int)Dqn_CArray_Count(raw_array));
|
|
|
|
Dqn_FArray_Add(&array, ITEM);
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERT(&test, array.size == 2);
|
2022-11-24 13:35:53 +00:00
|
|
|
DQN_TESTER_ASSERT(&test, array.data[0] == 1);
|
|
|
|
DQN_TESTER_ASSERT(&test, array.data[1] == ITEM);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-07-22 09:59:27 +00:00
|
|
|
}
|
|
|
|
|
2022-11-24 13:35:53 +00:00
|
|
|
// NOTE: Dqn_FArray_Clear
|
2021-07-22 09:59:27 +00:00
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Clear array");
|
2021-07-22 09:59:27 +00:00
|
|
|
int raw_array[] = {1};
|
2022-11-24 13:35:53 +00:00
|
|
|
auto array = Dqn_FArray_Init<int, 4>(raw_array, (int)Dqn_CArray_Count(raw_array));
|
|
|
|
Dqn_FArray_Clear(&array, Dqn_ZeroMem_No);
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERT(&test, array.size == 0);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-07-22 09:59:27 +00:00
|
|
|
}
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_END_GROUP(&test);
|
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
|
|
|
|
2022-11-24 13:35:53 +00:00
|
|
|
Dqn_Tester Dqn_Test_FString8()
|
2021-07-22 09:59:27 +00:00
|
|
|
{
|
2022-02-17 10:01:31 +00:00
|
|
|
Dqn_Tester test = {};
|
2021-08-15 13:57:18 +00:00
|
|
|
#if defined(DQN_WITH_FIXED_STRING)
|
2022-11-24 13:35:53 +00:00
|
|
|
DQN_TESTER_BEGIN_GROUP("Dqn_FString8");
|
2019-08-24 03:38:58 +00:00
|
|
|
|
2022-11-24 13:35:53 +00:00
|
|
|
// NOTE: Dqn_FString8_Append
|
2021-07-22 09:59:27 +00:00
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Append too much fails");
|
2022-11-24 13:35:53 +00:00
|
|
|
Dqn_FString8<4> str = {};
|
|
|
|
DQN_TESTER_ASSERTF(&test, Dqn_FString8_AppendCString8(&str, "abcd") == false, "We need space for the null-terminator");
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2019-08-24 01:39:12 +00:00
|
|
|
}
|
|
|
|
|
2022-11-24 13:35:53 +00:00
|
|
|
// NOTE: Dqn_FString8_AppendFmt
|
2021-07-22 09:59:27 +00:00
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Append format string too much fails");
|
2022-11-24 13:35:53 +00:00
|
|
|
Dqn_FString8<4> str = {};
|
2023-03-11 14:28:17 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, Dqn_FString8_AppendF(&str, "abcd") == false, "We need space for the null-terminator");
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-07-22 09:59:27 +00:00
|
|
|
}
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_END_GROUP(&test);
|
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
|
|
|
}
|
|
|
|
|
2022-02-17 10:01:31 +00:00
|
|
|
Dqn_Tester Dqn_Test_Hex()
|
2021-08-06 14:54:53 +00:00
|
|
|
{
|
2022-02-17 10:01:31 +00:00
|
|
|
Dqn_Tester test = {};
|
2021-08-15 13:57:18 +00:00
|
|
|
#if defined(DQN_WITH_HEX)
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_BEGIN_GROUP("Dqn_Hex");
|
2021-08-06 14:54:53 +00:00
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Convert 0x123");
|
2023-03-11 14:28:17 +00:00
|
|
|
uint64_t result = Dqn_Hex_String8ToU64(DQN_STRING8("0x123"));
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, result == 0x123, "result: %zu", result);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-06 14:54:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Convert 0xFFFF");
|
2023-03-11 14:28:17 +00:00
|
|
|
uint64_t result = Dqn_Hex_String8ToU64(DQN_STRING8("0xFFFF"));
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, result == 0xFFFF, "result: %zu", result);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-06 14:54:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Convert FFFF");
|
2023-03-11 14:28:17 +00:00
|
|
|
uint64_t result = Dqn_Hex_String8ToU64(DQN_STRING8("FFFF"));
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, result == 0xFFFF, "result: %zu", result);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-06 14:54:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Convert abCD");
|
2023-03-11 14:28:17 +00:00
|
|
|
uint64_t result = Dqn_Hex_String8ToU64(DQN_STRING8("abCD"));
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, result == 0xabCD, "result: %zu", result);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-06 14:54:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Convert 0xabCD");
|
2023-03-11 14:28:17 +00:00
|
|
|
uint64_t result = Dqn_Hex_String8ToU64(DQN_STRING8("0xabCD"));
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, result == 0xabCD, "result: %zu", result);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-06 14:54:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Convert 0x");
|
2023-03-11 14:28:17 +00:00
|
|
|
uint64_t result = Dqn_Hex_String8ToU64(DQN_STRING8("0x"));
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, result == 0x0, "result: %zu", result);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-06 14:54:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Convert 0X");
|
2023-03-11 14:28:17 +00:00
|
|
|
uint64_t result = Dqn_Hex_String8ToU64(DQN_STRING8("0X"));
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, result == 0x0, "result: %zu", result);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-06 14:54:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Convert 3");
|
2023-03-11 14:28:17 +00:00
|
|
|
uint64_t result = Dqn_Hex_String8ToU64(DQN_STRING8("3"));
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, result == 3, "result: %zu", result);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-06 14:54:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Convert f");
|
2023-03-11 14:28:17 +00:00
|
|
|
uint64_t result = Dqn_Hex_String8ToU64(DQN_STRING8("f"));
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, result == 0xf, "result: %zu", result);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-06 14:54:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Convert g");
|
2023-03-11 14:28:17 +00:00
|
|
|
uint64_t result = Dqn_Hex_String8ToU64(DQN_STRING8("g"));
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, result == 0, "result: %zu", result);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-06 14:54:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Convert -0x3");
|
2023-03-11 14:28:17 +00:00
|
|
|
uint64_t result = Dqn_Hex_String8ToU64(DQN_STRING8("-0x3"));
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, result == 0, "result: %zu", result);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-06 14:54:53 +00:00
|
|
|
}
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_END_GROUP(&test);
|
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
|
|
|
}
|
|
|
|
|
2022-02-17 10:01:31 +00:00
|
|
|
Dqn_Tester Dqn_Test_M4()
|
2021-07-22 09:59:27 +00:00
|
|
|
{
|
2022-02-17 10:01:31 +00:00
|
|
|
Dqn_Tester test = {};
|
2021-08-15 13:57:18 +00:00
|
|
|
#if defined(DQN_WITH_MATH)
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_BEGIN_GROUP("Dqn_M4");
|
2021-07-22 09:59:27 +00:00
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Simple translate and scale matrix");
|
2022-10-12 08:17:51 +00:00
|
|
|
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);
|
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},
|
|
|
|
}};
|
|
|
|
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test,
|
2022-10-12 08:17:51 +00:00
|
|
|
memcmp(result.columns, EXPECT.columns, sizeof(EXPECT)) == 0,
|
|
|
|
"\nresult =\n%s\nexpected =\n%s",
|
|
|
|
Dqn_M4_ColumnMajorString(result).data,
|
|
|
|
Dqn_M4_ColumnMajorString(EXPECT).data);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-07-22 09:59:27 +00:00
|
|
|
}
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_END_GROUP(&test);
|
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
|
|
|
}
|
|
|
|
|
2022-02-17 10:01:31 +00:00
|
|
|
Dqn_Tester Dqn_Test_DSMap()
|
2021-08-06 14:54:53 +00:00
|
|
|
{
|
2022-02-17 10:01:31 +00:00
|
|
|
Dqn_Tester test = {};
|
2021-08-15 13:57:18 +00:00
|
|
|
#if defined(DQN_WITH_DSMAP)
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_BEGIN_GROUP("Dqn_DSMap");
|
2021-08-06 14:54:53 +00:00
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Add r-value item to map");
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_DSMap<int> map = Dqn_DSMap_Init<int>(128);
|
2023-03-11 14:28:17 +00:00
|
|
|
Dqn_DSMapItem<int> *entry = Dqn_DSMap_Add(&map, 3 /*hash*/, 5 /*value*/);
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, map.size == 128, "size: %I64d", map.size);
|
|
|
|
DQN_TESTER_ASSERTF(&test, map.count == 1, "count: %zu", map.count);
|
|
|
|
DQN_TESTER_ASSERTF(&test, entry->hash == 3, "hash: %zu", entry->hash);
|
|
|
|
DQN_TESTER_ASSERTF(&test, entry->value == 5, "value: %d", entry->value);
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_DSMap_Free(&map, Dqn_ZeroMem_No);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-06 14:54:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Add l-value item to map");
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_DSMap<int> map = Dqn_DSMap_Init<int>(128);
|
2021-08-06 14:54:53 +00:00
|
|
|
int value = 5;
|
2023-03-11 14:28:17 +00:00
|
|
|
Dqn_DSMapItem<int> *entry = Dqn_DSMap_Add(&map, 3 /*hash*/, value);
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, map.size == 128, "size: %I64d", map.size);
|
|
|
|
DQN_TESTER_ASSERTF(&test, map.count == 1, "count: %zu", map.count);
|
|
|
|
DQN_TESTER_ASSERTF(&test, entry->hash == 3, "hash: %zu", entry->hash);
|
|
|
|
DQN_TESTER_ASSERTF(&test, entry->value == 5, "value: %d", entry->value);
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_DSMap_Free(&map, Dqn_ZeroMem_No);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-06 14:54:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Get item from map");
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_DSMap<int> map = Dqn_DSMap_Init<int>(128);
|
2023-03-11 14:28:17 +00:00
|
|
|
Dqn_DSMapItem<int> *entry = Dqn_DSMap_Add(&map, 3 /*hash*/, 5 /*value*/);
|
|
|
|
Dqn_DSMapItem<int> *get_entry = Dqn_DSMap_Get(&map, 3 /*hash*/);
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, get_entry == entry, "get_entry: %p, entry: %p", get_entry, entry);
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_DSMap_Free(&map, Dqn_ZeroMem_No);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-06 14:54:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Get non-existent item from map");
|
2023-03-11 14:28:17 +00:00
|
|
|
Dqn_DSMap<int> map = Dqn_DSMap_Init<int>(128);
|
|
|
|
Dqn_DSMapItem<int> *entry = Dqn_DSMap_Get(&map, 3 /*hash*/);
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERT(&test, entry == nullptr);
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_DSMap_Free(&map, Dqn_ZeroMem_No);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-06 14:54:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Erase item from map");
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_DSMap<int> map = Dqn_DSMap_Init<int>(128);
|
2023-03-11 14:28:17 +00:00
|
|
|
Dqn_DSMap_Add(&map, 3 /*hash*/, 5 /*value*/);
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, map.count == 1, "count: %I64d", map.count);
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_DSMap_Erase(&map, 3 /*hash*/, Dqn_ZeroMem_No);
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, map.count == 0, "count: %I64d", map.count);
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_DSMap_Free(&map, Dqn_ZeroMem_No);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-06 14:54:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Erase non-existent item from map");
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_DSMap<int> map = Dqn_DSMap_Init<int>(128);
|
|
|
|
Dqn_DSMap_Erase(&map, 3 /*hash*/, Dqn_ZeroMem_No);
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, map.count == 0, "count: %I64d", map.count);
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_DSMap_Free(&map, Dqn_ZeroMem_No);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-06 14:54:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Test resize on maximum load");
|
2021-08-06 14:54:53 +00:00
|
|
|
const Dqn_isize INIT_SIZE = 4;
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_DSMap<int> map = Dqn_DSMap_Init<int>(INIT_SIZE);
|
2023-03-11 14:28:17 +00:00
|
|
|
Dqn_DSMap_Add(&map, 0 /*hash*/, 5 /*value*/);
|
|
|
|
Dqn_DSMap_Add(&map, 1 /*hash*/, 5 /*value*/);
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&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
|
2023-03-11 14:28:17 +00:00
|
|
|
Dqn_DSMap_Add(&map, 6 /*hash*/, 5 /*value*/);
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, map.count == 3, "count: %I64d", map.count);
|
|
|
|
DQN_TESTER_ASSERTF(&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
|
2022-10-12 08:17:51 +00:00
|
|
|
DQN_TESTER_ASSERT(&test, map.slots[0].occupied == DQN_CAST(uint8_t)true);
|
|
|
|
DQN_TESTER_ASSERT(&test, map.slots[1].occupied == DQN_CAST(uint8_t)true);
|
|
|
|
DQN_TESTER_ASSERT(&test, map.slots[2].occupied == DQN_CAST(uint8_t)false);
|
|
|
|
DQN_TESTER_ASSERT(&test, map.slots[3].occupied == DQN_CAST(uint8_t)false);
|
|
|
|
DQN_TESTER_ASSERT(&test, map.slots[4].occupied == DQN_CAST(uint8_t)false);
|
|
|
|
DQN_TESTER_ASSERT(&test, map.slots[5].occupied == DQN_CAST(uint8_t)false);
|
|
|
|
DQN_TESTER_ASSERT(&test, map.slots[6].occupied == DQN_CAST(uint8_t)true);
|
|
|
|
DQN_TESTER_ASSERT(&test, map.slots[7].occupied == DQN_CAST(uint8_t)false);
|
2022-02-17 10:01:31 +00:00
|
|
|
|
|
|
|
DQN_TESTER_ASSERTF(&test, map.slots[0].value == 5, "value: %d", map.slots[0].value);
|
|
|
|
DQN_TESTER_ASSERTF(&test, map.slots[1].value == 5, "value: %d", map.slots[1].value);
|
|
|
|
DQN_TESTER_ASSERTF(&test, map.slots[6].value == 5, "value: %d", map.slots[6].value);
|
2021-08-06 14:54:53 +00:00
|
|
|
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_DSMap_Free(&map, Dqn_ZeroMem_No);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-06 14:54:53 +00:00
|
|
|
}
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_END_GROUP(&test);
|
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
|
|
|
}
|
|
|
|
|
2022-02-17 10:01:31 +00:00
|
|
|
Dqn_Tester Dqn_Test_Map()
|
2021-07-28 11:10:25 +00:00
|
|
|
{
|
2022-02-17 10:01:31 +00:00
|
|
|
Dqn_Tester test = {};
|
2021-08-15 13:57:18 +00:00
|
|
|
#if defined(DQN_WITH_MAP)
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_BEGIN_GROUP("Dqn_Map");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_Arena arena = {};
|
2021-07-28 11:10:25 +00:00
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Add r-value item to map");
|
2022-10-12 08:17:51 +00:00
|
|
|
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);
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, map.size == 1, "size: %I64d", map.size);
|
|
|
|
DQN_TESTER_ASSERTF(&test, map.count == 1, "count: %zu", map.count);
|
|
|
|
DQN_TESTER_ASSERTF(&test, map.chain_count == 0, "chain_count: %zu", map.chain_count);
|
|
|
|
DQN_TESTER_ASSERTF(&test, map.free_list == nullptr, "free_list: %p", map.free_list);
|
|
|
|
DQN_TESTER_ASSERTF(&test, entry->hash == 3, "hash: %zu", entry->hash);
|
|
|
|
DQN_TESTER_ASSERTF(&test, entry->value == 5, "value: %d", entry->value);
|
|
|
|
DQN_TESTER_ASSERTF(&test, entry->next == nullptr, "next: %p", entry->next);
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_Arena_Free(&arena, Dqn_ZeroMem_No);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-07-28 11:10:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Add l-value item to map");
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_Map<int> map = Dqn_Map_InitWithArena<int>(&arena, 1);
|
2021-07-28 11:10:25 +00:00
|
|
|
int value = 5;
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_MapEntry<int> *entry = Dqn_Map_Add(&map, 3 /*hash*/, value, Dqn_MapCollideRule::Overwrite);
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, map.size == 1, "size: %I64d", map.size);
|
|
|
|
DQN_TESTER_ASSERTF(&test, map.count == 1, "count: %zu", map.count);
|
|
|
|
DQN_TESTER_ASSERTF(&test, map.chain_count == 0, "chain_count: %zu", map.chain_count);
|
|
|
|
DQN_TESTER_ASSERTF(&test, map.free_list == nullptr, "free_list: %p", map.free_list);
|
|
|
|
DQN_TESTER_ASSERTF(&test, entry->hash == 3, "hash: %zu", entry->hash);
|
|
|
|
DQN_TESTER_ASSERTF(&test, entry->value == 5, "value: %d", entry->value);
|
|
|
|
DQN_TESTER_ASSERTF(&test, entry->next == nullptr, "next: %p", entry->next);
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_Arena_Free(&arena, Dqn_ZeroMem_No);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-07-28 11:10:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Add r-value item and overwrite on collision");
|
2022-10-12 08:17:51 +00:00
|
|
|
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);
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, map.size == 1, "size: %zu", map.size);
|
|
|
|
DQN_TESTER_ASSERTF(&test, map.count == 1, "count: %zu", map.count);
|
|
|
|
DQN_TESTER_ASSERTF(&test, map.chain_count == 0, "chain_count: %zu", map.chain_count);
|
|
|
|
DQN_TESTER_ASSERTF(&test, map.free_list == nullptr, "free_list: %p", map.free_list);
|
|
|
|
DQN_TESTER_ASSERTF(&test, entry_a == entry_b, "Expected entry to be overwritten");
|
|
|
|
DQN_TESTER_ASSERTF(&test, entry_b->hash == 4, "hash: %zu", entry_b->hash);
|
|
|
|
DQN_TESTER_ASSERTF(&test, entry_b->value == 6, "value: %d", entry_b->value);
|
|
|
|
DQN_TESTER_ASSERTF(&test, entry_b->next == nullptr, "next: %p", entry_b->next);
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_Arena_Free(&arena, Dqn_ZeroMem_No);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-07-28 11:10:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Add r-value item and fail on collision");
|
2022-10-12 08:17:51 +00:00
|
|
|
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);
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, entry_b == nullptr, "Expected entry to be overwritten");
|
|
|
|
DQN_TESTER_ASSERTF(&test, map.size == 1, "size: %zu", map.size);
|
|
|
|
DQN_TESTER_ASSERTF(&test, map.count == 1, "count: %zu", map.count);
|
|
|
|
DQN_TESTER_ASSERTF(&test, map.chain_count == 0, "chain_count: %zu", map.chain_count);
|
|
|
|
DQN_TESTER_ASSERTF(&test, map.free_list == nullptr, "free_list: %p", map.free_list);
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_Arena_Free(&arena, Dqn_ZeroMem_No);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-07-28 11:10:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Add r-value item and chain on collision");
|
2022-10-12 08:17:51 +00:00
|
|
|
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);
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, map.size == 1, "size: %zu", map.size);
|
|
|
|
DQN_TESTER_ASSERTF(&test, map.count == 1, "count: %zu", map.count);
|
|
|
|
DQN_TESTER_ASSERTF(&test, map.chain_count == 1, "chain_count: %zu", map.chain_count);
|
|
|
|
DQN_TESTER_ASSERTF(&test, map.free_list == nullptr, "free_list: %p", map.free_list);
|
|
|
|
DQN_TESTER_ASSERTF(&test, entry_a != entry_b, "Expected colliding entry to be chained");
|
|
|
|
DQN_TESTER_ASSERTF(&test, entry_a->next == entry_b, "Expected chained entry to be next to our first map entry");
|
|
|
|
DQN_TESTER_ASSERTF(&test, entry_b->hash == 4, "hash: %zu", entry_b->hash);
|
|
|
|
DQN_TESTER_ASSERTF(&test, entry_b->value == 6, "value: %d", entry_b->value);
|
|
|
|
DQN_TESTER_ASSERTF(&test, entry_b->next == nullptr, "next: %p", entry_b->next);
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_Arena_Free(&arena, Dqn_ZeroMem_No);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-07-28 11:10:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Add r-value item and get them back out again");
|
2022-10-12 08:17:51 +00:00
|
|
|
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);
|
2021-07-28 11:10:25 +00:00
|
|
|
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_MapEntry<int> *entry_a_copy = Dqn_Map_Get(&map, 3 /*hash*/);
|
|
|
|
Dqn_MapEntry<int> *entry_b_copy = Dqn_Map_Get(&map, 4 /*hash*/);
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, map.size == 1, "size: %zu", map.size);
|
|
|
|
DQN_TESTER_ASSERTF(&test, map.count == 1, "count: %zu", map.count);
|
|
|
|
DQN_TESTER_ASSERTF(&test, map.chain_count == 1, "chain_count: %zu", map.chain_count);
|
|
|
|
DQN_TESTER_ASSERTF(&test, map.free_list == nullptr, "free_list: %p", map.free_list);
|
|
|
|
DQN_TESTER_ASSERT(&test, entry_a_copy == entry_a);
|
|
|
|
DQN_TESTER_ASSERT(&test, entry_b_copy == entry_b);
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_Arena_Free(&arena, Dqn_ZeroMem_No);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-07-28 11:10:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Add r-value item and erase it");
|
2022-10-12 08:17:51 +00:00
|
|
|
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*/);
|
2021-07-28 11:10:25 +00:00
|
|
|
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_Map_Erase(&map, 3 /*hash*/, Dqn_ZeroMem_No);
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, map.size == 1, "size: %zu", map.size);
|
|
|
|
DQN_TESTER_ASSERTF(&test, map.count == 1, "count: %zu", map.count);
|
|
|
|
DQN_TESTER_ASSERTF(&test, map.chain_count == 0, "chain_count: %zu", map.chain_count);
|
|
|
|
DQN_TESTER_ASSERTF(&test, map.free_list != nullptr, "free_list: %p", map.free_list);
|
2021-07-28 11:10:25 +00:00
|
|
|
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, map.free_list->hash == 3, "Entry should not be zeroed out on erase");
|
|
|
|
DQN_TESTER_ASSERTF(&test, map.free_list->value == 5, "Entry should not be zeroed out on erase");
|
|
|
|
DQN_TESTER_ASSERTF(&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
|
|
|
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_MapEntry<int> *entry = Dqn_Map_Get(&map, 4 /*hash*/);
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, entry->hash == 4, "hash: %zu", entry->hash);
|
|
|
|
DQN_TESTER_ASSERTF(&test, entry->value == 6, "value: %d", entry->value);
|
|
|
|
DQN_TESTER_ASSERTF(&test, entry->next == nullptr, "next: %p", entry->next);
|
2021-07-28 11:10:25 +00:00
|
|
|
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_Arena_Free(&arena, Dqn_ZeroMem_No);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-07-28 11:10:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Add r-value item and erase it, zeroing the memory out");
|
2022-10-12 08:17:51 +00:00
|
|
|
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*/);
|
2021-07-28 11:10:25 +00:00
|
|
|
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_Map_Erase(&map, 3 /*hash*/, Dqn_ZeroMem_Yes);
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, map.size == 1, "size: %zu", map.size);
|
|
|
|
DQN_TESTER_ASSERTF(&test, map.count == 1, "count: %zu", map.count);
|
|
|
|
DQN_TESTER_ASSERTF(&test, map.chain_count == 0, "chain_count: %zu", map.chain_count);
|
|
|
|
DQN_TESTER_ASSERTF(&test, map.free_list != nullptr, "free_list: %p", map.free_list);
|
2021-07-28 11:10:25 +00:00
|
|
|
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, map.free_list->hash == 0, "Entry should be zeroed out on erase");
|
|
|
|
DQN_TESTER_ASSERTF(&test, map.free_list->value == 0, "Entry should be zeroed out on erase");
|
|
|
|
DQN_TESTER_ASSERTF(&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
|
|
|
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_MapEntry<int> *entry = Dqn_Map_Get(&map, 4 /*hash*/);
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, entry->hash == 4, "hash: %zu", entry->hash);
|
|
|
|
DQN_TESTER_ASSERTF(&test, entry->value == 6, "value: %d", entry->value);
|
|
|
|
DQN_TESTER_ASSERTF(&test, entry->next == nullptr, "next: %p", entry->next);
|
2021-07-28 11:10:25 +00:00
|
|
|
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_Arena_Free(&arena, Dqn_ZeroMem_No);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
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
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_END_GROUP(&test);
|
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
|
|
|
}
|
|
|
|
|
2022-02-17 10:01:31 +00:00
|
|
|
Dqn_Tester Dqn_Test_Intrinsics()
|
2021-07-22 15:35:08 +00:00
|
|
|
{
|
2022-02-17 10:01:31 +00:00
|
|
|
Dqn_Tester 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.
|
|
|
|
|
2022-10-12 08:17:51 +00:00
|
|
|
DQN_TESTER_BEGIN_GROUP("Dqn_Atomic_");
|
2021-07-22 15:35:08 +00:00
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Dqn_Atomic_AddU32");
|
2022-10-12 08:17:51 +00:00
|
|
|
uint32_t val = 0;
|
|
|
|
Dqn_Atomic_AddU32(&val, 1);
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, val == 1, "val: %u", val);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-07-22 15:35:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Dqn_Atomic_AddU64");
|
2022-10-12 08:17:51 +00:00
|
|
|
uint64_t val = 0;
|
|
|
|
Dqn_Atomic_AddU64(&val, 1);
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, val == 1, "val: %zu", val);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-07-22 15:35:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Dqn_Atomic_SubU32");
|
2022-10-12 08:17:51 +00:00
|
|
|
uint32_t val = 1;
|
|
|
|
Dqn_Atomic_SubU32(&val, 1);
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, val == 0, "val: %u", val);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-07-22 15:35:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Dqn_Atomic_SubU64");
|
2022-10-12 08:17:51 +00:00
|
|
|
uint64_t val = 1;
|
|
|
|
Dqn_Atomic_SubU64(&val, 1);
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, val == 0, "val: %zu", val);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-07-22 15:35:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Dqn_Atomic_SetValue32");
|
2021-07-22 15:35:08 +00:00
|
|
|
long a = 0;
|
|
|
|
long b = 111;
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_Atomic_SetValue32(&a, b);
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, a == b, "a: %lu, b: %lu", a, b);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-07-22 15:35:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Dqn_Atomic_SetValue64");
|
2022-10-12 08:17:51 +00:00
|
|
|
int64_t a = 0;
|
|
|
|
int64_t b = 111;
|
|
|
|
Dqn_Atomic_SetValue64(DQN_CAST(uint64_t *)&a, b);
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, a == b, "a: %I64i, b: %I64i", a, b);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-07-22 15:35:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Dqn_CPUClockCycle");
|
2021-07-22 15:35:08 +00:00
|
|
|
Dqn_CPUClockCycle();
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-07-22 15:35:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Dqn_CompilerReadBarrierAndCPUReadFence");
|
2021-07-22 15:35:08 +00:00
|
|
|
Dqn_CompilerReadBarrierAndCPUReadFence;
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-07-22 15:35:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Dqn_CompilerWriteBarrierAndCPUWriteFence");
|
2021-07-22 15:35:08 +00:00
|
|
|
Dqn_CompilerWriteBarrierAndCPUWriteFence;
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-07-22 15:35:08 +00:00
|
|
|
}
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_END_GROUP(&test);
|
2021-09-24 13:41:58 +00:00
|
|
|
return test;
|
2021-07-22 15:35:08 +00:00
|
|
|
}
|
|
|
|
|
2022-02-17 10:01:31 +00:00
|
|
|
Dqn_Tester Dqn_Test_Rect()
|
2021-07-22 09:59:27 +00:00
|
|
|
{
|
2022-02-17 10:01:31 +00:00
|
|
|
Dqn_Tester test = {};
|
2021-08-15 13:57:18 +00:00
|
|
|
#if defined(DQN_WITH_MATH)
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_BEGIN_GROUP("Dqn_Rect");
|
2022-10-12 08:17:51 +00:00
|
|
|
// NOTE: Dqn_Rect_Intersection
|
2019-08-24 01:39:12 +00:00
|
|
|
{
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "No intersection");
|
2022-10-12 08:17:51 +00:00
|
|
|
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);
|
2021-07-22 09:59:27 +00:00
|
|
|
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&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);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2019-08-24 01:39:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "A's min intersects B");
|
2022-10-12 08:17:51 +00:00
|
|
|
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);
|
2021-07-22 09:59:27 +00:00
|
|
|
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&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);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2019-08-24 01:39:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "B's min intersects A");
|
2022-10-12 08:17:51 +00:00
|
|
|
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);
|
2021-07-22 09:59:27 +00:00
|
|
|
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&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);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2019-08-24 01:39:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "A's max intersects B");
|
2022-10-12 08:17:51 +00:00
|
|
|
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);
|
2021-07-22 09:59:27 +00:00
|
|
|
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&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);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2019-08-24 01:39:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "B's max intersects A");
|
2022-10-12 08:17:51 +00:00
|
|
|
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);
|
2021-07-22 09:59:27 +00:00
|
|
|
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&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);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2019-08-24 01:39:12 +00:00
|
|
|
}
|
2019-09-23 13:21:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "B contains A");
|
2022-10-12 08:17:51 +00:00
|
|
|
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);
|
2021-07-22 09:59:27 +00:00
|
|
|
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&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);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2019-09-23 13:21:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "A contains B");
|
2022-10-12 08:17:51 +00:00
|
|
|
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);
|
2021-07-22 09:59:27 +00:00
|
|
|
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&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);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-07-22 09:59:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "A equals B");
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_Rect a = Dqn_Rect_InitFromPosAndSize(Dqn_V2(0, 0), Dqn_V2(100, 100));
|
2021-07-22 09:59:27 +00:00
|
|
|
Dqn_Rect b = a;
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_Rect ab = Dqn_Rect_Intersection(a, b);
|
2021-07-22 09:59:27 +00:00
|
|
|
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&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);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2019-09-23 13:21:14 +00:00
|
|
|
}
|
2019-08-24 01:39:12 +00:00
|
|
|
}
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_END_GROUP(&test);
|
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
|
|
|
|
2022-02-17 10:01:31 +00:00
|
|
|
Dqn_Tester Dqn_Test_PerfCounter()
|
2021-08-09 07:47:13 +00:00
|
|
|
{
|
2022-02-17 10:01:31 +00:00
|
|
|
Dqn_Tester test = {};
|
2023-03-11 14:28:17 +00:00
|
|
|
DQN_TESTER_BEGIN_GROUP("Dqn_OS_PerfCounter");
|
2021-08-09 07:47:13 +00:00
|
|
|
{
|
2023-03-11 14:28:17 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Dqn_OS_PerfCounterNow");
|
|
|
|
uint64_t result = Dqn_OS_PerfCounterNow();
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERT(&test, result != 0);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-09 07:47:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Consecutive ticks are ordered");
|
2023-03-11 14:28:17 +00:00
|
|
|
uint64_t a = Dqn_OS_PerfCounterNow();
|
|
|
|
uint64_t b = Dqn_OS_PerfCounterNow();
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, b >= a, "a: %zu, b: %zu", a, b);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-09 07:47:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Ticks to time are a correct order of magnitude");
|
2023-03-11 14:28:17 +00:00
|
|
|
uint64_t a = Dqn_OS_PerfCounterNow();
|
|
|
|
uint64_t b = Dqn_OS_PerfCounterNow();
|
2021-08-09 07:47:13 +00:00
|
|
|
|
2023-03-11 14:28:17 +00:00
|
|
|
Dqn_f64 s = Dqn_OS_PerfCounterS(a, b);
|
|
|
|
Dqn_f64 ms = Dqn_OS_PerfCounterMs(a, b);
|
|
|
|
Dqn_f64 micro_s = Dqn_OS_PerfCounterMicroS(a, b);
|
|
|
|
Dqn_f64 ns = Dqn_OS_PerfCounterNs(a, b);
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, s <= ms, "s: %f, ms: %f", s, ms);
|
|
|
|
DQN_TESTER_ASSERTF(&test, ms <= micro_s, "ms: %f, micro_s: %f", ms, micro_s);
|
|
|
|
DQN_TESTER_ASSERTF(&test, micro_s <= ns, "micro_s: %f, ns: %f", micro_s, ns);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_END_GROUP(&test);
|
2021-09-24 13:41:58 +00:00
|
|
|
return test;
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
2022-02-17 10:01:31 +00:00
|
|
|
Dqn_Tester Dqn_Test_OS()
|
2021-08-15 13:57:18 +00:00
|
|
|
{
|
2022-02-17 10:01:31 +00:00
|
|
|
Dqn_Tester test = {};
|
2022-10-12 08:17:51 +00:00
|
|
|
DQN_TESTER_BEGIN_GROUP("Dqn_OS_");
|
2021-08-15 13:57:18 +00:00
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Generate secure RNG bytes with nullptr");
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_b32 result = Dqn_OS_SecureRNGBytes(nullptr, 1);
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERT(&test, result == false);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Generate secure RNG 32 bytes");
|
2021-08-15 13:57:18 +00:00
|
|
|
char const ZERO[32] = {};
|
|
|
|
char buf[32] = {};
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_b32 result = Dqn_OS_SecureRNGBytes(buf, Dqn_CArray_CountI(buf));
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERT(&test, result);
|
2022-10-12 08:17:51 +00:00
|
|
|
DQN_TESTER_ASSERT(&test, DQN_MEMCMP(buf, ZERO, Dqn_CArray_Count(buf)) != 0);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Generate secure RNG 0 bytes");
|
2021-08-15 13:57:18 +00:00
|
|
|
char buf[32] = {};
|
|
|
|
buf[0] = 'Z';
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_b32 result = Dqn_OS_SecureRNGBytes(buf, 0);
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERT(&test, result);
|
|
|
|
DQN_TESTER_ASSERT(&test, buf[0] == 'Z');
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Query executable directory");
|
2021-09-24 12:29:34 +00:00
|
|
|
Dqn_Arena arena = {};
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_String8 result = Dqn_OS_EXEDir(Dqn_Arena_Allocator(&arena));
|
|
|
|
DQN_TESTER_ASSERT(&test, Dqn_String8_IsValid(result));
|
|
|
|
DQN_TESTER_ASSERTF(&test, Dqn_Fs_DirExists(result), "result(%zu): %.*s", result.size, DQN_STRING_FMT(result));
|
|
|
|
Dqn_Arena_Free(&arena, Dqn_ZeroMem_No);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-09 07:47:13 +00:00
|
|
|
}
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_END_GROUP(&test);
|
2021-09-24 13:41:58 +00:00
|
|
|
return test;
|
2021-08-09 07:47:13 +00:00
|
|
|
}
|
|
|
|
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester Dqn_Test_CString8()
|
2021-07-22 09:59:27 +00:00
|
|
|
{
|
2022-02-17 10:01:31 +00:00
|
|
|
Dqn_Tester test = {};
|
2022-10-15 06:12:13 +00:00
|
|
|
DQN_TESTER_BEGIN_GROUP("Dqn_CString8");
|
2022-10-15 06:26:31 +00:00
|
|
|
|
2022-10-12 08:17:51 +00:00
|
|
|
// NOTE: Dqn_CString8_ToI64
|
2019-08-24 01:39:12 +00:00
|
|
|
// ---------------------------------------------------------------------------------------------
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "To I64: Convert nullptr");
|
2022-10-12 08:17:51 +00:00
|
|
|
int64_t result = Dqn_CString8_ToI64(nullptr);
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERT(&test, result == 0);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
2019-08-24 01:39:12 +00:00
|
|
|
|
2021-08-15 13:57:18 +00:00
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "To I64: Convert empty string");
|
2022-10-12 08:17:51 +00:00
|
|
|
int64_t result = Dqn_CString8_ToI64("");
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERT(&test, result == 0);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
2019-08-24 01:39:12 +00:00
|
|
|
|
2021-08-15 13:57:18 +00:00
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "To I64: Convert \"1\"");
|
2022-10-12 08:17:51 +00:00
|
|
|
int64_t result = Dqn_CString8_ToI64("1");
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERT(&test, result == 1);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
2019-08-24 01:39:12 +00:00
|
|
|
|
2021-08-15 13:57:18 +00:00
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "To I64: Convert \"-0\"");
|
2022-10-12 08:17:51 +00:00
|
|
|
int64_t result = Dqn_CString8_ToI64("-0");
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERT(&test, result == 0);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
2019-08-24 01:39:12 +00:00
|
|
|
|
2021-08-15 13:57:18 +00:00
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "To I64: Convert \"-1\"");
|
2022-10-12 08:17:51 +00:00
|
|
|
int64_t result = Dqn_CString8_ToI64("-1");
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERT(&test, result == -1);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
2019-08-24 01:39:12 +00:00
|
|
|
|
2021-08-15 13:57:18 +00:00
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "To I64: Convert \"1.2\"");
|
2022-10-12 08:17:51 +00:00
|
|
|
int64_t result = Dqn_CString8_ToI64("1.2");
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERT(&test, result == 1);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
2019-08-24 01:39:12 +00:00
|
|
|
|
2021-08-15 13:57:18 +00:00
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "To I64: Convert \"1,234\"");
|
2022-10-12 08:17:51 +00:00
|
|
|
int64_t result = Dqn_CString8_ToI64("1,234");
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERT(&test, result == 1234);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
2019-08-24 01:39:12 +00:00
|
|
|
|
2021-08-15 13:57:18 +00:00
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "To I64: Convert \"1,2\"");
|
2022-10-12 08:17:51 +00:00
|
|
|
int64_t result = Dqn_CString8_ToI64("1,2");
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERT(&test, result == 12);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
2019-08-24 01:39:12 +00:00
|
|
|
|
2021-08-15 13:57:18 +00:00
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "To I64: Convert \"12a3\"");
|
2022-10-12 08:17:51 +00:00
|
|
|
int64_t result = Dqn_CString8_ToI64("12a3");
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERT(&test, result == 12);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2019-08-24 01:39:12 +00:00
|
|
|
}
|
|
|
|
|
2022-10-12 08:17:51 +00:00
|
|
|
// NOTE: Dqn_CString8_ToU64
|
2019-08-24 01:39:12 +00:00
|
|
|
// ---------------------------------------------------------------------------------------------
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "To U64: Convert nullptr");
|
2022-10-12 08:17:51 +00:00
|
|
|
uint64_t result = Dqn_CString8_ToU64(nullptr);
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, result == 0, "result: %zu", result);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
2019-08-24 01:39:12 +00:00
|
|
|
|
2021-08-15 13:57:18 +00:00
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "To U64: Convert empty string");
|
2022-10-12 08:17:51 +00:00
|
|
|
uint64_t result = Dqn_CString8_ToU64("");
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, result == 0, "result: %zu", result);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
2019-08-24 01:39:12 +00:00
|
|
|
|
2021-08-15 13:57:18 +00:00
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "To U64: Convert \"1\"");
|
2022-10-12 08:17:51 +00:00
|
|
|
uint64_t result = Dqn_CString8_ToU64("1");
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, result == 1, "result: %zu", result);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
2019-08-24 01:39:12 +00:00
|
|
|
|
2021-08-15 13:57:18 +00:00
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "To U64: Convert \"-0\"");
|
2022-10-12 08:17:51 +00:00
|
|
|
uint64_t result = Dqn_CString8_ToU64("-0");
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, result == 0, "result: %zu", result);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
2019-08-24 01:39:12 +00:00
|
|
|
|
2021-08-15 13:57:18 +00:00
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "To U64: Convert \"-1\"");
|
2022-10-12 08:17:51 +00:00
|
|
|
uint64_t result = Dqn_CString8_ToU64("-1");
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, result == 0, "result: %zu", result);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
2019-08-24 01:39:12 +00:00
|
|
|
|
2021-08-15 13:57:18 +00:00
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "To U64: Convert \"1.2\"");
|
2022-10-12 08:17:51 +00:00
|
|
|
uint64_t result = Dqn_CString8_ToU64("1.2");
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, result == 1, "result: %zu", result);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
2019-08-24 01:39:12 +00:00
|
|
|
|
2021-08-15 13:57:18 +00:00
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "To U64: Convert \"1,234\"");
|
2022-10-12 08:17:51 +00:00
|
|
|
uint64_t result = Dqn_CString8_ToU64("1,234");
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, result == 1234, "result: %zu", result);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
2019-08-24 01:39:12 +00:00
|
|
|
|
2021-08-15 13:57:18 +00:00
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "To U64: Convert \"1,2\"");
|
2022-10-12 08:17:51 +00:00
|
|
|
uint64_t result = Dqn_CString8_ToU64("1,2");
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, result == 12, "result: %zu", result);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
2019-08-24 01:39:12 +00:00
|
|
|
|
2021-08-15 13:57:18 +00:00
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "To U64: Convert \"12a3\"");
|
2022-10-12 08:17:51 +00:00
|
|
|
uint64_t result = Dqn_CString8_ToU64("12a3");
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, result == 12, "result: %zu", result);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2019-08-24 01:39:12 +00:00
|
|
|
}
|
2020-01-17 15:01:25 +00:00
|
|
|
|
2022-10-12 08:17:51 +00:00
|
|
|
// NOTE: Dqn_CString8_Find
|
2020-01-17 15:01:25 +00:00
|
|
|
// ---------------------------------------------------------------------------------------------
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Find: String (char) is not in buffer");
|
2021-08-15 13:57:18 +00:00
|
|
|
char const buf[] = "836a35becd4e74b66a0d6844d51f1a63018c7ebc44cf7e109e8e4bba57eefb55";
|
|
|
|
char const find[] = "2";
|
2022-10-12 08:17:51 +00:00
|
|
|
char const *result = Dqn_CString8_Find(buf, find, Dqn_CString8_ArrayCountI(buf), Dqn_CString8_ArrayCountI(find));
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERT(&test, result == nullptr);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
2020-01-17 15:01:25 +00:00
|
|
|
|
2021-08-15 13:57:18 +00:00
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Find: String (char) is in buffer");
|
2021-08-15 13:57:18 +00:00
|
|
|
char const buf[] = "836a35becd4e74b66a0d6844d51f1a63018c7ebc44cf7e109e8e4bba57eefb55";
|
|
|
|
char const find[] = "6";
|
2022-10-12 08:17:51 +00:00
|
|
|
char const *result = Dqn_CString8_Find(buf, find, Dqn_CString8_ArrayCountI(buf), Dqn_CString8_ArrayCountI(find));
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERT(&test, result != nullptr);
|
|
|
|
DQN_TESTER_ASSERT(&test, result[0] == '6' && result[1] == 'a');
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
2022-10-12 08:17:51 +00:00
|
|
|
// NOTE: Dqn_CString8_FileNameFromPath
|
2021-08-15 13:57:18 +00:00
|
|
|
// ---------------------------------------------------------------------------------------------
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&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";
|
2022-10-12 08:17:51 +00:00
|
|
|
char const *result = Dqn_CString8_FileNameFromPath(buf, Dqn_CString8_ArrayCountI(buf), &file_name_size);
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, file_name_size == 8, "size: %I64d", file_name_size);
|
2022-10-12 08:17:51 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, Dqn_String8_Eq(Dqn_String8_Init(result, file_name_size), DQN_STRING8("test.exe")), "%.*s", DQN_CAST(int)file_name_size, result);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&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";
|
2022-10-12 08:17:51 +00:00
|
|
|
char const *result = Dqn_CString8_FileNameFromPath(buf, Dqn_CString8_ArrayCountI(buf), &file_name_size);
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, file_name_size == 8, "size: %I64d", file_name_size);
|
2022-10-12 08:17:51 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, Dqn_String8_Eq(Dqn_String8_Init(result, file_name_size), DQN_STRING8("test.exe")), "%.*s", (int)file_name_size, result);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
2022-10-12 08:17:51 +00:00
|
|
|
// NOTE: Dqn_CString8_TrimPrefix
|
2021-08-15 13:57:18 +00:00
|
|
|
// ---------------------------------------------------------------------------------------------
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Trim prefix");
|
2021-08-15 13:57:18 +00:00
|
|
|
char const prefix[] = "@123";
|
|
|
|
char const buf[] = "@123string";
|
|
|
|
Dqn_isize trimmed_size = 0;
|
2022-10-15 06:12:13 +00:00
|
|
|
char const *result = Dqn_CString8_TrimPrefix(buf, prefix, Dqn_CString8_ArrayCountI(buf), Dqn_CString8_ArrayCountI(prefix), Dqn_CString8EqCase::Sensitive, &trimmed_size);
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, trimmed_size == 6, "size: %I64d", trimmed_size);
|
2022-10-12 08:17:51 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, Dqn_String8_Eq(Dqn_String8_Init(result, trimmed_size), DQN_STRING8("string")), "%.*s", (int)trimmed_size, result);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Trim prefix, nullptr trimmed size");
|
2021-08-15 13:57:18 +00:00
|
|
|
char const prefix[] = "@123";
|
|
|
|
char const buf[] = "@123string";
|
2022-10-15 06:12:13 +00:00
|
|
|
char const *result = Dqn_CString8_TrimPrefix(buf, prefix, Dqn_CString8_ArrayCountI(buf), Dqn_CString8_ArrayCountI(prefix), Dqn_CString8EqCase::Sensitive, nullptr);
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERT(&test, result);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
2022-10-12 08:17:51 +00:00
|
|
|
// NOTE: Dqn_CString8_IsAllDigits
|
2021-08-15 13:57:18 +00:00
|
|
|
// ---------------------------------------------------------------------------------------------
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Is all digits fails on non-digit string");
|
2021-08-15 13:57:18 +00:00
|
|
|
char const buf[] = "@123string";
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_b32 result = Dqn_CString8_IsAllDigits(buf, Dqn_CString8_ArrayCountI(buf));
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERT(&test, result == false);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Is all digits fails on nullptr");
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_b32 result = Dqn_CString8_IsAllDigits(nullptr, 0);
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERT(&test, result == false);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Is all digits fails on nullptr w/ size");
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_b32 result = Dqn_CString8_IsAllDigits(nullptr, 1);
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERT(&test, result == false);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Is all digits fails on 0 size w/ string");
|
2021-08-15 13:57:18 +00:00
|
|
|
char const buf[] = "@123string";
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_b32 result = Dqn_CString8_IsAllDigits(buf, 0);
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERT(&test, result == false);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Is all digits success");
|
2021-08-15 13:57:18 +00:00
|
|
|
char const buf[] = "23";
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_b32 result = Dqn_CString8_IsAllDigits(buf, Dqn_CString8_ArrayCountI(buf));
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERT(&test, DQN_CAST(bool)result == true);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Is all digits fails on whitespace");
|
2021-08-15 13:57:18 +00:00
|
|
|
char const buf[] = "23 ";
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_b32 result = Dqn_CString8_IsAllDigits(buf, Dqn_CString8_ArrayCountI(buf));
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERT(&test, DQN_CAST(bool)result == false);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-07-01 12:02:39 +00:00
|
|
|
}
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_END_GROUP(&test);
|
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
|
|
|
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester Dqn_Test_String8()
|
2021-07-28 11:10:25 +00:00
|
|
|
{
|
2022-02-17 10:01:31 +00:00
|
|
|
Dqn_Tester test = {};
|
2022-08-06 06:55:51 +00:00
|
|
|
DQN_TESTER_BEGIN_GROUP("Dqn_String8");
|
2021-07-28 11:10:25 +00:00
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Initialise with string literal w/ macro");
|
2022-10-09 09:51:23 +00:00
|
|
|
Dqn_String8 string = DQN_STRING8("AB");
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, string.size == 2, "size: %I64d", string.size);
|
2022-08-06 06:55:51 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, string.data[0] == 'A', "string[0]: %c", string.data[0]);
|
|
|
|
DQN_TESTER_ASSERTF(&test, string.data[1] == 'B', "string[1]: %c", string.data[1]);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-07-28 11:10:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Initialise with format string");
|
2022-08-06 06:55:51 +00:00
|
|
|
Dqn_Arena arena = {};
|
2023-03-11 14:28:17 +00:00
|
|
|
Dqn_String8 string = Dqn_String8_InitF(Dqn_Arena_Allocator(&arena), "%s", "AB");
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, string.size == 2, "size: %I64d", string.size);
|
2022-08-06 06:55:51 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, string.data[0] == 'A', "string[0]: %c", string.data[0]);
|
|
|
|
DQN_TESTER_ASSERTF(&test, string.data[1] == 'B', "string[1]: %c", string.data[1]);
|
|
|
|
DQN_TESTER_ASSERTF(&test, string.data[2] == 0, "string[2]: %c", string.data[2]);
|
2023-03-11 14:28:17 +00:00
|
|
|
Dqn_Arena_Free(&arena, Dqn_ZeroMem_No);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-07-28 11:10:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Copy string");
|
2022-10-15 06:26:31 +00:00
|
|
|
Dqn_Arena arena = {};
|
|
|
|
Dqn_String8 string = DQN_STRING8("AB");
|
|
|
|
Dqn_String8 copy = Dqn_String8_Copy(Dqn_Arena_Allocator(&arena), string);
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, copy.size == 2, "size: %I64d", copy.size);
|
2022-08-06 06:55:51 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, copy.data[0] == 'A', "copy[0]: %c", copy.data[0]);
|
|
|
|
DQN_TESTER_ASSERTF(&test, copy.data[1] == 'B', "copy[1]: %c", copy.data[1]);
|
|
|
|
DQN_TESTER_ASSERTF(&test, copy.data[2] == 0, "copy[2]: %c", copy.data[2]);
|
2023-03-11 14:28:17 +00:00
|
|
|
Dqn_Arena_Free(&arena, Dqn_ZeroMem_No);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-07-28 11:10:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Trim whitespace around string");
|
2022-10-15 06:26:31 +00:00
|
|
|
Dqn_String8 string = Dqn_String8_TrimWhitespaceAround(DQN_STRING8(" AB "));
|
|
|
|
DQN_TESTER_ASSERTF(&test, Dqn_String8_Eq(string, DQN_STRING8("AB")), "[string=%.*s]", DQN_STRING_FMT(string));
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-07-28 11:10:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Allocate string from arena");
|
2022-08-06 06:55:51 +00:00
|
|
|
Dqn_Arena arena = {};
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_String8 string = Dqn_String8_Allocate(Dqn_Arena_Allocator(&arena), 2, Dqn_ZeroMem_No);
|
2022-08-06 06:55:51 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, string.size == 2, "size: %I64d", string.size);
|
2023-03-11 14:28:17 +00:00
|
|
|
Dqn_Arena_Free(&arena, Dqn_ZeroMem_No);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-07-28 11:10:25 +00:00
|
|
|
}
|
2021-08-15 13:57:18 +00:00
|
|
|
|
2022-10-12 08:17:51 +00:00
|
|
|
// NOTE: Dqn_CString8_Trim[Prefix/Suffix]
|
2021-08-15 13:57:18 +00:00
|
|
|
// ---------------------------------------------------------------------------------------------
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Trim prefix with matching prefix");
|
2022-10-09 09:51:23 +00:00
|
|
|
Dqn_String8 input = DQN_STRING8("nft/abc");
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_String8 result = Dqn_String8_TrimPrefix(input, DQN_STRING8("nft/"));
|
|
|
|
DQN_TESTER_ASSERTF(&test, Dqn_String8_Eq(result, DQN_STRING8("abc")), "%.*s", DQN_STRING_FMT(result));
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Trim prefix with non matching prefix");
|
2022-10-09 09:51:23 +00:00
|
|
|
Dqn_String8 input = DQN_STRING8("nft/abc");
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_String8 result = Dqn_String8_TrimPrefix(input, DQN_STRING8(" ft/"));
|
|
|
|
DQN_TESTER_ASSERTF(&test, Dqn_String8_Eq(result, input), "%.*s", DQN_STRING_FMT(result));
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Trim suffix with matching suffix");
|
2022-10-09 09:51:23 +00:00
|
|
|
Dqn_String8 input = DQN_STRING8("nft/abc");
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_String8 result = Dqn_String8_TrimSuffix(input, DQN_STRING8("abc"));
|
|
|
|
DQN_TESTER_ASSERTF(&test, Dqn_String8_Eq(result, DQN_STRING8("nft/")), "%.*s", DQN_STRING_FMT(result));
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Trim suffix with non matching suffix");
|
2022-10-09 09:51:23 +00:00
|
|
|
Dqn_String8 input = DQN_STRING8("nft/abc");
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_String8 result = Dqn_String8_TrimSuffix(input, DQN_STRING8("ab"));
|
|
|
|
DQN_TESTER_ASSERTF(&test, Dqn_String8_Eq(result, input), "%.*s", DQN_STRING_FMT(result));
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
2022-10-12 08:17:51 +00:00
|
|
|
// NOTE: Dqn_String8_IsAllDigits
|
2021-08-15 13:57:18 +00:00
|
|
|
// ---------------------------------------------------------------------------------------------
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Is all digits fails on non-digit string");
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_b32 result = Dqn_String8_IsAllDigits(DQN_STRING8("@123string"));
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERT(&test, result == false);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Is all digits fails on nullptr");
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_b32 result = Dqn_String8_IsAllDigits(Dqn_String8_Init(nullptr, 0));
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERT(&test, result == false);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Is all digits fails on nullptr w/ size");
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_b32 result = Dqn_String8_IsAllDigits(Dqn_String8_Init(nullptr, 1));
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERT(&test, result == false);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Is all digits fails on string w/ 0 size");
|
2021-08-15 13:57:18 +00:00
|
|
|
char const buf[] = "@123string";
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_b32 result = Dqn_String8_IsAllDigits(Dqn_String8_Init(buf, 0));
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERT(&test, result == false);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Is all digits success");
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_b32 result = Dqn_String8_IsAllDigits(DQN_STRING8("23"));
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERT(&test, DQN_CAST(bool)result == true);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Is all digits fails on whitespace");
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_b32 result = Dqn_String8_IsAllDigits(DQN_STRING8("23 "));
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERT(&test, DQN_CAST(bool)result == false);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
2022-10-15 06:26:31 +00:00
|
|
|
|
|
|
|
// NOTE: Dqn_String8_BinarySplit
|
|
|
|
// ---------------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
char const *TEST_FMT = "Dqn_String8_BinarySplit: with '%.*s' with '%c'";
|
|
|
|
{
|
|
|
|
Dqn_String8 input = DQN_STRING8("abcdef");
|
|
|
|
char delimiter = '/';
|
|
|
|
Dqn_Tester_Begin(&test, TEST_FMT, DQN_STRING_FMT(input), delimiter);
|
|
|
|
|
|
|
|
Dqn_String8 rhs = {};
|
|
|
|
Dqn_String8 lhs = Dqn_String8_BinarySplit(input, delimiter, &rhs);
|
|
|
|
|
|
|
|
DQN_TESTER_ASSERTF(&test, Dqn_String8_Eq(lhs, DQN_STRING8("abcdef")), "[lhs=%.*s]", DQN_STRING_FMT(lhs));
|
|
|
|
DQN_TESTER_ASSERTF(&test, Dqn_String8_Eq(rhs, DQN_STRING8("")), "[rhs=%.*s]", DQN_STRING_FMT(rhs));
|
|
|
|
Dqn_Tester_End(&test);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
Dqn_String8 input = DQN_STRING8("abc/def");
|
|
|
|
char delimiter = '/';
|
|
|
|
Dqn_Tester_Begin(&test, TEST_FMT, DQN_STRING_FMT(input), delimiter);
|
|
|
|
|
|
|
|
Dqn_String8 rhs = {};
|
|
|
|
Dqn_String8 lhs = Dqn_String8_BinarySplit(input, delimiter, &rhs);
|
|
|
|
|
|
|
|
DQN_TESTER_ASSERTF(&test, Dqn_String8_Eq(lhs, DQN_STRING8("abc")), "[lhs=%.*s]", DQN_STRING_FMT(lhs));
|
|
|
|
DQN_TESTER_ASSERTF(&test, Dqn_String8_Eq(rhs, DQN_STRING8("def")), "[rhs=%.*s]", DQN_STRING_FMT(rhs));
|
|
|
|
Dqn_Tester_End(&test);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
Dqn_String8 input = DQN_STRING8("/abcdef");
|
|
|
|
char delimiter = '/';
|
|
|
|
Dqn_Tester_Begin(&test, TEST_FMT, DQN_STRING_FMT(input), delimiter);
|
|
|
|
|
|
|
|
Dqn_String8 rhs = {};
|
|
|
|
Dqn_String8 lhs = Dqn_String8_BinarySplit(input, delimiter, &rhs);
|
|
|
|
|
|
|
|
DQN_TESTER_ASSERTF(&test, Dqn_String8_Eq(lhs, DQN_STRING8("")), "[lhs=%.*s]", DQN_STRING_FMT(lhs));
|
|
|
|
DQN_TESTER_ASSERTF(&test, Dqn_String8_Eq(rhs, DQN_STRING8("abcdef")), "[rhs=%.*s]", DQN_STRING_FMT(rhs));
|
|
|
|
Dqn_Tester_End(&test);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_END_GROUP(&test);
|
2021-09-24 13:41:58 +00:00
|
|
|
return test;
|
2021-07-28 11:10:25 +00:00
|
|
|
}
|
|
|
|
|
2022-02-17 10:01:31 +00:00
|
|
|
Dqn_Tester Dqn_Test_TicketMutex()
|
2021-07-22 09:59:27 +00:00
|
|
|
{
|
2022-02-17 10:01:31 +00:00
|
|
|
Dqn_Tester test = {};
|
|
|
|
DQN_TESTER_BEGIN_GROUP("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.
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Ticket mutex start and stop");
|
2021-07-22 09:59:27 +00:00
|
|
|
Dqn_TicketMutex mutex = {};
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_TicketMutex_Begin(&mutex);
|
|
|
|
Dqn_TicketMutex_End(&mutex);
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERT(&test, mutex.ticket == mutex.serving);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-07-22 09:59:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "Ticket mutex start and stop w/ advanced API");
|
2021-07-22 09:59:27 +00:00
|
|
|
Dqn_TicketMutex mutex = {};
|
2022-10-12 08:17:51 +00:00
|
|
|
unsigned int ticket_a = Dqn_TicketMutex_MakeTicket(&mutex);
|
|
|
|
unsigned int ticket_b = Dqn_TicketMutex_MakeTicket(&mutex);
|
|
|
|
DQN_TESTER_ASSERT(&test, DQN_CAST(bool)Dqn_TicketMutex_CanLock(&mutex, ticket_b) == false);
|
|
|
|
DQN_TESTER_ASSERT(&test, DQN_CAST(bool)Dqn_TicketMutex_CanLock(&mutex, ticket_a) == true);
|
2021-07-22 09:59:27 +00:00
|
|
|
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_TicketMutex_BeginTicket(&mutex, ticket_a);
|
|
|
|
Dqn_TicketMutex_End(&mutex);
|
|
|
|
Dqn_TicketMutex_BeginTicket(&mutex, ticket_b);
|
|
|
|
Dqn_TicketMutex_End(&mutex);
|
2021-07-22 09:59:27 +00:00
|
|
|
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERT(&test, mutex.ticket == mutex.serving);
|
|
|
|
DQN_TESTER_ASSERT(&test, mutex.ticket == ticket_b + 1);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-07-22 09:59:27 +00:00
|
|
|
}
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_END_GROUP(&test);
|
2021-09-24 13:41:58 +00:00
|
|
|
return test;
|
2021-07-22 09:59:27 +00:00
|
|
|
}
|
|
|
|
|
2022-02-17 10:01:31 +00:00
|
|
|
Dqn_Tester Dqn_Test_Win()
|
2021-08-15 13:57:18 +00:00
|
|
|
{
|
2022-02-17 10:01:31 +00:00
|
|
|
Dqn_Tester test = {};
|
2021-08-15 13:57:18 +00:00
|
|
|
#if defined(DQN_OS_WIN32)
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_BEGIN_GROUP("Dqn_Win");
|
2021-08-15 13:57:18 +00:00
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "String8 to String16 size required");
|
2022-10-12 08:17:51 +00:00
|
|
|
int result = Dqn_Win_String8ToCString16(DQN_STRING8("a"), nullptr, 0);
|
2022-08-06 06:55:51 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, result == 2, "Size returned: %d. This size should include the null-terminator", result);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "String16 to String8 size required");
|
2022-10-12 08:17:51 +00:00
|
|
|
int result = Dqn_Win_String16ToCString8(DQN_STRING16(L"a"), nullptr, 0);
|
2022-08-06 06:55:51 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, result == 2, "Size returned: %d. This size should include the null-terminator", result);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2022-08-06 06:55:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "String8 to String16 size required");
|
2022-10-12 08:17:51 +00:00
|
|
|
int result = Dqn_Win_String8ToCString16(DQN_STRING8("String"), nullptr, 0);
|
2022-08-06 06:55:51 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, result == 7, "Size returned: %d. This size should include the null-terminator", result);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2022-08-06 06:55:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "String16 to String8 size required");
|
2022-10-12 08:17:51 +00:00
|
|
|
int result = Dqn_Win_String16ToCString8(DQN_STRING16(L"String"), nullptr, 0);
|
2022-08-06 06:55:51 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, result == 7, "Size returned: %d. This size should include the null-terminator", result);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2022-08-06 06:55:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "String8 to String16");
|
2022-08-06 06:55:51 +00:00
|
|
|
Dqn_Arena arena = {};
|
2022-10-09 09:51:23 +00:00
|
|
|
Dqn_String8 const INPUT = DQN_STRING8("String");
|
2022-10-12 08:17:51 +00:00
|
|
|
int size_required = Dqn_Win_String8ToCString16(INPUT, nullptr, 0);
|
|
|
|
wchar_t *string = Dqn_Arena_NewArray(&arena, wchar_t, size_required, Dqn_ZeroMem_No);
|
2021-08-15 13:57:18 +00:00
|
|
|
|
|
|
|
// Fill the string with error sentinels, which ensures the string is zero terminated
|
2022-08-06 06:55:51 +00:00
|
|
|
DQN_MEMSET(string, 'Z', size_required);
|
2021-08-15 13:57:18 +00:00
|
|
|
|
2022-10-12 08:17:51 +00:00
|
|
|
int size_returned = Dqn_Win_String8ToCString16(INPUT, string, size_required);
|
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};
|
|
|
|
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, size_required == size_returned, "string_size: %d, result: %d", size_required, size_returned);
|
2022-10-12 08:17:51 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, size_returned == Dqn_CArray_Count(EXPECTED), "string_size: %d, expected: %zu", size_returned, Dqn_CArray_Count(EXPECTED));
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERT(&test, DQN_MEMCMP(EXPECTED, string, sizeof(EXPECTED)) == 0);
|
2023-03-11 14:28:17 +00:00
|
|
|
Dqn_Arena_Free(&arena, Dqn_ZeroMem_No);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "String16 to String8: No null-terminate");
|
2022-08-06 06:55:51 +00:00
|
|
|
Dqn_Arena arena = {};
|
2022-10-09 09:51:23 +00:00
|
|
|
Dqn_String16 INPUT = DQN_STRING16(L"String");
|
2022-10-12 08:17:51 +00:00
|
|
|
int size_required = Dqn_Win_String16ToCString8(INPUT, nullptr, 0);
|
|
|
|
char *string = Dqn_Arena_NewArray(&arena, char, size_required, Dqn_ZeroMem_No);
|
2021-08-15 13:57:18 +00:00
|
|
|
|
|
|
|
// Fill the string with error sentinels, which ensures the string is zero terminated
|
2022-08-06 06:55:51 +00:00
|
|
|
DQN_MEMSET(string, 'Z', size_required);
|
2021-08-15 13:57:18 +00:00
|
|
|
|
2022-10-12 08:17:51 +00:00
|
|
|
int size_returned = Dqn_Win_String16ToCString8(INPUT, string, size_required);
|
2021-08-15 13:57:18 +00:00
|
|
|
char const EXPECTED[] = {'S', 't', 'r', 'i', 'n', 'g', 0};
|
|
|
|
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, size_required == size_returned, "string_size: %d, result: %d", size_required, size_returned);
|
2022-10-12 08:17:51 +00:00
|
|
|
DQN_TESTER_ASSERTF(&test, size_returned == Dqn_CArray_Count(EXPECTED), "string_size: %d, expected: %zu", size_returned, Dqn_CArray_Count(EXPECTED));
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERT(&test, DQN_MEMCMP(EXPECTED, string, sizeof(EXPECTED)) == 0);
|
2023-03-11 14:28:17 +00:00
|
|
|
Dqn_Arena_Free(&arena, Dqn_ZeroMem_No);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-08-15 13:57:18 +00:00
|
|
|
}
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_END_GROUP(&test);
|
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
|
|
|
|
};
|
|
|
|
|
2022-08-06 06:55:51 +00:00
|
|
|
Dqn_String8 const DQN_TESTS__HASH_STRING[] =
|
2021-09-19 11:49:56 +00:00
|
|
|
{
|
2022-10-09 09:51:23 +00:00
|
|
|
#define DQN_TESTS_HASH_X_ENTRY(enum_val, string) DQN_STRING8(string),
|
2021-09-19 11:49:56 +00:00
|
|
|
DQN_TESTS_HASH_X_MACRO
|
|
|
|
#undef DQN_TESTS_HASH_X_ENTRY
|
|
|
|
};
|
|
|
|
|
2022-08-06 06:55:51 +00:00
|
|
|
void Dqn_Test__KeccakDispatch(Dqn_Tester *test, int hash_type, Dqn_String8 input)
|
2021-09-19 11:49:56 +00:00
|
|
|
{
|
|
|
|
#if defined(DQN_KECCAK_H)
|
2023-03-11 14:28:17 +00:00
|
|
|
Dqn_ThreadScratch scratch = Dqn_Thread_GetScratch(nullptr);
|
|
|
|
Dqn_String8 input_hex = Dqn_Hex_BytesToString8Arena(scratch.arena, input.data, input.size);
|
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;
|
2022-08-06 06:55:51 +00:00
|
|
|
FIPS202_SHA3_224(DQN_CAST(u8 *)input.data, input.size, (u8 *)expect.data);
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(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),
|
2022-08-06 06:55:51 +00:00
|
|
|
DQN_KECCAK_STRING56_FMT(Dqn_KeccakBytes28ToHex(&hash).data),
|
|
|
|
DQN_KECCAK_STRING56_FMT(Dqn_KeccakBytes28ToHex(&expect).data));
|
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;
|
2022-08-06 06:55:51 +00:00
|
|
|
FIPS202_SHA3_256(DQN_CAST(u8 *)input.data, input.size, (u8 *)expect.data);
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(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),
|
2022-08-06 06:55:51 +00:00
|
|
|
DQN_KECCAK_STRING64_FMT(Dqn_KeccakBytes32ToHex(&hash).data),
|
|
|
|
DQN_KECCAK_STRING64_FMT(Dqn_KeccakBytes32ToHex(&expect).data));
|
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;
|
2022-08-06 06:55:51 +00:00
|
|
|
FIPS202_SHA3_384(DQN_CAST(u8 *)input.data, input.size, (u8 *)expect.data);
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(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),
|
2022-08-06 06:55:51 +00:00
|
|
|
DQN_KECCAK_STRING96_FMT(Dqn_KeccakBytes48ToHex(&hash).data),
|
|
|
|
DQN_KECCAK_STRING96_FMT(Dqn_KeccakBytes48ToHex(&expect).data));
|
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;
|
2022-08-06 06:55:51 +00:00
|
|
|
FIPS202_SHA3_512(DQN_CAST(u8 *)input.data, input.size, (u8 *)expect.data);
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(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),
|
2022-08-06 06:55:51 +00:00
|
|
|
DQN_KECCAK_STRING128_FMT(Dqn_KeccakBytes64ToHex(&hash).data),
|
|
|
|
DQN_KECCAK_STRING128_FMT(Dqn_KeccakBytes64ToHex(&expect).data));
|
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;
|
2022-08-06 06:55:51 +00:00
|
|
|
Keccak(1152, 448, DQN_CAST(u8 *)input.data, input.size, 0x01, (u8 *)expect.data, sizeof(expect));
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(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),
|
2022-08-06 06:55:51 +00:00
|
|
|
DQN_KECCAK_STRING56_FMT(Dqn_KeccakBytes28ToHex(&hash).data),
|
|
|
|
DQN_KECCAK_STRING56_FMT(Dqn_KeccakBytes28ToHex(&expect).data));
|
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;
|
2022-08-06 06:55:51 +00:00
|
|
|
Keccak(1088, 512, DQN_CAST(u8 *)input.data, input.size, 0x01, (u8 *)expect.data, sizeof(expect));
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(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),
|
2022-08-06 06:55:51 +00:00
|
|
|
DQN_KECCAK_STRING64_FMT(Dqn_KeccakBytes32ToHex(&hash).data),
|
|
|
|
DQN_KECCAK_STRING64_FMT(Dqn_KeccakBytes32ToHex(&expect).data));
|
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;
|
2022-08-06 06:55:51 +00:00
|
|
|
Keccak(832, 768, DQN_CAST(u8 *)input.data, input.size, 0x01, (u8 *)expect.data, sizeof(expect));
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(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),
|
2022-08-06 06:55:51 +00:00
|
|
|
DQN_KECCAK_STRING96_FMT(Dqn_KeccakBytes48ToHex(&hash).data),
|
|
|
|
DQN_KECCAK_STRING96_FMT(Dqn_KeccakBytes48ToHex(&expect).data));
|
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;
|
2022-08-06 06:55:51 +00:00
|
|
|
Keccak(576, 1024, DQN_CAST(u8 *)input.data, input.size, 0x01, (u8 *)expect.data, sizeof(expect));
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_ASSERTF(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),
|
2022-08-06 06:55:51 +00:00
|
|
|
DQN_KECCAK_STRING128_FMT(Dqn_KeccakBytes64ToHex(&hash).data),
|
|
|
|
DQN_KECCAK_STRING128_FMT(Dqn_KeccakBytes64ToHex(&expect).data));
|
2021-09-19 11:49:56 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
#endif // DQN_KECCAK_H
|
|
|
|
}
|
|
|
|
|
2022-02-17 10:01:31 +00:00
|
|
|
Dqn_Tester Dqn_Test_Keccak()
|
2021-09-19 11:49:56 +00:00
|
|
|
{
|
2022-02-17 10:01:31 +00:00
|
|
|
Dqn_Tester test = {};
|
2021-09-19 11:49:56 +00:00
|
|
|
#if defined(DQN_KECCAK_H)
|
2022-08-06 06:55:51 +00:00
|
|
|
Dqn_String8 const INPUTS[] = {
|
2022-10-09 09:51:23 +00:00
|
|
|
DQN_STRING8("abc"),
|
|
|
|
DQN_STRING8(""),
|
|
|
|
DQN_STRING8("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"),
|
|
|
|
DQN_STRING8("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmno"
|
|
|
|
"pqrstnopqrstu"),
|
2021-09-19 11:49:56 +00:00
|
|
|
};
|
|
|
|
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_BEGIN_GROUP("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);
|
|
|
|
|
2022-08-06 06:55:51 +00:00
|
|
|
for (Dqn_String8 input : INPUTS)
|
2021-09-19 11:49:56 +00:00
|
|
|
{
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&test, "%.*s - Input: %.*s", DQN_STRING_FMT(DQN_TESTS__HASH_STRING[hash_type]), DQN_CAST(int)DQN_MIN(input.size, 54), input.data);
|
2021-09-24 13:41:58 +00:00
|
|
|
Dqn_Test__KeccakDispatch(&test, hash_type, input);
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-09-19 11:49:56 +00:00
|
|
|
}
|
|
|
|
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_Begin(&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] = {};
|
2022-10-12 08:17:51 +00:00
|
|
|
uint32_t src_size = pcg32_boundedrand_r(&rng, sizeof(src));
|
2021-09-19 11:49:56 +00:00
|
|
|
|
|
|
|
for (int src_index = 0; src_index < src_size; src_index++)
|
|
|
|
src[src_index] = pcg32_boundedrand_r(&rng, 255);
|
|
|
|
|
2022-10-12 08:17:51 +00:00
|
|
|
Dqn_String8 input = Dqn_String8_Init(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
|
|
|
}
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Tester_End(&test);
|
2021-09-19 11:49:56 +00:00
|
|
|
}
|
2022-02-17 10:01:31 +00:00
|
|
|
DQN_TESTER_END_GROUP(&test);
|
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()
|
|
|
|
{
|
2022-02-17 10:01:31 +00:00
|
|
|
Dqn_Tester tests[]
|
2021-09-24 13:41:58 +00:00
|
|
|
{
|
|
|
|
Dqn_Test_File(),
|
|
|
|
Dqn_Test_FixedArray(),
|
2022-11-24 13:35:53 +00:00
|
|
|
Dqn_Test_FString8(),
|
2021-09-24 13:41:58 +00:00
|
|
|
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(),
|
2022-10-15 06:12:13 +00:00
|
|
|
Dqn_Test_CString8(),
|
|
|
|
Dqn_Test_String8(),
|
2021-09-24 13:41:58 +00:00
|
|
|
Dqn_Test_TicketMutex(),
|
|
|
|
Dqn_Test_Win(),
|
|
|
|
};
|
|
|
|
|
|
|
|
int total_tests = 0;
|
|
|
|
int total_good_tests = 0;
|
2022-10-15 06:12:13 +00:00
|
|
|
for (Dqn_Tester &test : tests) {
|
2021-09-24 13:41:58 +00:00
|
|
|
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
|
|
|
|