|
| 1 | +//===-- Unittests for wcscmp ----------------------------------------------===// |
| 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/wcscmp.h" |
| 10 | +#include "test/UnitTest/Test.h" |
| 11 | + |
| 12 | +TEST(LlvmLibcWcscmpTest, EmptyStringsShouldReturnZero) { |
| 13 | + const wchar_t *s1 = L""; |
| 14 | + const wchar_t *s2 = L""; |
| 15 | + int result = LIBC_NAMESPACE::wcscmp(s1, s2); |
| 16 | + ASSERT_EQ(result, 0); |
| 17 | + |
| 18 | + // Verify operands reversed. |
| 19 | + result = LIBC_NAMESPACE::wcscmp(s2, s1); |
| 20 | + ASSERT_EQ(result, 0); |
| 21 | +} |
| 22 | + |
| 23 | +TEST(LlvmLibcWcscmpTest, EmptyStringShouldNotEqualNonEmptyString) { |
| 24 | + const wchar_t *empty = L""; |
| 25 | + const wchar_t *s2 = L"abc"; |
| 26 | + int result = LIBC_NAMESPACE::wcscmp(empty, s2); |
| 27 | + ASSERT_LT(result, 0); |
| 28 | + |
| 29 | + // Similar case if empty string is second argument. |
| 30 | + const wchar_t *s3 = L"123"; |
| 31 | + result = LIBC_NAMESPACE::wcscmp(s3, empty); |
| 32 | + ASSERT_GT(result, 0); |
| 33 | +} |
| 34 | + |
| 35 | +TEST(LlvmLibcWcscmpTest, EqualStringsShouldReturnZero) { |
| 36 | + const wchar_t *s1 = L"abc"; |
| 37 | + const wchar_t *s2 = L"abc"; |
| 38 | + int result = LIBC_NAMESPACE::wcscmp(s1, s2); |
| 39 | + ASSERT_EQ(result, 0); |
| 40 | + |
| 41 | + // Verify operands reversed. |
| 42 | + result = LIBC_NAMESPACE::wcscmp(s2, s1); |
| 43 | + ASSERT_EQ(result, 0); |
| 44 | +} |
| 45 | + |
| 46 | +TEST(LlvmLibcWcscmpTest, ShouldReturnResultOfFirstDifference) { |
| 47 | + const wchar_t *s1 = L"___B42__"; |
| 48 | + const wchar_t *s2 = L"___C55__"; |
| 49 | + int result = LIBC_NAMESPACE::wcscmp(s1, s2); |
| 50 | + ASSERT_LT(result, 0); |
| 51 | + |
| 52 | + // Verify operands reversed. |
| 53 | + result = LIBC_NAMESPACE::wcscmp(s2, s1); |
| 54 | + ASSERT_GT(result, 0); |
| 55 | +} |
| 56 | + |
| 57 | +TEST(LlvmLibcWcscmpTest, CapitalizedLetterShouldNotBeEqual) { |
| 58 | + const wchar_t *s1 = L"abcd"; |
| 59 | + const wchar_t *s2 = L"abCd"; |
| 60 | + int result = LIBC_NAMESPACE::wcscmp(s1, s2); |
| 61 | + ASSERT_GT(result, 0); |
| 62 | + |
| 63 | + // Verify operands reversed. |
| 64 | + result = LIBC_NAMESPACE::wcscmp(s2, s1); |
| 65 | + ASSERT_LT(result, 0); |
| 66 | +} |
| 67 | + |
| 68 | +TEST(LlvmLibcWcscmpTest, UnequalLengthStringsShouldNotReturnZero) { |
| 69 | + const wchar_t *s1 = L"abc"; |
| 70 | + const wchar_t *s2 = L"abcd"; |
| 71 | + int result = LIBC_NAMESPACE::wcscmp(s1, s2); |
| 72 | + ASSERT_LT(result, 0); |
| 73 | + |
| 74 | + // Verify operands reversed. |
| 75 | + result = LIBC_NAMESPACE::wcscmp(s2, s1); |
| 76 | + ASSERT_GT(result, 0); |
| 77 | +} |
| 78 | + |
| 79 | +TEST(LlvmLibcWcscmpTest, StringArgumentSwapChangesSign) { |
| 80 | + const wchar_t *a = L"a"; |
| 81 | + const wchar_t *b = L"b"; |
| 82 | + int result = LIBC_NAMESPACE::wcscmp(b, a); |
| 83 | + ASSERT_GT(result, 0); |
| 84 | + |
| 85 | + result = LIBC_NAMESPACE::wcscmp(a, b); |
| 86 | + ASSERT_LT(result, 0); |
| 87 | +} |
| 88 | + |
| 89 | +#if defined(LIBC_ADD_NULL_CHECKS) && !defined(LIBC_HAS_SANITIZER) |
| 90 | +TEST(LlvmLibcWcscmpTest, NullptrCrash) { |
| 91 | + // Passing in a nullptr should crash the program. |
| 92 | + EXPECT_DEATH([] { LIBC_NAMESPACE::wcscmp(L"aaaaaaaaaaaaaa", nullptr); }, |
| 93 | + WITH_SIGNAL(-1)); |
| 94 | + EXPECT_DEATH([] { LIBC_NAMESPACE::wcscmp(nullptr, L"aaaaaaaaaaaaaa"); }, |
| 95 | + WITH_SIGNAL(-1)); |
| 96 | +} |
| 97 | +#endif // LIBC_HAS_ADDRESS_SANITIZER |
0 commit comments