From 1644367743a7e7df4efa3ac9127776e7eac177bd Mon Sep 17 00:00:00 2001 From: leitner Date: Mon, 12 Feb 2024 17:18:44 +0000 Subject: [PATCH] add unit tests, make code more obvious --- str/str_chr.c | 18 ++++++++++-------- str/str_diffn.c | 13 +++++++++++++ str/str_len.c | 10 ++++++++++ str/str_rchr.c | 16 ++++++++++++++++ str/str_start.c | 20 ++++++++++++++++++++ 5 files changed, 69 insertions(+), 8 deletions(-) diff --git a/str/str_chr.c b/str/str_chr.c index 418dd57..048deba 100644 --- a/str/str_chr.c +++ b/str/str_chr.c @@ -1,19 +1,21 @@ #include "str.h" size_t str_chr(const char *in, char needle) { +#if 0 register const char* t=in; register const char c=needle; for (;;) { - if (!*t || *t==c) break; - ++t; - if (!*t || *t==c) break; - ++t; - if (!*t || *t==c) break; - ++t; - if (!*t || *t==c) break; - ++t; + if (!*t || *t==c) break; else ++t; + if (!*t || *t==c) break; else ++t; + if (!*t || *t==c) break; else ++t; + if (!*t || *t==c) break; else ++t; } return (size_t)(t-in); +#else + size_t i; + for (i=0; in[i] && in[i]!=needle; ++i) ; + return i; +#endif } #ifdef UNITTEST diff --git a/str/str_diffn.c b/str/str_diffn.c index 055d432..7dced17 100644 --- a/str/str_diffn.c +++ b/str/str_diffn.c @@ -23,3 +23,16 @@ int str_diffn(const char* a, const char* b, size_t limit) { } return j; } + +#ifdef UNITTEST +#include +#include + +int main() { + assert(str_diffn("foo","foo",3)==0); + assert(str_diffn("foo","fob",3)==('o'-'b')); + assert(str_diffn("foo","foox",4)==(-'x')); + assert(str_diffn("foox","foo",4)=='x'); + assert(str_diffn("foo","fob",2)==0); +} +#endif diff --git a/str/str_len.c b/str/str_len.c index 2fe759e..9621518 100644 --- a/str/str_len.c +++ b/str/str_len.c @@ -15,3 +15,13 @@ size_t str_len(const char* in) { } return (size_t)(t-in); } + +#ifdef UNITTEST +#include +int main() { + assert(str_len("foo")==3); + assert(str_len("")==0); + assert(str_len("fnord")==5); + return 0; +} +#endif diff --git a/str/str_rchr.c b/str/str_rchr.c index 595001a..b774e67 100644 --- a/str/str_rchr.c +++ b/str/str_rchr.c @@ -1,6 +1,11 @@ #include "str.h" size_t str_rchr(const char *in, char needle) { +#if 1 + size_t i,j=-1; + for (i=0; in[i]; ++i) if (in[i]==needle) j=i; + return i +int main() { + assert(str_rchr("fnord",'r')==3); + assert(str_rchr("frord",'r')==3); + assert(str_rchr("fnord",'x')==5); + return 0; +} +#endif diff --git a/str/str_start.c b/str/str_start.c index 5cff770..43639dc 100644 --- a/str/str_start.c +++ b/str/str_start.c @@ -2,6 +2,14 @@ /* str_start returns 1 if the b is a prefix of a, 0 otherwise */ int str_start(const char* a, const char* b) { +#if 1 + size_t i; + for (i=0; ; ++i) { + if (!b[i]) return 1; + if (a[i]!=b[i]) break; + } + return 0; +#else register const char* s=a; register const char* t=b; for (;;) { @@ -19,4 +27,16 @@ int str_start(const char* a, const char* b) { ++s; ++t; } return 0; +#endif +} + +#ifdef UNITTEST +#include +int main() { + assert(str_start("fnord","no")==0); + assert(str_start("fnord","fno")==1); + assert(str_start("fnord","fnord")==1); + assert(str_start("fnord","fnord1")==0); + return 0; } +#endif