From b34f2149eeaecd54f4d342a85c149437111e5a11 Mon Sep 17 00:00:00 2001 From: apio Date: Fri, 28 Apr 2023 20:01:18 +0200 Subject: [PATCH] tests: Test libluna's UTF-8 suite --- tests/CMakeLists.txt | 1 + tests/libluna/TestUtf8.cpp | 141 +++++++++++++++++++++++++++++++++++++ 2 files changed, 142 insertions(+) create mode 100644 tests/libluna/TestUtf8.cpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 6868f67f..53c9eee9 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -18,3 +18,4 @@ endfunction() luna_test(libluna/TestVector.cpp TestVector OFF) luna_test(libluna/TestBase64.cpp TestBase64 OFF) +luna_test(libluna/TestUtf8.cpp TestUtf8 OFF) diff --git a/tests/libluna/TestUtf8.cpp b/tests/libluna/TestUtf8.cpp new file mode 100644 index 00000000..4966ccf0 --- /dev/null +++ b/tests/libluna/TestUtf8.cpp @@ -0,0 +1,141 @@ +#include +#include +#include + +TestResult test_basic_utf8_ascii_decoding() +{ + wchar_t buf[20]; + + Utf8StringDecoder decoder("hello, world!"); + validate(!decoder.decode(buf, sizeof(buf)).has_error()); + + validate(!wcscmp(buf, L"hello, world!")); + + test_success; +} + +TestResult test_basic_utf8_ascii_encoding() +{ + char buf[20]; + + Utf8StringEncoder encoder(L"hello, world!"); + validate(!encoder.encode(buf, sizeof(buf)).has_error()); + + validate(!strcmp(buf, "hello, world!")); + + test_success; +} + +TestResult test_utf8_decoding_with_non_ascii_chars() +{ + wchar_t buf[20]; + + Utf8StringDecoder decoder("¿ñ?"); + validate(!decoder.decode(buf, sizeof(buf)).has_error()); + + validate(!wcscmp(buf, L"¿ñ?")); + + test_success; +} + +TestResult test_utf8_encoding_with_non_ascii_chars() +{ + char buf[20]; + + Utf8StringEncoder encoder(L"¿ñ?"); + validate(!encoder.encode(buf, sizeof(buf)).has_error()); + + validate(!strcmp(buf, "¿ñ?")); + + test_success; +} + +TestResult test_utf8_decoding_invalid_continuation_byte_at_start() +{ + Utf8StringDecoder decoder("\x83"); + validate(decoder.code_points().has_error()); + + test_success; +} + +TestResult test_utf8_decoding_invalid_start_byte_at_continuation() +{ + Utf8StringDecoder decoder("\xc2\x7f"); + validate(decoder.code_points().has_error()); + + test_success; +} + +TestResult test_utf8_decoding_unfinished_sequence() +{ + Utf8StringDecoder decoder("\xe0\x83"); + validate(decoder.code_points().has_error()); + + test_success; +} + +TestResult test_utf8_decoding_invalid_bytes() +{ + Utf8StringDecoder decoder("\xf5\xf7"); + validate(decoder.code_points().has_error()); + + test_success; +} + +TestResult test_utf8_decoding_overlong_encoding() +{ + Utf8StringDecoder decoder("\xe0\x80\x80"); + validate(decoder.code_points().has_error()); + + test_success; +} + +TestResult test_utf8_decoder_calculate_code_points() +{ + Utf8StringDecoder decoder("¿ñ? ✨"); + auto rc = decoder.code_points(); + validate(!rc.has_error()); + validate(rc.value() == 5); + + test_success; +} + +TestResult test_utf8_encoder_calculate_byte_length() +{ + Utf8StringEncoder encoder(L"¿ñ? ✨"); + auto rc = encoder.byte_length(); + validate(!rc.has_error()); + validate(rc.value() == 9); + + test_success; +} + +TestResult test_utf8_encoder_code_points_outside_unicode() +{ + const wchar_t buf[] = { 0x120000, 0x000000 }; + + Utf8StringEncoder encoder(buf); + validate(encoder.byte_length().has_error()); + + test_success; +} + +Result test_main() +{ + test_prelude; + + run_test(test_basic_utf8_ascii_decoding); + run_test(test_basic_utf8_ascii_encoding); + run_test(test_utf8_decoding_with_non_ascii_chars); + run_test(test_utf8_encoding_with_non_ascii_chars); + run_test(test_utf8_decoding_invalid_continuation_byte_at_start); + run_test(test_utf8_decoding_invalid_start_byte_at_continuation); + run_test(test_utf8_decoding_unfinished_sequence); + run_test(test_utf8_decoding_invalid_bytes); + run_test(test_utf8_decoding_overlong_encoding); + run_test(test_utf8_decoder_calculate_code_points); + run_test(test_utf8_encoder_calculate_byte_length); + run_test(test_utf8_encoder_code_points_outside_unicode); + + return {}; +}