From f41fc5ad44e538b78c6395dd4a8e9bd2cc591725 Mon Sep 17 00:00:00 2001 From: fef Date: Fri, 16 Jul 2021 00:12:39 +0200 Subject: [PATCH] test: add tests for utf8_chrsize --- test/string/utf/utf.cmake | 1 + test/string/utf/utf8_chrsize.cpp | 73 ++++++++++++++++++++++++++++++ test/string/utf/utf8_from_nchr.cpp | 12 ++--- test/string/utf/utf8_to_nchr.cpp | 18 ++++---- 4 files changed, 89 insertions(+), 15 deletions(-) create mode 100644 test/string/utf/utf8_chrsize.cpp diff --git a/test/string/utf/utf.cmake b/test/string/utf/utf.cmake index 30807c8..7b5512a 100644 --- a/test/string/utf/utf.cmake +++ b/test/string/utf/utf.cmake @@ -1,6 +1,7 @@ # See the end of this file for copyright and license terms. target_sources(neo_test PRIVATE + string/utf/utf8_chrsize.cpp string/utf/utf8_from_nchr.cpp string/utf/utf8_to_nchr.cpp ) diff --git a/test/string/utf/utf8_chrsize.cpp b/test/string/utf/utf8_chrsize.cpp new file mode 100644 index 0000000..81e02d3 --- /dev/null +++ b/test/string/utf/utf8_chrsize.cpp @@ -0,0 +1,73 @@ +/** See the end of this file for copyright and license terms. */ + +#include +#include + +#include +#include + +TEST_CASE( "utf8_chrsize: Identify 1 byte character", "[string/utf.c]" ) +{ + error err; + usize size = utf8_chrsize(',', &err); + + REQUIRE( size == 1 ); + REQUIRE( errnum(&err) == 0 ); +} + +TEST_CASE( "utf8_chrsize: Identify 2 byte character", "[string/utf.c]" ) +{ + error err; + /* U+03B1 Greek Smol Letter Alpha */ + usize size = utf8_chrsize(0x03b1, &err); + + REQUIRE( size == 2 ); + REQUIRE( errnum(&err) == 0 ); +} + +TEST_CASE( "utf8_chrsize: Identify 3 byte character", "[string/utf.c]" ) +{ + error err; + /* U+3042 Hiragana Letter A */ + usize size = utf8_chrsize(0x3042, &err); + + REQUIRE( size == 3 ); + REQUIRE( errnum(&err) == 0 ); +} + +TEST_CASE( "utf8_chrsize: Identify 4 byte character", "[string/utf.c]" ) +{ + error err; + /* U+1F97A The Bottom Emoji(TM) */ + usize size = utf8_chrsize(0x01f97a, &err); + + REQUIRE( size == 4 ); + REQUIRE( errnum(&err) == 0 ); +} + +TEST_CASE( "utf8_chrsize: Error if out of Unicode range", "[string/utf.c]" ) +{ + error err; + /* Unicode range is 0x00~0x10ffff */ + usize size = utf8_chrsize(0x110000, &err); + + string *expected = nstr("Character code not within Unicode range", nil); + string *actual = errmsg(&err); + + REQUIRE( size == 0 ); + REQUIRE( errnum(&err) == EINVAL ); + REQUIRE( nstreq(expected, actual, nil) ); +} + +/* + * This file is part of libneo. + * Copyright (c) 2021 Fefie . + * + * libneo is non-violent software: you may only use, redistribute, + * and/or modify it under the terms of the CNPLv6+ as found in + * the LICENSE file in the source code root directory or at + * . + * + * libneo comes with ABSOLUTELY NO WARRANTY, to the extent + * permitted by applicable law. See the CNPLv6+ for details. + */ diff --git a/test/string/utf/utf8_from_nchr.cpp b/test/string/utf/utf8_from_nchr.cpp index c375d43..d0fef79 100644 --- a/test/string/utf/utf8_from_nchr.cpp +++ b/test/string/utf/utf8_from_nchr.cpp @@ -6,7 +6,7 @@ #include #include -TEST_CASE( "Encode 1-byte character", "[utf8_from_nchr]" ) +TEST_CASE( "utf8_from_nchr: Encode 1-byte character", "[string/utf.c]" ) { char buf[5] = { '\xff', '\xff', '\xff', '\xff', '\xff' }; error err; @@ -21,7 +21,7 @@ TEST_CASE( "Encode 1-byte character", "[utf8_from_nchr]" ) REQUIRE( errnum(&err) == 0 ); } -TEST_CASE( "Encode 2-byte character", "[utf8_from_nchr]" ) +TEST_CASE( "utf8_from_nchr: Encode 2-byte character", "[string/utf.c]" ) { char buf[5] = { '\xff', '\xff', '\xff', '\xff', '\xff' }; error err; @@ -37,7 +37,7 @@ TEST_CASE( "Encode 2-byte character", "[utf8_from_nchr]" ) REQUIRE( errnum(&err) == 0 ); } -TEST_CASE( "Encode 3-byte character", "[utf8_from_nchr]" ) +TEST_CASE( "utf8_from_nchr: Encode 3-byte character", "[string/utf.c]" ) { char buf[5] = { '\xff', '\xff', '\xff', '\xff', '\xff' }; error err; @@ -53,7 +53,7 @@ TEST_CASE( "Encode 3-byte character", "[utf8_from_nchr]" ) REQUIRE( errnum(&err) == 0 ); } -TEST_CASE( "Encode 4-byte character", "[utf8_from_nchr]" ) +TEST_CASE( "utf8_from_nchr: Encode 4-byte character", "[string/utf.c]" ) { char buf[5] = { '\xff', '\xff', '\xff', '\xff', '\xff' }; error err; @@ -69,7 +69,7 @@ TEST_CASE( "Encode 4-byte character", "[utf8_from_nchr]" ) REQUIRE( errnum(&err) == 0 ); } -TEST_CASE( "Error if out of Unicode range", "[utf8_from_nchr]" ) +TEST_CASE( "utf8_from_nchr: Error if out of Unicode range", "[string/utf.c]" ) { char buf[5] = { '\xff', '\xff', '\xff', '\xff', '\xff' }; error err; @@ -87,7 +87,7 @@ TEST_CASE( "Error if out of Unicode range", "[utf8_from_nchr]" ) REQUIRE( size == 0 ); REQUIRE( errnum(&err) == EINVAL ); REQUIRE( nstreq(expected, actual, nil) ); - printf("%s\n", errmsg(&err)->_data); + errput(&err); } /* diff --git a/test/string/utf/utf8_to_nchr.cpp b/test/string/utf/utf8_to_nchr.cpp index f3db808..06f9b58 100644 --- a/test/string/utf/utf8_to_nchr.cpp +++ b/test/string/utf/utf8_to_nchr.cpp @@ -6,7 +6,7 @@ #include #include -TEST_CASE( "Decode 1-byte character sequence", "[utf8_to_nchr]" ) +TEST_CASE( "utf8_to_nchr: Decode 1-byte character sequence", "[string/utf.c]" ) { error err; nchar c; @@ -18,7 +18,7 @@ TEST_CASE( "Decode 1-byte character sequence", "[utf8_to_nchr]" ) REQUIRE( errmsg(&err) == nil ); } -TEST_CASE( "Decode 2-byte character sequence", "[utf8_to_nchr]" ) +TEST_CASE( "utf8_to_nchr: Decode 2-byte character sequence", "[string/utf.c]" ) { error err; nchar c; @@ -31,7 +31,7 @@ TEST_CASE( "Decode 2-byte character sequence", "[utf8_to_nchr]" ) REQUIRE( errmsg(&err) == nil ); } -TEST_CASE( "Decode 3-byte character sequence", "[utf8_to_nchr]" ) +TEST_CASE( "utf8_to_nchr: Decode 3-byte character sequence", "[string/utf.c]" ) { error err; nchar c; @@ -44,7 +44,7 @@ TEST_CASE( "Decode 3-byte character sequence", "[utf8_to_nchr]" ) REQUIRE( errmsg(&err) == nil ); } -TEST_CASE( "Decode 4-byte character sequence", "[utf8_to_nchr]" ) +TEST_CASE( "utf8_to_nchr: Decode 4-byte character sequence", "[string/utf.c]" ) { error err; nchar c; @@ -57,7 +57,7 @@ TEST_CASE( "Decode 4-byte character sequence", "[utf8_to_nchr]" ) REQUIRE( errmsg(&err) == nil ); } -TEST_CASE( "Error on malformed sequence start", "[utf8_to_nchr]" ) +TEST_CASE( "utf8_to_nchr: Error on malformed sequence start", "[string/utf.c]" ) { error err; nchar c; @@ -72,7 +72,7 @@ TEST_CASE( "Error on malformed sequence start", "[utf8_to_nchr]" ) errput(&err); } -TEST_CASE( "Error on wrong second byte", "[utf8_to_nchr]" ) +TEST_CASE( "utf8_to_nchr: Error on wrong second byte", "[string/utf.c]" ) { error err; nchar c; @@ -87,7 +87,7 @@ TEST_CASE( "Error on wrong second byte", "[utf8_to_nchr]" ) errput(&err); } -TEST_CASE( "Error on wrong third byte", "[utf8_to_nchr]" ) +TEST_CASE( "utf8_to_nchr: Error on wrong third byte", "[string/utf.c]" ) { error err; nchar c; @@ -102,7 +102,7 @@ TEST_CASE( "Error on wrong third byte", "[utf8_to_nchr]" ) errput(&err); } -TEST_CASE( "Error on wrong fourth byte", "[utf8_to_nchr]" ) +TEST_CASE( "utf8_to_nchr: Error on wrong fourth byte", "[string/utf.c]" ) { error err; nchar c; @@ -117,7 +117,7 @@ TEST_CASE( "Error on wrong fourth byte", "[utf8_to_nchr]" ) errput(&err); } -TEST_CASE( "Error on non canonical encoding", "[utf8_to_nchr]" ) +TEST_CASE( "utf8_to_nchr: Error on non canonical encoding", "[string/utf.c]" ) { error err; nchar c;