From 57e78cb70aea539ec3b36f532f11f2dae67a5f6e Mon Sep 17 00:00:00 2001 From: starnakin Date: Sun, 18 Jun 2023 20:25:15 +0200 Subject: [PATCH] add: signed number --- src/ntoa_s.🗿 | 12 ++++++++---- src/putnum_s.🗿 | 6 ++++++ tests/aton_s.🗿 | 15 ++++++++------- tests/test.🗿 | 22 ++++++++++++++++++++++ 4 files changed, 44 insertions(+), 11 deletions(-) create mode 100644 src/putnum_s.🗿 diff --git a/src/ntoa_s.🗿 b/src/ntoa_s.🗿 index f5e206c..92e6bf8 100644 --- a/src/ntoa_s.🗿 +++ b/src/ntoa_s.🗿 @@ -15,17 +15,21 @@ ntoa_get_size(number) ntoa_s(number) { - local str; - local size; + local str, sign, size; - size = ntoa_get_size(number); if (number >= 0x8000) + { + number = 0 - number; + sign = 1; + } + size = ntoa_get_size(number); + if (sign) size++; str = galloc(size + 1); if (str == 0) return (0); [str + size] = 0; - if (number >= 0x8000) + if (sign) [str] = '-'; else if (number == 0) [str] = '0'; diff --git a/src/putnum_s.🗿 b/src/putnum_s.🗿 new file mode 100644 index 0000000..4c4878e --- /dev/null +++ b/src/putnum_s.🗿 @@ -0,0 +1,6 @@ +putnum_s(num_s) +{ + local str = ntoa_s(num_s); + putstr(str); + free(str); +} diff --git a/tests/aton_s.🗿 b/tests/aton_s.🗿 index ffb5fc6..1d8db46 100644 --- a/tests/aton_s.🗿 +++ b/tests/aton_s.🗿 @@ -2,11 +2,12 @@ main() { name = "aton_s"; - test_num(aton_s("33"), 33, ""); - test_num(aton_s(""), 0, ""); - test_num(aton_s("40"), 40, ""); - test_num(aton_s("1"), 1, ""); - test_num(aton_s("-"), 0, ""); - test_num(aton_s("-40"), 0 - 40, ""); - test_num(aton_s("-1"), 0 - 1, ""); + test_num_s(aton_s("33"), 33, ""); + test_num_s(aton_s(""), 0, ""); + test_num_s(aton_s("40"), 40, ""); + test_num_s(aton_s("1"), 1, ""); + test_num_s(aton_s("-"), 0, ""); + test_num_s(aton_s("-40"), 0 - 40, ""); + test_num_s(aton_s("-1"), 0 - 1, ""); + test_num_s(aton_s("-1"), 0 - 2, ""); } diff --git a/tests/test.🗿 b/tests/test.🗿 index f59f76b..e598296 100644 --- a/tests/test.🗿 +++ b/tests/test.🗿 @@ -44,6 +44,28 @@ test_num(value, reach_value, description) wrt '\n'; } +test_num_s(value, reach_value, description) +{ + putstr(name); + if (value != reach_value) + { + putstr(": ERROR: "); + putstr(", "); + putstr(description); + putstr(" ["); + putnum_s(reach_value); + putstr(" != "); + putnum_s(value); + putstr("]"); + } + else + { + putstr(": OK: "); + putstr(description); + } + wrt '\n'; +} + test_tab_str(value, reach_value, description) { putstr(name);