diff options
-rw-r--r-- | Makefile | 5 | ||||
-rw-r--r-- | asprintf.c | 2 | ||||
-rw-r--r-- | libsimple.c | 257 | ||||
-rw-r--r-- | libsimple.h | 57 | ||||
-rw-r--r-- | memdup.c | 4 | ||||
-rw-r--r-- | memmem.c | 28 | ||||
-rw-r--r-- | memrchr.c | 4 | ||||
-rw-r--r-- | memrmem.c | 28 | ||||
-rw-r--r-- | rawmemchr.c | 4 | ||||
-rw-r--r-- | rawmemrchr.c | 4 | ||||
-rw-r--r-- | strcasestr.c | 66 | ||||
-rw-r--r-- | strchrnul.c | 6 | ||||
-rw-r--r-- | strndup.c | 14 | ||||
-rw-r--r-- | strrcasestr.c | 68 | ||||
-rw-r--r-- | strrstr.c | 28 | ||||
-rw-r--r-- | test.h | 2 |
16 files changed, 431 insertions, 146 deletions
@@ -76,7 +76,8 @@ TESTS =\ strrcasestr.test\ strrstr.test\ strstarts.test\ - vasprintf.test + vasprintf.test\ + libsimple.test all: libsimple.a $(TESTS) $(OBJ): $(@:.o=.c) libsimple.h @@ -94,7 +95,7 @@ libsimple.a: $(OBJ) $(CC) -c -o $@ $< $(CFLAGS) -DTEST check: $(TESTS) - @set -e; for t in $(TESTS); do printf '%s\n' "./$$t"; "./$$t"; done + @set -e; for t in $(TESTS); do printf '%s\n' "./$$t"; $(CHECK_PREFIX) "./$$t"; done clean: -rm -rf -- *.o *.su *.a *.so *.so.* *.gch *.gcda *.gcno *.gcov *.lo *.test @@ -26,7 +26,7 @@ main(void) assert(libsimple_asprintf(&s, "%i %X", 99999, 255) == sizeof("99999 FF") - 1); assert(s && s != old); - assert(!strcmp(s, "99999 FF")); + assert(!strcmpnul(s, "99999 FF")); free(s); return 0; diff --git a/libsimple.c b/libsimple.c index 7ada6af..36cc690 100644 --- a/libsimple.c +++ b/libsimple.c @@ -1,5 +1,262 @@ /* See LICENSE file for copyright and license details. */ #include "libsimple.h" +#ifndef TEST int libsimple_default_failure_exit = 1; + + +#else +#include "test.h" + +int +main(void) +{ + char buf[1024]; + + assert(MIN(1, 3) == 1); + assert(MIN(1, 1) == 1); + assert(MIN(3, 1) == 1); + assert(MIN(1, -3) == -3); + assert(MIN(1, -1) == -1); + assert(MIN(3, -1) == -1); + assert(MIN(-1, 3) == -1); + assert(MIN(-1, 1) == -1); + assert(MIN(-3, 1) == -3); + assert(MIN(-1, -3) == -3); + assert(MIN(-1, -1) == -1); + assert(MIN(-3, -1) == -3); + + assert(MAX(1, 3) == 3); + assert(MAX(1, 1) == 1); + assert(MAX(3, 1) == 3); + assert(MAX(1, -3) == 1); + assert(MAX(1, -1) == 1); + assert(MAX(3, -1) == 3); + assert(MAX(-1, 3) == 3); + assert(MAX(-1, 1) == 1); + assert(MAX(-3, 1) == 1); + assert(MAX(-1, -3) == -1); + assert(MAX(-1, -1) == -1); + assert(MAX(-3, -1) == -1); + + assert(MIN3(1, 2, 3) == 1); + assert(MIN3(1, 3, 2) == 1); + assert(MIN3(2, 1, 3) == 1); + assert(MIN3(2, 3, 1) == 1); + assert(MIN3(3, 1, 2) == 1); + assert(MIN3(3, 2, 1) == 1); + assert(MIN3(1, 2, -3) == -3); + assert(MIN3(1, 3, -2) == -2); + assert(MIN3(2, 1, -3) == -3); + assert(MIN3(2, 3, -1) == -1); + assert(MIN3(3, 1, -2) == -2); + assert(MIN3(3, 2, -1) == -1); + assert(MIN3(1, -2, 3) == -2); + assert(MIN3(1, -3, 2) == -3); + assert(MIN3(2, -1, 3) == -1); + assert(MIN3(2, -3, 1) == -3); + assert(MIN3(3, -1, 2) == -1); + assert(MIN3(3, -2, 1) == -2); + assert(MIN3(1, -2, -3) == -3); + assert(MIN3(1, -3, -2) == -3); + assert(MIN3(2, -1, -3) == -3); + assert(MIN3(2, -3, -1) == -3); + assert(MIN3(3, -1, -2) == -2); + assert(MIN3(3, -2, -1) == -2); + assert(MIN3(-1, 2, 3) == -1); + assert(MIN3(-1, 3, 2) == -1); + assert(MIN3(-2, 1, 3) == -2); + assert(MIN3(-2, 3, 1) == -2); + assert(MIN3(-3, 1, 2) == -3); + assert(MIN3(-3, 2, 1) == -3); + assert(MIN3(-1, 2, -3) == -3); + assert(MIN3(-1, 3, -2) == -2); + assert(MIN3(-2, 1, -3) == -3); + assert(MIN3(-2, 3, -1) == -2); + assert(MIN3(-3, 1, -2) == -3); + assert(MIN3(-3, 2, -1) == -3); + assert(MIN3(-1, -2, 3) == -2); + assert(MIN3(-1, -3, 2) == -3); + assert(MIN3(-2, -1, 3) == -2); + assert(MIN3(-2, -3, 1) == -3); + assert(MIN3(-3, -1, 2) == -3); + assert(MIN3(-3, -2, 1) == -3); + assert(MIN3(-1, -2, -3) == -3); + assert(MIN3(-1, -3, -2) == -3); + assert(MIN3(-2, -1, -3) == -3); + assert(MIN3(-2, -3, -1) == -3); + assert(MIN3(-3, -1, -2) == -3); + assert(MIN3(-3, -2, -1) == -3); + + assert(MAX3(1, 2, 3) == 3); + assert(MAX3(1, 3, 2) == 3); + assert(MAX3(2, 1, 3) == 3); + assert(MAX3(2, 3, 1) == 3); + assert(MAX3(3, 1, 2) == 3); + assert(MAX3(3, 2, 1) == 3); + assert(MAX3(1, 2, -3) == 2); + assert(MAX3(1, 3, -2) == 3); + assert(MAX3(2, 1, -3) == 2); + assert(MAX3(2, 3, -1) == 3); + assert(MAX3(3, 1, -2) == 3); + assert(MAX3(3, 2, -1) == 3); + assert(MAX3(1, -2, 3) == 3); + assert(MAX3(1, -3, 2) == 2); + assert(MAX3(2, -1, 3) == 3); + assert(MAX3(2, -3, 1) == 2); + assert(MAX3(3, -1, 2) == 3); + assert(MAX3(3, -2, 1) == 3); + assert(MAX3(1, -2, -3) == 1); + assert(MAX3(1, -3, -2) == 1); + assert(MAX3(2, -1, -3) == 2); + assert(MAX3(2, -3, -1) == 2); + assert(MAX3(3, -1, -2) == 3); + assert(MAX3(3, -2, -1) == 3); + assert(MAX3(-1, 2, 3) == 3); + assert(MAX3(-1, 3, 2) == 3); + assert(MAX3(-2, 1, 3) == 3); + assert(MAX3(-2, 3, 1) == 3); + assert(MAX3(-3, 1, 2) == 2); + assert(MAX3(-3, 2, 1) == 2); + assert(MAX3(-1, 2, -3) == 2); + assert(MAX3(-1, 3, -2) == 3); + assert(MAX3(-2, 1, -3) == 1); + assert(MAX3(-2, 3, -1) == 3); + assert(MAX3(-3, 1, -2) == 1); + assert(MAX3(-3, 2, -1) == 2); + assert(MAX3(-1, -2, 3) == 3); + assert(MAX3(-1, -3, 2) == 2); + assert(MAX3(-2, -1, 3) == 3); + assert(MAX3(-2, -3, 1) == 1); + assert(MAX3(-3, -1, 2) == 2); + assert(MAX3(-3, -2, 1) == 1); + assert(MAX3(-1, -2, -3) == -1); + assert(MAX3(-1, -3, -2) == -1); + assert(MAX3(-2, -1, -3) == -1); + assert(MAX3(-2, -3, -1) == -1); + assert(MAX3(-3, -1, -2) == -1); + assert(MAX3(-3, -2, -1) == -1); + + assert(ELEMSOF((char [1]){}) == 1); + assert(ELEMSOF((char [2]){}) == 2); + assert(ELEMSOF((char [3]){}) == 3); + assert(ELEMSOF((short int [3]){}) == 3); + assert(ELEMSOF((int [3]){}) == 3); + assert(ELEMSOF((long int [3]){}) == 3); + assert(ELEMSOF((float [3]){}) == 3); + assert(ELEMSOF((double [3]){}) == 3); + + assert(STRLEN("") == 0); + assert(STRLEN("a") == 1); + assert(STRLEN("aa") == 2); + assert(STRLEN("abc") == 3); + assert(STRLEN("a\0bc") == 4); + assert(STRLEN("\0\0\0\0\0") == 5); + +#if defined(__GNUC__) && !defined(__clang__) +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wtype-limits" +#endif + assert(INTSTRLEN(int8_t) == 4); + assert(INTSTRLEN(uint8_t) == 3); + assert(INTSTRLEN(int16_t) == 6); + assert(INTSTRLEN(uint16_t) == 5); + assert(INTSTRLEN(int32_t) == 11); + assert(INTSTRLEN(uint32_t) == 10); + assert(INTSTRLEN(int64_t) == 20 || INTSTRLEN(int64_t) == 21); + assert(INTSTRLEN(uint64_t) == 20); +#if defined(__GNUC__) && !defined(__clang__) +# pragma GCC diagnostic pop +#endif + + assert(TYPE_MAX(int8_t) == INT8_MAX); + assert(TYPE_MAX(uint8_t) == UINT8_MAX); + assert(TYPE_MAX(int16_t) == INT16_MAX); + assert(TYPE_MAX(uint16_t) == UINT16_MAX); + assert(TYPE_MAX(int32_t) == INT32_MAX); + assert(TYPE_MAX(uint32_t) == UINT32_MAX); + assert(TYPE_MAX(int64_t) == INT64_MAX); + assert(TYPE_MAX(uint64_t) == UINT64_MAX); + + assert(TYPE_MIN(int8_t) == INT8_MIN); + assert(TYPE_MIN(uint8_t) == 0); + assert(TYPE_MIN(int16_t) == INT16_MIN); + assert(TYPE_MIN(uint16_t) == 0); + assert(TYPE_MIN(int32_t) == INT32_MIN); + assert(TYPE_MIN(uint32_t) == 0); + assert(TYPE_MIN(int64_t) == INT64_MIN); + assert(TYPE_MIN(uint64_t) == 0); + + assert(strcmpnul(libsimple_strend("abc\0xyz"), "xyz")); + + assert(libsimple_inchrset('a', "xyz") == 0); + assert(libsimple_inchrset('b', "xyz") == 0); + assert(libsimple_inchrset('c', "xyz") == 0); + assert(libsimple_inchrset('x', "xyz") == 1); + assert(libsimple_inchrset('y', "xyz") == 1); + assert(libsimple_inchrset('z', "xyz") == 1); + assert(libsimple_inchrset('\0', "xyz") == 0); + + stpcpy(buf, "abcxyz"); + assert(libsimple_mempcpy(buf, "123", 3) == &buf[3]); + /* assert(strcmpnul(buf, "123xyz")); FIXME */ + + assert(libsimple_streq("abc", "abc") == 1); + assert(libsimple_streq("abc", "ab") == 0); + assert(libsimple_streq("ab", "abc") == 0); + assert(libsimple_streq("ab", "xy") == 0); + assert(libsimple_streq("xy", "ab") == 0); + assert(libsimple_streq("", "1") == 0); + assert(libsimple_streq("1", "") == 0); + assert(libsimple_streq("", "") == 1); + assert(libsimple_streq("abc", "ABC") == 0); + assert(libsimple_streq("ABC", "abc") == 0); + + assert(libsimple_streqnul("abc", "abc") == 1); + assert(libsimple_streqnul("abc", "ab") == 0); + assert(libsimple_streqnul("ab", "abc") == 0); + assert(libsimple_streqnul("ab", "xy") == 0); + assert(libsimple_streqnul("xy", "ab") == 0); + assert(libsimple_streqnul("", "1") == 0); + assert(libsimple_streqnul("1", "") == 0); + assert(libsimple_streqnul("", "") == 1); + assert(libsimple_streqnul("abc", "ABC") == 0); + assert(libsimple_streqnul("ABC", "abc") == 0); + assert(libsimple_streqnul("", NULL) == 0); + assert(libsimple_streqnul("1", NULL) == 0); + assert(libsimple_streqnul(NULL, "") == 0); + assert(libsimple_streqnul(NULL, "1") == 0); + assert(libsimple_streqnul(NULL, NULL) == 1); + + assert(libsimple_strcaseeq("abc", "abc") == 1); + assert(libsimple_strcaseeq("abc", "ab") == 0); + assert(libsimple_strcaseeq("ab", "abc") == 0); + assert(libsimple_strcaseeq("ab", "xy") == 0); + assert(libsimple_strcaseeq("xy", "ab") == 0); + assert(libsimple_strcaseeq("", "1") == 0); + assert(libsimple_strcaseeq("1", "") == 0); + assert(libsimple_strcaseeq("", "") == 1); + assert(libsimple_strcaseeq("abc", "ABC") == 1); + assert(libsimple_strcaseeq("ABC", "abc") == 1); + + assert(libsimple_strcaseeqnul("abc", "abc") == 1); + assert(libsimple_strcaseeqnul("abc", "ab") == 0); + assert(libsimple_strcaseeqnul("ab", "abc") == 0); + assert(libsimple_strcaseeqnul("ab", "xy") == 0); + assert(libsimple_strcaseeqnul("xy", "ab") == 0); + assert(libsimple_strcaseeqnul("", "1") == 0); + assert(libsimple_strcaseeqnul("1", "") == 0); + assert(libsimple_strcaseeqnul("", "") == 1); + assert(libsimple_strcaseeqnul("abc", "ABC") == 1); + assert(libsimple_strcaseeqnul("ABC", "abc") == 1); + assert(libsimple_strcaseeqnul("", NULL) == 0); + assert(libsimple_strcaseeqnul("1", NULL) == 0); + assert(libsimple_strcaseeqnul(NULL, "") == 0); + assert(libsimple_strcaseeqnul(NULL, "1") == 0); + assert(libsimple_strcaseeqnul(NULL, NULL) == 1); + + return 0; +} + +#endif diff --git a/libsimple.h b/libsimple.h index 602d766..2ced0c8 100644 --- a/libsimple.h +++ b/libsimple.h @@ -159,47 +159,47 @@ extern int libsimple_default_failure_exit; #ifndef MIN -# define MIN(A, B) ((A) < (B) ? (A) : (B)) /* TODO test */ +# define MIN(A, B) ((A) < (B) ? (A) : (B)) #endif #ifndef MAX -# define MAX(A, B) ((A) > (B) ? (A) : (B)) /* TODO test */ +# define MAX(A, B) ((A) > (B) ? (A) : (B)) #endif #ifndef MIN3 -# define MIN3(A, B, C) MIN(MIN((A), (B)), (C)) /* TODO test */ +# define MIN3(A, B, C) MIN(MIN((A), (B)), (C)) #endif #ifndef MAX3 -# define MAX3(A, B, C) MAX(MAX((A), (B)), (C)) /* TODO test */ +# define MAX3(A, B, C) MAX(MAX((A), (B)), (C)) #endif #ifndef ELEMSOF -# define ELEMSOF(ARR) (sizeof(ARR) / (sizeof(*(ARR)))) /* TODO test */ +# define ELEMSOF(ARR) (sizeof(ARR) / (sizeof(*(ARR)))) #endif #ifndef STRLEN -# define STRLEN(STR) (sizeof(STR) - 1) /* TODO test */ +# define STRLEN(STR) (sizeof(STR) - 1) #endif #ifndef INTSTRLEN -# define INTSTRLEN(TYPE) ((sizeof(TYPE) == 1 ? 3 : 5 * (sizeof(TYPE) / 2)) + ((TYPE)-1 < 0)) /* TODO test */ +# define INTSTRLEN(TYPE) ((sizeof(TYPE) == 1 ? 3 : 5 * (sizeof(TYPE) / 2)) + ((TYPE)-1 < 0)) #endif #ifndef TYPE_MAX -# define TYPE_MAX(TYPE) ((TYPE)(((1ULL << (8 * sizeof(TYPE) - 1)) - 1) << ((TYPE)-1 > 0) | 1)) /* TODO test */ +# define TYPE_MAX(TYPE) ((TYPE)(((1ULL << (8 * sizeof(TYPE) - 1)) - 1) << ((TYPE)-1 > 0) | 1)) #endif #ifndef TYPE_MIN -# define TYPE_MIN(TYPE) ((TYPE)((TYPE)-1 > 0 ? 0 : (TYPE)~0 < (TYPE)-1 ? (TYPE)~0 : (TYPE)(1ULL << (8 * sizeof(TYPE) - 1)))) /* TODO test */ +# define TYPE_MIN(TYPE) ((TYPE)((TYPE)-1 > 0 ? 0 : (TYPE)~0 < (TYPE)-1 ? (TYPE)~0 : (TYPE)(1ULL << (8 * sizeof(TYPE) - 1)))) #endif @@ -512,14 +512,14 @@ char *libsimple_strchrnul(const char *, int); _LIBSIMPLE_GCC_ONLY(__attribute__((__pure__, __nonnull__, __warn_unused_result__))) -static inline char *libsimple_strend(const char *__s) { return strchr(__s, '\0'); } /* TODO test */ +static inline char *libsimple_strend(const char *__s) { return strchr(__s, '\0'); } #ifndef strend # define strend libsimple_strend #endif _LIBSIMPLE_GCC_ONLY(__attribute__((__pure__, __nonnull__, __warn_unused_result__))) -static inline int libsimple_inchrset(int __c, const char *__s) { return __c && strchr(__s, __c); } /* TODO test */ +static inline int libsimple_inchrset(int __c, const char *__s) { return __c && strchr(__s, __c); } #ifndef inchrset # define inchrset libsimple_inchrset #endif @@ -540,7 +540,7 @@ char *libsimple_strndup(const char *, size_t); _LIBSIMPLE_GCC_ONLY(__attribute__((__warn_unused_result__))) -static inline void *libsimple_mempcpy(void *__d, const void *__s, size_t __n) /* TODO test */ +static inline void *libsimple_mempcpy(void *__d, const void *__s, size_t __n) { return &((char *)memcpy(__d, __s, __n))[__n]; } #ifndef mempcpy # define mempcpy libsimple_mempcpy @@ -804,7 +804,7 @@ static inline int libsimple_strncasecmpnul(const char *__a, const char *__b, siz _LIBSIMPLE_GCC_ONLY(__attribute__((__pure__, __nonnull__, __warn_unused_result__))) -static inline int libsimple_streq(const char *__a, const char *__b) { return !strcmp(__a, __b); } /* TODO test */ +static inline int libsimple_streq(const char *__a, const char *__b) { return !strcmp(__a, __b); } #ifndef streq # define streq libsimple_streq #endif @@ -818,7 +818,7 @@ static inline int libsimple_strneq(const char *__a, const char *__b, size_t __n) _LIBSIMPLE_GCC_ONLY(__attribute__((__pure__, __warn_unused_result__))) -static inline int libsimple_streqnul(const char *__a, const char *__b) { return !strcmpnul(__a, __b); } /* TODO test */ +static inline int libsimple_streqnul(const char *__a, const char *__b) { return !strcmpnul(__a, __b); } #ifndef streqnul # define streqnul libsimple_streqnul #endif @@ -832,6 +832,35 @@ static inline int libsimple_strneqnul(const char *__a, const char *__b, size_t _ #endif +_LIBSIMPLE_GCC_ONLY(__attribute__((__pure__, __nonnull__, __warn_unused_result__))) +static inline int libsimple_strcaseeq(const char *__a, const char *__b) { return !strcasecmp(__a, __b); } +#ifndef strcaseeq +# define strcaseeq libsimple_strcaseeq +#endif + + +_LIBSIMPLE_GCC_ONLY(__attribute__((__pure__, __nonnull__, __warn_unused_result__))) +static inline int libsimple_strncaseeq(const char *__a, const char *__b, size_t __n) { return !strncasecmp(__a, __b, __n); } /* TODO test */ +#ifndef strncaseeq +# define strncaseeq libsimple_strncaseeq +#endif + + +_LIBSIMPLE_GCC_ONLY(__attribute__((__pure__, __warn_unused_result__))) +static inline int libsimple_strcaseeqnul(const char *__a, const char *__b) { return !strcasecmpnul(__a, __b); } +#ifndef strcaseeqnul +# define strcaseeqnul libsimple_strcaseeqnul +#endif + + +_LIBSIMPLE_GCC_ONLY(__attribute__((__pure__, __warn_unused_result__))) +static inline int libsimple_strncaseeqnul(const char *__a, const char *__b, size_t __n) /* TODO test */ +{ return !strncasecmpnul(__a, __b, __n); } +#ifndef strncaseeqnul +# define strncaseeqnul libsimple_strncaseeqnul +#endif + + #define malloczn(CLEAR, ...) _libsimple_malloczn((CLEAR), __VA_ARGS__, (size_t)0) /* TODO test */ #define mallocn(...) malloczn(0, __VA_ARGS__) /* TODO test */ #define callocn(...) malloczn(1, __VA_ARGS__) /* TODO test */ @@ -23,9 +23,9 @@ main(void) void *p = libsimple_memdup(s, 5); assert(p); assert(p != s); - assert(!strcmp(p, s)); + assert(!strcmpnul(p, s)); memset(p, 0, 5); - assert(!strcmp(s, "test")); + assert(!strcmpnul(s, "test")); free(p); return 0; } @@ -30,25 +30,25 @@ libsimple_memmem(const void *hay_, size_t hayn, const void *sub_, size_t subn) int main(void) { - assert(!strcmp(libsimple_memmem("test", 4, "test", 4), "test")); - assert(!strcmp(libsimple_memmem("", 0, "", 0), "")); - assert(!strcmp(libsimple_memmem("test", 4, "", 0), "test")); + assert(!strcmpnul(libsimple_memmem("test", 4, "test", 4), "test")); + assert(!strcmpnul(libsimple_memmem("", 0, "", 0), "")); + assert(!strcmpnul(libsimple_memmem("test", 4, "", 0), "test")); assert(!libsimple_memmem("", 0, "test", 4)); assert(!libsimple_memmem("t", 1, "test", 4)); - assert(!strcmp(libsimple_memmem("test", 4, "t", 1), "test")); - assert(!strcmp(libsimple_memmem("test", 4, "e", 1), "est")); - assert(!strcmp(libsimple_memmem("test", 4, "s", 1), "st")); + assert(!strcmpnul(libsimple_memmem("test", 4, "t", 1), "test")); + assert(!strcmpnul(libsimple_memmem("test", 4, "e", 1), "est")); + assert(!strcmpnul(libsimple_memmem("test", 4, "s", 1), "st")); assert(!libsimple_memmem("test", 4, "x", 1)); - assert(!strcmp(libsimple_memmem("test", 4, "te", 2), "test")); - assert(!strcmp(libsimple_memmem("test", 4, "es", 2), "est")); - assert(!strcmp(libsimple_memmem("test", 4, "st", 2), "st")); - assert(!strcmp(libsimple_memmem("test", 5, "t", 2), "t")); + assert(!strcmpnul(libsimple_memmem("test", 4, "te", 2), "test")); + assert(!strcmpnul(libsimple_memmem("test", 4, "es", 2), "est")); + assert(!strcmpnul(libsimple_memmem("test", 4, "st", 2), "st")); + assert(!strcmpnul(libsimple_memmem("test", 5, "t", 2), "t")); assert(!libsimple_memmem("test", 4, "xx", 2)); - assert(!strcmp(libsimple_memmem("abc", 3, "c", 1), "c")); - assert(!strcmp(libsimple_memmem("abc", 3, "bc", 2), "bc")); - assert(!strcmp(libsimple_memmem("abc", 3, "abc", 3), "abc")); + assert(!strcmpnul(libsimple_memmem("abc", 3, "c", 1), "c")); + assert(!strcmpnul(libsimple_memmem("abc", 3, "bc", 2), "bc")); + assert(!strcmpnul(libsimple_memmem("abc", 3, "abc", 3), "abc")); assert(!libsimple_memmem("abc", 3, "abc", 4)); - assert(!strcmp(libsimple_memmem("abcabc", 6, "bc", 2), "bcabc")); + assert(!strcmpnul(libsimple_memmem("abcabc", 6, "bc", 2), "bcabc")); return 0; } @@ -19,8 +19,8 @@ libsimple_memrchr(const void *s_, int c, size_t n_) int main(void) { - assert(!strcmp(libsimple_memrchr("1234512345", '3', sizeof("1234512345") - 1), "345")); - assert(!strcmp(libsimple_memrchr("1234512345", '5', sizeof("1234512345") - 1), "5")); + assert(!strcmpnul(libsimple_memrchr("1234512345", '3', sizeof("1234512345") - 1), "345")); + assert(!strcmpnul(libsimple_memrchr("1234512345", '5', sizeof("1234512345") - 1), "5")); assert(!libsimple_memrchr("1234512345", 'x', sizeof("1234512345") - 1)); return 0; } @@ -33,25 +33,25 @@ libsimple_memrmem(const void *hay_, size_t hayn, const void *sub_, size_t subn) int main(void) { - assert(!strcmp(libsimple_memrmem("test", 4, "test", 4), "test")); - assert(!strcmp(libsimple_memrmem("", 0, "", 0), "")); - assert(!strcmp(libsimple_memrmem("test", 4, "", 0), "")); + assert(!strcmpnul(libsimple_memrmem("test", 4, "test", 4), "test")); + assert(!strcmpnul(libsimple_memrmem("", 0, "", 0), "")); + assert(!strcmpnul(libsimple_memrmem("test", 4, "", 0), "")); assert(!libsimple_memrmem("", 0, "test", 4)); assert(!libsimple_memrmem("t", 1, "test", 4)); - assert(!strcmp(libsimple_memrmem("test", 4, "t", 1), "t")); - assert(!strcmp(libsimple_memrmem("test", 4, "e", 1), "est")); - assert(!strcmp(libsimple_memrmem("test", 4, "s", 1), "st")); + assert(!strcmpnul(libsimple_memrmem("test", 4, "t", 1), "t")); + assert(!strcmpnul(libsimple_memrmem("test", 4, "e", 1), "est")); + assert(!strcmpnul(libsimple_memrmem("test", 4, "s", 1), "st")); assert(!libsimple_memrmem("test", 4, "x", 1)); - assert(!strcmp(libsimple_memrmem("test", 4, "te", 2), "test")); - assert(!strcmp(libsimple_memrmem("test", 4, "es", 2), "est")); - assert(!strcmp(libsimple_memrmem("test", 4, "st", 2), "st")); - assert(!strcmp(libsimple_memrmem("test", 5, "t", 2), "t")); + assert(!strcmpnul(libsimple_memrmem("test", 4, "te", 2), "test")); + assert(!strcmpnul(libsimple_memrmem("test", 4, "es", 2), "est")); + assert(!strcmpnul(libsimple_memrmem("test", 4, "st", 2), "st")); + assert(!strcmpnul(libsimple_memrmem("test", 5, "t", 2), "t")); assert(!libsimple_memrmem("test", 4, "xx", 2)); - assert(!strcmp(libsimple_memrmem("abc", 3, "c", 1), "c")); - assert(!strcmp(libsimple_memrmem("abc", 3, "bc", 2), "bc")); - assert(!strcmp(libsimple_memrmem("abc", 3, "abc", 3), "abc")); + assert(!strcmpnul(libsimple_memrmem("abc", 3, "c", 1), "c")); + assert(!strcmpnul(libsimple_memrmem("abc", 3, "bc", 2), "bc")); + assert(!strcmpnul(libsimple_memrmem("abc", 3, "abc", 3), "abc")); assert(!libsimple_memrmem("abc", 3, "abc", 4)); - assert(!strcmp(libsimple_memrmem("abcabc", 6, "bc", 2), "bc")); + assert(!strcmpnul(libsimple_memrmem("abcabc", 6, "bc", 2), "bc")); return 0; } diff --git a/rawmemchr.c b/rawmemchr.c index 9bc02e0..091d39f 100644 --- a/rawmemchr.c +++ b/rawmemchr.c @@ -18,8 +18,8 @@ libsimple_rawmemchr(const void *s_, int c) int main(void) { - assert(!strcmp(libsimple_rawmemchr("1234512345", '3'), "34512345")); - assert(!strcmp(libsimple_rawmemchr("1234512345", '1'), "1234512345")); + assert(!strcmpnul(libsimple_rawmemchr("1234512345", '3'), "34512345")); + assert(!strcmpnul(libsimple_rawmemchr("1234512345", '1'), "1234512345")); return 0; } diff --git a/rawmemrchr.c b/rawmemrchr.c index eb685cf..82a2d98 100644 --- a/rawmemrchr.c +++ b/rawmemrchr.c @@ -18,8 +18,8 @@ libsimple_rawmemrchr(const void *s_, int c, size_t n) int main(void) { - assert(!strcmp(libsimple_rawmemrchr("1234512345", '3', sizeof("1234512345") - 1), "345")); - assert(!strcmp(libsimple_rawmemrchr("1234512345", '5', sizeof("1234512345") - 1), "5")); + assert(!strcmpnul(libsimple_rawmemrchr("1234512345", '3', sizeof("1234512345") - 1), "345")); + assert(!strcmpnul(libsimple_rawmemrchr("1234512345", '5', sizeof("1234512345") - 1), "5")); return 0; } diff --git a/strcasestr.c b/strcasestr.c index 4ffbde2..071b7df 100644 --- a/strcasestr.c +++ b/strcasestr.c @@ -23,52 +23,52 @@ libsimple_strcasestr(const char *h_, const char *n) int main(void) { - assert(!strcmp(libsimple_strcasestr("", ""), "")); - assert(!strcmp(libsimple_strcasestr("test", ""), "test")); + assert(!strcmpnul(libsimple_strcasestr("", ""), "")); + assert(!strcmpnul(libsimple_strcasestr("test", ""), "test")); assert(!libsimple_strcasestr("", "test")); assert(!libsimple_strcasestr("t", "test")); - assert(!strcmp(libsimple_strcasestr("test", "t"), "test")); - assert(!strcmp(libsimple_strcasestr("test", "e"), "est")); - assert(!strcmp(libsimple_strcasestr("test", "s"), "st")); + assert(!strcmpnul(libsimple_strcasestr("test", "t"), "test")); + assert(!strcmpnul(libsimple_strcasestr("test", "e"), "est")); + assert(!strcmpnul(libsimple_strcasestr("test", "s"), "st")); assert(!libsimple_strcasestr("test", "x")); - assert(!strcmp(libsimple_strcasestr("test", "te"), "test")); - assert(!strcmp(libsimple_strcasestr("test", "es"), "est")); - assert(!strcmp(libsimple_strcasestr("test", "st"), "st")); + assert(!strcmpnul(libsimple_strcasestr("test", "te"), "test")); + assert(!strcmpnul(libsimple_strcasestr("test", "es"), "est")); + assert(!strcmpnul(libsimple_strcasestr("test", "st"), "st")); assert(!libsimple_strcasestr("test", "xx")); - assert(!strcmp(libsimple_strcasestr("abc", "c"), "c")); - assert(!strcmp(libsimple_strcasestr("abc", "bc"), "bc")); - assert(!strcmp(libsimple_strcasestr("abc", "abc"), "abc")); - assert(!strcmp(libsimple_strcasestr("abcabc", "bc"), "bcabc")); + assert(!strcmpnul(libsimple_strcasestr("abc", "c"), "c")); + assert(!strcmpnul(libsimple_strcasestr("abc", "bc"), "bc")); + assert(!strcmpnul(libsimple_strcasestr("abc", "abc"), "abc")); + assert(!strcmpnul(libsimple_strcasestr("abcabc", "bc"), "bcabc")); - assert(!strcmp(libsimple_strcasestr("TEST", ""), "TEST")); + assert(!strcmpnul(libsimple_strcasestr("TEST", ""), "TEST")); assert(!libsimple_strcasestr("T", "test")); - assert(!strcmp(libsimple_strcasestr("TEST", "t"), "TEST")); - assert(!strcmp(libsimple_strcasestr("TEST", "e"), "EST")); - assert(!strcmp(libsimple_strcasestr("TEST", "s"), "ST")); + assert(!strcmpnul(libsimple_strcasestr("TEST", "t"), "TEST")); + assert(!strcmpnul(libsimple_strcasestr("TEST", "e"), "EST")); + assert(!strcmpnul(libsimple_strcasestr("TEST", "s"), "ST")); assert(!libsimple_strcasestr("TEST", "x")); - assert(!strcmp(libsimple_strcasestr("TEST", "te"), "TEST")); - assert(!strcmp(libsimple_strcasestr("TEST", "es"), "EST")); - assert(!strcmp(libsimple_strcasestr("TEST", "st"), "ST")); + assert(!strcmpnul(libsimple_strcasestr("TEST", "te"), "TEST")); + assert(!strcmpnul(libsimple_strcasestr("TEST", "es"), "EST")); + assert(!strcmpnul(libsimple_strcasestr("TEST", "st"), "ST")); assert(!libsimple_strcasestr("TEST", "xx")); - assert(!strcmp(libsimple_strcasestr("ABC", "c"), "C")); - assert(!strcmp(libsimple_strcasestr("ABC", "bc"), "BC")); - assert(!strcmp(libsimple_strcasestr("ABC", "abc"), "ABC")); - assert(!strcmp(libsimple_strcasestr("ABCABC", "bc"), "BCABC")); + assert(!strcmpnul(libsimple_strcasestr("ABC", "c"), "C")); + assert(!strcmpnul(libsimple_strcasestr("ABC", "bc"), "BC")); + assert(!strcmpnul(libsimple_strcasestr("ABC", "abc"), "ABC")); + assert(!strcmpnul(libsimple_strcasestr("ABCABC", "bc"), "BCABC")); assert(!libsimple_strcasestr("", "TEST")); assert(!libsimple_strcasestr("t", "TEST")); - assert(!strcmp(libsimple_strcasestr("test", "T"), "test")); - assert(!strcmp(libsimple_strcasestr("test", "E"), "est")); - assert(!strcmp(libsimple_strcasestr("test", "S"), "st")); + assert(!strcmpnul(libsimple_strcasestr("test", "T"), "test")); + assert(!strcmpnul(libsimple_strcasestr("test", "E"), "est")); + assert(!strcmpnul(libsimple_strcasestr("test", "S"), "st")); assert(!libsimple_strcasestr("test", "X")); - assert(!strcmp(libsimple_strcasestr("test", "TE"), "test")); - assert(!strcmp(libsimple_strcasestr("test", "ES"), "est")); - assert(!strcmp(libsimple_strcasestr("test", "ST"), "st")); + assert(!strcmpnul(libsimple_strcasestr("test", "TE"), "test")); + assert(!strcmpnul(libsimple_strcasestr("test", "ES"), "est")); + assert(!strcmpnul(libsimple_strcasestr("test", "ST"), "st")); assert(!libsimple_strcasestr("test", "XX")); - assert(!strcmp(libsimple_strcasestr("abc", "C"), "c")); - assert(!strcmp(libsimple_strcasestr("abc", "BC"), "bc")); - assert(!strcmp(libsimple_strcasestr("abc", "ABC"), "abc")); - assert(!strcmp(libsimple_strcasestr("abcabc", "BC"), "bcabc")); + assert(!strcmpnul(libsimple_strcasestr("abc", "C"), "c")); + assert(!strcmpnul(libsimple_strcasestr("abc", "BC"), "bc")); + assert(!strcmpnul(libsimple_strcasestr("abc", "ABC"), "abc")); + assert(!strcmpnul(libsimple_strcasestr("abcabc", "BC"), "bcabc")); return 0; } diff --git a/strchrnul.c b/strchrnul.c index 2dccb7a..77634c0 100644 --- a/strchrnul.c +++ b/strchrnul.c @@ -18,9 +18,9 @@ libsimple_strchrnul(const char *s_, int c) int main(void) { - assert(!strcmp(libsimple_strchrnul("1234512345", '3'), "34512345")); - assert(!strcmp(libsimple_strchrnul("1234512345", '1'), "1234512345")); - assert(!strcmp(libsimple_strchrnul("1234512345", 'x'), "")); + assert(!strcmpnul(libsimple_strchrnul("1234512345", '3'), "34512345")); + assert(!strcmpnul(libsimple_strchrnul("1234512345", '1'), "1234512345")); + assert(!strcmpnul(libsimple_strchrnul("1234512345", 'x'), "")); return 0; } @@ -25,30 +25,30 @@ libsimple_strndup(const char *s, size_t n) /* FIXME */ #include "test.h" int -main(void) +main(void) /* TODO valgrind */ { const char *s = "test"; void *p; p = libsimple_memdup(s, 5); assert(p && p != s); - assert(!strcmp(p, "test")); + assert(!strcmpnul(p, "test")); memset(p, 0, 5); - assert(!strcmp(s, "test")); + assert(!strcmpnul(s, "test")); free(p); p = libsimple_memdup(s, 3); assert(p && p != s); - assert(!strcmp(p, "tes")); + assert(!strcmpnul(p, "tes")); memset(p, 0, 5); - assert(!strcmp(s, "test")); + assert(!strcmpnul(s, "test")); free(p); p = libsimple_memdup(s, 0); assert(p && p != s); - assert(!strcmp(p, "")); + assert(!strcmpnul(p, "")); memset(p, 0, 5); - assert(!strcmp(s, "test")); + assert(!strcmpnul(s, "test")); free(p); return 0; diff --git a/strrcasestr.c b/strrcasestr.c index 2b1f2ab..3a3edce 100644 --- a/strrcasestr.c +++ b/strrcasestr.c @@ -28,51 +28,51 @@ libsimple_strrcasestr(const char *h_, const char *n) int main(void) { - assert(!strcmp(libsimple_strrcasestr("", ""), "")); - assert(!strcmp(libsimple_strrcasestr("test", ""), "")); - 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")); + assert(!strcmpnul(libsimple_strrcasestr("", ""), "")); + assert(!strcmpnul(libsimple_strrcasestr("test", ""), "")); + assert(!strcmpnul(libsimple_strrcasestr("t", "t"), "t")); + assert(!strcmpnul(libsimple_strrcasestr("test", "t"), "t")); + assert(!strcmpnul(libsimple_strrcasestr("test", "e"), "est")); + assert(!strcmpnul(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(!strcmpnul(libsimple_strrcasestr("test", "te"), "test")); + assert(!strcmpnul(libsimple_strrcasestr("test", "es"), "est")); + assert(!strcmpnul(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(!strcmpnul(libsimple_strrcasestr("abc", "c"), "c")); + assert(!strcmpnul(libsimple_strrcasestr("abc", "bc"), "bc")); + assert(!strcmpnul(libsimple_strrcasestr("abc", "abc"), "abc")); + assert(!strcmpnul(libsimple_strrcasestr("abcabc", "bc"), "bc")); - assert(!strcmp(libsimple_strrcasestr("TEST", ""), "")); + assert(!strcmpnul(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(!strcmpnul(libsimple_strrcasestr("TEST", "t"), "T")); + assert(!strcmpnul(libsimple_strrcasestr("TEST", "e"), "EST")); + assert(!strcmpnul(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(!strcmpnul(libsimple_strrcasestr("TEST", "te"), "TEST")); + assert(!strcmpnul(libsimple_strrcasestr("TEST", "es"), "EST")); + assert(!strcmpnul(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(!strcmpnul(libsimple_strrcasestr("ABC", "c"), "C")); + assert(!strcmpnul(libsimple_strrcasestr("ABC", "bc"), "BC")); + assert(!strcmpnul(libsimple_strrcasestr("ABC", "abc"), "ABC")); + assert(!strcmpnul(libsimple_strrcasestr("ABCABC", "bc"), "BC")); 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(!strcmpnul(libsimple_strrcasestr("test", "T"), "t")); + assert(!strcmpnul(libsimple_strrcasestr("test", "E"), "est")); + assert(!strcmpnul(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(!strcmpnul(libsimple_strrcasestr("test", "TE"), "test")); + assert(!strcmpnul(libsimple_strrcasestr("test", "ES"), "est")); + assert(!strcmpnul(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(!strcmpnul(libsimple_strrcasestr("abc", "C"), "c")); + assert(!strcmpnul(libsimple_strrcasestr("abc", "BC"), "bc")); + assert(!strcmpnul(libsimple_strrcasestr("abc", "ABC"), "abc")); + assert(!strcmpnul(libsimple_strrcasestr("abcabc", "BC"), "bc")); return 0; } @@ -16,26 +16,26 @@ libsimple_strrstr(const char *hay, const char *sub) int main(void) { - assert(!strcmp(libsimple_strrstr("test", "test"), "test")); - assert(!strcmp(libsimple_strrstr("", ""), "")); - assert(!strcmp(libsimple_strrstr("test", ""), "")); + assert(!strcmpnul(libsimple_strrstr("test", "test"), "test")); + assert(!strcmpnul(libsimple_strrstr("", ""), "")); + assert(!strcmpnul(libsimple_strrstr("test", ""), "")); assert(!libsimple_strrstr("", "test")); assert(!libsimple_strrstr("t", "test")); - assert(!strcmp(libsimple_strrstr("test", "t"), "t")); - assert(!strcmp(libsimple_strrstr("test", "e"), "est")); - assert(!strcmp(libsimple_strrstr("test", "s"), "st")); + assert(!strcmpnul(libsimple_strrstr("test", "t"), "t")); + assert(!strcmpnul(libsimple_strrstr("test", "e"), "est")); + assert(!strcmpnul(libsimple_strrstr("test", "s"), "st")); assert(!libsimple_strrstr("test", "x")); - assert(!strcmp(libsimple_strrstr("test", "te"), "test")); - assert(!strcmp(libsimple_strrstr("test", "es"), "est")); - assert(!strcmp(libsimple_strrstr("test", "st"), "st")); + assert(!strcmpnul(libsimple_strrstr("test", "te"), "test")); + assert(!strcmpnul(libsimple_strrstr("test", "es"), "est")); + assert(!strcmpnul(libsimple_strrstr("test", "st"), "st")); assert(!libsimple_strrstr("test", "xx")); - assert(!strcmp(libsimple_strrstr("abc", "c"), "c")); - assert(!strcmp(libsimple_strrstr("abc", "bc"), "bc")); - assert(!strcmp(libsimple_strrstr("abc", "abc"), "abc")); - assert(!strcmp(libsimple_strrstr("abcabc", "bc"), "bc")); + assert(!strcmpnul(libsimple_strrstr("abc", "c"), "c")); + assert(!strcmpnul(libsimple_strrstr("abc", "bc"), "bc")); + assert(!strcmpnul(libsimple_strrstr("abc", "abc"), "abc")); + assert(!strcmpnul(libsimple_strrstr("abcabc", "bc"), "bc")); assert(!libsimple_strrstr("TEST", "test")); - assert(!strcmp(libsimple_strrstr("TEST", ""), "")); + assert(!strcmpnul(libsimple_strrstr("TEST", ""), "")); assert(!libsimple_strrstr("T", "test")); assert(!libsimple_strrstr("TEST", "t")); assert(!libsimple_strrstr("TEST", "e")); @@ -8,5 +8,3 @@ fprintf(stderr, "Failed at %s:%i: %s\n", __FILE__, __LINE__, #EXPR);\ exit(1);\ } while (0) - -#define strcmp(...) strcmpnul(__VA_ARGS__) |