1//===-- Unittests for bcmp ------------------------------------------------===//
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//===----------------------------------------------------------------------===//
8
9#include "memory_utils/memory_check_utils.h"
10#include "src/string/bcmp.h"
11#include "test/UnitTest/Test.h"
12#include "test/UnitTest/TestLogger.h"
13
14namespace LIBC_NAMESPACE {
15
16TEST(LlvmLibcBcmpTest, CmpZeroByte) {
17 const char *lhs = "ab";
18 const char *rhs = "bc";
19 ASSERT_EQ(LIBC_NAMESPACE::bcmp(lhs, rhs, 0), 0);
20}
21
22TEST(LlvmLibcBcmpTest, LhsRhsAreTheSame) {
23 const char *lhs = "ab";
24 const char *rhs = "ab";
25 ASSERT_EQ(LIBC_NAMESPACE::bcmp(lhs, rhs, 2), 0);
26}
27
28TEST(LlvmLibcBcmpTest, LhsBeforeRhsLexically) {
29 const char *lhs = "ab";
30 const char *rhs = "ac";
31 ASSERT_NE(LIBC_NAMESPACE::bcmp(lhs, rhs, 2), 0);
32}
33
34TEST(LlvmLibcBcmpTest, LhsAfterRhsLexically) {
35 const char *lhs = "ac";
36 const char *rhs = "ab";
37 ASSERT_NE(LIBC_NAMESPACE::bcmp(lhs, rhs, 2), 0);
38}
39
40// Adapt CheckBcmp signature to bcmp.
41static inline int Adaptor(cpp::span<char> p1, cpp::span<char> p2, size_t size) {
42 return LIBC_NAMESPACE::bcmp(lhs: p1.begin(), rhs: p2.begin(), count: size);
43}
44
45TEST(LlvmLibcBcmpTest, SizeSweep) {
46 static constexpr size_t kMaxSize = 400;
47 Buffer Buffer1(kMaxSize);
48 Buffer Buffer2(kMaxSize);
49 Randomize(buffer: Buffer1.span());
50 for (size_t size = 0; size < kMaxSize; ++size) {
51 auto span1 = Buffer1.span().subspan(offset: 0, count: size);
52 auto span2 = Buffer2.span().subspan(offset: 0, count: size);
53 const bool OK = CheckBcmp<Adaptor>(span1, span2, size);
54 if (!OK)
55 testing::tlog << "Failed at size=" << size << '\n';
56 ASSERT_TRUE(OK);
57 }
58}
59
60} // namespace LIBC_NAMESPACE
61

source code of libc/test/src/string/bcmp_test.cpp