xref: /llvm-project/libc/test/src/ctype/toupper_test.cpp (revision 3d94a202302d808db49646c60601e75fdcc40c14)
1 //===-- Unittests for toupper----------------------------------------------===//
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/toupper.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(LlvmLibcToUpper, SimpleTest) {
42   EXPECT_EQ(LIBC_NAMESPACE::toupper('a'), int('A'));
43   EXPECT_EQ(LIBC_NAMESPACE::toupper('B'), int('B'));
44   EXPECT_EQ(LIBC_NAMESPACE::toupper('3'), int('3'));
45 
46   EXPECT_EQ(LIBC_NAMESPACE::toupper(' '), int(' '));
47   EXPECT_EQ(LIBC_NAMESPACE::toupper('?'), int('?'));
48   EXPECT_EQ(LIBC_NAMESPACE::toupper('\0'), int('\0'));
49   EXPECT_EQ(LIBC_NAMESPACE::toupper(-1), int(-1));
50 }
51 
52 TEST(LlvmLibcToUpper, DefaultLocale) {
53   for (int ch = -255; ch < 255; ++ch) {
54     int char_index = span_index(ch, LOWER_ARR);
55     if (char_index != -1)
56       EXPECT_EQ(LIBC_NAMESPACE::toupper(ch),
57                 static_cast<int>(UPPER_ARR[char_index]));
58     else
59       EXPECT_EQ(LIBC_NAMESPACE::toupper(ch), ch);
60   }
61 }
62