diff options
author | sanine <sanine.not@pm.me> | 2023-02-04 11:35:47 -0600 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2023-02-04 11:35:47 -0600 |
commit | 059a70686c49d591fd7a193493745a7bc172cbca (patch) | |
tree | 543634d6406c8204b74399175bed43b3a5eb55ff | |
parent | 0bb180e4ddeebf6d219a38b862ce3e8238f99c54 (diff) |
implement kai_read_param() and upgrade lily-test version
-rw-r--r-- | include/kalmia.h | 7 | ||||
-rw-r--r-- | src/geometry/geometry.c | 30 | ||||
-rw-r--r-- | src/geometry/geometry.h | 1 | ||||
-rw-r--r-- | src/geometry/geometry.test.c | 49 | ||||
-rw-r--r-- | src/test/lily-test.h | 31 |
5 files changed, 109 insertions, 9 deletions
diff --git a/include/kalmia.h b/include/kalmia.h index dca0a06..615753c 100644 --- a/include/kalmia.h +++ b/include/kalmia.h @@ -74,5 +74,12 @@ struct ka_float_array_t { ka_real_t *buf; }; +struct ka_param_t { + char *name; + char *sid; + char *type; /* required */ + char *semantic; +}; + #endif diff --git a/src/geometry/geometry.c b/src/geometry/geometry.c index ad23853..6f6cbae 100644 --- a/src/geometry/geometry.c +++ b/src/geometry/geometry.c @@ -6,12 +6,16 @@ #include "geometry.h" +/* macro to ensure tag type matches what we expect */ +#define CHECK_TAG_TYPE(expected) \ +if (strcmp(src->type, expected) != 0) { \ + return -1; \ +} + + int kai_read_float_array(struct ka_float_array_t *dest, struct kai_tag_t *src) { - if (strcmp(src->type, "float_array") != 0) { - /* incorrect tag type */ - return -1; - } + CHECK_TAG_TYPE("float_array"); long count = kai_tag_attr_to_long(src, "count", -1); if (count == -1) { @@ -30,3 +34,21 @@ int kai_read_float_array(struct ka_float_array_t *dest, struct kai_tag_t *src) return 0; } + + +int kai_read_param(struct ka_param_t *dest, struct kai_tag_t *src) +{ + CHECK_TAG_TYPE("param"); + + char *type = kai_tag_attr_to_dup(src, "type"); + if (type == NULL) { + return -1; + } + + dest->name = kai_tag_attr_to_dup(src, "name"); + dest->sid = kai_tag_attr_to_dup(src, "sid"); + dest->type = type; + dest->semantic = kai_tag_attr_to_dup(src, "semantic"); + + return 0; +} diff --git a/src/geometry/geometry.h b/src/geometry/geometry.h index 1672ffa..acb27ee 100644 --- a/src/geometry/geometry.h +++ b/src/geometry/geometry.h @@ -5,5 +5,6 @@ #include "xml/xml.h" int kai_read_float_array(struct ka_float_array_t *dest, struct kai_tag_t *src); +int kai_read_param(struct ka_param_t *dest, struct kai_tag_t *src); #endif diff --git a/src/geometry/geometry.test.c b/src/geometry/geometry.test.c index 56673a4..dec16e9 100644 --- a/src/geometry/geometry.test.c +++ b/src/geometry/geometry.test.c @@ -68,5 +68,54 @@ LILY_TEST("read normal float_array") #include LILY_PUSH_TEST() +LILY_TEST("fail to read non-param tag") +{ + struct kai_tag_t *t = kai_parse_string( + "<tag />" + ); + + struct ka_param_t param; + int result = kai_read_param(¶m, t); + CHECK_EQ(result, -1, "%d"); + + kai_tag_destroy(t); +} +#include LILY_PUSH_TEST() + + +LILY_TEST("fail to read param tag with no specified type") +{ + struct kai_tag_t *t = kai_parse_string( + "<param />" + ); + + struct ka_param_t param; + int result = kai_read_param(¶m, t); + CHECK_EQ(result, -1, "%d"); + + kai_tag_destroy(t); +} +#include LILY_PUSH_TEST() + + +LILY_TEST("read param tag") +{ + struct kai_tag_t *t = kai_parse_string( + "<param type=\"float\" name=\"x\" />" + ); + + struct ka_param_t param; + int result = kai_read_param(¶m, t); + kai_tag_destroy(t); + + REQUIRE_EQ(result, 0, "%d"); + CHECK_EQS(param.name, "x"); + CHECK_EQ(param.sid, NULL, "%p"); + CHECK_EQS(param.type, "float"); + CHECK_EQ(param.semantic, NULL, "%p"); +} +#include LILY_PUSH_TEST() + + #define LILY_FILE_END #include LILY_REGISTER_TESTS() diff --git a/src/test/lily-test.h b/src/test/lily-test.h index 8b1d9ad..7b1f72b 100644 --- a/src/test/lily-test.h +++ b/src/test/lily-test.h @@ -48,7 +48,7 @@ #define LILY_VERSION_MAJOR 2 #define LILY_VERSION_MINOR 0 -#define LILY_VERSION_PATCH 0 +#define LILY_VERSION_PATCH 1 #include <stdbool.h> #include <stddef.h> @@ -57,6 +57,7 @@ #include <stdio.h> #include <stdarg.h> #include <stdlib.h> +#include <string.h> #include <math.h> #define STR_IMP(x) #x @@ -69,6 +70,9 @@ #define LILY_LOCATION ((__FILE__ ":" STR(__LINE__)) + SOURCE_PATH_SIZE) #endif +#define LILY_NULLSAFE(x) (x==NULL ? "(nil)" : x) +int lily_streq(const char *str1, const char *str2); + /* self-location macro */ #ifndef LILY_TEST_H_LOCATION @@ -161,7 +165,7 @@ void lily_check(int x, const char *location, const char *fmt, ...); #define CHECK_EQF(x, y, fmt) LILY_CHECK_EQF(x, y, #x, #y, fmt) #define LILY_CHECK_EQS(x, y, xstr, ystr) \ - lily_check(strcmp(x, y) == 0, LILY_LOCATION, \ + lily_check(lily_streq(x, y), LILY_LOCATION, \ "CHECK failed: %s == %s\n %s = \"%s\"\n %s = \"%s\"", \ xstr, ystr, xstr, x, ystr, y) #define CHECK_EQS(x, y) LILY_CHECK_EQS(x, y, #x, #y) @@ -194,7 +198,7 @@ void lily_require(int x, const char *location, const char *fmt, ...); #define REQUIRE_EQF(x, y, fmt) LILY_REQUIRE_EQF(x, y, #x, #y, fmt) #define LILY_REQUIRE_EQS(x, y, xstr, ystr) \ - lily_require(strcmp(x, y) == 0, LILY_LOCATION, \ + lily_require(lily_streq(x, y), LILY_LOCATION, \ "REQUIRE failed: %s == %s\n %s = \"%s\"\n %s = \"%s\"", \ xstr, ystr, xstr, x, ystr, y) #define REQUIRE_EQS(x, y) LILY_REQUIRE_EQS(x, y, #x, #y) @@ -278,10 +282,10 @@ void lily_run_test(void (*test)()) if (lily_g.failed) { lily_g.n_tests_failed += 1; printf("================================================================================\n"); - printf("test \"%s\" failed!\n\n", lily_g.test_name); + printf("test \"%s\" failed!\n", lily_g.test_name); lily_test_msg_t *n = lily_g.HEAD.next; while(n != NULL) { - printf(" %s\n [%s]\n\n", n->msg, n->location); + printf(" %s\n [%s]\n", n->msg, n->location); n = n->next; } lily_msg_destroy(lily_g.HEAD.next); @@ -368,6 +372,23 @@ void lily_set_epsilon(double epsilon) { lily_g.epsilon = epsilon; } + + +int lily_streq(const char *str1, const char *str2) +{ + if (str1 == NULL && str2 == NULL) { + /* both are null (and therefore equal, i guess) */ + return 1; + } + + if (str1 == NULL || str2 == NULL) { + /* only one is null */ + return 0; + } + + /* neither are null, check normal string equality */ + return strcmp(str1, str2) == 0; +} #endif /* ifdef LILY_TEST_H */ |