Implement common_atoi and add unit tests
This commit is contained in:
parent
1f2c2082a0
commit
84a2c5e382
37
src/Common.c
37
src/Common.c
@ -74,8 +74,41 @@ void common_itoa(i32 value, char *buf, i32 bufSize)
|
||||
reverseString(buf, common_strlen(buf));
|
||||
}
|
||||
|
||||
i32 common_atoi(char *string, i32 len)
|
||||
// TODO(doyle): Consider if we should trash ptrs in string operations in general
|
||||
i32 common_atoi(const char *string, const i32 len)
|
||||
{
|
||||
if (len == 0) return -1;
|
||||
|
||||
// TODO(doyle): Implement ATOI
|
||||
return 0;
|
||||
i32 index = 0;
|
||||
if (string[index] != '-' && string[index] != '+' &&
|
||||
(string[index] < '0' || string[index] > '9'))
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
b32 isNegative = FALSE;
|
||||
if (string[index] == '-' || string[index] == '+')
|
||||
{
|
||||
if (string[index] == '-') isNegative = TRUE;
|
||||
index++;
|
||||
}
|
||||
|
||||
i32 result = 0;
|
||||
for (i32 i = index; i < len; i++)
|
||||
{
|
||||
if (string[i] >= '0' && string[i] <= '9')
|
||||
{
|
||||
result *= 10;
|
||||
result += string[i] - '0';
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (isNegative) result *= -1;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
@ -305,7 +305,7 @@ INTERNAL void assetInit(GameState *state)
|
||||
if (common_strcmp(nextToken->string, node->parent->name) == 0)
|
||||
{
|
||||
node->parent->isClosed = TRUE;
|
||||
node = node->parent;
|
||||
node = node->parent;
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -402,7 +402,9 @@ INTERNAL void assetInit(GameState *state)
|
||||
}
|
||||
}
|
||||
|
||||
#if 1
|
||||
DEBUG_RECURSIVE_PRINT_XML_TREE(&root);
|
||||
#endif
|
||||
|
||||
node = &root;
|
||||
while (node)
|
||||
@ -420,6 +422,12 @@ INTERNAL void assetInit(GameState *state)
|
||||
{
|
||||
// TODO(doyle): Fill in details properly
|
||||
Rect rect = {0};
|
||||
|
||||
// TODO(doyle): Work around for now in xml reading,
|
||||
// reading the last node closing node not being merged
|
||||
// to the parent
|
||||
if (!subTextureAttrib->name) continue;
|
||||
|
||||
if (common_strcmp(subTextureAttrib->name, "name") == 0)
|
||||
{
|
||||
}
|
||||
@ -762,11 +770,29 @@ INTERNAL v2 getPosRelativeToRect(Rect rect, v2 offset,
|
||||
return result;
|
||||
}
|
||||
|
||||
INTERNAL void unitTest()
|
||||
{
|
||||
ASSERT(common_atoi("-2", common_strlen("-2")) == -2);
|
||||
ASSERT(common_atoi("100", common_strlen("100")) == 100);
|
||||
ASSERT(common_atoi("1", common_strlen("1")) == 1);
|
||||
ASSERT(common_atoi("954 32", common_strlen("954 32")) == 954);
|
||||
|
||||
ASSERT(common_atoi("", 0) == -1);
|
||||
ASSERT(common_atoi(" 32", common_strlen(" 32")) == -1);
|
||||
ASSERT(common_atoi("+32", common_strlen("+32")) == 32);
|
||||
ASSERT(common_atoi("+ 32", common_strlen("+ 32")) == 0);
|
||||
|
||||
}
|
||||
|
||||
// TODO(doyle): Remove and implement own random generator!
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
void worldTraveller_gameInit(GameState *state, v2 windowSize)
|
||||
{
|
||||
#ifdef DENGINE_DEBUG
|
||||
unitTest();
|
||||
#endif
|
||||
|
||||
i32 result = audio_init(&state->audioManager);
|
||||
if (result)
|
||||
{
|
||||
|
@ -35,6 +35,6 @@ char *common_memset(char *const ptr, const i32 value, const i32 numBytes);
|
||||
|
||||
// Max buffer size should be 11 for 32 bit integers
|
||||
void common_itoa(i32 value, char *buf, i32 bufSize);
|
||||
i32 common_atoi(char *string, i32 len);
|
||||
i32 common_atoi(const char *string, const i32 len);
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user