From d37eb3feb226623431a464b189edf2fc2f31beed Mon Sep 17 00:00:00 2001 From: Alessandro Iezzi Date: Thu, 11 May 2023 17:26:15 +0200 Subject: Add tests --- test/Makefile | 35 +++++++++++++++++++++++++++++++++++ test/main.c | 14 ++++++++++++++ test/test1 | Bin 0 -> 16344 bytes test/test_split.c | 41 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 90 insertions(+) create mode 100644 test/Makefile create mode 100644 test/main.c create mode 100755 test/test1 create mode 100644 test/test_split.c diff --git a/test/Makefile b/test/Makefile new file mode 100644 index 0000000..fec56d9 --- /dev/null +++ b/test/Makefile @@ -0,0 +1,35 @@ +# See LICENSE file for copyright and license details. + +CC = cc +SRC != find * -name '*.c' +OBJ = ${SRC:.c=.o} + +LIBNAME = str2 +SHARED = ../lib${LIBNAME}.so + +#WARNINGS = -Wall -Werror -pedantic +STANDARD = -std=c99 +CFLAGS = ${WARNINGS} -I../src/ +LFLAGS = -l${LIBNAME} -L../ -Wl,-rpath=../ + +NAME = test1 + +all: ${SHARED} ${NAME} + @echo + @echo '+-----------------+' + @echo '| Executing tests |' + @echo '+-----------------+' + @echo + ./${NAME} + +${SHARED}: + @make -C .. + +.c.o: + ${CC} ${CFLAGS} -c $< -o $@ + +${NAME}: ${OBJ} + ${CC} ${LFLAGS} -o $@ ${OBJ} + +clean: + rm -f ${OBJ} ${NAME} *.core diff --git a/test/main.c b/test/main.c new file mode 100644 index 0000000..479d529 --- /dev/null +++ b/test/main.c @@ -0,0 +1,14 @@ +/* See LICENSE file for copyright and license details. */ + +#include + +void test_split1(); +void test_split2(); + +int main(int argc, char** argv) +{ + test_split1(); + test_split2(); + + return EXIT_SUCCESS; +} diff --git a/test/test1 b/test/test1 new file mode 100755 index 0000000..b83a949 Binary files /dev/null and b/test/test1 differ diff --git a/test/test_split.c b/test/test_split.c new file mode 100644 index 0000000..7b19caa --- /dev/null +++ b/test/test_split.c @@ -0,0 +1,41 @@ +/* See LICENSE file for copyright and license details. */ + +#include +#include +#include + +#define assert(c) if (c == 0) { printf("%s:%d\n", __FILE__, __LINE__); exit(1);} +#define log(str) printf("%s: %s\n", __func__, str); + +void +test_split1() +{ + log("Running..."); + + char *str = "STR_1\nSTR_2"; + int size; + char **tokens = strsplit(str, '\n', &size); + + assert(size == 2); + assert(strncmp("STR_1", tokens[0], 5) == 0); + assert(strncmp("STR_2", tokens[1], 5) == 0); + + log("OK\n"); +} + +void +test_split2() +{ + log("Running..."); + + char *str = "\nSTR_1\nSTR_2"; + int size; + char **tokens = strsplit(str, '\n', &size); + + assert(size == 3); + assert(strlen(tokens[0]) == 0); + assert(strncmp("STR_1", tokens[1], 5) == 0); + assert(strncmp("STR_2", tokens[2], 5) == 0); + + log("OK\n"); +} -- cgit v1.2.3