aboutsummaryrefslogtreecommitdiff
path: root/libc/test/src/wchar/wcschr_test.cpp
blob: b494f3d632ec93b44cba531e1fd33f9cb0779272 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//===-- Unittests for wcschr ----------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "hdr/types/wchar_t.h"
#include "src/wchar/wcschr.h"
#include "test/UnitTest/Test.h"

TEST(LlvmLibcWCSChrTest, FindsFirstCharacter) {
  // Should return pointer to original string since 'a' is the first character.
  const wchar_t *src = L"abcde";
  ASSERT_EQ(LIBC_NAMESPACE::wcschr(src, L'a'), src);
}

TEST(LlvmLibcWCSChrTest, FindsMiddleCharacter) {
  // Should return pointer to 'c'.
  const wchar_t *src = L"abcde";
  ASSERT_EQ(LIBC_NAMESPACE::wcschr(src, L'c'), (src + 2));
}

TEST(LlvmLibcWCSChrTest, FindsLastCharacterThatIsNotNullTerminator) {
  // Should return pointer to 'e'.
  const wchar_t *src = L"abcde";
  ASSERT_EQ(LIBC_NAMESPACE::wcschr(src, L'e'), (src + 4));
}

TEST(LlvmLibcWCSChrTest, FindsNullTerminator) {
  // Should return pointer to null terminator.
  const wchar_t *src = L"abcde";
  ASSERT_EQ(LIBC_NAMESPACE::wcschr(src, L'\0'), (src + 5));
}

TEST(LlvmLibcWCSChrTest, CharacterNotWithinStringShouldReturnNullptr) {
  // Since 'z' is not within the string, should return nullptr.
  const wchar_t *src = L"abcde";
  ASSERT_EQ(LIBC_NAMESPACE::wcschr(src, L'z'), nullptr);
}

TEST(LlvmLibcWCSChrTest, ShouldFindFirstOfDuplicates) {
  // Should return pointer to the first '1'.
  const wchar_t *src = L"abc1def1ghi";
  ASSERT_EQ((int)(LIBC_NAMESPACE::wcschr(src, L'1') - src), 3);

  // Should return original string since 'X' is the first character.
  const wchar_t *dups = L"XXXXX";
  ASSERT_EQ(LIBC_NAMESPACE::wcschr(dups, L'X'), dups);
}

TEST(LlvmLibcWCSChrTest, EmptyStringShouldOnlyMatchNullTerminator) {
  // Null terminator should match
  const wchar_t *src = L"";
  ASSERT_EQ(src, LIBC_NAMESPACE::wcschr(src, L'\0'));
  // All other characters should not match
  ASSERT_EQ(LIBC_NAMESPACE::wcschr(src, L'Z'), nullptr);
  ASSERT_EQ(LIBC_NAMESPACE::wcschr(src, L'3'), nullptr);
  ASSERT_EQ(LIBC_NAMESPACE::wcschr(src, L'*'), nullptr);
}