#include "test.h" #include #include #include void test_interpretation(void) { char **listing = list_dir("interpretation", ".pom"); if (!listing) return; for (size_t l = 0; listing[l]; l++) { const char *conf_path = listing[l]; printf("Testing %s...\n",conf_path); pom_error *error; pom_conf *conf = pom_load_path(NULL, conf_path, &error); if (error) { test_fail("Error parsing %s: %s.", conf_path, pom_error_to_string(error)); free(error); continue; } const pom_conf *bad = pom_conf_section(conf, "bad"); const pom_conf *good = pom_conf_section(conf, "good"); const char *key = NULL; pom_key_iter *iter = NULL; if (strstr(conf_path, "uint.pom")) { uint64_t val, val2; while ((key = pom_conf_next_key(bad, &iter))) { if (!(error = pom_conf_get_uint(bad, key, &val))) { test_fail("Key %s should be rejected as a uint", key); } free(error); } while ((key = pom_conf_next_key(good, &iter))) { const pom_conf *section = pom_conf_section(good, key); if (pom_conf_get_uint(section, "a", &val)) { test_fail("Key %s.a should be parsed as a uint", key); continue; } if (pom_conf_get_uint(section, "b", &val2)) { test_fail("Key %s.a should be parsed as a uint", key); continue; } if (val != val2) { test_fail("Keys %s.a and %s.b disagree:\n" "a: %" PRIu64 "\n" "b: %" PRIu64, key, key, val, val2); } } } else if (strstr(conf_path, "int.pom")) { int64_t val, val2; while ((key = pom_conf_next_key(bad, &iter))) { if (!(error = pom_conf_get_int(bad, key, &val))) { test_fail("Key %s should be rejected as an int", key); } free(error); } while ((key = pom_conf_next_key(good, &iter))) { const pom_conf *section = pom_conf_section(good, key); if (pom_conf_get_int(section, "a", &val)) { test_fail("Key %s.a should be parsed as an int", key); continue; } if (pom_conf_get_int(section, "b", &val2)) { test_fail("Key %s.a should be parsed as an int", key); continue; } if (val != val2) { test_fail("Keys %s.a and %s.b disagree:\n" "a: %" PRId64 "\n" "b: %" PRId64, key, key, val, val2); } } } else if (strstr(conf_path, "float.pom")) { double val, val2; while ((key = pom_conf_next_key(bad, &iter))) { if (!(error = pom_conf_get_float(bad, key, &val))) { test_fail("Key %s should be rejected as a float", key); } free(error); } while ((key = pom_conf_next_key(good, &iter))) { const pom_conf *section = pom_conf_section(good, key); if (pom_conf_get_float(section, "a", &val)) { test_fail("Key %s.a should be parsed as a float", key); continue; } if (pom_conf_get_float(section, "b", &val2)) { test_fail("Key %s.a should be parsed as a float", key); continue; } if (val != val2) { test_fail("Keys %s.a and %s.b disagree:\n" "a: %f\n" "b: %f", key, key, val, val2); } } } else if (strstr(conf_path, "bool.pom")) { bool val, val2; while ((key = pom_conf_next_key(bad, &iter))) { if (!(error = pom_conf_get_bool(bad, key, &val))) { test_fail("Key %s should be rejected as a bool", key); } free(error); } while ((key = pom_conf_next_key(good, &iter))) { const pom_conf *section = pom_conf_section(good, key); if (pom_conf_get_bool(section, "a", &val)) { test_fail("Key %s.a should be parsed as a bool", key); continue; } if (pom_conf_get_bool(section, "b", &val2)) { test_fail("Key %s.a should be parsed as a bool", key); continue; } if (val != val2) { test_fail("Keys %s.a and %s.b disagree:\n" "a: %d\n" "b: %d", key, key, val, val2); } } } else if (strstr(conf_path, "list.pom")) { // TODO while ((key = pom_conf_next_key(conf, &iter))) { const pom_conf *section = pom_conf_section(conf, key); char **list = pom_conf_get_list(section, "list"); if (!list) { test_fail("%s should have a subkey 'list'", key); continue; } const char *sep = pom_conf_get(section, "sep"); if (!sep) { test_fail("%s should have a subkey 'sep'", key); free(list); continue; } size_t list_count, sep_count = 0; for (list_count = 0; list[list_count]; list_count++); for (size_t i = 0; sep[i]; i++) sep_count += sep[i] == ';'; if (list_count != sep_count) { test_fail("List %s should have %zu elements (got %zu)", key, sep_count, list_count); free(list); continue; } for (size_t i = 0; list[i]; i++) { const char *item = list[i]; size_t sep_len = strcspn(sep, ";"); if (strlen(item) != sep_len || memcmp(item, sep, sep_len) != 0) { test_fail("List %s element %zu is wrong:\n" "expected: %.*s\n" " got: %s\n", key, i, (int)sep_len, sep, item); } sep += sep_len + 1; } free(list); } } else { test_fail("Unrecognized test: %s",conf_path); } pom_conf_free(conf); } free_listing(listing); // pom_conf *conf = pom_load_path("../tests"); }