Skip to content

Commit 87f8bcf

Browse files
committed
Add iswalpha tests
1 parent e310c0b commit 87f8bcf

File tree

4 files changed

+69
-0
lines changed

4 files changed

+69
-0
lines changed

libc/config/baremetal/riscv/entrypoints.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,9 @@ set(TARGET_LIBC_ENTRYPOINTS
278278
libc.src.wchar.wcslen
279279
libc.src.wchar.wctob
280280

281+
# wctype.h entrypoints
282+
libc.src.wctype.iswalpha
283+
281284
# internal entrypoints
282285
libc.startup.baremetal.init
283286
libc.startup.baremetal.fini

libc/test/src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ add_subdirectory(stdlib)
7070
add_subdirectory(string)
7171
add_subdirectory(strings)
7272
add_subdirectory(wchar)
73+
add_subdirectory(wctype)
7374
add_subdirectory(time)
7475
add_subdirectory(unistd)
7576

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
add_custom_target(libc_wctype_unittests)
2+
3+
add_libc_test(
4+
iswalpha_test
5+
SUITE
6+
libc_wctype_unittests
7+
SRCS
8+
iswalpha_test.cpp
9+
DEPENDS
10+
libc.src.wctype.iswalpha
11+
)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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

Comments
 (0)