From 3e746c51b3adcf97e4d11be30bfd91c8aaf2c2ba Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sat, 18 Aug 2018 23:40:11 +0200 Subject: Fix and run tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- .gitignore | 7 +++++++ Makefile | 39 +++++++++++++++++++++++++++++++---- TODO | 2 ++ asprintf.c | 5 +++-- config.mk | 4 ++-- isutf8.c | 41 +++++++++++++++++++++++++++++++------ libsimple.h | 2 +- memdup.c | 5 +++-- memends.c | 5 +++-- memmem.c | 5 +++-- memrchr.c | 5 +++-- memrmem.c | 7 ++++--- memstarts.c | 7 ++++--- rawmemchr.c | 5 +++-- rawmemrchr.c | 5 +++-- strcaseends.c | 5 +++-- strcasestr.c | 14 ++++++------- strchrnul.c | 5 +++-- strends.c | 5 +++-- strndup.c | 5 +++-- strrcasestr.c | 14 ++++++++----- strrstr.c | 65 ++++++++++++++++++++++++++++++----------------------------- strstarts.c | 5 +++-- test.h | 19 +++++++++++++++++ vasprintf.c | 5 +++-- 25 files changed, 197 insertions(+), 89 deletions(-) create mode 100644 test.h diff --git a/.gitignore b/.gitignore index 9d7e582..9a3192c 100644 --- a/.gitignore +++ b/.gitignore @@ -3,5 +3,12 @@ *.o *.a *.so +*.so.* *.su *.out +*.test +*.gch +*.gcda +*.gcno +*.gcov +*.lo diff --git a/Makefile b/Makefile index 68e5b97..591d62a 100644 --- a/Makefile +++ b/Makefile @@ -54,17 +54,48 @@ OBJ =\ vweprintf.o\ libsimple.o -all: libsimple.a +TESTS =\ + asprintf.test\ + isutf8.test\ + memdup.test\ + memends.test\ + memmem.test\ + memrchr.test\ + memrmem.test\ + memstarts.test\ + rawmemchr.test\ + rawmemrchr.test\ + strcaseends.test\ + strcasestr.test\ + strends.test\ + strndup.test\ + strrcasestr.test\ + strrstr.test\ + strstarts.test\ + vasprintf.test + +all: libsimple.a $(TESTS) $(OBJ): $(@:.o=.c) libsimple.h +$(TESTS): $(@:=.o) libsimple.a +$(TESTS:=.o): $(@:.test.o=.c) libsimple.h test.h libsimple.a: $(OBJ) $(AR) rc $@ $? $(AR) -s $@ +.test.o.test: + $(CC) -o $@ $< libsimple.a $(LDFLAGS) + +.c.test.o: + $(CC) -c -o $@ $< $(CFLAGS) -DTEST + +check: $(TESTS) + @set -e; for t in $(TESTS); do printf '%s\n' "./$$t"; "./$$t"; done + clean: - -rm -rf -- *.o *.su *.a *.so *.so.* *.gch *.gcda *.gcno *.gcov *.lo + -rm -rf -- *.o *.su *.a *.so *.so.* *.gch *.gcda *.gcno *.gcov *.lo *.test .SUFFIXES: -.SUFFIXES: .o .c +.SUFFIXES: .test .test.o .o .c -.PHONY: all clean +.PHONY: all check clean diff --git a/TODO b/TODO index 2b24a41..f0a33fb 100644 --- a/TODO +++ b/TODO @@ -2,3 +2,5 @@ strnstr strrnstr strncasestr strrncasestr +strcmpnul +strcasecmpnul diff --git a/asprintf.c b/asprintf.c index e57c320..78e00ca 100644 --- a/asprintf.c +++ b/asprintf.c @@ -1,5 +1,6 @@ /* See LICENSE file for copyright and license details. */ #include "libsimple.h" +#ifndef TEST int @@ -14,8 +15,8 @@ libsimple_asprintf(char **strp, const char *fmt, ...) } -#ifdef TEST -#include +#else +#include "test.h" int main(void) diff --git a/config.mk b/config.mk index 2751186..67b8e41 100644 --- a/config.mk +++ b/config.mk @@ -1,3 +1,3 @@ CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700 -CFLAGS = -std=c99 -Wall -Wextra -O2 $(CPPFLAGS) -LDFLAGS = -s +CFLAGS = -std=c99 -Wall -Wextra -g $(CPPFLAGS) +LDFLAGS = diff --git a/isutf8.c b/isutf8.c index 557aafb..232be5d 100644 --- a/isutf8.c +++ b/isutf8.c @@ -1,11 +1,12 @@ /* See LICENSE file for copyright and license details. */ #include "libsimple.h" +#ifndef TEST int libsimple_isutf8(const char *string, int allow_modified_nul) { - static long BYTES_TO_MIN_BITS[] = {0, 0, 8, 12, 17, 22, 37}; + static long BYTES_TO_MIN_BITS[] = {0, 0, 8, 12, 17, 22, 27}; static long BYTES_TO_MAX_BITS[] = {0, 7, 11, 16, 21, 26, 31}; long int bytes = 0, read_bytes = 0, bits = 0, c, character; @@ -35,7 +36,7 @@ libsimple_isutf8(const char *string, int allow_modified_nul) while ((c & 0x80)) bytes++, c <<= 1; read_bytes = 1; - character = c & 0x7F; + character = (c & 0xFF) >> bytes; if (bytes > 6) /* 31-bit characters can be encoded with 6-bytes, and UTF-8 does not cover higher code points. */ @@ -70,8 +71,8 @@ libsimple_isutf8(const char *string, int allow_modified_nul) } -#ifdef TEST -#include +#else +#include "test.h" int main(void) @@ -93,12 +94,40 @@ main(void) assert(libsimple_isutf8("\xC0", i) == 0); assert(libsimple_isutf8("\xC0\x80", i) == i); assert(libsimple_isutf8("\xC0\x81", i) == 0); + assert(libsimple_isutf8("\xCF", i) == 0); + assert(libsimple_isutf8("\xEF", i) == 0); + assert(libsimple_isutf8("\xEF\x8F", i) == 0); + assert(libsimple_isutf8("\xF7", i) == 0); + assert(libsimple_isutf8("\xF7\x8F", i) == 0); + assert(libsimple_isutf8("\xF7\x8F\x8F", i) == 0); + assert(libsimple_isutf8("\xFA", i) == 0); + assert(libsimple_isutf8("\xFA\x8F", i) == 0); + assert(libsimple_isutf8("\xFA\x8F\x8F", i) == 0); + assert(libsimple_isutf8("\xFA\x8F\x8F\x8F", i) == 0); + assert(libsimple_isutf8("\xFD", i) == 0); + assert(libsimple_isutf8("\xFD\x8F", i) == 0); + assert(libsimple_isutf8("\xFD\x8F\x8F", i) == 0); + assert(libsimple_isutf8("\xFD\x8F\x8F\x8F", i) == 0); + assert(libsimple_isutf8("\xFD\x8F\x8F\x8F\x8F", i) == 0); + assert(libsimple_isutf8("\xFE", i) == 0); + assert(libsimple_isutf8("\xFE\x8F", i) == 0); + assert(libsimple_isutf8("\xFE\x8F\x8F", i) == 0); + assert(libsimple_isutf8("\xFE\x8F\x8F\x8F", i) == 0); + assert(libsimple_isutf8("\xFE\x8F\x8F\x8F\x8F", i) == 0); + assert(libsimple_isutf8("\xFE\x8F\x8F\x8F\x8F\x8F", i) == 0); + assert(libsimple_isutf8("\xFF", i) == 0); + assert(libsimple_isutf8("\xFF\x8F", i) == 0); + assert(libsimple_isutf8("\xFF\x8F\x8F", i) == 0); + assert(libsimple_isutf8("\xFF\x8F\x8F\x8F", i) == 0); + assert(libsimple_isutf8("\xFF\x8F\x8F\x8F\x8F", i) == 0); + assert(libsimple_isutf8("\xFF\x8F\x8F\x8F\x8F\x8F", i) == 0); + assert(libsimple_isutf8("\xFF\x8F\x8F\x8F\x8F\x8F\x8F", i) == 0); assert(libsimple_isutf8("\xC1\x80", i) == 0); assert(libsimple_isutf8("\xC2\x80", i) == 1); - assert(libsimple_isutf8("\xE1\x80\x80\x80", i) == 1); + assert(libsimple_isutf8("\xE1\x80\x80\x80", i) == 0); assert(libsimple_isutf8("\xE1\x80\xC0\x80", i) == 0); assert(libsimple_isutf8("\xE1\x80\x00\x80", i) == 0); - assert(libsimple_isutf8("\xF1\x80\x80\x80", i) == 0); + assert(libsimple_isutf8("\xF1\x80\x80\x80", i) == 1); assert(libsimple_isutf8("\xFF\x80\x80\x80\x80\x80\x80\x80", i) == 0); } return 0; diff --git a/libsimple.h b/libsimple.h index 85f741f..9766404 100644 --- a/libsimple.h +++ b/libsimple.h @@ -709,7 +709,7 @@ int libsimple_strstarts(const char *, const char *); _LIBSIMPLE_GCC_ONLY(__attribute__((__pure__, __nonnull__, __warn_unused_result__))) -int libsimple_strcasestarts(const char *__s, const char *__t) { return !strncasecmp(__s, __t, strlen(__t)); } +static inline int libsimple_strcasestarts(const char *__s, const char *__t) { return !strncasecmp(__s, __t, strlen(__t)); } #ifndef strcasestarts # define strcasestarts libsimple_strcasestarts #endif diff --git a/memdup.c b/memdup.c index 882442b..ea85dc5 100644 --- a/memdup.c +++ b/memdup.c @@ -1,5 +1,6 @@ /* See LICENSE file for copyright and license details. */ #include "libsimple.h" +#ifndef TEST void * @@ -12,8 +13,8 @@ libsimple_memdup(const void *s, size_t n) } -#ifdef TEST -#include +#else +#include "test.h" int main(void) diff --git a/memends.c b/memends.c index a6fab30..f579222 100644 --- a/memends.c +++ b/memends.c @@ -1,5 +1,6 @@ /* See LICENSE file for copyright and license details. */ #include "libsimple.h" +#ifndef TEST int @@ -15,8 +16,8 @@ libsimple_memends(const void *s_, size_t n, const void *t_, size_t m) } -#ifdef TEST -#include +#else +#include "test.h" int main(void) diff --git a/memmem.c b/memmem.c index 0046e19..5affa16 100644 --- a/memmem.c +++ b/memmem.c @@ -1,5 +1,6 @@ /* See LICENSE file for copyright and license details. */ #include "libsimple.h" +#ifndef TEST void * @@ -23,8 +24,8 @@ libsimple_memmem(const void *hay_, size_t hayn, const void *sub_, size_t subn) } -#ifdef TEST -#include +#else +#include "test.h" int main(void) diff --git a/memrchr.c b/memrchr.c index 454ad4c..5028158 100644 --- a/memrchr.c +++ b/memrchr.c @@ -1,5 +1,6 @@ /* See LICENSE file for copyright and license details. */ #include "libsimple.h" +#ifndef TEST void * @@ -12,8 +13,8 @@ libsimple_memrchr(const void *s_, int c, size_t n_) } -#ifdef TEST -#include +#else +#include "test.h" int main(void) diff --git a/memrmem.c b/memrmem.c index 713ba34..2c61998 100644 --- a/memrmem.c +++ b/memrmem.c @@ -1,5 +1,6 @@ /* See LICENSE file for copyright and license details. */ #include "libsimple.h" +#ifndef TEST void * @@ -9,7 +10,7 @@ libsimple_memrmem(const void *hay_, size_t hayn, const void *sub_, size_t subn) const char *sub = sub_; if (!subn) - return hay; + return &hay[hayn]; if (hayn < subn) return NULL; if (subn == 1) @@ -26,8 +27,8 @@ libsimple_memrmem(const void *hay_, size_t hayn, const void *sub_, size_t subn) } -#ifdef TEST -#include +#else +#include "test.h" int main(void) diff --git a/memstarts.c b/memstarts.c index 05c8b39..ace2b70 100644 --- a/memstarts.c +++ b/memstarts.c @@ -1,5 +1,6 @@ /* See LICENSE file for copyright and license details. */ #include "libsimple.h" +#ifndef TEST int @@ -14,8 +15,8 @@ libsimple_memstarts(const void *s_, size_t n, const void *t_, size_t m) } -#ifdef TEST -#include +#else +#include "test.h" int main(void) @@ -32,7 +33,7 @@ main(void) assert(libsimple_memstarts("test", 4, "x", 1) == 0); assert(libsimple_memstarts("test", 4, "xx", 2) == 0); - assert(libsimple_memstarts("TEST", 4, "", 0) == 1; + assert(libsimple_memstarts("TEST", 4, "", 0) == 1); assert(libsimple_memstarts("TEST", 4, "test", 4) == 0); assert(libsimple_memstarts("TEST", 4, "test", 5) == 0); assert(libsimple_memstarts("TEST", 4, "testx", 5) == 0); diff --git a/rawmemchr.c b/rawmemchr.c index 57f584d..9bc02e0 100644 --- a/rawmemchr.c +++ b/rawmemchr.c @@ -1,5 +1,6 @@ /* See LICENSE file for copyright and license details. */ #include "libsimple.h" +#ifndef TEST void * @@ -11,8 +12,8 @@ libsimple_rawmemchr(const void *s_, int c) } -#ifdef TEST -#include +#else +#include "test.h" int main(void) diff --git a/rawmemrchr.c b/rawmemrchr.c index 3985840..eb685cf 100644 --- a/rawmemrchr.c +++ b/rawmemrchr.c @@ -1,5 +1,6 @@ /* See LICENSE file for copyright and license details. */ #include "libsimple.h" +#ifndef TEST void * @@ -11,8 +12,8 @@ libsimple_rawmemrchr(const void *s_, int c, size_t n) } -#ifdef TEST -#include +#else +#include "test.h" int main(void) diff --git a/strcaseends.c b/strcaseends.c index 6ff9d8b..adbebe4 100644 --- a/strcaseends.c +++ b/strcaseends.c @@ -1,5 +1,6 @@ /* See LICENSE file for copyright and license details. */ #include "libsimple.h" +#ifndef TEST int @@ -13,8 +14,8 @@ libsimple_strcaseends(const char *s, const char *t) } -#ifdef TEST -#include +#else +#include "test.h" int main(void) diff --git a/strcasestr.c b/strcasestr.c index a3a98fc..4ffbde2 100644 --- a/strcasestr.c +++ b/strcasestr.c @@ -1,24 +1,24 @@ /* See LICENSE file for copyright and license details. */ #include "libsimple.h" +#ifndef TEST char * libsimple_strcasestr(const char *h_, const char *n) { char *h = *(char **)(void *)&h_; - size_t hn = strlen(h); size_t nn = strlen(n); - if (hn < nn) - return NULL; - for (hn -= nn; hn--; h++) - if (!strcasecmp(h, n)) + if (!nn) + return h; + for (; *h; h++) + if (!strncasecmp(h, n, nn)) return h; return NULL; } -#ifdef TEST -#include +#else +#include "test.h" int main(void) diff --git a/strchrnul.c b/strchrnul.c index 893ff34..2dccb7a 100644 --- a/strchrnul.c +++ b/strchrnul.c @@ -1,5 +1,6 @@ /* See LICENSE file for copyright and license details. */ #include "libsimple.h" +#ifndef TEST char * @@ -11,8 +12,8 @@ libsimple_strchrnul(const char *s_, int c) } -#ifdef TEST -#include +#else +#include "test.h" int main(void) diff --git a/strends.c b/strends.c index e905451..9934623 100644 --- a/strends.c +++ b/strends.c @@ -1,5 +1,6 @@ /* See LICENSE file for copyright and license details. */ #include "libsimple.h" +#ifndef TEST int @@ -9,8 +10,8 @@ libsimple_strends(const char *s, const char *t) } -#ifdef TEST -#include +#else +#include "test.h" int main(void) diff --git a/strndup.c b/strndup.c index cc828a5..e2f144f 100644 --- a/strndup.c +++ b/strndup.c @@ -1,5 +1,6 @@ /* See LICENSE file for copyright and license details. */ #include "libsimple.h" +#ifndef TEST char * @@ -20,8 +21,8 @@ libsimple_strndup(const char *s, size_t n) /* FIXME */ } -#ifdef TEST -#include +#else +#include "test.h" int main(void) diff --git a/strrcasestr.c b/strrcasestr.c index 9bd40b3..2b1f2ab 100644 --- a/strrcasestr.c +++ b/strrcasestr.c @@ -1,5 +1,6 @@ /* See LICENSE file for copyright and license details. */ #include "libsimple.h" +#ifndef TEST char * @@ -8,25 +9,28 @@ libsimple_strrcasestr(const char *h_, const char *n) char *h = *(char **)(void *)&h_; size_t hn = strlen(h); size_t nn = strlen(n); + if (!nn) + return &h[hn]; if (hn < nn) return NULL; for (h += hn -= nn; hn--; h--) - if (!strcasecmp(h, n)) + if (!strncasecmp(h, n, nn)) return h; + if (!strncasecmp(h, n, nn)) + return h; return NULL; } -#ifdef TEST -#include +#else +#include "test.h" int main(void) { assert(!strcmp(libsimple_strrcasestr("", ""), "")); assert(!strcmp(libsimple_strrcasestr("test", ""), "")); - assert(!libsimple_strrcasestr("", "")); - assert(!libsimple_strrcasestr("t", "t")); + assert(!strcmp(libsimple_strrcasestr("t", "t"), "t")); assert(!strcmp(libsimple_strrcasestr("test", "t"), "t")); assert(!strcmp(libsimple_strrcasestr("test", "e"), "est")); assert(!strcmp(libsimple_strrcasestr("test", "s"), "st")); diff --git a/strrstr.c b/strrstr.c index 5d64744..59e0ade 100644 --- a/strrstr.c +++ b/strrstr.c @@ -1,5 +1,6 @@ /* See LICENSE file for copyright and license details. */ #include "libsimple.h" +#ifndef TEST char * @@ -9,8 +10,8 @@ libsimple_strrstr(const char *hay, const char *sub) } -#ifdef TEST -#include +#else +#include "test.h" int main(void) @@ -33,37 +34,37 @@ main(void) assert(!strcmp(libsimple_strrstr("abc", "abc"), "abc")); assert(!strcmp(libsimple_strrstr("abcabc", "bc"), "bc")); - assert(!strcmp(libsimple_strrcasestr("TEST", "test"), "TEST")); - assert(!strcmp(libsimple_strrcasestr("TEST", ""), "")); - assert(!libsimple_strrcasestr("T", "test")); - assert(!strcmp(libsimple_strrcasestr("TEST", "t"), "T")); - assert(!strcmp(libsimple_strrcasestr("TEST", "e"), "EST")); - assert(!strcmp(libsimple_strrcasestr("TEST", "s"), "ST")); - assert(!libsimple_strrcasestr("TEST", "x")); - assert(!strcmp(libsimple_strrcasestr("TEST", "te"), "TEST")); - assert(!strcmp(libsimple_strrcasestr("TEST", "es"), "EST")); - assert(!strcmp(libsimple_strrcasestr("TEST", "st"), "ST")); - assert(!libsimple_strrcasestr("TEST", "xx")); - assert(!strcmp(libsimple_strrcasestr("ABC", "c"), "C")); - assert(!strcmp(libsimple_strrcasestr("ABC", "bc"), "BC")); - assert(!strcmp(libsimple_strrcasestr("ABC", "abc"), "ABC")); - assert(!strcmp(libsimple_strrcasestr("ABCABC", "bc"), "BC")); + assert(!libsimple_strrstr("TEST", "test")); + assert(!strcmp(libsimple_strrstr("TEST", ""), "")); + assert(!libsimple_strrstr("T", "test")); + assert(!libsimple_strrstr("TEST", "t")); + assert(!libsimple_strrstr("TEST", "e")); + assert(!libsimple_strrstr("TEST", "s")); + assert(!libsimple_strrstr("TEST", "x")); + assert(!libsimple_strrstr("TEST", "te")); + assert(!libsimple_strrstr("TEST", "es")); + assert(!libsimple_strrstr("TEST", "st")); + assert(!libsimple_strrstr("TEST", "xx")); + assert(!libsimple_strrstr("ABC", "c")); + assert(!libsimple_strrstr("ABC", "bc")); + assert(!libsimple_strrstr("ABC", "abc")); + assert(!libsimple_strrstr("ABCABC", "bc")); - assert(!strcmp(libsimple_strrcasestr("test", "TEST"), "test")); - assert(!libsimple_strrcasestr("", "TEST")); - assert(!libsimple_strrcasestr("t", "TEST")); - assert(!strcmp(libsimple_strrcasestr("test", "T"), "t")); - assert(!strcmp(libsimple_strrcasestr("test", "E"), "est")); - assert(!strcmp(libsimple_strrcasestr("test", "S"), "st")); - assert(!libsimple_strrcasestr("test", "X")); - assert(!strcmp(libsimple_strrcasestr("test", "TE"), "test")); - assert(!strcmp(libsimple_strrcasestr("test", "ES"), "est")); - assert(!strcmp(libsimple_strrcasestr("test", "ST"), "st")); - assert(!libsimple_strrcasestr("test", "XX")); - assert(!strcmp(libsimple_strrcasestr("abc", "C"), "c")); - assert(!strcmp(libsimple_strrcasestr("abc", "BC"), "bc")); - assert(!strcmp(libsimple_strrcasestr("abc", "ABC"), "abc")); - assert(!strcmp(libsimple_strrcasestr("abcabc", "BC"), "bc")); + assert(!libsimple_strrstr("test", "TEST")); + assert(!libsimple_strrstr("", "TEST")); + assert(!libsimple_strrstr("t", "TEST")); + assert(!libsimple_strrstr("test", "T")); + assert(!libsimple_strrstr("test", "E")); + assert(!libsimple_strrstr("test", "S")); + assert(!libsimple_strrstr("test", "X")); + assert(!libsimple_strrstr("test", "TE")); + assert(!libsimple_strrstr("test", "ES")); + assert(!libsimple_strrstr("test", "ST")); + assert(!libsimple_strrstr("test", "XX")); + assert(!libsimple_strrstr("abc", "C")); + assert(!libsimple_strrstr("abc", "BC")); + assert(!libsimple_strrstr("abc", "ABC")); + assert(!libsimple_strrstr("abcabc", "BC")); return 0; } diff --git a/strstarts.c b/strstarts.c index f2db947..b476222 100644 --- a/strstarts.c +++ b/strstarts.c @@ -1,5 +1,6 @@ /* See LICENSE file for copyright and license details. */ #include "libsimple.h" +#ifndef TEST int @@ -10,8 +11,8 @@ libsimple_strstarts(const char *s, const char *t) } -#ifdef TEST -#include +#else +#include "test.h" int main(void) diff --git a/test.h b/test.h new file mode 100644 index 0000000..2a1c656 --- /dev/null +++ b/test.h @@ -0,0 +1,19 @@ +/* See LICENSE file for copyright and license details. */ + + +#define assert(EXPR)\ + do {\ + if (EXPR)\ + break;\ + fprintf(stderr, "Failed at %s:%i: %s\n", __FILE__, __LINE__, #EXPR);\ + exit(1);\ + } while (0) + + + +#define strcmp(...) test_strcmp(__VA_ARGS__) +static inline int +test_strcmp(const char *a, const char *b) +{ + return (!a || !b) ? !b - !a : (strcmp)(a, b); +} diff --git a/vasprintf.c b/vasprintf.c index 9ce1998..fd14650 100644 --- a/vasprintf.c +++ b/vasprintf.c @@ -1,5 +1,6 @@ /* See LICENSE file for copyright and license details. */ #include "libsimple.h" +#ifndef TEST int @@ -26,8 +27,8 @@ fail: } -#ifdef TEST -#include +#else +#include "test.h" int main(void) -- cgit v1.2.3-70-g09d2