1//===-- Unittests for isalpha----------------------------------------------===//
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/isalpha.h"
11
12#include "test/UnitTest/Test.h"
13
14namespace {
15
16// TODO: Merge the ctype tests using this framework.
17constexpr char ALPHA_ARRAY[] = {
18 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
19 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
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
24bool in_span(int ch, LIBC_NAMESPACE::cpp::span<const char> arr) {
25 for (size_t i = 0; i < arr.size(); ++i)
26 if (static_cast<int>(arr[i]) == ch)
27 return true;
28 return false;
29}
30
31} // namespace
32
33TEST(LlvmLibcIsAlpha, SimpleTest) {
34 EXPECT_NE(LIBC_NAMESPACE::isalpha('a'), 0);
35 EXPECT_NE(LIBC_NAMESPACE::isalpha('B'), 0);
36
37 EXPECT_EQ(LIBC_NAMESPACE::isalpha('3'), 0);
38 EXPECT_EQ(LIBC_NAMESPACE::isalpha(' '), 0);
39 EXPECT_EQ(LIBC_NAMESPACE::isalpha('?'), 0);
40 EXPECT_EQ(LIBC_NAMESPACE::isalpha('\0'), 0);
41 EXPECT_EQ(LIBC_NAMESPACE::isalpha(-1), 0);
42}
43
44TEST(LlvmLibcIsAlpha, DefaultLocale) {
45 // Loops through all characters, verifying that letters return a
46 // non-zero integer and everything else returns zero.
47 // TODO: encoding indep
48 for (int ch = -255; ch < 255; ++ch) {
49 if (in_span(ch, ALPHA_ARRAY))
50 EXPECT_NE(LIBC_NAMESPACE::isalpha(ch), 0);
51 else
52 EXPECT_EQ(LIBC_NAMESPACE::isalpha(ch), 0);
53 }
54}
55

Provided by KDAB

Privacy Policy
Learn to use CMake with our Intro Training
Find out more

source code of libc/test/src/ctype/isalpha_test.cpp