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

source code of libc/test/src/string/strncmp_test.cpp