From 496b92aed9ee82c865d5dbad60959272fd3071bd Mon Sep 17 00:00:00 2001 From: starnakin Date: Sun, 18 Jun 2023 12:39:18 +0200 Subject: [PATCH] add: itoa && create test_int func to test func return int and test_str to ... --- src/itoa.🗿 | 37 +++++++++++++++++++++++++++++++++++++ tests/galloc.🗿 | 4 ++-- tests/itoa.🗿 | 12 ++++++++++++ tests/strchr.🗿 | 8 ++++---- tests/strlen.🗿 | 7 +++---- tests/test.🗿 | 38 +++++++++++++++++++++++++++++++++++++- 6 files changed, 95 insertions(+), 11 deletions(-) create mode 100644 src/itoa.🗿 create mode 100644 tests/itoa.🗿 diff --git a/src/itoa.🗿 b/src/itoa.🗿 new file mode 100644 index 0000000..81a562e --- /dev/null +++ b/src/itoa.🗿 @@ -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--; + } +} diff --git a/tests/galloc.🗿 b/tests/galloc.🗿 index 2795c9a..370e7de 100644 --- a/tests/galloc.🗿 +++ b/tests/galloc.🗿 @@ -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); } diff --git a/tests/itoa.🗿 b/tests/itoa.🗿 new file mode 100644 index 0000000..1294afa --- /dev/null +++ b/tests/itoa.🗿 @@ -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); +} diff --git a/tests/strchr.🗿 b/tests/strchr.🗿 index 2a518f0..1aa1706 100644 --- a/tests/strchr.🗿 +++ b/tests/strchr.🗿 @@ -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, ""); } diff --git a/tests/strlen.🗿 b/tests/strlen.🗿 index 70ca382..4524a3a 100644 --- a/tests/strlen.🗿 +++ b/tests/strlen.🗿 @@ -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"); } diff --git a/tests/test.🗿 b/tests/test.🗿 index 01bdaa3..f7c9fb5 100644 --- a/tests/test.🗿 +++ b/tests/test.🗿 @@ -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'; +}