ctype: reduce number of assertions

This commit is contained in:
Lephenixnoir 2021-05-21 10:13:30 +02:00
parent b8c83ce9b4
commit 9b7c5ae70f
Signed by: Lephenixnoir
GPG Key ID: 1BBA026E13FC0495
4 changed files with 65 additions and 89 deletions

View File

@ -8,10 +8,8 @@
#include <ft/test.h>
/* 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;

View File

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

View File

@ -2,37 +2,24 @@
#include <ft/all-tests.h>
#include <ctype.h>
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,
};

View File

@ -14,10 +14,8 @@
ft_list headers_libc[] = {
{ "<ctype.h>", (ft_test*[]){
&ft_ctype_macros,
&ft_ctype_functions,
&ft_ctype_convert_macros,
&ft_ctype_convert_functions,
&ft_ctype_classes,
&ft_ctype_conversion,
NULL,
}},
{ "<inttypes.h>", (ft_test*[]){