109 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9#include "src/string/strcmp.h"10#include "test/UnitTest/Test.h"11 12TEST(LlvmLibcStrCmpTest, EmptyStringsShouldReturnZero) {13 const char *s1 = "";14 const char *s2 = "";15 int result = LIBC_NAMESPACE::strcmp(s1, s2);16 ASSERT_EQ(result, 0);17 18 // Verify operands reversed.19 result = LIBC_NAMESPACE::strcmp(s2, s1);20 ASSERT_EQ(result, 0);21}22 23TEST(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' = -9728 ASSERT_EQ(result, '\0' - 'a');29 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' = 4934 ASSERT_EQ(result, '1' - '\0');35}36 37TEST(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);42 43 // Verify operands reversed.44 result = LIBC_NAMESPACE::strcmp(s2, s1);45 ASSERT_EQ(result, 0);46}47 48TEST(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');54 55 // Verify operands reversed.56 result = LIBC_NAMESPACE::strcmp(s2, s1);57 // This should return 'C' - 'B' = 1.58 ASSERT_EQ(result, 'C' - 'B');59}60 61TEST(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');67 68 // Verify operands reversed.69 result = LIBC_NAMESPACE::strcmp(s2, s1);70 // 'C' - 'c' = -32.71 ASSERT_EQ(result, 'C' - 'c');72}73 74TEST(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');80 81 // Verify operands reversed.82 result = LIBC_NAMESPACE::strcmp(s2, s1);83 // 'd' - '\0' = 100.84 ASSERT_EQ(result, 'd' - '\0');85}86 87TEST(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');93 94 result = LIBC_NAMESPACE::strcmp(a, b);95 // 'a' - 'b' = -1.96 ASSERT_EQ(result, 'a' - 'b');97}98 99TEST(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);104 105 // Verify operands reversed.106 result = LIBC_NAMESPACE::strcmp(s2, s1);107 ASSERT_GT(result, 0);108}109