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