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
| #include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*****************************************************************************/
/*********************************** Debug **********************************/
/*****************************************************************************/
// active debug : gcc -Wall XXX.c
// or : gcc -Wall XXX.c -DD_VERBOSE=1
// deactivate debug : gcc -Wall XXX.c -DD_VERBOSE=0
#ifndef D_VERBOSE
#define D_VERBOSE 1
#endif
#if (D_VERBOSE)
#define DEBUG( XXX ) printf XXX ;
#else
#define DEBUG( XXX )
#endif
#define test__ok(TEST_NB, TEST_SRC, SRC_STR, FACTOR_STR) \
{ \
t_word src = { SRC_STR }; \
t_word factor = { FACTOR_STR }; \
\
if ( is_factor(src, factor) ) { \
printf("%02lu.%s : SUCCEED\n", TEST_NB, TEST_SRC); \
} else { \
printf("%02lu.%s : FAILED\n", TEST_NB, TEST_SRC); \
} \
}
#define test_nok(TEST_NB, TEST_SRC, SRC_STR, FACTOR_STR) \
{ \
t_word src = { SRC_STR }; \
t_word factor = { FACTOR_STR }; \
\
if ( !is_factor(src, factor) ) { \
printf("%02lu.%s : SUCCEED\n", TEST_NB, TEST_SRC); \
} else { \
printf("%02lu.%s : FAILED\n", TEST_NB, TEST_SRC); \
} \
}
/*****************************************************************************/
/*********************************** Unit **********************************/
/*****************************************************************************/
#define NMAX 300
typedef struct {
char m[NMAX];
} t_word;
int is_factor(t_word src, t_word factor) {
size_t fact_len, src_len;
size_t fact_pos, src_pos;
unsigned char b_is_not_factor;
fact_len = strlen(factor.m);
src_len = (strlen(src.m) - fact_len);
// assert if (fact_len != 0) && (src_len != 0)
// assert if (fact_len < src_len)
b_is_not_factor = 1;
for(src_pos=0; (b_is_not_factor && (src_pos <= src_len)); ++src_pos){
DEBUG( ("is_factor - debug: test 01 between %c and %c\n", src.m[src_pos], factor.m[0]) )
if (src.m[src_pos] == factor.m[0]) {
if (fact_len > 1) {
fact_pos = 1;
do {
if(src.m[src_pos + fact_pos] == factor.m[fact_pos]) {
DEBUG( ("is_factor - debug: test 02 between %c and %c\n", src.m[src_pos + fact_pos], factor.m[fact_pos]) )
++fact_pos;
}
} while (b_is_not_factor && (fact_pos < (fact_len - 1)));
if ((fact_pos == (fact_len - 1)) && (src.m[src_pos + fact_pos] == factor.m[fact_pos])) {
DEBUG( ("is_factor - debug: test 03 between %c and %c (find)\n", src.m[src_pos + fact_pos], factor.m[fact_pos]) )
b_is_not_factor = 0;
} else {
DEBUG( ("is_factor - debug: test 03 between %c and %c (%lu <- %lu)\n", src.m[src_pos + fact_pos], factor.m[fact_pos], fact_pos, fact_len) )
}
} else {
DEBUG( ("is_factor - debug: find\n") )
b_is_not_factor = 0;
}
}
}
return (!b_is_not_factor);
}
/*****************************************************************************/
/*********************************** Main **********************************/
/*****************************************************************************/
int main(int argc, char** argv)
{
size_t count;
count = 0;
count++; test__ok(count, "test true ", "polycopie", "ycop")
count++; test__ok(count, "test true (first) ", "polycopie", "pol")
count++; test__ok(count, "test true (last) ", "polycopie", "copie")
count++; test__ok(count, "test true, src == factor ", "polycopie", "polycopie")
count++; test_nok(count, "test false ", "polycopie", "copa")
count++; test__ok(count, "test true, len == 1 ", "polycopie", "o")
count++; test__ok(count, "test true, len == 1 (first)", "polycopie", "p")
count++; test__ok(count, "test true, len == 1 (last) ", "polycopie", "e")
count++; test_nok(count, "test false, len == 1 ", "polycopie", "z")
count++; test__ok(count, "test pattern true ", "copcopcopicop", "copi")
count++; test_nok(count, "test pattern false ", "copcopcopicop", "copa")
return EXIT_SUCCESS;
} |
Partager