From 0514e87d523b8b2d7ed67ecb4b59d9300217077c Mon Sep 17 00:00:00 2001 From: David Given Date: Sat, 21 Mar 2015 21:25:00 +0100 Subject: [PATCH] Revert to out-of-line (smaller) version of ctype. Now with unit test! Which passes! --HG-- rename : Library/libs/isspace.c => Library/libs/isblank.c rename : Library/libs/isspace.c => Library/libs/isprint.c --- Library/libs/Makefile | 6 +- Library/libs/Makefile.6502 | 6 +- Library/libs/isalnum.c | 5 ++ Library/libs/isalpha.c | 5 ++ Library/libs/isascii.c | 5 ++ Library/libs/isblank.c | 15 ++++ Library/libs/iscntrl.c | 5 ++ Library/libs/isdigit.c | 5 ++ Library/libs/isgraph.c | 8 +- Library/libs/islower.c | 5 ++ Library/libs/isoctal.c | 9 --- Library/libs/isprint.c | 15 ++++ Library/libs/ispunct.c | 7 +- Library/libs/isspace.c | 7 +- Library/libs/isupper.c | 5 ++ Library/libs/isxdigit.c | 5 ++ Library/libs/tolower.c | 5 ++ Library/libs/toupper.c | 5 ++ Library/tests/ctype.c | 145 +++++++++++++++++++++++++++++++++++++ 19 files changed, 250 insertions(+), 18 deletions(-) create mode 100644 Library/libs/isblank.c delete mode 100644 Library/libs/isoctal.c create mode 100644 Library/libs/isprint.c create mode 100644 Library/tests/ctype.c diff --git a/Library/libs/Makefile b/Library/libs/Makefile index c49fc2a2..c071c317 100644 --- a/Library/libs/Makefile +++ b/Library/libs/Makefile @@ -42,9 +42,9 @@ SRC_C += tzset.c ungetc.c utent.c utimes.c utsname.c SRC_C += vfprintf.c vprintf.c wait.c xitoa.c pathconf.c SRC_C += gethostname.c sysconf.c confstr.c memccpy.c getpass.c # ctype -SRC_C += toupper.c tolower.c toascii.c isascii.c isalnum.c isalpha.c -SRC_C += iscntrl.c isdigit.c isgraph.c islower.c ispunct.c isspace.c -SRC_C += isupper.c isxdigit.c isoctal.c +SRC_C += isalnum.c isalpha.c isascii.c isblank.c iscntrl.c isdigit.c +SRC_C += isgraph.c islower.c isprint.c ispunct.c isspace.c isupper.c +SRC_C += isxdigit.c # tty layer SRC_C += tcgetattr.c tcsetattr.c tcdrain.c tcflow.c tcflush.c SRC_C += cfmakeraw.c cfspeed.c revoke.c diff --git a/Library/libs/Makefile.6502 b/Library/libs/Makefile.6502 index ceb5c91e..b75bbc70 100644 --- a/Library/libs/Makefile.6502 +++ b/Library/libs/Makefile.6502 @@ -57,9 +57,9 @@ SRC_C += vfprintf.c vprintf.c wait.c xitoa.c pathconf.c SRC_C += gethostname.c sysconf.c confstr.c memccpy.c getpass.c SRC_C += strtol.c # ctype -SRC_C += toupper.c tolower.c toascii.c isascii.c isalnum.c isalpha.c -SRC_C += iscntrl.c isdigit.c isgraph.c islower.c ispunct.c isspace.c -SRC_C += isupper.c isxdigit.c isoctal.c +SRC_C += isalnum.c isalpha.c isascii.c isblank.c iscntrl.c isdigit.c +SRC_C += isgraph.c islower.c isprint.c ispunct.c isspace.c isupper.c +SRC_C += isxdigit.c # tty layer SRC_C += tcgetattr.c tcsetattr.c tcdrain.c tcflow.c tcflush.c SRC_C += cfmakeraw.c cfspeed.c revoke.c diff --git a/Library/libs/isalnum.c b/Library/libs/isalnum.c index 511038d7..adcd2f2e 100644 --- a/Library/libs/isalnum.c +++ b/Library/libs/isalnum.c @@ -1,5 +1,10 @@ +/* This file has a unit test in Library/test/ctype.c. If you change this file, + * please make sure the test still runs. */ + +#if !defined __TESTING__ #include #include +#endif int isalnum(int c) { diff --git a/Library/libs/isalpha.c b/Library/libs/isalpha.c index 07208ced..bdba0776 100644 --- a/Library/libs/isalpha.c +++ b/Library/libs/isalpha.c @@ -1,5 +1,10 @@ +/* This file has a unit test in Library/test/ctype.c. If you change this file, + * please make sure the test still runs. */ + +#if !defined __TESTING__ #include #include +#endif int isalpha(int c) { diff --git a/Library/libs/isascii.c b/Library/libs/isascii.c index 2bc38054..d91fbfa0 100644 --- a/Library/libs/isascii.c +++ b/Library/libs/isascii.c @@ -1,5 +1,10 @@ +/* This file has a unit test in Library/test/ctype.c. If you change this file, + * please make sure the test still runs. */ + +#if !defined __TESTING__ #include #include +#endif int isascii(int c) { diff --git a/Library/libs/isblank.c b/Library/libs/isblank.c new file mode 100644 index 00000000..7def9bca --- /dev/null +++ b/Library/libs/isblank.c @@ -0,0 +1,15 @@ +/* This file has a unit test in Library/test/ctype.c. If you change this file, + * please make sure the test still runs. */ + +#if !defined __TESTING__ +#include +#include +#include +#endif + +int isblank(int c) +{ + uint8_t cb = c; + return (cb == ' ') || (c == '\t'); +} + diff --git a/Library/libs/iscntrl.c b/Library/libs/iscntrl.c index a0f7d303..2987ec2f 100644 --- a/Library/libs/iscntrl.c +++ b/Library/libs/iscntrl.c @@ -1,5 +1,10 @@ +/* This file has a unit test in Library/test/ctype.c. If you change this file, + * please make sure the test still runs. */ + +#if !defined __TESTING__ #include #include +#endif int iscntrl(int c) { diff --git a/Library/libs/isdigit.c b/Library/libs/isdigit.c index 669bb28b..57f4e1a9 100644 --- a/Library/libs/isdigit.c +++ b/Library/libs/isdigit.c @@ -1,5 +1,10 @@ +/* This file has a unit test in Library/test/ctype.c. If you change this file, + * please make sure the test still runs. */ + +#if !defined __TESTING__ #include #include +#endif int isdigit(int c) { diff --git a/Library/libs/isgraph.c b/Library/libs/isgraph.c index fdc776e2..f95e9420 100644 --- a/Library/libs/isgraph.c +++ b/Library/libs/isgraph.c @@ -1,8 +1,14 @@ +/* This file has a unit test in Library/test/ctype.c. If you change this file, + * please make sure the test still runs. */ + +#if !defined __TESTING__ #include #include +#endif int isgraph(int c) { - return !iscntrl(c) && !isspace(c); + uint8_t cb = c; + return (c >= 33) && (c <= 126); } diff --git a/Library/libs/islower.c b/Library/libs/islower.c index ba78d10d..db7a5f2f 100644 --- a/Library/libs/islower.c +++ b/Library/libs/islower.c @@ -1,5 +1,10 @@ +/* This file has a unit test in Library/test/ctype.c. If you change this file, + * please make sure the test still runs. */ + +#if !defined __TESTING__ #include #include +#endif int islower(int c) { diff --git a/Library/libs/isoctal.c b/Library/libs/isoctal.c deleted file mode 100644 index a1a86225..00000000 --- a/Library/libs/isoctal.c +++ /dev/null @@ -1,9 +0,0 @@ -#include -#include - -int isoctal(int c) -{ - uint8_t bc = c; - return (bc >= '0') && (bc <= '7'); -} - diff --git a/Library/libs/isprint.c b/Library/libs/isprint.c new file mode 100644 index 00000000..d3f6ce38 --- /dev/null +++ b/Library/libs/isprint.c @@ -0,0 +1,15 @@ +/* This file has a unit test in Library/test/ctype.c. If you change this file, + * please make sure the test still runs. */ + +#if !defined __TESTING__ +#include +#include +#include +#endif + +int isprint(int c) +{ + uint8_t cb = c; + return (cb >= 32) && (cb <= 126); +} + diff --git a/Library/libs/ispunct.c b/Library/libs/ispunct.c index 2c88fa49..71fb2d0a 100644 --- a/Library/libs/ispunct.c +++ b/Library/libs/ispunct.c @@ -1,8 +1,13 @@ +/* This file has a unit test in Library/test/ctype.c. If you change this file, + * please make sure the test still runs. */ + +#if !defined __TESTING__ #include #include +#endif int ispunct(int c) { - return !iscntrl(c) && !isalpha(c) && !isspace(c); + return isascii(c) && !iscntrl(c) && !isalnum(c) && !isspace(c); } diff --git a/Library/libs/isspace.c b/Library/libs/isspace.c index 8cadeb5d..f0727600 100644 --- a/Library/libs/isspace.c +++ b/Library/libs/isspace.c @@ -1,9 +1,14 @@ +/* This file has a unit test in Library/test/ctype.c. If you change this file, + * please make sure the test still runs. */ + +#if !defined __TESTING__ #include #include #include +#endif int isspace(int c) { - return !!strchr(" \t\n\r\f\v", c); + return c && !!strchr(" \t\n\r\f\v", c); } diff --git a/Library/libs/isupper.c b/Library/libs/isupper.c index 0dad20dc..5368cad1 100644 --- a/Library/libs/isupper.c +++ b/Library/libs/isupper.c @@ -1,5 +1,10 @@ +/* This file has a unit test in Library/test/ctype.c. If you change this file, + * please make sure the test still runs. */ + +#if !defined __TESTING__ #include #include +#endif int isupper(int c) { diff --git a/Library/libs/isxdigit.c b/Library/libs/isxdigit.c index 483e64b8..94be6d03 100644 --- a/Library/libs/isxdigit.c +++ b/Library/libs/isxdigit.c @@ -1,5 +1,10 @@ +/* This file has a unit test in Library/test/ctype.c. If you change this file, + * please make sure the test still runs. */ + +#if !defined __TESTING__ #include #include +#endif int isxdigit(int c) { diff --git a/Library/libs/tolower.c b/Library/libs/tolower.c index 57a2bc7e..9824af50 100644 --- a/Library/libs/tolower.c +++ b/Library/libs/tolower.c @@ -1,4 +1,9 @@ +/* This file has a unit test in Library/test/ctype.c. If you change this file, + * please make sure the test still runs. */ + +#if !defined __TESTING__ #include +#endif int tolower(int c) { diff --git a/Library/libs/toupper.c b/Library/libs/toupper.c index 2617a225..44badc0e 100644 --- a/Library/libs/toupper.c +++ b/Library/libs/toupper.c @@ -1,4 +1,9 @@ +/* This file has a unit test in Library/test/ctype.c. If you change this file, + * please make sure the test still runs. */ + +#if !defined __TESTING__ #include +#endif int toupper(int c) { diff --git a/Library/tests/ctype.c b/Library/tests/ctype.c new file mode 100644 index 00000000..768ea945 --- /dev/null +++ b/Library/tests/ctype.c @@ -0,0 +1,145 @@ +/* Test for the Fuzix libc ctype library. Compares Fuzix's implementation + * with the system standard. + * + * Compile this with: + * + * gcc Library/tests/ctype.c -o Library/tests/ctype + * + * ...and run them. They'll moan if there's anything wrong. Note that this + * only tests the inline versions. + */ + +#include +#include +#include +#include +#include +#include + +typedef int (*ctype_fn)(int c); + +/* Import the system ctype functions under new names. */ + +#define define_sys(n) static ctype_fn sys_##n = n +define_sys(isalnum); +define_sys(isalpha); +define_sys(isascii); +define_sys(isblank); +define_sys(iscntrl); +define_sys(isdigit); +define_sys(isgraph); +define_sys(islower); +define_sys(isprint); +define_sys(ispunct); +define_sys(isspace); +define_sys(isupper); +define_sys(isxdigit); +define_sys(tolower); +define_sys(toupper); + +/* Now import the Fuzix ones. */ + +#define __TESTING__ + +#undef isalnum +#define isalnum fuzix_isalnum +#include "../libs/isalnum.c" + +#undef isalpha +#define isalpha fuzix_isalpha +#include "../libs/isalpha.c" + +#undef isascii +#define isascii fuzix_isascii +#include "../libs/isascii.c" + +#undef isblank +#define isblank fuzix_isblank +#include "../libs/isblank.c" + +#undef iscntrl +#define iscntrl fuzix_iscntrl +#include "../libs/iscntrl.c" + +#undef isdigit +#define isdigit fuzix_isdigit +#include "../libs/isdigit.c" + +#undef isgraph +#define isgraph fuzix_isgraph +#include "../libs/isgraph.c" + +#undef islower +#define islower fuzix_islower +#include "../libs/islower.c" + +#undef isprint +#define isprint fuzix_isprint +#include "../libs/isprint.c" + +#undef ispunct +#define ispunct fuzix_ispunct +#include "../libs/ispunct.c" + +#undef isspace +#define isspace fuzix_isspace +#include "../libs/isspace.c" + +#undef isupper +#define isupper fuzix_isupper +#include "../libs/isupper.c" + +#undef isxdigit +#define isxdigit fuzix_isxdigit +#include "../libs/isxdigit.c" + +#undef tolower +#define tolower fuzix_tolower +#include "../libs/tolower.c" + +#undef toupper +#define toupper fuzix_toupper +#include "../libs/toupper.c" + +static bool failed = false; + +static void test(int i) +{ + #define TEST(n) \ + if (!!sys_##n(i) != !!fuzix_##n(i)) \ + printf("FAIL sys_%s(%d)=%d; fuzix_%s(%d)=%d\n", \ + #n, i, sys_##n(i), #n, i, fuzix_##n(i)) + + TEST(isalnum); + TEST(isalpha); + TEST(isascii); + TEST(isblank); + TEST(iscntrl); + TEST(isdigit); + TEST(isgraph); + TEST(islower); + TEST(isprint); + TEST(ispunct); + TEST(isspace); + TEST(isupper); + TEST(isxdigit); + + if (sys_tolower(i) != fuzix_tolower(i)) + printf("FAIL sys_tolower(%d)=%d; fuzix_tolower(%d)=%d\n", + i, sys_tolower(i), i, fuzix_tolower(i)); + if (sys_toupper(i) != fuzix_toupper(i)) + printf("FAIL sys_toupper(%d)=%d; fuzix_toupper(%d)=%d\n", + i, sys_toupper(i), i, fuzix_toupper(i)); +} + +int main(int argc, const char* argv[]) +{ + int i; + + for (i=0; i<=255; i++) + test(i); + test(EOF); + + return failed ? EXIT_FAILURE : EXIT_SUCCESS; +} + -- 2.34.1