Fix the unit tests

This commit is contained in:
doyle 2021-05-18 21:15:36 +10:00
parent fb60fc44e1
commit 4442ccfb03
3 changed files with 34 additions and 25 deletions

View File

@ -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_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_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 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<T> Dqn_Map_InitWithMemory(void *mem, Dqn_isize mem_size)
// NOTE: Generate the map // NOTE: Generate the map
// //
Dqn_isize bytes_required = bytes_for_values + bytes_for_usage_bitset; Dqn_isize bytes_required = bytes_for_values + bytes_for_usage_bitset;
(void)bytes_required;
DQN_ASSERT_MSG(bytes_required <= mem_size, DQN_ASSERT_MSG(bytes_required <= mem_size,
"(bytes_for_values = %I64d, bytes_for_usage_bitset = %I64d, mem_size = %I64d)", "(bytes_for_values = %I64d, bytes_for_usage_bitset = %I64d, mem_size = %I64d)",
bytes_for_values, bytes_for_usage_bitset, mem_size); bytes_for_values, bytes_for_usage_bitset, mem_size);
@ -2697,7 +2700,7 @@ DQN_API void Dqn_Array_Pop(Dqn_Array<T> *a, Dqn_isize num, Dqn_ZeroMem zero_mem)
void *begin = a->data + a->size; void *begin = a->data + a->size;
void *end = a->data + (a->size + num); void *end = a->data + (a->size + num);
Dqn_isize bytes = DQN_CAST(Dqn_isize) end - DQN_CAST(Dqn_isize) begin; 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 <typename T> template <typename T>
@ -3602,8 +3605,10 @@ 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); 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); result = Dqn_PointerMetadata_Init(ptr, size, alignment);
if (!result && allocator->type == Dqn_AllocatorType::XHeap) if (!result && allocator->type == Dqn_AllocatorType::XHeap)
{
DQN_ASSERT(result); DQN_ASSERT(result);
} }
}
break; break;
case Dqn_AllocatorType::Arena: case Dqn_AllocatorType::Arena:
@ -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_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_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; 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; Dqn_u64 result = 0;
if (!buf) return result; if (!buf) return result;
@ -4547,8 +4552,11 @@ DQN_API Dqn_u64 Dqn_Str_ToU64(char const *buf, int len)
for (int buf_index = 0; buf_index < len; ++buf_index) for (int buf_index = 0; buf_index < len; ++buf_index)
{ {
char ch = buf_ptr[buf_index]; char ch = buf_ptr[buf_index];
if (ch == ',') continue; if (buf_index && ch == separator)
if (ch < '0' || ch > '9') break; continue;
if (ch < '0' || ch > '9')
break;
Dqn_u64 val = DQN_CAST(Dqn_u64)(ch - '0'); Dqn_u64 val = DQN_CAST(Dqn_u64)(ch - '0');
result = Dqn_Safe_AddU64(result, val); result = Dqn_Safe_AddU64(result, val);
@ -4559,7 +4567,7 @@ DQN_API Dqn_u64 Dqn_Str_ToU64(char const *buf, int len)
return result; 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; Dqn_i64 result = 0;
if (!buf) return result; 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) for (int buf_index = 0; buf_index < len; ++buf_index)
{ {
char ch = buf_ptr[buf_index]; char ch = buf_ptr[buf_index];
if (ch == ',') continue; if (buf_index && ch == separator) continue;
if (ch < '0' || ch > '9') break; if (ch < '0' || ch > '9') break;
Dqn_i64 val = ch - '0'; Dqn_i64 val = ch - '0';
@ -4754,6 +4762,7 @@ DQN_API Dqn_Timer Dqn_Timer_Begin()
#if defined(DQN_OS_WIN32) #if defined(DQN_OS_WIN32)
Dqn_Timer result = {}; Dqn_Timer result = {};
BOOL qpc_result = QueryPerformanceCounter(&result.start); 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"); DQN_ASSERT_MSG(qpc_result, "MSDN says this can only fail when running on a version older than Windows XP");
#endif #endif
return result; return result;

View File

@ -38,7 +38,7 @@ struct TestingState
testing_state.allocator = Dqn_Allocator_InitWithArena(&testing_state.arena_); \ testing_state.allocator = Dqn_Allocator_InitWithArena(&testing_state.arena_); \
testing_state.test = {}; \ 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.test.scope_started = true; \
testing_state.num_tests_in_group++ testing_state.num_tests_in_group++
@ -54,8 +54,8 @@ struct TestingState
DQN_ASSERT(testing_state.test.scope_started); \ DQN_ASSERT(testing_state.test.scope_started); \
if (!(expr)) \ if (!(expr)) \
{ \ { \
testing_state.test.fail_expr = Dqn_String_Fmt(&testing_state.allocator, #expr); \ testing_state.test.fail_expr = Dqn_String_InitFmt(&testing_state.allocator, #expr); \
testing_state.test.fail_msg = Dqn_String_Fmt(&testing_state.allocator, msg, ##__VA_ARGS__); \ 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, "") #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); bool all_tests_passed = (result->num_tests_ok_in_group == result->num_tests_in_group);
char buf[256] = {}; 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); 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 = (all_tests_passed) ? remaining_size - Dqn_CharCount(STATUS_OK) : remaining_size - Dqn_CharCount(STATUS_FAIL);
remaining_size = DQN_MAX(remaining_size, 0); remaining_size = DQN_MAX(remaining_size, 0);
DQN_FOR_EACH(i, remaining_size) fprintf(stdout, " "); DQN_FOR_EACH(i, remaining_size) fprintf(stdout, " ");
@ -99,7 +99,7 @@ void TestState_PrintResult(TestState const *result)
if (result->fail_expr.str) if (result->fail_expr.str)
{ {
fprintf(stdout, ANSI_COLOR_RED "%s" ANSI_COLOR_RESET "\n", STATUS_FAIL); 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 else
{ {
@ -754,55 +754,55 @@ static void UnitTests()
{ {
TEST_START_SCOPE(testing_state, "Convert nullptr"); TEST_START_SCOPE(testing_state, "Convert nullptr");
Dqn_u64 result = Dqn_Str_ToU64(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"); TEST_START_SCOPE(testing_state, "Convert empty string");
Dqn_u64 result = Dqn_Str_ToU64(""); 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\""); TEST_START_SCOPE(testing_state, "Convert \"1\"");
Dqn_u64 result = Dqn_Str_ToU64("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\""); TEST_START_SCOPE(testing_state, "Convert \"-0\"");
Dqn_u64 result = Dqn_Str_ToU64("-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\""); TEST_START_SCOPE(testing_state, "Convert \"-1\"");
Dqn_u64 result = Dqn_Str_ToU64("-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\""); TEST_START_SCOPE(testing_state, "Convert \"1.2\"");
Dqn_u64 result = Dqn_Str_ToU64("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\""); TEST_START_SCOPE(testing_state, "Convert \"1,234\"");
Dqn_u64 result = Dqn_Str_ToU64("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\""); TEST_START_SCOPE(testing_state, "Convert \"1,2\"");
Dqn_u64 result = Dqn_Str_ToU64("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\""); TEST_START_SCOPE(testing_state, "Convert \"12a3\"");
Dqn_u64 result = Dqn_Str_ToU64("12a3"); Dqn_u64 result = Dqn_Str_ToU64("12a3");
TEST_EXPECT(testing_state, result == 12); TEST_EXPECT_MSG(testing_state, result == 12, "result: %zu", result);
} }
} }

BIN
Project/dqn.rdbg Normal file

Binary file not shown.