stdlib: add a test for strtoull

This commit is contained in:
Lephenixnoir 2021-05-19 21:45:20 +02:00
parent a487b64fb3
commit d93b77724f
Signed by: Lephenixnoir
GPG Key ID: 1BBA026E13FC0495
4 changed files with 88 additions and 0 deletions

View File

@ -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

View File

@ -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;

View File

@ -36,6 +36,7 @@ ft_list headers_libc[] = {
{ "<stdlib.h>", (ft_test*[]){
&ft_stdlib_arith,
&ft_stdlib_sizes,
&ft_stdlib_intconv,
NULL,
}},
{ "<string.h>", (ft_test*[]){

85
src/stdlib/intconv.c Normal file
View File

@ -0,0 +1,85 @@
#include <stdlib.h>
#include <limits.h>
#include <errno.h>
#include <ft/test.h>
#include <ft/all-tests.h>
#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,
};