From 4442ccfb03a248d92e79e8720feab564669bf252 Mon Sep 17 00:00:00 2001 From: doyle Date: Tue, 18 May 2021 21:15:36 +1000 Subject: [PATCH] Fix the unit tests --- Code/Dqn.h | 31 ++++++++++++++++++++----------- Code/Dqn_UnitTests.cpp | 28 ++++++++++++++-------------- Project/dqn.rdbg | Bin 0 -> 161 bytes 3 files changed, 34 insertions(+), 25 deletions(-) create mode 100644 Project/dqn.rdbg diff --git a/Code/Dqn.h b/Code/Dqn.h index 9dbbaab..f49a8e2 100644 --- a/Code/Dqn.h +++ b/Code/Dqn.h @@ -1513,8 +1513,10 @@ DQN_API char const *Dqn_Str_SkipToNextWordInPlace (char const **src); DQN_API char const *Dqn_Str_SkipWhitespaceInPlace (char const **src); DQN_API char const *Dqn_Str_TrimWhitespaceAround (char const *src, Dqn_isize size, Dqn_isize *new_size); DQN_API char const *Dqn_Str_TrimPrefix (char const *src, Dqn_isize size, char const *prefix, Dqn_isize prefix_size, Dqn_isize *trimmed_size); -DQN_API Dqn_u64 Dqn_Str_ToU64 (char const *buf, int len = -1); -DQN_API Dqn_i64 Dqn_Str_ToI64 (char const *buf, int len = -1); + +// separator: The separator between the thousand-th digits, i.e. separator = ',' converts '1,234' to '1234'. +DQN_API Dqn_u64 Dqn_Str_ToU64 (char const *buf, int len = -1, char separator = ','); +DQN_API Dqn_i64 Dqn_Str_ToI64 (char const *buf, int len = -1, char separator = ','); // ------------------------------------------------------------------------------------------------- // @@ -2146,6 +2148,7 @@ Dqn_Map Dqn_Map_InitWithMemory(void *mem, Dqn_isize mem_size) // NOTE: Generate the map // Dqn_isize bytes_required = bytes_for_values + bytes_for_usage_bitset; + (void)bytes_required; DQN_ASSERT_MSG(bytes_required <= mem_size, "(bytes_for_values = %I64d, bytes_for_usage_bitset = %I64d, mem_size = %I64d)", bytes_for_values, bytes_for_usage_bitset, mem_size); @@ -2697,7 +2700,7 @@ DQN_API void Dqn_Array_Pop(Dqn_Array *a, Dqn_isize num, Dqn_ZeroMem zero_mem) void *begin = a->data + a->size; void *end = a->data + (a->size + num); Dqn_isize bytes = DQN_CAST(Dqn_isize) end - DQN_CAST(Dqn_isize) begin; - Dqn__ZeroMemBytes(zero_mem, begin, bytes); + Dqn__ZeroMemBytes(begin, bytes, zero_mem); } template @@ -3602,7 +3605,9 @@ DQN_API void *Dqn_Allocator__Allocate(Dqn_Allocator *allocator, Dqn_isize size, void *ptr = zero_mem == Dqn_ZeroMem::Yes ? DQN_CALLOC(1, DQN_CAST(size_t)size) : DQN_MALLOC(size); result = Dqn_PointerMetadata_Init(ptr, size, alignment); if (!result && allocator->type == Dqn_AllocatorType::XHeap) + { DQN_ASSERT(result); + } } break; @@ -4146,7 +4151,7 @@ DQN_API Dqn_i64 Dqn_Safe_MulI64(Dqn_i64 a, Dqn_i64 b) DQN_API Dqn_u64 Dqn_Safe_AddU64(Dqn_u64 a, Dqn_u64 b) { DQN_ASSERT_MSG(a <= DQN_U64_MAX - b, "%I64u <= %I64u", a, DQN_U64_MAX - b); - Dqn_u64 result = (a <= DQN_U64_MAX / b) ? (a * b) : DQN_U64_MAX; + Dqn_u64 result = (a <= DQN_U64_MAX / b) ? (a + b) : DQN_U64_MAX; return result; } @@ -4535,7 +4540,7 @@ DQN_API char const *Dqn_Str_TrimPrefix(char const *src, Dqn_isize size, char con } -DQN_API Dqn_u64 Dqn_Str_ToU64(char const *buf, int len) +DQN_API Dqn_u64 Dqn_Str_ToU64(char const *buf, int len, char separator) { Dqn_u64 result = 0; if (!buf) return result; @@ -4547,19 +4552,22 @@ DQN_API Dqn_u64 Dqn_Str_ToU64(char const *buf, int len) for (int buf_index = 0; buf_index < len; ++buf_index) { char ch = buf_ptr[buf_index]; - if (ch == ',') continue; - if (ch < '0' || ch > '9') break; + if (buf_index && ch == separator) + continue; + + if (ch < '0' || ch > '9') + break; Dqn_u64 val = DQN_CAST(Dqn_u64)(ch - '0'); - result = Dqn_Safe_AddU64(result, val); - result = Dqn_Safe_MulU64(result, 10); + result = Dqn_Safe_AddU64(result, val); + result = Dqn_Safe_MulU64(result, 10); } result /= 10; return result; } -DQN_API Dqn_i64 Dqn_Str_ToI64(char const *buf, int len) +DQN_API Dqn_i64 Dqn_Str_ToI64(char const *buf, int len, char separator) { Dqn_i64 result = 0; if (!buf) return result; @@ -4579,7 +4587,7 @@ DQN_API Dqn_i64 Dqn_Str_ToI64(char const *buf, int len) for (int buf_index = 0; buf_index < len; ++buf_index) { char ch = buf_ptr[buf_index]; - if (ch == ',') continue; + if (buf_index && ch == separator) continue; if (ch < '0' || ch > '9') break; Dqn_i64 val = ch - '0'; @@ -4754,6 +4762,7 @@ DQN_API Dqn_Timer Dqn_Timer_Begin() #if defined(DQN_OS_WIN32) Dqn_Timer result = {}; BOOL qpc_result = QueryPerformanceCounter(&result.start); + (void)qpc_result; DQN_ASSERT_MSG(qpc_result, "MSDN says this can only fail when running on a version older than Windows XP"); #endif return result; diff --git a/Code/Dqn_UnitTests.cpp b/Code/Dqn_UnitTests.cpp index b80f8c1..72d0dac 100644 --- a/Code/Dqn_UnitTests.cpp +++ b/Code/Dqn_UnitTests.cpp @@ -38,7 +38,7 @@ struct TestingState testing_state.allocator = Dqn_Allocator_InitWithArena(&testing_state.arena_); \ testing_state.test = {}; \ }; \ - testing_state.test.name = Dqn_String_Fmt(&testing_state.allocator, test_name); \ + testing_state.test.name = Dqn_String_InitFmt(&testing_state.allocator, test_name); \ testing_state.test.scope_started = true; \ testing_state.num_tests_in_group++ @@ -54,8 +54,8 @@ struct TestingState DQN_ASSERT(testing_state.test.scope_started); \ if (!(expr)) \ { \ - testing_state.test.fail_expr = Dqn_String_Fmt(&testing_state.allocator, #expr); \ - testing_state.test.fail_msg = Dqn_String_Fmt(&testing_state.allocator, msg, ##__VA_ARGS__); \ + testing_state.test.fail_expr = Dqn_String_InitFmt(&testing_state.allocator, #expr); \ + testing_state.test.fail_msg = Dqn_String_InitFmt(&testing_state.allocator, msg, ##__VA_ARGS__); \ } #define TEST_EXPECT(testing_state, expr) TEST_EXPECT_MSG(testing_state, expr, "") @@ -70,7 +70,7 @@ void TestingState_PrintGroupResult(TestingState const *result) bool all_tests_passed = (result->num_tests_ok_in_group == result->num_tests_in_group); char buf[256] = {}; int size = snprintf(buf, Dqn_ArrayCount(buf), "%02d/%02d Tests Passed ", result->num_tests_ok_in_group, result->num_tests_in_group); - Dqn_isize remaining_size = DESIRED_LEN - size - 1; + Dqn_isize remaining_size = DESIRED_LEN - size; remaining_size = (all_tests_passed) ? remaining_size - Dqn_CharCount(STATUS_OK) : remaining_size - Dqn_CharCount(STATUS_FAIL); remaining_size = DQN_MAX(remaining_size, 0); DQN_FOR_EACH(i, remaining_size) fprintf(stdout, " "); @@ -99,7 +99,7 @@ void TestState_PrintResult(TestState const *result) if (result->fail_expr.str) { fprintf(stdout, ANSI_COLOR_RED "%s" ANSI_COLOR_RESET "\n", STATUS_FAIL); - fprintf(stdout, "%s%sReason: [%s] %s\n", INDENT, INDENT, result->fail_expr.str, result->fail_msg.str); + fprintf(stdout, "%s%sReason: Expression failed (%s) %s\n", INDENT, INDENT, result->fail_expr.str, result->fail_msg.str); } else { @@ -754,55 +754,55 @@ static void UnitTests() { TEST_START_SCOPE(testing_state, "Convert nullptr"); Dqn_u64 result = Dqn_Str_ToU64(nullptr); - TEST_EXPECT(testing_state, result == 0); + TEST_EXPECT_MSG(testing_state, result == 0, "result: %zu", result); } { TEST_START_SCOPE(testing_state, "Convert empty string"); Dqn_u64 result = Dqn_Str_ToU64(""); - TEST_EXPECT(testing_state, result == 0); + TEST_EXPECT_MSG(testing_state, result == 0, "result: %zu", result); } { TEST_START_SCOPE(testing_state, "Convert \"1\""); Dqn_u64 result = Dqn_Str_ToU64("1"); - TEST_EXPECT(testing_state, result == 1); + TEST_EXPECT_MSG(testing_state, result == 1, "result: %zu", result); } { TEST_START_SCOPE(testing_state, "Convert \"-0\""); Dqn_u64 result = Dqn_Str_ToU64("-0"); - TEST_EXPECT(testing_state, result == 0); + TEST_EXPECT_MSG(testing_state, result == 0, "result: %zu", result); } { TEST_START_SCOPE(testing_state, "Convert \"-1\""); Dqn_u64 result = Dqn_Str_ToU64("-1"); - TEST_EXPECT(testing_state, result == 0); + TEST_EXPECT_MSG(testing_state, result == 0, "result: %zu", result); } { TEST_START_SCOPE(testing_state, "Convert \"1.2\""); Dqn_u64 result = Dqn_Str_ToU64("1.2"); - TEST_EXPECT(testing_state, result == 1); + TEST_EXPECT_MSG(testing_state, result == 1, "result: %zu", result); } { TEST_START_SCOPE(testing_state, "Convert \"1,234\""); Dqn_u64 result = Dqn_Str_ToU64("1,234"); - TEST_EXPECT(testing_state, result == 1234); + TEST_EXPECT_MSG(testing_state, result == 1234, "result: %zu", result); } { TEST_START_SCOPE(testing_state, "Convert \"1,2\""); Dqn_u64 result = Dqn_Str_ToU64("1,2"); - TEST_EXPECT(testing_state, result == 12); + TEST_EXPECT_MSG(testing_state, result == 12, "result: %zu", result); } { TEST_START_SCOPE(testing_state, "Convert \"12a3\""); Dqn_u64 result = Dqn_Str_ToU64("12a3"); - TEST_EXPECT(testing_state, result == 12); + TEST_EXPECT_MSG(testing_state, result == 12, "result: %zu", result); } } diff --git a/Project/dqn.rdbg b/Project/dqn.rdbg new file mode 100644 index 0000000000000000000000000000000000000000..0198314c892fee8374dee3fe1ef41a431709322b GIT binary patch literal 161 zcmWG?adOvSU|?{wit)(LO^tERPf3kQDa?y;D$UGEiE$~+ix17qED1?1E-BVatw;qb P#Sa*v((L$^5iJV