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