1 | //===-- Unittests for tolower----------------------------------------------===// |
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-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "src/__support/CPP/span.h" |
10 | #include "src/ctype/tolower.h" |
11 | |
12 | #include "test/UnitTest/Test.h" |
13 | |
14 | namespace { |
15 | |
16 | // TODO: Merge the ctype tests using this framework. |
17 | // Invariant: UPPER_ARR and LOWER_ARR are both the complete alphabet in the same |
18 | // order. |
19 | constexpr char UPPER_ARR[] = { |
20 | 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', |
21 | 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', |
22 | }; |
23 | constexpr char LOWER_ARR[] = { |
24 | 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', |
25 | 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', |
26 | }; |
27 | |
28 | static_assert( |
29 | sizeof(UPPER_ARR) == sizeof(LOWER_ARR), |
30 | "There must be the same number of uppercase and lowercase letters." ); |
31 | |
32 | int span_index(int ch, LIBC_NAMESPACE::cpp::span<const char> arr) { |
33 | for (size_t i = 0; i < arr.size(); ++i) |
34 | if (static_cast<int>(arr[i]) == ch) |
35 | return static_cast<int>(i); |
36 | return -1; |
37 | } |
38 | |
39 | } // namespace |
40 | |
41 | TEST(LlvmLibcToLower, SimpleTest) { |
42 | EXPECT_EQ(LIBC_NAMESPACE::tolower('a'), int('a')); |
43 | EXPECT_EQ(LIBC_NAMESPACE::tolower('B'), int('b')); |
44 | EXPECT_EQ(LIBC_NAMESPACE::tolower('3'), int('3')); |
45 | |
46 | EXPECT_EQ(LIBC_NAMESPACE::tolower(' '), int(' ')); |
47 | EXPECT_EQ(LIBC_NAMESPACE::tolower('?'), int('?')); |
48 | EXPECT_EQ(LIBC_NAMESPACE::tolower('\0'), int('\0')); |
49 | EXPECT_EQ(LIBC_NAMESPACE::tolower(-1), int(-1)); |
50 | } |
51 | |
52 | TEST(LlvmLibcToLower, DefaultLocale) { |
53 | for (int ch = -255; ch < 255; ++ch) { |
54 | int char_index = span_index(ch, UPPER_ARR); |
55 | if (char_index != -1) |
56 | EXPECT_EQ(LIBC_NAMESPACE::tolower(ch), |
57 | static_cast<int>(LOWER_ARR[char_index])); |
58 | else |
59 | EXPECT_EQ(LIBC_NAMESPACE::tolower(ch), ch); |
60 | } |
61 | } |
62 | |