Revert to out-of-line (smaller) version of ctype. Now with unit test!
authorDavid Given <dg@cowlark.com>
Sat, 21 Mar 2015 20:25:00 +0000 (21:25 +0100)
committerDavid Given <dg@cowlark.com>
Sat, 21 Mar 2015 20:25:00 +0000 (21:25 +0100)
Which passes!

--HG--
rename : Library/libs/isspace.c => Library/libs/isblank.c
rename : Library/libs/isspace.c => Library/libs/isprint.c

19 files changed:
Library/libs/Makefile
Library/libs/Makefile.6502
Library/libs/isalnum.c
Library/libs/isalpha.c
Library/libs/isascii.c
Library/libs/isblank.c [new file with mode: 0644]
Library/libs/iscntrl.c
Library/libs/isdigit.c
Library/libs/isgraph.c
Library/libs/islower.c
Library/libs/isoctal.c [deleted file]
Library/libs/isprint.c [new file with mode: 0644]
Library/libs/ispunct.c
Library/libs/isspace.c
Library/libs/isupper.c
Library/libs/isxdigit.c
Library/libs/tolower.c
Library/libs/toupper.c
Library/tests/ctype.c [new file with mode: 0644]

index c49fc2a..c071c31 100644 (file)
@@ -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
index ceb5c91..b75bbc7 100644 (file)
@@ -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
index 511038d..adcd2f2 100644 (file)
@@ -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 <stdint.h>
 #include <ctype.h>
+#endif
 
 int isalnum(int c)
 {
index 07208ce..bdba077 100644 (file)
@@ -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 <stdint.h>
 #include <ctype.h>
+#endif
 
 int isalpha(int c)
 {
index 2bc3805..d91fbfa 100644 (file)
@@ -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 <stdint.h>
 #include <ctype.h>
+#endif
 
 int isascii(int c)
 {
diff --git a/Library/libs/isblank.c b/Library/libs/isblank.c
new file mode 100644 (file)
index 0000000..7def9bc
--- /dev/null
@@ -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 <stdint.h>
+#include <ctype.h>
+#include <string.h>
+#endif
+
+int isblank(int c)
+{
+       uint8_t cb = c;
+       return (cb == ' ') || (c == '\t');
+}
+
index a0f7d30..2987ec2 100644 (file)
@@ -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 <stdint.h>
 #include <ctype.h>
+#endif
 
 int iscntrl(int c)
 {
index 669bb28..57f4e1a 100644 (file)
@@ -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 <stdint.h>
 #include <ctype.h>
+#endif
 
 int isdigit(int c)
 {
index fdc776e..f95e942 100644 (file)
@@ -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 <stdint.h>
 #include <ctype.h>
+#endif
 
 int isgraph(int c)
 {
-       return !iscntrl(c) && !isspace(c);
+       uint8_t cb = c;
+       return (c >= 33) && (c <= 126);
 }
 
index ba78d10..db7a5f2 100644 (file)
@@ -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 <stdint.h>
 #include <ctype.h>
+#endif
 
 int islower(int c)
 {
diff --git a/Library/libs/isoctal.c b/Library/libs/isoctal.c
deleted file mode 100644 (file)
index a1a8622..0000000
+++ /dev/null
@@ -1,9 +0,0 @@
-#include <stdint.h>
-#include <ctype.h>
-
-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 (file)
index 0000000..d3f6ce3
--- /dev/null
@@ -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 <stdint.h>
+#include <ctype.h>
+#include <string.h>
+#endif
+
+int isprint(int c)
+{
+       uint8_t cb = c;
+       return (cb >= 32) && (cb <= 126);
+}
+
index 2c88fa4..71fb2d0 100644 (file)
@@ -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 <stdint.h>
 #include <ctype.h>
+#endif
 
 int ispunct(int c)
 {
-       return !iscntrl(c) && !isalpha(c) && !isspace(c);
+       return isascii(c) && !iscntrl(c) && !isalnum(c) && !isspace(c);
 }
 
index 8cadeb5..f072760 100644 (file)
@@ -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 <stdint.h>
 #include <ctype.h>
 #include <string.h>
+#endif
 
 int isspace(int c)
 {
-       return !!strchr(" \t\n\r\f\v", c);
+       return c && !!strchr(" \t\n\r\f\v", c);
 }
 
index 0dad20d..5368cad 100644 (file)
@@ -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 <stdint.h>
 #include <ctype.h>
+#endif
 
 int isupper(int c)
 {
index 483e64b..94be6d0 100644 (file)
@@ -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 <stdint.h>
 #include <ctype.h>
+#endif
 
 int isxdigit(int c)
 {
index 57a2bc7..9824af5 100644 (file)
@@ -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 <ctype.h>
+#endif
 
 int tolower(int c)
 {
index 2617a22..44badc0 100644 (file)
@@ -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 <ctype.h>
+#endif
 
 int toupper(int c)
 {
diff --git a/Library/tests/ctype.c b/Library/tests/ctype.c
new file mode 100644 (file)
index 0000000..768ea94
--- /dev/null
@@ -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 <stdlib.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <stdbool.h>
+#include <string.h>
+#include <ctype.h>
+
+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;
+}
+