114 lines · cpp
1//===-- Unittests for wcsstr ----------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "hdr/types/wchar_t.h"10#include "src/wchar/wcsstr.h"11#include "test/UnitTest/Test.h"12 13TEST(LlvmLibcWCSStrTest, NeedleNotInHaystack) {14 // Should return nullptr if string is not found.15 const wchar_t *haystack = L"12345";16 const wchar_t *needle = L"a";17 ASSERT_EQ(LIBC_NAMESPACE::wcsstr(haystack, needle), nullptr);18}19 20TEST(LlvmLibcWCSStrTest, NeedleIsEmptyString) {21 // Should return pointer to first character if needle is empty.22 const wchar_t *haystack = L"12345";23 const wchar_t *needle = L"";24 ASSERT_EQ(LIBC_NAMESPACE::wcsstr(haystack, needle), haystack);25}26 27TEST(LlvmLibcWCSStrTest, HaystackIsEmptyString) {28 // Should return nullptr since haystack is empty.29 const wchar_t *needle = L"12345";30 const wchar_t *haystack = L"";31 ASSERT_EQ(LIBC_NAMESPACE::wcsstr(haystack, needle), nullptr);32}33 34TEST(LlvmLibcWCSStrTest, HaystackAndNeedleAreEmptyStrings) {35 // Should point to haystack since needle is empty.36 const wchar_t *needle = L"";37 const wchar_t *haystack = L"";38 ASSERT_EQ(LIBC_NAMESPACE::wcsstr(haystack, needle), haystack);39}40 41TEST(LlvmLibcWCSStrTest, HaystackAndNeedleAreSingleCharacters) {42 const wchar_t *haystack = L"a";43 // Should point to haystack.44 ASSERT_EQ(LIBC_NAMESPACE::wcsstr(haystack, L"a"), haystack);45 // Should return nullptr.46 ASSERT_EQ(LIBC_NAMESPACE::wcsstr(haystack, L"b"), nullptr);47}48 49TEST(LlvmLibcWCSStrTest, NeedleEqualToHaystack) {50 const wchar_t *haystack = L"12345";51 // Should point to haystack.52 ASSERT_EQ(LIBC_NAMESPACE::wcsstr(haystack, L"12345"), haystack);53}54 55TEST(LlvmLibcWCSStrTest, NeedleLargerThanHaystack) {56 const wchar_t *haystack = L"123";57 // Should return nullptr.58 ASSERT_EQ(LIBC_NAMESPACE::wcsstr(haystack, L"12345"), nullptr);59}60 61TEST(LlvmLibcWCSStrTest, NeedleAtBeginning) {62 const wchar_t *haystack = L"12345";63 const wchar_t *needle = L"12";64 ASSERT_EQ(LIBC_NAMESPACE::wcsstr(haystack, needle), haystack);65}66 67TEST(LlvmLibcWCSStrTest, NeedleInMiddle) {68 const wchar_t *haystack = L"abcdefghi";69 const wchar_t *needle = L"def";70 ASSERT_EQ(LIBC_NAMESPACE::wcsstr(haystack, needle), haystack + 3);71}72 73TEST(LlvmLibcWCSStrTest, NeedleDirectlyBeforeNullTerminator) {74 const wchar_t *haystack = L"abcdefghi";75 const wchar_t *needle = L"ghi";76 ASSERT_EQ(LIBC_NAMESPACE::wcsstr(haystack, needle), haystack + 6);77}78 79TEST(LlvmLibcWCSStrTest, NeedlePastNullTerminator) {80 const wchar_t haystack[5] = {L'1', L'2', L'\0', L'3', L'4'};81 // Shouldn't find anything after the null terminator.82 ASSERT_EQ(LIBC_NAMESPACE::wcsstr(haystack, /*needle=*/L"3"), nullptr);83 ASSERT_EQ(LIBC_NAMESPACE::wcsstr(haystack, /*needle=*/L"4"), nullptr);84}85 86TEST(LlvmLibcWCSStrTest, PartialNeedle) {87 const wchar_t *haystack = L"la_ap_lap";88 const wchar_t *needle = L"lap";89 // Shouldn't find la or ap.90 ASSERT_EQ(LIBC_NAMESPACE::wcsstr(haystack, needle), haystack + 6);91}92 93TEST(LlvmLibcWCSStrTest, MisspelledNeedle) {94 const wchar_t *haystack = L"atalloftwocities...wait, tale";95 const wchar_t *needle = L"tale";96 ASSERT_EQ(LIBC_NAMESPACE::wcsstr(haystack, needle), haystack + 25);97}98 99TEST(LlvmLibcWCSStrTest, AnagramNeedle) {100 const wchar_t *haystack = L"dgo_ogd_god_odg_gdo_dog";101 const wchar_t *needle = L"dog";102 ASSERT_EQ(LIBC_NAMESPACE::wcsstr(haystack, needle), haystack + 20);103}104 105TEST(LlvmLibcWCSStrTest, MorphedNeedle) {106 // Changes a single letter in the needle to mismatch with the haystack.107 const wchar_t *haystack = L"once upon a time";108 ASSERT_EQ(LIBC_NAMESPACE::wcsstr(haystack, L"time"), haystack + 12);109 ASSERT_EQ(LIBC_NAMESPACE::wcsstr(haystack, L"lime"), nullptr);110 ASSERT_EQ(LIBC_NAMESPACE::wcsstr(haystack, L"tome"), nullptr);111 ASSERT_EQ(LIBC_NAMESPACE::wcsstr(haystack, L"tire"), nullptr);112 ASSERT_EQ(LIBC_NAMESPACE::wcsstr(haystack, L"timo"), nullptr);113}114