void test_suite_0(void) { LTSV *ltsv = ltsv_parse_file("tests/test_0.txt"); LTSV_Record *record; int ltsv_count; TEST(ltsv); if (!ltsv) { fprintf(stderr, "parse failed: tests/test_0.txt\n"); return; } ltsv_count = ltsv_get_count(ltsv); TEST(ltsv_count == 4); if (ltsv_count != 4) { fprintf(stderr, "ltsv count doesn't match!\n"); return; } record = ltsv_get_record(ltsv, 0); TEST(STREQ(ltsv_record_get_value(record, "a"), "05/02/2013:12:00:00 +0900")); TEST(STREQ(ltsv_record_get_value(record, "b"), "x.x.x.x")); TEST(STREQ(ltsv_record_get_value(record, "c"), "nn")); record = ltsv_get_record(ltsv, 1); TEST(STREQ(ltsv_record_get_value(record, "b"), "05/02/2013:12:01:00 +0900")); TEST(STREQ(ltsv_record_get_value(record, "c"), "x.y.x.y")); TEST(STREQ(ltsv_record_get_value(record, "a"), "nm")); record = ltsv_get_record(ltsv, 2); TEST(STREQ(ltsv_record_get_value(record, "c"), "05/02/2013:12:02:00 +0900")); TEST(STREQ(ltsv_record_get_value(record, "b"), "x.z.x.z")); TEST(STREQ(ltsv_record_get_value(record, "utf-8"), "あいうえお")); record = ltsv_get_record(ltsv, 3); TEST(STREQ(ltsv_record_get_value(record, "host"), "127.0.0.1")); TEST(STREQ(ltsv_record_get_value(record, "ident"), "-")); TEST(STREQ(ltsv_record_get_value(record, "time"), "[10/Oct/2000:13:55:36 -0700]")); TEST(STREQ(ltsv_record_get_value(record, "req"), "GET /apache_pb.gif HTTP/1.0")); TEST(STREQ(ltsv_record_get_value(record, "status"), "200")); TEST(STREQ(ltsv_record_get_value(record, "size"), "2326")); TEST(STREQ(ltsv_record_get_value(record, "referer"), "http://www.example.com/start.html")); TEST(STREQ(ltsv_record_get_value(record, "ua"), "Mozilla/4.08 [en] (Win98; I ;Nav)")); ltsv_free(ltsv); ltsv = ltsv_parse_string("label:text\thoge:fuga\nmm:value:1"); ltsv_count = ltsv_get_count(ltsv); TEST(ltsv_count == 2); if (ltsv_count != 2) { fprintf(stderr, "ltsv count doesn't match!%d \n", ltsv_count); return; } record = ltsv_get_record(ltsv, 0); TEST(STREQ(ltsv_record_get_value(record, "label"), "text")); TEST(STREQ(ltsv_record_get_value(record, "hoge"), "fuga")); record = ltsv_get_record(ltsv, 1); TEST(STREQ(ltsv_record_get_value(record, "mm"), "value:1")); ltsv_free(ltsv); }
LTSV *ltsv_parse_file(const char *filename) { FILE *fp = fopen(filename, "r"); size_t file_size; char *file_contents; LTSV *output_value; if (!fp) { return NULL; } fseek(fp, 0L, SEEK_END); file_size = ftell(fp); rewind(fp); file_contents = (char*)malloc(sizeof(char) * (file_size + 1)); if (!file_contents) { fclose(fp); return NULL; } fread(file_contents, file_size, 1, fp); fclose(fp); file_contents[file_size] = '\0'; output_value = ltsv_parse_string(file_contents); free((void*)file_contents); return output_value; }
void test_suite_1(void) { TEST(ltsv_parse_string("k") == NULL); TEST(ltsv_parse_string("a:b\nk:v:\t~:v") == NULL); TEST(ltsv_parse_string(":b\nk:v:\t~:v") == NULL); TEST(ltsv_parse_string("a:b\ta:1") == NULL); }