1 | //===-- Unittests for strcmp ----------------------------------------------===// |
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 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "src/string/strcmp.h" |
10 | #include "test/UnitTest/Test.h" |
11 | |
12 | TEST(LlvmLibcStrCmpTest, EmptyStringsShouldReturnZero) { |
13 | const char *s1 = "" ; |
14 | const char *s2 = "" ; |
15 | int result = LIBC_NAMESPACE::strcmp(left: s1, right: s2); |
16 | ASSERT_EQ(result, 0); |
17 | |
18 | // Verify operands reversed. |
19 | result = LIBC_NAMESPACE::strcmp(left: s2, right: s1); |
20 | ASSERT_EQ(result, 0); |
21 | } |
22 | |
23 | TEST(LlvmLibcStrCmpTest, EmptyStringShouldNotEqualNonEmptyString) { |
24 | const char *empty = "" ; |
25 | const char *s2 = "abc" ; |
26 | int result = LIBC_NAMESPACE::strcmp(left: empty, right: s2); |
27 | // This should be '\0' - 'a' = -97 |
28 | ASSERT_EQ(result, -97); |
29 | |
30 | // Similar case if empty string is second argument. |
31 | const char *s3 = "123" ; |
32 | result = LIBC_NAMESPACE::strcmp(left: s3, right: empty); |
33 | // This should be '1' - '\0' = 49 |
34 | ASSERT_EQ(result, 49); |
35 | } |
36 | |
37 | TEST(LlvmLibcStrCmpTest, EqualStringsShouldReturnZero) { |
38 | const char *s1 = "abc" ; |
39 | const char *s2 = "abc" ; |
40 | int result = LIBC_NAMESPACE::strcmp(left: s1, right: s2); |
41 | ASSERT_EQ(result, 0); |
42 | |
43 | // Verify operands reversed. |
44 | result = LIBC_NAMESPACE::strcmp(left: s2, right: s1); |
45 | ASSERT_EQ(result, 0); |
46 | } |
47 | |
48 | TEST(LlvmLibcStrCmpTest, ShouldReturnResultOfFirstDifference) { |
49 | const char *s1 = "___B42__" ; |
50 | const char *s2 = "___C55__" ; |
51 | int result = LIBC_NAMESPACE::strcmp(left: s1, right: s2); |
52 | // This should return 'B' - 'C' = -1. |
53 | ASSERT_EQ(result, -1); |
54 | |
55 | // Verify operands reversed. |
56 | result = LIBC_NAMESPACE::strcmp(left: s2, right: s1); |
57 | // This should return 'C' - 'B' = 1. |
58 | ASSERT_EQ(result, 1); |
59 | } |
60 | |
61 | TEST(LlvmLibcStrCmpTest, CapitalizedLetterShouldNotBeEqual) { |
62 | const char *s1 = "abcd" ; |
63 | const char *s2 = "abCd" ; |
64 | int result = LIBC_NAMESPACE::strcmp(left: s1, right: s2); |
65 | // 'c' - 'C' = 32. |
66 | ASSERT_EQ(result, 32); |
67 | |
68 | // Verify operands reversed. |
69 | result = LIBC_NAMESPACE::strcmp(left: s2, right: s1); |
70 | // 'C' - 'c' = -32. |
71 | ASSERT_EQ(result, -32); |
72 | } |
73 | |
74 | TEST(LlvmLibcStrCmpTest, UnequalLengthStringsShouldNotReturnZero) { |
75 | const char *s1 = "abc" ; |
76 | const char *s2 = "abcd" ; |
77 | int result = LIBC_NAMESPACE::strcmp(left: s1, right: s2); |
78 | // '\0' - 'd' = -100. |
79 | ASSERT_EQ(result, -100); |
80 | |
81 | // Verify operands reversed. |
82 | result = LIBC_NAMESPACE::strcmp(left: s2, right: s1); |
83 | // 'd' - '\0' = 100. |
84 | ASSERT_EQ(result, 100); |
85 | } |
86 | |
87 | TEST(LlvmLibcStrCmpTest, StringArgumentSwapChangesSign) { |
88 | const char *a = "a" ; |
89 | const char *b = "b" ; |
90 | int result = LIBC_NAMESPACE::strcmp(left: b, right: a); |
91 | // 'b' - 'a' = 1. |
92 | ASSERT_EQ(result, 1); |
93 | |
94 | result = LIBC_NAMESPACE::strcmp(left: a, right: b); |
95 | // 'a' - 'b' = -1. |
96 | ASSERT_EQ(result, -1); |
97 | } |
98 | |
99 | TEST(LlvmLibcStrCmpTest, Case) { |
100 | const char *s1 = "aB" ; |
101 | const char *s2 = "ab" ; |
102 | int result = LIBC_NAMESPACE::strcmp(left: s1, right: s2); |
103 | ASSERT_LT(result, 0); |
104 | |
105 | // Verify operands reversed. |
106 | result = LIBC_NAMESPACE::strcmp(left: s2, right: s1); |
107 | ASSERT_GT(result, 0); |
108 | } |
109 | |