add: itoa && create test_int func to test func return int and test_str to ...

This commit is contained in:
starnakin 2023-06-18 12:39:18 +02:00
parent 60ca301082
commit 496b92aed9
6 changed files with 95 additions and 11 deletions

37
src/itoa.🗿 Normal file
View File

@ -0,0 +1,37 @@
itoa_get_size(number)
{
local size;
size = 0;
if (number == 0)
size++;
loop
{
if (number == 0)
return (size);
number = number / 10;
size++;
}
}
itoa(number)
{
local str;
local size;
size = itoa_get_size(number);
str = galloc(size + 1);
if (str == 0)
return (0);
[str + size] = 0;
if (number == 0)
[str] = '0';
loop
{
if (number == 0)
return (str);
[str + size - 1] = number % 10 + '0';
number = number / 10;
size--;
}
}

View File

@ -5,9 +5,9 @@ main()
name = "galloc";
ptr1 = galloc(1);
test_str(ptr1, heap + LOCATION_DATA, "");
test_int(ptr1, heap + LOCATION_DATA, "");
free(ptr1);
ptr1 = galloc(1);
test_str(ptr1, heap + LOCATION_DATA, "alloc after free");
test_int(ptr1, heap + LOCATION_DATA, "alloc after free");
ptr2 = galloc(1);
}

12
tests/itoa.🗿 Normal file
View File

@ -0,0 +1,12 @@
main()
{
local ptr;
name = "itoa";
ptr = itoa(55);
test_str(ptr, "55", "");
free(ptr);
ptr = itoa(0);
test_str(ptr, "0", "");
free(ptr);
}

View File

@ -3,8 +3,8 @@ main()
local str;
name = "strchr";
str = "bozoman du 36";
test(strchr(str, 'z'), str + 2, "");
test(strchr(str, 0), str + 13, "");
test(strchr(str, '5'), 0, "");
test(strchr(str, '6'), str + 12, "");
test_str(strchr(str, 'z'), str + 2, "");
test_str(strchr(str, 0), str + 13, "");
test_str(strchr(str, '5'), 0, "");
test_str(strchr(str, '6'), str + 12, "");
}

View File

@ -1,8 +1,7 @@
main()
{
local str;
name = "strlen";
str = "bozoman du 36";
test(strlen(str), 13, "");
test(strlen(""), 0, "Empty string");
test_int(strlen("bozoman du 36"), 13, "");
test_int(strlen(""), 0, "Empty string");
}

View File

@ -3,7 +3,7 @@ global name;
test_str(value, reach_value, description)
{
putstr(name);
if (value != reach_value)
if (strcmp(value, reach_value))
{
putstr(": ERROR: ");
putstr(", ");
@ -21,3 +21,39 @@ test_str(value, reach_value, description)
}
wrt '\n';
}
test_int(value, reach_value, description)
{
local value_str;
local reach_value_str;
putstr(name);
if (value != reach_value)
{
value_str = itoa(value);
if (value_str == 0)
return (0);
reach_value_str = itoa(value);
if (reach_value_str == 0)
{
free(value_str);
return (0);
}
putstr(": ERROR: ");
putstr(", ");
putstr(description);
putstr(" [");
putstr(reach_value_str);
putstr(" != ");
putstr(value_str);
putstr("]");
free(value_str);
free(reach_value_str);
}
else
{
putstr(": OK: ");
putstr(description);
}
wrt '\n';
}