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
|
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "cfg_tokenizer.h"
struct Result {
enum Token type;
const char *value;
};
struct Test {
const char *config;
struct Result *results;
int len;
};
static char config1[] = "# Comment\n"
"numbers {\n"
" one\n"
" two\n"
" three\n"
" \"[0-9a-z-]+\\.edu\"\n"
"}";
static struct Result results1[] = {
{ TOKEN_EOL, NULL },
{ TOKEN_WORD, "numbers" },
{ TOKEN_OBRACE, NULL },
{ TOKEN_EOL, NULL },
{ TOKEN_WORD, "one" },
{ TOKEN_EOL, NULL },
{ TOKEN_WORD, "two" },
{ TOKEN_EOL, NULL },
{ TOKEN_WORD, "three" },
{ TOKEN_EOL, NULL },
{ TOKEN_WORD, "[0-9a-z-]+.edu" },
{ TOKEN_EOL, NULL },
{ TOKEN_CBRACE, NULL },
{ TOKEN_END, NULL },
};
static struct Test tests[] = {
{ config1, results1, sizeof(results1) / sizeof(struct Result) },
{ NULL, NULL, 0 } /* End of tests */
};
int main() {
FILE *cfg;
char buffer[256];
enum Token token;
struct Test *test;
int i;
cfg = tmpfile();
if (cfg == NULL) {
perror("tmpfile");
return 1;
}
for (test = tests; test->config; test++) {
fprintf(cfg, "%s", test->config);
rewind(cfg);
for (i = 0; i < test->len; i++) {
token = next_token(cfg, buffer, sizeof(buffer));
assert(token == test->results[i].type);
if (test->results[i].value)
assert(strncmp(buffer, test->results[i].value, sizeof(buffer)) == 0);
}
rewind(cfg);
}
fclose(cfg);
return (0);
}
|