Make test more lenient for custom clang version strings
[llvm-project.git] / libc / test / src / string / strcmp_test.cpp
blob234447610222f77a75c1f9111356c78427a038f9
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 //===----------------------------------------------------------------------===//
9 #include "src/string/strcmp.h"
10 #include "test/UnitTest/Test.h"
12 TEST(LlvmLibcStrCmpTest, EmptyStringsShouldReturnZero) {
13 const char *s1 = "";
14 const char *s2 = "";
15 int result = LIBC_NAMESPACE::strcmp(s1, s2);
16 ASSERT_EQ(result, 0);
18 // Verify operands reversed.
19 result = LIBC_NAMESPACE::strcmp(s2, s1);
20 ASSERT_EQ(result, 0);
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);
41 ASSERT_EQ(result, 0);
43 // Verify operands reversed.
44 result = LIBC_NAMESPACE::strcmp(s2, s1);
45 ASSERT_EQ(result, 0);
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);
65 // 'c' - 'C' = 32.
66 ASSERT_EQ(result, 'c' - 'C');
68 // Verify operands reversed.
69 result = LIBC_NAMESPACE::strcmp(s2, s1);
70 // 'C' - 'c' = -32.
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);
78 // '\0' - 'd' = -100.
79 ASSERT_EQ(result, -'\0' - 'd');
81 // Verify operands reversed.
82 result = LIBC_NAMESPACE::strcmp(s2, s1);
83 // 'd' - '\0' = 100.
84 ASSERT_EQ(result, 'd' - '\0');
87 TEST(LlvmLibcStrCmpTest, StringArgumentSwapChangesSign) {
88 const char *a = "a";
89 const char *b = "b";
90 int result = LIBC_NAMESPACE::strcmp(b, a);
91 // 'b' - 'a' = 1.
92 ASSERT_EQ(result, 'b' - 'a');
94 result = LIBC_NAMESPACE::strcmp(a, b);
95 // 'a' - 'b' = -1.
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);