diff options
Diffstat (limited to '')
-rw-r--r-- | allocn.c | 6 | ||||
-rw-r--r-- | asprintf.c | 20 | ||||
-rw-r--r-- | difftimespec.c | 2 | ||||
-rw-r--r-- | difftimeval.c | 2 | ||||
-rw-r--r-- | doubletotimespec.c | 2 | ||||
-rw-r--r-- | doubletotimeval.c | 2 | ||||
-rw-r--r-- | encalloc.c | 2 | ||||
-rw-r--r-- | enmalloc.c | 2 | ||||
-rw-r--r-- | enmemdup.c | 2 | ||||
-rw-r--r-- | enrealloc.c | 2 | ||||
-rw-r--r-- | enstrdup.c | 2 | ||||
-rw-r--r-- | enstrndup.c | 2 | ||||
-rw-r--r-- | envmalloczn.c | 2 | ||||
-rw-r--r-- | envputenvf.c | 2 | ||||
-rw-r--r-- | envreallocn.c | 2 | ||||
-rw-r--r-- | isutf8.c | 37 | ||||
-rw-r--r-- | libsimple.h | 156 | ||||
-rw-r--r-- | memdup.c | 20 | ||||
-rw-r--r-- | memends.c | 43 | ||||
-rw-r--r-- | memmem.c | 31 | ||||
-rw-r--r-- | memrchr.c | 15 | ||||
-rw-r--r-- | memstarts.c | 43 | ||||
-rw-r--r-- | multimespec.c | 2 | ||||
-rw-r--r-- | multimeval.c | 2 | ||||
-rw-r--r-- | rawmemchr.c | 14 | ||||
-rw-r--r-- | rawmemrchr.c | 14 | ||||
-rw-r--r-- | recvfd.c | 2 | ||||
-rw-r--r-- | recvfrom_timestamped.c | 2 | ||||
-rw-r--r-- | sendfd.c | 2 | ||||
-rw-r--r-- | strcaseends.c | 40 | ||||
-rw-r--r-- | strcasestr.c | 58 | ||||
-rw-r--r-- | strchrnul.c | 15 | ||||
-rw-r--r-- | strends.c | 40 | ||||
-rw-r--r-- | strndup.c | 44 | ||||
-rw-r--r-- | strstarts.c | 40 | ||||
-rw-r--r-- | strtotimespec.c | 2 | ||||
-rw-r--r-- | strtotimeval.c | 2 | ||||
-rw-r--r-- | sumtimespec.c | 2 | ||||
-rw-r--r-- | sumtimeval.c | 2 | ||||
-rw-r--r-- | timespec2timeval.c | 2 | ||||
-rw-r--r-- | timespectostr.c | 2 | ||||
-rw-r--r-- | timevaltostr.c | 2 | ||||
-rw-r--r-- | vasprintf.c | 12 | ||||
-rw-r--r-- | vputenvf.c | 2 | ||||
-rw-r--r-- | vweprintf.c | 2 |
45 files changed, 591 insertions, 111 deletions
@@ -3,7 +3,7 @@ static inline size_t -alloc_size_product(size_t n, va_list ap) +alloc_size_product(size_t n, va_list ap) /* TODO test */ { size_t prod = n; if (!n) { @@ -24,14 +24,14 @@ alloc_size_product(size_t n, va_list ap) } void * -libsimple_vmalloczn(int clear, size_t n, va_list ap) +libsimple_vmalloczn(int clear, size_t n, va_list ap) /* TODO test */ { n = alloc_size_product(n, ap); return !n ? NULL : clear ? calloc(1, n) : malloc(n); } void * -libsimple_vreallocn(void *ptr, size_t n, va_list ap) +libsimple_vreallocn(void *ptr, size_t n, va_list ap) /* TODO test */ { n = alloc_size_product(n, ap); return !n ? NULL : realloc(ptr, n); @@ -12,3 +12,23 @@ libsimple_asprintf(char **strp, const char *fmt, ...) va_end(ap); return r; } + + +#ifdef TEST +#include <assert.h> + +int +main(void) +{ + char *s = ""; + char *old = s; + + assert(libsimple_asprintf(&s, "%i %X", 99999, 255) == sizeof("99999 FF") - 1); + assert(s && s != old); + assert(!strcmp(s, "99999 FF")); + free(s); + + return 0; +} + +#endif diff --git a/difftimespec.c b/difftimespec.c index 4ea74a8..383dced 100644 --- a/difftimespec.c +++ b/difftimespec.c @@ -3,7 +3,7 @@ int -libsimple_difftimespec(struct timespec *diff, const struct timespec *minuend, const struct timespec *subtrahend) +libsimple_difftimespec(struct timespec *diff, const struct timespec *minuend, const struct timespec *subtrahend) /* TODO test */ { long int ns = minuend->tv_nsec - subtrahend->tv_nsec; time_t s; diff --git a/difftimeval.c b/difftimeval.c index b4c3451..9ba06c5 100644 --- a/difftimeval.c +++ b/difftimeval.c @@ -3,7 +3,7 @@ int -libsimple_difftimeval(struct timeval *diff, const struct timeval *minuend, const struct timeval *subtrahend) +libsimple_difftimeval(struct timeval *diff, const struct timeval *minuend, const struct timeval *subtrahend) /* TODO test */ { struct timespec a, b, d; int r; diff --git a/doubletotimespec.c b/doubletotimespec.c index 8b2cccd..e74f04a 100644 --- a/doubletotimespec.c +++ b/doubletotimespec.c @@ -3,7 +3,7 @@ void -libsimple_doubletotimespec(struct timespec *ts, double d) +libsimple_doubletotimespec(struct timespec *ts, double d) /* TODO test */ { double ns = (long long int)d; long int nsi; diff --git a/doubletotimeval.c b/doubletotimeval.c index c9c1b61..c86c771 100644 --- a/doubletotimeval.c +++ b/doubletotimeval.c @@ -3,7 +3,7 @@ void -libsimple_doubletotimeval(struct timeval *tv, double d) +libsimple_doubletotimeval(struct timeval *tv, double d) /* TODO test */ { double ns = (long long int)d; long int nsi; @@ -6,7 +6,7 @@ extern char *argv0; void * -encalloc(int status, size_t n, size_t m) +encalloc(int status, size_t n, size_t m) /* TODO test */ { void *ret = calloc(n, m); if (!ret) { @@ -6,7 +6,7 @@ extern char *argv0; void * -enmalloc(int status, size_t n) +enmalloc(int status, size_t n) /* TODO test */ { void *ret = malloc(n); if (!ret) { @@ -6,7 +6,7 @@ extern char *argv0; void * -enmemdup(int status, const void *s, size_t n) +enmemdup(int status, const void *s, size_t n) /* TODO test */ { void *ret = memdup(s, n); if (!ret) { diff --git a/enrealloc.c b/enrealloc.c index 565eb0a..4b0775b 100644 --- a/enrealloc.c +++ b/enrealloc.c @@ -6,7 +6,7 @@ extern char *argv0; void * -enrealloc(int status, void *ptr, size_t n) +enrealloc(int status, void *ptr, size_t n) /* TODO test */ { char *ret = realloc(ptr, n); if (!ret) { @@ -6,7 +6,7 @@ extern char *argv0; char * -enstrdup(int status, const char *s) +enstrdup(int status, const char *s) /* TODO test */ { char *ret = strdup(s); if (!ret) { diff --git a/enstrndup.c b/enstrndup.c index 58fd74e..8d96da9 100644 --- a/enstrndup.c +++ b/enstrndup.c @@ -6,7 +6,7 @@ extern char *argv0; char * -enstrndup(int status, const char *s, size_t n) +enstrndup(int status, const char *s, size_t n) /* TODO test */ { void *ret = strndup(s, n); if (!ret) { diff --git a/envmalloczn.c b/envmalloczn.c index 27c6c0b..661e18a 100644 --- a/envmalloczn.c +++ b/envmalloczn.c @@ -6,7 +6,7 @@ extern char *argv0; void * -libsimple_envmalloczn(int status, int clear, size_t n, va_list ap) +libsimple_envmalloczn(int status, int clear, size_t n, va_list ap) /* TODO test */ { void *ret = libsimple_vmalloczn(clear, n, ap); if (!ret) { diff --git a/envputenvf.c b/envputenvf.c index 1b463ce..d59d809 100644 --- a/envputenvf.c +++ b/envputenvf.c @@ -6,7 +6,7 @@ extern char *argv0; void -envputenvf(int status, const char *fmt, va_list ap) +envputenvf(int status, const char *fmt, va_list ap) /* TODO test */ { if (vputenvf(fmt, ap)) { fprintf(stderr, "%s: putenvf: %s\n", argv0, strerror(errno)); diff --git a/envreallocn.c b/envreallocn.c index 25a5e33..ec64d29 100644 --- a/envreallocn.c +++ b/envreallocn.c @@ -6,7 +6,7 @@ extern char *argv0; void * -libsimple_envreallocn(int status, void *ptr, size_t n, va_list ap) +libsimple_envreallocn(int status, void *ptr, size_t n, va_list ap) /* TODO test */ { void *ret = libsimple_vreallocn(ptr, n, ap); if (!ret) { @@ -68,3 +68,40 @@ libsimple_isutf8(const char *string, int allow_modified_nul) /* Make sure we did not stop at the middle of a multibyte character. */ return !read_bytes; } + + +#ifdef TEST +#include <assert.h> + +int +main(void) +{ + int i; + for (i = 0; i < 2; i++) { + assert(libsimple_isutf8("", i) == 1); + assert(libsimple_isutf8("a", i) == 1); + assert(libsimple_isutf8("abc", i) == 1); + assert(libsimple_isutf8("123", i) == 1); + assert(libsimple_isutf8("åäö", i) == 1); + assert(libsimple_isutf8("𝖆𝖇𝖈", i) == 1); + assert(libsimple_isutf8("\x1b", i) == 1); + assert(libsimple_isutf8("\n\r\t\f", i) == 1); + assert(libsimple_isutf8("\xFF", i) == 0); + assert(libsimple_isutf8("\x01", i) == 1); + assert(libsimple_isutf8("\x7F", i) == 1); + assert(libsimple_isutf8("\x80", i) == 0); + assert(libsimple_isutf8("\xC0", i) == 0); + assert(libsimple_isutf8("\xC0\x80", i) == i); + assert(libsimple_isutf8("\xC0\x81", 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\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("\xFF\x80\x80\x80\x80\x80\x80\x80", i) == 0); + } + return 0; +} + +#endif diff --git a/libsimple.h b/libsimple.h index 5080be2..8a6395f 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)) +# define MIN(A, B) ((A) < (B) ? (A) : (B)) /* TODO test */ #endif #ifndef MAX -# define MAX(A, B) ((A) > (B) ? (A) : (B)) +# define MAX(A, B) ((A) > (B) ? (A) : (B)) /* TODO test */ #endif #ifndef MIN3 -# define MIN3(A, B, C) MIN(MIN((A), (B)), (C)) +# define MIN3(A, B, C) MIN(MIN((A), (B)), (C)) /* TODO test */ #endif #ifndef MAX3 -# define MAX3(A, B, C) MAX(MAX((A), (B)), (C)) +# define MAX3(A, B, C) MAX(MAX((A), (B)), (C)) /* TODO test */ #endif #ifndef ELEMSOF -# define ELEMSOF(ARR) (sizeof(ARR) / (sizeof(*(ARR)))) +# define ELEMSOF(ARR) (sizeof(ARR) / (sizeof(*(ARR)))) /* TODO test */ #endif #ifndef STRLEN -# define STRLEN(STR) (sizeof(STR) - 1) +# define STRLEN(STR) (sizeof(STR) - 1) /* TODO test */ #endif #ifndef INTSTRLEN -# define INTSTRLEN(TYPE) ((sizeof(TYPE) == 1 ? 3 : 5 * (sizeof(TYPE) / 2)) + ((TYPE)-1 < 0)) +# define INTSTRLEN(TYPE) ((sizeof(TYPE) == 1 ? 3 : 5 * (sizeof(TYPE) / 2)) + ((TYPE)-1 < 0)) /* TODO test */ #endif #ifndef TYPE_MAX -# define TYPE_MAX(TYPE) ((TYPE)(((1ULL << (8 * sizeof(TYPE) - 1)) - 1) << ((TYPE)-1 > 0) | 1)) +# define TYPE_MAX(TYPE) ((TYPE)(((1ULL << (8 * sizeof(TYPE) - 1)) - 1) << ((TYPE)-1 > 0) | 1)) /* TODO test */ #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)))) +# define TYPE_MIN(TYPE) ((TYPE)((TYPE)-1 > 0 ? 0 : (TYPE)~0 < (TYPE)-1 ? (TYPE)~0 : (TYPE)(1ULL << (8 * sizeof(TYPE) - 1)))) /* TODO test */ #endif @@ -477,10 +477,10 @@ extern int libsimple_default_failure_exit; #endif -#define FREE(PTR) (free(PTR), (PTR) = NULL, 0) +#define FREE(PTR) (free(PTR), (PTR) = NULL, 0) /* TODO test */ -#define CLOSE(FD) ((FD) >= 0 ? (close(FD), (FD) = -1, 0) : 0) +#define CLOSE(FD) ((FD) >= 0 ? (close(FD), (FD) = -1, 0) : 0) /* TODO test */ _LIBSIMPLE_GCC_ONLY(__attribute__((__pure__, __nonnull__, __warn_unused_result__))) @@ -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'); } +static inline char *libsimple_strend(const char *__s) { return strchr(__s, '\0'); } /* TODO test */ #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); } +static inline int libsimple_inchrset(int __c, const char *__s) { return __c && strchr(__s, __c); } /* TODO test */ #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) +static inline void *libsimple_mempcpy(void *__d, const void *__s, size_t __n) /* TODO test */ { return &((char *)memcpy(__d, __s, __n))[__n]; } #ifndef mempcpy # define mempcpy libsimple_mempcpy @@ -549,7 +549,7 @@ static inline void *libsimple_mempcpy(void *__d, const void *__s, size_t __n) #ifndef strdupa # if defined(__GNUC__) || defined(__clang__) -# define strdupa(s)\ +# define strdupa(s) /* TODO test */\ ({\ const char *__s = (s);\ size_t __n = strlen(__s) + 1;\ @@ -562,7 +562,7 @@ static inline void *libsimple_mempcpy(void *__d, const void *__s, size_t __n) #ifndef strdupa # if defined(__GNUC__) || defined(__clang__) -# define strndupa(s, n)\ +# define strndupa(s, n) /* TODO test */\ ({\ const char *__s = (s);\ size_t __n = (n);\ @@ -580,7 +580,7 @@ static inline void *libsimple_mempcpy(void *__d, const void *__s, size_t __n) #ifndef strdupa # if defined(__GNUC__) || defined(__clang__) -# define memdupa(s, n)\ +# define memdupa(s, n) /* TODO test */\ ({\ const char *__s = (s);\ size_t __n = (n);\ @@ -621,7 +621,7 @@ int libsimple_vasprintf(char **, const char *, va_list); #ifndef asprintfa # if defined(__GNUC__) && !defined(__clang__) -# define asprintfa(__fmt, ...)\ +# define asprintfa(__fmt, ...) /* TODO test */\ ({\ const char *__f = (__fmt);\ char *__ret = NULL;\ @@ -643,7 +643,7 @@ int libsimple_vasprintf(char **, const char *, va_list); #ifndef vasprintfa # if defined(__GNUC__) || defined(__clang__) -# define vasprintfa(__fmt, __ap)\ +# define vasprintfa(__fmt, __ap) /* TODO test */\ ({\ const char *__f = (__fmt);\ va_list __a = (__ap);\ @@ -723,29 +723,29 @@ char *libsimple_strcasestr(const char *, const char *); _LIBSIMPLE_GCC_ONLY(__attribute__((__pure__, __nonnull__, __warn_unused_result__))) -static inline int streq(const char *__a, const char *__b) { return !strcmp(__a, __b); } +static inline int streq(const char *__a, const char *__b) { return !strcmp(__a, __b); } /* TODO test */ _LIBSIMPLE_GCC_ONLY(__attribute__((__pure__, __nonnull__, __warn_unused_result__))) -static inline int strneq(const char *__a, const char *__b, size_t __n) { return !strncmp(__a, __b, __n); } +static inline int strneq(const char *__a, const char *__b, size_t __n) { return !strncmp(__a, __b, __n); } /* TODO test */ -#define malloczn(CLEAR, ...) _libsimple_malloczn((CLEAR), __VA_ARGS__, (size_t)0) -#define mallocn(...) malloczn(0, __VA_ARGS__) -#define callocn(...) malloczn(1, __VA_ARGS__) +#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 */ _LIBSIMPLE_GCC_ONLY(__attribute__((__warn_unused_result__))) void *libsimple_vmalloczn(int, size_t, va_list); _LIBSIMPLE_GCC_ONLY(__attribute__((__warn_unused_result__))) -static inline void *libsimple_vmallocn(size_t __n, va_list __ap) { return libsimple_vmalloczn(0, __n, __ap); } +static inline void *libsimple_vmallocn(size_t __n, va_list __ap) { return libsimple_vmalloczn(0, __n, __ap); } /* TODO test */ _LIBSIMPLE_GCC_ONLY(__attribute__((__warn_unused_result__))) -static inline void *libsimple_vcallocn(size_t __n, va_list __ap) { return libsimple_vmalloczn(1, __n, __ap); } +static inline void *libsimple_vcallocn(size_t __n, va_list __ap) { return libsimple_vmalloczn(1, __n, __ap); } /* TODO test */ _LIBSIMPLE_GCC_ONLY(__attribute__((__warn_unused_result__))) static inline void * -libsimple_malloczn(int __clear, size_t __n, ...) +libsimple_malloczn(int __clear, size_t __n, ...) /* TODO test */ { va_list __ap; va_start(__ap, __n); @@ -755,7 +755,7 @@ libsimple_malloczn(int __clear, size_t __n, ...) _LIBSIMPLE_GCC_ONLY(__attribute__((__warn_unused_result__))) static inline void * -libsimple_mallocn(size_t __n, ...) +libsimple_mallocn(size_t __n, ...) /* TODO test */ { va_list __ap; va_start(__ap, __n); @@ -765,7 +765,7 @@ libsimple_mallocn(size_t __n, ...) _LIBSIMPLE_GCC_ONLY(__attribute__((__warn_unused_result__))) static inline void * -libsimple_callocn(size_t __n, ...) +libsimple_callocn(size_t __n, ...) /* TODO test */ { va_list __ap; va_start(__ap, __n); @@ -774,13 +774,13 @@ libsimple_callocn(size_t __n, ...) } -#define reallocn(PTR, ...) _libsimple_reallocn((PTR), __VA_ARGS__, (size_t)0) +#define reallocn(PTR, ...) _libsimple_reallocn((PTR), __VA_ARGS__, (size_t)0) /* TODO test */ _LIBSIMPLE_GCC_ONLY(__attribute__((__warn_unused_result__))) void *libsimple_vreallocn(void *, size_t, va_list); _LIBSIMPLE_GCC_ONLY(__attribute__((__warn_unused_result__))) static inline void * -libsimple_reallocn(void *__ptr, size_t __n, ...) +libsimple_reallocn(void *__ptr, size_t __n, ...) /* TODO test */ { va_list __ap; va_start(__ap, __n); @@ -807,10 +807,10 @@ char *enstrndup(int, const char *, size_t); _LIBSIMPLE_GCC_ONLY(__attribute__((__warn_unused_result__, __returns_nonnull__))) void *enmemdup(int, const void *, size_t); -#define enmalloczn(STATUS, CLEAR, ...) _libsimple_enmalloczn((STATUS), (CLEAR), __VA_ARGS__, (size_t)0) -#define enmallocn(STATUS, ...) _libsimple_enmallocn((STATUS), (CLEAR), __VA_ARGS__, (size_t)0) -#define encallocn(STATUS, ...) _libsimple_encallocn((STATUS), (CLEAR), __VA_ARGS__, (size_t)0) -#define enreallocn(STATUS, PTR, ...) _libsimple_enreallocn((STATUS), (PTR), __VA_ARGS__, (size_t)0) +#define enmalloczn(STATUS, CLEAR, ...) _libsimple_enmalloczn((STATUS), (CLEAR), __VA_ARGS__, (size_t)0) /* TODO test */ +#define enmallocn(STATUS, ...) _libsimple_enmallocn((STATUS), (CLEAR), __VA_ARGS__, (size_t)0) /* TODO test */ +#define encallocn(STATUS, ...) _libsimple_encallocn((STATUS), (CLEAR), __VA_ARGS__, (size_t)0) /* TODO test */ +#define enreallocn(STATUS, PTR, ...) _libsimple_enreallocn((STATUS), (PTR), __VA_ARGS__, (size_t)0) /* TODO test */ _LIBSIMPLE_GCC_ONLY(__attribute__((__warn_unused_result__, __returns_nonnull__))) void *libsimple_envmalloczn(int, int, size_t, va_list); @@ -819,14 +819,14 @@ _LIBSIMPLE_GCC_ONLY(__attribute__((__warn_unused_result__, __returns_nonnull__)) void *libsimple_envreallocn(int, void *, size_t, va_list); _LIBSIMPLE_GCC_ONLY(__attribute__((__warn_unused_result__, __returns_nonnull__))) -static inline void *libsimple_envmallocn(int __st, size_t __n, va_list __ap) { return libsimple_envmalloczn(__st, 0, __n, __ap); } +static inline void *libsimple_envmallocn(int __st, size_t __n, va_list __ap) { return libsimple_envmalloczn(__st, 0, __n, __ap); } /* TODO test */ _LIBSIMPLE_GCC_ONLY(__attribute__((__warn_unused_result__, __returns_nonnull__))) -static inline void *libsimple_envcallocn(int __st, size_t __n, va_list __ap) { return libsimple_envmalloczn(__st, 1, __n, __ap); } +static inline void *libsimple_envcallocn(int __st, size_t __n, va_list __ap) { return libsimple_envmalloczn(__st, 1, __n, __ap); } /* TODO test */ _LIBSIMPLE_GCC_ONLY(__attribute__((__warn_unused_result__, __returns_nonnull__))) static inline void * -libsimple_enmalloczn(int __status, int __clear, size_t __n, ...) +libsimple_enmalloczn(int __status, int __clear, size_t __n, ...) /* TODO test */ { va_list __ap; va_start(__ap, __n); @@ -836,7 +836,7 @@ libsimple_enmalloczn(int __status, int __clear, size_t __n, ...) _LIBSIMPLE_GCC_ONLY(__attribute__((__warn_unused_result__, __returns_nonnull__))) static inline void * -libsimple_enmallocn(int __status, size_t __n, ...) +libsimple_enmallocn(int __status, size_t __n, ...) /* TODO test */ { va_list __ap; va_start(__ap, __n); @@ -846,7 +846,7 @@ libsimple_enmallocn(int __status, size_t __n, ...) _LIBSIMPLE_GCC_ONLY(__attribute__((__warn_unused_result__, __returns_nonnull__))) static inline void * -libsimple_encallocn(int __status, size_t __n, ...) +libsimple_encallocn(int __status, size_t __n, ...) /* TODO test */ { va_list __ap; va_start(__ap, __n); @@ -856,7 +856,7 @@ libsimple_encallocn(int __status, size_t __n, ...) _LIBSIMPLE_GCC_ONLY(__attribute__((__warn_unused_result__, __returns_nonnull__))) static inline void * -libsimple_enreallocn(int __status, void *__ptr, size_t __n, ...) +libsimple_enreallocn(int __status, void *__ptr, size_t __n, ...) /* TODO test */ { va_list __ap; va_start(__ap, __n); @@ -866,47 +866,47 @@ libsimple_enreallocn(int __status, void *__ptr, size_t __n, ...) _LIBSIMPLE_GCC_ONLY(__attribute__((__warn_unused_result__, __returns_nonnull__))) -static inline void *emalloc(size_t __n) { return enmalloc(libsimple_default_failure_exit, __n); } +static inline void *emalloc(size_t __n) { return enmalloc(libsimple_default_failure_exit, __n); } /* TODO test */ _LIBSIMPLE_GCC_ONLY(__attribute__((__warn_unused_result__, __returns_nonnull__))) -static inline void *ecalloc(size_t __n, size_t __m) { return encalloc(libsimple_default_failure_exit, __n, __m); } +static inline void *ecalloc(size_t __n, size_t __m) { return encalloc(libsimple_default_failure_exit, __n, __m); } /* TODO test */ _LIBSIMPLE_GCC_ONLY(__attribute__((__warn_unused_result__, __returns_nonnull__))) -static inline void *erealloc(void *__ptr, size_t __n) { return enrealloc(libsimple_default_failure_exit, __ptr, __n); } +static inline void *erealloc(void *__ptr, size_t __n) { return enrealloc(libsimple_default_failure_exit, __ptr, __n); } /* TODO test */ _LIBSIMPLE_GCC_ONLY(__attribute__((nonnull, __warn_unused_result__, __returns_nonnull__))) -static inline char *estrdup(const char *__s) { return enstrdup(libsimple_default_failure_exit, __s); } +static inline char *estrdup(const char *__s) { return enstrdup(libsimple_default_failure_exit, __s); } /* TODO test */ _LIBSIMPLE_GCC_ONLY(__attribute__((nonnull, __warn_unused_result__, __returns_nonnull__))) -static inline char *estrndup(const char *__s, size_t __n) { return enstrndup(libsimple_default_failure_exit, __s, __n); } +static inline char *estrndup(const char *__s, size_t __n) { return enstrndup(libsimple_default_failure_exit, __s, __n); } /* TODO test */ _LIBSIMPLE_GCC_ONLY(__attribute__((__warn_unused_result__, __returns_nonnull__))) -static inline void *ememdup(const void *__s, size_t __n) { return enmemdup(libsimple_default_failure_exit, __s, __n); } +static inline void *ememdup(const void *__s, size_t __n) { return enmemdup(libsimple_default_failure_exit, __s, __n); } /* TODO test */ -#define emalloczn(CLEAR, ...) enmalloczn(libsimple_default_failure_exit, (CLEAR), __VA_ARGS__) -#define emallocn(...) enmallocn(libsimple_default_failure_exit, __VA_ARGS__) -#define ecallocn(...) encallocn(libsimple_default_failure_exit, __VA_ARGS__) -#define ereallocn(PTR, ...) enreallocn(libsimple_default_failure_exit, (PTR), __VA_ARGS__) +#define emalloczn(CLEAR, ...) enmalloczn(libsimple_default_failure_exit, (CLEAR), __VA_ARGS__) /* TODO test */ +#define emallocn(...) enmallocn(libsimple_default_failure_exit, __VA_ARGS__) /* TODO test */ +#define ecallocn(...) encallocn(libsimple_default_failure_exit, __VA_ARGS__) /* TODO test */ +#define ereallocn(PTR, ...) enreallocn(libsimple_default_failure_exit, (PTR), __VA_ARGS__) /* TODO test */ _LIBSIMPLE_GCC_ONLY(__attribute__((__warn_unused_result__, __returns_nonnull__))) -static inline void *libsimple_evmalloczn(int __clear, size_t __n, va_list __ap) +static inline void *libsimple_evmalloczn(int __clear, size_t __n, va_list __ap) /* TODO test */ { return libsimple_envmalloczn(libsimple_default_failure_exit, __clear, __n, __ap); } _LIBSIMPLE_GCC_ONLY(__attribute__((__warn_unused_result__, __returns_nonnull__))) -static inline void *libsimple_evmallocn(size_t __n, va_list __ap) +static inline void *libsimple_evmallocn(size_t __n, va_list __ap) /* TODO test */ { return libsimple_envcallocn(libsimple_default_failure_exit, __n, __ap); } _LIBSIMPLE_GCC_ONLY(__attribute__((__warn_unused_result__, __returns_nonnull__))) -static inline void *libsimple_evcallocn(size_t __n, va_list __ap) +static inline void *libsimple_evcallocn(size_t __n, va_list __ap) /* TODO test */ { return libsimple_envmallocn(libsimple_default_failure_exit, __n, __ap); } _LIBSIMPLE_GCC_ONLY(__attribute__((__warn_unused_result__, __returns_nonnull__))) -static inline void *libsimple_evreallocn(void *__ptr, size_t __n, va_list __ap) +static inline void *libsimple_evreallocn(void *__ptr, size_t __n, va_list __ap) /* TODO test */ { return libsimple_envreallocn(libsimple_default_failure_exit, __ptr, __n, __ap); } _LIBSIMPLE_GCC_ONLY(__attribute__((__warn_unused_result__, __returns_nonnull__))) static inline void * -libsimple_emalloczn(int __c, size_t __n, ...) +libsimple_emalloczn(int __c, size_t __n, ...) /* TODO test */ { va_list __ap; va_start(__ap, __n); @@ -916,7 +916,7 @@ libsimple_emalloczn(int __c, size_t __n, ...) _LIBSIMPLE_GCC_ONLY(__attribute__((__warn_unused_result__, __returns_nonnull__))) static inline void * -libsimple_emallocn(size_t __n, ...) +libsimple_emallocn(size_t __n, ...) /* TODO test */ { va_list __ap; va_start(__ap, __n); @@ -926,7 +926,7 @@ libsimple_emallocn(size_t __n, ...) _LIBSIMPLE_GCC_ONLY(__attribute__((__warn_unused_result__, __returns_nonnull__))) static inline void * -libsimple_ecallocn(size_t __n, ...) +libsimple_ecallocn(size_t __n, ...) /* TODO test */ { va_list __ap; va_start(__ap, __n); @@ -936,7 +936,7 @@ libsimple_ecallocn(size_t __n, ...) _LIBSIMPLE_GCC_ONLY(__attribute__((__warn_unused_result__, __returns_nonnull__))) static inline void * -libsimple_ereallocn(void *__p, size_t __n, ...) +libsimple_ereallocn(void *__p, size_t __n, ...) /* TODO test */ { va_list __ap; va_start(__ap, __n); @@ -953,7 +953,7 @@ libsimple_ereallocn(void *__p, size_t __n, ...) */ _LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__))) static inline char * -getenv_ne(const char *__name) +getenv_ne(const char *__name) /* TODO test */ { char *__env = getenv(__name); return (__env && *__env) ? __env : NULL; @@ -968,7 +968,7 @@ void envputenvf(int, const char *, va_list); _LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__, __format__(__printf__, 1, 2)))) static inline int -putenvf(const char *__fmt, ...) +putenvf(const char *__fmt, ...) /* TODO test */ { va_list __ap; va_start(__ap, __fmt); @@ -978,7 +978,7 @@ putenvf(const char *__fmt, ...) _LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__, __format__(__printf__, 1, 2)))) static inline void -eputenvf(const char *__fmt, ...) +eputenvf(const char *__fmt, ...) /* TODO test */ { va_list __ap; va_start(__ap, __fmt); @@ -988,14 +988,14 @@ eputenvf(const char *__fmt, ...) _LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__))) static inline void -evputenvf(const char *__fmt, va_list __ap) +evputenvf(const char *__fmt, va_list __ap) /* TODO test */ { envputenvf(libsimple_default_failure_exit, __fmt, __ap); } _LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__, __format__(__printf__, 2, 3)))) static inline void -enputenvf(int __status, const char *__fmt, ...) +enputenvf(int __status, const char *__fmt, ...) /* TODO test */ { va_list __ap; va_start(__ap, __fmt); @@ -1010,7 +1010,7 @@ void vweprintf(const char *, va_list); _LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__(1), __format__(__printf__, 1, 2), __noreturn__))) static inline void -eprintf(const char *__fmt, ...) +eprintf(const char *__fmt, ...) /* TODO test */ { va_list __ap; va_start(__ap, __fmt); @@ -1021,7 +1021,7 @@ eprintf(const char *__fmt, ...) _LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__(1), __noreturn__))) static inline void -veprintf(const char *__fmt, va_list __ap) +veprintf(const char *__fmt, va_list __ap) /* TODO test */ { vweprintf(__fmt, __ap); exit(libsimple_default_failure_exit); @@ -1029,7 +1029,7 @@ veprintf(const char *__fmt, va_list __ap) _LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__(2), __format__(__printf__, 2, 3), __noreturn__))) static inline void -enprintf(int __status, const char *__fmt, ...) +enprintf(int __status, const char *__fmt, ...) /* TODO test */ { va_list __ap; va_start(__ap, __fmt); @@ -1040,7 +1040,7 @@ enprintf(int __status, const char *__fmt, ...) _LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__(2), __noreturn__))) static inline void -venprintf(int __status, const char *__fmt, va_list __ap) +venprintf(int __status, const char *__fmt, va_list __ap) /* TODO test */ { vweprintf(__fmt, __ap); exit(__status); @@ -1048,7 +1048,7 @@ venprintf(int __status, const char *__fmt, va_list __ap) _LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__(1), __format__(__printf__, 1, 2)))) static inline void -weprintf(const char *__fmt, ...) +weprintf(const char *__fmt, ...) /* TODO test */ { va_list __ap; va_start(__ap, __fmt); @@ -1081,7 +1081,7 @@ ssize_t libsimple_recvfrom_timestamped(int, void *restrict, size_t, int, struct _LIBSIMPLE_GCC_ONLY(__attribute__((__warn_unused_result__))) static inline ssize_t -libsimple_recv_timestamped(int __fd, void *restrict __buf, size_t __n, +libsimple_recv_timestamped(int __fd, void *restrict __buf, size_t __n, /* TODO test */ int __flags, struct timespec *restrict __ts) { return libsimple_recvfrom_timestamped(__fd, __buf, __n, __flags, NULL, 0, __ts); @@ -1114,7 +1114,7 @@ int libsimple_multimespec(struct timespec *, const struct timespec *, int); _LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__, __warn_unused_result__))) static inline int -libsimple_cmptimespec(const struct timespec *__a, const struct timespec *__b) +libsimple_cmptimespec(const struct timespec *__a, const struct timespec *__b) /* TODO test */ { if (__a->tv_sec != __b->tv_sec) return __a->tv_sec < __b->tv_sec ? -1 : +1; @@ -1148,7 +1148,7 @@ int libsimple_multimeval(struct timeval *, const struct timeval *, int); _LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__, __warn_unused_result__))) static inline int -libsimple_cmptimeval(const struct timeval *__a, const struct timeval *__b) +libsimple_cmptimeval(const struct timeval *__a, const struct timeval *__b) /* TODO test */ { if (__a->tv_sec != __b->tv_sec) return __a->tv_sec < __b->tv_sec ? -1 : +1; @@ -1161,7 +1161,7 @@ libsimple_cmptimeval(const struct timeval *__a, const struct timeval *__b) _LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__))) static inline void -libsimple_timeval2timespec(struct timespec *restrict __ts, const struct timeval *restrict __tv) +libsimple_timeval2timespec(struct timespec *restrict __ts, const struct timeval *restrict __tv) /* TODO test */ { __ts->tv_sec = __tv->tv_sec; __ts->tv_nsec = __tv->tv_usec; @@ -1209,7 +1209,7 @@ char *libsimple_timevaltostr(char *restrict, const struct timeval *restrict); _LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__))) static inline double -libsimple_timespectodouble(const struct timespec *__ts) +libsimple_timespectodouble(const struct timespec *__ts) /* TODO test */ { double __ret = (double)(__ts->tv_nsec); __ret /= (double)1000000000L; @@ -1223,7 +1223,7 @@ libsimple_timespectodouble(const struct timespec *__ts) _LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__))) static inline double -libsimple_timevaltodouble(const struct timeval *__tv) +libsimple_timevaltodouble(const struct timeval *__tv) /* TODO test */ { double __ret = (double)(__tv->tv_usec); __ret /= (double)1000000L; @@ -1249,15 +1249,15 @@ void libsimple_doubletotimeval(struct timeval *, double); #endif -#define LIBSIMPLE_UNLIST(LIST, I, NP) libsimple_unlist((LIST), (I), (NP), sizeof(*(LIST))) +#define LIBSIMPLE_UNLIST(LIST, I, NP) libsimple_unlist((LIST), (I), (NP), sizeof(*(LIST))) /* TODO test */ #ifndef UNLIST -# define UNLIST(LIST, I, NP) libsimple_unlist((LIST), (I), (NP), sizeof(*(LIST))) +# define UNLIST(LIST, I, NP) LIBSIMPLE_UNLIST((LIST), (I), (NP)) #endif _LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__))) static inline void -libsimple_unlist(void *__list, size_t __i, size_t *__np, size_t __width) +libsimple_unlist(void *__list, size_t __i, size_t *__np, size_t __width) /* TODO test */ { char *__lst = __list; memmove(&__lst[__i * __width], &__lst[(__i + 1) * __width], (--*__np - __i) * __width); @@ -10,3 +10,23 @@ libsimple_memdup(const void *s, size_t n) return NULL; return memcpy(ret, s, n); } + + +#ifdef TEST +#include <assert.h> + +int +main(void) +{ + const char *s = "test"; + void *p = libsimple_memdup(s, 5); + assert(p); + assert(p != s); + assert(!strcmp(p, s)); + memset(p, 0, 5); + assert(!strcmp(s, "test")); + free(p); + return 0; +} + +#endif @@ -13,3 +13,46 @@ libsimple_memends(const void *s_, size_t n, const void *t_, size_t m) return 0; return 1; } + + +#ifdef TEST +#include <assert.h> + +int +main(void) +{ + assert(libsimple_memends("", 0, "", 0) == 1); + assert(libsimple_memends("test", 4, "", 0) == 1); + assert(libsimple_memends("", 0, "test", 4) == 0); + assert(libsimple_memends("test", 4, "test", 4) == 1); + assert(libsimple_memends("test", 4, "test", 5) == 0); + assert(libsimple_memends("test", 4, "testx", 5) == 0); + assert(libsimple_memends("test", 4, "est", 3) == 1); + assert(libsimple_memends("test", 4, "st", 2) == 1); + assert(libsimple_memends("test", 4, "t", 1) == 1); + assert(libsimple_memends("test", 4, "x", 1) == 0); + assert(libsimple_memends("test", 4, "xx", 2) == 0); + + assert(libsimple_memends("TEST", 4, "", 0) == 1); + assert(libsimple_memends("TEST", 4, "test", 4) == 0); + assert(libsimple_memends("TEST", 4, "test", 5) == 0); + assert(libsimple_memends("TEST", 4, "testx", 5) == 0); + assert(libsimple_memends("TEST", 4, "est", 3) == 0); + assert(libsimple_memends("TEST", 4, "st", 2) == 0); + assert(libsimple_memends("TEST", 4, "t", 1) == 0); + assert(libsimple_memends("TEST", 4, "x", 1) == 0); + assert(libsimple_memends("TEST", 4, "xx", 2) == 0); + + assert(libsimple_memends("", 0, "TEST", 4) == 0); + assert(libsimple_memends("test", 4, "TEST", 4) == 0); + assert(libsimple_memends("test", 4, "TEST", 5) == 0); + assert(libsimple_memends("test", 4, "TESTX", 5) == 0); + assert(libsimple_memends("test", 4, "EST", 3) == 0); + assert(libsimple_memends("test", 4, "ST", 2) == 0); + assert(libsimple_memends("test", 4, "T", 1) == 0); + assert(libsimple_memends("test", 4, "X", 1) == 0); + assert(libsimple_memends("test", 4, "XX", 2) == 0); + return 0; +} + +#endif @@ -21,3 +21,34 @@ libsimple_memmem(const void *hay_, size_t hayn, const void *sub_, size_t subn) return NULL; } + + +#ifdef TEST +#include <assert.h> + +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(!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(!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(!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(!libsimple_memmem("abc", 3, "abc", 4)); + assert(!strcmp(libsimple_memmem("abcabc", 6, "bc", 2), "bcabc")); + return 0; +} + +#endif @@ -10,3 +10,18 @@ libsimple_memrchr(const void *s_, int c, size_t n_) while (n-- && (int)s[n] != c); return n < 0 ? NULL : &s[n]; } + + +#ifdef TEST +#include <assert.h> + +int +main(void) +{ + assert(!strcmp(libsimple_memrchr("1234512345", '3', sizeof("1234512345") - 1), "345")); + assert(!strcmp(libsimple_memrchr("1234512345", '5', sizeof("1234512345") - 1), "5")); + assert(!libsimple_memrchr("1234512345", 'x', sizeof("1234512345") - 1)); + return 0; +} + +#endif diff --git a/memstarts.c b/memstarts.c index 1dd1e6a..05c8b39 100644 --- a/memstarts.c +++ b/memstarts.c @@ -12,3 +12,46 @@ libsimple_memstarts(const void *s_, size_t n, const void *t_, size_t m) while (i < m && s[i] == t[i]) i++; return i == m; } + + +#ifdef TEST +#include <assert.h> + +int +main(void) +{ + assert(libsimple_memstarts("", 0, "", 0) == 1); + assert(libsimple_memstarts("test", 4, "", 0) == 1); + assert(libsimple_memstarts("", 0, "test", 4) == 0); + assert(libsimple_memstarts("test", 4, "test", 4) == 1); + assert(libsimple_memstarts("test", 4, "test", 5) == 0); + assert(libsimple_memstarts("test", 4, "testx", 5) == 0); + assert(libsimple_memstarts("test", 4, "tes", 3) == 1); + assert(libsimple_memstarts("test", 4, "te", 2) == 1); + assert(libsimple_memstarts("test", 4, "t", 1) == 1); + 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, "test", 4) == 0); + assert(libsimple_memstarts("TEST", 4, "test", 5) == 0); + assert(libsimple_memstarts("TEST", 4, "testx", 5) == 0); + assert(libsimple_memstarts("TEST", 4, "tes", 3) == 0); + assert(libsimple_memstarts("TEST", 4, "te", 2) == 0); + assert(libsimple_memstarts("TEST", 4, "t", 1) == 0); + assert(libsimple_memstarts("TEST", 4, "x", 1) == 0); + assert(libsimple_memstarts("TEST", 4, "xx", 2) == 0); + + assert(libsimple_memstarts("", 0, "TEST", 4) == 0); + assert(libsimple_memstarts("test", 4, "TEST", 4) == 0); + assert(libsimple_memstarts("test", 4, "TEST", 5) == 0); + assert(libsimple_memstarts("test", 4, "TESTX", 5) == 0); + assert(libsimple_memstarts("test", 4, "TES", 3) == 0); + assert(libsimple_memstarts("test", 4, "TE", 2) == 0); + assert(libsimple_memstarts("test", 4, "T", 1) == 0); + assert(libsimple_memstarts("test", 4, "X", 1) == 0); + assert(libsimple_memstarts("test", 4, "XX", 2) == 0); + return 0; +} + +#endif diff --git a/multimespec.c b/multimespec.c index 2e1c2a9..ec60c1e 100644 --- a/multimespec.c +++ b/multimespec.c @@ -3,7 +3,7 @@ int -libsimple_multimespec(struct timespec *prod, const struct timespec *multiplicand, int multiplier) +libsimple_multimespec(struct timespec *prod, const struct timespec *multiplicand, int multiplier) /* TODO test */ { time_t s = multiplicand->tv_sec; long long int ns = (long long int)(multiplicand->tv_nsec); diff --git a/multimeval.c b/multimeval.c index 10b8be9..bb8c951 100644 --- a/multimeval.c +++ b/multimeval.c @@ -3,7 +3,7 @@ int -libsimple_multimeval(struct timeval *prod, const struct timeval *multiplicand, int multiplier) +libsimple_multimeval(struct timeval *prod, const struct timeval *multiplicand, int multiplier) /* TODO test */ { struct timespec a, p; int r; diff --git a/rawmemchr.c b/rawmemchr.c index 6dd8496..57f584d 100644 --- a/rawmemchr.c +++ b/rawmemchr.c @@ -9,3 +9,17 @@ libsimple_rawmemchr(const void *s_, int c) while ((int)*s++ != c); return &s[-1]; } + + +#ifdef TEST +#include <assert.h> + +int +main(void) +{ + assert(!strcmp(libsimple_rawmemchr("1234512345", '3'), "34512345")); + assert(!strcmp(libsimple_rawmemchr("1234512345", '1'), "1234512345")); + return 0; +} + +#endif diff --git a/rawmemrchr.c b/rawmemrchr.c index 5ce374d..3985840 100644 --- a/rawmemrchr.c +++ b/rawmemrchr.c @@ -9,3 +9,17 @@ libsimple_rawmemrchr(const void *s_, int c, size_t n) while ((int)s[--n] != c); return &s[n]; } + + +#ifdef TEST +#include <assert.h> + +int +main(void) +{ + assert(!strcmp(libsimple_rawmemrchr("1234512345", '3', sizeof("1234512345") - 1), "345")); + assert(!strcmp(libsimple_rawmemrchr("1234512345", '5', sizeof("1234512345") - 1), "5")); + return 0; +} + +#endif @@ -3,7 +3,7 @@ int -libsimple_recvfd(int sock) +libsimple_recvfd(int sock) /* TODO test */ { int fd; char buf[1]; diff --git a/recvfrom_timestamped.c b/recvfrom_timestamped.c index 6dd43b0..f5f8e53 100644 --- a/recvfrom_timestamped.c +++ b/recvfrom_timestamped.c @@ -3,7 +3,7 @@ ssize_t -libsimple_recvfrom_timestamped(int fd, void *restrict buf, size_t n, int flags, struct sockaddr *restrict addr, +libsimple_recvfrom_timestamped(int fd, void *restrict buf, size_t n, int flags, struct sockaddr *restrict addr, /* TODO test */ socklen_t addrlen, struct timespec *restrict ts) { struct iovec iov; @@ -3,7 +3,7 @@ int -libsimple_sendfd(int sock, int fd) +libsimple_sendfd(int sock, int fd) /* TODO test */ { char buf[1]; struct iovec iov; diff --git a/strcaseends.c b/strcaseends.c index a672b0a..6ff9d8b 100644 --- a/strcaseends.c +++ b/strcaseends.c @@ -11,3 +11,43 @@ libsimple_strcaseends(const char *s, const char *t) return 0; return !strcasecmp(&s[sn - tn], t); } + + +#ifdef TEST +#include <assert.h> + +int +main(void) +{ + assert(libsimple_strcaseends("", "") == 1); + assert(libsimple_strcaseends("test", "") == 1); + assert(libsimple_strcaseends("", "test") == 0); + assert(libsimple_strcaseends("test", "test") == 1); + assert(libsimple_strcaseends("test", "testx") == 0); + assert(libsimple_strcaseends("test", "est") == 1); + assert(libsimple_strcaseends("test", "st") == 1); + assert(libsimple_strcaseends("test", "t") == 1); + assert(libsimple_strcaseends("test", "x") == 0); + assert(libsimple_strcaseends("test", "xx") == 0); + + assert(libsimple_strcaseends("TEST", "") == 1); + assert(libsimple_strcaseends("TEST", "test") == 1); + assert(libsimple_strcaseends("TEST", "testx") == 0); + assert(libsimple_strcaseends("TEST", "est") == 1); + assert(libsimple_strcaseends("TEST", "st") == 1); + assert(libsimple_strcaseends("TEST", "t") == 1); + assert(libsimple_strcaseends("TEST", "x") == 0); + assert(libsimple_strcaseends("TEST", "xx") == 0); + + assert(libsimple_strcaseends("", "TEST") == 0); + assert(libsimple_strcaseends("test", "TEST") == 1); + assert(libsimple_strcaseends("test", "TESTX") == 0); + assert(libsimple_strcaseends("test", "EST") == 1); + assert(libsimple_strcaseends("test", "ST") == 1); + assert(libsimple_strcaseends("test", "T") == 1); + assert(libsimple_strcaseends("test", "X") == 0); + assert(libsimple_strcaseends("test", "XX") == 0); + return 0; +} + +#endif diff --git a/strcasestr.c b/strcasestr.c index 62bcfdb..a3a98fc 100644 --- a/strcasestr.c +++ b/strcasestr.c @@ -15,3 +15,61 @@ libsimple_strcasestr(const char *h_, const char *n) return h; return NULL; } + + +#ifdef TEST +#include <assert.h> + +int +main(void) +{ + assert(!strcmp(libsimple_strcasestr("", ""), "")); + assert(!strcmp(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(!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(!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(!strcmp(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(!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(!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(!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(!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(!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")); + return 0; +} + +#endif diff --git a/strchrnul.c b/strchrnul.c index e76978f..893ff34 100644 --- a/strchrnul.c +++ b/strchrnul.c @@ -9,3 +9,18 @@ libsimple_strchrnul(const char *s_, int c) for (; *s && (int)*s != c; s++); return s; } + + +#ifdef TEST +#include <assert.h> + +int +main(void) +{ + assert(!strcmp(libsimple_strchrnul("1234512345", '3'), "34512345")); + assert(!strcmp(libsimple_strchrnul("1234512345", '1'), "1234512345")); + assert(!strcmp(libsimple_strchrnul("1234512345", 'x'), "")); + return 0; +} + +#endif @@ -7,3 +7,43 @@ libsimple_strends(const char *s, const char *t) { return memends(s, strlen(s), t, strlen(t)); } + + +#ifdef TEST +#include <assert.h> + +int +main(void) +{ + assert(libsimple_strends("", "") == 1); + assert(libsimple_strends("test", "") == 1); + assert(libsimple_strends("", "test") == 0); + assert(libsimple_strends("test", "test") == 1); + assert(libsimple_strends("test", "testx") == 0); + assert(libsimple_strends("test", "est") == 1); + assert(libsimple_strends("test", "st") == 1); + assert(libsimple_strends("test", "t") == 1); + assert(libsimple_strends("test", "x") == 0); + assert(libsimple_strends("test", "xx") == 0); + + assert(libsimple_strends("TEST", "") == 1); + assert(libsimple_strends("TEST", "test") == 0); + assert(libsimple_strends("TEST", "testx") == 0); + assert(libsimple_strends("TEST", "est") == 0); + assert(libsimple_strends("TEST", "st") == 0); + assert(libsimple_strends("TEST", "t") == 0); + assert(libsimple_strends("TEST", "x") == 0); + assert(libsimple_strends("TEST", "xx") == 0); + + assert(libsimple_strends("", "TEST") == 0); + assert(libsimple_strends("test", "TEST") == 0); + assert(libsimple_strends("test", "TESTX") == 0); + assert(libsimple_strends("test", "EST") == 0); + assert(libsimple_strends("test", "ST") == 0); + assert(libsimple_strends("test", "T") == 0); + assert(libsimple_strends("test", "X") == 0); + assert(libsimple_strends("test", "XX") == 0); + return 0; +} + +#endif @@ -3,9 +3,11 @@ char * -libsimple_strndup(const char *s, size_t n) +libsimple_strndup(const char *s, size_t n) /* FIXME */ { - void *ret; + char *ret; + size_t m = strlen(s); + n = MIN(n, m); if (n == SIZE_MAX) { errno = ENOMEM; return NULL; @@ -13,6 +15,42 @@ libsimple_strndup(const char *s, size_t n) if (!(ret = malloc(n + 1))) return NULL; memcpy(ret, s, n); - ((char *)ret)[n] = '\0'; + ret[n] = '\0'; return ret; } + + +#ifdef TEST +#include <assert.h> + +int +main(void) +{ + const char *s = "test"; + void *p; + + p = libsimple_memdup(s, 5); + assert(p && p != s); + assert(!strcmp(p, "test")); + memset(p, 0, 5); + assert(!strcmp(s, "test")); + free(p); + + p = libsimple_memdup(s, 3); + assert(p && p != s); + assert(!strcmp(p, "tes")); + memset(p, 0, 5); + assert(!strcmp(s, "test")); + free(p); + + p = libsimple_memdup(s, 0); + assert(p && p != s); + assert(!strcmp(p, "")); + memset(p, 0, 5); + assert(!strcmp(s, "test")); + free(p); + + return 0; +} + +#endif diff --git a/strstarts.c b/strstarts.c index ca44e52..f2db947 100644 --- a/strstarts.c +++ b/strstarts.c @@ -8,3 +8,43 @@ libsimple_strstarts(const char *s, const char *t) for (; *t && *s == *t; s++, t++); return !*t; } + + +#ifdef TEST +#include <assert.h> + +int +main(void) +{ + assert(libsimple_strstarts("", "") == 1); + assert(libsimple_strstarts("test", "") == 1); + assert(libsimple_strstarts("", "test") == 0); + assert(libsimple_strstarts("test", "test") == 1); + assert(libsimple_strstarts("test", "testx") == 0); + assert(libsimple_strstarts("test", "tes") == 1); + assert(libsimple_strstarts("test", "te") == 1); + assert(libsimple_strstarts("test", "t") == 1); + assert(libsimple_strstarts("test", "x") == 0); + assert(libsimple_strstarts("test", "xx") == 0); + + assert(libsimple_strstarts("TEST", "") == 1); + assert(libsimple_strstarts("TEST", "test") == 0); + assert(libsimple_strstarts("TEST", "testx") == 0); + assert(libsimple_strstarts("TEST", "tes") == 0); + assert(libsimple_strstarts("TEST", "te") == 0); + assert(libsimple_strstarts("TEST", "t") == 0); + assert(libsimple_strstarts("TEST", "x") == 0); + assert(libsimple_strstarts("TEST", "xx") == 0); + + assert(libsimple_strstarts("", "TEST") == 0); + assert(libsimple_strstarts("test", "TEST") == 0); + assert(libsimple_strstarts("test", "TESTX") == 0); + assert(libsimple_strstarts("test", "TES") == 0); + assert(libsimple_strstarts("test", "TE") == 0); + assert(libsimple_strstarts("test", "T") == 0); + assert(libsimple_strstarts("test", "X") == 0); + assert(libsimple_strstarts("test", "XX") == 0); + return 0; +} + +#endif diff --git a/strtotimespec.c b/strtotimespec.c index b8a7489..1e6e8d1 100644 --- a/strtotimespec.c +++ b/strtotimespec.c @@ -3,7 +3,7 @@ int -libsimple_strtotimespec(struct timespec *restrict ts, const char *restrict s, char **restrict end) +libsimple_strtotimespec(struct timespec *restrict ts, const char *restrict s, char **restrict end) /* TODO test */ { int neg = 0, bracket = 0; time_t sec = 0; diff --git a/strtotimeval.c b/strtotimeval.c index e210e87..4df5674 100644 --- a/strtotimeval.c +++ b/strtotimeval.c @@ -3,7 +3,7 @@ int -libsimple_strtotimeval(struct timeval *restrict tv, const char *restrict s, char **restrict end) +libsimple_strtotimeval(struct timeval *restrict tv, const char *restrict s, char **restrict end) /* TODO test */ { struct timespec ts; int r = libsimple_strtotimespec(&ts, s, end); diff --git a/sumtimespec.c b/sumtimespec.c index 11c5903..c4bf68c 100644 --- a/sumtimespec.c +++ b/sumtimespec.c @@ -3,7 +3,7 @@ int -libsimple_sumtimespec(struct timespec *sum, const struct timespec *augend, const struct timespec *addend) +libsimple_sumtimespec(struct timespec *sum, const struct timespec *augend, const struct timespec *addend) /* TODO test */ { long int ns = augend->tv_nsec + addend->tv_nsec; time_t s; diff --git a/sumtimeval.c b/sumtimeval.c index 04746aa..409500d 100644 --- a/sumtimeval.c +++ b/sumtimeval.c @@ -3,7 +3,7 @@ int -libsimple_sumtimeval(struct timeval *sum, const struct timeval *augend, const struct timeval *addend) +libsimple_sumtimeval(struct timeval *sum, const struct timeval *augend, const struct timeval *addend) /* TODO test */ { struct timespec a, b, s; int r; diff --git a/timespec2timeval.c b/timespec2timeval.c index c4069ff..282f794 100644 --- a/timespec2timeval.c +++ b/timespec2timeval.c @@ -3,7 +3,7 @@ int -libsimple_timespec2timeval(struct timeval *restrict tv, const struct timespec *restrict ts) +libsimple_timespec2timeval(struct timeval *restrict tv, const struct timespec *restrict ts) /* TODO test */ { tv->tv_sec = ts->tv_sec; tv->tv_usec = ts->tv_nsec / 1000L; diff --git a/timespectostr.c b/timespectostr.c index 2e7dd7a..3eca1f6 100644 --- a/timespectostr.c +++ b/timespectostr.c @@ -3,7 +3,7 @@ char * -libsimple_timespectostr(char *restrict buf, const struct timespec *restrict ts) +libsimple_timespectostr(char *restrict buf, const struct timespec *restrict ts) /* TODO test */ { time_t s = ts->tv_sec; long int ns = ts->tv_nsec; diff --git a/timevaltostr.c b/timevaltostr.c index d8610b3..4dd6b9c 100644 --- a/timevaltostr.c +++ b/timevaltostr.c @@ -3,7 +3,7 @@ char * -libsimple_timevaltostr(char *restrict buf, const struct timeval *restrict tv) +libsimple_timevaltostr(char *restrict buf, const struct timeval *restrict tv) /* TODO test */ { time_t s = tv->tv_sec; long int us = tv->tv_usec; diff --git a/vasprintf.c b/vasprintf.c index 59fb190..9ce1998 100644 --- a/vasprintf.c +++ b/vasprintf.c @@ -24,3 +24,15 @@ fail: *strp = NULL; return -1; } + + +#ifdef TEST +#include <assert.h> + +int +main(void) +{ + return 0; /* Tested via libsimple_asprintf */ +} + +#endif @@ -3,7 +3,7 @@ int -vputenvf(const char *fmt, va_list ap) +vputenvf(const char *fmt, va_list ap) /* TODO test */ { va_list ap2; int n; diff --git a/vweprintf.c b/vweprintf.c index 605f1c2..4118837 100644 --- a/vweprintf.c +++ b/vweprintf.c @@ -6,7 +6,7 @@ extern char *argv0; void -vweprintf(const char *fmt, va_list ap) +vweprintf(const char *fmt, va_list ap) /* TODO test */ { int saved_errno = errno, r; const char *end = strchr(fmt, '\0'); |