144 lines · cpp
1//===-- Unittests for memchr ----------------------------------------------===//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/memchr.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 memchr and abstracts away the explicit cast for19// readability purposes.20const char *call_memchr(const void *src, int c, size_t size) {21 return reinterpret_cast<const char *>(LIBC_NAMESPACE::memchr(src, c, size));22}23 24TEST(LlvmLibcMemChrTest, WideReadMultiIteration) {25 const char *src = "abcdefghijklmnopqrst$\n";26 ASSERT_STREQ(call_memchr(src, '$', 22), "$\n");27}28 29TEST(LlvmLibcMemChrTest, FindsCharacterAfterNullTerminator) {30 // memchr should continue searching after a null terminator.31 const size_t size = 5;32 const unsigned char src[size] = {'a', '\0', 'b', 'c', '\0'};33 // Should return 'b', 'c', '\0' even when after null terminator.34 ASSERT_STREQ(call_memchr(src, 'b', size), "bc");35}36 37TEST(LlvmLibcMemChrTest, FindsCharacterInNonNullTerminatedCollection) {38 const size_t size = 3;39 const unsigned char src[size] = {'a', 'b', 'c'};40 // Should return 'b', 'c'.41 const char *ret = call_memchr(src, 'b', size);42 ASSERT_EQ(ret[0], 'b');43 ASSERT_EQ(ret[1], 'c');44}45 46TEST(LlvmLibcMemChrTest, FindsFirstCharacter) {47 const size_t size = 6;48 const unsigned char src[size] = {'a', 'b', 'c', 'd', 'e', '\0'};49 // Should return original array since 'a' is the first character.50 ASSERT_STREQ(call_memchr(src, 'a', size), "abcde");51}52 53TEST(LlvmLibcMemChrTest, FindsMiddleCharacter) {54 const size_t size = 6;55 const unsigned char src[size] = {'a', 'b', 'c', 'd', 'e', '\0'};56 // Should return characters after (and including) 'c'.57 ASSERT_STREQ(call_memchr(src, 'c', size), "cde");58}59 60TEST(LlvmLibcMemChrTest, FindsLastCharacterThatIsNotNullTerminator) {61 const size_t size = 6;62 const unsigned char src[size] = {'a', 'b', 'c', 'd', 'e', '\0'};63 // Should return 'e' and null-terminator.64 ASSERT_STREQ(call_memchr(src, 'e', size), "e");65}66 67TEST(LlvmLibcMemChrTest, FindsNullTerminator) {68 const size_t size = 6;69 const unsigned char src[size] = {'a', 'b', 'c', 'd', 'e', '\0'};70 // Should return null terminator.71 ASSERT_STREQ(call_memchr(src, '\0', size), "");72}73 74TEST(LlvmLibcMemChrTest, CharacterNotWithinStringShouldReturnNullptr) {75 const size_t size = 4;76 const unsigned char src[size] = {'1', '2', '3', '?'};77 // Since 'z' is not within 'characters', should return nullptr.78 ASSERT_STREQ(call_memchr(src, 'z', size), nullptr);79}80 81TEST(LlvmLibcMemChrTest, CharacterNotWithinSizeShouldReturnNullptr) {82 const unsigned char src[5] = {'1', '2', '3', '4', '\0'};83 // Since '4' is not the first or second character, this should return nullptr.84 const size_t size = 2;85 ASSERT_STREQ(call_memchr(src, '4', size), nullptr);86}87 88TEST(LlvmLibcMemChrTest, TheSourceShouldNotChange) {89 const size_t size = 6;90 const unsigned char src[size] = {'a', 'b', 'c', 'd', 'e', '\0'};91 const char *src_copy = reinterpret_cast<const char *>(src);92 // When the character is found, the source string should not change.93 LIBC_NAMESPACE::memchr(src, 'd', size);94 ASSERT_STREQ(reinterpret_cast<const char *>(src), src_copy);95 // Same case for when the character is not found.96 LIBC_NAMESPACE::memchr(src, 'z', size);97 ASSERT_STREQ(reinterpret_cast<const char *>(src), src_copy);98}99 100TEST(LlvmLibcMemChrTest, ShouldFindFirstOfDuplicates) {101 const size_t size = 12; // 11 characters + null terminator.102 const char *dups = "abc1def1ghi";103 // 1 is duplicated in 'dups', but it should find the first copy.104 ASSERT_STREQ(call_memchr(dups, '1', size), "1def1ghi");105}106 107TEST(LlvmLibcMemChrTest, EmptyStringShouldOnlyMatchNullTerminator) {108 const size_t size = 1; // Null terminator.109 const char *empty_string = "";110 // Null terminator should match.111 ASSERT_STREQ(call_memchr(empty_string, '\0', size), "");112 // All other characters should not match.113 ASSERT_STREQ(call_memchr(empty_string, 'A', size), nullptr);114 ASSERT_STREQ(call_memchr(empty_string, '9', size), nullptr);115 ASSERT_STREQ(call_memchr(empty_string, '?', size), nullptr);116}117 118TEST(LlvmLibcMemChrTest, SingleRepeatedCharacterShouldReturnFirst) {119 const char *dups = "XXXXX";120 const size_t size = 6; // 5 characters + null terminator.121 // Should return original string since X is first character.122 ASSERT_STREQ(call_memchr(dups, 'X', size), dups);123}124 125TEST(LlvmLibcMemChrTest, SignedCharacterFound) {126 char c = -1;127 const size_t size = 1;128 char src[size] = {c};129 const char *actual = call_memchr(src, c, size);130 // Should find the first character 'c'.131 ASSERT_EQ(actual[0], c);132}133 134#if defined(LIBC_ADD_NULL_CHECKS)135 136TEST(LlvmLibcMemChrTest, CrashOnNullPtr) {137 ASSERT_DEATH([]() { LIBC_NAMESPACE::memchr(nullptr, 1, 1); },138 WITH_SIGNAL(-1));139}140 141#endif // defined(LIBC_ADD_NULL_CHECKS)142 143} // namespace144