2018-09-19 15:55:45 +00:00
|
|
|
// Formatting library for C++ - core tests
|
2018-03-04 17:16:51 +00:00
|
|
|
//
|
|
|
|
// Copyright (c) 2012 - present, Victor Zverovich
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// For the license information refer to format.h.
|
2014-06-06 13:38:37 +00:00
|
|
|
|
2021-04-24 00:11:01 +00:00
|
|
|
// clang-format off
|
|
|
|
#include "test-assert.h"
|
|
|
|
// clang-format on
|
|
|
|
|
2024-01-16 15:01:15 +00:00
|
|
|
#include "fmt/base.h"
|
|
|
|
|
2021-04-25 23:19:01 +00:00
|
|
|
#include <climits> // INT_MAX
|
|
|
|
#include <cstring> // std::strlen
|
|
|
|
#include <functional> // std::equal_to
|
2024-01-16 15:01:15 +00:00
|
|
|
#include <iterator> // std::back_insert_iterator, std::distance
|
2021-04-25 23:19:01 +00:00
|
|
|
#include <limits> // std::numeric_limits
|
|
|
|
#include <string> // std::string
|
|
|
|
#include <type_traits> // std::is_same
|
2014-09-29 15:48:16 +00:00
|
|
|
|
2021-04-28 22:59:43 +00:00
|
|
|
#include "gmock/gmock.h"
|
2014-06-06 14:29:57 +00:00
|
|
|
|
2019-01-13 02:27:38 +00:00
|
|
|
using fmt::string_view;
|
2020-05-10 14:25:42 +00:00
|
|
|
using fmt::detail::buffer;
|
2014-09-29 15:48:16 +00:00
|
|
|
|
2016-12-11 21:22:45 +00:00
|
|
|
using testing::_;
|
2020-07-11 13:53:25 +00:00
|
|
|
using testing::Invoke;
|
2020-07-10 14:50:37 +00:00
|
|
|
using testing::Return;
|
2014-06-06 13:38:37 +00:00
|
|
|
|
2021-06-06 16:25:12 +00:00
|
|
|
#ifdef FMT_FORMAT_H_
|
|
|
|
# error core-test includes format.h
|
|
|
|
#endif
|
|
|
|
|
2024-01-03 04:56:51 +00:00
|
|
|
fmt::appender copy(fmt::string_view s, fmt::appender out) {
|
|
|
|
for (char c : s) *out++ = c;
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2021-04-24 00:11:01 +00:00
|
|
|
TEST(string_view_test, value_type) {
|
|
|
|
static_assert(std::is_same<string_view::value_type, char>::value, "");
|
|
|
|
}
|
2016-05-06 14:37:20 +00:00
|
|
|
|
2021-05-01 00:02:14 +00:00
|
|
|
TEST(string_view_test, ctor) {
|
|
|
|
EXPECT_STREQ("abc", fmt::string_view("abc").data());
|
|
|
|
EXPECT_EQ(3u, fmt::string_view("abc").size());
|
|
|
|
|
|
|
|
EXPECT_STREQ("defg", fmt::string_view(std::string("defg")).data());
|
|
|
|
EXPECT_EQ(4u, fmt::string_view(std::string("defg")).size());
|
|
|
|
}
|
|
|
|
|
2021-04-24 00:11:01 +00:00
|
|
|
TEST(string_view_test, length) {
|
|
|
|
// Test that string_view::size() returns string length, not buffer size.
|
|
|
|
char str[100] = "some string";
|
|
|
|
EXPECT_EQ(std::strlen(str), string_view(str).size());
|
|
|
|
EXPECT_LT(std::strlen(str), sizeof(str));
|
|
|
|
}
|
2017-08-13 20:09:02 +00:00
|
|
|
|
2021-04-24 00:11:01 +00:00
|
|
|
// Check string_view's comparison operator.
|
|
|
|
template <template <typename> class Op> void check_op() {
|
|
|
|
const char* inputs[] = {"foo", "fop", "fo"};
|
|
|
|
size_t num_inputs = sizeof(inputs) / sizeof(*inputs);
|
|
|
|
for (size_t i = 0; i < num_inputs; ++i) {
|
|
|
|
for (size_t j = 0; j < num_inputs; ++j) {
|
|
|
|
string_view lhs(inputs[i]), rhs(inputs[j]);
|
|
|
|
EXPECT_EQ(Op<int>()(lhs.compare(rhs), 0), Op<string_view>()(lhs, rhs));
|
|
|
|
}
|
2017-08-13 20:09:02 +00:00
|
|
|
}
|
2021-04-24 00:11:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(string_view_test, compare) {
|
|
|
|
EXPECT_EQ(string_view("foo").compare(string_view("foo")), 0);
|
|
|
|
EXPECT_GT(string_view("fop").compare(string_view("foo")), 0);
|
|
|
|
EXPECT_LT(string_view("foo").compare(string_view("fop")), 0);
|
|
|
|
EXPECT_GT(string_view("foo").compare(string_view("fo")), 0);
|
|
|
|
EXPECT_LT(string_view("fo").compare(string_view("foo")), 0);
|
2022-09-04 18:41:16 +00:00
|
|
|
|
|
|
|
EXPECT_TRUE(string_view("foo").starts_with('f'));
|
|
|
|
EXPECT_FALSE(string_view("foo").starts_with('o'));
|
|
|
|
EXPECT_FALSE(string_view().starts_with('o'));
|
|
|
|
|
|
|
|
EXPECT_TRUE(string_view("foo").starts_with("fo"));
|
|
|
|
EXPECT_TRUE(string_view("foo").starts_with("foo"));
|
|
|
|
EXPECT_FALSE(string_view("foo").starts_with("fooo"));
|
|
|
|
EXPECT_FALSE(string_view().starts_with("fooo"));
|
|
|
|
|
2021-04-24 00:11:01 +00:00
|
|
|
check_op<std::equal_to>();
|
|
|
|
check_op<std::not_equal_to>();
|
|
|
|
check_op<std::less>();
|
|
|
|
check_op<std::less_equal>();
|
|
|
|
check_op<std::greater>();
|
|
|
|
check_op<std::greater_equal>();
|
|
|
|
}
|
2017-08-13 20:09:02 +00:00
|
|
|
|
2024-07-21 15:00:34 +00:00
|
|
|
TEST(base_test, is_locking) {
|
2024-07-29 16:18:20 +00:00
|
|
|
EXPECT_FALSE(fmt::detail::is_locking<const char(&)[3]>());
|
2024-07-21 15:00:34 +00:00
|
|
|
}
|
|
|
|
|
2024-06-22 18:10:54 +00:00
|
|
|
TEST(base_test, is_output_iterator) {
|
2021-05-01 00:02:14 +00:00
|
|
|
EXPECT_TRUE((fmt::detail::is_output_iterator<char*, char>::value));
|
|
|
|
EXPECT_FALSE((fmt::detail::is_output_iterator<const char*, char>::value));
|
|
|
|
EXPECT_FALSE((fmt::detail::is_output_iterator<std::string, char>::value));
|
|
|
|
EXPECT_TRUE(
|
|
|
|
(fmt::detail::is_output_iterator<std::back_insert_iterator<std::string>,
|
|
|
|
char>::value));
|
|
|
|
EXPECT_TRUE(
|
|
|
|
(fmt::detail::is_output_iterator<std::string::iterator, char>::value));
|
|
|
|
EXPECT_FALSE((fmt::detail::is_output_iterator<std::string::const_iterator,
|
|
|
|
char>::value));
|
|
|
|
}
|
|
|
|
|
2024-06-22 18:10:54 +00:00
|
|
|
TEST(base_test, is_back_insert_iterator) {
|
2024-01-08 00:44:42 +00:00
|
|
|
EXPECT_TRUE(fmt::detail::is_back_insert_iterator<
|
|
|
|
std::back_insert_iterator<std::string>>::value);
|
|
|
|
EXPECT_FALSE(fmt::detail::is_back_insert_iterator<
|
|
|
|
std::front_insert_iterator<std::string>>::value);
|
|
|
|
}
|
|
|
|
|
2018-10-24 13:34:28 +00:00
|
|
|
#if !FMT_GCC_VERSION || FMT_GCC_VERSION >= 470
|
2021-04-24 00:11:01 +00:00
|
|
|
TEST(buffer_test, noncopyable) {
|
2019-04-07 17:05:49 +00:00
|
|
|
EXPECT_FALSE(std::is_copy_constructible<buffer<char>>::value);
|
2022-05-29 22:01:43 +00:00
|
|
|
# if !FMT_MSC_VERSION
|
2018-09-19 15:55:45 +00:00
|
|
|
// std::is_copy_assignable is broken in MSVC2013.
|
2019-04-07 17:05:49 +00:00
|
|
|
EXPECT_FALSE(std::is_copy_assignable<buffer<char>>::value);
|
2019-01-13 02:27:38 +00:00
|
|
|
# endif
|
2014-09-29 15:48:16 +00:00
|
|
|
}
|
|
|
|
|
2021-04-24 00:11:01 +00:00
|
|
|
TEST(buffer_test, nonmoveable) {
|
2019-04-07 17:05:49 +00:00
|
|
|
EXPECT_FALSE(std::is_move_constructible<buffer<char>>::value);
|
2022-05-29 22:01:43 +00:00
|
|
|
# if !FMT_MSC_VERSION
|
2018-09-19 15:55:45 +00:00
|
|
|
// std::is_move_assignable is broken in MSVC2013.
|
2019-04-07 17:05:49 +00:00
|
|
|
EXPECT_FALSE(std::is_move_assignable<buffer<char>>::value);
|
2019-01-13 02:27:38 +00:00
|
|
|
# endif
|
2018-09-19 15:55:45 +00:00
|
|
|
}
|
2018-10-24 13:34:28 +00:00
|
|
|
#endif
|
2014-09-29 15:48:16 +00:00
|
|
|
|
2021-04-24 00:11:01 +00:00
|
|
|
TEST(buffer_test, indestructible) {
|
2020-07-11 03:54:15 +00:00
|
|
|
static_assert(!std::is_destructible<fmt::detail::buffer<int>>(),
|
|
|
|
"buffer's destructor is protected");
|
|
|
|
}
|
2014-09-30 14:30:27 +00:00
|
|
|
|
2020-10-16 00:41:56 +00:00
|
|
|
template <typename T> struct mock_buffer final : buffer<T> {
|
2023-04-20 20:16:21 +00:00
|
|
|
MOCK_METHOD(size_t, do_grow, (size_t));
|
2014-10-01 16:32:31 +00:00
|
|
|
|
2024-01-02 15:02:20 +00:00
|
|
|
static void grow(buffer<T>& buf, size_t capacity) {
|
|
|
|
auto& self = static_cast<mock_buffer&>(buf);
|
|
|
|
self.set(buf.data(), self.do_grow(capacity));
|
2021-07-09 15:05:02 +00:00
|
|
|
}
|
2014-09-30 14:30:27 +00:00
|
|
|
|
2024-01-02 15:02:20 +00:00
|
|
|
mock_buffer(T* data = nullptr, size_t buf_capacity = 0) : buffer<T>(grow) {
|
2020-10-28 03:55:44 +00:00
|
|
|
this->set(data, buf_capacity);
|
2020-07-11 13:53:25 +00:00
|
|
|
ON_CALL(*this, do_grow(_)).WillByDefault(Invoke([](size_t capacity) {
|
|
|
|
return capacity;
|
|
|
|
}));
|
2020-07-10 14:50:37 +00:00
|
|
|
}
|
2014-09-30 14:30:27 +00:00
|
|
|
};
|
|
|
|
|
2021-04-24 00:11:01 +00:00
|
|
|
TEST(buffer_test, ctor) {
|
2014-09-30 14:30:27 +00:00
|
|
|
{
|
2018-09-19 15:55:45 +00:00
|
|
|
mock_buffer<int> buffer;
|
2019-10-08 13:28:39 +00:00
|
|
|
EXPECT_EQ(nullptr, buffer.data());
|
2018-06-06 13:57:59 +00:00
|
|
|
EXPECT_EQ(static_cast<size_t>(0), buffer.size());
|
|
|
|
EXPECT_EQ(static_cast<size_t>(0), buffer.capacity());
|
2014-09-30 14:30:27 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
int dummy;
|
2018-09-19 15:55:45 +00:00
|
|
|
mock_buffer<int> buffer(&dummy);
|
2014-09-30 14:30:27 +00:00
|
|
|
EXPECT_EQ(&dummy, &buffer[0]);
|
2018-06-06 13:57:59 +00:00
|
|
|
EXPECT_EQ(static_cast<size_t>(0), buffer.size());
|
|
|
|
EXPECT_EQ(static_cast<size_t>(0), buffer.capacity());
|
2014-09-30 14:30:27 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
int dummy;
|
2020-05-07 22:59:46 +00:00
|
|
|
size_t capacity = std::numeric_limits<size_t>::max();
|
2018-09-19 15:55:45 +00:00
|
|
|
mock_buffer<int> buffer(&dummy, capacity);
|
2014-09-30 14:30:27 +00:00
|
|
|
EXPECT_EQ(&dummy, &buffer[0]);
|
2018-06-06 13:57:59 +00:00
|
|
|
EXPECT_EQ(static_cast<size_t>(0), buffer.size());
|
2014-09-30 14:30:27 +00:00
|
|
|
EXPECT_EQ(capacity, buffer.capacity());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-24 00:11:01 +00:00
|
|
|
TEST(buffer_test, access) {
|
2014-09-30 14:30:27 +00:00
|
|
|
char data[10];
|
2018-09-19 15:55:45 +00:00
|
|
|
mock_buffer<char> buffer(data, sizeof(data));
|
2014-09-30 14:30:27 +00:00
|
|
|
buffer[0] = 11;
|
|
|
|
EXPECT_EQ(11, buffer[0]);
|
|
|
|
buffer[3] = 42;
|
|
|
|
EXPECT_EQ(42, *(&buffer[0] + 3));
|
2020-05-10 14:25:42 +00:00
|
|
|
const fmt::detail::buffer<char>& const_buffer = buffer;
|
2014-09-30 14:30:27 +00:00
|
|
|
EXPECT_EQ(42, const_buffer[3]);
|
|
|
|
}
|
|
|
|
|
2021-04-24 00:11:01 +00:00
|
|
|
TEST(buffer_test, try_resize) {
|
2014-10-01 15:12:10 +00:00
|
|
|
char data[123];
|
2018-09-19 15:55:45 +00:00
|
|
|
mock_buffer<char> buffer(data, sizeof(data));
|
2014-10-01 15:12:10 +00:00
|
|
|
buffer[10] = 42;
|
|
|
|
EXPECT_EQ(42, buffer[10]);
|
2020-07-10 14:50:37 +00:00
|
|
|
buffer.try_resize(20);
|
2014-10-01 15:12:10 +00:00
|
|
|
EXPECT_EQ(20u, buffer.size());
|
|
|
|
EXPECT_EQ(123u, buffer.capacity());
|
|
|
|
EXPECT_EQ(42, buffer[10]);
|
2020-07-10 14:50:37 +00:00
|
|
|
buffer.try_resize(5);
|
2014-10-01 15:12:10 +00:00
|
|
|
EXPECT_EQ(5u, buffer.size());
|
|
|
|
EXPECT_EQ(123u, buffer.capacity());
|
|
|
|
EXPECT_EQ(42, buffer[10]);
|
2020-07-10 14:50:37 +00:00
|
|
|
// Check if try_resize calls grow.
|
2014-10-01 16:32:31 +00:00
|
|
|
EXPECT_CALL(buffer, do_grow(124));
|
2020-07-10 14:50:37 +00:00
|
|
|
buffer.try_resize(124);
|
2014-10-01 16:32:31 +00:00
|
|
|
EXPECT_CALL(buffer, do_grow(200));
|
2020-07-10 14:50:37 +00:00
|
|
|
buffer.try_resize(200);
|
|
|
|
}
|
|
|
|
|
2021-04-24 00:11:01 +00:00
|
|
|
TEST(buffer_test, try_resize_partial) {
|
2020-07-10 14:50:37 +00:00
|
|
|
char data[10];
|
|
|
|
mock_buffer<char> buffer(data, sizeof(data));
|
|
|
|
EXPECT_CALL(buffer, do_grow(20)).WillOnce(Return(15));
|
|
|
|
buffer.try_resize(20);
|
|
|
|
EXPECT_EQ(buffer.capacity(), 15);
|
|
|
|
EXPECT_EQ(buffer.size(), 15);
|
2014-10-01 15:12:10 +00:00
|
|
|
}
|
|
|
|
|
2021-04-24 00:11:01 +00:00
|
|
|
TEST(buffer_test, clear) {
|
2020-07-11 03:54:15 +00:00
|
|
|
mock_buffer<char> buffer;
|
|
|
|
EXPECT_CALL(buffer, do_grow(20));
|
2020-07-10 14:50:37 +00:00
|
|
|
buffer.try_resize(20);
|
|
|
|
buffer.try_resize(0);
|
2018-06-06 13:57:59 +00:00
|
|
|
EXPECT_EQ(static_cast<size_t>(0), buffer.size());
|
2014-10-01 15:12:10 +00:00
|
|
|
EXPECT_EQ(20u, buffer.capacity());
|
|
|
|
}
|
|
|
|
|
2021-04-24 00:11:01 +00:00
|
|
|
TEST(buffer_test, append) {
|
2014-10-01 15:12:10 +00:00
|
|
|
char data[15];
|
2018-09-19 15:55:45 +00:00
|
|
|
mock_buffer<char> buffer(data, 10);
|
2020-07-11 03:54:15 +00:00
|
|
|
auto test = "test";
|
2014-10-01 15:12:10 +00:00
|
|
|
buffer.append(test, test + 5);
|
|
|
|
EXPECT_STREQ(test, &buffer[0]);
|
|
|
|
EXPECT_EQ(5u, buffer.size());
|
2020-07-10 14:50:37 +00:00
|
|
|
buffer.try_resize(10);
|
2014-10-01 16:32:31 +00:00
|
|
|
EXPECT_CALL(buffer, do_grow(12));
|
2014-10-01 15:12:10 +00:00
|
|
|
buffer.append(test, test + 2);
|
|
|
|
EXPECT_EQ('t', buffer[10]);
|
|
|
|
EXPECT_EQ('e', buffer[11]);
|
|
|
|
EXPECT_EQ(12u, buffer.size());
|
|
|
|
}
|
|
|
|
|
2021-04-24 00:11:01 +00:00
|
|
|
TEST(buffer_test, append_partial) {
|
2020-07-11 13:53:25 +00:00
|
|
|
char data[10];
|
|
|
|
mock_buffer<char> buffer(data, sizeof(data));
|
|
|
|
testing::InSequence seq;
|
|
|
|
EXPECT_CALL(buffer, do_grow(15)).WillOnce(Return(10));
|
|
|
|
EXPECT_CALL(buffer, do_grow(15)).WillOnce(Invoke([&buffer](size_t) {
|
|
|
|
EXPECT_EQ(fmt::string_view(buffer.data(), buffer.size()), "0123456789");
|
|
|
|
buffer.clear();
|
|
|
|
return 10;
|
|
|
|
}));
|
|
|
|
auto test = "0123456789abcde";
|
|
|
|
buffer.append(test, test + 15);
|
|
|
|
}
|
|
|
|
|
2021-04-24 00:11:01 +00:00
|
|
|
TEST(buffer_test, append_allocates_enough_storage) {
|
2014-10-01 15:12:10 +00:00
|
|
|
char data[19];
|
2018-09-19 15:55:45 +00:00
|
|
|
mock_buffer<char> buffer(data, 10);
|
2020-07-11 03:54:15 +00:00
|
|
|
auto test = "abcdefgh";
|
2020-07-10 14:50:37 +00:00
|
|
|
buffer.try_resize(10);
|
2014-10-01 16:32:31 +00:00
|
|
|
EXPECT_CALL(buffer, do_grow(19));
|
2014-10-01 15:12:10 +00:00
|
|
|
buffer.append(test, test + 9);
|
|
|
|
}
|
|
|
|
|
2024-07-13 14:23:02 +00:00
|
|
|
TEST(base_test, get_buffer) {
|
|
|
|
mock_buffer<char> buffer;
|
|
|
|
void* buffer_ptr = &buffer;
|
|
|
|
auto&& appender_result = fmt::detail::get_buffer<char>(fmt::appender(buffer));
|
|
|
|
EXPECT_EQ(&appender_result, buffer_ptr);
|
|
|
|
auto&& back_inserter_result =
|
|
|
|
fmt::detail::get_buffer<char>(std::back_inserter(buffer));
|
|
|
|
EXPECT_EQ(&back_inserter_result, buffer_ptr);
|
|
|
|
}
|
|
|
|
|
2021-04-24 00:11:01 +00:00
|
|
|
struct test_struct {};
|
|
|
|
|
|
|
|
FMT_BEGIN_NAMESPACE
|
|
|
|
template <typename Char> struct formatter<test_struct, Char> {
|
2023-07-14 00:17:00 +00:00
|
|
|
FMT_CONSTEXPR auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) {
|
2021-04-24 00:11:01 +00:00
|
|
|
return ctx.begin();
|
|
|
|
}
|
|
|
|
|
2023-01-02 17:45:51 +00:00
|
|
|
auto format(test_struct, format_context& ctx) const -> decltype(ctx.out()) {
|
2024-01-03 04:56:51 +00:00
|
|
|
return copy("test", ctx.out());
|
2021-04-24 00:11:01 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
FMT_END_NAMESPACE
|
|
|
|
|
|
|
|
TEST(arg_test, format_args) {
|
|
|
|
auto args = fmt::format_args();
|
|
|
|
EXPECT_FALSE(args.get(1));
|
|
|
|
}
|
|
|
|
|
2018-03-03 22:04:59 +00:00
|
|
|
// Use a unique result type to make sure that there are no undesirable
|
|
|
|
// conversions.
|
2018-09-19 15:55:45 +00:00
|
|
|
struct test_result {};
|
2016-12-11 21:22:45 +00:00
|
|
|
|
2019-01-13 02:27:38 +00:00
|
|
|
template <typename T> struct mock_visitor {
|
2024-01-02 01:31:36 +00:00
|
|
|
template <typename U> struct result {
|
|
|
|
using type = test_result;
|
|
|
|
};
|
2018-09-19 15:55:45 +00:00
|
|
|
|
|
|
|
mock_visitor() {
|
2020-07-10 14:50:37 +00:00
|
|
|
ON_CALL(*this, visit(_)).WillByDefault(Return(test_result()));
|
2016-04-20 16:11:33 +00:00
|
|
|
}
|
2016-12-11 21:22:45 +00:00
|
|
|
|
2023-04-20 20:16:21 +00:00
|
|
|
MOCK_METHOD(test_result, visit, (T));
|
|
|
|
MOCK_METHOD(void, unexpected, ());
|
2016-12-11 21:22:45 +00:00
|
|
|
|
2023-01-02 17:45:51 +00:00
|
|
|
auto operator()(T value) -> test_result { return visit(value); }
|
2016-12-11 21:22:45 +00:00
|
|
|
|
2023-01-02 17:45:51 +00:00
|
|
|
template <typename U> auto operator()(U) -> test_result {
|
2016-12-11 21:22:45 +00:00
|
|
|
unexpected();
|
2018-09-19 15:55:45 +00:00
|
|
|
return test_result();
|
2014-07-14 14:27:07 +00:00
|
|
|
}
|
2014-07-14 13:55:29 +00:00
|
|
|
};
|
|
|
|
|
2024-01-02 01:31:36 +00:00
|
|
|
template <typename T> struct visit_type {
|
|
|
|
using type = T;
|
|
|
|
};
|
2016-12-11 21:22:45 +00:00
|
|
|
|
2024-01-02 01:31:36 +00:00
|
|
|
#define VISIT_TYPE(type_, visit_type_) \
|
|
|
|
template <> struct visit_type<type_> { \
|
|
|
|
using type = visit_type_; \
|
|
|
|
}
|
2016-12-11 21:22:45 +00:00
|
|
|
|
|
|
|
VISIT_TYPE(signed char, int);
|
|
|
|
VISIT_TYPE(unsigned char, unsigned);
|
|
|
|
VISIT_TYPE(short, int);
|
|
|
|
VISIT_TYPE(unsigned short, unsigned);
|
|
|
|
|
|
|
|
#if LONG_MAX == INT_MAX
|
|
|
|
VISIT_TYPE(long, int);
|
|
|
|
VISIT_TYPE(unsigned long, unsigned);
|
|
|
|
#else
|
2017-08-26 16:09:43 +00:00
|
|
|
VISIT_TYPE(long, long long);
|
|
|
|
VISIT_TYPE(unsigned long, unsigned long long);
|
2016-12-11 21:22:45 +00:00
|
|
|
#endif
|
|
|
|
|
2024-08-25 16:20:40 +00:00
|
|
|
#if FMT_BUILTIN_TYPES
|
2024-09-14 16:18:47 +00:00
|
|
|
# define CHECK_ARG(expected, value) \
|
|
|
|
{ \
|
|
|
|
testing::StrictMock<mock_visitor<decltype(expected)>> visitor; \
|
|
|
|
EXPECT_CALL(visitor, visit(expected)); \
|
|
|
|
auto var = value; \
|
|
|
|
fmt::basic_format_arg<fmt::format_context>(var).visit(visitor); \
|
2024-08-25 16:20:40 +00:00
|
|
|
}
|
|
|
|
#else
|
2024-09-04 21:42:25 +00:00
|
|
|
# define CHECK_ARG(expected, value)
|
2024-08-25 16:20:40 +00:00
|
|
|
#endif
|
2016-12-11 21:22:45 +00:00
|
|
|
|
2021-04-24 00:11:01 +00:00
|
|
|
#define CHECK_ARG_SIMPLE(value) \
|
|
|
|
{ \
|
|
|
|
using value_type = decltype(value); \
|
|
|
|
typename visit_type<value_type>::type expected = value; \
|
2024-09-04 21:42:25 +00:00
|
|
|
CHECK_ARG(expected, value) \
|
2019-01-13 02:27:38 +00:00
|
|
|
}
|
2016-12-11 21:22:45 +00:00
|
|
|
|
2021-04-24 00:11:01 +00:00
|
|
|
template <typename T> class numeric_arg_test : public testing::Test {};
|
2016-12-11 21:22:45 +00:00
|
|
|
|
2024-08-25 16:20:40 +00:00
|
|
|
#if FMT_BUILTIN_TYPES
|
2022-06-09 23:57:52 +00:00
|
|
|
using test_types =
|
2021-04-24 18:35:49 +00:00
|
|
|
testing::Types<bool, signed char, unsigned char, short, unsigned short, int,
|
|
|
|
unsigned, long, unsigned long, long long, unsigned long long,
|
|
|
|
float, double, long double>;
|
2024-08-25 16:20:40 +00:00
|
|
|
#else
|
|
|
|
using test_types = testing::Types<int>;
|
|
|
|
#endif
|
2022-06-09 23:57:52 +00:00
|
|
|
TYPED_TEST_SUITE(numeric_arg_test, test_types);
|
2016-12-11 21:22:45 +00:00
|
|
|
|
2021-04-24 00:11:01 +00:00
|
|
|
template <typename T, fmt::enable_if_t<std::is_integral<T>::value, int> = 0>
|
2023-01-02 17:45:51 +00:00
|
|
|
auto test_value() -> T {
|
2016-12-11 21:22:45 +00:00
|
|
|
return static_cast<T>(42);
|
|
|
|
}
|
|
|
|
|
2021-04-24 00:11:01 +00:00
|
|
|
template <typename T,
|
|
|
|
fmt::enable_if_t<std::is_floating_point<T>::value, int> = 0>
|
2023-01-02 17:45:51 +00:00
|
|
|
auto test_value() -> T {
|
2016-12-11 21:22:45 +00:00
|
|
|
return static_cast<T>(4.2);
|
|
|
|
}
|
|
|
|
|
2021-04-24 00:11:01 +00:00
|
|
|
TYPED_TEST(numeric_arg_test, make_and_visit) {
|
|
|
|
CHECK_ARG_SIMPLE(test_value<TypeParam>());
|
|
|
|
CHECK_ARG_SIMPLE(std::numeric_limits<TypeParam>::min());
|
|
|
|
CHECK_ARG_SIMPLE(std::numeric_limits<TypeParam>::max());
|
2016-12-11 21:22:45 +00:00
|
|
|
}
|
|
|
|
|
2024-09-04 21:42:25 +00:00
|
|
|
TEST(arg_test, char_arg) { CHECK_ARG('a', 'a'); }
|
2016-12-11 21:22:45 +00:00
|
|
|
|
2021-04-24 00:11:01 +00:00
|
|
|
TEST(arg_test, string_arg) {
|
2016-12-11 21:22:45 +00:00
|
|
|
char str_data[] = "test";
|
2019-01-13 02:27:38 +00:00
|
|
|
char* str = str_data;
|
|
|
|
const char* cstr = str;
|
2024-09-04 21:42:25 +00:00
|
|
|
CHECK_ARG(cstr, str);
|
2016-12-11 21:22:45 +00:00
|
|
|
|
2021-04-24 00:11:01 +00:00
|
|
|
auto sv = fmt::string_view(str);
|
2024-09-04 21:42:25 +00:00
|
|
|
CHECK_ARG(sv, std::string(str));
|
2016-12-11 21:22:45 +00:00
|
|
|
}
|
|
|
|
|
2021-04-24 00:11:01 +00:00
|
|
|
TEST(arg_test, pointer_arg) {
|
2019-05-30 14:01:31 +00:00
|
|
|
void* p = nullptr;
|
|
|
|
const void* cp = nullptr;
|
2024-09-04 21:42:25 +00:00
|
|
|
CHECK_ARG(cp, p);
|
2021-04-24 00:11:01 +00:00
|
|
|
CHECK_ARG_SIMPLE(cp);
|
2016-12-11 21:22:45 +00:00
|
|
|
}
|
|
|
|
|
2024-07-10 15:35:06 +00:00
|
|
|
TEST(arg_test, volatile_pointer_arg) {
|
|
|
|
const void* p = nullptr;
|
|
|
|
volatile int* vip = nullptr;
|
|
|
|
const volatile int* cvip = nullptr;
|
2024-09-04 21:42:25 +00:00
|
|
|
CHECK_ARG(p, static_cast<volatile void*>(vip));
|
|
|
|
CHECK_ARG(p, static_cast<const volatile void*>(cvip));
|
2024-07-10 15:35:06 +00:00
|
|
|
}
|
|
|
|
|
2018-03-03 22:04:59 +00:00
|
|
|
struct check_custom {
|
2023-01-02 17:45:51 +00:00
|
|
|
auto operator()(fmt::basic_format_arg<fmt::format_context>::handle h) const
|
|
|
|
-> test_result {
|
2020-10-16 00:41:56 +00:00
|
|
|
struct test_buffer final : fmt::detail::buffer<char> {
|
2018-09-19 15:55:45 +00:00
|
|
|
char data[10];
|
2024-01-02 15:02:20 +00:00
|
|
|
test_buffer()
|
|
|
|
: fmt::detail::buffer<char>([](buffer<char>&, size_t) {}, data, 0,
|
|
|
|
10) {}
|
2018-09-19 15:55:45 +00:00
|
|
|
} buffer;
|
2021-04-24 00:11:01 +00:00
|
|
|
auto parse_ctx = fmt::format_parse_context("");
|
2024-01-08 06:03:02 +00:00
|
|
|
auto ctx = fmt::format_context(fmt::appender(buffer), fmt::format_args());
|
2019-02-10 03:34:42 +00:00
|
|
|
h.format(parse_ctx, ctx);
|
2018-09-19 15:55:45 +00:00
|
|
|
EXPECT_EQ("test", std::string(buffer.data, buffer.size()));
|
|
|
|
return test_result();
|
2018-03-03 22:04:59 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-04-24 00:11:01 +00:00
|
|
|
TEST(arg_test, custom_arg) {
|
|
|
|
auto test = test_struct();
|
2020-07-09 15:41:05 +00:00
|
|
|
using visitor =
|
2020-07-09 16:04:46 +00:00
|
|
|
mock_visitor<fmt::basic_format_arg<fmt::format_context>::handle>;
|
2023-01-02 17:45:51 +00:00
|
|
|
auto&& v = testing::StrictMock<visitor>();
|
2020-07-11 13:53:25 +00:00
|
|
|
EXPECT_CALL(v, visit(_)).WillOnce(Invoke(check_custom()));
|
2024-09-04 21:42:25 +00:00
|
|
|
fmt::basic_format_arg<fmt::format_context>(test).visit(v);
|
2014-07-14 13:55:29 +00:00
|
|
|
}
|
|
|
|
|
2021-04-24 00:11:01 +00:00
|
|
|
TEST(arg_test, visit_invalid_arg) {
|
2023-01-02 17:45:51 +00:00
|
|
|
auto&& visitor = testing::StrictMock<mock_visitor<fmt::monostate>>();
|
2016-12-27 15:43:25 +00:00
|
|
|
EXPECT_CALL(visitor, visit(_));
|
2024-01-02 01:31:36 +00:00
|
|
|
fmt::basic_format_arg<fmt::format_context>().visit(visitor);
|
2014-07-16 14:55:31 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 23:08:56 +00:00
|
|
|
#if FMT_USE_CONSTEXPR
|
|
|
|
|
2024-07-21 16:38:00 +00:00
|
|
|
enum class arg_id_result { none, index, name };
|
2021-05-19 23:08:56 +00:00
|
|
|
struct test_arg_id_handler {
|
|
|
|
arg_id_result res = arg_id_result::none;
|
|
|
|
int index = 0;
|
|
|
|
string_view name;
|
|
|
|
|
2022-12-25 00:27:29 +00:00
|
|
|
constexpr void on_index(int i) {
|
2021-05-19 23:08:56 +00:00
|
|
|
res = arg_id_result::index;
|
|
|
|
index = i;
|
|
|
|
}
|
|
|
|
|
2022-12-25 00:27:29 +00:00
|
|
|
constexpr void on_name(string_view n) {
|
2021-05-19 23:08:56 +00:00
|
|
|
res = arg_id_result::name;
|
|
|
|
name = n;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <size_t N>
|
|
|
|
constexpr test_arg_id_handler parse_arg_id(const char (&s)[N]) {
|
2022-12-25 00:27:29 +00:00
|
|
|
auto h = test_arg_id_handler();
|
2021-05-19 23:08:56 +00:00
|
|
|
fmt::detail::parse_arg_id(s, s + N, h);
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
2024-06-22 18:10:54 +00:00
|
|
|
TEST(base_test, constexpr_parse_arg_id) {
|
2021-05-19 23:08:56 +00:00
|
|
|
static_assert(parse_arg_id("42:").res == arg_id_result::index, "");
|
|
|
|
static_assert(parse_arg_id("42:").index == 42, "");
|
|
|
|
static_assert(parse_arg_id("foo:").res == arg_id_result::name, "");
|
|
|
|
static_assert(parse_arg_id("foo:").name.size() == 3, "");
|
|
|
|
}
|
|
|
|
|
2022-12-25 03:22:39 +00:00
|
|
|
template <size_t N> constexpr auto parse_test_specs(const char (&s)[N]) {
|
|
|
|
auto ctx = fmt::detail::compile_parse_context<char>(fmt::string_view(s, N),
|
|
|
|
43, nullptr);
|
2022-12-25 20:31:38 +00:00
|
|
|
auto specs = fmt::detail::dynamic_format_specs<>();
|
|
|
|
fmt::detail::parse_format_specs(s, s + N - 1, specs, ctx,
|
|
|
|
fmt::detail::type::float_type);
|
|
|
|
return specs;
|
2021-05-19 23:08:56 +00:00
|
|
|
}
|
|
|
|
|
2024-06-22 18:10:54 +00:00
|
|
|
TEST(base_test, constexpr_parse_format_specs) {
|
2024-08-11 17:13:17 +00:00
|
|
|
static_assert(parse_test_specs("<").align() == fmt::align::left, "");
|
|
|
|
static_assert(parse_test_specs("*^").fill_unit<char>() == '*', "");
|
|
|
|
static_assert(parse_test_specs("+").sign() == fmt::sign::plus, "");
|
|
|
|
static_assert(parse_test_specs("-").sign() == fmt::sign::none, "");
|
|
|
|
static_assert(parse_test_specs(" ").sign() == fmt::sign::space, "");
|
|
|
|
static_assert(parse_test_specs("#").alt(), "");
|
|
|
|
static_assert(parse_test_specs("0").align() == fmt::align::numeric, "");
|
|
|
|
static_assert(parse_test_specs("L").localized(), "");
|
2021-05-19 23:08:56 +00:00
|
|
|
static_assert(parse_test_specs("42").width == 42, "");
|
2024-08-10 15:10:55 +00:00
|
|
|
static_assert(parse_test_specs("{42}").width_ref.index == 42, "");
|
2021-05-19 23:08:56 +00:00
|
|
|
static_assert(parse_test_specs(".42").precision == 42, "");
|
2024-08-10 15:10:55 +00:00
|
|
|
static_assert(parse_test_specs(".{42}").precision_ref.index == 42, "");
|
2024-08-11 17:13:17 +00:00
|
|
|
static_assert(parse_test_specs("f").type() == fmt::presentation_type::fixed,
|
2024-06-22 14:16:43 +00:00
|
|
|
"");
|
2021-05-19 23:08:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct test_format_string_handler {
|
|
|
|
constexpr void on_text(const char*, const char*) {}
|
|
|
|
|
2023-01-02 17:45:51 +00:00
|
|
|
constexpr auto on_arg_id() -> int { return 0; }
|
2021-05-19 23:08:56 +00:00
|
|
|
|
2023-01-02 17:45:51 +00:00
|
|
|
template <typename T> constexpr auto on_arg_id(T) -> int { return 0; }
|
2021-05-19 23:08:56 +00:00
|
|
|
|
|
|
|
constexpr void on_replacement_field(int, const char*) {}
|
|
|
|
|
2023-01-02 17:45:51 +00:00
|
|
|
constexpr auto on_format_specs(int, const char* begin, const char*) -> const
|
|
|
|
char* {
|
2021-05-19 23:08:56 +00:00
|
|
|
return begin;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr void on_error(const char*) { error = true; }
|
|
|
|
|
|
|
|
bool error = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <size_t N> constexpr bool parse_string(const char (&s)[N]) {
|
|
|
|
auto h = test_format_string_handler();
|
2024-09-01 16:34:38 +00:00
|
|
|
fmt::detail::parse_format_string(fmt::string_view(s, N - 1), h);
|
2021-05-19 23:08:56 +00:00
|
|
|
return !h.error;
|
|
|
|
}
|
|
|
|
|
2024-06-22 18:10:54 +00:00
|
|
|
TEST(base_test, constexpr_parse_format_string) {
|
2021-05-19 23:08:56 +00:00
|
|
|
static_assert(parse_string("foo"), "");
|
|
|
|
static_assert(!parse_string("}"), "");
|
|
|
|
static_assert(parse_string("{}"), "");
|
|
|
|
static_assert(parse_string("{42}"), "");
|
|
|
|
static_assert(parse_string("{foo}"), "");
|
|
|
|
static_assert(parse_string("{:}"), "");
|
|
|
|
}
|
|
|
|
#endif // FMT_USE_CONSTEXPR
|
|
|
|
|
2019-06-07 14:08:04 +00:00
|
|
|
struct enabled_formatter {};
|
2022-04-12 13:45:46 +00:00
|
|
|
struct enabled_ptr_formatter {};
|
2019-06-07 14:08:04 +00:00
|
|
|
struct disabled_formatter {};
|
|
|
|
struct disabled_formatter_convertible {
|
|
|
|
operator int() const { return 42; }
|
|
|
|
};
|
|
|
|
|
|
|
|
FMT_BEGIN_NAMESPACE
|
|
|
|
template <> struct formatter<enabled_formatter> {
|
2023-07-14 00:17:00 +00:00
|
|
|
FMT_CONSTEXPR auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) {
|
2019-06-07 14:08:04 +00:00
|
|
|
return ctx.begin();
|
|
|
|
}
|
2023-01-02 17:45:51 +00:00
|
|
|
auto format(enabled_formatter, format_context& ctx) const
|
|
|
|
-> decltype(ctx.out()) {
|
2019-06-07 14:08:04 +00:00
|
|
|
return ctx.out();
|
|
|
|
}
|
|
|
|
};
|
2022-04-12 13:45:46 +00:00
|
|
|
|
|
|
|
template <> struct formatter<enabled_ptr_formatter*> {
|
2023-07-14 00:17:00 +00:00
|
|
|
FMT_CONSTEXPR auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) {
|
2022-04-12 13:45:46 +00:00
|
|
|
return ctx.begin();
|
|
|
|
}
|
2023-01-02 17:45:51 +00:00
|
|
|
auto format(enabled_ptr_formatter*, format_context& ctx) const
|
2022-04-12 13:45:46 +00:00
|
|
|
-> decltype(ctx.out()) {
|
|
|
|
return ctx.out();
|
|
|
|
}
|
|
|
|
};
|
2019-06-07 14:08:04 +00:00
|
|
|
FMT_END_NAMESPACE
|
|
|
|
|
2021-08-27 01:47:59 +00:00
|
|
|
struct const_formattable {};
|
|
|
|
struct nonconst_formattable {};
|
|
|
|
|
|
|
|
FMT_BEGIN_NAMESPACE
|
|
|
|
template <> struct formatter<const_formattable> {
|
2023-07-14 00:17:00 +00:00
|
|
|
FMT_CONSTEXPR auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) {
|
2021-08-27 01:47:59 +00:00
|
|
|
return ctx.begin();
|
|
|
|
}
|
|
|
|
|
2024-01-14 14:34:25 +00:00
|
|
|
auto format(const const_formattable&, format_context& ctx) const
|
2021-08-27 01:47:59 +00:00
|
|
|
-> decltype(ctx.out()) {
|
2024-01-03 04:56:51 +00:00
|
|
|
return copy("test", ctx.out());
|
2021-08-27 01:47:59 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <> struct formatter<nonconst_formattable> {
|
2023-07-14 00:17:00 +00:00
|
|
|
FMT_CONSTEXPR auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) {
|
2021-08-27 01:47:59 +00:00
|
|
|
return ctx.begin();
|
|
|
|
}
|
|
|
|
|
2024-01-14 14:34:25 +00:00
|
|
|
auto format(nonconst_formattable&, format_context& ctx) const
|
2021-08-27 01:47:59 +00:00
|
|
|
-> decltype(ctx.out()) {
|
2024-01-03 04:56:51 +00:00
|
|
|
return copy("test", ctx.out());
|
2021-08-27 01:47:59 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
FMT_END_NAMESPACE
|
|
|
|
|
2021-11-13 16:26:27 +00:00
|
|
|
struct convertible_to_pointer {
|
|
|
|
operator const int*() const { return nullptr; }
|
|
|
|
};
|
|
|
|
|
2022-01-14 21:08:14 +00:00
|
|
|
struct convertible_to_pointer_formattable {
|
|
|
|
operator const int*() const { return nullptr; }
|
|
|
|
};
|
|
|
|
|
|
|
|
FMT_BEGIN_NAMESPACE
|
|
|
|
template <> struct formatter<convertible_to_pointer_formattable> {
|
2023-07-14 00:17:00 +00:00
|
|
|
FMT_CONSTEXPR auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) {
|
2022-01-14 21:08:14 +00:00
|
|
|
return ctx.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto format(convertible_to_pointer_formattable, format_context& ctx) const
|
|
|
|
-> decltype(ctx.out()) {
|
2024-01-03 04:56:51 +00:00
|
|
|
return copy("test", ctx.out());
|
2022-01-14 21:08:14 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
FMT_END_NAMESPACE
|
|
|
|
|
2022-01-28 14:31:29 +00:00
|
|
|
enum class unformattable_scoped_enum {};
|
|
|
|
|
2024-06-22 18:10:54 +00:00
|
|
|
TEST(base_test, is_formattable) {
|
2024-09-04 14:33:56 +00:00
|
|
|
EXPECT_FALSE(fmt::is_formattable<void>::value);
|
2024-09-04 01:31:39 +00:00
|
|
|
EXPECT_FALSE(fmt::is_formattable<wchar_t>::value);
|
2021-10-17 15:58:41 +00:00
|
|
|
#ifdef __cpp_char8_t
|
2024-09-04 01:31:39 +00:00
|
|
|
EXPECT_FALSE(fmt::is_formattable<char8_t>::value);
|
2021-10-17 15:58:41 +00:00
|
|
|
#endif
|
2024-09-04 01:31:39 +00:00
|
|
|
EXPECT_FALSE(fmt::is_formattable<char16_t>::value);
|
|
|
|
EXPECT_FALSE(fmt::is_formattable<char32_t>::value);
|
|
|
|
EXPECT_FALSE(fmt::is_formattable<signed char*>::value);
|
|
|
|
EXPECT_FALSE(fmt::is_formattable<unsigned char*>::value);
|
|
|
|
EXPECT_FALSE(fmt::is_formattable<const signed char*>::value);
|
|
|
|
EXPECT_FALSE(fmt::is_formattable<const unsigned char*>::value);
|
|
|
|
EXPECT_FALSE(fmt::is_formattable<const wchar_t*>::value);
|
|
|
|
EXPECT_FALSE(fmt::is_formattable<const wchar_t[3]>::value);
|
|
|
|
EXPECT_FALSE(fmt::is_formattable<fmt::basic_string_view<wchar_t>>::value);
|
|
|
|
EXPECT_FALSE(fmt::is_formattable<enabled_ptr_formatter*>::value);
|
|
|
|
EXPECT_FALSE(fmt::is_formattable<disabled_formatter>::value);
|
|
|
|
EXPECT_FALSE(fmt::is_formattable<disabled_formatter_convertible>::value);
|
|
|
|
|
|
|
|
EXPECT_TRUE(fmt::is_formattable<enabled_formatter>::value);
|
|
|
|
EXPECT_TRUE(fmt::is_formattable<const_formattable&>::value);
|
|
|
|
EXPECT_TRUE(fmt::is_formattable<const const_formattable&>::value);
|
|
|
|
|
|
|
|
EXPECT_TRUE(fmt::is_formattable<nonconst_formattable&>::value);
|
2022-05-29 22:01:43 +00:00
|
|
|
#if !FMT_MSC_VERSION || FMT_MSC_VERSION >= 1910
|
2024-09-04 01:31:39 +00:00
|
|
|
EXPECT_FALSE(fmt::is_formattable<const nonconst_formattable&>::value);
|
2021-08-27 01:47:59 +00:00
|
|
|
#endif
|
|
|
|
|
2024-09-04 01:31:39 +00:00
|
|
|
EXPECT_FALSE(fmt::is_formattable<convertible_to_pointer>::value);
|
2022-01-14 21:08:14 +00:00
|
|
|
const auto f = convertible_to_pointer_formattable();
|
2024-01-10 14:07:01 +00:00
|
|
|
auto str = std::string();
|
|
|
|
fmt::format_to(std::back_inserter(str), "{}", f);
|
|
|
|
EXPECT_EQ(str, "test");
|
2021-11-13 16:26:27 +00:00
|
|
|
|
2024-09-04 01:31:39 +00:00
|
|
|
EXPECT_FALSE(fmt::is_formattable<void (*)()>::value);
|
2021-11-23 20:55:22 +00:00
|
|
|
|
|
|
|
struct s;
|
2024-09-04 01:31:39 +00:00
|
|
|
EXPECT_FALSE(fmt::is_formattable<int(s::*)>::value);
|
|
|
|
EXPECT_FALSE(fmt::is_formattable<int (s::*)()>::value);
|
|
|
|
EXPECT_FALSE(fmt::is_formattable<unformattable_scoped_enum>::value);
|
|
|
|
EXPECT_FALSE(fmt::is_formattable<unformattable_scoped_enum>::value);
|
2021-02-28 23:25:33 +00:00
|
|
|
}
|
|
|
|
|
2024-09-01 19:43:01 +00:00
|
|
|
#ifdef __cpp_concepts
|
|
|
|
TEST(base_test, formattable_concept) {
|
2024-05-26 14:47:56 +00:00
|
|
|
static_assert(fmt::formattable<char>);
|
|
|
|
static_assert(fmt::formattable<char&>);
|
|
|
|
static_assert(fmt::formattable<char&&>);
|
|
|
|
static_assert(fmt::formattable<const char>);
|
|
|
|
static_assert(fmt::formattable<const char&>);
|
|
|
|
static_assert(fmt::formattable<const char&&>);
|
2024-06-05 15:38:00 +00:00
|
|
|
static_assert(fmt::formattable<int>);
|
2024-05-26 14:47:56 +00:00
|
|
|
static_assert(!fmt::formattable<wchar_t>);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2024-06-22 18:10:54 +00:00
|
|
|
TEST(base_test, format_to) {
|
2023-01-02 17:45:51 +00:00
|
|
|
auto s = std::string();
|
2021-04-25 23:19:01 +00:00
|
|
|
fmt::format_to(std::back_inserter(s), "{}", 42);
|
|
|
|
EXPECT_EQ(s, "42");
|
|
|
|
}
|
|
|
|
|
2024-06-22 18:10:54 +00:00
|
|
|
TEST(base_test, format_to_array) {
|
2024-01-16 15:01:15 +00:00
|
|
|
char buffer[4];
|
|
|
|
auto result = fmt::format_to(buffer, "{}", 12345);
|
|
|
|
EXPECT_EQ(4, std::distance(&buffer[0], result.out));
|
2024-03-09 16:20:27 +00:00
|
|
|
EXPECT_TRUE(result.truncated);
|
2024-01-16 15:01:15 +00:00
|
|
|
EXPECT_EQ(buffer + 4, result.out);
|
|
|
|
EXPECT_EQ("1234", fmt::string_view(buffer, 4));
|
|
|
|
|
2024-06-22 14:16:43 +00:00
|
|
|
char* out = nullptr;
|
|
|
|
EXPECT_THROW(out = result, std::runtime_error);
|
|
|
|
(void)out;
|
|
|
|
|
2024-01-16 15:01:15 +00:00
|
|
|
result = fmt::format_to(buffer, "{:s}", "foobar");
|
|
|
|
EXPECT_EQ(4, std::distance(&buffer[0], result.out));
|
2024-03-09 16:20:27 +00:00
|
|
|
EXPECT_TRUE(result.truncated);
|
2024-01-16 15:01:15 +00:00
|
|
|
EXPECT_EQ(buffer + 4, result.out);
|
|
|
|
EXPECT_EQ("foob", fmt::string_view(buffer, 4));
|
|
|
|
|
|
|
|
buffer[0] = 'x';
|
|
|
|
buffer[1] = 'x';
|
|
|
|
buffer[2] = 'x';
|
|
|
|
buffer[3] = 'x';
|
|
|
|
result = fmt::format_to(buffer, "{}", 'A');
|
|
|
|
EXPECT_EQ(1, std::distance(&buffer[0], result.out));
|
2024-03-09 16:20:27 +00:00
|
|
|
EXPECT_FALSE(result.truncated);
|
2024-01-16 15:01:15 +00:00
|
|
|
EXPECT_EQ(buffer + 1, result.out);
|
|
|
|
EXPECT_EQ("Axxx", fmt::string_view(buffer, 4));
|
|
|
|
|
|
|
|
result = fmt::format_to(buffer, "{}{} ", 'B', 'C');
|
|
|
|
EXPECT_EQ(3, std::distance(&buffer[0], result.out));
|
2024-03-09 16:20:27 +00:00
|
|
|
EXPECT_FALSE(result.truncated);
|
2024-01-16 15:01:15 +00:00
|
|
|
EXPECT_EQ(buffer + 3, result.out);
|
|
|
|
EXPECT_EQ("BC x", fmt::string_view(buffer, 4));
|
|
|
|
|
|
|
|
result = fmt::format_to(buffer, "{}", "ABCDE");
|
|
|
|
EXPECT_EQ(4, std::distance(&buffer[0], result.out));
|
2024-03-09 16:20:27 +00:00
|
|
|
EXPECT_TRUE(result.truncated);
|
2024-01-16 15:01:15 +00:00
|
|
|
EXPECT_EQ("ABCD", fmt::string_view(buffer, 4));
|
|
|
|
|
2024-08-25 16:20:40 +00:00
|
|
|
result = fmt::format_to(buffer, "{}", std::string(1000, '*').c_str());
|
2024-01-16 15:01:15 +00:00
|
|
|
EXPECT_EQ(4, std::distance(&buffer[0], result.out));
|
2024-03-09 16:20:27 +00:00
|
|
|
EXPECT_TRUE(result.truncated);
|
2024-01-16 15:01:15 +00:00
|
|
|
EXPECT_EQ("****", fmt::string_view(buffer, 4));
|
|
|
|
}
|
|
|
|
|
2021-04-25 23:19:01 +00:00
|
|
|
// Test that check is not found by ADL.
|
2021-03-18 03:59:36 +00:00
|
|
|
template <typename T> void check(T);
|
2024-06-22 18:10:54 +00:00
|
|
|
TEST(base_test, adl_check) {
|
2024-01-10 14:07:01 +00:00
|
|
|
auto s = std::string();
|
|
|
|
fmt::format_to(std::back_inserter(s), "{}", test_struct());
|
|
|
|
EXPECT_EQ(s, "test");
|
2018-10-25 01:42:42 +00:00
|
|
|
}
|
|
|
|
|
2018-10-28 00:10:19 +00:00
|
|
|
struct implicitly_convertible_to_string_view {
|
|
|
|
operator fmt::string_view() const { return "foo"; }
|
|
|
|
};
|
|
|
|
|
2024-06-22 18:10:54 +00:00
|
|
|
TEST(base_test, no_implicit_conversion_to_string_view) {
|
2023-01-03 19:57:37 +00:00
|
|
|
EXPECT_FALSE(
|
|
|
|
fmt::is_formattable<implicitly_convertible_to_string_view>::value);
|
2018-10-28 00:10:19 +00:00
|
|
|
}
|
|
|
|
|
2023-01-03 19:57:37 +00:00
|
|
|
#ifdef FMT_USE_STRING_VIEW
|
|
|
|
struct implicitly_convertible_to_std_string_view {
|
|
|
|
operator std::string_view() const { return "foo"; }
|
|
|
|
};
|
|
|
|
|
2024-06-22 18:10:54 +00:00
|
|
|
TEST(base_test, no_implicit_conversion_to_std_string_view) {
|
2023-01-03 19:57:37 +00:00
|
|
|
EXPECT_FALSE(
|
|
|
|
fmt::is_formattable<implicitly_convertible_to_std_string_view>::value);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-10-28 00:10:19 +00:00
|
|
|
// std::is_constructible is broken in MSVC until version 2015.
|
2022-05-29 22:01:43 +00:00
|
|
|
#if !FMT_MSC_VERSION || FMT_MSC_VERSION >= 1900
|
2018-10-28 00:10:19 +00:00
|
|
|
struct explicitly_convertible_to_string_view {
|
|
|
|
explicit operator fmt::string_view() const { return "foo"; }
|
|
|
|
};
|
|
|
|
|
2024-06-22 18:10:54 +00:00
|
|
|
TEST(base_test, format_explicitly_convertible_to_string_view) {
|
2022-02-04 19:12:04 +00:00
|
|
|
// Types explicitly convertible to string_view are not formattable by
|
|
|
|
// default because it may introduce ODR violations.
|
|
|
|
static_assert(
|
|
|
|
!fmt::is_formattable<explicitly_convertible_to_string_view>::value, "");
|
2018-10-28 00:10:19 +00:00
|
|
|
}
|
|
|
|
|
2019-12-21 21:10:45 +00:00
|
|
|
# ifdef FMT_USE_STRING_VIEW
|
2019-10-05 00:21:10 +00:00
|
|
|
struct explicitly_convertible_to_std_string_view {
|
|
|
|
explicit operator std::string_view() const { return "foo"; }
|
|
|
|
};
|
|
|
|
|
2024-06-22 18:10:54 +00:00
|
|
|
TEST(base_test, format_explicitly_convertible_to_std_string_view) {
|
2022-02-04 19:12:04 +00:00
|
|
|
// Types explicitly convertible to string_view are not formattable by
|
|
|
|
// default because it may introduce ODR violations.
|
|
|
|
static_assert(
|
|
|
|
!fmt::is_formattable<explicitly_convertible_to_std_string_view>::value,
|
|
|
|
"");
|
2019-10-05 00:21:10 +00:00
|
|
|
}
|
2019-12-21 21:10:45 +00:00
|
|
|
# endif
|
2018-10-28 00:10:19 +00:00
|
|
|
#endif
|
2019-11-25 16:30:47 +00:00
|
|
|
|
2024-09-17 02:48:02 +00:00
|
|
|
TEST(base_test, has_formatter) {
|
|
|
|
EXPECT_TRUE((fmt::detail::has_formatter<const const_formattable, char>()));
|
2024-09-04 01:31:39 +00:00
|
|
|
EXPECT_FALSE(
|
2024-09-17 02:48:02 +00:00
|
|
|
(fmt::detail::has_formatter<const nonconst_formattable, char>()));
|
2021-07-24 15:02:23 +00:00
|
|
|
}
|
|
|
|
|
2024-06-22 18:10:54 +00:00
|
|
|
TEST(base_test, format_nonconst) {
|
2024-01-10 14:07:01 +00:00
|
|
|
auto s = std::string();
|
|
|
|
fmt::format_to(std::back_inserter(s), "{}", nonconst_formattable());
|
|
|
|
EXPECT_EQ(s, "test");
|
2023-06-26 22:46:12 +00:00
|
|
|
}
|
2024-01-07 22:00:26 +00:00
|
|
|
|
2024-06-22 18:10:54 +00:00
|
|
|
TEST(base_test, throw_in_buffer_dtor) {
|
2024-01-07 22:00:26 +00:00
|
|
|
enum { buffer_size = 256 };
|
|
|
|
|
|
|
|
struct throwing_iterator {
|
|
|
|
int& count;
|
|
|
|
|
|
|
|
auto operator=(char) -> throwing_iterator& {
|
|
|
|
if (++count > buffer_size) throw std::exception();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
auto operator*() -> throwing_iterator& { return *this; }
|
|
|
|
auto operator++() -> throwing_iterator& { return *this; }
|
|
|
|
auto operator++(int) -> throwing_iterator { return *this; }
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
int count = 0;
|
|
|
|
fmt::format_to(throwing_iterator{count}, fmt::runtime("{:{}}{"), "",
|
|
|
|
buffer_size + 1);
|
|
|
|
} catch (const std::exception&) {
|
|
|
|
}
|
|
|
|
}
|
2024-01-10 14:07:01 +00:00
|
|
|
|
|
|
|
struct its_a_trap {
|
|
|
|
template <typename T> operator T() const {
|
|
|
|
auto v = T();
|
|
|
|
v.x = 42;
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
FMT_BEGIN_NAMESPACE
|
|
|
|
template <> struct formatter<its_a_trap> {
|
|
|
|
FMT_CONSTEXPR auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) {
|
|
|
|
return ctx.begin();
|
|
|
|
}
|
|
|
|
|
2024-01-14 14:34:25 +00:00
|
|
|
auto format(its_a_trap, format_context& ctx) const
|
|
|
|
-> decltype(ctx.out()) const {
|
2024-01-10 14:07:01 +00:00
|
|
|
auto out = ctx.out();
|
|
|
|
*out++ = 'x';
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
FMT_END_NAMESPACE
|
|
|
|
|
2024-06-22 18:10:54 +00:00
|
|
|
TEST(base_test, trappy_conversion) {
|
2024-01-10 14:07:01 +00:00
|
|
|
auto s = std::string();
|
|
|
|
fmt::format_to(std::back_inserter(s), "{}", its_a_trap());
|
|
|
|
EXPECT_EQ(s, "x");
|
|
|
|
}
|
2024-08-09 17:18:11 +00:00
|
|
|
|
|
|
|
struct custom_container {
|
|
|
|
char data;
|
|
|
|
|
|
|
|
using value_type = char;
|
|
|
|
|
|
|
|
size_t size() const { return 0; }
|
|
|
|
void resize(size_t) {}
|
|
|
|
|
|
|
|
void push_back(char) {}
|
|
|
|
char& operator[](size_t) { return data; }
|
|
|
|
};
|
|
|
|
|
|
|
|
FMT_BEGIN_NAMESPACE
|
|
|
|
template <> struct is_contiguous<custom_container> : std::true_type {};
|
|
|
|
FMT_END_NAMESPACE
|
|
|
|
|
|
|
|
TEST(base_test, format_to_custom_container) {
|
|
|
|
auto c = custom_container();
|
|
|
|
fmt::format_to(std::back_inserter(c), "");
|
|
|
|
}
|
2024-09-28 15:20:24 +00:00
|
|
|
|
|
|
|
struct nondeterministic_format_string {
|
|
|
|
mutable int i = 0;
|
|
|
|
FMT_CONSTEXPR operator string_view() const {
|
|
|
|
return string_view("{}", i++ != 0 ? 2 : 0);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(base_test, no_repeated_format_string_conversions) {
|
|
|
|
#if !FMT_GCC_VERSION
|
|
|
|
char buf[10];
|
|
|
|
fmt::format_to(buf, nondeterministic_format_string());
|
|
|
|
#endif
|
|
|
|
}
|