|
Packit |
fcad23 |
/* HEADER Testing copy_nword */
|
|
Packit |
fcad23 |
|
|
Packit |
fcad23 |
#define ASSERT1(cond) \
|
|
Packit |
fcad23 |
do { \
|
|
Packit |
fcad23 |
++__test_counter; \
|
|
Packit |
fcad23 |
if ((cond)) \
|
|
Packit |
fcad23 |
printf("ok %d\n", __test_counter); \
|
|
Packit |
fcad23 |
else { \
|
|
Packit |
fcad23 |
printf("not ok %d - %d: %s failed\n", \
|
|
Packit |
fcad23 |
__test_counter, __LINE__, #cond); \
|
|
Packit |
fcad23 |
} \
|
|
Packit |
fcad23 |
} while (0)
|
|
Packit |
fcad23 |
|
|
Packit |
fcad23 |
#define ASSERT2(cond, on_error) \
|
|
Packit |
fcad23 |
do { \
|
|
Packit |
fcad23 |
++__test_counter; \
|
|
Packit |
fcad23 |
if ((cond)) \
|
|
Packit |
fcad23 |
printf("ok %d\n", __test_counter); \
|
|
Packit |
fcad23 |
else { \
|
|
Packit |
fcad23 |
printf("not ok %d - %d: %s failed, ", \
|
|
Packit |
fcad23 |
__test_counter, __LINE__, #cond); \
|
|
Packit |
fcad23 |
printf on_error ; \
|
|
Packit |
fcad23 |
} \
|
|
Packit |
fcad23 |
} while (0)
|
|
Packit |
fcad23 |
|
|
Packit |
fcad23 |
{
|
|
Packit |
fcad23 |
/* A quoted string */
|
|
Packit |
fcad23 |
char input[] = "\"The red rose\"";
|
|
Packit |
fcad23 |
char output[sizeof(input)] = "";
|
|
Packit |
fcad23 |
char* run = copy_nword(input, output, sizeof(output));
|
|
Packit |
fcad23 |
ASSERT2(strcmp(output, "The red rose") == 0,
|
|
Packit |
fcad23 |
("output = >%s<\n", output));
|
|
Packit |
fcad23 |
ASSERT1(run == NULL);
|
|
Packit |
fcad23 |
}
|
|
Packit |
fcad23 |
|
|
Packit |
fcad23 |
{
|
|
Packit |
fcad23 |
/* Escaped quotes */
|
|
Packit |
fcad23 |
char input[] = "\\\"The red rose\\\"";
|
|
Packit |
fcad23 |
char output[sizeof(input)] = "";
|
|
Packit |
fcad23 |
char* run = copy_nword(input, output, sizeof(output));
|
|
Packit |
fcad23 |
ASSERT2(strcmp(output, "\"The") == 0, ("output = >%s<\n", output));
|
|
Packit |
fcad23 |
ASSERT2(run == input + 6, ("run = input + %d\n", (int)(run - input)));
|
|
Packit |
fcad23 |
run = copy_nword(run, output, sizeof(output));
|
|
Packit |
fcad23 |
ASSERT2(strcmp(output, "red") == 0, ("output = >%s<\n", output));
|
|
Packit |
fcad23 |
ASSERT2(run == input + 10, ("run = input + %d\n", (int)(run - input)));
|
|
Packit |
fcad23 |
run = copy_nword(run, output, sizeof(output));
|
|
Packit |
fcad23 |
ASSERT2(strcmp(output, "rose\"") == 0, ("output = >%s<\n", output));
|
|
Packit |
fcad23 |
ASSERT1(run == NULL);
|
|
Packit |
fcad23 |
}
|
|
Packit |
fcad23 |
|
|
Packit |
fcad23 |
{
|
|
Packit |
fcad23 |
/* Unterminated "-quote */
|
|
Packit |
fcad23 |
char input[] = "\"The";
|
|
Packit |
fcad23 |
char output[sizeof(input)] = "";
|
|
Packit |
fcad23 |
char* run = copy_nword(input, output, sizeof(output));
|
|
Packit |
fcad23 |
ASSERT2(strcmp(output, "The") == 0, ("output = >%s<\n", output));
|
|
Packit |
fcad23 |
ASSERT1(run == NULL);
|
|
Packit |
fcad23 |
}
|
|
Packit |
fcad23 |
|
|
Packit |
fcad23 |
{
|
|
Packit |
fcad23 |
/* Unterminated '-quote */
|
|
Packit |
fcad23 |
char input[] = "\'The";
|
|
Packit |
fcad23 |
char output[sizeof(input)] = "";
|
|
Packit |
fcad23 |
char* run = copy_nword(input, output, sizeof(output));
|
|
Packit |
fcad23 |
ASSERT2(strcmp(output, "The") == 0, ("output = >%s<\n", output));
|
|
Packit |
fcad23 |
ASSERT1(run == NULL);
|
|
Packit |
fcad23 |
}
|
|
Packit |
fcad23 |
|
|
Packit |
fcad23 |
{
|
|
Packit |
fcad23 |
/* Extract from NULL */
|
|
Packit |
fcad23 |
char output[10] = "";
|
|
Packit |
fcad23 |
char* run = NULL;
|
|
Packit |
fcad23 |
run = copy_nword(run, output, sizeof(output));
|
|
Packit |
fcad23 |
ASSERT1(run == NULL);
|
|
Packit |
fcad23 |
}
|
|
Packit |
fcad23 |
|
|
Packit |
fcad23 |
{
|
|
Packit |
fcad23 |
/* Extract to NULL */
|
|
Packit |
fcad23 |
char input[] = "The red rose";
|
|
Packit |
fcad23 |
char* output = NULL;
|
|
Packit |
fcad23 |
char* run = copy_nword(input, output, sizeof(output));
|
|
Packit |
fcad23 |
ASSERT1(run == NULL);
|
|
Packit |
fcad23 |
}
|
|
Packit |
fcad23 |
|
|
Packit |
fcad23 |
{
|
|
Packit |
fcad23 |
/* Long token */
|
|
Packit |
fcad23 |
char input[] = "\"Very long token that overflows the buffer\" foo";
|
|
Packit |
fcad23 |
char output[10] = "";
|
|
Packit |
fcad23 |
char* run = copy_nword(input, output, sizeof(output));
|
|
Packit |
fcad23 |
ASSERT2(strcmp(output, "Very long") == 0, ("output = >%s<\n", output));
|
|
Packit |
fcad23 |
ASSERT2(run == input + 44, ("run = input + %d\n", (int)(run - input)));
|
|
Packit |
fcad23 |
}
|
|
Packit |
fcad23 |
|
|
Packit |
fcad23 |
{
|
|
Packit |
fcad23 |
/* Quoted end of string / embedded \0 */
|
|
Packit |
fcad23 |
char input[] = "The\\\0red rose";
|
|
Packit |
fcad23 |
char output[sizeof(input)] = "";
|
|
Packit |
fcad23 |
char* run = copy_nword(input, output, sizeof(output));
|
|
Packit |
fcad23 |
ASSERT2(strcmp(output, "The\\") == 0, ("output = >%s<\n", output));
|
|
Packit |
fcad23 |
ASSERT1(run == NULL);
|
|
Packit |
fcad23 |
}
|
|
Packit |
fcad23 |
|
|
Packit |
fcad23 |
{
|
|
Packit |
fcad23 |
/* Empty string */
|
|
Packit |
fcad23 |
char input[] = "";
|
|
Packit |
fcad23 |
char output[sizeof(input) + 1] = "X";
|
|
Packit |
fcad23 |
char* run = copy_nword(input, output, sizeof(output));
|
|
Packit |
fcad23 |
ASSERT2(strcmp(output, "") == 0, ("output = >%s<\n", output));
|
|
Packit |
fcad23 |
ASSERT2(run == NULL, ("run = >%s<\n", run));
|
|
Packit |
fcad23 |
}
|
|
Packit |
fcad23 |
|
|
Packit |
fcad23 |
{
|
|
Packit |
fcad23 |
/* Whitespace string */
|
|
Packit |
fcad23 |
char input[] = " \t ";
|
|
Packit |
fcad23 |
char output[sizeof(input)] = "X";
|
|
Packit |
fcad23 |
char* run = copy_nword(input, output, sizeof(output));
|
|
Packit |
fcad23 |
ASSERT2(strcmp(output, "") == 0, ("output = >%s<\n", output));
|
|
Packit |
fcad23 |
ASSERT2(run == NULL, ("run = >%s<\n", run));
|
|
Packit |
fcad23 |
}
|
|
Packit |
fcad23 |
|
|
Packit |
fcad23 |
{
|
|
Packit |
fcad23 |
/* Quote, no whitespace after */
|
|
Packit |
fcad23 |
char input[] = "\"The\"red rose";
|
|
Packit |
fcad23 |
char output[sizeof(input)] = "";
|
|
Packit |
fcad23 |
char* run = copy_nword(input, output, sizeof(output));
|
|
Packit |
fcad23 |
ASSERT2(strcmp(output, "The") == 0, ("output = >%s<\n", output));
|
|
Packit |
fcad23 |
ASSERT2(run == input + 5, ("run = input + %d\n", (int)(run - input)));
|
|
Packit |
fcad23 |
}
|
|
Packit |
fcad23 |
|
|
Packit |
fcad23 |
{
|
|
Packit |
fcad23 |
/* Quote, no whitespace before */
|
|
Packit |
fcad23 |
char input[] = "The\"red\" rose";
|
|
Packit |
fcad23 |
char output[sizeof(input)] = "";
|
|
Packit |
fcad23 |
char* run = copy_nword(input, output, sizeof(output));
|
|
Packit |
fcad23 |
ASSERT2(strcmp(output, "The\"red\"") == 0, ("output = >%s<\n", output));
|
|
Packit |
fcad23 |
ASSERT2(run == input + 9, ("run = input + %d\n", (int)(run - input)));
|
|
Packit |
fcad23 |
}
|