mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-13 01:59:14 +03:00
35c0a6c54d
As many macros as possible are moved to Macros.h, while the macros to create a test case are moved to TestCase.h. TestCase is now the only user-facing header for creating a test case. TestSuite and its helpers have moved into a .cpp file. Instead of requiring a TEST_MAIN macro to be instantiated into the test file, a TestMain.cpp file is provided instead that will be linked against each test. This has the side effect that, if we wanted to have test cases split across multiple files, it's as simple as adding them all to the same executable. The test main should be portable to kernel mode as well, so if there's a set of tests that should be run in self-test mode in kernel space, we can accomodate that. A new serenity_test CMake function streamlines adding a new test with arguments for the test source file, subdirectory under /usr/Tests to install the test application and an optional list of libraries to link against the test application. To accomodate future test where the provided TestMain.cpp is not suitable (e.g. test-js), a CUSTOM_MAIN parameter can be passed to the function to not link against the boilerplate main function.
70 lines
2.0 KiB
C++
70 lines
2.0 KiB
C++
/*
|
||
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
|
||
*
|
||
* SPDX-License-Identifier: BSD-2-Clause
|
||
*/
|
||
|
||
#include <LibTest/TestCase.h>
|
||
|
||
#include <AK/Utf8View.h>
|
||
|
||
TEST_CASE(decode_ascii)
|
||
{
|
||
Utf8View utf8 { "Hello World!11" };
|
||
EXPECT(utf8.validate());
|
||
|
||
u32 expected[] = { 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33, 49, 49 };
|
||
size_t expected_size = sizeof(expected) / sizeof(expected[0]);
|
||
|
||
size_t i = 0;
|
||
for (u32 code_point : utf8) {
|
||
VERIFY(i < expected_size);
|
||
EXPECT_EQ(code_point, expected[i]);
|
||
i++;
|
||
}
|
||
EXPECT_EQ(i, expected_size);
|
||
}
|
||
|
||
TEST_CASE(decode_utf8)
|
||
{
|
||
Utf8View utf8 { "Привет, мир! 😀 γειά σου κόσμος こんにちは世界" };
|
||
size_t valid_bytes;
|
||
EXPECT(utf8.validate(valid_bytes));
|
||
EXPECT(valid_bytes == (size_t)utf8.byte_length());
|
||
|
||
u32 expected[] = { 1055, 1088, 1080, 1074, 1077, 1090, 44, 32, 1084, 1080, 1088, 33, 32, 128512, 32, 947, 949, 953, 940, 32, 963, 959, 965, 32, 954, 972, 963, 956, 959, 962, 32, 12371, 12435, 12395, 12385, 12399, 19990, 30028 };
|
||
size_t expected_size = sizeof(expected) / sizeof(expected[0]);
|
||
|
||
size_t i = 0;
|
||
for (u32 code_point : utf8) {
|
||
VERIFY(i < expected_size);
|
||
EXPECT_EQ(code_point, expected[i]);
|
||
i++;
|
||
}
|
||
EXPECT_EQ(i, expected_size);
|
||
}
|
||
|
||
TEST_CASE(validate_invalid_ut8)
|
||
{
|
||
size_t valid_bytes;
|
||
char invalid_utf8_1[] = { 42, 35, (char)182, 9, 0 };
|
||
Utf8View utf8_1 { invalid_utf8_1 };
|
||
EXPECT(!utf8_1.validate(valid_bytes));
|
||
EXPECT(valid_bytes == 2);
|
||
|
||
char invalid_utf8_2[] = { 42, 35, (char)208, (char)208, 0 };
|
||
Utf8View utf8_2 { invalid_utf8_2 };
|
||
EXPECT(!utf8_2.validate(valid_bytes));
|
||
EXPECT(valid_bytes == 2);
|
||
|
||
char invalid_utf8_3[] = { (char)208, 0 };
|
||
Utf8View utf8_3 { invalid_utf8_3 };
|
||
EXPECT(!utf8_3.validate(valid_bytes));
|
||
EXPECT(valid_bytes == 0);
|
||
|
||
char invalid_utf8_4[] = { (char)208, 35, 0 };
|
||
Utf8View utf8_4 { invalid_utf8_4 };
|
||
EXPECT(!utf8_4.validate(valid_bytes));
|
||
EXPECT(valid_bytes == 0);
|
||
}
|