From a81300180831d14bf98ea6dacd2c5c85a3ddee95 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Sun, 25 Dec 2022 15:23:09 +0400 Subject: [PATCH] Namespace names & add tests --- .gitignore | 4 ++-- include/ctype.h | 14 ++++++++------ include/errno.h | 4 +++- include/libclayer.h | 8 ++++++++ include/setjmp.h | 9 ++++----- include/stdlib.h | 15 ++++++++------- include/string.h | 31 ++++++++++++++++--------------- src/ctype.c | 16 ++++++++-------- src/errno.c | 2 +- src/stdlib.c | 24 ++++++++++++------------ src/string.c | 38 ++++++++++++++++++++------------------ tests/.gitignore | 1 + tests/Makefile.am | 4 ++++ tests/main.c | 4 +--- tests/string.c | 20 ++++++++++++++++++++ 15 files changed, 116 insertions(+), 78 deletions(-) create mode 100644 tests/.gitignore create mode 100644 tests/string.c diff --git a/.gitignore b/.gitignore index a35472b..df1a762 100644 --- a/.gitignore +++ b/.gitignore @@ -64,8 +64,8 @@ /conftest.err /tests/test-suite.log -/tests/test_*.log -/tests/test_*.trs +/tests/*.log +/tests/*.trs /Makefile /include/Makefile diff --git a/include/ctype.h b/include/ctype.h index f8b895a..df86aab 100644 --- a/include/ctype.h +++ b/include/ctype.h @@ -5,13 +5,15 @@ extern "C" { #endif -int isdigit(int c); -int islower(int c); -int isspace(int c); -int isupper(int c); +#include -int tolower(int c); -int toupper(int c); +int LIBCLAYER(isdigit)(int c); +int LIBCLAYER(islower)(int c); +int LIBCLAYER(isspace)(int c); +int LIBCLAYER(isupper)(int c); + +int LIBCLAYER(tolower)(int c); +int LIBCLAYER(toupper)(int c); #ifdef __cplusplus } diff --git a/include/errno.h b/include/errno.h index 755a4c7..d9927a4 100644 --- a/include/errno.h +++ b/include/errno.h @@ -5,9 +5,11 @@ extern "C" { #endif +#include + #define ERANGE 1 -extern int errno; +extern int LIBCLAYER(errno); #ifdef __cplusplus } diff --git a/include/libclayer.h b/include/libclayer.h index 7d06f15..5342062 100644 --- a/include/libclayer.h +++ b/include/libclayer.h @@ -7,6 +7,14 @@ extern "C" { #include +#ifndef LIBCLAYER_PREFIX +#define LIBCLAYER(name) name +#else +#define LIBCLAYER(name) LIBCLAYER2(LIBCLAYER_PREFIX, name) +#define LIBCLAYER2(prefix, name) LIBCLAYER3(prefix, name) +#define LIBCLAYER3(prefix, name) prefix ## name +#endif + struct Libclayer { __attribute__((noreturn)) void (*abort)(); __attribute__((noreturn)) void (*exit)(int status); diff --git a/include/setjmp.h b/include/setjmp.h index b46ffac..65698b0 100644 --- a/include/setjmp.h +++ b/include/setjmp.h @@ -5,6 +5,8 @@ extern "C" { #endif +#include + // TODO: define in architecture-specific header typedef unsigned long __jmp_buf[sizeof(long) == 8 ? 8 : 6]; @@ -14,11 +16,8 @@ typedef struct __jmp_buf_tag { unsigned long __ss[128 / sizeof(long)]; } jmp_buf[1]; -__attribute__((returns_twice)) -int setjmp(jmp_buf env); - -__attribute__((noreturn)) -void longjmp(jmp_buf env, int val); +int LIBCLAYER(setjmp)(jmp_buf env) __attribute__((returns_twice)); +void LIBCLAYER(longjmp)(jmp_buf env, int val) __attribute__((noreturn)); #ifdef __cplusplus } diff --git a/include/stdlib.h b/include/stdlib.h index 7de7e83..757ff0c 100644 --- a/include/stdlib.h +++ b/include/stdlib.h @@ -5,20 +5,21 @@ extern "C" { #endif +#include #include #define EXIT_SUCCESS 0 #define EXIT_FAILURE 1 -int atoi(const char *str); +int LIBCLAYER(atoi)(const char *str); -void abort() __attribute__((noreturn)); -void exit(int status) __attribute__((noreturn)); +void LIBCLAYER(abort)() __attribute__((noreturn)); +void LIBCLAYER(exit)(int status) __attribute__((noreturn)); -void *calloc(size_t nmemb, size_t size); -void free(void *ptr); -void *malloc(size_t size); -void *realloc(void *ptr, size_t size); +void *LIBCLAYER(calloc)(size_t nmemb, size_t size); +void LIBCLAYER(free)(void *ptr); +void *LIBCLAYER(malloc)(size_t size); +void *LIBCLAYER(realloc)(void *ptr, size_t size); #ifdef __cplusplus } diff --git a/include/string.h b/include/string.h index 682a117..b7d0697 100644 --- a/include/string.h +++ b/include/string.h @@ -5,30 +5,31 @@ extern "C" { #endif +#include #include // mem* -int memcmp(const void *s1, const void *s2, size_t n); -void *memcpy(void *dest, const void *src, size_t n); -void *memmove(void *dest, const void *src, size_t n); -void *memchr(const void *s, int c, size_t n); -void *memset(void *s, int c, size_t n); +int LIBCLAYER(memcmp)(const void *s1, const void *s2, size_t n); +void *LIBCLAYER(memcpy)(void *dest, const void *src, size_t n); +void *LIBCLAYER(memmove)(void *dest, const void *src, size_t n); +void *LIBCLAYER(memchr)(const void *s, int c, size_t n); +void *LIBCLAYER(memset)(void *s, int c, size_t n); // str* -char *strcat(char *dest, const char *src); -char *strchr(const char *s, int c); -int strcmp(const char *s1, const char *s2); -char *strcpy(char *dest, const char *src); -size_t strlen(const char *s); +char *LIBCLAYER(strcat)(char *dest, const char *src); +char *LIBCLAYER(strchr)(const char *s, int c); +int LIBCLAYER(strcmp)(const char *s1, const char *s2); +char *LIBCLAYER(strcpy)(char *dest, const char *src); +size_t LIBCLAYER(strlen)(const char *s); // strn* -char *strncat(char *dest, const char *src, size_t n); -int strncmp(const char *s1, const char *s2, size_t n); -char *strncpy(char *dest, const char *src, size_t n); -size_t strnlen(const char *s, size_t maxlen); +char *LIBCLAYER(strncat)(char *dest, const char *src, size_t n); +int LIBCLAYER(strncmp)(const char *s1, const char *s2, size_t n); +char *LIBCLAYER(strncpy)(char *dest, const char *src, size_t n); +size_t LIBCLAYER(strnlen)(const char *s, size_t maxlen); // str* -char *strstr(const char *haystack, const char *needle); +char *LIBCLAYER(strstr)(const char *haystack, const char *needle); #ifdef __cplusplus } diff --git a/src/ctype.c b/src/ctype.c index 92ab5f4..0fd30af 100644 --- a/src/ctype.c +++ b/src/ctype.c @@ -4,32 +4,32 @@ #include -int isdigit(const int c) +int LIBCLAYER(isdigit)(const int c) { return (unsigned)c - '0' < 10; } -int islower(const int c) +int LIBCLAYER(islower)(const int c) { return (unsigned)c - 'a' < 26; } -int isspace(const int c) +int LIBCLAYER(isspace)(const int c) { return c == ' ' || (unsigned)c - '\t' < 5; } -int isupper(const int c) +int LIBCLAYER(isupper)(const int c) { return (unsigned)c - 'A' < 26; } -int tolower(const int c) +int LIBCLAYER(tolower)(const int c) { - return isupper(c) ? (c + ('a' - 'A')) : c; + return LIBCLAYER(isupper)(c) ? (c + ('a' - 'A')) : c; } -int toupper(const int c) +int LIBCLAYER(toupper)(const int c) { - return islower(c) ? (c - ('a' - 'A')) : c; + return LIBCLAYER(islower)(c) ? (c - ('a' - 'A')) : c; } diff --git a/src/errno.c b/src/errno.c index ebd7d69..cbb9687 100644 --- a/src/errno.c +++ b/src/errno.c @@ -4,4 +4,4 @@ #include -int errno = 0; +int LIBCLAYER(errno) = 0; diff --git a/src/stdlib.c b/src/stdlib.c index 75af43f..9e44350 100644 --- a/src/stdlib.c +++ b/src/stdlib.c @@ -9,22 +9,22 @@ #include #include -void exit(const int status) +void LIBCLAYER(exit)(const int status) { // Custom implementation libclayer.exit(status); } -void abort() +void LIBCLAYER(abort)() { // Custom implementation if (libclayer.abort) libclayer.abort(); // Default implementation - exit(EXIT_FAILURE); + LIBCLAYER(exit)(EXIT_FAILURE); } -void *calloc(const size_t nmemb, const size_t size) +void *LIBCLAYER(calloc)(const size_t nmemb, const size_t size) { // Custom implementation if (libclayer.calloc) return libclayer.calloc(nmemb, size); @@ -33,38 +33,38 @@ void *calloc(const size_t nmemb, const size_t size) const size_t total_size = nmemb * size; if (!total_size) return NULL; if (total_size / nmemb != size) return NULL; - void *const ptr = malloc(total_size); - if (ptr) memset(ptr, 0, total_size); + void *const ptr = LIBCLAYER(malloc)(total_size); + if (ptr) LIBCLAYER(memset)(ptr, 0, total_size); return ptr; } -void free(void *const ptr) +void LIBCLAYER(free)(void *const ptr) { // Custom implementation libclayer.free(ptr); } -void *malloc(const size_t size) +void *LIBCLAYER(malloc)(const size_t size) { // Custom implementation return libclayer.malloc(size); } -void *realloc(void *const ptr, const size_t size) +void *LIBCLAYER(realloc)(void *const ptr, const size_t size) { // Custom implementation return libclayer.realloc(ptr, size); } -int atoi(const char *str) +int LIBCLAYER(atoi)(const char *str) { - while (isspace(*str)) ++str; + while (LIBCLAYER(isspace)(*str)) ++str; bool is_negative = false; switch (*str) { case '-': is_negative = true; // fall through case '+': ++str; } int result = 0; - while (isdigit(*str)) result = 10 * result - (*str++ - '0'); + while (LIBCLAYER(isdigit)(*str)) result = 10 * result - (*str++ - '0'); return is_negative ? result : -result; } diff --git a/src/string.c b/src/string.c index 45045f8..442334c 100644 --- a/src/string.c +++ b/src/string.c @@ -5,7 +5,7 @@ #include #include -int memcmp(const void *s1, const void *s2, size_t n) +int LIBCLAYER(memcmp)(const void *s1, const void *s2, size_t n) { for (const unsigned char *p1 = s1, *p2 = s2; n--; ++p1, ++p2) { if (*p1 != *p2) return *p1 - *p2; @@ -13,7 +13,7 @@ int memcmp(const void *s1, const void *s2, size_t n) return 0; } -void *memcpy(void *dest, const void *src, size_t n) +void *LIBCLAYER(memcpy)(void *dest, const void *src, size_t n) { char *dest_cell = dest; char *src_cell = (char*)src; @@ -21,7 +21,7 @@ void *memcpy(void *dest, const void *src, size_t n) return dest; } -void *memmove(void *dest, const void *src, size_t n) +void *LIBCLAYER(memmove)(void *dest, const void *src, size_t n) { char *dest_cell = dest; char *src_cell = (char*)src; @@ -35,7 +35,7 @@ void *memmove(void *dest, const void *src, size_t n) return dest; } -void *memchr(const void *s, int c, size_t n) +void *LIBCLAYER(memchr)(const void *s, int c, size_t n) { for (const unsigned char *p = s; n--; ++p) { if ((unsigned char)c == *p) return (void*)p; @@ -43,14 +43,14 @@ void *memchr(const void *s, int c, size_t n) return NULL; } -void *memset(void *s, int c, size_t n) +void *LIBCLAYER(memset)(void *s, int c, size_t n) { char *ss = s; while (n--) *ss++ = c; return s; } -char *strcat(char *dest, const char *src) +char *LIBCLAYER(strcat)(char *dest, const char *src) { char *const dest_start = dest; while (*dest) ++dest; @@ -58,33 +58,33 @@ char *strcat(char *dest, const char *src) return dest_start; } -char *strchr(const char *s, int c) +char *LIBCLAYER(strchr)(const char *s, int c) { for (; *s != (char)c; ++s) if (*s == '\0') return NULL; return (char*)s; } -int strcmp(const char *s1, const char *s2) +int LIBCLAYER(strcmp)(const char *s1, const char *s2) { for (; *s1; ++s1, ++s2) if (*s1 != *s2) return *s1 < *s2 ? -1 : 1; return 0; } -char *strcpy(char *dest, const char *src) +char *LIBCLAYER(strcpy)(char *dest, const char *src) { char *tmp = dest; while ((*dest++ = *src++) != '\0'); return tmp; } -size_t strlen(const char *s) +size_t LIBCLAYER(strlen)(const char *s) { const char *ss = s; while (*ss != '\0') ++ss; return ss - s; } -char *strncat(char *dest, const char *src, size_t n) +char *LIBCLAYER(strncat)(char *dest, const char *src, size_t n) { char *const dest_start = dest; if (n) { @@ -99,35 +99,37 @@ char *strncat(char *dest, const char *src, size_t n) return dest_start; } -int strncmp(const char *s1, const char *s2, size_t n) +int LIBCLAYER(strncmp)(const char *s1, const char *s2, size_t n) { for (; *s1 && n; ++s1, ++s2, --n) if (*s1 != *s2) return *s1 < *s2 ? -1 : 1; return 0; } -char *strncpy(char *dest, const char *src, size_t n) +char *LIBCLAYER(strncpy)(char *dest, const char *src, size_t n) { char *tmp = dest; while (n-- && (*dest++ = *src++) != '\0'); return tmp; } -size_t strnlen(const char *s, size_t maxlen) +size_t LIBCLAYER(strnlen)(const char *s, size_t maxlen) { const char *ss = s; while (*ss != '\0' && maxlen--) ++ss; return ss - s; } -char *strstr(const char *haystack, const char *needle) +char *LIBCLAYER(strstr)(const char *haystack, const char *needle) { - const size_t needle_slen = strlen(needle); + const size_t needle_slen = LIBCLAYER(strlen)(needle); if (!needle_slen) return (char*)haystack; - size_t haystack_slen = strlen(haystack); + size_t haystack_slen = LIBCLAYER(strlen)(haystack); while (haystack_slen >= needle_slen) { --haystack_slen; - if (!memcmp(haystack, needle, needle_slen)) return (char*)haystack; + if (!LIBCLAYER(memcmp)(haystack, needle, needle_slen)) { + return (char*)haystack; + } ++haystack; } diff --git a/tests/.gitignore b/tests/.gitignore new file mode 100644 index 0000000..390c071 --- /dev/null +++ b/tests/.gitignore @@ -0,0 +1 @@ +/string diff --git a/tests/Makefile.am b/tests/Makefile.am index 7a1cd46..789a8fd 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -3,3 +3,7 @@ include $(top_srcdir)/make/shared.am CLEANFILES = TESTS = noinst_PROGRAMS = $(TESTS) + +TESTS += string +string_LDADD = $(top_builddir)/libclayer.la +string_SOURCES = main.c string.c diff --git a/tests/main.c b/tests/main.c index b0ba2ac..11c3081 100644 --- a/tests/main.c +++ b/tests/main.c @@ -2,12 +2,10 @@ #include "config.h" #endif -#include - void test_main(int argc, char **argv); int main(int argc, char **argv) { test_main(argc, argv); - exit(EXIT_SUCCESS); + return 0; } diff --git a/tests/string.c b/tests/string.c new file mode 100644 index 0000000..69c30f9 --- /dev/null +++ b/tests/string.c @@ -0,0 +1,20 @@ +#include +#include +#include + +void test_main() +{ + { + const size_t count = 1024; + char aaa[count]; + char bbb[count]; + for (size_t i = 0; i < count; ++i) aaa[i] = bbb[i] = i*i; + assert(LIBCLAYER(memcmp)(aaa, bbb, count) == 0); + aaa[123] = 1; + bbb[123] = 2; + assert(LIBCLAYER(memcmp)(aaa, bbb, count) == -1); + aaa[123] = 2; + bbb[123] = 1; + assert(LIBCLAYER(memcmp)(aaa, bbb, count) == 1); + } +}