Compare commits
No commits in common. "a3a700a2c045a6d55e40ab92fb9e3f3fb4a60848" and "bda49793fa5c73e9360ce463c23dda21175efd5c" have entirely different histories.
a3a700a2c0
...
bda49793fa
@ -1,22 +0,0 @@
|
|||||||
.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
|
|
@ -1,85 +0,0 @@
|
|||||||
#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;
|
|
||||||
}
|
|
@ -1,7 +1,6 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
// return the length of string to the null terminator
|
|
||||||
int x_strlen(char* str){
|
int x_strlen(char* str){
|
||||||
char* ptr = str;
|
char* ptr = str;
|
||||||
while(*ptr != '\0'){
|
while(*ptr != '\0'){
|
||||||
@ -10,8 +9,6 @@ int x_strlen(char* str){
|
|||||||
return ptr - str;
|
return ptr - str;
|
||||||
}
|
}
|
||||||
|
|
||||||
// copy one string to another destination
|
|
||||||
// must include the size of the destination
|
|
||||||
void x_strcpy(char* dest, char* src, const int size_dest){
|
void x_strcpy(char* dest, char* src, const int size_dest){
|
||||||
char* dest_ptr = dest;
|
char* dest_ptr = dest;
|
||||||
char* src_ptr = src;
|
char* src_ptr = src;
|
||||||
@ -29,32 +26,7 @@ void x_strcpy(char* dest, char* src, const int size_dest){
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
// compare two strings
|
|
||||||
// return 0 if equal to eachother
|
|
||||||
// return -1 if not
|
|
||||||
int x_strcmp (char* str1, char* str2){
|
|
||||||
char* ptr1 = str1;
|
|
||||||
char* ptr2 = str2;
|
|
||||||
|
|
||||||
while(*ptr1 != '\0' && *ptr2 != '\0'){
|
|
||||||
if(*ptr1 != *ptr2){
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
ptr1++;
|
|
||||||
ptr2++;
|
|
||||||
}
|
|
||||||
|
|
||||||
// if havent reached the end of one of the strings
|
|
||||||
if (*ptr1 != '\0' || *ptr2 != '\0'){
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// concatinate two strings
|
|
||||||
char* x_strconcat(char* str1, char* str2){
|
char* x_strconcat(char* str1, char* str2){
|
||||||
|
|
||||||
char* new_string;
|
char* new_string;
|
||||||
|
@ -3,7 +3,6 @@
|
|||||||
|
|
||||||
int x_strlen(char*);
|
int x_strlen(char*);
|
||||||
void x_strcpy(char*, char*, const int);
|
void x_strcpy(char*, char*, const int);
|
||||||
int x_strcmp(char*, char*);
|
|
||||||
char* x_strconcat(char*, char*);
|
char* x_strconcat(char*, char*);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user