Skip to content

Commit c874185

Browse files
authored
[libc] Implemented wcsncmp (#142429)
Implemented wcsncmp and added tests
1 parent e3d1a33 commit c874185

File tree

7 files changed

+258
-0
lines changed

7 files changed

+258
-0
lines changed

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ set(TARGET_LIBC_ENTRYPOINTS
366366
libc.src.wchar.wctob
367367
libc.src.wchar.wmemset
368368
libc.src.wchar.wcschr
369+
libc.src.wchar.wcsncmp
369370
libc.src.wchar.wcscmp
370371
libc.src.wchar.wcspbrk
371372
libc.src.wchar.wcsrchr

libc/include/wchar.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ functions:
4242
arguments:
4343
- type: const wchar_t *
4444
- type: wchar_t
45+
- name: wcsncmp
46+
standards:
47+
- stdc
48+
return_type: int
49+
arguments:
50+
- type: const wchar_t *
51+
- type: const wchar_t *
52+
- type: size_t
4553
- name: wcscmp
4654
standards:
4755
- stdc

libc/src/wchar/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,17 @@ add_entrypoint_object(
8989
libc.hdr.wchar_macros
9090
)
9191

92+
add_entrypoint_object(
93+
wcsncmp
94+
SRCS
95+
wcsncmp.cpp
96+
HDRS
97+
wcsncmp.h
98+
DEPENDS
99+
libc.hdr.wchar_macros
100+
libc.hdr.types.size_t
101+
)
102+
92103
add_entrypoint_object(
93104
wcsspn
94105
SRCS

libc/src/wchar/wcsncmp.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//===-- Implementation of wcsncmp -----------------------------------------===//
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/wchar/wcsncmp.h"
10+
11+
#include "hdr/types/size_t.h"
12+
#include "hdr/types/wchar_t.h"
13+
#include "src/__support/common.h"
14+
#include "src/__support/macros/null_check.h"
15+
16+
namespace LIBC_NAMESPACE_DECL {
17+
18+
LLVM_LIBC_FUNCTION(int, wcsncmp,
19+
(const wchar_t *left, const wchar_t *right, size_t n)) {
20+
LIBC_CRASH_ON_NULLPTR(left);
21+
LIBC_CRASH_ON_NULLPTR(right);
22+
23+
if (n == 0)
24+
return 0;
25+
26+
auto comp = [](wchar_t l, wchar_t r) -> int { return l - r; };
27+
28+
for (; n > 1; --n, ++left, ++right) {
29+
wchar_t lc = *left;
30+
if (!comp(lc, '\0') || comp(lc, *right))
31+
break;
32+
}
33+
return comp(*reinterpret_cast<const wchar_t *>(left),
34+
*reinterpret_cast<const wchar_t *>(right));
35+
}
36+
37+
} // namespace LIBC_NAMESPACE_DECL

libc/src/wchar/wcsncmp.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===-- Implementation header for wcsncmp ---------------------------------===//
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+
#ifndef LLVM_LIBC_SRC_WCHAR_WCSNCMP_H
10+
#define LLVM_LIBC_SRC_WCHAR_WCSNCMP_H
11+
12+
#include "hdr/types/size_t.h"
13+
#include "hdr/types/wchar_t.h"
14+
#include "src/__support/macros/config.h"
15+
16+
namespace LIBC_NAMESPACE_DECL {
17+
18+
int wcsncmp(const wchar_t *left, const wchar_t *right, size_t n);
19+
20+
} // namespace LIBC_NAMESPACE_DECL
21+
22+
#endif // LLVM_LIBC_SRC_WCHAR_WCSNCMP_H

libc/test/src/wchar/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ add_libc_test(
5555
libc.src.wchar.wcschr
5656
)
5757

58+
add_libc_test(
59+
wcsncmp_test
60+
SUITE
61+
libc_wchar_unittests
62+
SRCS
63+
wcsncmp_test.cpp
64+
DEPENDS
65+
libc.src.wchar.wcsncmp
66+
)
67+
5868
add_libc_test(
5969
wcscmp_test
6070
SUITE

libc/test/src/wchar/wcsncmp_test.cpp

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
//===-- Unittests for wcsncmp ---------------------------------------------===//
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/wchar/wcsncmp.h"
10+
#include "test/UnitTest/Test.h"
11+
12+
// This group is just copies of the wcscmp tests, since all the same cases still
13+
// need to be tested.
14+
15+
TEST(LlvmLibcWcsncmpTest, EmptyStringsShouldReturnZeroWithSufficientLength) {
16+
const wchar_t *s1 = L"";
17+
const wchar_t *s2 = L"";
18+
int result = LIBC_NAMESPACE::wcsncmp(s1, s2, 1);
19+
ASSERT_EQ(result, 0);
20+
21+
// Verify operands reversed.
22+
result = LIBC_NAMESPACE::wcsncmp(s2, s1, 1);
23+
ASSERT_EQ(result, 0);
24+
}
25+
26+
TEST(LlvmLibcWcsncmpTest,
27+
EmptyStringShouldNotEqualNonEmptyStringWithSufficientLength) {
28+
const wchar_t *empty = L"";
29+
const wchar_t *s2 = L"abc";
30+
int result = LIBC_NAMESPACE::wcsncmp(empty, s2, 3);
31+
ASSERT_LT(result, 0);
32+
33+
// Similar case if empty string is second argument.
34+
const wchar_t *s3 = L"123";
35+
result = LIBC_NAMESPACE::wcsncmp(s3, empty, 3);
36+
ASSERT_GT(result, 0);
37+
}
38+
39+
TEST(LlvmLibcWcsncmpTest, EqualStringsShouldReturnZeroWithSufficientLength) {
40+
const wchar_t *s1 = L"abc";
41+
const wchar_t *s2 = L"abc";
42+
int result = LIBC_NAMESPACE::wcsncmp(s1, s2, 3);
43+
ASSERT_EQ(result, 0);
44+
45+
// Verify operands reversed.
46+
result = LIBC_NAMESPACE::wcsncmp(s2, s1, 3);
47+
ASSERT_EQ(result, 0);
48+
}
49+
50+
TEST(LlvmLibcWcsncmpTest,
51+
ShouldReturnResultOfFirstDifferenceWithSufficientLength) {
52+
const wchar_t *s1 = L"___B42__";
53+
const wchar_t *s2 = L"___C55__";
54+
int result = LIBC_NAMESPACE::wcsncmp(s1, s2, 8);
55+
ASSERT_LT(result, 0);
56+
57+
// Verify operands reversed.
58+
result = LIBC_NAMESPACE::wcsncmp(s2, s1, 8);
59+
ASSERT_GT(result, 0);
60+
}
61+
62+
TEST(LlvmLibcWcsncmpTest,
63+
CapitalizedLetterShouldNotBeEqualWithSufficientLength) {
64+
const wchar_t *s1 = L"abcd";
65+
const wchar_t *s2 = L"abCd";
66+
int result = LIBC_NAMESPACE::wcsncmp(s1, s2, 4);
67+
ASSERT_GT(result, 0);
68+
69+
// Verify operands reversed.
70+
result = LIBC_NAMESPACE::wcsncmp(s2, s1, 4);
71+
ASSERT_LT(result, 0);
72+
}
73+
74+
TEST(LlvmLibcWcsncmpTest,
75+
UnequalLengthStringsShouldNotReturnZeroWithSufficientLength) {
76+
const wchar_t *s1 = L"abc";
77+
const wchar_t *s2 = L"abcd";
78+
int result = LIBC_NAMESPACE::wcsncmp(s1, s2, 4);
79+
ASSERT_LT(result, 0);
80+
81+
// Verify operands reversed.
82+
result = LIBC_NAMESPACE::wcsncmp(s2, s1, 4);
83+
ASSERT_GT(result, 0);
84+
}
85+
86+
TEST(LlvmLibcWcsncmpTest, StringArgumentSwapChangesSignWithSufficientLength) {
87+
const wchar_t *a = L"a";
88+
const wchar_t *b = L"b";
89+
int result = LIBC_NAMESPACE::wcsncmp(b, a, 1);
90+
ASSERT_GT(result, 0);
91+
92+
result = LIBC_NAMESPACE::wcsncmp(a, b, 1);
93+
ASSERT_LT(result, 0);
94+
}
95+
96+
#if defined(LIBC_ADD_NULL_CHECKS) && !defined(LIBC_HAS_SANITIZER)
97+
TEST(LlvmLibcWcsncmpTest, NullptrCrash) {
98+
// Passing in a nullptr should crash the program.
99+
EXPECT_DEATH([] { LIBC_NAMESPACE::wcsncmp(L"aaaaaaaaaaaaaa", nullptr, 3); },
100+
WITH_SIGNAL(-1));
101+
EXPECT_DEATH([] { LIBC_NAMESPACE::wcsncmp(nullptr, L"aaaaaaaaaaaaaa", 3); },
102+
WITH_SIGNAL(-1));
103+
}
104+
#endif // LIBC_HAS_ADDRESS_SANITIZER
105+
106+
// This group is actually testing wcsncmp functionality
107+
108+
TEST(LlvmLibcWcsncmpTest, NonEqualStringsEqualWithLengthZero) {
109+
const wchar_t *s1 = L"abc";
110+
const wchar_t *s2 = L"def";
111+
int result = LIBC_NAMESPACE::wcsncmp(s1, s2, 0);
112+
ASSERT_EQ(result, 0);
113+
114+
// Verify operands reversed.
115+
result = LIBC_NAMESPACE::wcsncmp(s2, s1, 0);
116+
ASSERT_EQ(result, 0);
117+
}
118+
119+
TEST(LlvmLibcWcsncmpTest, NonEqualStringsNotEqualWithLengthOne) {
120+
const wchar_t *s1 = L"abc";
121+
const wchar_t *s2 = L"def";
122+
int result = LIBC_NAMESPACE::wcsncmp(s1, s2, 1);
123+
ASSERT_LT(result, 0);
124+
125+
// Verify operands reversed.
126+
result = LIBC_NAMESPACE::wcsncmp(s2, s1, 1);
127+
ASSERT_GT(result, 0);
128+
}
129+
130+
TEST(LlvmLibcWcsncmpTest, NonEqualStringsEqualWithShorterLength) {
131+
const wchar_t *s1 = L"___B42__";
132+
const wchar_t *s2 = L"___C55__";
133+
int result = LIBC_NAMESPACE::wcsncmp(s1, s2, 3);
134+
ASSERT_EQ(result, 0);
135+
136+
// This should return 'B' - 'C' = -1.
137+
result = LIBC_NAMESPACE::wcsncmp(s1, s2, 4);
138+
ASSERT_LT(result, 0);
139+
140+
// Verify operands reversed.
141+
result = LIBC_NAMESPACE::wcsncmp(s2, s1, 3);
142+
ASSERT_EQ(result, 0);
143+
144+
// This should return 'C' - 'B' = 1.
145+
result = LIBC_NAMESPACE::wcsncmp(s2, s1, 4);
146+
ASSERT_GT(result, 0);
147+
}
148+
149+
TEST(LlvmLibcWcsncmpTest, StringComparisonEndsOnNullByteEvenWithLongerLength) {
150+
const wchar_t *s1 = L"abc\0def";
151+
const wchar_t *s2 = L"abc\0abc";
152+
int result = LIBC_NAMESPACE::wcsncmp(s1, s2, 7);
153+
ASSERT_EQ(result, 0);
154+
155+
// Verify operands reversed.
156+
result = LIBC_NAMESPACE::wcsncmp(s2, s1, 7);
157+
ASSERT_EQ(result, 0);
158+
}
159+
160+
TEST(LlvmLibcWcsncmpTest, Case) {
161+
const wchar_t *s1 = L"aB";
162+
const wchar_t *s2 = L"ab";
163+
int result = LIBC_NAMESPACE::wcsncmp(s1, s2, 2);
164+
ASSERT_LT(result, 0);
165+
166+
// Verify operands reversed.
167+
result = LIBC_NAMESPACE::wcsncmp(s2, s1, 2);
168+
ASSERT_GT(result, 0);
169+
}

0 commit comments

Comments
 (0)