Compare commits

...

4 Commits

Author SHA1 Message Date
8ca828ebf6 Added tests for x_isdigit and x_isnumber 2024-09-22 17:48:51 -07:00
609d2d4d77 Added x_isnumber function 2024-09-22 17:48:37 -07:00
c69f18a479 Added x_is_digit function 2024-09-22 17:48:21 -07:00
5b3224c60c Made Makefile for x_ctypes 2024-09-22 17:47:04 -07:00
4 changed files with 218 additions and 0 deletions

25
x_ctypes/src/Makefile Normal file
View File

@ -0,0 +1,25 @@
.POSIX:
TEST_SRC = $(wildcard tests/*c)
TEST_OBJ = $(TEST_SRC:tests/%.c=tests/%.o)
SRC = x_ctypes.c
OBJ = $(SRC:.c=.o)
CFLAGS = -ggdb -Wall -Wextra -I.
run_testtest: test
./test
test: $(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 test

160
x_ctypes/src/tests/tests.c Normal file
View File

@ -0,0 +1,160 @@
#include <stdio.h>
#include "x_ctypes.h"
// printf Formating
#define X_RED "\x1B[31m"
#define X_GREEN "\x1B[32m"
#define X_RST "\x1B[0m"
// INSERT HEADERS FOR FUNCS
// SET THE NUMBER OF TESTS
#define NUM_TESTS 9
// DUMMY TEST
int fail(){
printf("%s... ", __func__);
return -1;
}
// DEFINE TESTS
int test_x_isdigit_with_char_is_ascii_3(){
printf("%s... ", __func__);
char x = '3';
if ( x_isdigit(x) ){
return 0;
}
return -1;
}
int test_x_isdigit_with_char_is_ascii_l(){
printf("%s... ", __func__);
char x = 'l';
if ( !x_isdigit(x) ){
return 0;
}
return -1;
}
int test_x_isdigit_with_int_4(){
printf("%s... ", __func__);
char x = 4;
if ( x_isdigit(x) ){
return 0;
}
return -1;
}
int test_x_isdigit_with_ascii_pound(){
printf("%s... ", __func__);
char x = '#';
if ( !x_isdigit(x) ){
return 0;
}
return -1;
}
int test_x_isnumber_with_ascii_1234(){
printf("%s... ", __func__);
char *str = "1234";
if ( x_isnumber(str) ){
return 0;
}
return -1;
}
int test_x_isnumber_with_ascii_abcd(){
printf("%s... ", __func__);
char *str = "abcd";
if ( !x_isnumber(str) ){
return 0;
}
return -1;
}
int test_x_isnumber_with_ascii_6(){
printf("%s... ", __func__);
char *str = "6";
if ( x_isnumber(str) ){
return 0;
}
return -1;
}
int test_x_isnumber_with_ascii_b(){
printf("%s... ", __func__);
char *str = "b";
if ( !x_isnumber(str) ){
return 0;
}
return -1;
}
// END OF TEST DEFINITIONS
void run_tests(int (**tests)()){
int i = 0;
int errors = 0;
for ( i = 0; i < NUM_TESTS; i++ ){
printf("Test #%d... ", i);
if ( tests[i]() == 0){
printf("%spassed%s\n", X_GREEN, X_RST);
}
else {
printf("%sfailed%s\n", X_RED, X_RST);
errors++;
}
}
printf("Tests completed with %s%d passed%s", X_GREEN, NUM_TESTS - errors, X_RST);
printf(" and %s%d failed%s\n", X_RED, errors, X_RST);
}
int main(){
int (*tests[NUM_TESTS])();
// PASS TESTS INTO ARRAY
tests[0] = fail;
tests[1] = test_x_isdigit_with_char_is_ascii_3;
tests[2] = test_x_isdigit_with_char_is_ascii_l;
tests[3] = test_x_isdigit_with_int_4;
tests[4] = test_x_isdigit_with_ascii_pound;
tests[5] = test_x_isnumber_with_ascii_1234;
tests[6] = test_x_isnumber_with_ascii_abcd;
tests[7] = test_x_isnumber_with_ascii_6;
tests[8] = test_x_isnumber_with_ascii_b;
// END OF PASSING TESTS
// RUN TESTS
run_tests(tests);
return 0;
}

24
x_ctypes/src/x_ctypes.c Normal file
View File

@ -0,0 +1,24 @@
#include <stdio.h>
int x_isdigit(const char x){
if ( x > 48 && x < 57 ){
return 1;
}
else if ( x > 0 && x < 9 ){
return 1;
}
return 0;
}
int x_isnumber(const char* str){
const char * ptr;
for (ptr = str; *ptr != '\0'; ptr++){
if ( !x_isdigit(*ptr) ){
return 0;
}
}
return 1;
}

9
x_ctypes/src/x_ctypes.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef X_CTYPES_H
#define X_CTYPES_H
// returns 0 if x is not a digit and 1 if it is
int x_isdigit(char x);
int x_isnumber(char *str);
#endif