/* Utility tests. Copyright (c) 2012-2014, Victor Zverovich All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "test-assert.h" #include #include #include #include #include #if FMT_USE_TYPE_TRAITS # include #endif #include "gmock/gmock.h" #include "gtest-extra.h" #include "mock-allocator.h" #include "util.h" // Check if format.h compiles with windows.h included. #ifdef _WIN32 # include #endif #include "fmt/format.h" #undef max using fmt::format_arg; using fmt::Buffer; using fmt::StringRef; using fmt::internal::MemoryBuffer; using testing::Return; using testing::StrictMock; namespace { struct Test {}; template void format_value(fmt::BasicWriter &w, Test, fmt::basic_format_context &) { w << "test"; } template format_arg make_arg(const T &value) { typedef fmt::internal::MakeValue< fmt::basic_format_context > MakeValue; format_arg arg = MakeValue(value); arg.type = fmt::internal::type(); return arg; } } // namespace void CheckForwarding( MockAllocator &alloc, AllocatorRef< MockAllocator > &ref) { int mem; // Check if value_type is properly defined. AllocatorRef< MockAllocator >::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) { StrictMock< MockAllocator > alloc; typedef AllocatorRef< MockAllocator > 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 TEST(BufferTest, Noncopyable) { EXPECT_FALSE(std::is_copy_constructible >::value); EXPECT_FALSE(std::is_copy_assignable >::value); } TEST(BufferTest, Nonmoveable) { EXPECT_FALSE(std::is_move_constructible >::value); EXPECT_FALSE(std::is_move_assignable >::value); } #endif // A test buffer with a dummy grow method. template struct TestBuffer : Buffer { void grow(std::size_t size) { this->capacity_ = size; } }; template struct MockBuffer : Buffer { MOCK_METHOD1(do_grow, void (std::size_t size)); void grow(std::size_t size) { this->capacity_ = size; do_grow(size); } MockBuffer() {} MockBuffer(T *ptr) : Buffer(ptr) {} MockBuffer(T *ptr, std::size_t capacity) : Buffer(ptr, capacity) {} }; TEST(BufferTest, Ctor) { { MockBuffer buffer; EXPECT_EQ(0, &buffer[0]); EXPECT_EQ(0u, buffer.size()); EXPECT_EQ(0u, buffer.capacity()); } { int dummy; MockBuffer buffer(&dummy); EXPECT_EQ(&dummy, &buffer[0]); EXPECT_EQ(0u, buffer.size()); EXPECT_EQ(0u, buffer.capacity()); } { int dummy; std::size_t capacity = std::numeric_limits::max(); MockBuffer buffer(&dummy, capacity); EXPECT_EQ(&dummy, &buffer[0]); EXPECT_EQ(0u, buffer.size()); EXPECT_EQ(capacity, buffer.capacity()); } } struct DyingBuffer : TestBuffer { MOCK_METHOD0(die, void()); ~DyingBuffer() { die(); } }; TEST(BufferTest, VirtualDtor) { typedef StrictMock StictMockBuffer; StictMockBuffer *mock_buffer = new StictMockBuffer(); EXPECT_CALL(*mock_buffer, die()); Buffer *buffer = mock_buffer; delete buffer; } TEST(BufferTest, Access) { char data[10]; MockBuffer buffer(data, sizeof(data)); buffer[0] = 11; EXPECT_EQ(11, buffer[0]); buffer[3] = 42; EXPECT_EQ(42, *(&buffer[0] + 3)); const Buffer &const_buffer = buffer; EXPECT_EQ(42, const_buffer[3]); } TEST(BufferTest, Resize) { char data[123]; MockBuffer 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. EXPECT_CALL(buffer, do_grow(124)); buffer.resize(124); EXPECT_CALL(buffer, do_grow(200)); buffer.resize(200); } TEST(BufferTest, Clear) { TestBuffer buffer; buffer.resize(20); buffer.clear(); EXPECT_EQ(0u, buffer.size()); EXPECT_EQ(20u, buffer.capacity()); } TEST(BufferTest, PushBack) { int data[15]; MockBuffer buffer(data, 10); buffer.push_back(11); EXPECT_EQ(11, buffer[0]); EXPECT_EQ(1u, buffer.size()); buffer.resize(10); EXPECT_CALL(buffer, do_grow(11)); buffer.push_back(22); EXPECT_EQ(22, buffer[10]); EXPECT_EQ(11u, buffer.size()); } TEST(BufferTest, Append) { char data[15]; MockBuffer 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); EXPECT_CALL(buffer, do_grow(12)); 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 buffer(data, 10); const char *test = "abcdefgh"; buffer.resize(10); EXPECT_CALL(buffer, do_grow(19)); buffer.append(test, test + 9); } TEST(MemoryBufferTest, Ctor) { MemoryBuffer buffer; EXPECT_EQ(0u, buffer.size()); EXPECT_EQ(123u, buffer.capacity()); } #if FMT_USE_RVALUE_REFERENCES typedef AllocatorRef< std::allocator > TestAllocator; void check_move_buffer(const char *str, MemoryBuffer &buffer) { std::allocator *alloc = buffer.get_allocator().get(); MemoryBuffer 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())); EXPECT_EQ(5u, buffer2.capacity()); // Move should transfer allocator. EXPECT_EQ(0, buffer.get_allocator().get()); EXPECT_EQ(alloc, buffer2.get_allocator().get()); } TEST(MemoryBufferTest, MoveCtor) { std::allocator alloc; MemoryBuffer buffer((TestAllocator(&alloc))); 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'); MemoryBuffer 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); } void check_move_assign_buffer(const char *str, MemoryBuffer &buffer) { MemoryBuffer buffer2; 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())); EXPECT_EQ(5u, buffer2.capacity()); } TEST(MemoryBufferTest, MoveAssignment) { MemoryBuffer buffer; 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'); MemoryBuffer buffer2; 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) { typedef AllocatorRef< MockAllocator > Allocator; typedef MemoryBuffer Base; MockAllocator alloc; struct TestMemoryBuffer : Base { TestMemoryBuffer(Allocator alloc) : Base(alloc) {} void grow(std::size_t size) { Base::grow(size); } } buffer((Allocator(&alloc))); buffer.resize(7); using fmt::internal::to_unsigned; for (int i = 0; i < 7; ++i) buffer[to_unsigned(i)] = i * i; EXPECT_EQ(10u, buffer.capacity()); int mem[20]; mem[7] = 0xdead; EXPECT_CALL(alloc, allocate(20)).WillOnce(Return(mem)); buffer.grow(20); EXPECT_EQ(20u, buffer.capacity()); // Check if size elements have been copied for (int i = 0; i < 7; ++i) EXPECT_EQ(i * i, buffer[to_unsigned(i)]); // and no more than that. EXPECT_EQ(0xdead, buffer[7]); EXPECT_CALL(alloc, deallocate(mem, 20)); } TEST(MemoryBufferTest, Allocator) { typedef AllocatorRef< MockAllocator > TestAllocator; MemoryBuffer buffer; EXPECT_EQ(0, buffer.get_allocator().get()); StrictMock< MockAllocator > alloc; char mem; { MemoryBuffer buffer2((TestAllocator(&alloc))); EXPECT_EQ(&alloc, buffer2.get_allocator().get()); std::size_t size = 2 * fmt::internal::INLINE_BUFFER_SIZE; EXPECT_CALL(alloc, allocate(size)).WillOnce(Return(&mem)); buffer2.reserve(size); EXPECT_CALL(alloc, deallocate(&mem, size)); } } TEST(MemoryBufferTest, ExceptionInDeallocate) { typedef AllocatorRef< MockAllocator > TestAllocator; StrictMock< MockAllocator > alloc; MemoryBuffer buffer((TestAllocator(&alloc))); std::size_t size = 2 * fmt::internal::INLINE_BUFFER_SIZE; std::vector mem(size); { EXPECT_CALL(alloc, allocate(size)).WillOnce(Return(&mem[0])); buffer.resize(size); std::fill(&buffer[0], &buffer[0] + size, 'x'); } std::vector 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)); } TEST(UtilTest, Increment) { char s[10] = "123"; increment(s); EXPECT_STREQ("124", s); s[2] = '8'; increment(s); EXPECT_STREQ("129", s); increment(s); EXPECT_STREQ("130", s); s[1] = s[2] = '9'; increment(s); EXPECT_STREQ("200", s); } template struct ArgInfo; #define ARG_INFO(type_code, Type, field) \ template <> \ struct ArgInfo { \ static Type get(const format_arg &arg) { return arg.field; } \ } ARG_INFO(INT, int, int_value); ARG_INFO(UINT, unsigned, uint_value); ARG_INFO(LONG_LONG, fmt::LongLong, long_long_value); ARG_INFO(ULONG_LONG, fmt::ULongLong, ulong_long_value); ARG_INFO(BOOL, int, int_value); ARG_INFO(CHAR, int, int_value); ARG_INFO(DOUBLE, double, double_value); ARG_INFO(LONG_DOUBLE, long double, long_double_value); ARG_INFO(CSTRING, const char *, string.value); ARG_INFO(STRING, const char *, string.value); ARG_INFO(WSTRING, const wchar_t *, wstring.value); ARG_INFO(POINTER, const void *, pointer); ARG_INFO(CUSTOM, format_arg::CustomValue, custom); #define CHECK_ARG_INFO(Type, field, value) { \ format_arg arg = format_arg(); \ arg.field = value; \ EXPECT_EQ(value, ArgInfo::get(arg)); \ } TEST(ArgTest, ArgInfo) { CHECK_ARG_INFO(INT, int_value, 42); CHECK_ARG_INFO(UINT, uint_value, 42u); CHECK_ARG_INFO(LONG_LONG, long_long_value, 42); CHECK_ARG_INFO(ULONG_LONG, ulong_long_value, 42u); CHECK_ARG_INFO(DOUBLE, double_value, 4.2); CHECK_ARG_INFO(LONG_DOUBLE, long_double_value, 4.2); CHECK_ARG_INFO(CHAR, int_value, 'x'); const char STR[] = "abc"; CHECK_ARG_INFO(CSTRING, string.value, STR); const wchar_t WSTR[] = L"abc"; CHECK_ARG_INFO(WSTRING, wstring.value, WSTR); int p = 0; CHECK_ARG_INFO(POINTER, pointer, &p); format_arg arg = format_arg(); arg.custom.value = &p; EXPECT_EQ(&p, ArgInfo::get(arg).value); } #define EXPECT_ARG_(Char, type_code, MakeArgType, ExpectedType, value) { \ MakeArgType input = static_cast(value); \ format_arg arg = make_arg(input); \ EXPECT_EQ(format_arg::type_code, arg.type); \ ExpectedType expected_value = static_cast(value); \ EXPECT_EQ(expected_value, ArgInfo::get(arg)); \ } #define EXPECT_ARG(type_code, Type, value) \ EXPECT_ARG_(char, type_code, Type, Type, value) #define EXPECT_ARGW(type_code, Type, value) \ EXPECT_ARG_(wchar_t, type_code, Type, Type, value) TEST(ArgTest, MakeArg) { // Test bool. EXPECT_ARG_(char, BOOL, bool, int, true); EXPECT_ARG_(wchar_t, BOOL, bool, int, true); // Test char. EXPECT_ARG(CHAR, char, 'a'); EXPECT_ARG(CHAR, char, CHAR_MIN); EXPECT_ARG(CHAR, char, CHAR_MAX); // Test wchar_t. EXPECT_ARGW(CHAR, wchar_t, L'a'); EXPECT_ARGW(CHAR, wchar_t, WCHAR_MIN); EXPECT_ARGW(CHAR, wchar_t, WCHAR_MAX); // Test signed/unsigned char. EXPECT_ARG(INT, signed char, 42); EXPECT_ARG(INT, signed char, SCHAR_MIN); EXPECT_ARG(INT, signed char, SCHAR_MAX); EXPECT_ARG(UINT, unsigned char, 42); EXPECT_ARG(UINT, unsigned char, UCHAR_MAX ); // Test short. EXPECT_ARG(INT, short, 42); EXPECT_ARG(INT, short, SHRT_MIN); EXPECT_ARG(INT, short, SHRT_MAX); EXPECT_ARG(UINT, unsigned short, 42); EXPECT_ARG(UINT, unsigned short, USHRT_MAX); // Test int. EXPECT_ARG(INT, int, 42); EXPECT_ARG(INT, int, INT_MIN); EXPECT_ARG(INT, int, INT_MAX); EXPECT_ARG(UINT, unsigned, 42); EXPECT_ARG(UINT, unsigned, UINT_MAX); // Test long. #if LONG_MAX == INT_MAX # define LONG INT # define ULONG UINT # define long_value int_value # define ulong_value uint_value #else # define LONG LONG_LONG # define ULONG ULONG_LONG # define long_value long_long_value # define ulong_value ulong_long_value #endif EXPECT_ARG(LONG, long, 42); EXPECT_ARG(LONG, long, LONG_MIN); EXPECT_ARG(LONG, long, LONG_MAX); EXPECT_ARG(ULONG, unsigned long, 42); EXPECT_ARG(ULONG, unsigned long, ULONG_MAX); // Test long long. EXPECT_ARG(LONG_LONG, fmt::LongLong, 42); EXPECT_ARG(LONG_LONG, fmt::LongLong, LLONG_MIN); EXPECT_ARG(LONG_LONG, fmt::LongLong, LLONG_MAX); EXPECT_ARG(ULONG_LONG, fmt::ULongLong, 42); EXPECT_ARG(ULONG_LONG, fmt::ULongLong, ULLONG_MAX); // Test float. EXPECT_ARG(DOUBLE, float, 4.2); EXPECT_ARG(DOUBLE, float, FLT_MIN); EXPECT_ARG(DOUBLE, float, FLT_MAX); // Test double. EXPECT_ARG(DOUBLE, double, 4.2); EXPECT_ARG(DOUBLE, double, DBL_MIN); EXPECT_ARG(DOUBLE, double, DBL_MAX); // Test long double. EXPECT_ARG(LONG_DOUBLE, long double, 4.2); EXPECT_ARG(LONG_DOUBLE, long double, LDBL_MIN); EXPECT_ARG(LONG_DOUBLE, long double, LDBL_MAX); // Test string. char STR[] = "test"; EXPECT_ARG(CSTRING, char*, STR); EXPECT_ARG(CSTRING, const char*, STR); EXPECT_ARG(STRING, std::string, STR); EXPECT_ARG(STRING, fmt::StringRef, STR); // Test wide string. wchar_t WSTR[] = L"test"; EXPECT_ARGW(WSTRING, wchar_t*, WSTR); EXPECT_ARGW(WSTRING, const wchar_t*, WSTR); EXPECT_ARGW(WSTRING, std::wstring, WSTR); EXPECT_ARGW(WSTRING, fmt::WStringRef, WSTR); int n = 42; EXPECT_ARG(POINTER, void*, &n); EXPECT_ARG(POINTER, const void*, &n); ::Test t; format_arg arg = make_arg(t); EXPECT_EQ(format_arg::CUSTOM, arg.type); EXPECT_EQ(&t, arg.custom.value); fmt::MemoryWriter w; fmt::format_context ctx("}", fmt::format_args()); arg.custom.format(&w, &t, &ctx); EXPECT_EQ("test", w.str()); } TEST(UtilTest, FormatArgs) { fmt::format_args args; EXPECT_EQ(format_arg::NONE, args[1].type); } struct CustomFormatter { typedef char char_type; bool called; }; void format_value(fmt::Writer &, const Test &, CustomFormatter &ctx) { ctx.called = true; } TEST(UtilTest, MakeValueWithCustomFormatter) { ::Test t; format_arg arg = fmt::internal::MakeValue(t); CustomFormatter ctx = {false}; fmt::MemoryWriter w; arg.custom.format(&w, &t, &ctx); EXPECT_TRUE(ctx.called); } struct Result { format_arg arg; Result() : arg(make_arg(0xdeadbeef)) {} template Result(const T& value) : arg(make_arg(value)) {} Result(const wchar_t *s) : arg(make_arg(s)) {} }; struct TestVisitor { Result operator()(int value) { return value; } Result operator()(unsigned value) { return value; } Result operator()(fmt::LongLong value) { return value; } Result operator()(fmt::ULongLong value) { return value; } Result operator()(double value) { return value; } Result operator()(long double value) { return value; } Result operator()(wchar_t value) { return static_cast(value); } Result operator()(const char *s) { return s; } Result operator()(fmt::format_arg::StringValue s) { return s.value; } Result operator()(fmt::format_arg::StringValue s) { return s.value; } Result operator()(const void *p) { return p; } Result operator()(fmt::format_arg::CustomValue c) { return *static_cast(c.value); } }; #define EXPECT_RESULT_(Char, type_code, value) { \ format_arg arg = make_arg(value); \ Result result = fmt::visit(TestVisitor(), arg); \ EXPECT_EQ(format_arg::type_code, result.arg.type); \ EXPECT_EQ(value, ArgInfo::get(result.arg)); \ } #define EXPECT_RESULT(type_code, value) \ EXPECT_RESULT_(char, type_code, value) #define EXPECT_RESULTW(type_code, value) \ EXPECT_RESULT_(wchar_t, type_code, value) TEST(ArgVisitorTest, VisitAll) { EXPECT_RESULT(INT, 42); EXPECT_RESULT(UINT, 42u); EXPECT_RESULT(LONG_LONG, 42ll); EXPECT_RESULT(ULONG_LONG, 42ull); EXPECT_RESULT(DOUBLE, 4.2); EXPECT_RESULT(LONG_DOUBLE, 4.2l); EXPECT_RESULT(CHAR, 'x'); const char STR[] = "abc"; EXPECT_RESULT(CSTRING, STR); const wchar_t WSTR[] = L"abc"; EXPECT_RESULTW(WSTRING, WSTR); const void *p = STR; EXPECT_RESULT(POINTER, p); ::Test t; Result result = visit(TestVisitor(), make_arg(t)); EXPECT_EQ(format_arg::CUSTOM, result.arg.type); EXPECT_EQ(&t, result.arg.custom.value); } TEST(ArgVisitorTest, VisitInvalidArg) { format_arg arg = format_arg(); arg.type = static_cast(format_arg::NONE); EXPECT_ASSERT(visit(TestVisitor(), arg), "invalid argument type"); } // Tests fmt::internal::count_digits for integer type Int. template void test_count_digits() { for (Int i = 0; i < 10; ++i) EXPECT_EQ(1u, fmt::internal::count_digits(i)); for (Int i = 1, n = 1, end = std::numeric_limits::max() / 10; n <= end; ++i) { n *= 10; EXPECT_EQ(i, fmt::internal::count_digits(n - 1)); EXPECT_EQ(i + 1, fmt::internal::count_digits(n)); } } TEST(UtilTest, StringRef) { // Test that StringRef::size() returns string length, not buffer size. char str[100] = "some string"; EXPECT_EQ(std::strlen(str), StringRef(str).size()); EXPECT_LT(std::strlen(str), sizeof(str)); } // Check StringRef's comparison operator. template