Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions libc/include/wchar.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions libc/src/wchar/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ add_entrypoint_object(
libc.src.__support.wctype_utils
)

add_entrypoint_object(
wcscmp
SRCS
wcscmp.cpp
HDRS
wcscmp.h
DEPENDS
libc.hdr.wchar_macros
)

add_entrypoint_object(
wcspbrk
SRCS
Expand Down
30 changes: 30 additions & 0 deletions libc/src/wchar/wcscmp.cpp
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions libc/src/wchar/wcscmp.h
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions libc/test/src/wchar/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
97 changes: 97 additions & 0 deletions libc/test/src/wchar/wcscmp_test.cpp
Original file line number Diff line number Diff line change
@@ -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
Loading