Skip to content

Commit 4441fce

Browse files
author
Sriya Pratipati
committed
Fixed part of the implementation and added more tests.
1 parent 873b716 commit 4441fce

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

libc/src/wchar/wmemcmp.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ namespace LIBC_NAMESPACE_DECL {
1717

1818
LLVM_LIBC_FUNCTION(int, wmemcmp,
1919
(const wchar_t *s1, const wchar_t *s2, size_t n)) {
20-
for (size_t i = 0; i < n; ++s1, ++s2, ++i) {
21-
if (*s1 != *s2)
22-
return (int)(*s1 - *s2);
20+
for (size_t i = 0; i < n; ++i) {
21+
if (s1[i] != s2[i])
22+
return (int)(s1[i] - s2[i]);
2323
}
2424
// If it reaches the end, all n values must be the same.
2525
return 0;

libc/test/src/wchar/wmemcmp_test.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,31 @@ TEST(LlvmLibcWMemcmpTest, LhsAfterRhsLexically) {
3838
const wchar_t *rhs = L"ab";
3939
EXPECT_GT(LIBC_NAMESPACE::wmemcmp(lhs, rhs, 2), 0);
4040
}
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, 2), 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)