summaryrefslogtreecommitdiff
path: root/Tests/AK
diff options
context:
space:
mode:
authorMax Wipfli <mail@maxwipfli.ch>2021-06-01 16:14:01 +0200
committerAndreas Kling <kling@serenityos.org>2021-06-03 13:31:46 +0200
commit1c9d87c4558304cf2e955df7e4c49f9f60cd55f2 (patch)
treed443b6d24a99d2c570bfe9386e403655041d36ed /Tests/AK
parentf4a4c36fa0a95211ebbfc8475c4697ea1f2eded2 (diff)
downloadserenity-1c9d87c4558304cf2e955df7e4c49f9f60cd55f2.zip
Tests: Add tests for most functions in AK/CharacterType.h
Diffstat (limited to 'Tests/AK')
-rw-r--r--Tests/AK/CMakeLists.txt1
-rw-r--r--Tests/AK/TestCharacterTypes.cpp135
2 files changed, 136 insertions, 0 deletions
diff --git a/Tests/AK/CMakeLists.txt b/Tests/AK/CMakeLists.txt
index 8ac2f2d60e..4469057dd1 100644
--- a/Tests/AK/CMakeLists.txt
+++ b/Tests/AK/CMakeLists.txt
@@ -10,6 +10,7 @@ set(AK_TEST_SOURCES
TestBitCast.cpp
TestBitmap.cpp
TestByteBuffer.cpp
+ TestCharacterTypes.cpp
TestChecked.cpp
TestCircularDeque.cpp
TestCircularDuplexStream.cpp
diff --git a/Tests/AK/TestCharacterTypes.cpp b/Tests/AK/TestCharacterTypes.cpp
new file mode 100644
index 0000000000..0c21eb75d7
--- /dev/null
+++ b/Tests/AK/TestCharacterTypes.cpp
@@ -0,0 +1,135 @@
+/*
+ * Copyright (c) 2021, Max Wipfli <mail@maxwipfli.ch>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibTest/TestCase.h>
+
+#include <AK/CharacterTypes.h>
+#include <ctype.h>
+
+#define ASCII 0x80
+#define UNICODE 0x10FFFF + 100
+
+void compare_bool_output_over(u32 range, auto& old_function, auto& new_function)
+{
+ bool result1 = false;
+ bool result2 = false;
+ for (u32 i = 0; i < range; ++i) {
+ EXPECT_EQ(result1 = (old_function(i) > 0), result2 = (new_function(i) > 0));
+ if (result1 != result2)
+ dbgln("Function input value was {}.", i);
+ }
+}
+
+void compare_value_output_over(u32 range, auto& old_function, auto& new_function)
+{
+ i64 result1 = 0;
+ i64 result2 = 0;
+ for (u32 i = 0; i < range; ++i) {
+ EXPECT_EQ(result1 = old_function(i), result2 = new_function(i));
+ if (result1 != result2)
+ dbgln("Function input value was {}.", i);
+ }
+}
+
+TEST_CASE(is_ascii)
+{
+ compare_bool_output_over(UNICODE, isascii, is_ascii);
+}
+
+TEST_CASE(is_ascii_alphanumeric)
+{
+ compare_bool_output_over(ASCII, isalnum, is_ascii_alphanumeric);
+}
+
+TEST_CASE(is_ascii_blank)
+{
+ compare_bool_output_over(ASCII, isblank, is_ascii_blank);
+}
+
+TEST_CASE(is_ascii_c0_control)
+{
+ compare_bool_output_over(ASCII - 1, iscntrl, is_ascii_c0_control);
+}
+
+TEST_CASE(is_ascii_control)
+{
+ compare_bool_output_over(ASCII, iscntrl, is_ascii_control);
+}
+
+TEST_CASE(is_ascii_digit)
+{
+ compare_bool_output_over(ASCII, isdigit, is_ascii_digit);
+}
+
+TEST_CASE(is_ascii_graphical)
+{
+ compare_bool_output_over(ASCII, isgraph, is_ascii_graphical);
+}
+
+TEST_CASE(is_ascii_hex_digit)
+{
+ compare_bool_output_over(ASCII, isxdigit, is_ascii_hex_digit);
+}
+
+TEST_CASE(is_ascii_lower_alpha)
+{
+ compare_bool_output_over(ASCII, islower, is_ascii_lower_alpha);
+}
+
+TEST_CASE(is_ascii_printable)
+{
+ compare_bool_output_over(ASCII, isprint, is_ascii_printable);
+}
+
+TEST_CASE(is_ascii_punctuation)
+{
+ compare_bool_output_over(ASCII, ispunct, is_ascii_punctuation);
+}
+
+TEST_CASE(is_ascii_space)
+{
+ compare_bool_output_over(ASCII, isspace, is_ascii_space);
+}
+
+TEST_CASE(is_ascii_upper_alpha)
+{
+ compare_bool_output_over(ASCII, isupper, is_ascii_upper_alpha);
+}
+
+TEST_CASE(to_ascii_lowercase)
+{
+ compare_value_output_over(UNICODE, tolower, to_ascii_lowercase);
+}
+
+TEST_CASE(to_ascii_uppercase)
+{
+ compare_value_output_over(UNICODE, toupper, to_ascii_uppercase);
+}
+
+TEST_CASE(parse_ascii_digit)
+{
+ EXPECT_EQ(parse_ascii_digit('0'), 0u);
+ EXPECT_EQ(parse_ascii_digit('9'), 9u);
+ EXPECT_CRASH("parsing invalid ASCII digit", [] {
+ parse_ascii_digit('a');
+ return Test::Crash::Failure::DidNotCrash;
+ });
+ EXPECT_CRASH("parsing invalid unicode digit", [] {
+ parse_ascii_digit(0x00A9);
+ return Test::Crash::Failure::DidNotCrash;
+ });
+}
+
+TEST_CASE(parse_ascii_hex_digit)
+{
+ EXPECT_EQ(parse_ascii_hex_digit('0'), 0u);
+ EXPECT_EQ(parse_ascii_hex_digit('F'), 15u);
+ EXPECT_EQ(parse_ascii_hex_digit('f'), 15u);
+ EXPECT_CRASH("parsing invalid ASCII hex digit", [] {
+ parse_ascii_hex_digit('g');
+ return Test::Crash::Failure::DidNotCrash;
+ });
+}