diff options
-rw-r--r-- | .gitignore | 7 | ||||
-rw-r--r-- | Makefile | 39 | ||||
-rw-r--r-- | TODO | 2 | ||||
-rw-r--r-- | asprintf.c | 5 | ||||
-rw-r--r-- | config.mk | 4 | ||||
-rw-r--r-- | isutf8.c | 41 | ||||
-rw-r--r-- | libsimple.h | 2 | ||||
-rw-r--r-- | memdup.c | 5 | ||||
-rw-r--r-- | memends.c | 5 | ||||
-rw-r--r-- | memmem.c | 5 | ||||
-rw-r--r-- | memrchr.c | 5 | ||||
-rw-r--r-- | memrmem.c | 7 | ||||
-rw-r--r-- | memstarts.c | 7 | ||||
-rw-r--r-- | rawmemchr.c | 5 | ||||
-rw-r--r-- | rawmemrchr.c | 5 | ||||
-rw-r--r-- | strcaseends.c | 5 | ||||
-rw-r--r-- | strcasestr.c | 14 | ||||
-rw-r--r-- | strchrnul.c | 5 | ||||
-rw-r--r-- | strends.c | 5 | ||||
-rw-r--r-- | strndup.c | 5 | ||||
-rw-r--r-- | strrcasestr.c | 14 | ||||
-rw-r--r-- | strrstr.c | 65 | ||||
-rw-r--r-- | strstarts.c | 5 | ||||
-rw-r--r-- | test.h | 19 | ||||
-rw-r--r-- | vasprintf.c | 5 |
25 files changed, 197 insertions, 89 deletions
@@ -3,5 +3,12 @@ *.o *.a *.so +*.so.* *.su *.out +*.test +*.gch +*.gcda +*.gcno +*.gcov +*.lo @@ -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 @@ -2,3 +2,5 @@ strnstr strrnstr strncasestr strrncasestr +strcmpnul +strcasecmpnul @@ -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 <assert.h> +#else +#include "test.h" int main(void) @@ -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 = @@ -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 <assert.h> +#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 @@ -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 <assert.h> +#else +#include "test.h" int main(void) @@ -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 <assert.h> +#else +#include "test.h" int main(void) @@ -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 <assert.h> +#else +#include "test.h" int main(void) @@ -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 <assert.h> +#else +#include "test.h" int main(void) @@ -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 <assert.h> +#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 <assert.h> +#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 <assert.h> +#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 <assert.h> +#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 <assert.h> +#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 <assert.h> +#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 <assert.h> +#else +#include "test.h" int main(void) @@ -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 <assert.h> +#else +#include "test.h" int main(void) @@ -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 <assert.h> +#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 <assert.h> +#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")); @@ -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 <assert.h> +#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 <assert.h> +#else +#include "test.h" int main(void) @@ -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 <assert.h> +#else +#include "test.h" int main(void) |