commit a10fd0cb80e1297d01f0fd2a4d96c4ea01c0b7bb
parent 213e9f2465744fe34d2c16e9acaa582f5011da98
Author: Rolf Eike Beer <eike@sf-mail.de>
Date: Sun, 24 May 2020 12:14:45 +0200
tests: verify byte_diff() and some str_* functions
Diffstat:
4 files changed, 183 insertions(+), 1 deletion(-)
diff --git a/tests/.gitignore b/tests/.gitignore
@@ -1,2 +1,4 @@
*.o
+unittest_byte
+unittest_str
unittest_stralloc
diff --git a/tests/Makefile b/tests/Makefile
@@ -9,7 +9,7 @@ default: it
.PHONY: clean default it test
-TESTBINS = unittest_stralloc
+TESTBINS = unittest_byte unittest_str unittest_stralloc
clean:
rm -f $(TESTBINS) *.o
@@ -21,6 +21,26 @@ test: it
./$$tbin || exit 1 ; \
done
+unittest_byte: \
+../load unittest_byte.o ../str.a
+ ../load unittest_byte ../str.a \
+ `pkg-config --libs check`
+
+unittest_byte.o: \
+../compile unittest_byte.c ../byte.h
+ ../compile unittest_byte.c -I.. \
+ `pkg-config --cflags check`
+
+unittest_str: \
+../load unittest_str.o ../str.a
+ ../load unittest_str ../str.a \
+ `pkg-config --libs check`
+
+unittest_str.o: \
+../compile unittest_str.c ../str.h
+ ../compile unittest_str.c -I.. \
+ `pkg-config --cflags check`
+
unittest_stralloc: \
../load unittest_stralloc.o ../stralloc.a ../str.a ../error.a
../load unittest_stralloc ../stralloc.a ../str.a ../error.a \
diff --git a/tests/unittest_byte.c b/tests/unittest_byte.c
@@ -0,0 +1,62 @@
+#include <check.h>
+
+#include "byte.h"
+
+START_TEST(test_byte_equal)
+{
+ ck_assert_int_ne(byte_equal("", 0, ""), 0);
+ ck_assert_int_ne(byte_equal("", 1, ""), 0);
+
+ ck_assert_int_ne(byte_equal("", 0, "a"), 0);
+ ck_assert_int_eq(byte_equal("", 1, "a"), 0);
+
+ ck_assert_int_ne(byte_equal("b", 0, "a"), 0);
+ ck_assert_int_eq(byte_equal("b", 1, "a"), 0);
+
+ ck_assert_int_ne(byte_equal("b", 0, ""), 0);
+ ck_assert_int_eq(byte_equal("b", 1, ""), 0);
+
+ ck_assert_int_ne(byte_equal("notqmail", 8, "notqmail"), 0);
+ ck_assert_int_ne(byte_equal("notqmail", 9, "notqmail"), 0);
+ ck_assert_int_ne(byte_equal("notqmail", 4, "notqmail"), 0);
+
+ ck_assert_int_ne(byte_equal("netqmail", 1, "notqmail"), 0);
+ ck_assert_int_eq(byte_equal("netqmail", 8, "notqmail"), 0);
+
+ ck_assert_int_ne(byte_equal("a\0b\0", 3, "a\0bc"), 0);
+ ck_assert_int_eq(byte_equal("a\0b\0", 4, "a\0bc"), 0);
+}
+END_TEST
+
+TCase
+*str_something(void)
+{
+ TCase *tc = tcase_create("basic operations");
+
+ tcase_add_test(tc, test_byte_equal);
+
+ return tc;
+}
+
+Suite
+*str_suite(void)
+{
+ Suite *s = suite_create("notqmail byte");
+
+ suite_add_tcase(s, str_something());
+
+ return s;
+}
+
+int
+main(void)
+{
+ int number_failed;
+
+ SRunner *sr = srunner_create(str_suite());
+ srunner_run_all(sr, CK_NORMAL);
+ number_failed = srunner_ntests_failed(sr);
+ srunner_free(sr);
+
+ return number_failed;
+}
diff --git a/tests/unittest_str.c b/tests/unittest_str.c
@@ -0,0 +1,98 @@
+#include <check.h>
+
+#include "str.h"
+
+START_TEST(test_str_len)
+{
+ ck_assert_uint_eq(str_len(""), 0);
+ ck_assert_uint_eq(str_len("notqmail"), 8);
+}
+END_TEST
+
+START_TEST(test_str_diff)
+{
+ ck_assert_int_eq(str_diff("", ""), 0);
+
+ ck_assert_int_ne(str_diff("", "a"), 0);
+
+ ck_assert_int_ne(str_diff("b", "a"), 0);
+
+ ck_assert_int_ne(str_diff("b", ""), 0);
+
+ ck_assert_int_eq(str_diff("notqmail", "notqmail"), 0);
+
+ ck_assert_int_ne(str_diff("netqmail", "notqmail"), 0);
+}
+END_TEST
+
+START_TEST(test_str_diffn)
+{
+ ck_assert_int_eq(str_diffn("", "", 0), 0);
+ ck_assert_int_eq(str_diffn("", "", 1), 0);
+
+ ck_assert_int_eq(str_diffn("", "a", 0), 0);
+ ck_assert_int_ne(str_diffn("", "a", 1), 0);
+
+ ck_assert_int_eq(str_diffn("b", "a", 0), 0);
+ ck_assert_int_ne(str_diffn("b", "a", 1), 0);
+
+ ck_assert_int_eq(str_diffn("b", "", 0), 0);
+ ck_assert_int_ne(str_diffn("b", "", 1), 0);
+
+ ck_assert_int_eq(str_diffn("notqmail", "notqmail", 8), 0);
+ ck_assert_int_eq(str_diffn("notqmail", "notqmail", 4), 0);
+
+ ck_assert_int_eq(str_diffn("netqmail", "notqmail", 1), 0);
+ ck_assert_int_ne(str_diffn("netqmail", "notqmail", 8), 0);
+}
+END_TEST
+
+START_TEST(test_str_cpy)
+{
+ char outbuf[32];
+
+ memset(outbuf, 'x', sizeof(outbuf));
+ str_copy(outbuf, "");
+ ck_assert_str_eq(outbuf, "");
+
+ memset(outbuf, 'x', sizeof(outbuf));
+ str_copy(outbuf, "notqmail");
+ ck_assert_str_eq(outbuf, "notqmail");
+}
+END_TEST
+
+TCase
+*str_something(void)
+{
+ TCase *tc = tcase_create("basic operations");
+
+ tcase_add_test(tc, test_str_len);
+ tcase_add_test(tc, test_str_diff);
+ tcase_add_test(tc, test_str_diffn);
+ tcase_add_test(tc, test_str_cpy);
+
+ return tc;
+}
+
+Suite
+*str_suite(void)
+{
+ Suite *s = suite_create("notqmail str");
+
+ suite_add_tcase(s, str_something());
+
+ return s;
+}
+
+int
+main(void)
+{
+ int number_failed;
+
+ SRunner *sr = srunner_create(str_suite());
+ srunner_run_all(sr, CK_NORMAL);
+ number_failed = srunner_ntests_failed(sr);
+ srunner_free(sr);
+
+ return number_failed;
+}