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