From 3984056ee5b6b750a59e7ed034cdc948508b9b93 Mon Sep 17 00:00:00 2001 From: Lephenixnoir Date: Thu, 20 May 2021 10:59:19 +0200 Subject: [PATCH] stdlib: add tests for strol, strtoul, strtoll --- include/ft/all-tests.h | 3 +- src/main.c | 3 +- src/stdlib/intconv.c | 118 +++++++++++++++++++++++++++++++++++++---- 3 files changed, 112 insertions(+), 12 deletions(-) diff --git a/include/ft/all-tests.h b/include/ft/all-tests.h index b7e2cc3..0f8c656 100644 --- a/include/ft/all-tests.h +++ b/include/ft/all-tests.h @@ -20,7 +20,8 @@ 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; +extern ft_test ft_stdlib_llconv; +extern ft_test ft_stdlib_lconv; /* string */ extern ft_test ft_string_memset; diff --git a/src/main.c b/src/main.c index 2dff8b0..82510af 100644 --- a/src/main.c +++ b/src/main.c @@ -36,7 +36,8 @@ ft_list headers_libc[] = { { "", (ft_test*[]){ &ft_stdlib_arith, &ft_stdlib_sizes, - &ft_stdlib_intconv, + &ft_stdlib_llconv, + &ft_stdlib_lconv, NULL, }}, { "", (ft_test*[]){ diff --git a/src/stdlib/intconv.c b/src/stdlib/intconv.c index 2534dc2..6cfd876 100644 --- a/src/stdlib/intconv.c +++ b/src/stdlib/intconv.c @@ -5,36 +5,47 @@ #include #include +/* All these macros have free variables "func" and "format". */ + +/* Assert that converting (string) gives (result) with (errno == 0). */ #define assert_conv(string, result) { \ errno = 0; \ - ft_assert_eval(t, strtoull(string, NULL, 0), result, "%llu"); \ + ft_assert_eval(t, func(string, NULL, 0), result, format); \ ft_assert(t, errno == 0); \ } +/* Same with a custom base. */ #define assert_conv_base(string, base, result) { \ errno = 0; \ - ft_assert_eval(t, strtoull(string, NULL, base), result, "%llu"); \ + ft_assert_eval(t, func(string, NULL, base), result, format); \ ft_assert(t, errno == 0); \ } +/* Assert that converting (string) gives (errno == error). */ #define assert_errno(string, error) { \ errno = 0; \ ft_log(t, string " should be errno " #error "\n"); \ - strtoull(string, NULL, 0); \ + func(string, NULL, 0); \ ft_assert(t, errno == error); \ } +/* Assert that converting (string) sets (endptr == string + distance). */ #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); \ + func(_str, &_end, base); \ ft_assert(t, _end - _str == distance); \ } -static void _ft_stdlib_intconv(ft_test *t) +static void _ft_stdlib_llconv(ft_test *t) { - ft_log(t, "Simple unsigned decimal:\n"); + #define func strtoull + #define format "%llu" + + ft_log(t, "--- strtoull ---\n"); + + ft_log(t, "\nSimple unsigned decimal:\n"); assert_conv("73", 73ull); assert_conv("0", 0ull); - assert_conv("1729", 1729ull); + assert_conv(" 1729", 1729ull); assert_conv("18446744073709551615", 18446744073709551615ull); ft_log(t, "\nOverflow situations:\n"); @@ -63,6 +74,7 @@ static void _ft_stdlib_intconv(ft_test *t) assert_conv_base("fxlibc", 36, 963423480ull); ft_log(t, "\nEnd pointer for valid cases:\n"); + assert_end(" 987", 0, 7); assert_end("73_test", 0, 2); assert_end("0xdeadbeef404te3", 16, 13); assert_end("-0109637", 0, 4); @@ -77,9 +89,95 @@ static void _ft_stdlib_intconv(ft_test *t) assert_end("891", 8, 0); assert_end("-891", 8, 0); assert_end("fxlibc", 0, 0); + assert_end(" ++", 0, 0); + + #undef func + #undef format + #define func strtoll + #define format "%lld" + + ft_log(t, "\n--- strtoll ---\n"); + + ft_log(t, "\nSimple signed decimal:\n"); + assert_conv("73", 73ll); + assert_conv("-73", -73ll); + assert_conv("0", 0ll); + assert_conv("-0", 0ll); + assert_conv("-1729", -1729ll); + assert_conv("9223372036854775807", 9223372036854775807ll); + assert_conv("-9223372036854775808", -9223372036854775807ll-1); + + ft_log(t, "\nOverflow situations:\n"); + assert_errno("9223372036854775807", 0); + assert_errno("9223372036854775808", ERANGE); + assert_errno("-9223372036854775807", 0); + assert_errno("-9223372036854775808", 0); + assert_errno("-9223372036854775809", ERANGE); + + ft_log(t, "\nGeneral base for negative values:\n"); + assert_conv_base(" -0x37", 16, -55ll); + assert_conv_base(" -0x37", 0, -55ll); + assert_conv_base("-00080", 8, 0ll); + assert_conv_base("-00080", 0, 0ll); + assert_conv_base("-fxlibc", 34, -726477026ll); } -ft_test ft_stdlib_intconv = { - .name = "Integer conversion", - .function = _ft_stdlib_intconv, +ft_test ft_stdlib_llconv = { + .name = "long long conversion", + .function = _ft_stdlib_llconv, +}; + +static void _ft_stdlib_lconv(ft_test *t) +{ + ft_log(t, "See also: long long conversion\n"); + + #undef func + #undef format + #define func strtoul + #define format "%lu" + + ft_log(t, "\n--- strtoul ---\n"); + + ft_log(t, "\nSimple unsigned decimal:\n"); + assert_conv("73", 73ul); + assert_conv("0", 0ul); + assert_conv(" 1729", 1729ul); + assert_conv("4294967295", 4294967295ul); + + ft_log(t, "\nOverflow situations:\n"); + assert_errno("4294967296", ERANGE); + assert_errno("-0", 0); + assert_errno("-1", ERANGE); + assert_errno("-73", ERANGE); + assert_errno("-4294967296", ERANGE); + assert_errno("0xffffffff", 0); + assert_errno("0x100000001", ERANGE); + + #undef func + #undef format + #define func strtol + #define format "%ld" + + ft_log(t, "\n--- strtol ---\n"); + + ft_log(t, "\nSimple signed decimal:\n"); + assert_conv("73", 73l); + assert_conv("-73", -73l); + assert_conv("0", 0l); + assert_conv("-0", 0l); + assert_conv("-1729", -1729l); + assert_conv("2147483647", 2147483647l); + assert_conv("-2147483648", -2147483647l-1); + + ft_log(t, "\nOverflow situations:\n"); + assert_errno("2147483647", 0); + assert_errno("2147483648", ERANGE); + assert_errno("-2147483647", 0); + assert_errno("-2147483648", 0); + assert_errno("-2147483649", ERANGE); +} + +ft_test ft_stdlib_lconv = { + .name = "long conversion", + .function = _ft_stdlib_lconv, };