[libc-commits] [libc] [libc] Implemented wmemset and added tests (PR #141691)

Michael Jones via libc-commits libc-commits at lists.llvm.org
Wed May 28 13:36:05 PDT 2025


================
@@ -0,0 +1,87 @@
+//===-- Unittests for wmemset ---------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "hdr/types/size_t.h"
+#include "hdr/types/wchar_t.h"
+#include "src/wchar/wmemset.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcWMemsetTest, SmallStringBoundCheck) {
+  wchar_t *str = new wchar_t[5];
+  for (int i = 0; i < 5; i++) {
+    str[i] = 'A';
+  }
+
+  wchar_t *output = LIBC_NAMESPACE::wmemset(str + 1, 'B', 3);
+
+  EXPECT_EQ(output, str + 1);
+
+  EXPECT_TRUE(str[0] == (wchar_t)'A');
+  EXPECT_TRUE(str[1] == (wchar_t)'B');
+  EXPECT_TRUE(str[2] == (wchar_t)'B');
+  EXPECT_TRUE(str[3] == (wchar_t)'B');
+  EXPECT_TRUE(str[4] == (wchar_t)'A');
+}
+
+TEST(LlvmLibcWMemsetTest, LargeStringBoundCheck) {
+  const int str_size = 1000;
+  wchar_t *str = new wchar_t[str_size];
+  for (int i = 0; i < str_size; i++) {
+    str[i] = 'A';
+  }
+
+  wchar_t *output = LIBC_NAMESPACE::wmemset(str + 1, 'B', str_size - 2);
+
+  EXPECT_EQ(output, str + 1);
+
+  EXPECT_TRUE(str[0] == (wchar_t)'A');
+  for (int i = 1; i < str_size - 1; i++) {
+    EXPECT_TRUE(str[i] == (wchar_t)'B');
+  }
+  EXPECT_TRUE(str[str_size - 1] == (wchar_t)'A');
+}
+
+TEST(LlvmLibcWMemsetTest, WChar_Size_Small) {
+  // ensure we can handle 32 bit values
+  wchar_t *str = new wchar_t[5];
+  const wchar_t magic = INT32_MAX;
----------------
michaelrj-google wrote:

it's not necessarily safe to assume `wchar_t` is 32 bits. Instead it's better to use the provided `WCHAR_MAX` macro.

It would also be good to give `magic` a more descriptive name.
```suggestion
  const wchar_t magic = WCHAR_MAX;
```

https://github.com/llvm/llvm-project/pull/141691


More information about the libc-commits mailing list