summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorpommicket <pommicket@gmail.com>2025-09-15 18:54:37 -0400
committerpommicket <pommicket@gmail.com>2025-09-15 18:54:37 -0400
commitd5cf3acb7a45b7e46bf51dc6a66030d7bc986597 (patch)
tree357dc594623a3abc2a04d2d516df3bed9406a933 /tests
parent2b519f42fec913763abd0d967301916e763e770a (diff)
Interpretation tests, various bugfixes
Diffstat (limited to 'tests')
-rw-r--r--tests/interpretation.c163
-rw-r--r--tests/main.c1
-rw-r--r--tests/test.h1
3 files changed, 165 insertions, 0 deletions
diff --git a/tests/interpretation.c b/tests/interpretation.c
new file mode 100644
index 0000000..bdae290
--- /dev/null
+++ b/tests/interpretation.c
@@ -0,0 +1,163 @@
+#include "test.h"
+
+#include <stdlib.h>
+#include <string.h>
+#include <inttypes.h>
+
+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 (!pom_conf_get_uint(bad, key, &val)) {
+ test_fail("Key %s should be rejected as a uint", key);
+ }
+ }
+ 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 (!pom_conf_get_int(bad, key, &val)) {
+ test_fail("Key %s should be rejected as an int", key);
+ }
+ }
+ 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 (!pom_conf_get_float(bad, key, &val)) {
+ test_fail("Key %s should be rejected as a float", key);
+ }
+ }
+ 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 (!pom_conf_get_bool(bad, key, &val)) {
+ test_fail("Key %s should be rejected as a bool", key);
+ }
+ }
+ 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");
+}
diff --git a/tests/main.c b/tests/main.c
index c34a79d..791fdc7 100644
--- a/tests/main.c
+++ b/tests/main.c
@@ -65,6 +65,7 @@ int main(int argc, char **argv) {
test_parsing();
test_errors();
test_location();
+ test_interpretation();
if (any_failure) {
fprintf(stderr, "\x1b[1m\x1b[91mSome tests failed.\x1b[0m\n");
return EXIT_FAILURE;
diff --git a/tests/test.h b/tests/test.h
index 59807d1..c6109fb 100644
--- a/tests/test.h
+++ b/tests/test.h
@@ -16,5 +16,6 @@ void test_fail(PRINTF_FORMAT_STRING const char *, ...) ATTRIBUTE_PRINTF(1, 2);
void test_parsing(void);
void test_errors(void);
void test_location(void);
+void test_interpretation(void);
char **list_dir(const char *dir, const char *suffix);
void free_listing(char **listing);