blob: 1aa7e7cdaf274c4101bd9936d275aac40d296ed9 [file]
// Copyright 2026 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <array>
#include <cstdarg>
#include <span>
#include <string>
#include <string_view>
#include "../../test/unittest.h"
#include "wrapper.h"
namespace {
using LIBC_NAMESPACE::printf_core::MakePrintf;
using LIBC_NAMESPACE::printf_core::Printf;
using LIBC_NAMESPACE::printf_core::PrintfBuffer;
using LIBC_NAMESPACE::printf_core::PrintfNewline;
using LIBC_NAMESPACE::printf_core::VPrintf;
static_assert(PrintfBuffer<char (&)[16]>);
static_assert(PrintfBuffer<std::array<char, 16>&>);
static_assert(PrintfBuffer<std::span<char>>);
static_assert(PrintfBuffer<std::span<char, 16>>);
static_assert(!PrintfBuffer<std::array<char, 16>>);
static_assert(!PrintfBuffer<const std::array<char, 16>&>);
static_assert(!PrintfBuffer<std::span<const char>>);
template <size_t BufferSize, PrintfNewline AddNewline>
int CallVaListBufferSize(auto&& write, const char* format, ...) {
va_list args;
va_start(args, format);
int result = VPrintf<BufferSize, AddNewline>(write, format, args);
va_end(args);
return result;
}
template <PrintfNewline AddNewline>
int CallVaListSpan(auto&& write, std::span<char> buffer, const char* format, ...) {
va_list args;
va_start(args, format);
int result = VPrintf<AddNewline>(write, buffer, format, args);
va_end(args);
return result;
}
TEST(LibcPrintfCoreWrapperTests, BufferSizeOverloads) {
std::string out;
auto sink = [&out](std::string_view str) -> int {
out.append(str);
return static_cast<int>(str.size());
};
EXPECT_EQ(Printf<16>(sink, "hello %d", 42), 8);
EXPECT_EQ(out, "hello 42");
out.clear();
EXPECT_EQ((Printf<16, PrintfNewline::kYes>(sink, "hello %d", 42)), 9);
EXPECT_EQ(out, "hello 42\n");
out.clear();
EXPECT_EQ((CallVaListBufferSize<8, PrintfNewline::kNo>(sink, "abcdef%d", 12345)), 11);
EXPECT_EQ(out, "abcdef12345");
out.clear();
EXPECT_EQ((CallVaListBufferSize<8, PrintfNewline::kYes>(sink, "abcdef%d", 12345)), 12);
EXPECT_EQ(out, "abcdef12345\n");
}
TEST(LibcPrintfCoreWrapperTests, CallerBufferOverloads) {
std::string out;
auto sink = [&out](std::string_view str) -> int {
out.append(str);
return static_cast<int>(str.size());
};
std::array<char, 8> array_buf{};
EXPECT_EQ(Printf(sink, array_buf, "hello %s", "world"), 11);
EXPECT_EQ(out, "hello world");
out.clear();
char c_buf[8];
EXPECT_EQ(Printf<PrintfNewline::kYes>(sink, c_buf, "line %d", 7), 7);
EXPECT_EQ(out, "line 7\n");
out.clear();
std::span<char, 8> fixed_span{array_buf};
EXPECT_EQ(CallVaListSpan<PrintfNewline::kYes>(sink, fixed_span, "span %d", 99), 8);
EXPECT_EQ(out, "span 99\n");
out.clear();
auto fn = MakePrintf<8, PrintfNewline::kYes>(sink);
EXPECT_EQ(fn("made %d", 1), 7);
EXPECT_EQ(out, "made 1\n");
// Single trailing 0, typed-pointer, and void* arguments all format via '...'.
out.clear();
EXPECT_EQ(Printf(sink, array_buf, "zero %d", 0), 6);
EXPECT_EQ(out, "zero 0");
out.clear();
EXPECT_EQ(Printf<16>(sink, "zero %d", 0), 6);
EXPECT_EQ(out, "zero 0");
char ok[] = "ok";
char* typed_ptr = ok;
out.clear();
EXPECT_EQ(Printf(sink, array_buf, "%s", typed_ptr), 2);
EXPECT_EQ(out, "ok");
out.clear();
EXPECT_EQ(Printf<16>(sink, "%s", typed_ptr), 2);
EXPECT_EQ(out, "ok");
int dummy = 0;
void* void_ptr = &dummy;
out.clear();
int ptr_len = Printf(sink, array_buf, "%p", void_ptr);
EXPECT_GT(ptr_len, 0);
std::string ptr_out = out;
out.clear();
EXPECT_EQ(Printf<32>(sink, "%p", void_ptr), ptr_len);
EXPECT_EQ(out, ptr_out);
}
TEST(LibcPrintfCoreWrapperTests, SinkErrors) {
int calls = 0;
auto fail_immediately = [&calls](std::string_view) -> int {
++calls;
return -1;
};
// Overflow flush failure mid-format (output > 4-byte buffer).
EXPECT_EQ(Printf<4>(fail_immediately, "hello %d", 42), -1);
EXPECT_EQ(calls, 1);
// Final buffer flush failure.
calls = 0;
std::array<char, 16> buf{};
EXPECT_EQ(Printf(fail_immediately, buf, "hi"), -1);
EXPECT_EQ(calls, 1);
// Final newline flush failure after initial buffer flush succeeds.
int fail_on_second = 0;
auto fail_newline = [&fail_on_second](std::string_view) -> int {
return ++fail_on_second == 1 ? 2 : -1;
};
EXPECT_EQ(Printf<PrintfNewline::kYes>(fail_newline, buf, "hi"), -1);
EXPECT_EQ(fail_on_second, 2);
}
} // namespace