1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
| #include <stdio.h>
#include <stdlib.h>
typedef enum e_error {
ERROR_NONE = 0,
ERROR_END,
ERROR_EMPTY_PARAMETER,
ERROR_FIRST_PART,
ERROR_FIRST_PART_SIZE,
ERROR_SEPARATOR,
ERROR_SECOND_PART,
ERROR_SECOND_PART_SIZE
} t_error;
char* get_error_str(t_error error) {
switch (error) {
case ERROR_NONE: return "ERROR_NONE"; break;
case ERROR_END: return "ERROR_END"; break;
case ERROR_EMPTY_PARAMETER: return "ERROR_EMPTY_PARAMETER"; break;
case ERROR_FIRST_PART: return "ERROR_FIRST_PART"; break;
case ERROR_FIRST_PART_SIZE: return "ERROR_FIRST_PART_SIZE"; break;
case ERROR_SEPARATOR: return "ERROR_SEPARATOR"; break;
case ERROR_SECOND_PART: return "ERROR_SECOND_PART"; break;
case ERROR_SECOND_PART_SIZE: return "ERROR_SECOND_PART_SIZE"; break;
}
return "unknow error";
}
unsigned char test_number(char* str, size_t n, size_t m, char sep, t_error* error) {
unsigned char ret;
if (str != NULL) {
char* tmp_str=str;
size_t count;
char c;
ret = 1;
count = 0;
c = (*tmp_str);
while (ret & (c != '\0') && (count < n)) {
if ((c >= '0') && (c <= '9')) {
// printf("%c - ok [first part]\n", c);
++tmp_str;
++count;
c = (*tmp_str);
} else {
ret = 0;
if (error != NULL) { (*error) = ERROR_FIRST_PART; }
// printf("%c - nok [first part]\n", c);
}
}
if (ret) {
c = (*tmp_str);
if (c == sep) {
++tmp_str;
} else if (c != '\0') {
ret = 0;
if (error != NULL) { (*error) = (((c >= '0') && (c <= '9'))? ERROR_FIRST_PART_SIZE: ERROR_SEPARATOR); }
// printf("%c - nok [seperator]\n", c);
}
}
count = 0;
c = (*tmp_str);
while (ret & (c != '\0') && (count < m)) {
if ((c >= '0') && (c <= '9')) {
// printf("%c - ok [second part]\n", c);
++tmp_str;
++count;
c = (*tmp_str);
} else {
ret = 0;
if (error != NULL) { (*error) = ERROR_SECOND_PART; }
// printf("%c - nok [second part]\n", c);
}
}
if (ret) {
if (c == '\0') {
if (error != NULL) { (*error) = ERROR_NONE; }
} else {
ret = 0;
if (error != NULL) { (*error) = (((c >= '0') && (c <= '9'))? ERROR_SECOND_PART_SIZE: ERROR_END); }
}
}
} else {
ret = 0;
if (error != NULL) { (*error) = ERROR_EMPTY_PARAMETER; }
}
return ret;
}
#define TEST_OK(STR, N, M, SEP, TEXT_STR) \
if ( test_number(STR, N, M, SEP, &error) ) { \
printf("%s ok\n", TEXT_STR); \
} else { \
printf("%s nok - %s\n", TEXT_STR, get_error_str(error)); \
}
#define TEST_NOK(STR, N, M, SEP, TEXT_STR, ERR0R) \
if ((!test_number(STR, N, M, SEP, &error)) && (error == ERR0R)) { \
printf("%s ok\n", TEXT_STR); \
} else { \
printf("%s nok - %s\n", TEXT_STR, get_error_str(error)); \
}
int main()
{
t_error error;
TEST_OK("125", 3, 4, '.', "test first part ");
TEST_OK("125.", 3, 4, '.', "test first part + sep ");
TEST_OK("125.44", 3, 4, '.', "test full ");
TEST_NOK("12a.44", 3, 4, '.', "test error first part ", ERROR_FIRST_PART);
TEST_NOK("125a44", 3, 4, '.', "test error sep ", ERROR_SEPARATOR);
TEST_NOK("125.4a", 3, 4, '.', "test error second part ", ERROR_SECOND_PART);
TEST_NOK("1258.459", 3, 4, '.', "test error first part too long ", ERROR_FIRST_PART_SIZE);
TEST_NOK("125.45974", 3, 4, '.', "test error second part too long", ERROR_SECOND_PART_SIZE);
// Others tests
return EXIT_SUCCESS;
} |
Partager