2018-03-04 17:16:51 +00:00
|
|
|
// Formatting library for C++ - utility tests
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
2015-06-22 15:17:23 +00:00
|
|
|
#include "test-assert.h"
|
|
|
|
|
2014-07-02 15:38:31 +00:00
|
|
|
#include <cfloat>
|
|
|
|
#include <climits>
|
2014-06-06 13:38:37 +00:00
|
|
|
#include <cstring>
|
2015-10-22 15:56:52 +00:00
|
|
|
#include <functional>
|
2014-06-06 15:07:05 +00:00
|
|
|
#include <limits>
|
2014-09-29 15:48:16 +00:00
|
|
|
|
|
|
|
#if FMT_USE_TYPE_TRAITS
|
|
|
|
# include <type_traits>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "gmock/gmock.h"
|
2014-06-06 13:38:37 +00:00
|
|
|
#include "gtest-extra.h"
|
2014-09-29 15:48:16 +00:00
|
|
|
#include "mock-allocator.h"
|
2014-06-06 13:38:37 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
2014-06-06 14:29:57 +00:00
|
|
|
// Check if format.h compiles with windows.h included.
|
|
|
|
#ifdef _WIN32
|
|
|
|
# include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
2017-12-06 15:42:42 +00:00
|
|
|
#include "fmt/core.h"
|
2014-06-06 14:29:57 +00:00
|
|
|
|
2016-12-11 21:22:45 +00:00
|
|
|
#undef min
|
2014-06-06 15:18:53 +00:00
|
|
|
#undef max
|
|
|
|
|
2018-04-04 14:38:21 +00:00
|
|
|
using fmt::basic_format_arg;
|
2018-01-14 20:25:03 +00:00
|
|
|
using fmt::internal::basic_buffer;
|
2017-02-18 17:13:12 +00:00
|
|
|
using fmt::basic_memory_buffer;
|
2017-02-18 14:52:52 +00:00
|
|
|
using fmt::string_view;
|
2018-04-22 00:26:24 +00:00
|
|
|
using fmt::internal::fp;
|
2016-12-30 16:05:26 +00:00
|
|
|
using fmt::internal::value;
|
2014-09-29 15:48:16 +00:00
|
|
|
|
2016-12-11 21:22:45 +00:00
|
|
|
using testing::_;
|
2014-09-29 15:48:16 +00:00
|
|
|
using testing::Return;
|
2014-09-30 14:30:27 +00:00
|
|
|
using testing::StrictMock;
|
2014-06-06 13:38:37 +00:00
|
|
|
|
|
|
|
namespace {
|
2014-07-04 14:18:44 +00:00
|
|
|
|
|
|
|
struct Test {};
|
2016-05-06 14:37:20 +00:00
|
|
|
|
2016-12-29 17:07:39 +00:00
|
|
|
template <typename Context, typename T>
|
2018-04-04 14:38:21 +00:00
|
|
|
basic_format_arg<Context> make_arg(const T &value) {
|
2016-12-29 17:07:39 +00:00
|
|
|
return fmt::internal::make_arg<Context>(value);
|
2014-09-23 14:59:43 +00:00
|
|
|
}
|
2014-07-29 14:50:05 +00:00
|
|
|
} // namespace
|
2014-06-06 13:38:37 +00:00
|
|
|
|
2018-05-12 15:33:51 +00:00
|
|
|
FMT_BEGIN_NAMESPACE
|
2017-08-13 20:09:02 +00:00
|
|
|
template <typename Char>
|
|
|
|
struct formatter<Test, Char> {
|
2017-09-16 23:50:40 +00:00
|
|
|
template <typename ParseContext>
|
|
|
|
auto parse(ParseContext &ctx) -> decltype(ctx.begin()) {
|
|
|
|
return ctx.begin();
|
2017-08-13 20:09:02 +00:00
|
|
|
}
|
|
|
|
|
2018-02-11 17:23:47 +00:00
|
|
|
typedef std::back_insert_iterator<basic_buffer<Char>> iterator;
|
2018-01-14 15:19:23 +00:00
|
|
|
|
2018-04-08 13:45:21 +00:00
|
|
|
auto format(Test, basic_format_context<iterator, char> &ctx)
|
2018-04-22 16:16:32 +00:00
|
|
|
-> decltype(ctx.out()) {
|
2017-08-13 20:09:02 +00:00
|
|
|
const Char *test = "test";
|
2018-04-22 16:16:32 +00:00
|
|
|
return std::copy_n(test, std::strlen(test), ctx.out());
|
2017-08-13 20:09:02 +00:00
|
|
|
}
|
|
|
|
};
|
2018-05-12 15:33:51 +00:00
|
|
|
FMT_END_NAMESPACE
|
2017-08-13 20:09:02 +00:00
|
|
|
|
2014-09-29 15:48:16 +00:00
|
|
|
void CheckForwarding(
|
|
|
|
MockAllocator<int> &alloc, AllocatorRef< MockAllocator<int> > &ref) {
|
|
|
|
int mem;
|
|
|
|
// Check if value_type is properly defined.
|
|
|
|
AllocatorRef< MockAllocator<int> >::value_type *ptr = &mem;
|
|
|
|
// Check forwarding.
|
|
|
|
EXPECT_CALL(alloc, allocate(42)).WillOnce(Return(ptr));
|
|
|
|
ref.allocate(42);
|
|
|
|
EXPECT_CALL(alloc, deallocate(ptr, 42));
|
|
|
|
ref.deallocate(ptr, 42);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(AllocatorTest, AllocatorRef) {
|
2014-09-30 14:30:27 +00:00
|
|
|
StrictMock< MockAllocator<int> > alloc;
|
2014-09-29 15:48:16 +00:00
|
|
|
typedef AllocatorRef< MockAllocator<int> > TestAllocatorRef;
|
|
|
|
TestAllocatorRef ref(&alloc);
|
|
|
|
// Check if AllocatorRef forwards to the underlying allocator.
|
|
|
|
CheckForwarding(alloc, ref);
|
|
|
|
TestAllocatorRef ref2(ref);
|
|
|
|
CheckForwarding(alloc, ref2);
|
|
|
|
TestAllocatorRef ref3;
|
|
|
|
EXPECT_EQ(0, ref3.get());
|
|
|
|
ref3 = ref;
|
|
|
|
CheckForwarding(alloc, ref3);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if FMT_USE_TYPE_TRAITS
|
2014-10-01 15:12:10 +00:00
|
|
|
TEST(BufferTest, Noncopyable) {
|
2017-02-14 17:08:37 +00:00
|
|
|
EXPECT_FALSE(std::is_copy_constructible<basic_buffer<char> >::value);
|
|
|
|
EXPECT_FALSE(std::is_copy_assignable<basic_buffer<char> >::value);
|
2014-09-29 15:48:16 +00:00
|
|
|
}
|
|
|
|
|
2014-10-01 15:12:10 +00:00
|
|
|
TEST(BufferTest, Nonmoveable) {
|
2017-02-14 17:08:37 +00:00
|
|
|
EXPECT_FALSE(std::is_move_constructible<basic_buffer<char> >::value);
|
|
|
|
EXPECT_FALSE(std::is_move_assignable<basic_buffer<char> >::value);
|
2014-09-29 15:48:16 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-10-01 15:12:10 +00:00
|
|
|
// A test buffer with a dummy grow method.
|
2014-09-30 14:30:27 +00:00
|
|
|
template <typename T>
|
2017-02-14 17:08:37 +00:00
|
|
|
struct TestBuffer : basic_buffer<T> {
|
2017-03-12 14:30:20 +00:00
|
|
|
void grow(std::size_t capacity) { this->set(0, capacity); }
|
2014-10-01 15:12:10 +00:00
|
|
|
};
|
2014-09-30 14:30:27 +00:00
|
|
|
|
2014-10-01 15:12:10 +00:00
|
|
|
template <typename T>
|
2017-02-14 17:08:37 +00:00
|
|
|
struct MockBuffer : basic_buffer<T> {
|
2017-03-12 14:30:20 +00:00
|
|
|
MOCK_METHOD1(do_grow, void (std::size_t capacity));
|
2014-10-01 16:32:31 +00:00
|
|
|
|
2017-03-12 14:30:20 +00:00
|
|
|
void grow(std::size_t capacity) {
|
|
|
|
this->set(this->data(), capacity);
|
|
|
|
do_grow(capacity);
|
2014-10-01 16:32:31 +00:00
|
|
|
}
|
2014-09-30 14:30:27 +00:00
|
|
|
|
|
|
|
MockBuffer() {}
|
2017-03-12 14:30:20 +00:00
|
|
|
MockBuffer(T *data) { this->set(data, 0); }
|
|
|
|
MockBuffer(T *data, std::size_t capacity) { this->set(data, capacity); }
|
2014-09-30 14:30:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST(BufferTest, Ctor) {
|
|
|
|
{
|
|
|
|
MockBuffer<int> buffer;
|
|
|
|
EXPECT_EQ(0, &buffer[0]);
|
2014-10-13 13:46:07 +00:00
|
|
|
EXPECT_EQ(0u, buffer.size());
|
|
|
|
EXPECT_EQ(0u, buffer.capacity());
|
2014-09-30 14:30:27 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
int dummy;
|
|
|
|
MockBuffer<int> buffer(&dummy);
|
|
|
|
EXPECT_EQ(&dummy, &buffer[0]);
|
2014-10-13 13:46:07 +00:00
|
|
|
EXPECT_EQ(0u, buffer.size());
|
|
|
|
EXPECT_EQ(0u, buffer.capacity());
|
2014-09-30 14:30:27 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
int dummy;
|
|
|
|
std::size_t capacity = std::numeric_limits<std::size_t>::max();
|
|
|
|
MockBuffer<int> buffer(&dummy, capacity);
|
|
|
|
EXPECT_EQ(&dummy, &buffer[0]);
|
2014-10-13 13:46:07 +00:00
|
|
|
EXPECT_EQ(0u, buffer.size());
|
2014-09-30 14:30:27 +00:00
|
|
|
EXPECT_EQ(capacity, buffer.capacity());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-01 15:24:47 +00:00
|
|
|
struct DyingBuffer : TestBuffer<int> {
|
|
|
|
MOCK_METHOD0(die, void());
|
|
|
|
~DyingBuffer() { die(); }
|
|
|
|
};
|
|
|
|
|
2014-09-30 14:30:27 +00:00
|
|
|
TEST(BufferTest, VirtualDtor) {
|
2014-10-01 15:12:10 +00:00
|
|
|
typedef StrictMock<DyingBuffer> StictMockBuffer;
|
2014-09-30 14:30:27 +00:00
|
|
|
StictMockBuffer *mock_buffer = new StictMockBuffer();
|
2014-10-01 15:12:10 +00:00
|
|
|
EXPECT_CALL(*mock_buffer, die());
|
2017-02-14 17:08:37 +00:00
|
|
|
basic_buffer<int> *buffer = mock_buffer;
|
2014-09-30 14:30:27 +00:00
|
|
|
delete buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(BufferTest, Access) {
|
|
|
|
char data[10];
|
2014-10-01 15:12:10 +00:00
|
|
|
MockBuffer<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));
|
2018-01-14 20:25:03 +00:00
|
|
|
const basic_buffer<char> &const_buffer = buffer;
|
2014-09-30 14:30:27 +00:00
|
|
|
EXPECT_EQ(42, const_buffer[3]);
|
|
|
|
}
|
|
|
|
|
2014-10-01 15:12:10 +00:00
|
|
|
TEST(BufferTest, Resize) {
|
|
|
|
char data[123];
|
|
|
|
MockBuffer<char> buffer(data, sizeof(data));
|
|
|
|
buffer[10] = 42;
|
|
|
|
EXPECT_EQ(42, buffer[10]);
|
|
|
|
buffer.resize(20);
|
|
|
|
EXPECT_EQ(20u, buffer.size());
|
|
|
|
EXPECT_EQ(123u, buffer.capacity());
|
|
|
|
EXPECT_EQ(42, buffer[10]);
|
|
|
|
buffer.resize(5);
|
|
|
|
EXPECT_EQ(5u, buffer.size());
|
|
|
|
EXPECT_EQ(123u, buffer.capacity());
|
|
|
|
EXPECT_EQ(42, buffer[10]);
|
|
|
|
// Check if resize calls grow.
|
2014-10-01 16:32:31 +00:00
|
|
|
EXPECT_CALL(buffer, do_grow(124));
|
2014-10-01 15:12:10 +00:00
|
|
|
buffer.resize(124);
|
2014-10-01 16:32:31 +00:00
|
|
|
EXPECT_CALL(buffer, do_grow(200));
|
2014-10-01 15:12:10 +00:00
|
|
|
buffer.resize(200);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(BufferTest, Clear) {
|
|
|
|
TestBuffer<char> buffer;
|
|
|
|
buffer.resize(20);
|
2017-03-11 15:43:26 +00:00
|
|
|
buffer.resize(0);
|
2014-10-01 15:12:10 +00:00
|
|
|
EXPECT_EQ(0u, buffer.size());
|
|
|
|
EXPECT_EQ(20u, buffer.capacity());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(BufferTest, Append) {
|
|
|
|
char data[15];
|
|
|
|
MockBuffer<char> buffer(data, 10);
|
|
|
|
const char *test = "test";
|
|
|
|
buffer.append(test, test + 5);
|
|
|
|
EXPECT_STREQ(test, &buffer[0]);
|
|
|
|
EXPECT_EQ(5u, buffer.size());
|
|
|
|
buffer.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());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(BufferTest, AppendAllocatesEnoughStorage) {
|
|
|
|
char data[19];
|
|
|
|
MockBuffer<char> buffer(data, 10);
|
|
|
|
const char *test = "abcdefgh";
|
|
|
|
buffer.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);
|
|
|
|
}
|
|
|
|
|
2014-09-29 15:48:16 +00:00
|
|
|
TEST(MemoryBufferTest, Ctor) {
|
2017-02-18 17:13:12 +00:00
|
|
|
basic_memory_buffer<char, 123> buffer;
|
2014-09-29 15:48:16 +00:00
|
|
|
EXPECT_EQ(0u, buffer.size());
|
|
|
|
EXPECT_EQ(123u, buffer.capacity());
|
|
|
|
}
|
|
|
|
|
|
|
|
#if FMT_USE_RVALUE_REFERENCES
|
|
|
|
|
|
|
|
typedef AllocatorRef< std::allocator<char> > TestAllocator;
|
|
|
|
|
|
|
|
void check_move_buffer(const char *str,
|
2017-02-18 17:13:12 +00:00
|
|
|
basic_memory_buffer<char, 5, TestAllocator> &buffer) {
|
2014-09-29 15:48:16 +00:00
|
|
|
std::allocator<char> *alloc = buffer.get_allocator().get();
|
2017-02-18 17:13:12 +00:00
|
|
|
basic_memory_buffer<char, 5, TestAllocator> buffer2(std::move(buffer));
|
2014-09-29 15:48:16 +00:00
|
|
|
// Move shouldn't destroy the inline content of the first buffer.
|
|
|
|
EXPECT_EQ(str, std::string(&buffer[0], buffer.size()));
|
|
|
|
EXPECT_EQ(str, std::string(&buffer2[0], buffer2.size()));
|
2014-10-13 20:28:47 +00:00
|
|
|
EXPECT_EQ(5u, buffer2.capacity());
|
2014-09-29 15:48:16 +00:00
|
|
|
// Move should transfer allocator.
|
|
|
|
EXPECT_EQ(0, buffer.get_allocator().get());
|
|
|
|
EXPECT_EQ(alloc, buffer2.get_allocator().get());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(MemoryBufferTest, MoveCtor) {
|
|
|
|
std::allocator<char> alloc;
|
2017-02-18 17:13:12 +00:00
|
|
|
basic_memory_buffer<char, 5, TestAllocator> buffer((TestAllocator(&alloc)));
|
2014-09-29 15:48:16 +00:00
|
|
|
const char test[] = "test";
|
|
|
|
buffer.append(test, test + 4);
|
|
|
|
check_move_buffer("test", buffer);
|
|
|
|
// Adding one more character fills the inline buffer, but doesn't cause
|
|
|
|
// dynamic allocation.
|
|
|
|
buffer.push_back('a');
|
|
|
|
check_move_buffer("testa", buffer);
|
|
|
|
const char *inline_buffer_ptr = &buffer[0];
|
|
|
|
// Adding one more character causes the content to move from the inline to
|
|
|
|
// a dynamically allocated buffer.
|
|
|
|
buffer.push_back('b');
|
2017-02-18 17:13:12 +00:00
|
|
|
basic_memory_buffer<char, 5, TestAllocator> buffer2(std::move(buffer));
|
2014-09-29 15:48:16 +00:00
|
|
|
// Move should rip the guts of the first buffer.
|
|
|
|
EXPECT_EQ(inline_buffer_ptr, &buffer[0]);
|
|
|
|
EXPECT_EQ("testab", std::string(&buffer2[0], buffer2.size()));
|
|
|
|
EXPECT_GT(buffer2.capacity(), 5u);
|
|
|
|
}
|
|
|
|
|
2017-02-18 17:13:12 +00:00
|
|
|
void check_move_assign_buffer(
|
|
|
|
const char *str, basic_memory_buffer<char, 5> &buffer) {
|
|
|
|
basic_memory_buffer<char, 5> buffer2;
|
2014-09-29 15:48:16 +00:00
|
|
|
buffer2 = std::move(buffer);
|
|
|
|
// Move shouldn't destroy the inline content of the first buffer.
|
|
|
|
EXPECT_EQ(str, std::string(&buffer[0], buffer.size()));
|
|
|
|
EXPECT_EQ(str, std::string(&buffer2[0], buffer2.size()));
|
2014-10-13 20:28:47 +00:00
|
|
|
EXPECT_EQ(5u, buffer2.capacity());
|
2014-09-29 15:48:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(MemoryBufferTest, MoveAssignment) {
|
2017-02-18 17:13:12 +00:00
|
|
|
basic_memory_buffer<char, 5> buffer;
|
2014-09-29 15:48:16 +00:00
|
|
|
const char test[] = "test";
|
|
|
|
buffer.append(test, test + 4);
|
|
|
|
check_move_assign_buffer("test", buffer);
|
|
|
|
// Adding one more character fills the inline buffer, but doesn't cause
|
|
|
|
// dynamic allocation.
|
|
|
|
buffer.push_back('a');
|
|
|
|
check_move_assign_buffer("testa", buffer);
|
|
|
|
const char *inline_buffer_ptr = &buffer[0];
|
|
|
|
// Adding one more character causes the content to move from the inline to
|
|
|
|
// a dynamically allocated buffer.
|
|
|
|
buffer.push_back('b');
|
2017-02-18 17:13:12 +00:00
|
|
|
basic_memory_buffer<char, 5> buffer2;
|
2014-09-29 15:48:16 +00:00
|
|
|
buffer2 = std::move(buffer);
|
|
|
|
// Move should rip the guts of the first buffer.
|
|
|
|
EXPECT_EQ(inline_buffer_ptr, &buffer[0]);
|
|
|
|
EXPECT_EQ("testab", std::string(&buffer2[0], buffer2.size()));
|
|
|
|
EXPECT_GT(buffer2.capacity(), 5u);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // FMT_USE_RVALUE_REFERENCES
|
|
|
|
|
|
|
|
TEST(MemoryBufferTest, Grow) {
|
2014-10-01 15:12:10 +00:00
|
|
|
typedef AllocatorRef< MockAllocator<int> > Allocator;
|
2017-02-18 17:13:12 +00:00
|
|
|
typedef basic_memory_buffer<int, 10, Allocator> Base;
|
2014-10-01 15:12:10 +00:00
|
|
|
MockAllocator<int> alloc;
|
|
|
|
struct TestMemoryBuffer : Base {
|
|
|
|
TestMemoryBuffer(Allocator alloc) : Base(alloc) {}
|
|
|
|
void grow(std::size_t size) { Base::grow(size); }
|
|
|
|
} buffer((Allocator(&alloc)));
|
|
|
|
buffer.resize(7);
|
2016-03-02 15:53:14 +00:00
|
|
|
using fmt::internal::to_unsigned;
|
2014-10-01 15:12:10 +00:00
|
|
|
for (int i = 0; i < 7; ++i)
|
2016-03-02 15:53:14 +00:00
|
|
|
buffer[to_unsigned(i)] = i * i;
|
2014-10-01 15:12:10 +00:00
|
|
|
EXPECT_EQ(10u, buffer.capacity());
|
|
|
|
int mem[20];
|
|
|
|
mem[7] = 0xdead;
|
|
|
|
EXPECT_CALL(alloc, allocate(20)).WillOnce(Return(mem));
|
|
|
|
buffer.grow(20);
|
2014-09-29 15:48:16 +00:00
|
|
|
EXPECT_EQ(20u, buffer.capacity());
|
2014-10-01 15:12:10 +00:00
|
|
|
// Check if size elements have been copied
|
|
|
|
for (int i = 0; i < 7; ++i)
|
2016-03-02 15:53:14 +00:00
|
|
|
EXPECT_EQ(i * i, buffer[to_unsigned(i)]);
|
2014-10-01 15:12:10 +00:00
|
|
|
// and no more than that.
|
|
|
|
EXPECT_EQ(0xdead, buffer[7]);
|
|
|
|
EXPECT_CALL(alloc, deallocate(mem, 20));
|
2014-09-29 15:48:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(MemoryBufferTest, Allocator) {
|
|
|
|
typedef AllocatorRef< MockAllocator<char> > TestAllocator;
|
2017-02-18 17:13:12 +00:00
|
|
|
basic_memory_buffer<char, 10, TestAllocator> buffer;
|
2014-09-29 15:48:16 +00:00
|
|
|
EXPECT_EQ(0, buffer.get_allocator().get());
|
2014-09-30 14:30:27 +00:00
|
|
|
StrictMock< MockAllocator<char> > alloc;
|
2014-09-29 15:48:16 +00:00
|
|
|
char mem;
|
|
|
|
{
|
2017-02-18 17:13:12 +00:00
|
|
|
basic_memory_buffer<char, 10, TestAllocator> buffer2((TestAllocator(&alloc)));
|
2014-09-29 15:48:16 +00:00
|
|
|
EXPECT_EQ(&alloc, buffer2.get_allocator().get());
|
2018-03-04 18:33:42 +00:00
|
|
|
std::size_t size = 2 * fmt::inline_buffer_size;
|
2014-09-29 15:48:16 +00:00
|
|
|
EXPECT_CALL(alloc, allocate(size)).WillOnce(Return(&mem));
|
|
|
|
buffer2.reserve(size);
|
|
|
|
EXPECT_CALL(alloc, deallocate(&mem, size));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(MemoryBufferTest, ExceptionInDeallocate) {
|
|
|
|
typedef AllocatorRef< MockAllocator<char> > TestAllocator;
|
2014-09-30 14:30:27 +00:00
|
|
|
StrictMock< MockAllocator<char> > alloc;
|
2017-02-18 17:13:12 +00:00
|
|
|
basic_memory_buffer<char, 10, TestAllocator> buffer((TestAllocator(&alloc)));
|
2018-03-04 18:33:42 +00:00
|
|
|
std::size_t size = 2 * fmt::inline_buffer_size;
|
2014-09-29 15:48:16 +00:00
|
|
|
std::vector<char> mem(size);
|
|
|
|
{
|
|
|
|
EXPECT_CALL(alloc, allocate(size)).WillOnce(Return(&mem[0]));
|
|
|
|
buffer.resize(size);
|
|
|
|
std::fill(&buffer[0], &buffer[0] + size, 'x');
|
|
|
|
}
|
|
|
|
std::vector<char> mem2(2 * size);
|
|
|
|
{
|
|
|
|
EXPECT_CALL(alloc, allocate(2 * size)).WillOnce(Return(&mem2[0]));
|
|
|
|
std::exception e;
|
|
|
|
EXPECT_CALL(alloc, deallocate(&mem[0], size)).WillOnce(testing::Throw(e));
|
|
|
|
EXPECT_THROW(buffer.reserve(2 * size), std::exception);
|
|
|
|
EXPECT_EQ(&mem2[0], &buffer[0]);
|
|
|
|
// Check that the data has been copied.
|
|
|
|
for (std::size_t i = 0; i < size; ++i)
|
|
|
|
EXPECT_EQ('x', buffer[i]);
|
|
|
|
}
|
|
|
|
EXPECT_CALL(alloc, deallocate(&mem2[0], 2 * size));
|
|
|
|
}
|
|
|
|
|
2017-02-15 01:12:39 +00:00
|
|
|
TEST(FixedBufferTest, Ctor) {
|
|
|
|
char array[10] = "garbage";
|
2017-02-19 14:46:51 +00:00
|
|
|
fmt::basic_fixed_buffer<char> buffer(array, sizeof(array));
|
2017-02-15 01:12:39 +00:00
|
|
|
EXPECT_EQ(0u, buffer.size());
|
|
|
|
EXPECT_EQ(10u, buffer.capacity());
|
|
|
|
EXPECT_EQ(array, buffer.data());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FixedBufferTest, CompileTimeSizeCtor) {
|
|
|
|
char array[10] = "garbage";
|
2017-02-19 14:46:51 +00:00
|
|
|
fmt::basic_fixed_buffer<char> buffer(array);
|
2017-02-15 01:12:39 +00:00
|
|
|
EXPECT_EQ(0u, buffer.size());
|
|
|
|
EXPECT_EQ(10u, buffer.capacity());
|
|
|
|
EXPECT_EQ(array, buffer.data());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FixedBufferTest, BufferOverflow) {
|
|
|
|
char array[10];
|
2017-02-19 14:46:51 +00:00
|
|
|
fmt::basic_fixed_buffer<char> buffer(array);
|
2017-02-15 01:12:39 +00:00
|
|
|
buffer.resize(10);
|
|
|
|
EXPECT_THROW_MSG(buffer.resize(11), std::runtime_error, "buffer overflow");
|
|
|
|
}
|
|
|
|
|
2018-05-20 16:09:03 +00:00
|
|
|
struct uint32_pair {
|
|
|
|
uint32_t u[2];
|
|
|
|
};
|
|
|
|
|
2018-05-06 15:07:28 +00:00
|
|
|
TEST(UtilTest, BitCast) {
|
2018-05-20 16:09:03 +00:00
|
|
|
auto s = fmt::internal::bit_cast<uint32_pair>(uint64_t{42});
|
2018-05-19 14:14:13 +00:00
|
|
|
EXPECT_EQ(fmt::internal::bit_cast<uint64_t>(s), 42u);
|
2018-05-20 16:09:03 +00:00
|
|
|
s = fmt::internal::bit_cast<uint32_pair>(uint64_t(~0ull));
|
2018-05-06 15:07:28 +00:00
|
|
|
EXPECT_EQ(fmt::internal::bit_cast<uint64_t>(s), ~0ull);
|
|
|
|
}
|
|
|
|
|
2014-06-06 13:38:37 +00:00
|
|
|
TEST(UtilTest, Increment) {
|
|
|
|
char s[10] = "123";
|
2014-07-29 14:50:05 +00:00
|
|
|
increment(s);
|
2014-06-06 13:38:37 +00:00
|
|
|
EXPECT_STREQ("124", s);
|
|
|
|
s[2] = '8';
|
2014-07-29 14:50:05 +00:00
|
|
|
increment(s);
|
2014-06-06 13:38:37 +00:00
|
|
|
EXPECT_STREQ("129", s);
|
2014-07-29 14:50:05 +00:00
|
|
|
increment(s);
|
2014-06-06 13:38:37 +00:00
|
|
|
EXPECT_STREQ("130", s);
|
|
|
|
s[1] = s[2] = '9';
|
2014-07-29 14:50:05 +00:00
|
|
|
increment(s);
|
2014-06-06 13:38:37 +00:00
|
|
|
EXPECT_STREQ("200", s);
|
|
|
|
}
|
|
|
|
|
2016-08-25 15:50:07 +00:00
|
|
|
TEST(UtilTest, FormatArgs) {
|
2017-12-03 15:32:04 +00:00
|
|
|
fmt::format_args args;
|
2018-04-21 21:29:24 +00:00
|
|
|
EXPECT_FALSE(args.get(1));
|
2014-09-25 16:11:51 +00:00
|
|
|
}
|
|
|
|
|
2018-01-14 19:00:27 +00:00
|
|
|
struct custom_context {
|
2018-02-11 17:23:47 +00:00
|
|
|
typedef char char_type;
|
2017-08-13 20:09:02 +00:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct formatter_type {
|
2018-02-11 16:32:02 +00:00
|
|
|
struct type {
|
|
|
|
template <typename ParseContext>
|
|
|
|
auto parse(ParseContext &ctx) -> decltype(ctx.begin()) {
|
|
|
|
return ctx.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *format(const T &, custom_context& ctx) {
|
|
|
|
ctx.called = true;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
};
|
2017-08-13 20:09:02 +00:00
|
|
|
};
|
|
|
|
|
2016-11-07 00:11:24 +00:00
|
|
|
bool called;
|
2015-12-02 16:41:05 +00:00
|
|
|
|
2017-11-19 16:49:58 +00:00
|
|
|
fmt::parse_context parse_context() { return fmt::parse_context(""); }
|
2018-01-14 19:00:27 +00:00
|
|
|
void advance_to(const char *) {}
|
2017-08-13 20:09:02 +00:00
|
|
|
};
|
2015-12-02 16:41:05 +00:00
|
|
|
|
|
|
|
TEST(UtilTest, MakeValueWithCustomFormatter) {
|
|
|
|
::Test t;
|
2018-02-03 14:14:10 +00:00
|
|
|
fmt::internal::value<custom_context> arg =
|
|
|
|
fmt::internal::make_value<custom_context>(t);
|
2018-01-14 19:00:27 +00:00
|
|
|
custom_context ctx = {false};
|
2018-01-06 17:09:50 +00:00
|
|
|
arg.custom.format(&t, ctx);
|
2016-11-07 00:11:24 +00:00
|
|
|
EXPECT_TRUE(ctx.called);
|
2015-12-02 16:41:05 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 15:33:51 +00:00
|
|
|
FMT_BEGIN_NAMESPACE
|
2016-12-11 21:22:45 +00:00
|
|
|
namespace internal {
|
2014-07-14 13:55:29 +00:00
|
|
|
|
2016-12-28 15:55:33 +00:00
|
|
|
template <typename Char>
|
2017-02-19 14:46:51 +00:00
|
|
|
bool operator==(custom_value<Char> lhs, custom_value<Char> rhs) {
|
2016-12-11 21:22:45 +00:00
|
|
|
return lhs.value == rhs.value;
|
|
|
|
}
|
|
|
|
}
|
2018-05-12 15:33:51 +00:00
|
|
|
FMT_END_NAMESPACE
|
2014-07-14 13:55:29 +00:00
|
|
|
|
2018-03-03 22:04:59 +00:00
|
|
|
// Use a unique result type to make sure that there are no undesirable
|
|
|
|
// conversions.
|
|
|
|
struct Result {};
|
2016-12-11 21:22:45 +00:00
|
|
|
|
2018-03-03 22:04:59 +00:00
|
|
|
template <typename T>
|
|
|
|
struct MockVisitor: fmt::internal::function<Result> {
|
2016-12-11 21:22:45 +00:00
|
|
|
MockVisitor() {
|
|
|
|
ON_CALL(*this, visit(_)).WillByDefault(Return(Result()));
|
2016-04-20 16:11:33 +00:00
|
|
|
}
|
2016-12-11 21:22:45 +00:00
|
|
|
|
|
|
|
MOCK_METHOD1_T(visit, Result (T value));
|
|
|
|
MOCK_METHOD0_T(unexpected, void ());
|
|
|
|
|
|
|
|
Result operator()(T value) { return visit(value); }
|
|
|
|
|
|
|
|
template <typename U>
|
2017-08-26 16:32:37 +00:00
|
|
|
Result operator()(U) {
|
2016-12-11 21:22:45 +00:00
|
|
|
unexpected();
|
|
|
|
return Result();
|
2014-07-14 14:27:07 +00:00
|
|
|
}
|
2014-07-14 13:55:29 +00:00
|
|
|
};
|
|
|
|
|
2016-12-11 21:22:45 +00:00
|
|
|
template <typename T>
|
|
|
|
struct VisitType { typedef T Type; };
|
|
|
|
|
|
|
|
#define VISIT_TYPE(Type_, VisitType_) \
|
|
|
|
template <> \
|
|
|
|
struct VisitType<Type_> { typedef VisitType_ Type; }
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
VISIT_TYPE(float, double);
|
|
|
|
|
|
|
|
#define CHECK_ARG_(Char, expected, value) { \
|
|
|
|
testing::StrictMock<MockVisitor<decltype(expected)>> visitor; \
|
|
|
|
EXPECT_CALL(visitor, visit(expected)); \
|
2018-02-11 17:23:47 +00:00
|
|
|
typedef std::back_insert_iterator<basic_buffer<Char>> iterator; \
|
2018-04-08 13:45:21 +00:00
|
|
|
fmt::visit(visitor, \
|
|
|
|
make_arg<fmt::basic_format_context<iterator, Char>>(value)); \
|
2016-12-11 21:22:45 +00:00
|
|
|
}
|
|
|
|
|
2018-03-03 22:04:59 +00:00
|
|
|
#define CHECK_ARG(value, typename_) { \
|
|
|
|
typedef decltype(value) value_type; \
|
|
|
|
typename_ VisitType<value_type>::Type expected = value; \
|
2016-12-11 21:22:45 +00:00
|
|
|
CHECK_ARG_(char, expected, value) \
|
|
|
|
CHECK_ARG_(wchar_t, expected, value) \
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class NumericArgTest : public testing::Test {};
|
|
|
|
|
|
|
|
typedef ::testing::Types<
|
|
|
|
bool, signed char, unsigned char, signed, unsigned short,
|
2017-08-26 16:09:43 +00:00
|
|
|
int, unsigned, long, unsigned long, long long, unsigned long long,
|
2016-12-11 21:22:45 +00:00
|
|
|
float, double, long double> Types;
|
|
|
|
TYPED_TEST_CASE(NumericArgTest, Types);
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
typename std::enable_if<std::is_integral<T>::value, T>::type test_value() {
|
|
|
|
return static_cast<T>(42);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
typename std::enable_if<std::is_floating_point<T>::value, T>::type
|
|
|
|
test_value() {
|
|
|
|
return static_cast<T>(4.2);
|
|
|
|
}
|
|
|
|
|
|
|
|
TYPED_TEST(NumericArgTest, MakeAndVisit) {
|
2018-03-03 22:04:59 +00:00
|
|
|
CHECK_ARG(test_value<TypeParam>(), typename);
|
|
|
|
CHECK_ARG(std::numeric_limits<TypeParam>::min(), typename);
|
|
|
|
CHECK_ARG(std::numeric_limits<TypeParam>::max(), typename);
|
2016-12-11 21:22:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(UtilTest, CharArg) {
|
|
|
|
CHECK_ARG_(char, 'a', 'a');
|
|
|
|
CHECK_ARG_(wchar_t, L'a', 'a');
|
|
|
|
CHECK_ARG_(wchar_t, L'a', L'a');
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(UtilTest, StringArg) {
|
|
|
|
char str_data[] = "test";
|
|
|
|
char *str = str_data;
|
|
|
|
const char *cstr = str;
|
|
|
|
CHECK_ARG_(char, cstr, str);
|
|
|
|
|
2017-02-18 14:52:52 +00:00
|
|
|
string_view sref(str);
|
2016-12-12 05:13:54 +00:00
|
|
|
CHECK_ARG_(char, sref, std::string(str));
|
2016-12-11 21:22:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(UtilTest, WStringArg) {
|
|
|
|
wchar_t str_data[] = L"test";
|
|
|
|
wchar_t *str = str_data;
|
|
|
|
const wchar_t *cstr = str;
|
|
|
|
|
2017-02-18 14:52:52 +00:00
|
|
|
fmt::wstring_view sref(str);
|
2017-11-19 18:28:01 +00:00
|
|
|
CHECK_ARG_(wchar_t, cstr, str);
|
|
|
|
CHECK_ARG_(wchar_t, cstr, cstr);
|
2016-12-12 05:13:54 +00:00
|
|
|
CHECK_ARG_(wchar_t, sref, std::wstring(str));
|
2017-02-18 14:52:52 +00:00
|
|
|
CHECK_ARG_(wchar_t, sref, fmt::wstring_view(str));
|
2016-12-11 21:22:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(UtilTest, PointerArg) {
|
|
|
|
void *p = 0;
|
|
|
|
const void *cp = 0;
|
|
|
|
CHECK_ARG_(char, cp, p);
|
|
|
|
CHECK_ARG_(wchar_t, cp, p);
|
2018-03-03 22:04:59 +00:00
|
|
|
CHECK_ARG(cp, );
|
2016-12-11 21:22:45 +00:00
|
|
|
}
|
|
|
|
|
2018-03-03 22:04:59 +00:00
|
|
|
struct check_custom {
|
2018-04-08 14:03:44 +00:00
|
|
|
Result operator()(fmt::basic_format_arg<fmt::format_context>::handle h) const {
|
2017-02-18 17:13:12 +00:00
|
|
|
fmt::memory_buffer buffer;
|
2018-01-21 22:30:38 +00:00
|
|
|
fmt::internal::basic_buffer<char> &base = buffer;
|
2018-04-08 14:03:44 +00:00
|
|
|
fmt::format_context ctx(std::back_inserter(base), "", fmt::format_args());
|
2018-01-06 17:09:50 +00:00
|
|
|
h.format(ctx);
|
2017-02-14 21:29:47 +00:00
|
|
|
EXPECT_EQ("test", std::string(buffer.data(), buffer.size()));
|
2018-03-03 22:04:59 +00:00
|
|
|
return Result();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(UtilTest, CustomArg) {
|
|
|
|
::Test test;
|
2018-04-08 14:03:44 +00:00
|
|
|
typedef MockVisitor<fmt::basic_format_arg<fmt::format_context>::handle>
|
|
|
|
visitor;
|
2018-03-03 22:04:59 +00:00
|
|
|
testing::StrictMock<visitor> v;
|
|
|
|
EXPECT_CALL(v, visit(_)).WillOnce(testing::Invoke(check_custom()));
|
2018-04-08 14:03:44 +00:00
|
|
|
fmt::visit(v, make_arg<fmt::format_context>(test));
|
2014-07-14 13:55:29 +00:00
|
|
|
}
|
|
|
|
|
2014-07-16 14:55:31 +00:00
|
|
|
TEST(ArgVisitorTest, VisitInvalidArg) {
|
2016-12-27 15:43:25 +00:00
|
|
|
typedef MockVisitor<fmt::monostate> Visitor;
|
|
|
|
testing::StrictMock<Visitor> visitor;
|
|
|
|
EXPECT_CALL(visitor, visit(_));
|
2018-04-08 14:03:44 +00:00
|
|
|
fmt::basic_format_arg<fmt::format_context> arg;
|
2016-12-27 15:43:25 +00:00
|
|
|
visit(visitor, arg);
|
2014-07-16 14:55:31 +00:00
|
|
|
}
|
|
|
|
|
2014-07-24 16:07:43 +00:00
|
|
|
// Tests fmt::internal::count_digits for integer type Int.
|
2014-06-06 13:38:37 +00:00
|
|
|
template <typename Int>
|
2014-07-29 14:50:05 +00:00
|
|
|
void test_count_digits() {
|
2014-06-06 13:38:37 +00:00
|
|
|
for (Int i = 0; i < 10; ++i)
|
2014-07-24 16:07:43 +00:00
|
|
|
EXPECT_EQ(1u, fmt::internal::count_digits(i));
|
2014-06-06 13:38:37 +00:00
|
|
|
for (Int i = 1, n = 1,
|
2014-07-24 16:07:43 +00:00
|
|
|
end = std::numeric_limits<Int>::max() / 10; n <= end; ++i) {
|
2014-06-06 13:38:37 +00:00
|
|
|
n *= 10;
|
2014-07-24 16:07:43 +00:00
|
|
|
EXPECT_EQ(i, fmt::internal::count_digits(n - 1));
|
|
|
|
EXPECT_EQ(i + 1, fmt::internal::count_digits(n));
|
2014-06-06 13:38:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-30 13:27:44 +00:00
|
|
|
TEST(UtilTest, StringRef) {
|
2015-02-17 14:45:45 +00:00
|
|
|
// Test that StringRef::size() returns string length, not buffer size.
|
2015-02-24 17:52:16 +00:00
|
|
|
char str[100] = "some string";
|
2017-02-18 14:52:52 +00:00
|
|
|
EXPECT_EQ(std::strlen(str), string_view(str).size());
|
2015-02-24 17:52:16 +00:00
|
|
|
EXPECT_LT(std::strlen(str), sizeof(str));
|
2014-10-30 13:27:44 +00:00
|
|
|
}
|
|
|
|
|
2015-10-22 15:41:42 +00:00
|
|
|
// Check StringRef's comparison operator.
|
|
|
|
template <template <typename> class Op>
|
|
|
|
void CheckOp() {
|
|
|
|
const char *inputs[] = {"foo", "fop", "fo"};
|
|
|
|
std::size_t num_inputs = sizeof(inputs) / sizeof(*inputs);
|
|
|
|
for (std::size_t i = 0; i < num_inputs; ++i) {
|
|
|
|
for (std::size_t j = 0; j < num_inputs; ++j) {
|
2017-02-18 14:52:52 +00:00
|
|
|
string_view lhs(inputs[i]), rhs(inputs[j]);
|
|
|
|
EXPECT_EQ(Op<int>()(lhs.compare(rhs), 0), Op<string_view>()(lhs, rhs));
|
2015-10-22 15:41:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(UtilTest, StringRefCompare) {
|
2017-02-18 14:52:52 +00:00
|
|
|
EXPECT_EQ(0, string_view("foo").compare(string_view("foo")));
|
|
|
|
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);
|
2015-10-22 15:41:42 +00:00
|
|
|
CheckOp<std::equal_to>();
|
|
|
|
CheckOp<std::not_equal_to>();
|
|
|
|
CheckOp<std::less>();
|
|
|
|
CheckOp<std::less_equal>();
|
|
|
|
CheckOp<std::greater>();
|
|
|
|
CheckOp<std::greater_equal>();
|
|
|
|
}
|
|
|
|
|
2014-06-06 13:38:37 +00:00
|
|
|
TEST(UtilTest, CountDigits) {
|
2014-07-29 14:50:05 +00:00
|
|
|
test_count_digits<uint32_t>();
|
|
|
|
test_count_digits<uint64_t>();
|
2014-06-06 13:38:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
TEST(UtilTest, UTF16ToUTF8) {
|
|
|
|
std::string s = "ёжик";
|
2017-02-19 16:41:38 +00:00
|
|
|
fmt::internal::utf16_to_utf8 u(L"\x0451\x0436\x0438\x043A");
|
2014-07-01 01:12:57 +00:00
|
|
|
EXPECT_EQ(s, u.str());
|
2014-06-06 13:38:37 +00:00
|
|
|
EXPECT_EQ(s.size(), u.size());
|
|
|
|
}
|
|
|
|
|
2018-03-12 12:43:29 +00:00
|
|
|
TEST(UtilTest, UTF16ToUTF8EmptyString) {
|
|
|
|
std::string s = "";
|
|
|
|
fmt::internal::utf16_to_utf8 u(L"");
|
|
|
|
EXPECT_EQ(s, u.str());
|
|
|
|
EXPECT_EQ(s.size(), u.size());
|
|
|
|
}
|
|
|
|
|
2014-06-06 13:38:37 +00:00
|
|
|
TEST(UtilTest, UTF8ToUTF16) {
|
|
|
|
std::string s = "лошадка";
|
2017-02-19 14:46:51 +00:00
|
|
|
fmt::internal::utf8_to_utf16 u(s.c_str());
|
2014-07-01 01:12:57 +00:00
|
|
|
EXPECT_EQ(L"\x043B\x043E\x0448\x0430\x0434\x043A\x0430", u.str());
|
2014-06-06 13:38:37 +00:00
|
|
|
EXPECT_EQ(7, u.size());
|
|
|
|
}
|
|
|
|
|
2018-03-12 12:43:29 +00:00
|
|
|
TEST(UtilTest, UTF8ToUTF16EmptyString) {
|
|
|
|
std::string s = "";
|
|
|
|
fmt::internal::utf8_to_utf16 u(s.c_str());
|
|
|
|
EXPECT_EQ(L"", u.str());
|
|
|
|
EXPECT_EQ(s.size(), u.size());
|
|
|
|
}
|
|
|
|
|
2014-10-30 20:00:44 +00:00
|
|
|
template <typename Converter, typename Char>
|
2015-08-07 14:34:58 +00:00
|
|
|
void check_utf_conversion_error(
|
|
|
|
const char *message,
|
2018-03-14 02:20:06 +00:00
|
|
|
fmt::basic_string_view<Char> str = fmt::basic_string_view<Char>(0, 1)) {
|
2017-02-19 14:46:51 +00:00
|
|
|
fmt::memory_buffer out;
|
2014-07-27 22:09:05 +00:00
|
|
|
fmt::internal::format_windows_error(out, ERROR_INVALID_PARAMETER, message);
|
2017-03-08 15:34:10 +00:00
|
|
|
fmt::system_error error(0, "");
|
2014-06-06 13:38:37 +00:00
|
|
|
try {
|
2015-08-07 14:34:58 +00:00
|
|
|
(Converter)(str);
|
2017-03-08 15:34:10 +00:00
|
|
|
} catch (const fmt::system_error &e) {
|
2014-06-06 13:38:37 +00:00
|
|
|
error = e;
|
|
|
|
}
|
|
|
|
EXPECT_EQ(ERROR_INVALID_PARAMETER, error.error_code());
|
2017-02-17 14:38:53 +00:00
|
|
|
EXPECT_EQ(fmt::to_string(out), error.what());
|
2014-06-06 13:38:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(UtilTest, UTF16ToUTF8Error) {
|
2017-02-19 16:41:38 +00:00
|
|
|
check_utf_conversion_error<fmt::internal::utf16_to_utf8, wchar_t>(
|
2014-06-06 13:38:37 +00:00
|
|
|
"cannot convert string from UTF-16 to UTF-8");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(UtilTest, UTF8ToUTF16Error) {
|
2015-08-07 14:34:58 +00:00
|
|
|
const char *message = "cannot convert string from UTF-8 to UTF-16";
|
2017-02-19 14:46:51 +00:00
|
|
|
check_utf_conversion_error<fmt::internal::utf8_to_utf16, char>(message);
|
|
|
|
check_utf_conversion_error<fmt::internal::utf8_to_utf16, char>(
|
2017-02-18 14:52:52 +00:00
|
|
|
message, fmt::string_view("foo", INT_MAX + 1u));
|
2014-06-06 13:38:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(UtilTest, UTF16ToUTF8Convert) {
|
2017-02-19 16:41:38 +00:00
|
|
|
fmt::internal::utf16_to_utf8 u;
|
2018-03-14 02:20:06 +00:00
|
|
|
EXPECT_EQ(ERROR_INVALID_PARAMETER, u.convert(fmt::wstring_view(0, 1)));
|
2015-08-07 14:34:58 +00:00
|
|
|
EXPECT_EQ(ERROR_INVALID_PARAMETER,
|
2017-02-18 14:52:52 +00:00
|
|
|
u.convert(fmt::wstring_view(L"foo", INT_MAX + 1u)));
|
2014-06-06 13:38:37 +00:00
|
|
|
}
|
|
|
|
#endif // _WIN32
|
|
|
|
|
|
|
|
typedef void (*FormatErrorMessage)(
|
2018-01-14 22:15:59 +00:00
|
|
|
fmt::internal::buffer &out, int error_code, string_view message);
|
2014-06-06 13:38:37 +00:00
|
|
|
|
2014-06-30 21:26:29 +00:00
|
|
|
template <typename Error>
|
2014-07-29 14:50:05 +00:00
|
|
|
void check_throw_error(int error_code, FormatErrorMessage format) {
|
2017-02-19 16:41:38 +00:00
|
|
|
fmt::system_error error(0, "");
|
2014-06-06 13:38:37 +00:00
|
|
|
try {
|
2014-06-30 21:26:29 +00:00
|
|
|
throw Error(error_code, "test {}", "error");
|
2017-02-19 16:41:38 +00:00
|
|
|
} catch (const fmt::system_error &e) {
|
2014-06-06 13:38:37 +00:00
|
|
|
error = e;
|
|
|
|
}
|
2017-02-18 17:13:12 +00:00
|
|
|
fmt::memory_buffer message;
|
2014-06-06 13:38:37 +00:00
|
|
|
format(message, error_code, "test error");
|
2017-02-14 21:29:47 +00:00
|
|
|
EXPECT_EQ(to_string(message), error.what());
|
2014-06-06 13:38:37 +00:00
|
|
|
EXPECT_EQ(error_code, error.error_code());
|
|
|
|
}
|
|
|
|
|
2014-07-27 22:09:05 +00:00
|
|
|
TEST(UtilTest, FormatSystemError) {
|
2017-02-18 17:13:12 +00:00
|
|
|
fmt::memory_buffer message;
|
2016-05-12 03:36:22 +00:00
|
|
|
fmt::format_system_error(message, EDOM, "test");
|
2017-02-14 21:29:47 +00:00
|
|
|
EXPECT_EQ(fmt::format("test: {}", get_system_error(EDOM)),
|
|
|
|
to_string(message));
|
2018-02-03 03:34:08 +00:00
|
|
|
message = fmt::memory_buffer();
|
|
|
|
|
|
|
|
// Check if std::allocator throws on allocating max size_t / 2 chars.
|
|
|
|
size_t max_size = std::numeric_limits<size_t>::max() / 2;
|
|
|
|
bool throws_on_alloc = false;
|
|
|
|
try {
|
|
|
|
std::allocator<char> alloc;
|
|
|
|
alloc.deallocate(alloc.allocate(max_size), max_size);
|
|
|
|
} catch (std::bad_alloc) {
|
|
|
|
throws_on_alloc = true;
|
|
|
|
}
|
|
|
|
if (!throws_on_alloc) {
|
|
|
|
fmt::print("warning: std::allocator allocates {} chars", max_size);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fmt::format_system_error(message, EDOM, fmt::string_view(0, max_size));
|
2017-02-14 21:29:47 +00:00
|
|
|
EXPECT_EQ(fmt::format("error {}", EDOM), to_string(message));
|
2014-06-06 13:38:37 +00:00
|
|
|
}
|
|
|
|
|
2014-07-28 15:41:50 +00:00
|
|
|
TEST(UtilTest, SystemError) {
|
2017-02-19 16:41:38 +00:00
|
|
|
fmt::system_error e(EDOM, "test");
|
2014-07-29 14:50:05 +00:00
|
|
|
EXPECT_EQ(fmt::format("test: {}", get_system_error(EDOM)), e.what());
|
2014-07-28 15:41:50 +00:00
|
|
|
EXPECT_EQ(EDOM, e.error_code());
|
2017-02-19 16:41:38 +00:00
|
|
|
check_throw_error<fmt::system_error>(EDOM, fmt::format_system_error);
|
2014-06-06 13:38:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(UtilTest, ReportSystemError) {
|
2017-02-18 17:13:12 +00:00
|
|
|
fmt::memory_buffer out;
|
2016-05-12 03:36:22 +00:00
|
|
|
fmt::format_system_error(out, EDOM, "test error");
|
2017-02-14 21:29:47 +00:00
|
|
|
out.push_back('\n');
|
|
|
|
EXPECT_WRITE(stderr, fmt::report_system_error(EDOM, "test error"),
|
|
|
|
to_string(out));
|
2014-06-06 13:38:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
2014-07-28 15:41:50 +00:00
|
|
|
TEST(UtilTest, FormatWindowsError) {
|
2014-06-06 13:38:37 +00:00
|
|
|
LPWSTR message = 0;
|
|
|
|
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
|
|
|
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0,
|
|
|
|
ERROR_FILE_EXISTS, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
|
|
|
reinterpret_cast<LPWSTR>(&message), 0, 0);
|
2017-02-19 16:41:38 +00:00
|
|
|
fmt::internal::utf16_to_utf8 utf8_message(message);
|
2014-06-06 13:38:37 +00:00
|
|
|
LocalFree(message);
|
2017-02-19 14:46:51 +00:00
|
|
|
fmt::memory_buffer actual_message;
|
2014-07-27 22:09:05 +00:00
|
|
|
fmt::internal::format_windows_error(
|
2014-06-06 13:38:37 +00:00
|
|
|
actual_message, ERROR_FILE_EXISTS, "test");
|
2014-07-01 02:11:20 +00:00
|
|
|
EXPECT_EQ(fmt::format("test: {}", utf8_message.str()),
|
2017-02-17 14:38:53 +00:00
|
|
|
fmt::to_string(actual_message));
|
2017-03-11 15:43:26 +00:00
|
|
|
actual_message.resize(0);
|
2014-09-03 15:03:05 +00:00
|
|
|
fmt::internal::format_windows_error(
|
|
|
|
actual_message, ERROR_FILE_EXISTS,
|
2017-02-18 14:52:52 +00:00
|
|
|
fmt::string_view(0, std::numeric_limits<size_t>::max()));
|
2017-02-17 14:38:53 +00:00
|
|
|
EXPECT_EQ(fmt::format("error {}", ERROR_FILE_EXISTS),
|
|
|
|
fmt::to_string(actual_message));
|
2014-06-06 13:38:37 +00:00
|
|
|
}
|
|
|
|
|
2016-03-05 01:47:37 +00:00
|
|
|
TEST(UtilTest, FormatLongWindowsError) {
|
|
|
|
LPWSTR message = 0;
|
|
|
|
// this error code is not available on all Windows platforms and
|
|
|
|
// Windows SDKs, so do not fail the test if the error string cannot
|
|
|
|
// be retrieved.
|
|
|
|
const int provisioning_not_allowed = 0x80284013L /*TBS_E_PROVISIONING_NOT_ALLOWED*/;
|
|
|
|
if (FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
|
|
|
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0,
|
|
|
|
provisioning_not_allowed, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
|
|
|
reinterpret_cast<LPWSTR>(&message), 0, 0) == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2017-02-19 16:41:38 +00:00
|
|
|
fmt::internal::utf16_to_utf8 utf8_message(message);
|
2016-03-05 01:47:37 +00:00
|
|
|
LocalFree(message);
|
2017-02-19 14:46:51 +00:00
|
|
|
fmt::memory_buffer actual_message;
|
2016-03-05 01:47:37 +00:00
|
|
|
fmt::internal::format_windows_error(
|
|
|
|
actual_message, provisioning_not_allowed, "test");
|
|
|
|
EXPECT_EQ(fmt::format("test: {}", utf8_message.str()),
|
2017-02-17 14:38:53 +00:00
|
|
|
fmt::to_string(actual_message));
|
2016-03-05 01:47:37 +00:00
|
|
|
}
|
|
|
|
|
2014-07-28 15:41:50 +00:00
|
|
|
TEST(UtilTest, WindowsError) {
|
2017-03-08 15:34:10 +00:00
|
|
|
check_throw_error<fmt::windows_error>(
|
2014-07-27 22:09:05 +00:00
|
|
|
ERROR_FILE_EXISTS, fmt::internal::format_windows_error);
|
2014-06-06 13:38:37 +00:00
|
|
|
}
|
|
|
|
|
2014-07-28 15:41:50 +00:00
|
|
|
TEST(UtilTest, ReportWindowsError) {
|
2017-02-19 14:46:51 +00:00
|
|
|
fmt::memory_buffer out;
|
2014-07-27 22:09:05 +00:00
|
|
|
fmt::internal::format_windows_error(out, ERROR_FILE_EXISTS, "test error");
|
2017-02-17 14:38:53 +00:00
|
|
|
out.push_back('\n');
|
2014-06-06 13:38:37 +00:00
|
|
|
EXPECT_WRITE(stderr,
|
2017-02-17 14:38:53 +00:00
|
|
|
fmt::report_windows_error(ERROR_FILE_EXISTS, "test error"),
|
|
|
|
fmt::to_string(out));
|
2014-06-06 13:38:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // _WIN32
|
2015-03-10 14:53:46 +00:00
|
|
|
|
2015-11-24 16:18:19 +00:00
|
|
|
enum TestEnum2 {};
|
|
|
|
|
|
|
|
TEST(UtilTest, ConvertToInt) {
|
2018-02-17 09:03:43 +00:00
|
|
|
EXPECT_FALSE((fmt::internal::convert_to_int<char, char>::value));
|
|
|
|
EXPECT_FALSE((fmt::internal::convert_to_int<const char *, char>::value));
|
|
|
|
EXPECT_TRUE((fmt::internal::convert_to_int<TestEnum2, char>::value));
|
2015-03-10 14:53:46 +00:00
|
|
|
}
|
2015-03-18 18:02:37 +00:00
|
|
|
|
|
|
|
#if FMT_USE_ENUM_BASE
|
|
|
|
enum TestEnum : char {TestValue};
|
|
|
|
TEST(UtilTest, IsEnumConvertibleToInt) {
|
2018-02-17 09:03:43 +00:00
|
|
|
EXPECT_TRUE((fmt::internal::convert_to_int<TestEnum, char>::value));
|
2015-03-18 18:02:37 +00:00
|
|
|
}
|
|
|
|
#endif
|
2017-11-12 14:58:11 +00:00
|
|
|
|
|
|
|
TEST(UtilTest, ParseNonnegativeInt) {
|
|
|
|
if (std::numeric_limits<int>::max() != (1 << 31)) {
|
|
|
|
fmt::print("Skipping parse_nonnegative_int test\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const char *s = "10000000000";
|
|
|
|
EXPECT_THROW_MSG(
|
|
|
|
parse_nonnegative_int(s, fmt::internal::error_handler()),
|
|
|
|
fmt::format_error, "number is too big");
|
|
|
|
s = "2147483649";
|
|
|
|
EXPECT_THROW_MSG(
|
|
|
|
parse_nonnegative_int(s, fmt::internal::error_handler()),
|
|
|
|
fmt::format_error, "number is too big");
|
|
|
|
}
|
2018-04-22 00:26:24 +00:00
|
|
|
|
2018-05-27 15:04:30 +00:00
|
|
|
template <bool is_iec559>
|
|
|
|
void test_construct_from_double() {
|
|
|
|
fmt::print("warning: double is not IEC559, skipping FP tests\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
void test_construct_from_double<true>() {
|
|
|
|
auto v = fp(1.23);
|
|
|
|
EXPECT_EQ(v.f, 0x13ae147ae147aeu);
|
|
|
|
EXPECT_EQ(v.e, -52);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FPTest, ConstructFromDouble) {
|
|
|
|
test_construct_from_double<std::numeric_limits<double>::is_iec559>();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FPTest, Normalize) {
|
|
|
|
auto v = fp(0xbeef, 42);
|
|
|
|
v.normalize();
|
|
|
|
EXPECT_EQ(0xbeef000000000000, v.f);
|
|
|
|
EXPECT_EQ(-6, v.e);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FPTest, Subtract) {
|
|
|
|
auto v = fp(123, 1) - fp(102, 1);
|
|
|
|
EXPECT_EQ(v.f, 21u);
|
|
|
|
EXPECT_EQ(v.e, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FPTest, Multiply) {
|
|
|
|
auto v = fp(123ULL << 32, 4) * fp(56ULL << 32, 7);
|
|
|
|
EXPECT_EQ(v.f, 123u * 56u);
|
|
|
|
EXPECT_EQ(v.e, 4 + 7 + 64);
|
|
|
|
v = fp(123ULL << 32, 4) * fp(567ULL << 31, 8);
|
|
|
|
EXPECT_EQ(v.f, (123 * 567 + 1u) / 2);
|
|
|
|
EXPECT_EQ(v.e, 4 + 8 + 64);
|
2018-04-22 00:26:24 +00:00
|
|
|
}
|
2018-05-27 17:57:26 +00:00
|
|
|
|
|
|
|
TEST(FPTest, GetCachedPower) {
|
|
|
|
typedef std::numeric_limits<double> limits;
|
|
|
|
for (auto exp = limits::min_exponent; exp <= limits::max_exponent; ++exp) {
|
|
|
|
int dec_exp = 0;
|
|
|
|
auto fp = fmt::internal::get_cached_power(exp, dec_exp);
|
|
|
|
EXPECT_LE(exp, fp.e);
|
|
|
|
int dec_exp_step = 8;
|
|
|
|
EXPECT_LE(fp.e, exp + dec_exp_step * log2(10));
|
|
|
|
EXPECT_EQ(pow(10, dec_exp), ldexp(fp.f, fp.e));
|
|
|
|
}
|
|
|
|
}
|