mirror of
https://github.com/tailix/libclayer.git
synced 2024-11-20 11:06:24 -05:00
Namespace names & add tests
This commit is contained in:
parent
e2b9465803
commit
a813001808
15 changed files with 116 additions and 78 deletions
4
.gitignore
vendored
4
.gitignore
vendored
|
@ -64,8 +64,8 @@
|
|||
/conftest.err
|
||||
|
||||
/tests/test-suite.log
|
||||
/tests/test_*.log
|
||||
/tests/test_*.trs
|
||||
/tests/*.log
|
||||
/tests/*.trs
|
||||
|
||||
/Makefile
|
||||
/include/Makefile
|
||||
|
|
|
@ -5,13 +5,15 @@
|
|||
extern "C" {
|
||||
#endif
|
||||
|
||||
int isdigit(int c);
|
||||
int islower(int c);
|
||||
int isspace(int c);
|
||||
int isupper(int c);
|
||||
#include <libclayer.h>
|
||||
|
||||
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
|
||||
}
|
||||
|
|
|
@ -5,9 +5,11 @@
|
|||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <libclayer.h>
|
||||
|
||||
#define ERANGE 1
|
||||
|
||||
extern int errno;
|
||||
extern int LIBCLAYER(errno);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
|
@ -7,6 +7,14 @@ extern "C" {
|
|||
|
||||
#include <stddef.h>
|
||||
|
||||
#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);
|
||||
|
|
|
@ -5,6 +5,8 @@
|
|||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <libclayer.h>
|
||||
|
||||
// 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
|
||||
}
|
||||
|
|
|
@ -5,20 +5,21 @@
|
|||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <libclayer.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#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
|
||||
}
|
||||
|
|
|
@ -5,30 +5,31 @@
|
|||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <libclayer.h>
|
||||
#include <stddef.h>
|
||||
|
||||
// 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
|
||||
}
|
||||
|
|
16
src/ctype.c
16
src/ctype.c
|
@ -4,32 +4,32 @@
|
|||
|
||||
#include <ctype.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -4,4 +4,4 @@
|
|||
|
||||
#include <errno.h>
|
||||
|
||||
int errno = 0;
|
||||
int LIBCLAYER(errno) = 0;
|
||||
|
|
24
src/stdlib.c
24
src/stdlib.c
|
@ -9,22 +9,22 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
38
src/string.c
38
src/string.c
|
@ -5,7 +5,7 @@
|
|||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
1
tests/.gitignore
vendored
Normal file
1
tests/.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
/string
|
|
@ -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
|
||||
|
|
|
@ -2,12 +2,10 @@
|
|||
#include "config.h"
|
||||
#endif
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
void test_main(int argc, char **argv);
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
test_main(argc, argv);
|
||||
exit(EXIT_SUCCESS);
|
||||
return 0;
|
||||
}
|
||||
|
|
20
tests/string.c
Normal file
20
tests/string.c
Normal file
|
@ -0,0 +1,20 @@
|
|||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue