Added tests for x_string

This commit is contained in:
xavi 2024-09-07 23:26:33 -07:00
parent bda49793fa
commit ad2aa8db06
2 changed files with 107 additions and 0 deletions

22
x_string/src/Makefile Normal file
View File

@ -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

View File

@ -0,0 +1,85 @@
#include <stdio.h>
#include "x_string.h"
#include <string.h>
#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;
}