diff --git a/include/ft/all-tests.h b/include/ft/all-tests.h index 0f8c656..7601346 100644 --- a/include/ft/all-tests.h +++ b/include/ft/all-tests.h @@ -8,10 +8,8 @@ #include /* ctype */ -extern ft_test ft_ctype_macros; -extern ft_test ft_ctype_functions; -extern ft_test ft_ctype_convert_macros; -extern ft_test ft_ctype_convert_functions; +extern ft_test ft_ctype_classes; +extern ft_test ft_ctype_conversion; /* inttypes */ extern ft_test ft_inttypes_sizes; diff --git a/src/ctype/classes.c b/src/ctype/classes.c index 0cb8c0d..cfada3d 100644 --- a/src/ctype/classes.c +++ b/src/ctype/classes.c @@ -4,63 +4,56 @@ #include "charprops.h" -static void _ctype_macros(ft_test *t) +static void _ctype_classes(ft_test *t) { for(int c = 0; c < 128; c++) { - ft_assert(t, (isalnum(c) != 0) == ((charprops[c] & CP_ALNUM) != 0)); - ft_assert(t, (isalpha(c) != 0) == ((charprops[c] & CP_ALPHA) != 0)); - ft_assert(t, (isblank(c) != 0) == ((charprops[c] & CP_BLANK) != 0)); - ft_assert(t, (iscntrl(c) != 0) == ((charprops[c] & CP_CNTRL) != 0)); - ft_assert(t, (isdigit(c) != 0) == ((charprops[c] & CP_DIGIT) != 0)); - ft_assert(t, (islower(c) != 0) == ((charprops[c] & CP_LOWER) != 0)); - ft_assert(t, (isgraph(c) != 0) == ((charprops[c] & CP_GRAPH) != 0)); - ft_assert(t, (ispunct(c) != 0) == ((charprops[c] & CP_PUNCT) != 0)); - ft_assert(t, (isprint(c) != 0) == ((charprops[c] & CP_PRINT) != 0)); - ft_assert(t, (isspace(c) != 0) == ((charprops[c] & CP_SPACE) != 0)); - ft_assert(t, (isupper(c) != 0) == ((charprops[c] & CP_UPPER) != 0)); - ft_assert(t, (isxdigit(c) != 0) == ((charprops[c] & CP_XDIGIT) != 0)); + uint16_t props_f=0, props_m=0; + if(isalnum(c)) props_f |= CP_ALNUM; + if(isalpha(c)) props_f |= CP_ALPHA; + if(isblank(c)) props_f |= CP_BLANK; + if(iscntrl(c)) props_f |= CP_CNTRL; + if(isdigit(c)) props_f |= CP_DIGIT; + if(islower(c)) props_f |= CP_LOWER; + if(isgraph(c)) props_f |= CP_GRAPH; + if(ispunct(c)) props_f |= CP_PUNCT; + if(isprint(c)) props_f |= CP_PRINT; + if(isspace(c)) props_f |= CP_SPACE; + if(isupper(c)) props_f |= CP_UPPER; + if(isxdigit(c)) props_f |= CP_XDIGIT; + + #undef isalnum + #undef isalpha + #undef isblank + #undef iscntrl + #undef isdigit + #undef islower + #undef isgraph + #undef ispunct + #undef isprint + #undef isspace + #undef isupper + #undef isxdigit + #undef tolower + #undef toupper + + if(isalnum(c)) props_m |= CP_ALNUM; + if(isalpha(c)) props_m |= CP_ALPHA; + if(isblank(c)) props_m |= CP_BLANK; + if(iscntrl(c)) props_m |= CP_CNTRL; + if(isdigit(c)) props_m |= CP_DIGIT; + if(islower(c)) props_m |= CP_LOWER; + if(isgraph(c)) props_m |= CP_GRAPH; + if(ispunct(c)) props_m |= CP_PUNCT; + if(isprint(c)) props_m |= CP_PRINT; + if(isspace(c)) props_m |= CP_SPACE; + if(isupper(c)) props_m |= CP_UPPER; + if(isxdigit(c)) props_m |= CP_XDIGIT; + + ft_assert(t, props_f == charprops[c] && props_m == charprops[c]); } } -ft_test ft_ctype_macros = { - .name = "Macro-based classification", - .function = _ctype_macros, -}; - -#undef isalnum -#undef isalpha -#undef isblank -#undef iscntrl -#undef isdigit -#undef islower -#undef isgraph -#undef ispunct -#undef isprint -#undef isspace -#undef isupper -#undef isxdigit -#undef tolower -#undef toupper - -static void _ctype_functions(ft_test *t) -{ - for(int c = 0; c < 128; c++) { - ft_assert(t, (isalnum(c) != 0) == ((charprops[c] & CP_ALNUM) != 0)); - ft_assert(t, (isalpha(c) != 0) == ((charprops[c] & CP_ALPHA) != 0)); - ft_assert(t, (isblank(c) != 0) == ((charprops[c] & CP_BLANK) != 0)); - ft_assert(t, (iscntrl(c) != 0) == ((charprops[c] & CP_CNTRL) != 0)); - ft_assert(t, (isdigit(c) != 0) == ((charprops[c] & CP_DIGIT) != 0)); - ft_assert(t, (islower(c) != 0) == ((charprops[c] & CP_LOWER) != 0)); - ft_assert(t, (isgraph(c) != 0) == ((charprops[c] & CP_GRAPH) != 0)); - ft_assert(t, (ispunct(c) != 0) == ((charprops[c] & CP_PUNCT) != 0)); - ft_assert(t, (isprint(c) != 0) == ((charprops[c] & CP_PRINT) != 0)); - ft_assert(t, (isspace(c) != 0) == ((charprops[c] & CP_SPACE) != 0)); - ft_assert(t, (isupper(c) != 0) == ((charprops[c] & CP_UPPER) != 0)); - ft_assert(t, (isxdigit(c) != 0) == ((charprops[c] & CP_XDIGIT) != 0)); - } -} - -ft_test ft_ctype_functions = { - .name = "Function-based classification", - .function = _ctype_functions, +ft_test ft_ctype_classes = { + .name = "Character classification", + .function = _ctype_classes, }; diff --git a/src/ctype/convert.c b/src/ctype/convert.c index c612ae0..2310dfe 100644 --- a/src/ctype/convert.c +++ b/src/ctype/convert.c @@ -2,37 +2,24 @@ #include #include -static void _ctype_convert_macros(ft_test *t) +static void _ctype_conversion(ft_test *t) { for(int c = 0; c < 128; c++) { - int cl = (isupper(c) ? c - 'A' + 'a' : c); - int cu = (islower(c) ? c - 'a' + 'A' : c); + int cl_f = (isupper(c) ? c - 'A' + 'a' : c); + int cu_f = (islower(c) ? c - 'a' + 'A' : c); - ft_assert(t, tolower(c) == cl); - ft_assert(t, toupper(c) == cu); + #undef tolower + #undef toupper + + int cl_m = (isupper(c) ? c - 'A' + 'a' : c); + int cu_m = (islower(c) ? c - 'a' + 'A' : c); + + ft_assert(t, tolower(c) == cl_f && toupper(c) == cu_f + && tolower(c) == cl_m && toupper(c) == cu_m); } } -ft_test ft_ctype_convert_macros = { - .name = "Macro-based conversion", - .function = _ctype_convert_macros, -}; - -#undef tolower -#undef toupper - -static void _ctype_convert_functions(ft_test *t) -{ - for(int c = 0; c < 128; c++) { - int cl = (isupper(c) ? c - 'A' + 'a' : c); - int cu = (islower(c) ? c - 'a' + 'A' : c); - - ft_assert(t, tolower(c) == cl); - ft_assert(t, toupper(c) == cu); - } -} - -ft_test ft_ctype_convert_functions = { - .name = "Function-based conversion", - .function = _ctype_convert_functions, +ft_test ft_ctype_conversion = { + .name = "Case conversion", + .function = _ctype_conversion, }; diff --git a/src/main.c b/src/main.c index 82510af..6c345cc 100644 --- a/src/main.c +++ b/src/main.c @@ -14,10 +14,8 @@ ft_list headers_libc[] = { { "", (ft_test*[]){ - &ft_ctype_macros, - &ft_ctype_functions, - &ft_ctype_convert_macros, - &ft_ctype_convert_functions, + &ft_ctype_classes, + &ft_ctype_conversion, NULL, }}, { "", (ft_test*[]){