Added tests for x_isdigit and x_isnumber
This commit is contained in:
parent
609d2d4d77
commit
8ca828ebf6
160
x_ctypes/src/tests/tests.c
Normal file
160
x_ctypes/src/tests/tests.c
Normal 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;
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user