summaryrefslogtreecommitdiff
path: root/src/util/util.test.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/util.test.c')
-rw-r--r--src/util/util.test.c102
1 files changed, 102 insertions, 0 deletions
diff --git a/src/util/util.test.c b/src/util/util.test.c
index 6288039..53bb8b2 100644
--- a/src/util/util.test.c
+++ b/src/util/util.test.c
@@ -127,6 +127,108 @@ LILY_TEST("convert string to int array with fewer conversions than expected")
+int f_read_int(int *dest, struct kai_tag_t *src)
+{
+ long num = kai_tag_attr_to_long(src, "num", -1);
+ if (num == -1) { return -1; }
+ *dest = num;
+ return 0;
+}
+
+int release_calls;
+void f_release_int(int i) { release_calls += 1; }
+
+
+LILY_TEST("KAI_FILL_ARRAY_FROM_TAGS produces NULL when no tags are found")
+{
+ int result, count;
+ int *buf;
+ release_calls = 0;
+
+ struct kai_tag_t *t = kai_parse_string(
+ "<tag>"
+ " <xxx />"
+ " <yyy />"
+ " <zzz />"
+ "</tag>"
+ );
+
+ KAI_FILL_ARRAY_FROM_TAGS(result, buf, count, int, t, "num", f_read_int, f_release_int);
+ kai_tag_destroy(t);
+
+ CHECK_EQ(result, 0, "%d");
+ CHECK_EQ(release_calls, 0, "%d");
+ CHECK_EQ(buf, NULL, "%p");
+ CHECK_EQ(count, 0, "%d");
+}
+#include LILY_PUSH_TEST()
+
+
+LILY_TEST("KAI_FILL_ARRAY_FROM_TAGS properly fills array")
+{
+ int result, count;
+ int *buf;
+ release_calls = 0;
+
+ struct kai_tag_t *t = kai_parse_string(
+ "<tag>"
+ " <xxx />"
+ " <num num=\"5\" />"
+ " <yyy />"
+ " <num num=\"4\" />"
+ " <num num=\"3\" />"
+ " <zzz />"
+ " <num num=\"2\" />"
+ "</tag>"
+ );
+
+ KAI_FILL_ARRAY_FROM_TAGS(result, buf, count, int, t, "num", f_read_int, f_release_int);
+ kai_tag_destroy(t);
+
+ REQUIRE_EQ(result, 0, "%d");
+ CHECK_EQ(release_calls, 0, "%d");
+ REQUIRE_EQ(count, 4, "%d");
+ CHECK_EQ(buf[0], 5, "%d");
+ CHECK_EQ(buf[1], 4, "%d");
+ CHECK_EQ(buf[2], 3, "%d");
+ CHECK_EQ(buf[3], 2, "%d");
+
+ free(buf);
+}
+#include LILY_PUSH_TEST()
+
+
+
+LILY_TEST("KAI_FILL_ARRAY_FROM_TAGS properly releases on error")
+{
+ int result, count;
+ int *buf;
+ release_calls = 0;
+
+ struct kai_tag_t *t = kai_parse_string(
+ "<tag>"
+ " <xxx />"
+ " <num num=\"5\" />"
+ " <yyy />"
+ " <num num=\"4\" />"
+ " <num nm=\"3\" />"
+ " <zzz />"
+ " <num num=\"2\" />"
+ "</tag>"
+ );
+
+ KAI_FILL_ARRAY_FROM_TAGS(result, buf, count, int, t, "num", f_read_int, f_release_int);
+ kai_tag_destroy(t);
+
+ CHECK_EQ(result, -1, "%d");
+ CHECK_EQ(release_calls, 2, "%d");
+ CHECK_EQ(count, 0, "%d");
+ CHECK_EQ(buf, NULL, "%p");
+}
+#include LILY_PUSH_TEST()
+
+
+
#define LILY_FILE_END
#include LILY_REGISTER_TESTS()