diff options
63 files changed, 721 insertions, 197 deletions
diff --git a/include/alloc.h b/include/alloc.h index 6877f61..139b027 100644 --- a/include/alloc.h +++ b/include/alloc.h @@ -16,22 +16,8 @@ struct calt_alloc { extern struct calt_alloc calt_get_alloc(void); extern void calt_use_custom_alloc(struct calt_alloc allocator); - -inline void *calt_malloc(size_t size) { - if (size) { - struct calt_alloc alloc = calt_get_alloc(); - return alloc.malloc ? alloc.malloc(size) : NULL; - } else { - return NULL; - } -} - -inline void calt_free(void *ptr) { - if (ptr) { - calt_get_alloc().free(ptr); - } -} - +extern void *calt_malloc(size_t size); +extern void calt_free(void *ptr); extern void *calt_calloc(size_t size); extern void *calt_realloc(void *ptr, size_t size); extern void *calt_memdup(void const *restrict src, size_t len); diff --git a/include/err.h b/include/err.h new file mode 100644 index 0000000..ba9191c --- /dev/null +++ b/include/err.h @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2023 Marc Pervaz Boocha + * + * SPDX-License-Identifier: MIT + */ +#ifndef CALT_ERR_H +#define CALT_ERR_H + +enum calt_err { + CALT_OK = 0, + CALT_ERR_UNKNOWN = 1, + CALT_ERR_NULL = 2, + CALT_ERR_OP_NOT_IMPLEMENTED = 3, + CALT_ERR_OOM = 4, + CALT_ERR_INVALID_ARGUEMENT = 5, +}; + + +extern void calt_set_err(enum calt_err *err, enum calt_err code); +extern char* calt_errstr(enum calt_err err); + +#endif diff --git a/include/mem.h b/include/mem.h index b18822c..4136c76 100644 --- a/include/mem.h +++ b/include/mem.h @@ -7,159 +7,30 @@ #define CALT_MEM_H #include <stddef.h> -inline void *calt_memset(void *restrict dest, unsigned char value, size_t count) { - unsigned char *p = dest; - while (count-- > 0) { - *p++ = value; - } - - return dest; -} - -inline void *calt_memset_null(void *dest, unsigned char value, size_t count) { - return dest ? calt_memset(dest, value, count) : NULL; -} - -inline void *calt_memzero(void *dest, size_t count) { - return calt_memset(dest, '\0', count); -} - -inline void *calt_memzero_null(void *dest, size_t count) { - return dest ? calt_memzero(dest, count) : NULL; -} - -inline void *calt_memcpy(void *restrict dest, const void *restrict src, - size_t count) { - unsigned char *d = dest; - unsigned char const *s = src; - - while (count-- > 0) { - *d++ = *s++; - } - - return dest; -} - - -inline void *calt_memcpy_null(void *restrict dest, void const *restrict src, - size_t count) { - return dest ? calt_memcpy(dest, src, count) : NULL; -} +extern void *calt_memset(void *dest, unsigned char value, size_t count); +extern void *calt_memset_null(void *dest, unsigned char value, size_t count); +extern void *calt_memzero(void *restrict dest, size_t count); +extern void *calt_memzero_null(void *restrict dest, size_t count); +extern void *calt_memcpy(void *restrict dest, void const *restrict src, + size_t count); +extern void *calt_memcpy_null(void *restrict dest, void const *restrict src, + size_t count); extern void *calt_memccpy(void *restrict dest, void const *restrict src, unsigned char value, size_t count); -inline void *calt_memccpy_null(void *restrict dest, void const *restrict src, - unsigned char value, size_t count) { - return dest ? calt_memccpy(dest, src, value, count) : NULL; -} - -inline void *calt_memmove(void *dest, void const *src, size_t count) { - unsigned char *d = dest; - unsigned char const *s = src; - - if (d > s) { - d += count; - s += count; - while (count-- > 0) { - *--d = *--s; - } - } else if (d < s) { - calt_memcpy(dest, src, count); - } - - return dest; -} - -inline void *calt_memmove_null(void *dest, void const *src, size_t count) { - return dest ? calt_memmove(dest, src, count) : NULL; -} - -inline void *calt_memchr(void const *ptr, unsigned char value, size_t count) { - unsigned char const *p = ptr; - - while (count-- > 0) { - if (*p == value) { - return (void *)p; - } - p++; - } - - return NULL; -} - -inline void *calt_memrchr(void const *ptr, unsigned char value, size_t count) { - unsigned char const *p = ptr; - p = p + count; - - while (count > 0) { - if (*(--p) == value) { - return (void *)p; - } - } - - return NULL; -} - -inline int calt_memcmp(const void *ptr1, const void *ptr2, size_t count) { - const unsigned char *p1 = ptr1; - const unsigned char *p2 = ptr2; - - while (count-- > 0) { - if (*p1 != *p2) { - return (*p1 < *p2) ? -1 : 1; - } - p1++; - p2++; - } - - return 0; -} - -inline void calt_memswap(void *restrict ptr1, void *restrict ptr2, size_t size) { - unsigned char *p1 = ptr1; - unsigned char *p2 = ptr2; - while (size-- > 0) { - unsigned char temp = *p1; - *p1++ = *p2; - *p2++ = temp; - } -} - -inline size_t calt_memlen(void const* ptr, unsigned char value, size_t size) { - unsigned char const *p = ptr; - size_t len = 0; - - while (size-- > 0 && *p != value) { - p++; - len++; - } - - return len; -} - +extern void *calt_memccpy_null(void *restrict dest, void const *restrict src, + unsigned char value, size_t count); +extern void *calt_memmove(void *dest, void const *src, size_t count); +extern void *calt_memmove_null(void *dest, void const *src, size_t count); +extern void *calt_memchr(void const *ptr, unsigned char value, size_t count); +extern void *calt_memrchr(void const *ptr, unsigned char value, size_t count); +extern void *calt_memrchr(void const *ptr, unsigned char value, size_t count); +extern int calt_memcmp(void const *ptr1, void const *ptr2, size_t count); +extern void calt_memswap(void *restrict ptr1, void *restrict ptr2, size_t size); +extern size_t calt_memlen(void const *ptr, unsigned char value, size_t count); extern size_t calt_memhash(void const *ptr, size_t count); - -inline void *calt_memmem(void const *haystack, size_t haystacklen, - void const *needle, size_t needlelen) { - unsigned char const *p = haystack; - unsigned char const *end = p + haystacklen - needlelen; - while (p <= end) { - if (calt_memcmp(p, needle, needlelen) == 0) { - return (void *)p; - } - p++; - } - return NULL; -} - -inline void calt_memrev(void *ptr, size_t size) { - unsigned char *p1 = ptr; - unsigned char *p2 = p1 + size - 1; - while (p1 < p2) { - unsigned char temp = *p1; - *p1++ = *p2; - *p2-- = temp; - } -} +extern void *calt_memmem(void const *haystack, size_t haystacklen, + void const *needle, size_t needlelen); +extern void *calt_memrev(void *ptr, size_t size); #endif diff --git a/include/meson.build b/include/meson.build index 403f666..08f2234 100644 --- a/include/meson.build +++ b/include/meson.build @@ -1,5 +1,6 @@ header = files([ 'calt.h', + 'err.h', 'mem.h', 'alloc.h' ]) @@ -7,8 +7,11 @@ A C library that is to implement extend and be an alternative to the C stdard li ## Build Instructions Requirements: + - [Meson](https://mesonbuild.com) + - [Ninja](https://ninja-build.org/) + - A C compiler which supports C11 or higher. To Build: diff --git a/src/alloc/free.c b/src/alloc/free.c index 90d16d5..67b54bd 100644 --- a/src/alloc/free.c +++ b/src/alloc/free.c @@ -6,5 +6,9 @@ #include <alloc.h> -extern inline void calt_free(void *ptr); +void calt_free(void *ptr) { + if (ptr) { + calt_get_alloc().free(ptr); + } +} diff --git a/src/alloc/malloc.c b/src/alloc/malloc.c index 3abcf2e..3c354c0 100644 --- a/src/alloc/malloc.c +++ b/src/alloc/malloc.c @@ -6,5 +6,11 @@ #include <alloc.h> -extern inline void *calt_malloc(size_t size); - +void *calt_malloc(size_t size) { + if (size) { + struct calt_alloc alloc = calt_get_alloc(); + return alloc.malloc ? alloc.malloc(size) : NULL; + } else { + return NULL; + } +} diff --git a/src/err/errstr.c b/src/err/errstr.c new file mode 100644 index 0000000..075b285 --- /dev/null +++ b/src/err/errstr.c @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2023 Marc Pervaz Boocha + * + * SPDX-License-Identifier: MIT + */ + +#include <err.h> + +char* calt_errstr(enum calt_err err){ + switch (err) { + case CALT_OK: + return "No Error"; + case CALT_ERR_NULL: + return "Null Pointer"; + case CALT_ERR_OOM: + return "Unable to Allocate Memmory(Out of Memmory)"; + case CALT_ERR_OP_NOT_IMPLEMENTED: + return "Operation has not been implemented"; + case CALT_ERR_INVALID_ARGUEMENT: + return "Invalid Arguement"; + case CALT_ERR_UNKNOWN: + default: + return "Unknown Error"; + } +} diff --git a/src/err/meson.build b/src/err/meson.build new file mode 100644 index 0000000..5660eb1 --- /dev/null +++ b/src/err/meson.build @@ -0,0 +1,4 @@ +src += files([ + 'set_err.c', + 'errstr.c', +]) diff --git a/src/err/set_err.c b/src/err/set_err.c new file mode 100644 index 0000000..3b107af --- /dev/null +++ b/src/err/set_err.c @@ -0,0 +1,13 @@ +/* + * Copyright (c) 2023 Marc Pervaz Boocha + * + * SPDX-License-Identifier: MIT + */ + +#include <err.h> + +void calt_set_err(enum calt_err *err, enum calt_err code) { + if (err) { + *err = code; + } +} diff --git a/src/mem/memccpy.c b/src/mem/memccpy.c index 810b71b..82de236 100644 --- a/src/mem/memccpy.c +++ b/src/mem/memccpy.c @@ -21,6 +21,7 @@ void *calt_memccpy(void *restrict dest, void const *restrict src, unsigned char return NULL; } -extern inline void *calt_memccpy_null(void *restrict dest, - void const *restrict src, - unsigned char value, size_t size); +void *calt_memccpy_null(void *restrict dest, void const *restrict src, + unsigned char value, size_t count) { + return dest ? calt_memccpy(dest, src, value, count) : NULL; +} diff --git a/src/mem/memchr.c b/src/mem/memchr.c index b583ab3..ed1187a 100644 --- a/src/mem/memchr.c +++ b/src/mem/memchr.c @@ -6,4 +6,15 @@ #include <mem.h> -extern inline void *calt_memchr(void const *ptr, unsigned char value, size_t count); +void *calt_memchr(void const *ptr, unsigned char value, size_t count) { + unsigned char const *p = ptr; + + while (count-- > 0) { + if (*p == value) { + return (void *)p; + } + p++; + } + + return NULL; +} diff --git a/src/mem/memcmp.c b/src/mem/memcmp.c index bcb2fb5..70419be 100644 --- a/src/mem/memcmp.c +++ b/src/mem/memcmp.c @@ -11,5 +11,22 @@ */ #include <mem.h> -extern inline int calt_memcmp(void const *ptr1, void const *ptr2, size_t count); +int calt_memcmp(const void *ptr1, const void *ptr2, size_t count) { + if(ptr1 == ptr2){ + return 0; + } + + const unsigned char *p1 = ptr1; + const unsigned char *p2 = ptr2; + + while (count-- > 0) { + if (*p1 != *p2) { + return (*p1 < *p2) ? -1 : 1; + } + p1++; + p2++; + } + + return 0; +} diff --git a/src/mem/memcpy.c b/src/mem/memcpy.c index 7880c53..c9fbfab 100644 --- a/src/mem/memcpy.c +++ b/src/mem/memcpy.c @@ -5,10 +5,21 @@ */ #include <mem.h> +void *calt_memcpy(void *restrict dest, const void *restrict src, + size_t count) { + unsigned char *d = dest; + unsigned char const *s = src; -extern inline void *calt_memcpy(void *restrict dest, void const *restrict src, - size_t count); + while (count-- > 0) { + *d++ = *s++; + } + return dest; +} + + +void *calt_memcpy_null(void *restrict dest, void const *restrict src, + size_t count) { + return dest ? calt_memcpy(dest, src, count) : NULL; +} -extern inline void *calt_memcpy_null(void *restrict dest, void const *restrict src, - size_t count); diff --git a/src/mem/memlen.c b/src/mem/memlen.c index 6d6ddfe..83d15b1 100644 --- a/src/mem/memlen.c +++ b/src/mem/memlen.c @@ -6,4 +6,14 @@ #include <mem.h> -extern inline size_t calt_memlen(void const *ptr, unsigned char value, size_t count); +size_t calt_memlen(void const* ptr, unsigned char value, size_t size) { + unsigned char const *p = ptr; + size_t len = 0; + + while (size-- > 0 && *p != value) { + p++; + len++; + } + + return len; +} diff --git a/src/mem/memmem.c b/src/mem/memmem.c index 14be8cc..68060cd 100644 --- a/src/mem/memmem.c +++ b/src/mem/memmem.c @@ -6,5 +6,15 @@ #include <mem.h> -extern inline void *calt_memmem(void const *haystack, size_t haystacklen, - void const *needle, size_t needlelen); +void *calt_memmem(void const *haystack, size_t haystacklen, + void const *needle, size_t needlelen) { + unsigned char const *p = haystack; + unsigned char const *end = p + haystacklen - needlelen; + while (p <= end) { + if (calt_memcmp(p, needle, needlelen) == 0) { + return (void *)p; + } + p++; + } + return NULL; +} diff --git a/src/mem/memmove.c b/src/mem/memmove.c index b6c6b1e..abb5d41 100644 --- a/src/mem/memmove.c +++ b/src/mem/memmove.c @@ -6,6 +6,24 @@ #include <mem.h> -extern inline void *calt_memmove(void *dest, void const *src, size_t count); -extern inline void *calt_memmove_null(void *dest, void const *src, size_t count); +void *calt_memmove(void *dest, void const *src, size_t count) { + unsigned char *d = dest; + unsigned char const *s = src; + + if (d > s) { + d += count; + s += count; + while (count-- > 0) { + *--d = *--s; + } + } else if (d < s) { + calt_memcpy(dest, src, count); + } + + return dest; +} + +void *calt_memmove_null(void *dest, void const *src, size_t count) { + return dest ? calt_memmove(dest, src, count) : NULL; +} diff --git a/src/mem/memrchr.c b/src/mem/memrchr.c index 1799d27..a6e35af 100644 --- a/src/mem/memrchr.c +++ b/src/mem/memrchr.c @@ -6,4 +6,15 @@ #include <mem.h> -extern inline void *calt_memrchr(void const *ptr, unsigned char value, size_t count); +void *calt_memrchr(void const *ptr, unsigned char value, size_t count) { + unsigned char const *p = ptr; + p = p + count; + + while (count > 0) { + if (*(--p) == value) { + return (void *)p; + } + } + + return NULL; +} diff --git a/src/mem/memrev.c b/src/mem/memrev.c index 472efcb..29c549c 100644 --- a/src/mem/memrev.c +++ b/src/mem/memrev.c @@ -6,5 +6,14 @@ #include <mem.h> -extern inline void calt_memrev(void *ptr, size_t size); +void* calt_memrev(void *ptr, size_t size) { + unsigned char *p1 = ptr; + unsigned char *p2 = p1 + size - 1; + while (p1 < p2) { + unsigned char temp = *p1; + *p1++ = *p2; + *p2-- = temp; + } + return ptr; +} diff --git a/src/mem/memset.c b/src/mem/memset.c index 48fee61..c14cda8 100644 --- a/src/mem/memset.c +++ b/src/mem/memset.c @@ -6,6 +6,18 @@ #include <mem.h> -extern inline void *calt_memset(void *dest, unsigned char value, size_t count); -extern inline void *calt_memset_null(void *dest, unsigned char value, size_t count); +void *calt_memset(void *restrict dest, unsigned char value, size_t count) { + unsigned char *p = dest; + + while (count-- > 0) { + *p++ = value; + } + + return dest; +} + +void *calt_memset_null(void *dest, unsigned char value, size_t count) { + return dest ? calt_memset(dest, value, count) : NULL; +} + diff --git a/src/mem/memswap.c b/src/mem/memswap.c index 512c8c9..b6d30b1 100644 --- a/src/mem/memswap.c +++ b/src/mem/memswap.c @@ -6,4 +6,16 @@ #include <mem.h> -extern inline void calt_memswap(void *restrict ptr1, void *restrict ptr2, size_t size); +void calt_memswap(void *restrict ptr1, void *restrict ptr2, size_t size) { + if(ptr1 == ptr2){ + return; + } + unsigned char *p1 = ptr1; + unsigned char *p2 = ptr2; + while (size-- > 0) { + unsigned char temp = *p1; + *p1++ = *p2; + *p2++ = temp; + } +} + diff --git a/src/mem/memzero.c b/src/mem/memzero.c index c92691f..e715539 100644 --- a/src/mem/memzero.c +++ b/src/mem/memzero.c @@ -6,5 +6,11 @@ #include <mem.h> -extern inline void *calt_memzero(void *restrict dest, size_t count); -extern inline void *calt_memzero_null(void *restrict dest, size_t count); +void *calt_memzero(void *dest, size_t count) { + return calt_memset(dest, '\0', count); +} + +void *calt_memzero_null(void *dest, size_t count) { + return dest ? calt_memzero(dest, count) : NULL; +} + diff --git a/src/meson.build b/src/meson.build index 765ef7c..8358f93 100644 --- a/src/meson.build +++ b/src/meson.build @@ -1,6 +1,7 @@ src = files() dirs = [ + 'err', 'alloc', 'mem', ] diff --git a/test/alloc/memdup.c b/test/alloc/memdup.c new file mode 100644 index 0000000..db40146 --- /dev/null +++ b/test/alloc/memdup.c @@ -0,0 +1,11 @@ +#include <alloc.h> +#include <mem.h> + +static char const src[] = "hello"; + +int main(void) { + void *dest = calt_memdup(src, sizeof(src)); + int res = calt_memcmp(src, dest, sizeof(src)) == 0 ? 0 : 1; + calt_free(dest); + return res; +} diff --git a/test/alloc/meson.build b/test/alloc/meson.build index e69de29..8e8c5cd 100644 --- a/test/alloc/meson.build +++ b/test/alloc/meson.build @@ -0,0 +1,3 @@ +test('memdup', executable('memdup', 'memdup.c', + dependencies : calt +)) diff --git a/test/mem/memccpy_nullnull.c b/test/mem/memccpy_nullnull.c new file mode 100644 index 0000000..629c990 --- /dev/null +++ b/test/mem/memccpy_nullnull.c @@ -0,0 +1,8 @@ +#include <mem.h> +#include <stddef.h> + +static char const src[] = "Hello World"; + +int main(void){ + return !calt_memccpy_null(NULL, src, 'e' ,sizeof src) ? 0 : 1; +} diff --git a/test/mem/memchr.c b/test/mem/memchr.c new file mode 100644 index 0000000..d3fafaa --- /dev/null +++ b/test/mem/memchr.c @@ -0,0 +1,5 @@ +#include <mem.h> + +static char const buf[] = "test"; + +int main(void) { return calt_memchr(buf, 't', sizeof buf) == &buf[0] ? 0 : 1; } diff --git a/test/mem/memchr_notfound.c b/test/mem/memchr_notfound.c new file mode 100644 index 0000000..30c12a3 --- /dev/null +++ b/test/mem/memchr_notfound.c @@ -0,0 +1,5 @@ +#include <mem.h> + +static char const buf[] = "test"; + +int main(void) { return calt_memchr(buf, 'u', sizeof buf) == NULL ? 0 : 1; } diff --git a/test/mem/memcmp_diff.c b/test/mem/memcmp_diff.c new file mode 100644 index 0000000..2601656 --- /dev/null +++ b/test/mem/memcmp_diff.c @@ -0,0 +1,8 @@ +#include <mem.h> + +static char const buf1[] = "hello"; +static char const buf2[] = "HELLO"; + +int main(void) { + return calt_memcmp(buf1, buf2, sizeof(buf1)) != 0 ? 0 : 1; +} diff --git a/test/mem/memcmp_prefix.c b/test/mem/memcmp_prefix.c new file mode 100644 index 0000000..175f85d --- /dev/null +++ b/test/mem/memcmp_prefix.c @@ -0,0 +1,6 @@ +#include <mem.h> + +char const buf1[] = "hello"; +char const buf2[] = "hello world"; + +int main(void) { return 0 == calt_memcmp(buf1, buf2, sizeof(buf1) - 1) ? 0 : 1; } diff --git a/test/mem/memcmp_same.c b/test/mem/memcmp_same.c new file mode 100644 index 0000000..6881fe7 --- /dev/null +++ b/test/mem/memcmp_same.c @@ -0,0 +1,8 @@ +#include <mem.h> + +static char const buf1[] = "hello"; +static char const buf2[] = "hello"; + +int main(void) { + return calt_memcmp(buf1, buf2, sizeof(buf1)) == 0 ? 0 : 1; +} diff --git a/test/mem/memcmp_samenull.c b/test/mem/memcmp_samenull.c new file mode 100644 index 0000000..68e00a7 --- /dev/null +++ b/test/mem/memcmp_samenull.c @@ -0,0 +1,5 @@ +#include <mem.h> + +int main(void) { + return calt_memcmp(NULL, NULL, 10) == 0 ? 0 : 1; +} diff --git a/test/mem/memcmp_sing.c b/test/mem/memcmp_sing.c new file mode 100644 index 0000000..0d0800e --- /dev/null +++ b/test/mem/memcmp_sing.c @@ -0,0 +1,7 @@ +#include <mem.h> + +static char const buf1[] = "hello"; + +int main(void) { + return calt_memcmp(buf1, buf1, sizeof(buf1)) == 0 ? 0 : 1; +} diff --git a/test/mem/memcpy_null_prefix.c b/test/mem/memcpy_null_prefix.c new file mode 100644 index 0000000..ffb3084 --- /dev/null +++ b/test/mem/memcpy_null_prefix.c @@ -0,0 +1,11 @@ +#include <mem.h> + +static char const src[] = "hello"; +static char dest[sizeof(src)] = {0}; + +int main(void) { + calt_memcpy_null(dest, src, 3); + return calt_memcmp(src, dest, 3) == 0 + ? calt_memcmp("\0\0\0", dest + 3, 3) == 0 ? 0 : 1 + : 1; +} diff --git a/test/mem/memcpy_null_same.c b/test/mem/memcpy_null_same.c new file mode 100644 index 0000000..d0b2ae0 --- /dev/null +++ b/test/mem/memcpy_null_same.c @@ -0,0 +1,9 @@ +#include <mem.h> + +static char const src[] = "hello"; +static char dest[sizeof(src)] = { 0 }; + +int main(void) { + calt_memcpy_null(dest, src, sizeof(src)); + return calt_memcmp(src, dest, sizeof(src)) == 0 ? 0 : 1; +} diff --git a/test/mem/memcpy_nullnull.c b/test/mem/memcpy_nullnull.c new file mode 100644 index 0000000..30b72f3 --- /dev/null +++ b/test/mem/memcpy_nullnull.c @@ -0,0 +1,8 @@ +#include <mem.h> +#include <stddef.h> + +static char const src[] = "Hello World"; + +int main(void){ + return !calt_memcpy_null(NULL, src, sizeof src) ? 0 : 1; +} diff --git a/test/mem/memcpy_prefix.c b/test/mem/memcpy_prefix.c new file mode 100644 index 0000000..675f2bc --- /dev/null +++ b/test/mem/memcpy_prefix.c @@ -0,0 +1,11 @@ +#include <mem.h> + +static char const src[] = "hello"; +static char dest[sizeof(src)] = {0}; + +int main(void) { + calt_memcpy(dest, src, 3); + return calt_memcmp(src, dest, 3) == 0 + ? calt_memcmp("\0\0\0", dest + 3, 3) == 0 ? 0 : 1 + : 1; +} diff --git a/test/mem/memcpy_same.c b/test/mem/memcpy_same.c new file mode 100644 index 0000000..3fe9e84 --- /dev/null +++ b/test/mem/memcpy_same.c @@ -0,0 +1,9 @@ +#include <mem.h> + +static char const src[] = "hello"; +static char dest[sizeof(src)] = { 0 }; + +int main(void) { + calt_memcpy(dest, src, sizeof(src)); + return calt_memcmp(src, dest, sizeof(src)) == 0 ? 0 : 1; +} diff --git a/test/mem/memlen.c b/test/mem/memlen.c new file mode 100644 index 0000000..1127b2b --- /dev/null +++ b/test/mem/memlen.c @@ -0,0 +1,5 @@ +#include <mem.h> + +static char const buf[] = "Test"; + +int main(void) { return calt_memlen(buf, '\0', sizeof buf) == 4 ? 0 : 1; } diff --git a/test/mem/memlen_overflow.c b/test/mem/memlen_overflow.c new file mode 100644 index 0000000..258f2a0 --- /dev/null +++ b/test/mem/memlen_overflow.c @@ -0,0 +1,5 @@ +#include <mem.h> + +static char const buf[] = "Test"; + +int main(void) { return calt_memlen(buf, '\0', 3) == 3 ? 0 : 1; } diff --git a/test/mem/memmove_backward.c b/test/mem/memmove_backward.c new file mode 100644 index 0000000..5c8d477 --- /dev/null +++ b/test/mem/memmove_backward.c @@ -0,0 +1,10 @@ +#include <mem.h> + +static char buf[] = "hello world"; + +int main(void) { + return calt_memcmp(calt_memmove(buf, buf + 6, 5), "world world", + sizeof buf - 1) == 0 + ? 0 + : 1; +} diff --git a/test/mem/memmove_null_backward.c b/test/mem/memmove_null_backward.c new file mode 100644 index 0000000..5c8d477 --- /dev/null +++ b/test/mem/memmove_null_backward.c @@ -0,0 +1,10 @@ +#include <mem.h> + +static char buf[] = "hello world"; + +int main(void) { + return calt_memcmp(calt_memmove(buf, buf + 6, 5), "world world", + sizeof buf - 1) == 0 + ? 0 + : 1; +} diff --git a/test/mem/memmove_null_overlap.c b/test/mem/memmove_null_overlap.c new file mode 100644 index 0000000..6e786b5 --- /dev/null +++ b/test/mem/memmove_null_overlap.c @@ -0,0 +1,13 @@ +#include <mem.h> +#include <stdio.h> +#include <string.h> + +static char buf[] = "hello world"; + +int main(void) { + calt_memmove(buf + 1, buf + 2, 5); + return calt_memcmp(buf, "hllo wworld", + sizeof buf - 1) == 0 + ? 0 + : 1; +} diff --git a/test/mem/memmove_null_prefix.c b/test/mem/memmove_null_prefix.c new file mode 100644 index 0000000..87a505c --- /dev/null +++ b/test/mem/memmove_null_prefix.c @@ -0,0 +1,11 @@ +#include <mem.h> + +static char const src[] = "hello"; +static char dest[sizeof(src)] = {0}; + +int main(void) { + calt_memmove_null(dest, src, 3); + return calt_memcmp(src, dest, 3) == 0 + ? calt_memcmp("\0\0\0", dest + 3, 3) == 0 ? 0 : 1 + : 1; +} diff --git a/test/mem/memmove_null_same.c b/test/mem/memmove_null_same.c new file mode 100644 index 0000000..95c75da --- /dev/null +++ b/test/mem/memmove_null_same.c @@ -0,0 +1,9 @@ +#include <mem.h> + +static char const src[] = "hello"; +static char dest[sizeof(src)] = { 0 }; + +int main(void) { + calt_memmove_null(dest, src, sizeof(src)); + return calt_memcmp(src, dest, sizeof(src)) == 0 ? 0 : 1; +} diff --git a/test/mem/memmove_null_sing.c b/test/mem/memmove_null_sing.c new file mode 100644 index 0000000..0ac6420 --- /dev/null +++ b/test/mem/memmove_null_sing.c @@ -0,0 +1,8 @@ +#include <mem.h> + +static char src[] = "hello"; + +int main(void) { + calt_memmove(src, src, sizeof(src)); + return calt_memcmp(src, "hello", sizeof(src)) == 0 ? 0 : 1; +} diff --git a/test/mem/memmove_nullnull.c b/test/mem/memmove_nullnull.c new file mode 100644 index 0000000..325b859 --- /dev/null +++ b/test/mem/memmove_nullnull.c @@ -0,0 +1,8 @@ +#include <mem.h> +#include <stddef.h> + +static char const src[] = "Hello World"; + +int main(void){ + return !calt_memmove_null(NULL, src, sizeof src) ? 0 : 1; +} diff --git a/test/mem/memmove_overlap.c b/test/mem/memmove_overlap.c new file mode 100644 index 0000000..6e786b5 --- /dev/null +++ b/test/mem/memmove_overlap.c @@ -0,0 +1,13 @@ +#include <mem.h> +#include <stdio.h> +#include <string.h> + +static char buf[] = "hello world"; + +int main(void) { + calt_memmove(buf + 1, buf + 2, 5); + return calt_memcmp(buf, "hllo wworld", + sizeof buf - 1) == 0 + ? 0 + : 1; +} diff --git a/test/mem/memmove_prefix.c b/test/mem/memmove_prefix.c new file mode 100644 index 0000000..a6715c7 --- /dev/null +++ b/test/mem/memmove_prefix.c @@ -0,0 +1,11 @@ +#include <mem.h> + +static char const src[] = "hello"; +static char dest[sizeof(src)] = {0}; + +int main(void) { + calt_memmove(dest, src, 3); + return calt_memcmp(src, dest, 3) == 0 + ? calt_memcmp("\0\0\0", dest + 3, 3) == 0 ? 0 : 1 + : 1; +} diff --git a/test/mem/memmove_same.c b/test/mem/memmove_same.c new file mode 100644 index 0000000..0e032de --- /dev/null +++ b/test/mem/memmove_same.c @@ -0,0 +1,9 @@ +#include <mem.h> + +static char const src[] = "hello"; +static char dest[sizeof(src)] = { 0 }; + +int main(void) { + calt_memmove(dest, src, sizeof(src)); + return calt_memcmp(src, dest, sizeof(src)) == 0 ? 0 : 1; +} diff --git a/test/mem/memmove_sing.c b/test/mem/memmove_sing.c new file mode 100644 index 0000000..3845f35 --- /dev/null +++ b/test/mem/memmove_sing.c @@ -0,0 +1,8 @@ +#include <mem.h> + +static char src[] = "hello"; + +int main(void) { + calt_memmove(src, src, sizeof(src)); + return calt_memcmp(src, "hello", sizeof(src)) == 0 ? 0 : 1; +} diff --git a/test/mem/memrchr.c b/test/mem/memrchr.c new file mode 100644 index 0000000..3ade333 --- /dev/null +++ b/test/mem/memrchr.c @@ -0,0 +1,5 @@ +#include <mem.h> + +static char const buf[] = "test"; + +int main(void) { return calt_memrchr(buf, 't', sizeof buf) == &buf[3] ? 0 : 1; } diff --git a/test/mem/memrchr_notfound.c b/test/mem/memrchr_notfound.c new file mode 100644 index 0000000..bc9ddec --- /dev/null +++ b/test/mem/memrchr_notfound.c @@ -0,0 +1,5 @@ +#include <mem.h> + +static char const buf[] = "test"; + +int main(void) { return calt_memrchr(buf, 'u', sizeof buf) == NULL ? 0 : 1; } diff --git a/test/mem/memrev.c b/test/mem/memrev.c new file mode 100644 index 0000000..b10c7c1 --- /dev/null +++ b/test/mem/memrev.c @@ -0,0 +1,7 @@ +#include <mem.h> + +static char src[] = "abc"; + +int main(void) { + return calt_memcmp(calt_memrev(src, 3), "cba", 3) == 0 ? 0 : 1; +} diff --git a/test/mem/memset_null.c b/test/mem/memset_null.c new file mode 100644 index 0000000..bcf4339 --- /dev/null +++ b/test/mem/memset_null.c @@ -0,0 +1,14 @@ +#include <mem.h> +#include <stddef.h> + +static char buf[100]; + +int main(void){ + calt_memset_null(buf, 'A', sizeof buf); + for(size_t i = 0; i < sizeof buf; i++){ + if('A' != buf[i]){ + return 1; + } + } + return 0; +} diff --git a/test/mem/memset_nullnull.c b/test/mem/memset_nullnull.c new file mode 100644 index 0000000..a74e180 --- /dev/null +++ b/test/mem/memset_nullnull.c @@ -0,0 +1,6 @@ +#include <mem.h> +#include <stddef.h> + +int main(void){ + return !calt_memset_null(NULL, 'A', 10) ? 0 : 1; +} diff --git a/test/mem/memswap.c b/test/mem/memswap.c new file mode 100644 index 0000000..a5be39f --- /dev/null +++ b/test/mem/memswap.c @@ -0,0 +1,11 @@ +#include <mem.h> + +static int const a = 1; +static int const b = 2; + +int main(void){ + int x = a; + int y = b; + calt_memswap(&x, &y, sizeof x); + return x == b && y == a ? 0 : 1; +} diff --git a/test/mem/memswap_null.c b/test/mem/memswap_null.c new file mode 100644 index 0000000..015f3f3 --- /dev/null +++ b/test/mem/memswap_null.c @@ -0,0 +1,6 @@ +#include <mem.h> + +int main(void){ + calt_memswap(NULL, NULL, 1); + return 0; +} diff --git a/test/mem/memswap_same.c b/test/mem/memswap_same.c new file mode 100644 index 0000000..532f8c9 --- /dev/null +++ b/test/mem/memswap_same.c @@ -0,0 +1,9 @@ +#include <mem.h> + +static int const a = 1; + +int main(void){ + int x = a; + calt_memswap(&x, &x, sizeof x); + return x == a ? 0 : 1; +} diff --git a/test/mem/memzero_null.c b/test/mem/memzero_null.c new file mode 100644 index 0000000..ef817a1 --- /dev/null +++ b/test/mem/memzero_null.c @@ -0,0 +1,14 @@ +#include <mem.h> +#include <stddef.h> + +static char buf[100]; + +int main(void){ + calt_memzero_null(buf, sizeof buf); + for(size_t i = 0; i < sizeof buf; i++){ + if('\0' != buf[i]){ + return 1; + } + } + return 0; +} diff --git a/test/mem/memzero_nullnull.c b/test/mem/memzero_nullnull.c new file mode 100644 index 0000000..c916818 --- /dev/null +++ b/test/mem/memzero_nullnull.c @@ -0,0 +1,6 @@ +#include <mem.h> +#include <stddef.h> + +int main(void){ + return !calt_memzero_null(NULL, 10) ? 0 : 1; +} diff --git a/test/mem/meson.build b/test/mem/meson.build index 8cc5fc9..d4d75ae 100644 --- a/test/mem/meson.build +++ b/test/mem/meson.build @@ -1,9 +1,152 @@ -memset = executable('memset', 'memset.c', +test('memset', executable('memset', 'memset.c', dependencies : calt -) -test('memset', memset) +)) -memzero = executable('memzero', 'memzero.c', +test('memset_null Non Null Pointer', executable('memset_null', 'memset_null.c', dependencies : calt -) -test('memzero', memzero) +)) + +test('memset_null Null Pointer', executable('memset_nullnull', 'memset_nullnull.c', + dependencies : calt +)) + +test('memzero', executable('memzero', 'memzero.c', + dependencies : calt +)) + +test('memzero_null Non Null Pointer', executable('memzero_null', 'memzero_null.c', + dependencies : calt +)) + +test('memzero_null Null Pointer', executable('memzero_nullnull', 'memzero_nullnull.c', + dependencies : calt +)) + +test('memcmp Same', executable('memcmp_same', 'memcmp_same.c', + dependencies : calt +)) + +test('memcmp Different',executable('memcmp_diff', 'memcmp_diff.c', + dependencies : calt +)) + +test('memcmp Prefix', executable('memcmp_prefix', 'memcmp_prefix.c', + dependencies : calt +)) + +test('memcmp Same Non-Null', executable('memcmp_sing', 'memcmp_sing.c', + dependencies : calt +)) + +test('memcmp Same Null', executable('memcmp_samenull', 'memcmp_samenull.c', + dependencies : calt +)) + +test('memcpy Same', executable('memcpy_same', 'memcpy_same.c', + dependencies : calt +)) + +test('memcpy Prefix', executable('memcpy_prefix', 'memcpy_prefix.c', + dependencies : calt +)) + +test('memcpy_null Same', executable('memcpy_null_same', 'memcpy_null_same.c', + dependencies : calt +)) + +test('memcpy_null Prefix', executable('memcpy_null_prefix', 'memcpy_null_prefix.c', + dependencies : calt +)) + +test('memcpy_null Null Pointer', executable('memcpy_nullnull', 'memcpy_nullnull.c', + dependencies : calt +)) + +test('memmove Same', executable('memmove_same', 'memmove_same.c', + dependencies : calt +)) + +test('memmove Same Pointer', executable('memmove_sing', 'memmove_null_sing.c', + dependencies : calt +)) + +test('memmove Prefix', executable('memmove_prefix', 'memmove_prefix.c', + dependencies : calt +)) + +test('memmove Overlap', executable('memmove_overlap', 'memmove_overlap.c', + dependencies : calt +)) + +test('memmove Backward', executable('memmove_backward', 'memmove_backward.c', + dependencies : calt +)) + +test('memmove_null Same', executable('memmove_null_same', 'memmove_null_same.c', + dependencies : calt +)) + +test('memmove_null Same Pointer', executable('memmove_null_sing', 'memmove_null_sing.c', + dependencies : calt +)) + +test('memmove_null Prefix', executable('memmove_null_prefix', 'memmove_null_prefix.c', + dependencies : calt +)) + +test('memmove_null Overlap', executable('memmove_null_overlap', 'memmove_null_overlap.c', + dependencies : calt +)) + +test('memmove_null Backward', executable('memmove_null_backward', 'memmove_null_backward.c', + dependencies : calt +)) + +test('memmove_null Null Pointer', executable('memmove_nullnull', 'memmove_nullnull.c', + dependencies : calt +)) + +test('memccpy_null Null Pointer', executable('memccpy_nullnull', 'memccpy_nullnull.c', + dependencies : calt +)) + +test('memrev', executable('memrev', 'memrev.c', + dependencies : calt +)) + +test('memswap', executable('memswap', 'memswap.c', + dependencies : calt +)) + +test('memswap Same', executable('memswap_same', 'memswap_same.c', + dependencies : calt +)) + +test('memswap Null', executable('memswap_null', 'memswap_null.c', + dependencies : calt +)) + +test('memlen', executable('memlen', 'memlen.c', + dependencies : calt +)) + +test('memlen Overflow', executable('memlen_overflow', 'memlen_overflow.c', + dependencies : calt +)) + +test('memchr Found', executable('memchr', 'memchr.c', + dependencies : calt +)) + +test('memchr Not Found', executable('memchr_notfound', 'memchr_notfound.c', + dependencies : calt +)) + +test('memrchr Found', executable('memrchr', 'memrchr.c', + dependencies : calt +)) + +test('memrchr Not Found', executable('memrchr_notfound', 'memrchr_notfound.c', + dependencies : calt +)) + diff --git a/test/meson.build b/test/meson.build index 5e7b530..65c5ed3 100644 --- a/test/meson.build +++ b/test/meson.build @@ -1,6 +1,6 @@ dirs = [ - 'alloc', 'mem', + 'alloc', ] foreach dir: dirs |