Skip to content

Commit a267962

Browse files
uzairnawazsvkeerthy
authored andcommitted
[libc] Implemented wmemset and added tests (#141691)
Implemented and tests the wmemset function in libc.
1 parent 7755254 commit a267962

File tree

7 files changed

+168
-0
lines changed

7 files changed

+168
-0
lines changed

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ set(TARGET_LIBC_ENTRYPOINTS
364364
libc.src.wchar.btowc
365365
libc.src.wchar.wcslen
366366
libc.src.wchar.wctob
367+
libc.src.wchar.wmemset
367368
libc.src.wchar.wcschr
368369

369370
# sys/uio.h entrypoints

libc/include/wchar.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ functions:
2727
return_type: wint_t
2828
arguments:
2929
- type: int
30+
- name: wmemset
31+
standards:
32+
- stdc
33+
return_type: wchar_t*
34+
arguments:
35+
- type: wchar_t*
36+
- type: wchar_t
37+
- type: size_t
3038
- name: wcschr
3139
standards:
3240
- stdc

libc/src/wchar/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ add_entrypoint_object(
3434
libc.src.__support.wctype_utils
3535
)
3636

37+
add_entrypoint_object(
38+
wmemset
39+
SRCS
40+
wmemset.cpp
41+
HDRS
42+
wmemset.h
43+
DEPENDS
44+
libc.hdr.types.size_t
45+
libc.hdr.types.wchar_t
46+
libc.src.__support.wctype_utils
47+
)
48+
3749
add_entrypoint_object(
3850
wcschr
3951
SRCS

libc/src/wchar/wmemset.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//===-- Implementation of wmemset -----------------------------------------===//
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/wmemset.h"
10+
11+
#include "hdr/types/size_t.h"
12+
#include "hdr/types/wchar_t.h"
13+
#include "src/__support/common.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
LLVM_LIBC_FUNCTION(wchar_t *, wmemset, (wchar_t * s, wchar_t c, size_t n)) {
18+
for (size_t i = 0; i < n; i++)
19+
s[i] = c;
20+
21+
return s;
22+
}
23+
24+
} // namespace LIBC_NAMESPACE_DECL

libc/src/wchar/wmemset.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===-- Implementation header for wmemset
2+
//----------------------------------===//
3+
//
4+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5+
// See https://llvm.org/LICENSE.txt for license information.
6+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#ifndef LLVM_LIBC_SRC_WCHAR_WMEMSET_H
11+
#define LLVM_LIBC_SRC_WCHAR_WMEMSET_H
12+
13+
#include "hdr/types/size_t.h"
14+
#include "hdr/types/wchar_t.h"
15+
#include "src/__support/macros/config.h"
16+
17+
namespace LIBC_NAMESPACE_DECL {
18+
19+
wchar_t *wmemset(wchar_t *s, wchar_t c, size_t n);
20+
21+
} // namespace LIBC_NAMESPACE_DECL
22+
23+
#endif // LLVM_LIBC_SRC_WCHAR_WMEMSET_H

libc/test/src/wchar/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ add_libc_test(
3333
libc.src.wchar.wctob
3434
)
3535

36+
add_libc_test(
37+
wmemset_test
38+
SUITE
39+
libc_wchar_unittests
40+
SRCS
41+
wmemset_test.cpp
42+
DEPENDS
43+
libc.hdr.types.size_t
44+
libc.hdr.types.wchar_t
45+
libc.src.wchar.wmemset
46+
)
47+
3648
add_libc_test(
3749
wcschr_test
3850
SUITE

libc/test/src/wchar/wmemset_test.cpp

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
//===-- Unittests for wmemset ---------------------------------------------===//
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/wmemset.h"
12+
#include "test/UnitTest/Test.h"
13+
14+
TEST(LlvmLibcWMemsetTest, SmallStringBoundCheck) {
15+
wchar_t str[5];
16+
for (int i = 0; i < 5; i++)
17+
str[i] = 'A';
18+
19+
wchar_t *output = LIBC_NAMESPACE::wmemset(str + 1, 'B', 3);
20+
21+
EXPECT_EQ(output, str + 1);
22+
23+
// EXPECT_TRUE being used since there isn't currently support for printing
24+
// wide chars in the future, it would be preferred to switch these to
25+
// EXPECT_EQ
26+
EXPECT_TRUE(str[0] == (wchar_t)'A');
27+
EXPECT_TRUE(str[1] == (wchar_t)'B');
28+
EXPECT_TRUE(str[2] == (wchar_t)'B');
29+
EXPECT_TRUE(str[3] == (wchar_t)'B');
30+
EXPECT_TRUE(str[4] == (wchar_t)'A');
31+
}
32+
33+
TEST(LlvmLibcWMemsetTest, LargeStringBoundCheck) {
34+
constexpr int str_size = 1000;
35+
wchar_t str[str_size];
36+
for (int i = 0; i < str_size; i++)
37+
str[i] = 'A';
38+
39+
wchar_t *output = LIBC_NAMESPACE::wmemset(str + 1, 'B', str_size - 2);
40+
41+
EXPECT_EQ(output, str + 1);
42+
43+
EXPECT_TRUE(str[0] == (wchar_t)'A');
44+
for (int i = 1; i < str_size - 1; i++)
45+
EXPECT_TRUE(str[i] == (wchar_t)'B');
46+
47+
EXPECT_TRUE(str[str_size - 1] == (wchar_t)'A');
48+
}
49+
50+
TEST(LlvmLibcWMemsetTest, WCharSizeSmallString) {
51+
// ensure we can handle full range of widechars
52+
wchar_t str[5];
53+
const wchar_t target = WCHAR_MAX;
54+
55+
for (int i = 0; i < 5; i++)
56+
str[i] = 'A';
57+
58+
wchar_t *output = LIBC_NAMESPACE::wmemset(str + 1, target, 3);
59+
60+
EXPECT_EQ(output, str + 1);
61+
62+
EXPECT_TRUE(str[0] == (wchar_t)'A');
63+
EXPECT_TRUE(str[1] == target);
64+
EXPECT_TRUE(str[2] == target);
65+
EXPECT_TRUE(str[3] == target);
66+
EXPECT_TRUE(str[4] == (wchar_t)'A');
67+
}
68+
69+
TEST(LlvmLibcWMemsetTest, WCharSizeLargeString) {
70+
// ensure we can handle full range of widechars
71+
constexpr int str_size = 1000;
72+
wchar_t str[str_size];
73+
74+
const wchar_t target = WCHAR_MAX;
75+
76+
for (int i = 0; i < str_size; i++)
77+
str[i] = 'A';
78+
79+
wchar_t *output = LIBC_NAMESPACE::wmemset(str + 1, target, str_size - 2);
80+
81+
EXPECT_EQ(output, str + 1);
82+
83+
EXPECT_TRUE(str[0] == (wchar_t)'A');
84+
for (int i = 1; i < str_size - 1; i++)
85+
EXPECT_TRUE(str[i] == target);
86+
87+
EXPECT_TRUE(str[str_size - 1] == (wchar_t)'A');
88+
}

0 commit comments

Comments
 (0)