Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libc / test / src / string / strncmp_test.cpp
blob1724a5436d1ea336a036400452009b2419c4f9d7
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 //===----------------------------------------------------------------------===//
9 #include "src/string/strncmp.h"
10 #include "test/UnitTest/Test.h"
12 // This group is just copies of the strcmp tests, since all the same cases still
13 // need to be tested.
15 TEST(LlvmLibcStrNCmpTest, EmptyStringsShouldReturnZeroWithSufficientLength) {
16 const char *s1 = "";
17 const char *s2 = "";
18 int result = LIBC_NAMESPACE::strncmp(s1, s2, 1);
19 ASSERT_EQ(result, 0);
21 // Verify operands reversed.
22 result = LIBC_NAMESPACE::strncmp(s2, s1, 1);
23 ASSERT_EQ(result, 0);
26 TEST(LlvmLibcStrNCmpTest,
27 EmptyStringShouldNotEqualNonEmptyStringWithSufficientLength) {
28 const char *empty = "";
29 const char *s2 = "abc";
30 int result = LIBC_NAMESPACE::strncmp(empty, s2, 3);
31 // This should be '\0' - 'a' = -97
32 ASSERT_EQ(result, -97);
34 // Similar case if empty string is second argument.
35 const char *s3 = "123";
36 result = LIBC_NAMESPACE::strncmp(s3, empty, 3);
37 // This should be '1' - '\0' = 49
38 ASSERT_EQ(result, 49);
41 TEST(LlvmLibcStrNCmpTest, EqualStringsShouldReturnZeroWithSufficientLength) {
42 const char *s1 = "abc";
43 const char *s2 = "abc";
44 int result = LIBC_NAMESPACE::strncmp(s1, s2, 3);
45 ASSERT_EQ(result, 0);
47 // Verify operands reversed.
48 result = LIBC_NAMESPACE::strncmp(s2, s1, 3);
49 ASSERT_EQ(result, 0);
52 TEST(LlvmLibcStrNCmpTest,
53 ShouldReturnResultOfFirstDifferenceWithSufficientLength) {
54 const char *s1 = "___B42__";
55 const char *s2 = "___C55__";
56 int result = LIBC_NAMESPACE::strncmp(s1, s2, 8);
57 // This should return 'B' - 'C' = -1.
58 ASSERT_EQ(result, -1);
60 // Verify operands reversed.
61 result = LIBC_NAMESPACE::strncmp(s2, s1, 8);
62 // This should return 'C' - 'B' = 1.
63 ASSERT_EQ(result, 1);
66 TEST(LlvmLibcStrNCmpTest,
67 CapitalizedLetterShouldNotBeEqualWithSufficientLength) {
68 const char *s1 = "abcd";
69 const char *s2 = "abCd";
70 int result = LIBC_NAMESPACE::strncmp(s1, s2, 4);
71 // 'c' - 'C' = 32.
72 ASSERT_EQ(result, 32);
74 // Verify operands reversed.
75 result = LIBC_NAMESPACE::strncmp(s2, s1, 4);
76 // 'C' - 'c' = -32.
77 ASSERT_EQ(result, -32);
80 TEST(LlvmLibcStrNCmpTest,
81 UnequalLengthStringsShouldNotReturnZeroWithSufficientLength) {
82 const char *s1 = "abc";
83 const char *s2 = "abcd";
84 int result = LIBC_NAMESPACE::strncmp(s1, s2, 4);
85 // '\0' - 'd' = -100.
86 ASSERT_EQ(result, -100);
88 // Verify operands reversed.
89 result = LIBC_NAMESPACE::strncmp(s2, s1, 4);
90 // 'd' - '\0' = 100.
91 ASSERT_EQ(result, 100);
94 TEST(LlvmLibcStrNCmpTest, StringArgumentSwapChangesSignWithSufficientLength) {
95 const char *a = "a";
96 const char *b = "b";
97 int result = LIBC_NAMESPACE::strncmp(b, a, 1);
98 // 'b' - 'a' = 1.
99 ASSERT_EQ(result, 1);
101 result = LIBC_NAMESPACE::strncmp(a, b, 1);
102 // 'a' - 'b' = -1.
103 ASSERT_EQ(result, -1);
106 // This group is actually testing strncmp functionality
108 TEST(LlvmLibcStrNCmpTest, NonEqualStringsEqualWithLengthZero) {
109 const char *s1 = "abc";
110 const char *s2 = "def";
111 int result = LIBC_NAMESPACE::strncmp(s1, s2, 0);
112 ASSERT_EQ(result, 0);
114 // Verify operands reversed.
115 result = LIBC_NAMESPACE::strncmp(s2, s1, 0);
116 ASSERT_EQ(result, 0);
119 TEST(LlvmLibcStrNCmpTest, NonEqualStringsNotEqualWithLengthOne) {
120 const char *s1 = "abc";
121 const char *s2 = "def";
122 int result = LIBC_NAMESPACE::strncmp(s1, s2, 1);
123 ASSERT_EQ(result, -3);
125 // Verify operands reversed.
126 result = LIBC_NAMESPACE::strncmp(s2, s1, 1);
127 ASSERT_EQ(result, 3);
130 TEST(LlvmLibcStrNCmpTest, NonEqualStringsEqualWithShorterLength) {
131 const char *s1 = "___B42__";
132 const char *s2 = "___C55__";
133 int result = LIBC_NAMESPACE::strncmp(s1, s2, 3);
134 ASSERT_EQ(result, 0);
136 // This should return 'B' - 'C' = -1.
137 result = LIBC_NAMESPACE::strncmp(s1, s2, 4);
138 ASSERT_EQ(result, -1);
140 // Verify operands reversed.
141 result = LIBC_NAMESPACE::strncmp(s2, s1, 3);
142 ASSERT_EQ(result, 0);
144 // This should return 'C' - 'B' = 1.
145 result = LIBC_NAMESPACE::strncmp(s2, s1, 4);
146 ASSERT_EQ(result, 1);
149 TEST(LlvmLibcStrNCmpTest, StringComparisonEndsOnNullByteEvenWithLongerLength) {
150 const char *s1 = "abc\0def";
151 const char *s2 = "abc\0abc";
152 int result = LIBC_NAMESPACE::strncmp(s1, s2, 7);
153 ASSERT_EQ(result, 0);
155 // Verify operands reversed.
156 result = LIBC_NAMESPACE::strncmp(s2, s1, 7);
157 ASSERT_EQ(result, 0);
160 TEST(LlvmLibcStrNCmpTest, Case) {
161 const char *s1 = "aB";
162 const char *s2 = "ab";
163 int result = LIBC_NAMESPACE::strncmp(s1, s2, 2);
164 ASSERT_LT(result, 0);
166 // Verify operands reversed.
167 result = LIBC_NAMESPACE::strncmp(s2, s1, 2);
168 ASSERT_GT(result, 0);