From 8942041eeef0c51247f8989066f7cfaff236b756 Mon Sep 17 00:00:00 2001 From: Martin Janiczek Date: Mon, 2 Oct 2023 22:26:07 +0200 Subject: [PATCH] AK: Test is_ascii, to_ascii_*case over random Unicode samples --- Tests/AK/TestCharacterTypes.cpp | 48 ++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/Tests/AK/TestCharacterTypes.cpp b/Tests/AK/TestCharacterTypes.cpp index 0f5e5f492a..1f64f77ae4 100644 --- a/Tests/AK/TestCharacterTypes.cpp +++ b/Tests/AK/TestCharacterTypes.cpp @@ -12,6 +12,8 @@ #define ASCII 0x80 #define UNICODE 0x10FFFF + 100 +using namespace Test::Randomized; + void compare_bool_output_over(u32 range, auto& old_function, auto& new_function) { bool result1 = false; @@ -19,7 +21,7 @@ void compare_bool_output_over(u32 range, auto& old_function, auto& new_function) 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); + FAIL(String::formatted("New result {} does not match old result {} for input {}.", result1, result2, i)); } } @@ -30,11 +32,35 @@ void compare_value_output_over(u32 range, auto& old_function, auto& new_function 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); + FAIL(String::formatted("New result {} does not match old result {} for input {}.", result1, result2, i)); } } -// NOTE: Avoid comparing over UNICODE in "TEST_CASE" due to test runtime becoming too long. +void randomized_compare_bool_output_over(u32 range, auto& old_function, auto& new_function) +{ + // NOTE: randomized tests also run multiple times (100 by default). This means we'll try 10k random numbers times each time the test suite is run. + for (u32 n = 0; n < 100; ++n) { + bool result1 = false; + bool result2 = false; + GEN(i, Gen::unsigned_int(range - 1)); + EXPECT_EQ(result1 = (old_function(i) > 0), result2 = (new_function(i) > 0)); + if (result1 != result2) + FAIL(String::formatted("New result {} does not match old result {} for input {}.", result1, result2, i)); + } +} + +void randomized_compare_value_output_over(u32 range, auto& old_function, auto& new_function) +{ + // NOTE: randomized tests also run multiple times (100 by default). This means we'll try 10k random numbers times each time the test suite is run. + for (u32 n = 0; n < 100; ++n) { + i64 result1 = false; + i64 result2 = false; + GEN(i, Gen::unsigned_int(range - 1)); + EXPECT_EQ(result1 = old_function(i), result2 = new_function(i)); + if (result1 != result2) + FAIL(String::formatted("New result {} does not match old result {} for input {}.", result1, result2, i)); + } +} TEST_CASE(is_ascii_alphanumeric) { @@ -145,3 +171,19 @@ BENCHMARK_CASE(to_ascii_uppercase_unicode) { compare_value_output_over(UNICODE, toupper, to_ascii_uppercase); } + +// NOTE: This would take too long to run exhaustively. Let's at least run random subsets of it! +RANDOMIZED_TEST_CASE(is_ascii_unicode) +{ + randomized_compare_bool_output_over(UNICODE, isascii, is_ascii); +} + +RANDOMIZED_TEST_CASE(to_ascii_lowercase_unicode) +{ + randomized_compare_value_output_over(UNICODE, tolower, to_ascii_lowercase); +} + +RANDOMIZED_TEST_CASE(to_ascii_uppercase_unicode) +{ + randomized_compare_value_output_over(UNICODE, toupper, to_ascii_uppercase); +}