Serenity Operating System
at master 86 lines 3.4 kB view raw
1/* 2 * Copyright (c) 2020-2022, the SerenityOS developers. 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#include <LibTest/TestCase.h> 8 9#include <AK/DeprecatedString.h> 10#include <AK/MemMem.h> 11#include <AK/Memory.h> 12 13TEST_CASE(bitap) 14{ 15 Array<u8, 8> haystack { 1, 0, 1, 2, 3, 4, 5, 0 }; 16 Array<u8, 4> needle_0 { 2, 3, 4, 5 }; 17 Array<u8, 4> needle_1 { 1, 2, 3, 4 }; 18 Array<u8, 4> needle_2 { 3, 4, 5, 0 }; 19 Array<u8, 4> needle_3 { 3, 4, 5, 6 }; 20 21 auto result_0 = AK::memmem(haystack.data(), haystack.size(), needle_0.data(), needle_0.size()); 22 auto result_1 = AK::memmem(haystack.data(), haystack.size(), needle_1.data(), needle_1.size()); 23 auto result_2 = AK::memmem(haystack.data(), haystack.size(), needle_2.data(), needle_2.size()); 24 auto result_3 = AK::memmem(haystack.data(), haystack.size(), needle_3.data(), needle_3.size()); 25 26 EXPECT_EQ(result_0, &haystack[3]); 27 EXPECT_EQ(result_1, &haystack[2]); 28 EXPECT_EQ(result_2, &haystack[4]); 29 EXPECT_EQ(result_3, nullptr); 30 31 auto haystack_string = "Main function must return c_int\n"sv; 32 auto needle_string = "Main function must return c_int"sv; 33 34 auto result = AK::Detail::bitap_bitwise(haystack_string.characters_without_null_termination(), haystack_string.length(), needle_string.characters_without_null_termination(), needle_string.length()); 35 36 EXPECT_NE(result, nullptr); 37} 38 39TEST_CASE(kmp_one_chunk) 40{ 41 Array<u8, 8> haystack { 1, 0, 1, 2, 3, 4, 5, 0 }; 42 Array<Array<u8, 8>, 1> haystack_arr { haystack }; 43 Array<u8, 4> needle_0 { 2, 3, 4, 5 }; 44 Array<u8, 4> needle_1 { 1, 2, 3, 4 }; 45 Array<u8, 4> needle_2 { 3, 4, 5, 0 }; 46 Array<u8, 4> needle_3 { 3, 4, 5, 6 }; 47 48 auto result_0 = AK::memmem(haystack_arr.begin(), haystack_arr.end(), needle_0); 49 auto result_1 = AK::memmem(haystack_arr.begin(), haystack_arr.end(), needle_1); 50 auto result_2 = AK::memmem(haystack_arr.begin(), haystack_arr.end(), needle_2); 51 auto result_3 = AK::memmem(haystack_arr.begin(), haystack_arr.end(), needle_3); 52 53 EXPECT_EQ(result_0.value_or(9), 3u); 54 EXPECT_EQ(result_1.value_or(9), 2u); 55 EXPECT_EQ(result_2.value_or(9), 4u); 56 EXPECT(!result_3.has_value()); 57} 58 59TEST_CASE(kmp_two_chunks) 60{ 61 Array<u8, 4> haystack_first_half { 1, 0, 1, 2 }, haystack_second_half { 3, 4, 5, 0 }; 62 Array<Array<u8, 4>, 2> haystack { haystack_first_half, haystack_second_half }; 63 Array<u8, 4> needle_0 { 2, 3, 4, 5 }; 64 Array<u8, 4> needle_1 { 1, 2, 3, 4 }; 65 Array<u8, 4> needle_2 { 3, 4, 5, 0 }; 66 Array<u8, 4> needle_3 { 3, 4, 5, 6 }; 67 68 auto result_0 = AK::memmem(haystack.begin(), haystack.end(), needle_0); 69 auto result_1 = AK::memmem(haystack.begin(), haystack.end(), needle_1); 70 auto result_2 = AK::memmem(haystack.begin(), haystack.end(), needle_2); 71 auto result_3 = AK::memmem(haystack.begin(), haystack.end(), needle_3); 72 73 EXPECT_EQ(result_0.value_or(9), 3u); 74 EXPECT_EQ(result_1.value_or(9), 2u); 75 EXPECT_EQ(result_2.value_or(9), 4u); 76 EXPECT(!result_3.has_value()); 77} 78 79TEST_CASE(timing_safe_compare) 80{ 81 DeprecatedString data_set = "abcdefghijklmnopqrstuvwxyz123456789"; 82 EXPECT_EQ(true, AK::timing_safe_compare(data_set.characters(), data_set.characters(), data_set.length())); 83 84 DeprecatedString reversed = data_set.reverse(); 85 EXPECT_EQ(false, AK::timing_safe_compare(data_set.characters(), reversed.characters(), reversed.length())); 86}