From 5c59abb6c5d8aadffae75a48a08b4070f3066699 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Mon, 24 Jan 2022 08:30:43 +0500 Subject: [PATCH] Make func "kernaux_utoa" public --- ChangeLog | 1 + include/kernaux/ntoa.h | 6 ++ src/ntoa.c | 3 +- tests/test_ntoa.c | 137 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 145 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index b394f8a..f8bcdf3 100644 --- a/ChangeLog +++ b/ChangeLog @@ -7,6 +7,7 @@ * include/kernaux/ntoa.h: Add funcs "kernaux_utoa16" and "kernaux_itoa16" * include/kernaux/ntoa.h: Add consts "KERNAUX_UTOA10_BUFFER_SIZE" and "KERNAUX_UTOA16_BUFFER_SIZE" + * include/kernaux/ntoa.h: Add func "kernaux_utoa" 2022-01-22 Alex Kotov diff --git a/include/kernaux/ntoa.h b/include/kernaux/ntoa.h index 4a48c31..a105f52 100644 --- a/include/kernaux/ntoa.h +++ b/include/kernaux/ntoa.h @@ -7,6 +7,9 @@ extern "C" { #include +// TODO: define +#define KERNAUX_UTOA_BUFFER_SIZE (0/0) + // "18446744073709551615" #define KERNAUX_UTOA10_BUFFER_SIZE 21 @@ -21,6 +24,9 @@ extern "C" { // "-8000000000000000" #define KERNAUX_ITOA16_BUFFER_SIZE 18 +// TODO: add tests for assertion +char *kernaux_utoa(uint64_t value, char *buffer, int base); + void kernaux_utoa10(uint64_t value, char *buffer); void kernaux_itoa10(int64_t value, char *buffer); diff --git a/src/ntoa.c b/src/ntoa.c index 21b30d1..d681ace 100644 --- a/src/ntoa.c +++ b/src/ntoa.c @@ -7,8 +7,7 @@ #include -// TODO: example common func, maybe it may become public -static char *kernaux_utoa(uint64_t value, char *buffer, int base) +char *kernaux_utoa(uint64_t value, char *buffer, int base) { switch (base) { case 'b': case 'B': base = 2; break; diff --git a/tests/test_ntoa.c b/tests/test_ntoa.c index 6717ab2..3b75520 100644 --- a/tests/test_ntoa.c +++ b/tests/test_ntoa.c @@ -8,6 +8,124 @@ #include #include +static const struct { + const char *result; + int base; + uint64_t value; +} utoa_cases[] = { + { "0", 2, 0 }, { "0", -2, 0 }, { "0", 'b', 0 }, { "0", 'B', 0 }, + { "0", 3, 0 }, { "0", -3, 0 }, + { "0", 4, 0 }, { "0", -4, 0 }, + { "0", 5, 0 }, { "0", -5, 0 }, + { "0", 6, 0 }, { "0", -6, 0 }, + { "0", 7, 0 }, { "0", -7, 0 }, + { "0", 8, 0 }, { "0", -8, 0 }, { "0", 'o', 0 }, { "0", 'O', 0 }, + { "0", 9, 0 }, { "0", -9, 0 }, + { "0", 10, 0 }, { "0", -10, 0 }, { "0", 'd', 0 }, { "0", 'D', 0 }, + { "0", 11, 0 }, { "0", -11, 0 }, + { "0", 12, 0 }, { "0", -12, 0 }, + { "0", 13, 0 }, { "0", -13, 0 }, + { "0", 14, 0 }, { "0", -14, 0 }, + { "0", 15, 0 }, { "0", -15, 0 }, + { "0", 16, 0 }, { "0", -16, 0 }, + { "0", 'h', 0 }, { "0", 'H', 0 }, { "0", 'x', 0 }, { "0", 'X', 0 }, + { "0", 17, 0 }, { "0", -17, 0 }, + { "0", 18, 0 }, { "0", -18, 0 }, + { "0", 19, 0 }, { "0", -19, 0 }, + { "0", 20, 0 }, { "0", -20, 0 }, + { "0", 21, 0 }, { "0", -21, 0 }, + { "0", 22, 0 }, { "0", -22, 0 }, + { "0", 23, 0 }, { "0", -23, 0 }, + { "0", 24, 0 }, { "0", -24, 0 }, + { "0", 25, 0 }, { "0", -25, 0 }, + { "0", 26, 0 }, { "0", -26, 0 }, + { "0", 27, 0 }, { "0", -27, 0 }, + { "0", 28, 0 }, { "0", -28, 0 }, + { "0", 29, 0 }, { "0", -29, 0 }, + { "0", 30, 0 }, { "0", -30, 0 }, + { "0", 31, 0 }, { "0", -31, 0 }, + { "0", 32, 0 }, { "0", -32, 0 }, + { "0", 33, 0 }, { "0", -33, 0 }, + { "0", 34, 0 }, { "0", -34, 0 }, + { "0", 35, 0 }, { "0", -35, 0 }, + { "0", 36, 0 }, { "0", -36, 0 }, + { "1111011", 2, 123 }, { "1111011", -2, 123 }, + { "1111011", 'b', 123 }, { "1111011", 'B', 123 }, + { "11120", 3, 123 }, { "11120", -3, 123 }, + { "1323", 4, 123 }, { "1323", -4, 123 }, + { "443", 5, 123 }, { "443", -5, 123 }, + { "323", 6, 123 }, { "323", -6, 123 }, + { "234", 7, 123 }, { "234", -7, 123 }, + { "173", 8, 123 }, { "173", -8, 123 }, + { "173", 'o', 123 }, { "173", 'O', 123 }, + { "146", 9, 123 }, { "146", -9, 123 }, + { "123", 10, 123 }, { "123", -10, 123 }, + { "123", 'd', 123 }, { "123", 'D', 123 }, + { "102", 11, 123 }, { "102", -11, 123 }, + { "a3", 12, 123 }, { "A3", -12, 123 }, + { "96", 13, 123 }, { "96", -13, 123 }, + { "8b", 14, 123 }, { "8B", -14, 123 }, + { "83", 15, 123 }, { "83", -15, 123 }, + { "7b", 16, 123 }, { "7B", -16, 123 }, + { "7b", 'h', 123 }, { "7B", 'H', 123 }, + { "7b", 'x', 123 }, { "7B", 'X', 123 }, + { "74", 17, 123 }, { "74", -17, 123 }, + { "6f", 18, 123 }, { "6F", -18, 123 }, + { "69", 19, 123 }, { "69", -19, 123 }, + { "63", 20, 123 }, { "63", -20, 123 }, + { "5i", 21, 123 }, { "5I", -21, 123 }, + { "5d", 22, 123 }, { "5D", -22, 123 }, + { "58", 23, 123 }, { "58", -23, 123 }, + { "53", 24, 123 }, { "53", -24, 123 }, + { "4n", 25, 123 }, { "4N", -25, 123 }, + { "4j", 26, 123 }, { "4J", -26, 123 }, + { "4f", 27, 123 }, { "4F", -27, 123 }, + { "4b", 28, 123 }, { "4B", -28, 123 }, + { "47", 29, 123 }, { "47", -29, 123 }, + { "43", 30, 123 }, { "43", -30, 123 }, + { "3u", 31, 123 }, { "3U", -31, 123 }, + { "3r", 32, 123 }, { "3R", -32, 123 }, + { "3o", 33, 123 }, { "3O", -33, 123 }, + { "3l", 34, 123 }, { "3L", -34, 123 }, + { "3i", 35, 123 }, { "3I", -35, 123 }, + { "3f", 36, 123 }, { "3F", -36, 123 }, + { "11000000111001", 2, 12345 }, { "11000000111001", -2, 12345 }, + { "121221020", 3, 12345 }, { "121221020", -3, 12345 }, + { "3000321", 4, 12345 }, { "3000321", -4, 12345 }, + { "343340", 5, 12345 }, { "343340", -5, 12345 }, + { "133053", 6, 12345 }, { "133053", -6, 12345 }, + { "50664", 7, 12345 }, { "50664", -7, 12345 }, + { "30071", 8, 12345 }, { "30071", -8, 12345 }, + { "17836", 9, 12345 }, { "17836", -9, 12345 }, + { "12345", 10, 12345 }, { "12345", -10, 12345 }, + { "9303", 11, 12345 }, { "9303", -11, 12345 }, + { "7189", 12, 12345 }, { "7189", -12, 12345 }, + { "5808", 13, 12345 }, { "5808", -13, 12345 }, + { "46db", 14, 12345 }, { "46DB", -14, 12345 }, + { "39d0", 15, 12345 }, { "39D0", -15, 12345 }, + { "3039", 16, 12345 }, { "3039", -16, 12345 }, + { "28c3", 17, 12345 }, { "28C3", -17, 12345 }, + { "221f", 18, 12345 }, { "221F", -18, 12345 }, + { "1f3e", 19, 12345 }, { "1F3E", -19, 12345 }, + { "1ah5", 20, 12345 }, { "1AH5", -20, 12345 }, + { "16ki", 21, 12345 }, { "16KI", -21, 12345 }, + { "13b3", 22, 12345 }, { "13B3", -22, 12345 }, + { "107h", 23, 12345 }, { "107H", -23, 12345 }, + { "la9", 24, 12345 }, { "LA9", -24, 12345 }, + { "jik", 25, 12345 }, { "JIK", -25, 12345 }, + { "i6l", 26, 12345 }, { "I6L", -26, 12345 }, + { "gp6", 27, 12345 }, { "GP6", -27, 12345 }, + { "fkp", 28, 12345 }, { "FKP", -28, 12345 }, + { "ejk", 29, 12345 }, { "EJK", -29, 12345 }, + { "dlf", 30, 12345 }, { "DLF", -30, 12345 }, + { "cq7", 31, 12345 }, { "CQ7", -31, 12345 }, + { "c1p", 32, 12345 }, { "C1P", -32, 12345 }, + { "bb3", 33, 12345 }, { "BB3", -33, 12345 }, + { "an3", 34, 12345 }, { "AN3", -34, 12345 }, + { "a2p", 35, 12345 }, { "A2P", -35, 12345 }, + { "9ix", 36, 12345 }, { "9IX", -36, 12345 }, +}; + static const struct { uint64_t value; const char *result; @@ -246,6 +364,25 @@ static const struct { int main() { + { + char buffer[1000]; + + for ( + size_t index = 0; + index < sizeof(utoa_cases) / sizeof(utoa_cases[0]); + ++index + ) { + const char *const end = kernaux_utoa(utoa_cases[index].value, buffer, utoa_cases[index].base); + assert(strcmp(buffer, utoa_cases[index].result) == 0); + for (const char *pos = buffer;; ++pos) { + if (*pos == '\0') { + assert(end == pos); + break; + } + } + } + } + { char buffer[KERNAUX_UTOA10_BUFFER_SIZE];