131 lines · cpp
1//===-- Unittests for memrchr ---------------------------------------------===//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/memrchr.h"10 11#include <stddef.h>12 13#include "hdr/signal_macros.h"14#include "test/UnitTest/Test.h"15 16namespace {17 18// A helper function that calls memrchr and abstracts away the explicit cast for19// readability purposes.20const char *call_memrchr(const void *src, int c, size_t size) {21 return reinterpret_cast<const char *>(LIBC_NAMESPACE::memrchr(src, c, size));22}23 24TEST(LlvmLibcMemRChrTest, FindsCharacterAfterNullTerminator) {25 // memrchr should continue searching after a null terminator.26 const size_t size = 6;27 const unsigned char src[size] = {'a', '\0', 'b', 'c', 'd', '\0'};28 // Should return 'b', 'c', 'd', '\0' even when after null terminator.29 ASSERT_STREQ(call_memrchr(src, 'b', size), "bcd");30}31 32TEST(LlvmLibcMemRChrTest, FindsCharacterInNonNullTerminatedCollection) {33 const size_t size = 3;34 const unsigned char src[size] = {'a', 'b', 'c'};35 // Should return 'b', 'c'.36 const char *ret = call_memrchr(src, 'b', size);37 ASSERT_EQ(ret[0], 'b');38 ASSERT_EQ(ret[1], 'c');39}40 41TEST(LlvmLibcMemRChrTest, FindsFirstCharacter) {42 const size_t size = 6;43 const unsigned char src[size] = {'a', 'b', 'c', 'd', 'e', '\0'};44 // Should return original array since 'a' is the first character.45 ASSERT_STREQ(call_memrchr(src, 'a', size), "abcde");46}47 48TEST(LlvmLibcMemRChrTest, FindsMiddleCharacter) {49 const size_t size = 6;50 const unsigned char src[size] = {'a', 'b', 'c', 'd', 'e', '\0'};51 // Should return characters after (and including) 'c'.52 ASSERT_STREQ(call_memrchr(src, 'c', size), "cde");53}54 55TEST(LlvmLibcMemRChrTest, FindsLastCharacterThatIsNotNullTerminator) {56 const size_t size = 6;57 const unsigned char src[size] = {'a', 'b', 'c', 'd', 'e', '\0'};58 // Should return 'e' and null-terminator.59 ASSERT_STREQ(call_memrchr(src, 'e', size), "e");60}61 62TEST(LlvmLibcMemRChrTest, FindsNullTerminator) {63 const size_t size = 6;64 const unsigned char src[size] = {'a', 'b', 'c', 'd', 'e', '\0'};65 // Should return null terminator.66 ASSERT_STREQ(call_memrchr(src, '\0', size), "");67}68 69TEST(LlvmLibcMemRChrTest, CharacterNotWithinStringShouldReturnNullptr) {70 const size_t size = 4;71 const unsigned char src[size] = {'1', '2', '3', '?'};72 // Since 'z' is not within 'characters', should return nullptr.73 ASSERT_STREQ(call_memrchr(src, 'z', size), nullptr);74}75 76TEST(LlvmLibcMemRChrTest, CharacterNotWithinSizeShouldReturnNullptr) {77 const unsigned char src[5] = {'1', '2', '3', '4', '\0'};78 // Since '4' is not within the first 2 characters, this should return nullptr.79 const size_t size = 2;80 ASSERT_STREQ(call_memrchr(src, '4', size), nullptr);81}82 83TEST(LlvmLibcMemRChrTest, ShouldFindLastOfDuplicates) {84 size_t size = 12; // 11 characters + null terminator.85 const char *dups = "abc1def1ghi";86 // 1 is duplicated in 'dups', but it should find the last copy.87 ASSERT_STREQ(call_memrchr(dups, '1', size), "1ghi");88 89 const char *repeated = "XXXXX";90 size = 6; // 5 characters + null terminator.91 // Should return the last X with the null terminator.92 ASSERT_STREQ(call_memrchr(repeated, 'X', size), "X");93}94 95TEST(LlvmLibcMemRChrTest, EmptyStringShouldOnlyMatchNullTerminator) {96 const size_t size = 1; // Null terminator.97 const char *empty_string = "";98 // Null terminator should match.99 ASSERT_STREQ(call_memrchr(empty_string, '\0', size), "");100 // All other characters should not match.101 ASSERT_STREQ(call_memrchr(empty_string, 'A', size), nullptr);102 ASSERT_STREQ(call_memrchr(empty_string, '9', size), nullptr);103 ASSERT_STREQ(call_memrchr(empty_string, '?', size), nullptr);104}105 106TEST(LlvmLibcMemRChrTest, SignedCharacterFound) {107 char c = -1;108 const size_t size = 1;109 char src[size] = {c};110 const char *actual = call_memrchr(src, c, size);111 // Should find the last character 'c'.112 ASSERT_EQ(actual[0], c);113}114 115TEST(LlvmLibcMemRChrTest, ZeroLengthShouldReturnNullptr) {116 const unsigned char src[4] = {'a', 'b', 'c', '\0'};117 // This will iterate over exactly zero characters, so should return nullptr.118 ASSERT_STREQ(call_memrchr(src, 'd', 0), nullptr);119}120 121#if defined(LIBC_ADD_NULL_CHECKS)122 123TEST(LlvmLibcMemRChrTest, CrashOnNullPtr) {124 ASSERT_DEATH([]() { LIBC_NAMESPACE::memrchr(nullptr, 'd', 1); },125 WITH_SIGNAL(-1));126}127 128#endif // defined(LIBC_ADD_NULL_CHECKS)129 130} // namespace131