xref: /llvm-project/libc/test/src/ctype/isxdigit_test.cpp (revision 3d94a202302d808db49646c60601e75fdcc40c14)
1 //===-- Unittests for isxdigit---------------------------------------------===//
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/isxdigit.h"
11 
12 #include "test/UnitTest/Test.h"
13 
14 namespace {
15 
16 // TODO: Merge the ctype tests using this framework.
17 constexpr char XDIGIT_ARRAY[] = {
18     'a', 'b', 'c', 'd', 'e', 'f', 'A', 'B', 'C', 'D', 'E',
19     'F', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
20 };
21 
22 bool in_span(int ch, LIBC_NAMESPACE::cpp::span<const char> arr) {
23   for (size_t i = 0; i < arr.size(); ++i)
24     if (static_cast<int>(arr[i]) == ch)
25       return true;
26   return false;
27 }
28 
29 } // namespace
30 
31 TEST(LlvmLibcIsXdigit, SimpleTest) {
32   EXPECT_NE(LIBC_NAMESPACE::isxdigit('a'), 0);
33   EXPECT_NE(LIBC_NAMESPACE::isxdigit('B'), 0);
34   EXPECT_NE(LIBC_NAMESPACE::isxdigit('3'), 0);
35 
36   EXPECT_EQ(LIBC_NAMESPACE::isxdigit('z'), 0);
37   EXPECT_EQ(LIBC_NAMESPACE::isxdigit(' '), 0);
38   EXPECT_EQ(LIBC_NAMESPACE::isxdigit('?'), 0);
39   EXPECT_EQ(LIBC_NAMESPACE::isxdigit('\0'), 0);
40   EXPECT_EQ(LIBC_NAMESPACE::isxdigit(-1), 0);
41 }
42 
43 TEST(LlvmLibcIsXdigit, DefaultLocale) {
44   // Loops through all characters, verifying that numbers and letters
45   // return non-zero integer and everything else returns a zero.
46   for (int ch = -255; ch < 255; ++ch) {
47     if (in_span(ch, XDIGIT_ARRAY))
48       EXPECT_NE(LIBC_NAMESPACE::isxdigit(ch), 0);
49     else
50       EXPECT_EQ(LIBC_NAMESPACE::isxdigit(ch), 0);
51   }
52 }
53