| 
 | 1 | +//===-- Unittests for wcsncmp ---------------------------------------------===//  | 
 | 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/wcsncmp.h"  | 
 | 10 | +#include "test/UnitTest/Test.h"  | 
 | 11 | + | 
 | 12 | +// This group is just copies of the wcscmp tests, since all the same cases still  | 
 | 13 | +// need to be tested.  | 
 | 14 | + | 
 | 15 | +TEST(LlvmLibcWcsncmpTest, EmptyStringsShouldReturnZeroWithSufficientLength) {  | 
 | 16 | +  const wchar_t *s1 = L"";  | 
 | 17 | +  const wchar_t *s2 = L"";  | 
 | 18 | +  int result = LIBC_NAMESPACE::wcsncmp(s1, s2, 1);  | 
 | 19 | +  ASSERT_EQ(result, 0);  | 
 | 20 | + | 
 | 21 | +  // Verify operands reversed.  | 
 | 22 | +  result = LIBC_NAMESPACE::wcsncmp(s2, s1, 1);  | 
 | 23 | +  ASSERT_EQ(result, 0);  | 
 | 24 | +}  | 
 | 25 | + | 
 | 26 | +TEST(LlvmLibcWcsncmpTest,  | 
 | 27 | +     EmptyStringShouldNotEqualNonEmptyStringWithSufficientLength) {  | 
 | 28 | +  const wchar_t *empty = L"";  | 
 | 29 | +  const wchar_t *s2 = L"abc";  | 
 | 30 | +  int result = LIBC_NAMESPACE::wcsncmp(empty, s2, 3);  | 
 | 31 | +  ASSERT_LT(result, 0);  | 
 | 32 | + | 
 | 33 | +  // Similar case if empty string is second argument.  | 
 | 34 | +  const wchar_t *s3 = L"123";  | 
 | 35 | +  result = LIBC_NAMESPACE::wcsncmp(s3, empty, 3);  | 
 | 36 | +  ASSERT_GT(result, 0);  | 
 | 37 | +}  | 
 | 38 | + | 
 | 39 | +TEST(LlvmLibcWcsncmpTest, EqualStringsShouldReturnZeroWithSufficientLength) {  | 
 | 40 | +  const wchar_t *s1 = L"abc";  | 
 | 41 | +  const wchar_t *s2 = L"abc";  | 
 | 42 | +  int result = LIBC_NAMESPACE::wcsncmp(s1, s2, 3);  | 
 | 43 | +  ASSERT_EQ(result, 0);  | 
 | 44 | + | 
 | 45 | +  // Verify operands reversed.  | 
 | 46 | +  result = LIBC_NAMESPACE::wcsncmp(s2, s1, 3);  | 
 | 47 | +  ASSERT_EQ(result, 0);  | 
 | 48 | +}  | 
 | 49 | + | 
 | 50 | +TEST(LlvmLibcWcsncmpTest,  | 
 | 51 | +     ShouldReturnResultOfFirstDifferenceWithSufficientLength) {  | 
 | 52 | +  const wchar_t *s1 = L"___B42__";  | 
 | 53 | +  const wchar_t *s2 = L"___C55__";  | 
 | 54 | +  int result = LIBC_NAMESPACE::wcsncmp(s1, s2, 8);  | 
 | 55 | +  ASSERT_LT(result, 0);  | 
 | 56 | + | 
 | 57 | +  // Verify operands reversed.  | 
 | 58 | +  result = LIBC_NAMESPACE::wcsncmp(s2, s1, 8);  | 
 | 59 | +  ASSERT_GT(result, 0);  | 
 | 60 | +}  | 
 | 61 | + | 
 | 62 | +TEST(LlvmLibcWcsncmpTest,  | 
 | 63 | +     CapitalizedLetterShouldNotBeEqualWithSufficientLength) {  | 
 | 64 | +  const wchar_t *s1 = L"abcd";  | 
 | 65 | +  const wchar_t *s2 = L"abCd";  | 
 | 66 | +  int result = LIBC_NAMESPACE::wcsncmp(s1, s2, 4);  | 
 | 67 | +  ASSERT_GT(result, 0);  | 
 | 68 | + | 
 | 69 | +  // Verify operands reversed.  | 
 | 70 | +  result = LIBC_NAMESPACE::wcsncmp(s2, s1, 4);  | 
 | 71 | +  ASSERT_LT(result, 0);  | 
 | 72 | +}  | 
 | 73 | + | 
 | 74 | +TEST(LlvmLibcWcsncmpTest,  | 
 | 75 | +     UnequalLengthStringsShouldNotReturnZeroWithSufficientLength) {  | 
 | 76 | +  const wchar_t *s1 = L"abc";  | 
 | 77 | +  const wchar_t *s2 = L"abcd";  | 
 | 78 | +  int result = LIBC_NAMESPACE::wcsncmp(s1, s2, 4);  | 
 | 79 | +  ASSERT_LT(result, 0);  | 
 | 80 | + | 
 | 81 | +  // Verify operands reversed.  | 
 | 82 | +  result = LIBC_NAMESPACE::wcsncmp(s2, s1, 4);  | 
 | 83 | +  ASSERT_GT(result, 0);  | 
 | 84 | +}  | 
 | 85 | + | 
 | 86 | +TEST(LlvmLibcWcsncmpTest, StringArgumentSwapChangesSignWithSufficientLength) {  | 
 | 87 | +  const wchar_t *a = L"a";  | 
 | 88 | +  const wchar_t *b = L"b";  | 
 | 89 | +  int result = LIBC_NAMESPACE::wcsncmp(b, a, 1);  | 
 | 90 | +  ASSERT_GT(result, 0);  | 
 | 91 | + | 
 | 92 | +  result = LIBC_NAMESPACE::wcsncmp(a, b, 1);  | 
 | 93 | +  ASSERT_LT(result, 0);  | 
 | 94 | +}  | 
 | 95 | + | 
 | 96 | +#if defined(LIBC_ADD_NULL_CHECKS) && !defined(LIBC_HAS_SANITIZER)  | 
 | 97 | +TEST(LlvmLibcWcsncmpTest, NullptrCrash) {  | 
 | 98 | +  // Passing in a nullptr should crash the program.  | 
 | 99 | +  EXPECT_DEATH([] { LIBC_NAMESPACE::wcsncmp(L"aaaaaaaaaaaaaa", nullptr, 3); },  | 
 | 100 | +               WITH_SIGNAL(-1));  | 
 | 101 | +  EXPECT_DEATH([] { LIBC_NAMESPACE::wcsncmp(nullptr, L"aaaaaaaaaaaaaa", 3); },  | 
 | 102 | +               WITH_SIGNAL(-1));  | 
 | 103 | +}  | 
 | 104 | +#endif // LIBC_HAS_ADDRESS_SANITIZER  | 
 | 105 | + | 
 | 106 | +// This group is actually testing wcsncmp functionality  | 
 | 107 | + | 
 | 108 | +TEST(LlvmLibcWcsncmpTest, NonEqualStringsEqualWithLengthZero) {  | 
 | 109 | +  const wchar_t *s1 = L"abc";  | 
 | 110 | +  const wchar_t *s2 = L"def";  | 
 | 111 | +  int result = LIBC_NAMESPACE::wcsncmp(s1, s2, 0);  | 
 | 112 | +  ASSERT_EQ(result, 0);  | 
 | 113 | + | 
 | 114 | +  // Verify operands reversed.  | 
 | 115 | +  result = LIBC_NAMESPACE::wcsncmp(s2, s1, 0);  | 
 | 116 | +  ASSERT_EQ(result, 0);  | 
 | 117 | +}  | 
 | 118 | + | 
 | 119 | +TEST(LlvmLibcWcsncmpTest, NonEqualStringsNotEqualWithLengthOne) {  | 
 | 120 | +  const wchar_t *s1 = L"abc";  | 
 | 121 | +  const wchar_t *s2 = L"def";  | 
 | 122 | +  int result = LIBC_NAMESPACE::wcsncmp(s1, s2, 1);  | 
 | 123 | +  ASSERT_LT(result, 0);  | 
 | 124 | + | 
 | 125 | +  // Verify operands reversed.  | 
 | 126 | +  result = LIBC_NAMESPACE::wcsncmp(s2, s1, 1);  | 
 | 127 | +  ASSERT_GT(result, 0);  | 
 | 128 | +}  | 
 | 129 | + | 
 | 130 | +TEST(LlvmLibcWcsncmpTest, NonEqualStringsEqualWithShorterLength) {  | 
 | 131 | +  const wchar_t *s1 = L"___B42__";  | 
 | 132 | +  const wchar_t *s2 = L"___C55__";  | 
 | 133 | +  int result = LIBC_NAMESPACE::wcsncmp(s1, s2, 3);  | 
 | 134 | +  ASSERT_EQ(result, 0);  | 
 | 135 | + | 
 | 136 | +  // This should return 'B' - 'C' = -1.  | 
 | 137 | +  result = LIBC_NAMESPACE::wcsncmp(s1, s2, 4);  | 
 | 138 | +  ASSERT_LT(result, 0);  | 
 | 139 | + | 
 | 140 | +  // Verify operands reversed.  | 
 | 141 | +  result = LIBC_NAMESPACE::wcsncmp(s2, s1, 3);  | 
 | 142 | +  ASSERT_EQ(result, 0);  | 
 | 143 | + | 
 | 144 | +  // This should return 'C' - 'B' = 1.  | 
 | 145 | +  result = LIBC_NAMESPACE::wcsncmp(s2, s1, 4);  | 
 | 146 | +  ASSERT_GT(result, 0);  | 
 | 147 | +}  | 
 | 148 | + | 
 | 149 | +TEST(LlvmLibcWcsncmpTest, StringComparisonEndsOnNullByteEvenWithLongerLength) {  | 
 | 150 | +  const wchar_t *s1 = L"abc\0def";  | 
 | 151 | +  const wchar_t *s2 = L"abc\0abc";  | 
 | 152 | +  int result = LIBC_NAMESPACE::wcsncmp(s1, s2, 7);  | 
 | 153 | +  ASSERT_EQ(result, 0);  | 
 | 154 | + | 
 | 155 | +  // Verify operands reversed.  | 
 | 156 | +  result = LIBC_NAMESPACE::wcsncmp(s2, s1, 7);  | 
 | 157 | +  ASSERT_EQ(result, 0);  | 
 | 158 | +}  | 
 | 159 | + | 
 | 160 | +TEST(LlvmLibcWcsncmpTest, Case) {  | 
 | 161 | +  const wchar_t *s1 = L"aB";  | 
 | 162 | +  const wchar_t *s2 = L"ab";  | 
 | 163 | +  int result = LIBC_NAMESPACE::wcsncmp(s1, s2, 2);  | 
 | 164 | +  ASSERT_LT(result, 0);  | 
 | 165 | + | 
 | 166 | +  // Verify operands reversed.  | 
 | 167 | +  result = LIBC_NAMESPACE::wcsncmp(s2, s1, 2);  | 
 | 168 | +  ASSERT_GT(result, 0);  | 
 | 169 | +}  | 
0 commit comments