From eb0dfac697b18435b71c36148e6dd016027f50ee Mon Sep 17 00:00:00 2001 From: Nick Chambers Date: Sat, 2 Jul 2022 22:42:03 -0500 Subject: [PATCH] Add test suite for numeric types in `gargoyle_from_rope` --- Makefile | 2 +- test/scribe.c | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index b10e649..b57caf3 100644 --- a/Makefile +++ b/Makefile @@ -30,7 +30,7 @@ test: bin/libgargoyle.so bin/gargoyle-test bin/gargoyle-test bin/gargoyle-test: $(addprefix bin/test/,$(addsuffix .o,$(GARGOYLE_TEST_OBJS))) - cc $(LDFLAGS) -L bin -o $@ $^ -l gargoyle -l cunit + cc $(LDFLAGS) -L bin -o $@ $^ -l gargoyle -l cunit -l m bin/test/%.o: test/%.c | init cc $(CFLAGS) -std=c99 -I include -c -o $@ $< diff --git a/test/scribe.c b/test/scribe.c index cb0864c..1767b89 100644 --- a/test/scribe.c +++ b/test/scribe.c @@ -1,6 +1,7 @@ #include #include #include +#include int init_suite_scribe(void) { return CUE_SUCCESS; @@ -545,4 +546,108 @@ void scribe_test_from_bool(void) { } void scribe_test_from_rope(void) { + uint64_t uint_val = 0; + int64_t sint_val = 0; + double dble_val = 0; + + struct gargoyle_opt opts[] = { + { GARGOYLE_EZ_OPT("uint", uint_val), GARGOYLE_TYPE_UINT }, + { GARGOYLE_EZ_OPT("sint", sint_val), GARGOYLE_TYPE_SINT }, + { GARGOYLE_EZ_OPT("dble", dble_val), GARGOYLE_TYPE_DBLE } + }; + + uint8_t res = gargoyle_from_rope(&opts[0], "42", 0); + CU_ASSERT_EQUAL(res, GARGOYLE_ERR_SUCCESS); + CU_ASSERT_EQUAL(uint_val, 42); + + res = gargoyle_from_rope(&opts[0], "42u", 0); + CU_ASSERT_EQUAL(res, GARGOYLE_ERR_INVALID_UINT); + + res = gargoyle_from_rope(&opts[0], ")(*&^%$#@!~", 0); + CU_ASSERT_EQUAL(res, GARGOYLE_ERR_INVALID_UINT); + + res = gargoyle_from_rope(&opts[0], "thunderfury", 0); + CU_ASSERT_EQUAL(res, GARGOYLE_ERR_INVALID_UINT); + + res = gargoyle_from_rope(&opts[0], "", 0); + CU_ASSERT_EQUAL(res, GARGOYLE_ERR_SUCCESS); + CU_ASSERT_EQUAL(uint_val, 0); + + res = gargoyle_from_rope(&opts[0], "-1", 0); + CU_ASSERT_EQUAL(res, GARGOYLE_ERR_SUCCESS); + CU_ASSERT_EQUAL(uint_val, ULONG_MAX); + + res = gargoyle_from_rope(&opts[0], "-19", 0); + CU_ASSERT_EQUAL(res, GARGOYLE_ERR_SUCCESS); + CU_ASSERT_EQUAL(uint_val, ULONG_MAX - 18); + + res = gargoyle_from_rope(&opts[0], "42.42", 0); + CU_ASSERT_EQUAL(res, GARGOYLE_ERR_INVALID_UINT); + + res = gargoyle_from_rope(&opts[0], "-8.19", 0); + CU_ASSERT_EQUAL(res, GARGOYLE_ERR_INVALID_UINT); + + res = gargoyle_from_rope(&opts[1], "42", 0); + CU_ASSERT_EQUAL(res, GARGOYLE_ERR_SUCCESS); + CU_ASSERT_EQUAL(sint_val, 42); + + res = gargoyle_from_rope(&opts[1], "42u", 0); + CU_ASSERT_EQUAL(res, GARGOYLE_ERR_INVALID_SINT); + + res = gargoyle_from_rope(&opts[1], ")(*&^%$#@!~", 0); + CU_ASSERT_EQUAL(res, GARGOYLE_ERR_INVALID_SINT); + + res = gargoyle_from_rope(&opts[1], "thunderfury", 0); + CU_ASSERT_EQUAL(res, GARGOYLE_ERR_INVALID_SINT); + + res = gargoyle_from_rope(&opts[1], "", 0); + CU_ASSERT_EQUAL(res, GARGOYLE_ERR_SUCCESS); + CU_ASSERT_EQUAL(sint_val, 0); + + res = gargoyle_from_rope(&opts[1], "-1", 0); + CU_ASSERT_EQUAL(res, GARGOYLE_ERR_SUCCESS); + CU_ASSERT_EQUAL(sint_val, -1); + + res = gargoyle_from_rope(&opts[1], "-19", 0); + CU_ASSERT_EQUAL(res, GARGOYLE_ERR_SUCCESS); + CU_ASSERT_EQUAL(sint_val, -19); + + res = gargoyle_from_rope(&opts[1], "42.42", 0); + CU_ASSERT_EQUAL(res, GARGOYLE_ERR_INVALID_SINT); + + res = gargoyle_from_rope(&opts[1], "-8.19", 0); + CU_ASSERT_EQUAL(res, GARGOYLE_ERR_INVALID_SINT); + + res = gargoyle_from_rope(&opts[2], "42", 0); + CU_ASSERT_EQUAL(res, GARGOYLE_ERR_SUCCESS); + CU_ASSERT_DOUBLE_EQUAL(dble_val, 42.0, 0.0); + + res = gargoyle_from_rope(&opts[2], "42u", 0); + CU_ASSERT_EQUAL(res, GARGOYLE_ERR_INVALID_DBLE); + + res = gargoyle_from_rope(&opts[2], ")(*&^%$#@!~", 0); + CU_ASSERT_EQUAL(res, GARGOYLE_ERR_INVALID_DBLE); + + res = gargoyle_from_rope(&opts[2], "thunderfury", 0); + CU_ASSERT_EQUAL(res, GARGOYLE_ERR_INVALID_DBLE); + + res = gargoyle_from_rope(&opts[2], "", 0); + CU_ASSERT_EQUAL(res, GARGOYLE_ERR_SUCCESS); + CU_ASSERT_DOUBLE_EQUAL(dble_val, 0.0, 0.0); + + res = gargoyle_from_rope(&opts[2], "-1", 0); + CU_ASSERT_EQUAL(res, GARGOYLE_ERR_SUCCESS); + CU_ASSERT_DOUBLE_EQUAL(dble_val, -1.0, 0.0); + + res = gargoyle_from_rope(&opts[2], "-19", 0); + CU_ASSERT_EQUAL(res, GARGOYLE_ERR_SUCCESS); + CU_ASSERT_DOUBLE_EQUAL(dble_val, -19.0, 0.0); + + res = gargoyle_from_rope(&opts[2], "42.42", 0); + CU_ASSERT_EQUAL(res, GARGOYLE_ERR_SUCCESS); + CU_ASSERT_DOUBLE_EQUAL(dble_val, 42.42, 0.0); + + res = gargoyle_from_rope(&opts[2], "-8.19", 0); + CU_ASSERT_EQUAL(res, GARGOYLE_ERR_SUCCESS); + CU_ASSERT_DOUBLE_EQUAL(dble_val, -8.19, 0.0); }