From d93b77724fbf20b910977a40611045c186acf344 Mon Sep 17 00:00:00 2001 From: Lephenixnoir Date: Wed, 19 May 2021 21:45:20 +0200 Subject: [PATCH] stdlib: add a test for strtoull --- CMakeLists.txt | 1 + include/ft/all-tests.h | 1 + src/main.c | 1 + src/stdlib/intconv.c | 85 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 88 insertions(+) create mode 100644 src/stdlib/intconv.c diff --git a/CMakeLists.txt b/CMakeLists.txt index f9723d3..f303f27 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,6 +29,7 @@ set(SOURCES src/inttypes/sizes.c # stdlib src/stdlib/arith.c + src/stdlib/intconv.c src/stdlib/sizes.c # string src/string/memarray.c diff --git a/include/ft/all-tests.h b/include/ft/all-tests.h index 4e62cc3..b7e2cc3 100644 --- a/include/ft/all-tests.h +++ b/include/ft/all-tests.h @@ -20,6 +20,7 @@ extern ft_test ft_inttypes_functions; /* stdlib */ extern ft_test ft_stdlib_arith; extern ft_test ft_stdlib_sizes; +extern ft_test ft_stdlib_intconv; /* string */ extern ft_test ft_string_memset; diff --git a/src/main.c b/src/main.c index a3f8ac5..2dff8b0 100644 --- a/src/main.c +++ b/src/main.c @@ -36,6 +36,7 @@ ft_list headers_libc[] = { { "", (ft_test*[]){ &ft_stdlib_arith, &ft_stdlib_sizes, + &ft_stdlib_intconv, NULL, }}, { "", (ft_test*[]){ diff --git a/src/stdlib/intconv.c b/src/stdlib/intconv.c new file mode 100644 index 0000000..2534dc2 --- /dev/null +++ b/src/stdlib/intconv.c @@ -0,0 +1,85 @@ +#include +#include +#include + +#include +#include + +#define assert_conv(string, result) { \ + errno = 0; \ + ft_assert_eval(t, strtoull(string, NULL, 0), result, "%llu"); \ + ft_assert(t, errno == 0); \ +} +#define assert_conv_base(string, base, result) { \ + errno = 0; \ + ft_assert_eval(t, strtoull(string, NULL, base), result, "%llu"); \ + ft_assert(t, errno == 0); \ +} +#define assert_errno(string, error) { \ + errno = 0; \ + ft_log(t, string " should be errno " #error "\n"); \ + strtoull(string, NULL, 0); \ + ft_assert(t, errno == error); \ +} +#define assert_end(string, base, distance) { \ + char const *_str = string; \ + char *_end; \ + ft_log(t, string " (base " #base ") should read " #distance "\n"); \ + strtoull(_str, &_end, base); \ + ft_assert(t, _end - _str == distance); \ +} + +static void _ft_stdlib_intconv(ft_test *t) +{ + ft_log(t, "Simple unsigned decimal:\n"); + assert_conv("73", 73ull); + assert_conv("0", 0ull); + assert_conv("1729", 1729ull); + assert_conv("18446744073709551615", 18446744073709551615ull); + + ft_log(t, "\nOverflow situations:\n"); + assert_errno("18446744073709551616", ERANGE); + assert_errno("-0", 0); + assert_errno("-1", ERANGE); + assert_errno("-73", ERANGE); + assert_errno("-18446744073709551616", ERANGE); + assert_errno("0xffffffffffffffff", 0); + assert_errno("0x10000000000000001", ERANGE); + + ft_log(t, "\nBase detection:\n"); + assert_conv_base("0101", 0, 65ull); + assert_conv_base("0x101", 0, 257ull); + assert_conv_base("0X101", 0, 257ull); + assert_conv_base("0101", 8, 65ull); + assert_conv_base("0x101", 16, 257ull); + assert_conv_base("0X101", 16, 257ull); + + ft_log(t, "\nGeneral bases:\n"); + assert_conv_base("01001001", 2, 73ull); + assert_conv_base("257", 16, 599ull); + assert_conv_base("fxlibc", 33, 15ull); + assert_conv_base("fxlibc", 34, 726477026ull); + assert_conv_base("fxlibc", 35, 838271572ull); + assert_conv_base("fxlibc", 36, 963423480ull); + + ft_log(t, "\nEnd pointer for valid cases:\n"); + assert_end("73_test", 0, 2); + assert_end("0xdeadbeef404te3", 16, 13); + assert_end("-0109637", 0, 4); + assert_end("-109637", 0, 7); + assert_end("73dec", 0, 2); + assert_end("7891", 8, 1); + + ft_log(t, "\nEnd pointer for invalid cases:\n"); + assert_end("-!!", 0, 0); + assert_end("-0xk", 0, 0); + assert_end("", 16, 0); + assert_end("891", 8, 0); + assert_end("-891", 8, 0); + assert_end("fxlibc", 0, 0); +} + +ft_test ft_stdlib_intconv = { + .name = "Integer conversion", + .function = _ft_stdlib_intconv, +};