Added tests for x_string
This commit is contained in:
parent
bda49793fa
commit
ad2aa8db06
22
x_string/src/Makefile
Normal file
22
x_string/src/Makefile
Normal 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
|
85
x_string/src/tests/str_cmp.c
Normal file
85
x_string/src/tests/str_cmp.c
Normal 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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user