From 98a25ec7cb9990fa11806b12973a54a745d1aac3 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Tue, 30 Sep 2014 07:30:27 -0700 Subject: [PATCH] Test Buffer --- test/util-test.cc | 79 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 64 insertions(+), 15 deletions(-) diff --git a/test/util-test.cc b/test/util-test.cc index 10c1960a..91f4b5ca 100644 --- a/test/util-test.cc +++ b/test/util-test.cc @@ -51,9 +51,11 @@ using fmt::StringRef; using fmt::internal::Arg; using fmt::internal::Value; +using fmt::internal::Buffer; using fmt::internal::MemoryBuffer; using testing::Return; +using testing::StrictMock; namespace { @@ -87,7 +89,7 @@ void CheckForwarding( } TEST(AllocatorTest, AllocatorRef) { - testing::StrictMock< MockAllocator > alloc; + StrictMock< MockAllocator > alloc; typedef AllocatorRef< MockAllocator > TestAllocatorRef; TestAllocatorRef ref(&alloc); // Check if AllocatorRef forwards to the underlying allocator. @@ -102,14 +104,71 @@ TEST(AllocatorTest, AllocatorRef) { #if FMT_USE_TYPE_TRAITS TEST(BufferTest, NotCopyConstructible) { - EXPECT_FALSE(std::is_copy_constructible >::value); + EXPECT_FALSE(std::is_copy_constructible >::value); } TEST(BufferTest, NotCopyAssignable) { - EXPECT_FALSE(std::is_copy_assignable >::value); + EXPECT_FALSE(std::is_copy_assignable >::value); } #endif +template +class MockBuffer : public Buffer { + protected: + MOCK_METHOD1(grow, void (std::size_t size)); + + public: + MOCK_METHOD0(Die, void()); + + MockBuffer() {} + MockBuffer(T *ptr) : Buffer(ptr) {} + MockBuffer(T *ptr, std::size_t capacity) : Buffer(ptr, capacity) {} + ~MockBuffer() { Die(); } +}; + +TEST(BufferTest, Ctor) { + { + MockBuffer buffer; + EXPECT_EQ(0, &buffer[0]); + EXPECT_EQ(0, buffer.size()); + EXPECT_EQ(0, buffer.capacity()); + } + { + int dummy; + MockBuffer buffer(&dummy); + EXPECT_EQ(&dummy, &buffer[0]); + EXPECT_EQ(0, buffer.size()); + EXPECT_EQ(0, buffer.capacity()); + } + { + int dummy; + std::size_t capacity = std::numeric_limits::max(); + MockBuffer buffer(&dummy, capacity); + EXPECT_EQ(&dummy, &buffer[0]); + EXPECT_EQ(0, buffer.size()); + EXPECT_EQ(capacity, buffer.capacity()); + } +} + +TEST(BufferTest, VirtualDtor) { + typedef StrictMock< MockBuffer > 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, 10); + 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(MemoryBufferTest, Ctor) { MemoryBuffer buffer; EXPECT_EQ(0u, buffer.size()); @@ -186,16 +245,6 @@ TEST(MemoryBufferTest, MoveAssignment) { #endif // FMT_USE_RVALUE_REFERENCES -TEST(MemoryBufferTest, Access) { - MemoryBuffer buffer; - buffer[0] = 11; - EXPECT_EQ(11, buffer[0]); - buffer[3] = 42; - EXPECT_EQ(42, *(&buffer[0] + 3)); - const MemoryBuffer &const_buffer = buffer; - EXPECT_EQ(42, const_buffer[3]); -} - TEST(MemoryBufferTest, Resize) { MemoryBuffer buffer; buffer[10] = 42; @@ -270,7 +319,7 @@ TEST(MemoryBufferTest, Allocator) { typedef AllocatorRef< MockAllocator > TestAllocator; MemoryBuffer buffer; EXPECT_EQ(0, buffer.get_allocator().get()); - testing::StrictMock< MockAllocator > alloc; + StrictMock< MockAllocator > alloc; char mem; { MemoryBuffer buffer2((TestAllocator(&alloc))); @@ -284,7 +333,7 @@ TEST(MemoryBufferTest, Allocator) { TEST(MemoryBufferTest, ExceptionInDeallocate) { typedef AllocatorRef< MockAllocator > TestAllocator; - testing::StrictMock< MockAllocator > alloc; + StrictMock< MockAllocator > alloc; MemoryBuffer buffer((TestAllocator(&alloc))); std::size_t size = 2 * fmt::internal::INLINE_BUFFER_SIZE; std::vector mem(size);