utf: add tests for utf8_strlen and utf8_check

This commit is contained in:
anna 2021-07-16 00:25:57 +02:00
parent f41fc5ad44
commit 9b40d46287
Signed by: fef
GPG key ID: EC22E476DC2D3D84
3 changed files with 167 additions and 0 deletions

View file

@ -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
)

View file

@ -0,0 +1,124 @@
/** See the end of this file for copyright and license terms. */
#include <catch2/catch.hpp>
#include <errno.h>
#include <neo.h>
#include <neo/utf.h>
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 <owo@fef.moe>.
*
* 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
* <https://git.pixie.town/thufie/CNPL>.
*
* libneo comes with ABSOLUTELY NO WARRANTY, to the extent
* permitted by applicable law. See the CNPLv6+ for details.
*/

View file

@ -0,0 +1,41 @@
/** See the end of this file for copyright and license terms. */
#include <catch2/catch.hpp>
#include <errno.h>
#include <neo.h>
#include <neo/utf.h>
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 <owo@fef.moe>.
*
* 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
* <https://git.pixie.town/thufie/CNPL>.
*
* libneo comes with ABSOLUTELY NO WARRANTY, to the extent
* permitted by applicable law. See the CNPLv6+ for details.
*/