| 1 |
/* HEADER Testing copy_nword */ |
| 2 |
|
| 3 |
#define ASSERT1(cond) \ |
| 4 |
do { \ |
| 5 |
++__test_counter; \ |
| 6 |
if ((cond)) \ |
| 7 |
printf("ok %d\n", __test_counter); \ |
| 8 |
else { \ |
| 9 |
printf("not ok %d - %d: %s failed\n", \ |
| 10 |
__test_counter, __LINE__, #cond); \ |
| 11 |
} \ |
| 12 |
} while (0) |
| 13 |
|
| 14 |
#define ASSERT2(cond, on_error) \ |
| 15 |
do { \ |
| 16 |
++__test_counter; \ |
| 17 |
if ((cond)) \ |
| 18 |
printf("ok %d\n", __test_counter); \ |
| 19 |
else { \ |
| 20 |
printf("not ok %d - %d: %s failed, ", \ |
| 21 |
__test_counter, __LINE__, #cond); \ |
| 22 |
printf on_error ; \ |
| 23 |
} \ |
| 24 |
} while (0) |
| 25 |
|
| 26 |
{ |
| 27 |
/* A quoted string */ |
| 28 |
char input[] = "\"The red rose\""; |
| 29 |
char output[sizeof(input)] = ""; |
| 30 |
char* run = copy_nword(input, output, sizeof(output)); |
| 31 |
ASSERT2(strcmp(output, "The red rose") == 0, |
| 32 |
("output = >%s<\n", output)); |
| 33 |
ASSERT1(run == NULL); |
| 34 |
} |
| 35 |
|
| 36 |
{ |
| 37 |
/* Escaped quotes */ |
| 38 |
char input[] = "\\\"The red rose\\\""; |
| 39 |
char output[sizeof(input)] = ""; |
| 40 |
char* run = copy_nword(input, output, sizeof(output)); |
| 41 |
ASSERT2(strcmp(output, "\"The") == 0, ("output = >%s<\n", output)); |
| 42 |
ASSERT2(run == input + 6, ("run = input + %d\n", (int)(run - input))); |
| 43 |
run = copy_nword(run, output, sizeof(output)); |
| 44 |
ASSERT2(strcmp(output, "red") == 0, ("output = >%s<\n", output)); |
| 45 |
ASSERT2(run == input + 10, ("run = input + %d\n", (int)(run - input))); |
| 46 |
run = copy_nword(run, output, sizeof(output)); |
| 47 |
ASSERT2(strcmp(output, "rose\"") == 0, ("output = >%s<\n", output)); |
| 48 |
ASSERT1(run == NULL); |
| 49 |
} |
| 50 |
|
| 51 |
{ |
| 52 |
/* Unterminated "-quote */ |
| 53 |
char input[] = "\"The"; |
| 54 |
char output[sizeof(input)] = ""; |
| 55 |
char* run = copy_nword(input, output, sizeof(output)); |
| 56 |
ASSERT2(strcmp(output, "The") == 0, ("output = >%s<\n", output)); |
| 57 |
ASSERT1(run == NULL); |
| 58 |
} |
| 59 |
|
| 60 |
{ |
| 61 |
/* Unterminated '-quote */ |
| 62 |
char input[] = "\'The"; |
| 63 |
char output[sizeof(input)] = ""; |
| 64 |
char* run = copy_nword(input, output, sizeof(output)); |
| 65 |
ASSERT2(strcmp(output, "The") == 0, ("output = >%s<\n", output)); |
| 66 |
ASSERT1(run == NULL); |
| 67 |
} |
| 68 |
|
| 69 |
{ |
| 70 |
/* Extract from NULL */ |
| 71 |
char output[10] = ""; |
| 72 |
char* run = NULL; |
| 73 |
run = copy_nword(run, output, sizeof(output)); |
| 74 |
ASSERT1(run == NULL); |
| 75 |
} |
| 76 |
|
| 77 |
{ |
| 78 |
/* Extract to NULL */ |
| 79 |
char input[] = "The red rose"; |
| 80 |
char* output = NULL; |
| 81 |
char* run = copy_nword(input, output, sizeof(output)); |
| 82 |
ASSERT1(run == NULL); |
| 83 |
} |
| 84 |
|
| 85 |
{ |
| 86 |
/* Long token */ |
| 87 |
char input[] = "\"Very long token that overflows the buffer\" foo"; |
| 88 |
char output[10] = ""; |
| 89 |
char* run = copy_nword(input, output, sizeof(output)); |
| 90 |
ASSERT2(strcmp(output, "Very long") == 0, ("output = >%s<\n", output)); |
| 91 |
ASSERT2(run == input + 44, ("run = input + %d\n", (int)(run - input))); |
| 92 |
} |
| 93 |
|
| 94 |
{ |
| 95 |
/* Quoted end of string / embedded \0 */ |
| 96 |
char input[] = "The\\\0red rose"; |
| 97 |
char output[sizeof(input)] = ""; |
| 98 |
char* run = copy_nword(input, output, sizeof(output)); |
| 99 |
ASSERT2(strcmp(output, "The\\") == 0, ("output = >%s<\n", output)); |
| 100 |
ASSERT1(run == NULL); |
| 101 |
} |
| 102 |
|
| 103 |
{ |
| 104 |
/* Empty string */ |
| 105 |
char input[] = ""; |
| 106 |
char output[sizeof(input) + 1] = "X"; |
| 107 |
char* run = copy_nword(input, output, sizeof(output)); |
| 108 |
ASSERT2(strcmp(output, "") == 0, ("output = >%s<\n", output)); |
| 109 |
ASSERT2(run == NULL, ("run = >%s<\n", run)); |
| 110 |
} |
| 111 |
|
| 112 |
{ |
| 113 |
/* Whitespace string */ |
| 114 |
char input[] = " \t "; |
| 115 |
char output[sizeof(input)] = "X"; |
| 116 |
char* run = copy_nword(input, output, sizeof(output)); |
| 117 |
ASSERT2(strcmp(output, "") == 0, ("output = >%s<\n", output)); |
| 118 |
ASSERT2(run == NULL, ("run = >%s<\n", run)); |
| 119 |
} |
| 120 |
|
| 121 |
{ |
| 122 |
/* Quote, no whitespace after */ |
| 123 |
char input[] = "\"The\"red rose"; |
| 124 |
char output[sizeof(input)] = ""; |
| 125 |
char* run = copy_nword(input, output, sizeof(output)); |
| 126 |
ASSERT2(strcmp(output, "The") == 0, ("output = >%s<\n", output)); |
| 127 |
ASSERT2(run == input + 5, ("run = input + %d\n", (int)(run - input))); |
| 128 |
} |
| 129 |
|
| 130 |
{ |
| 131 |
/* Quote, no whitespace before */ |
| 132 |
char input[] = "The\"red\" rose"; |
| 133 |
char output[sizeof(input)] = ""; |
| 134 |
char* run = copy_nword(input, output, sizeof(output)); |
| 135 |
ASSERT2(strcmp(output, "The\"red\"") == 0, ("output = >%s<\n", output)); |
| 136 |
ASSERT2(run == input + 9, ("run = input + %d\n", (int)(run - input))); |
| 137 |
} |