diff --git a/test/string/utf/utf.cmake b/test/string/utf/utf.cmake index 7b5512a..26c2b41 100644 --- a/test/string/utf/utf.cmake +++ b/test/string/utf/utf.cmake @@ -1,8 +1,10 @@ # See the end of this file for copyright and license terms. target_sources(neo_test PRIVATE + string/utf/utf8_check.cpp string/utf/utf8_chrsize.cpp string/utf/utf8_from_nchr.cpp + string/utf/utf8_strlen.cpp string/utf/utf8_to_nchr.cpp ) diff --git a/test/string/utf/utf8_check.cpp b/test/string/utf/utf8_check.cpp new file mode 100644 index 0000000..8d6c110 --- /dev/null +++ b/test/string/utf/utf8_check.cpp @@ -0,0 +1,124 @@ +/** See the end of this file for copyright and license terms. */ + +#include +#include + +#include +#include + +TEST_CASE( "utf8_check: ASCII string", "[string/utf.c]" ) +{ + error err; + utf8_check("i'm gay,,,", &err); + + REQUIRE( errnum(&err) == 0 ); + REQUIRE( errmsg(&err) == nil ); +} + +TEST_CASE( "utf8_check: String with 2-byte UTF-8 sequence", "[string/utf.c]" ) +{ + error err; + /* U+03B1 Greek Smol Letter Alpha */ + utf8_check("i'm g\xce\xb1y,,,", &err); + + REQUIRE( errnum(&err) == 0 ); + REQUIRE( errmsg(&err) == nil ); +} + +TEST_CASE( "utf8_check: String with 3-byte UTF-8 sequence", "[string/utf.c]" ) +{ + error err; + /* U+3042 Hiragana Letter A */ + utf8_check("i'm g\xe3\x81\x82y,,,", &err); + + REQUIRE( errnum(&err) == 0 ); + REQUIRE( errmsg(&err) == nil ); +} + +TEST_CASE( "utf8_check: String with 4-byte UTF-8 sequence", "[string/utf.c]" ) +{ + error err; + /* U+1F97A The Bottom Emoji(TM) */ + utf8_check("i'm gay\xf0\x9f\xa5\xba,,,", &err); + + REQUIRE( errnum(&err) == 0 ); + REQUIRE( errmsg(&err) == nil ); +} + +TEST_CASE( "utf8_check: Error on malformed sequence start", "[string/utf.c]" ) +{ + error err; + utf8_check("\xff", &err); + + string *expected = nstr("Illegal UTF-8 sequence start byte: 0xff", nil); + string *actual = errmsg(&err); + + REQUIRE( errnum(&err) == EINVAL ); + REQUIRE( nstreq(expected, actual, nil) ); + errput(&err); +} + +TEST_CASE( "utf8_check: Error on wrong second byte", "[string/utf.c]" ) +{ + error err; + utf8_check("\xce\xff", &err); + + string *expected = nstr("Byte 2 in UTF-8 sequence invalid: 0xff", nil); + string *actual = errmsg(&err); + + REQUIRE( errnum(&err) == EINVAL ); + REQUIRE( nstreq(expected, actual, nil) ); + errput(&err); +} + +TEST_CASE( "utf8_check: Error on wrong third byte", "[string/utf.c]" ) +{ + error err; + utf8_check("\xe3\x81\xff", &err); + + string *expected = nstr("Byte 3 in UTF-8 sequence invalid: 0xff", nil); + string *actual = errmsg(&err); + + REQUIRE( errnum(&err) == EINVAL ); + REQUIRE( nstreq(expected, actual, nil) ); + errput(&err); +} + +TEST_CASE( "utf8_check: Error on wrong fourth byte", "[string/utf.c]" ) +{ + error err; + utf8_check("\xf0\x9f\xa5\xff", &err); + + string *expected = nstr("Byte 4 in UTF-8 sequence invalid: 0xff", nil); + string *actual = errmsg(&err); + + REQUIRE( errnum(&err) == EINVAL ); + REQUIRE( nstreq(expected, actual, nil) ); + errput(&err); +} + +TEST_CASE( "utf8_check: Error on non canonical encoding", "[string/utf.c]" ) +{ + error err; + utf8_check("\xf0\x80\x80\xa0", &err); + + string *expected = nstr("Non canonical UTF-8 encoding: 1 byte character stored in 4 bytes", nil); + string *actual = errmsg(&err); + + REQUIRE( errnum(&err) == EINVAL ); + REQUIRE( nstreq(expected, actual, nil) ); + errput(&err); +} + +/* + * 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_strlen.cpp b/test/string/utf/utf8_strlen.cpp new file mode 100644 index 0000000..488c7f2 --- /dev/null +++ b/test/string/utf/utf8_strlen.cpp @@ -0,0 +1,41 @@ +/** See the end of this file for copyright and license terms. */ + +#include +#include + +#include +#include + +TEST_CASE( "utf8_strlen: ASCII string", "[string/utf.c]" ) +{ + usize len = utf8_strlen("i'm gay,,,"); + + REQUIRE( len == 10 ); +} + +TEST_CASE( "utf8_strlen: UTF-8 string", "[string/utf.c]" ) +{ + usize len = utf8_strlen("i'm gay\xf0\x9f\xa5\xba,,,"); + + REQUIRE( len == 11 ); +} + +TEST_CASE( "utf8_strlen: Empty string", "[string/utf.c]" ) +{ + usize len = utf8_strlen(""); + + REQUIRE( len == 0 ); +} + +/* + * 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. + */