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 "utils/UnitTest/Test.h"
11 
TEST(StrCmpTest,EmptyStringsShouldReturnZero)12 TEST(StrCmpTest, EmptyStringsShouldReturnZero) {
13   const char *s1 = "";
14   const char *s2 = "";
15   int result = __llvm_libc::strcmp(s1, s2);
16   ASSERT_EQ(result, 0);
17 
18   // Verify operands reversed.
19   result = __llvm_libc::strcmp(s2, s1);
20   ASSERT_EQ(result, 0);
21 }
22 
TEST(StrCmpTest,EmptyStringShouldNotEqualNonEmptyString)23 TEST(StrCmpTest, EmptyStringShouldNotEqualNonEmptyString) {
24   const char *empty = "";
25   const char *s2 = "abc";
26   int result = __llvm_libc::strcmp(empty, 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 = __llvm_libc::strcmp(s3, empty);
33   // This should be '1' - '\0' = 49
34   ASSERT_EQ(result, 49);
35 }
36 
TEST(StrCmpTest,EqualStringsShouldReturnZero)37 TEST(StrCmpTest, EqualStringsShouldReturnZero) {
38   const char *s1 = "abc";
39   const char *s2 = "abc";
40   int result = __llvm_libc::strcmp(s1, s2);
41   ASSERT_EQ(result, 0);
42 
43   // Verify operands reversed.
44   result = __llvm_libc::strcmp(s2, s1);
45   ASSERT_EQ(result, 0);
46 }
47 
TEST(StrCmpTest,ShouldReturnResultOfFirstDifference)48 TEST(StrCmpTest, ShouldReturnResultOfFirstDifference) {
49   const char *s1 = "___B42__";
50   const char *s2 = "___C55__";
51   int result = __llvm_libc::strcmp(s1, s2);
52   // This should return 'B' - 'C' = -1.
53   ASSERT_EQ(result, -1);
54 
55   // Verify operands reversed.
56   result = __llvm_libc::strcmp(s2, s1);
57   // This should return 'C' - 'B' = 1.
58   ASSERT_EQ(result, 1);
59 }
60 
TEST(StrCmpTest,CapitalizedLetterShouldNotBeEqual)61 TEST(StrCmpTest, CapitalizedLetterShouldNotBeEqual) {
62   const char *s1 = "abcd";
63   const char *s2 = "abCd";
64   int result = __llvm_libc::strcmp(s1, s2);
65   // 'c' - 'C' = 32.
66   ASSERT_EQ(result, 32);
67 
68   // Verify operands reversed.
69   result = __llvm_libc::strcmp(s2, s1);
70   // 'C' - 'c' = -32.
71   ASSERT_EQ(result, -32);
72 }
73 
TEST(StrCmpTest,UnequalLengthStringsShouldNotReturnZero)74 TEST(StrCmpTest, UnequalLengthStringsShouldNotReturnZero) {
75   const char *s1 = "abc";
76   const char *s2 = "abcd";
77   int result = __llvm_libc::strcmp(s1, s2);
78   // '\0' - 'd' = -100.
79   ASSERT_EQ(result, -100);
80 
81   // Verify operands reversed.
82   result = __llvm_libc::strcmp(s2, s1);
83   // 'd' - '\0' = 100.
84   ASSERT_EQ(result, 100);
85 }
86 
TEST(StrCmpTest,StringArgumentSwapChangesSign)87 TEST(StrCmpTest, StringArgumentSwapChangesSign) {
88   const char *a = "a";
89   const char *b = "b";
90   int result = __llvm_libc::strcmp(b, a);
91   // 'b' - 'a' = 1.
92   ASSERT_EQ(result, 1);
93 
94   result = __llvm_libc::strcmp(a, b);
95   // 'a' - 'b' = -1.
96   ASSERT_EQ(result, -1);
97 }
98