Skip to content

Commit a8c6a50

Browse files
sribee8Sriya Pratipati
andauthored
[libc] wmemcmp implementation (#141880)
Implemented wmemcmp as well as tests for the function. Fixes Issue #141857 --------- Co-authored-by: Sriya Pratipati <[email protected]>
1 parent ddc8db7 commit a8c6a50

File tree

7 files changed

+149
-0
lines changed

7 files changed

+149
-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.wmemcmp
369370

370371
# sys/uio.h entrypoints
371372
libc.src.sys.uio.writev

libc/include/wchar.yaml

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

libc/src/wchar/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,15 @@ add_entrypoint_object(
5656
libc.hdr.wchar_macros
5757
libc.src.__support.wctype_utils
5858
)
59+
60+
add_entrypoint_object(
61+
wmemcmp
62+
SRCS
63+
wmemcmp.cpp
64+
HDRS
65+
wmemcmp.h
66+
DEPENDS
67+
libc.hdr.types.size_t
68+
libc.hdr.wchar_macros
69+
libc.src.__support.wctype_utils
70+
)

libc/src/wchar/wmemcmp.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//===-- Implementation of wmemcmp -----------------------------------------===//
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/wmemcmp.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/config.h"
15+
16+
namespace LIBC_NAMESPACE_DECL {
17+
18+
LLVM_LIBC_FUNCTION(int, wmemcmp,
19+
(const wchar_t *s1, const wchar_t *s2, size_t n)) {
20+
for (size_t i = 0; i < n; ++i) {
21+
if (s1[i] != s2[i])
22+
return (int)(s1[i] - s2[i]);
23+
}
24+
// If it reaches the end, all n values must be the same.
25+
return 0;
26+
}
27+
28+
} // namespace LIBC_NAMESPACE_DECL

libc/src/wchar/wmemcmp.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===-- Implementation header for wmemcmp ---------------------------------===//
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_WMEMCMP_H
10+
#define LLVM_LIBC_SRC_WCHAR_WMEMCMP_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 wmemcmp(const wchar_t *s1, const wchar_t *s2, size_t n);
19+
20+
} // namespace LIBC_NAMESPACE_DECL
21+
22+
#endif // LLVM_LIBC_SRC_WCHAR_WMEMCMP_H

libc/test/src/wchar/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,13 @@ add_libc_test(
5454
DEPENDS
5555
libc.src.wchar.wcschr
5656
)
57+
58+
add_libc_test(
59+
wmemcmp_test
60+
SUITE
61+
libc_wchar_unittests
62+
SRCS
63+
wmemcmp_test.cpp
64+
DEPENDS
65+
libc.src.wchar.wmemcmp
66+
)

libc/test/src/wchar/wmemcmp_test.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//===-- Unittests for wmemcmp ---------------------------------------------===//
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 "hdr/types/size_t.h"
10+
#include "hdr/types/wchar_t.h"
11+
#include "src/wchar/wmemcmp.h"
12+
#include "test/UnitTest/Test.h"
13+
14+
TEST(LlvmLibcWMemcmpTest, CmpZeroByte) {
15+
// Comparing zero bytes should result in 0.
16+
const wchar_t *lhs = L"ab";
17+
const wchar_t *rhs = L"yz";
18+
EXPECT_EQ(LIBC_NAMESPACE::wmemcmp(lhs, rhs, 0), 0);
19+
}
20+
21+
TEST(LlvmLibcWMemcmpTest, LhsRhsAreTheSame) {
22+
// Comparing strings of equal value should result in 0.
23+
const wchar_t *lhs = L"ab";
24+
const wchar_t *rhs = L"ab";
25+
EXPECT_EQ(LIBC_NAMESPACE::wmemcmp(lhs, rhs, 2), 0);
26+
}
27+
28+
TEST(LlvmLibcWMemcmpTest, LhsBeforeRhsLexically) {
29+
// z after b, should result in a value less than 0.
30+
const wchar_t *lhs = L"ab";
31+
const wchar_t *rhs = L"az";
32+
EXPECT_LT(LIBC_NAMESPACE::wmemcmp(lhs, rhs, 2), 0);
33+
}
34+
35+
TEST(LlvmLibcWMemcmpTest, LhsAfterRhsLexically) {
36+
// z after b, should result in a value greater than 0.
37+
const wchar_t *lhs = L"az";
38+
const wchar_t *rhs = L"ab";
39+
EXPECT_GT(LIBC_NAMESPACE::wmemcmp(lhs, rhs, 2), 0);
40+
}
41+
42+
TEST(LlvmLibcWMemcmpTest, CompareToEmpty) {
43+
// lhs is nonempty, should result in a value greater than 0.
44+
const wchar_t *lhs = L"az";
45+
const wchar_t *rhs = L"";
46+
EXPECT_GT(LIBC_NAMESPACE::wmemcmp(lhs, rhs, 1), 0);
47+
}
48+
49+
TEST(LlvmLibcWMemcmpTest, LhsAfterRhsLexicallyLong) {
50+
// b after a, should result in a value greater than 0.
51+
const wchar_t *lhs = L"aaaaaaaaaaaaab";
52+
const wchar_t *rhs = L"aaaaaaaaaaaaaa";
53+
EXPECT_GT(LIBC_NAMESPACE::wmemcmp(lhs, rhs, 15), 0);
54+
}
55+
56+
TEST(LlvmLibcWMemcmpTest, RhsAfterLhsLexicallyLong) {
57+
// b after a, should result in a value less than 0.
58+
const wchar_t *lhs = L"aaaaaaaaaaaaaa";
59+
const wchar_t *rhs = L"aaaaaaaaaaaaab";
60+
EXPECT_LT(LIBC_NAMESPACE::wmemcmp(lhs, rhs, 15), 0);
61+
}
62+
63+
TEST(LlvmLibcWMemcmpTest, LhsRhsAreTheSameLong) {
64+
// Comparing strings of equal value should result in 0.
65+
const wchar_t *lhs = L"aaaaaaaaaaaaaa";
66+
const wchar_t *rhs = L"aaaaaaaaaaaaaa";
67+
EXPECT_EQ(LIBC_NAMESPACE::wmemcmp(lhs, rhs, 15), 0);
68+
}

0 commit comments

Comments
 (0)