diff --git a/x_string/src/Makefile b/x_string/src/Makefile new file mode 100644 index 0000000..6dd9352 --- /dev/null +++ b/x_string/src/Makefile @@ -0,0 +1,22 @@ +.POSIX: + + +TEST_SRC = $(wildcard tests/*c) +TEST_OBJ = $(TEST_SRC:tests/%.c=tests/%.o) +SRC = x_string.c +OBJ = $(SRC:.c=.o) +CFLAGS = -ggdb -Wall -Wextra -I. + + +run_tests: $(TEST_OBJ) $(OBJ) + $(CC) $(CFLAGS) -o $@ $(TEST_OBJ) $(OBJ) + +$(OBJ): + +$(TEST_OBJ): $(TEST_SRC) + $(CC) $(CFLAGS) -c $(TEST_SRC) -o $@ + +clean: + rm -f test $(OBJ) $(TEST_OBJ) + +.PHONY: all clean_all clean install uninstall diff --git a/x_string/src/tests/str_cmp.c b/x_string/src/tests/str_cmp.c new file mode 100644 index 0000000..0b6cc08 --- /dev/null +++ b/x_string/src/tests/str_cmp.c @@ -0,0 +1,85 @@ +#include +#include "x_string.h" +#include + +#define NUM_TESTS 5 + + +int check_strcpy(){ + char src[100] = "hello world\0"; + char dest[100]; + + x_strcpy(dest, src, 100); + + if ( strcmp(dest, src) == 0 ){ + return 0; + } + + return -1; +} + +int check_x_strcmp_when_equal(){ + char str1[100] = "hello world\0"; + char str2[100] = "hello world\0"; + + if ( x_strcmp(str1, str2) == 0 ){ + return 0; + } + + return 1; +} + +int check_x_strcmp_when_not_equal(){ + char str1[100] = "hello world\0"; + char str2[100] = "goodbye sun\0"; + + if ( x_strcmp(str1, str2) == -1 ){ + return 0; + } + + return 1; +} + +int check_x_strcmp_when_equal_start_but_not_equal(){ + char str1[100] = "hello world\0"; + char str2[100] = "hello w\0"; + + if ( x_strcmp(str1, str2) == -1 ){ + return 0; + } + + return 1; +} + +int fail(){ + return -1; +} + + + +int main(){ + int i = 0; + int (*tester[NUM_TESTS])(); + int errors = 0; + + tester[0] = check_strcpy; + tester[1] = check_x_strcmp_when_equal; + tester[2] = check_x_strcmp_when_not_equal; + tester[3] = check_x_strcmp_when_equal_start_but_not_equal; + tester[4] = fail; + + for ( i = 0; i < NUM_TESTS; i++ ){ + printf("Test #%d...", i); + if ( tester[i]() == 0){ + printf("passed\n"); + } + else { + printf("failed\n"); + errors++; + } + } + + printf("Tests completed with %d passed and %d failed\n", NUM_TESTS - errors, errors); + + return 0; +}