-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[libc] Implemented wcscmp #142423
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[libc] Implemented wcscmp #142423
Conversation
@llvm/pr-subscribers-libc Author: Uzair Nawaz (uzairnawaz) ChangesImplemented wcscmp and added tests Full diff: https://github.com/llvm/llvm-project/pull/142423.diff 7 Files Affected:
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 545b9227349fe..9c76067a9a042 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -366,6 +366,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.wchar.wctob
libc.src.wchar.wmemset
libc.src.wchar.wcschr
+ libc.src.wchar.wcscmp
libc.src.wchar.wcspbrk
libc.src.wchar.wcsspn
libc.src.wchar.wmemcmp
diff --git a/libc/include/wchar.yaml b/libc/include/wchar.yaml
index bfd9a10342019..a6be5bedf7a1e 100644
--- a/libc/include/wchar.yaml
+++ b/libc/include/wchar.yaml
@@ -42,6 +42,13 @@ functions:
arguments:
- type: const wchar_t *
- type: wchar_t
+ - name: wcscmp
+ standards:
+ - stdc
+ return_type: int
+ arguments:
+ - type: const wchar_t *
+ - type: const wchar_t *
- name: wcspbrk
standards:
- stdc
diff --git a/libc/src/wchar/CMakeLists.txt b/libc/src/wchar/CMakeLists.txt
index 9db121762348b..420a8a277c4a0 100644
--- a/libc/src/wchar/CMakeLists.txt
+++ b/libc/src/wchar/CMakeLists.txt
@@ -57,6 +57,17 @@ add_entrypoint_object(
libc.src.__support.wctype_utils
)
+add_entrypoint_object(
+ wcscmp
+ SRCS
+ wcscmp.cpp
+ HDRS
+ wcscmp.h
+ DEPENDS
+ libc.hdr.wchar_macros
+ libc.src.__support.wctype_utils
+)
+
add_entrypoint_object(
wcspbrk
SRCS
diff --git a/libc/src/wchar/wcscmp.cpp b/libc/src/wchar/wcscmp.cpp
new file mode 100644
index 0000000000000..f285efd905390
--- /dev/null
+++ b/libc/src/wchar/wcscmp.cpp
@@ -0,0 +1,30 @@
+//===-- Implementation of wcscmp ------------------------------------------===//
+//
+// 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 "src/wchar/wcscmp.h"
+
+#include "hdr/types/size_t.h"
+#include "hdr/types/wchar_t.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/null_check.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, wcscmp, (const wchar_t *left, const wchar_t *right)) {
+ LIBC_CRASH_ON_NULLPTR(left);
+ LIBC_CRASH_ON_NULLPTR(right);
+
+ auto comp = [](wchar_t l, wchar_t r) -> int { return l - r; };
+
+ for (; *left && !comp(*left, *right); ++left, ++right)
+ ;
+
+ return comp(*left, *right);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/wchar/wcscmp.h b/libc/src/wchar/wcscmp.h
new file mode 100644
index 0000000000000..af82d063f017f
--- /dev/null
+++ b/libc/src/wchar/wcscmp.h
@@ -0,0 +1,22 @@
+//===-- Implementation header for wcscmp ----------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_WCHAR_WCSCMP_H
+#define LLVM_LIBC_SRC_WCHAR_WCSCMP_H
+
+#include "hdr/types/size_t.h"
+#include "hdr/types/wchar_t.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int wcscmp(const wchar_t *left, const wchar_t *right);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_WCHAR_WCSCMP_H
diff --git a/libc/test/src/wchar/CMakeLists.txt b/libc/test/src/wchar/CMakeLists.txt
index 9bc230e0bddf3..9a3585554a0db 100644
--- a/libc/test/src/wchar/CMakeLists.txt
+++ b/libc/test/src/wchar/CMakeLists.txt
@@ -55,6 +55,16 @@ add_libc_test(
libc.src.wchar.wcschr
)
+add_libc_test(
+ wcscmp_test
+ SUITE
+ libc_wchar_unittests
+ SRCS
+ wcscmp_test.cpp
+ DEPENDS
+ libc.src.wchar.wcscmp
+)
+
add_libc_test(
wcspbrk_test
SUITE
diff --git a/libc/test/src/wchar/wcscmp_test.cpp b/libc/test/src/wchar/wcscmp_test.cpp
new file mode 100644
index 0000000000000..6572aadb066ae
--- /dev/null
+++ b/libc/test/src/wchar/wcscmp_test.cpp
@@ -0,0 +1,97 @@
+//===-- Unittests for wcscmp ----------------------------------------------===//
+//
+// 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 "src/wchar/wcscmp.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcWcscmpTest, EmptyStringsShouldReturnZero) {
+ const wchar_t *s1 = L"";
+ const wchar_t *s2 = L"";
+ int result = LIBC_NAMESPACE::wcscmp(s1, s2);
+ ASSERT_EQ(result, 0);
+
+ // Verify operands reversed.
+ result = LIBC_NAMESPACE::wcscmp(s2, s1);
+ ASSERT_EQ(result, 0);
+}
+
+TEST(LlvmLibcWcscmpTest, EmptyStringShouldNotEqualNonEmptyString) {
+ const wchar_t *empty = L"";
+ const wchar_t *s2 = L"abc";
+ int result = LIBC_NAMESPACE::wcscmp(empty, s2);
+ ASSERT_LT(result, 0);
+
+ // Similar case if empty string is second argument.
+ const wchar_t *s3 = L"123";
+ result = LIBC_NAMESPACE::wcscmp(s3, empty);
+ ASSERT_GT(result, 0);
+}
+
+TEST(LlvmLibcWcscmpTest, EqualStringsShouldReturnZero) {
+ const wchar_t *s1 = L"abc";
+ const wchar_t *s2 = L"abc";
+ int result = LIBC_NAMESPACE::wcscmp(s1, s2);
+ ASSERT_EQ(result, 0);
+
+ // Verify operands reversed.
+ result = LIBC_NAMESPACE::wcscmp(s2, s1);
+ ASSERT_EQ(result, 0);
+}
+
+TEST(LlvmLibcWcscmpTest, ShouldReturnResultOfFirstDifference) {
+ const wchar_t *s1 = L"___B42__";
+ const wchar_t *s2 = L"___C55__";
+ int result = LIBC_NAMESPACE::wcscmp(s1, s2);
+ ASSERT_LT(result, 0);
+
+ // Verify operands reversed.
+ result = LIBC_NAMESPACE::wcscmp(s2, s1);
+ ASSERT_GT(result, 0);
+}
+
+TEST(LlvmLibcWcscmpTest, CapitalizedLetterShouldNotBeEqual) {
+ const wchar_t *s1 = L"abcd";
+ const wchar_t *s2 = L"abCd";
+ int result = LIBC_NAMESPACE::wcscmp(s1, s2);
+ ASSERT_GT(result, 0);
+
+ // Verify operands reversed.
+ result = LIBC_NAMESPACE::wcscmp(s2, s1);
+ ASSERT_LT(result, 0);
+}
+
+TEST(LlvmLibcWcscmpTest, UnequalLengthStringsShouldNotReturnZero) {
+ const wchar_t *s1 = L"abc";
+ const wchar_t *s2 = L"abcd";
+ int result = LIBC_NAMESPACE::wcscmp(s1, s2);
+ ASSERT_LT(result, 0);
+
+ // Verify operands reversed.
+ result = LIBC_NAMESPACE::wcscmp(s2, s1);
+ ASSERT_GT(result, 0);
+}
+
+TEST(LlvmLibcWcscmpTest, StringArgumentSwapChangesSign) {
+ const wchar_t *a = L"a";
+ const wchar_t *b = L"b";
+ int result = LIBC_NAMESPACE::wcscmp(b, a);
+ ASSERT_GT(result, 0);
+
+ result = LIBC_NAMESPACE::wcscmp(a, b);
+ ASSERT_LT(result, 0);
+}
+
+#if defined(LIBC_ADD_NULL_CHECKS) && !defined(LIBC_HAS_SANITIZER)
+TEST(LlvmLibcWcscmpTest, NullptrCrash) {
+ // Passing in a nullptr should crash the program.
+ EXPECT_DEATH([] { LIBC_NAMESPACE::wcscmp(L"aaaaaaaaaaaaaa", nullptr); },
+ WITH_SIGNAL(-1));
+ EXPECT_DEATH([] { LIBC_NAMESPACE::wcscmp(nullptr, L"aaaaaaaaaaaaaa"); },
+ WITH_SIGNAL(-1));
+}
+#endif // LIBC_HAS_ADDRESS_SANITIZER
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM with a build fix
libc/src/wchar/CMakeLists.txt
Outdated
wcscmp.h | ||
DEPENDS | ||
libc.hdr.wchar_macros | ||
libc.src.__support.wctype_utils |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wctype_utils
isn't used, fix dependency
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Implemented wcscmp and added tests
Implemented wcscmp and added tests