Mercurial > libunicode
diff tests/test-unicode.c @ 28:f06312a7432b
cmake: enable tests
author | David Demelier <markand@malikania.fr> |
---|---|
date | Tue, 07 Feb 2023 14:30:33 +0100 |
parents | 4da5819148c6 |
children | 303403de1314 |
line wrap: on
line diff
--- a/tests/test-unicode.c Wed Feb 01 13:07:04 2023 +0100 +++ b/tests/test-unicode.c Tue Feb 07 14:30:33 2023 +0100 @@ -18,7 +18,7 @@ #include <errno.h> -#include <rexo.h> +#include <dt.h> #include "unicode.h" @@ -46,7 +46,8 @@ return l1 == l2 && memcmp(s1, s2, l1) == 0; } -RX_TEST_CASE(uni8_encode, simple) +static void +uni8_encode_simple(void) { size_t r; @@ -55,8 +56,8 @@ uint8_t buffer[5] = { 0 }; r = uni8_encode(buffer, sizeof (buffer), U'a'); - RX_INT_REQUIRE_EQUAL(r, 1); - RX_STR_REQUIRE_EQUAL((const char *)buffer, (const char *)u8"a"); + DT_EQ_INT(r, 1); + DT_EQ_STR((const char *)buffer, (const char *)u8"a"); } /* é -> 2 bytes. */ @@ -64,32 +65,35 @@ uint8_t buffer[5] = { 0 }; r = uni8_encode(buffer, sizeof (buffer), U'é'); - RX_INT_REQUIRE_EQUAL(r, 2); - RX_STR_REQUIRE_EQUAL((const char *)buffer, (const char *)u8"é"); + DT_EQ_INT(r, 2); + DT_EQ_STR((const char *)buffer, (const char *)u8"é"); } } -RX_TEST_CASE(uni8_encode, invalid) +static void +uni8_encode_invalid(void) { size_t r; uint8_t buffer[5] = { 0 }; r = uni8_encode(buffer, sizeof (buffer), 0xffffffff); - RX_UINT_REQUIRE_EQUAL(r, (size_t)-1); - RX_INT_REQUIRE_EQUAL(errno, EILSEQ); + DT_EQ_SIZE(r, (size_t)-1); + DT_EQ_INT(errno, EILSEQ); } -RX_TEST_CASE(uni8_encode, toosmall) +static void +uni8_encode_toosmall(void) { size_t r; uint8_t buffer[1] = { 0 }; r = uni8_encode(buffer, sizeof (buffer), U'é'); - RX_UINT_REQUIRE_EQUAL(r, (size_t)-1); - RX_INT_REQUIRE_EQUAL(errno, ERANGE); + DT_EQ_SIZE(r, (size_t)-1); + DT_EQ_INT(errno, ERANGE); } -RX_TEST_CASE(unit8_decode, simple) +static void +uni8_decode_simple(void) { size_t r; @@ -98,8 +102,8 @@ uint32_t code = -1; r = uni8_decode((const uint8_t *)u8"a", &code); - RX_UINT_REQUIRE_EQUAL(r, 1U); - RX_INT_REQUIRE_EQUAL(code, 'a'); + DT_EQ_SIZE(r, 1U); + DT_EQ_INT(code, 'a'); } /* é -> 2 bytes. */ @@ -107,12 +111,13 @@ uint32_t code = -1; r = uni8_decode((const uint8_t *)u8"é", &code); - RX_UINT_REQUIRE_EQUAL(r, 2U); - RX_INT_REQUIRE_EQUAL(code, U'é'); + DT_EQ_SIZE(r, 2U); + DT_EQ_INT(code, U'é'); } } -RX_TEST_CASE(uni8_decode, invalid) +static void +uni8_decode_invalid(void) { size_t r; @@ -121,9 +126,9 @@ uint32_t code = -1; r = uni8_decode((const uint8_t *)u8"\xff""a", &code); - RX_UINT_REQUIRE_EQUAL(r, (size_t)-1); - RX_UINT_REQUIRE_EQUAL(code, (uint32_t)-1); - RX_INT_REQUIRE_EQUAL(errno, EILSEQ); + DT_EQ_SIZE(r, (size_t)-1); + DT_EQ_SIZE(code, (uint32_t)-1); + DT_EQ_INT(errno, EILSEQ); } /* Valid "€" but unfinished sequence. */ @@ -131,39 +136,44 @@ uint32_t code = -1; r = uni8_decode((const uint8_t []){ -30, 0 }, &code); - RX_UINT_REQUIRE_EQUAL(r, (size_t)-1); - RX_UINT_REQUIRE_EQUAL(code, (uint32_t)-1); - RX_INT_REQUIRE_EQUAL(errno, EILSEQ); + DT_EQ_SIZE(r, (size_t)-1); + DT_EQ_SIZE(code, (uint32_t)-1); + DT_EQ_INT(errno, EILSEQ); } } -RX_TEST_CASE(uni8_sizeof, simple) +static void +uni8_sizeof_simple(void) { - RX_INT_REQUIRE_EQUAL(uni8_sizeof(u8"a"[0]), 1U); - RX_INT_REQUIRE_EQUAL(uni8_sizeof(u8"é"[0]), 2U); - RX_INT_REQUIRE_EQUAL(uni8_sizeof(u8"€"[0]), 3U); - RX_INT_REQUIRE_EQUAL(uni8_sizeof(u8"𐍈"[0]), 4U); + DT_EQ_INT(uni8_sizeof(u8"a"[0]), 1U); + DT_EQ_INT(uni8_sizeof(u8"é"[0]), 2U); + DT_EQ_INT(uni8_sizeof(u8"€"[0]), 3U); + DT_EQ_INT(uni8_sizeof(u8"𐍈"[0]), 4U); } -RX_TEST_CASE(uni8_sizeof, invalid) +static void +uni8_sizeof_invalid(void) { - RX_UINT_REQUIRE_EQUAL((size_t)-1, uni8_sizeof(u8"\xff"[0])); - RX_INT_REQUIRE_EQUAL(errno, EILSEQ); + DT_EQ_SIZE((size_t)-1, uni8_sizeof(u8"\xff"[0])); + DT_EQ_INT(errno, EILSEQ); } -RX_TEST_CASE(uni8_length, simple) +static void +uni8_length_simple(void) { - RX_UINT_REQUIRE_EQUAL(uni8_length((const uint8_t *)"abc"), 3U); - RX_UINT_REQUIRE_EQUAL(uni8_length((const uint8_t *)"5€"), 2U); + DT_EQ_SIZE(uni8_length((const uint8_t *)"abc"), 3U); + DT_EQ_SIZE(uni8_length((const uint8_t *)"5€"), 2U); } -RX_TEST_CASE(uni8_length, invalid) +static void +uni8_length_invalid(void) { - RX_UINT_REQUIRE_EQUAL((size_t)-1, uni8_length((const uint8_t *)"a""\xff""b")); - RX_INT_REQUIRE_EQUAL(errno, EILSEQ); + DT_EQ_SIZE((size_t)-1, uni8_length((const uint8_t *)"a""\xff""b")); + DT_EQ_INT(errno, EILSEQ); } -RX_TEST_CASE(uni8_to32, simple) +static void +uni8_to32_simple(void) { size_t r; @@ -172,8 +182,8 @@ uint32_t expected[] = { U'a', U'b', U'c', 0 }; r = uni8_to32((const uint8_t *)"abc", buffer, 10); - RX_UINT_REQUIRE_EQUAL(r, 3U); - RX_REQUIRE(u32cmp(buffer, expected)); + DT_EQ_SIZE(r, 3U); + DT_ASSERT(u32cmp(buffer, expected)); } { @@ -181,69 +191,77 @@ uint32_t expected[] = { U'a', U'é', U'c', 0 }; r = uni8_to32((const uint8_t *)"aéc", buffer, 10); - RX_UINT_REQUIRE_EQUAL(r, 3); - RX_REQUIRE(u32cmp(buffer, expected)); + DT_EQ_SIZE(r, 3); + DT_ASSERT(u32cmp(buffer, expected)); } } -RX_TEST_CASE(uni8_to32, invalid) +static void +uni8_to32_invalid(void) { size_t r; uint32_t buffer[10] = { 0 }; /* Invalid UTF-8 sequence. */ r = uni8_to32((const uint8_t *)u8"\xff""a", buffer, 10); - RX_UINT_REQUIRE_EQUAL(r, (size_t)-1); - RX_INT_REQUIRE_EQUAL(errno, EILSEQ); + DT_EQ_SIZE(r, (size_t)-1); + DT_EQ_INT(errno, EILSEQ); /* Valid "€" but unfinished sequence. */ r = uni8_to32((const uint8_t []){ -30, 0 }, buffer, 10); - RX_UINT_REQUIRE_EQUAL(r, (size_t)-1); - RX_INT_REQUIRE_EQUAL(errno, EILSEQ); + DT_EQ_SIZE(r, (size_t)-1); + DT_EQ_INT(errno, EILSEQ); } -RX_TEST_CASE(uni8_to32, toosmall) +static void +uni8_to32_toosmall(void) { size_t r; uint32_t buffer[4] = { 0 }; r = uni8_to32((const uint8_t *)u8"bonjour à tous", buffer, 1); - RX_UINT_REQUIRE_EQUAL(r, (size_t)-1); - RX_INT_REQUIRE_EQUAL(errno, ERANGE); + DT_EQ_SIZE(r, (size_t)-1); + DT_EQ_INT(errno, ERANGE); } -RX_TEST_CASE(uni32_sizeof, simple) +static void +uni32_sizeof_simple(void) { - RX_UINT_REQUIRE_EQUAL(uni32_sizeof(U'a'), 1); - RX_UINT_REQUIRE_EQUAL(uni32_sizeof(U'é'), 2); - RX_UINT_REQUIRE_EQUAL(uni32_sizeof(U'€'), 3); - RX_UINT_REQUIRE_EQUAL(uni32_sizeof(U'𐍈'), 4); + DT_EQ_SIZE(uni32_sizeof(U'a'), 1); + DT_EQ_SIZE(uni32_sizeof(U'é'), 2); + DT_EQ_SIZE(uni32_sizeof(U'€'), 3); + DT_EQ_SIZE(uni32_sizeof(U'𐍈'), 4); } -RX_TEST_CASE(uni32_sizeof, invalid) +static void +uni32_sizeof_invalid(void) { - RX_UINT_REQUIRE_EQUAL((size_t)-1, uni32_sizeof(0xffffffff)); - RX_INT_REQUIRE_EQUAL(errno, EILSEQ); + DT_EQ_SIZE((size_t)-1, uni32_sizeof(0xffffffff)); + DT_EQ_INT(errno, EILSEQ); } -RX_TEST_CASE(uni32_length, simple) +static void +uni32_length_simple(void) { - RX_UINT_REQUIRE_EQUAL(uni32_length((const uint32_t []){ U'a', U'é', U'c', 0 }), 3U); + DT_EQ_SIZE(uni32_length((const uint32_t []){ U'a', U'é', U'c', 0 }), 3U); } -RX_TEST_CASE(uni32_requires, simple) +static void +uni32_requires_simple(void) { - RX_UINT_REQUIRE_EQUAL(uni32_requires(U"abc"), 3U); - RX_UINT_REQUIRE_EQUAL(uni32_requires(U"é€𐍈"), 9U); + DT_EQ_SIZE(uni32_requires(U"abc"), 3U); + DT_EQ_SIZE(uni32_requires(U"é€𐍈"), 9U); } -RX_TEST_CASE(uni32_requires, invalid) +static void +uni32_requires_invalid(void) { - RX_UINT_REQUIRE_EQUAL((size_t)-1, uni32_requires(U"\xffffffff")); - RX_INT_REQUIRE_EQUAL(errno, EILSEQ); + DT_EQ_SIZE((size_t)-1, uni32_requires(U"\xffffffff")); + DT_EQ_INT(errno, EILSEQ); } -RX_TEST_CASE(uni32_to8, simple) +static void +uni32_to8_simple(void) { size_t r; @@ -251,79 +269,113 @@ uint8_t buffer[10] = { 0 }; r = uni32_to8(U"abc", buffer, sizeof (buffer)); - RX_UINT_REQUIRE_EQUAL(r, 3U); - RX_STR_REQUIRE_EQUAL((const char *)buffer, (const char *)u8"abc"); + DT_EQ_SIZE(r, 3U); + DT_EQ_STR((const char *)buffer, (const char *)u8"abc"); } { uint8_t buffer[20] = { 0 }; r = uni32_to8(U"ça va, 5€ ?", buffer, sizeof (buffer)); - RX_UINT_REQUIRE_EQUAL(r, 14U); - RX_STR_REQUIRE_EQUAL((const char *)buffer, (const char *)u8"ça va, 5€ ?"); + DT_EQ_SIZE(r, 14U); + DT_EQ_STR((const char *)buffer, (const char *)u8"ça va, 5€ ?"); } } -RX_TEST_CASE(uni32_to8, invalid) +static void +uni32_to8_invalid(void) { uint8_t buffer[10] = { 0 }; - RX_INT_REQUIRE_EQUAL(uni32_to8(U"\xffffffff", buffer, sizeof (buffer)), (size_t)-1); - RX_UINT_REQUIRE_EQUAL(errno, EILSEQ); + DT_EQ_SIZE(uni32_to8(U"\xffffffff", buffer, sizeof (buffer)), (size_t)-1); + DT_EQ_INT(errno, EILSEQ); } -RX_TEST_CASE(uni32_to8, toosmall) +static void +uni32_to8_toosmall(void) { size_t r; uint8_t buffer[3] = { 0 }; r = uni32_to8(U"ça va ?", buffer, sizeof (buffer)); - RX_UINT_REQUIRE_EQUAL(r, (size_t)-1); - RX_INT_REQUIRE_EQUAL(errno, ERANGE); + DT_EQ_SIZE(r, (size_t)-1); + DT_EQ_INT(errno, ERANGE); } -RX_TEST_CASE(misc, isalpha) +static void +misc_isalpha(void) { - RX_REQUIRE(uni_isalpha(U'é')); - RX_REQUIRE(!uni_isalpha(U'€')); + DT_ASSERT(uni_isalpha(U'é')); + DT_ASSERT(!uni_isalpha(U'€')); } -RX_TEST_CASE(misc, isdigit) +static void +misc_isdigit(void) { - RX_REQUIRE(uni_isdigit(U'۱')); - RX_REQUIRE(!uni_isdigit(U'€')); + DT_ASSERT(uni_isdigit(U'۱')); + DT_ASSERT(!uni_isdigit(U'€')); } -RX_TEST_CASE(misc, islower) +static void +misc_islower(void) { - RX_REQUIRE(uni_islower(U'a')); - RX_REQUIRE(uni_islower(U'é')); - RX_REQUIRE(!uni_islower(U'A')); - RX_REQUIRE(!uni_islower(U'É')); + DT_ASSERT(uni_islower(U'a')); + DT_ASSERT(uni_islower(U'é')); + DT_ASSERT(!uni_islower(U'A')); + DT_ASSERT(!uni_islower(U'É')); } -RX_TEST_CASE(misc, isspace) +static void +misc_isspace(void) { - RX_REQUIRE(uni_isspace(U' ')); - RX_REQUIRE(!uni_isspace(U'é')); + DT_ASSERT(uni_isspace(U' ')); + DT_ASSERT(!uni_isspace(U'é')); } -RX_TEST_CASE(misc, istitle) +static void +misc_istitle(void) { - RX_REQUIRE(uni_istitle(U'Dž')); - RX_REQUIRE(!uni_istitle(U'€')); + DT_ASSERT(uni_istitle(U'Dž')); + DT_ASSERT(!uni_istitle(U'€')); } -RX_TEST_CASE(misc, isupper) +static void +misc_isupper(void) { - RX_REQUIRE(!uni_isupper('a')); - RX_REQUIRE(!uni_isupper(U'é')); - RX_REQUIRE(uni_isupper('A')); - RX_REQUIRE(uni_isupper(U'É')); + DT_ASSERT(!uni_isupper('a')); + DT_ASSERT(!uni_isupper(U'é')); + DT_ASSERT(uni_isupper('A')); + DT_ASSERT(uni_isupper(U'É')); } int main(int argc, char **argv) { - return rx_main(0, NULL, argc, (const char **)argv) == RX_SUCCESS ? 0 : 1; + DT_RUN(uni8_encode_simple); + DT_RUN(uni8_encode_invalid); + DT_RUN(uni8_encode_toosmall); + DT_RUN(uni8_decode_simple); + DT_RUN(uni8_decode_invalid); + DT_RUN(uni8_sizeof_simple); + DT_RUN(uni8_sizeof_invalid); + DT_RUN(uni8_length_simple); + DT_RUN(uni8_length_invalid); + DT_RUN(uni8_to32_simple); + DT_RUN(uni8_to32_invalid); + DT_RUN(uni8_to32_toosmall); + DT_RUN(uni32_sizeof_simple); + DT_RUN(uni32_sizeof_invalid); + DT_RUN(uni32_length_simple); + DT_RUN(uni32_requires_simple); + DT_RUN(uni32_requires_invalid); + DT_RUN(uni32_to8_simple); + DT_RUN(uni32_to8_invalid); + DT_RUN(uni32_to8_toosmall); + DT_RUN(misc_isalpha); + DT_RUN(misc_isdigit); + DT_RUN(misc_islower); + DT_RUN(misc_isspace); + DT_RUN(misc_istitle); + DT_RUN(misc_isupper); + DT_SUMMARY(); }