1 //===-- Unittests for strcmp ----------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "src/string/strcmp.h"
10 #include "test/UnitTest/Test.h"
12 TEST(LlvmLibcStrCmpTest
, EmptyStringsShouldReturnZero
) {
15 int result
= LIBC_NAMESPACE::strcmp(s1
, s2
);
18 // Verify operands reversed.
19 result
= LIBC_NAMESPACE::strcmp(s2
, s1
);
23 TEST(LlvmLibcStrCmpTest
, EmptyStringShouldNotEqualNonEmptyString
) {
24 const char *empty
= "";
25 const char *s2
= "abc";
26 int result
= LIBC_NAMESPACE::strcmp(empty
, s2
);
27 // This should be '\0' - 'a' = -97
28 ASSERT_EQ(result
, '\0' - 'a');
30 // Similar case if empty string is second argument.
31 const char *s3
= "123";
32 result
= LIBC_NAMESPACE::strcmp(s3
, empty
);
33 // This should be '1' - '\0' = 49
34 ASSERT_EQ(result
, '1' - '\0');
37 TEST(LlvmLibcStrCmpTest
, EqualStringsShouldReturnZero
) {
38 const char *s1
= "abc";
39 const char *s2
= "abc";
40 int result
= LIBC_NAMESPACE::strcmp(s1
, s2
);
43 // Verify operands reversed.
44 result
= LIBC_NAMESPACE::strcmp(s2
, s1
);
48 TEST(LlvmLibcStrCmpTest
, ShouldReturnResultOfFirstDifference
) {
49 const char *s1
= "___B42__";
50 const char *s2
= "___C55__";
51 int result
= LIBC_NAMESPACE::strcmp(s1
, s2
);
52 // This should return 'B' - 'C' = -1.
53 ASSERT_EQ(result
, 'B' - 'C');
55 // Verify operands reversed.
56 result
= LIBC_NAMESPACE::strcmp(s2
, s1
);
57 // This should return 'C' - 'B' = 1.
58 ASSERT_EQ(result
, 'C' - 'B');
61 TEST(LlvmLibcStrCmpTest
, CapitalizedLetterShouldNotBeEqual
) {
62 const char *s1
= "abcd";
63 const char *s2
= "abCd";
64 int result
= LIBC_NAMESPACE::strcmp(s1
, s2
);
66 ASSERT_EQ(result
, 'c' - 'C');
68 // Verify operands reversed.
69 result
= LIBC_NAMESPACE::strcmp(s2
, s1
);
71 ASSERT_EQ(result
, 'C' - 'c');
74 TEST(LlvmLibcStrCmpTest
, UnequalLengthStringsShouldNotReturnZero
) {
75 const char *s1
= "abc";
76 const char *s2
= "abcd";
77 int result
= LIBC_NAMESPACE::strcmp(s1
, s2
);
79 ASSERT_EQ(result
, -'\0' - 'd');
81 // Verify operands reversed.
82 result
= LIBC_NAMESPACE::strcmp(s2
, s1
);
84 ASSERT_EQ(result
, 'd' - '\0');
87 TEST(LlvmLibcStrCmpTest
, StringArgumentSwapChangesSign
) {
90 int result
= LIBC_NAMESPACE::strcmp(b
, a
);
92 ASSERT_EQ(result
, 'b' - 'a');
94 result
= LIBC_NAMESPACE::strcmp(a
, b
);
96 ASSERT_EQ(result
, 'a' - 'b');
99 TEST(LlvmLibcStrCmpTest
, Case
) {
100 const char *s1
= "aB";
101 const char *s2
= "ab";
102 int result
= LIBC_NAMESPACE::strcmp(s1
, s2
);
103 ASSERT_LT(result
, 0);
105 // Verify operands reversed.
106 result
= LIBC_NAMESPACE::strcmp(s2
, s1
);
107 ASSERT_GT(result
, 0);