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