|
11 | 11 | #include <linux/slab.h> |
12 | 12 | #include <linux/string.h> |
13 | 13 |
|
| 14 | +#define STRCMP_LARGE_BUF_LEN 2048 |
| 15 | +#define STRCMP_CHANGE_POINT 1337 |
| 16 | +#define STRCMP_TEST_EXPECT_EQUAL(test, fn, ...) KUNIT_EXPECT_EQ(test, fn(__VA_ARGS__), 0) |
| 17 | +#define STRCMP_TEST_EXPECT_LOWER(test, fn, ...) KUNIT_EXPECT_LT(test, fn(__VA_ARGS__), 0) |
| 18 | +#define STRCMP_TEST_EXPECT_GREATER(test, fn, ...) KUNIT_EXPECT_GT(test, fn(__VA_ARGS__), 0) |
| 19 | + |
14 | 20 | static void test_memset16(struct kunit *test) |
15 | 21 | { |
16 | 22 | unsigned i, j, k; |
@@ -179,13 +185,162 @@ static void test_strspn(struct kunit *test) |
179 | 185 | } |
180 | 186 | } |
181 | 187 |
|
| 188 | +static char strcmp_buffer1[STRCMP_LARGE_BUF_LEN]; |
| 189 | +static char strcmp_buffer2[STRCMP_LARGE_BUF_LEN]; |
| 190 | + |
| 191 | +static void strcmp_fill_buffers(char fill1, char fill2) |
| 192 | +{ |
| 193 | + memset(strcmp_buffer1, fill1, STRCMP_LARGE_BUF_LEN); |
| 194 | + memset(strcmp_buffer2, fill2, STRCMP_LARGE_BUF_LEN); |
| 195 | + strcmp_buffer1[STRCMP_LARGE_BUF_LEN - 1] = 0; |
| 196 | + strcmp_buffer2[STRCMP_LARGE_BUF_LEN - 1] = 0; |
| 197 | +} |
| 198 | + |
| 199 | +static void test_strcmp(struct kunit *test) |
| 200 | +{ |
| 201 | + /* Equal strings */ |
| 202 | + STRCMP_TEST_EXPECT_EQUAL(test, strcmp, "Hello, Kernel!", "Hello, Kernel!"); |
| 203 | + /* First string is lexicographically less than the second */ |
| 204 | + STRCMP_TEST_EXPECT_LOWER(test, strcmp, "Hello, KUnit!", "Hello, Kernel!"); |
| 205 | + /* First string is lexicographically larger than the second */ |
| 206 | + STRCMP_TEST_EXPECT_GREATER(test, strcmp, "Hello, Kernel!", "Hello, KUnit!"); |
| 207 | + /* Empty string is always lexicographically less than any non-empty string */ |
| 208 | + STRCMP_TEST_EXPECT_LOWER(test, strcmp, "", "Non-empty string"); |
| 209 | + /* Two empty strings should be equal */ |
| 210 | + STRCMP_TEST_EXPECT_EQUAL(test, strcmp, "", ""); |
| 211 | + /* Compare two strings which have only one char difference */ |
| 212 | + STRCMP_TEST_EXPECT_LOWER(test, strcmp, "Abacaba", "Abadaba"); |
| 213 | + /* Compare two strings which have the same prefix*/ |
| 214 | + STRCMP_TEST_EXPECT_LOWER(test, strcmp, "Just a string", "Just a string and something else"); |
| 215 | +} |
| 216 | + |
| 217 | +static void test_strcmp_long_strings(struct kunit *test) |
| 218 | +{ |
| 219 | + strcmp_fill_buffers('B', 'B'); |
| 220 | + STRCMP_TEST_EXPECT_EQUAL(test, strcmp, strcmp_buffer1, strcmp_buffer2); |
| 221 | + |
| 222 | + strcmp_buffer1[STRCMP_CHANGE_POINT] = 'A'; |
| 223 | + STRCMP_TEST_EXPECT_LOWER(test, strcmp, strcmp_buffer1, strcmp_buffer2); |
| 224 | + |
| 225 | + strcmp_buffer1[STRCMP_CHANGE_POINT] = 'C'; |
| 226 | + STRCMP_TEST_EXPECT_GREATER(test, strcmp, strcmp_buffer1, strcmp_buffer2); |
| 227 | +} |
| 228 | + |
| 229 | +static void test_strncmp(struct kunit *test) |
| 230 | +{ |
| 231 | + /* Equal strings */ |
| 232 | + STRCMP_TEST_EXPECT_EQUAL(test, strncmp, "Hello, KUnit!", "Hello, KUnit!", 13); |
| 233 | + /* First string is lexicographically less than the second */ |
| 234 | + STRCMP_TEST_EXPECT_LOWER(test, strncmp, "Hello, KUnit!", "Hello, Kernel!", 13); |
| 235 | + /* Result is always 'equal' when count = 0 */ |
| 236 | + STRCMP_TEST_EXPECT_EQUAL(test, strncmp, "Hello, Kernel!", "Hello, KUnit!", 0); |
| 237 | + /* Strings with common prefix are equal if count = length of prefix */ |
| 238 | + STRCMP_TEST_EXPECT_EQUAL(test, strncmp, "Abacaba", "Abadaba", 3); |
| 239 | + /* Strings with common prefix are not equal when count = length of prefix + 1 */ |
| 240 | + STRCMP_TEST_EXPECT_LOWER(test, strncmp, "Abacaba", "Abadaba", 4); |
| 241 | + /* If one string is a prefix of another, the shorter string is lexicographically smaller */ |
| 242 | + STRCMP_TEST_EXPECT_LOWER(test, strncmp, "Just a string", "Just a string and something else", |
| 243 | + strlen("Just a string and something else")); |
| 244 | + /* |
| 245 | + * If one string is a prefix of another, and we check first length |
| 246 | + * of prefix chars, the result is 'equal' |
| 247 | + */ |
| 248 | + STRCMP_TEST_EXPECT_EQUAL(test, strncmp, "Just a string", "Just a string and something else", |
| 249 | + strlen("Just a string")); |
| 250 | +} |
| 251 | + |
| 252 | +static void test_strncmp_long_strings(struct kunit *test) |
| 253 | +{ |
| 254 | + strcmp_fill_buffers('B', 'B'); |
| 255 | + STRCMP_TEST_EXPECT_EQUAL(test, strncmp, strcmp_buffer1, |
| 256 | + strcmp_buffer2, STRCMP_LARGE_BUF_LEN); |
| 257 | + |
| 258 | + strcmp_buffer1[STRCMP_CHANGE_POINT] = 'A'; |
| 259 | + STRCMP_TEST_EXPECT_LOWER(test, strncmp, strcmp_buffer1, |
| 260 | + strcmp_buffer2, STRCMP_LARGE_BUF_LEN); |
| 261 | + |
| 262 | + strcmp_buffer1[STRCMP_CHANGE_POINT] = 'C'; |
| 263 | + STRCMP_TEST_EXPECT_GREATER(test, strncmp, strcmp_buffer1, |
| 264 | + strcmp_buffer2, STRCMP_LARGE_BUF_LEN); |
| 265 | + /* the strings are equal up to STRCMP_CHANGE_POINT */ |
| 266 | + STRCMP_TEST_EXPECT_EQUAL(test, strncmp, strcmp_buffer1, |
| 267 | + strcmp_buffer2, STRCMP_CHANGE_POINT); |
| 268 | + STRCMP_TEST_EXPECT_GREATER(test, strncmp, strcmp_buffer1, |
| 269 | + strcmp_buffer2, STRCMP_CHANGE_POINT + 1); |
| 270 | +} |
| 271 | + |
| 272 | +static void test_strcasecmp(struct kunit *test) |
| 273 | +{ |
| 274 | + /* Same strings in different case should be equal */ |
| 275 | + STRCMP_TEST_EXPECT_EQUAL(test, strcasecmp, "Hello, Kernel!", "HeLLO, KErNeL!"); |
| 276 | + /* Empty strings should be equal */ |
| 277 | + STRCMP_TEST_EXPECT_EQUAL(test, strcasecmp, "", ""); |
| 278 | + /* Despite ascii code for 'a' is larger than ascii code for 'B', 'a' < 'B' */ |
| 279 | + STRCMP_TEST_EXPECT_LOWER(test, strcasecmp, "a", "B"); |
| 280 | + STRCMP_TEST_EXPECT_GREATER(test, strcasecmp, "B", "a"); |
| 281 | + /* Special symbols and numbers should be processed correctly */ |
| 282 | + STRCMP_TEST_EXPECT_EQUAL(test, strcasecmp, "-+**.1230ghTTT~^", "-+**.1230Ghttt~^"); |
| 283 | +} |
| 284 | + |
| 285 | +static void test_strcasecmp_long_strings(struct kunit *test) |
| 286 | +{ |
| 287 | + strcmp_fill_buffers('b', 'B'); |
| 288 | + STRCMP_TEST_EXPECT_EQUAL(test, strcasecmp, strcmp_buffer1, strcmp_buffer2); |
| 289 | + |
| 290 | + strcmp_buffer1[STRCMP_CHANGE_POINT] = 'a'; |
| 291 | + STRCMP_TEST_EXPECT_LOWER(test, strcasecmp, strcmp_buffer1, strcmp_buffer2); |
| 292 | + |
| 293 | + strcmp_buffer1[STRCMP_CHANGE_POINT] = 'C'; |
| 294 | + STRCMP_TEST_EXPECT_GREATER(test, strcasecmp, strcmp_buffer1, strcmp_buffer2); |
| 295 | +} |
| 296 | + |
| 297 | +static void test_strncasecmp(struct kunit *test) |
| 298 | +{ |
| 299 | + /* Same strings in different case should be equal */ |
| 300 | + STRCMP_TEST_EXPECT_EQUAL(test, strncasecmp, "AbAcAbA", "Abacaba", strlen("Abacaba")); |
| 301 | + /* strncasecmp should check 'count' chars only */ |
| 302 | + STRCMP_TEST_EXPECT_EQUAL(test, strncasecmp, "AbaCaBa", "abaCaDa", 5); |
| 303 | + STRCMP_TEST_EXPECT_LOWER(test, strncasecmp, "a", "B", 1); |
| 304 | + STRCMP_TEST_EXPECT_GREATER(test, strncasecmp, "B", "a", 1); |
| 305 | + /* Result is always 'equal' when count = 0 */ |
| 306 | + STRCMP_TEST_EXPECT_EQUAL(test, strncasecmp, "Abacaba", "Not abacaba", 0); |
| 307 | +} |
| 308 | + |
| 309 | +static void test_strncasecmp_long_strings(struct kunit *test) |
| 310 | +{ |
| 311 | + strcmp_fill_buffers('b', 'B'); |
| 312 | + STRCMP_TEST_EXPECT_EQUAL(test, strncasecmp, strcmp_buffer1, |
| 313 | + strcmp_buffer2, STRCMP_LARGE_BUF_LEN); |
| 314 | + |
| 315 | + strcmp_buffer1[STRCMP_CHANGE_POINT] = 'a'; |
| 316 | + STRCMP_TEST_EXPECT_LOWER(test, strncasecmp, strcmp_buffer1, |
| 317 | + strcmp_buffer2, STRCMP_LARGE_BUF_LEN); |
| 318 | + |
| 319 | + strcmp_buffer1[STRCMP_CHANGE_POINT] = 'C'; |
| 320 | + STRCMP_TEST_EXPECT_GREATER(test, strncasecmp, strcmp_buffer1, |
| 321 | + strcmp_buffer2, STRCMP_LARGE_BUF_LEN); |
| 322 | + |
| 323 | + STRCMP_TEST_EXPECT_EQUAL(test, strncasecmp, strcmp_buffer1, |
| 324 | + strcmp_buffer2, STRCMP_CHANGE_POINT); |
| 325 | + STRCMP_TEST_EXPECT_GREATER(test, strncasecmp, strcmp_buffer1, |
| 326 | + strcmp_buffer2, STRCMP_CHANGE_POINT + 1); |
| 327 | +} |
| 328 | + |
182 | 329 | static struct kunit_case string_test_cases[] = { |
183 | 330 | KUNIT_CASE(test_memset16), |
184 | 331 | KUNIT_CASE(test_memset32), |
185 | 332 | KUNIT_CASE(test_memset64), |
186 | 333 | KUNIT_CASE(test_strchr), |
187 | 334 | KUNIT_CASE(test_strnchr), |
188 | 335 | KUNIT_CASE(test_strspn), |
| 336 | + KUNIT_CASE(test_strcmp), |
| 337 | + KUNIT_CASE(test_strcmp_long_strings), |
| 338 | + KUNIT_CASE(test_strncmp), |
| 339 | + KUNIT_CASE(test_strncmp_long_strings), |
| 340 | + KUNIT_CASE(test_strcasecmp), |
| 341 | + KUNIT_CASE(test_strcasecmp_long_strings), |
| 342 | + KUNIT_CASE(test_strncasecmp), |
| 343 | + KUNIT_CASE(test_strncasecmp_long_strings), |
189 | 344 | {} |
190 | 345 | }; |
191 | 346 |
|
|
0 commit comments