|
| 1 | +//===-- Unittests for iswalpha----------------------------------------------===// |
| 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/wctype/iswalpha.h" |
| 11 | + |
| 12 | +#include "test/UnitTest/LibcTest.h" |
| 13 | +#include "test/UnitTest/Test.h" |
| 14 | + |
| 15 | +namespace { |
| 16 | + |
| 17 | +// TODO: Merge the wctype tests using this framework. |
| 18 | +constexpr char WALPHA_ARRAY[] = { |
| 19 | + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', |
| 20 | + 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', |
| 21 | + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', |
| 22 | + 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', |
| 23 | +}; |
| 24 | + |
| 25 | +bool in_span(int ch, LIBC_NAMESPACE::cpp::span<const char> arr) { |
| 26 | + for (size_t i = 0; i < arr.size(); ++i) |
| 27 | + if (static_cast<int>(arr[i]) == ch) |
| 28 | + return true; |
| 29 | + return false; |
| 30 | +} |
| 31 | + |
| 32 | +} // namespace |
| 33 | + |
| 34 | +TEST(LlvmLibciswalpha, SimpleTest) { |
| 35 | + EXPECT_TRUE(LIBC_NAMESPACE::iswalpha('a')); |
| 36 | + EXPECT_TRUE(LIBC_NAMESPACE::iswalpha('B')); |
| 37 | + |
| 38 | + EXPECT_FALSE(LIBC_NAMESPACE::iswalpha('3')); |
| 39 | + EXPECT_FALSE(LIBC_NAMESPACE::iswalpha(' ')); |
| 40 | + EXPECT_FALSE(LIBC_NAMESPACE::iswalpha('?')); |
| 41 | + EXPECT_FALSE(LIBC_NAMESPACE::iswalpha('\0')); |
| 42 | + EXPECT_FALSE(LIBC_NAMESPACE::iswalpha(-1)); |
| 43 | +} |
| 44 | + |
| 45 | +TEST(LlvmLibciswalpha, DefaultLocale) { |
| 46 | + // Loops through all characters, verifying that letters return |
| 47 | + // true and everything else returns false. |
| 48 | + for (int ch = -255; ch < 255; ++ch) { |
| 49 | + if (in_span(ch, WALPHA_ARRAY)) |
| 50 | + EXPECT_TRUE(LIBC_NAMESPACE::iswalpha(ch)); |
| 51 | + else |
| 52 | + EXPECT_FALSE(LIBC_NAMESPACE::iswalpha(ch)); |
| 53 | + } |
| 54 | +} |
0 commit comments