universe@530: /* universe@530: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@530: * universe@530: * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. universe@530: * universe@530: * Redistribution and use in source and binary forms, with or without universe@530: * modification, are permitted provided that the following conditions are met: universe@530: * universe@530: * 1. Redistributions of source code must retain the above copyright universe@530: * notice, this list of conditions and the following disclaimer. universe@530: * universe@530: * 2. Redistributions in binary form must reproduce the above copyright universe@530: * notice, this list of conditions and the following disclaimer in the universe@530: * documentation and/or other materials provided with the distribution. universe@530: * universe@530: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@530: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@530: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@530: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@530: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@530: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@530: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@530: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@530: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@530: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@530: * POSSIBILITY OF SUCH DAMAGE. universe@530: */ universe@530: universe@530: #include "cx/buffer.h" universe@530: universe@530: #include universe@530: #include "util_allocator.h" universe@530: universe@530: class Buffer : public ::testing::Test { universe@530: protected: universe@530: void TearDown() override { universe@530: EXPECT_TRUE(testingAllocator.verify()); universe@530: } universe@530: universe@530: CxTestingAllocator testingAllocator; universe@530: }; universe@530: universe@530: TEST_F(Buffer, WrapSpace) { universe@530: CxBuffer buf; universe@530: void *space = cxMalloc(&testingAllocator, 16); universe@530: cxBufferInit(&buf, space, 16, &testingAllocator, CX_BUFFER_DEFAULT); universe@530: EXPECT_EQ(buf.space, space); universe@530: EXPECT_EQ(buf.flags & CX_BUFFER_AUTO_EXTEND, 0); universe@530: EXPECT_EQ(buf.flags & CX_BUFFER_FREE_CONTENTS, 0); universe@530: EXPECT_EQ(buf.pos, 0); universe@530: EXPECT_EQ(buf.size, 0); universe@530: EXPECT_EQ(buf.capacity, 16); universe@530: EXPECT_EQ(buf.allocator, &testingAllocator); universe@530: cxBufferDestroy(&buf); universe@530: EXPECT_FALSE(testingAllocator.verify()); universe@530: cxFree(&testingAllocator, space); universe@530: EXPECT_TRUE(testingAllocator.verify()); universe@530: } universe@530: universe@530: TEST_F(Buffer, WrapSpaceAutoFree) { universe@530: CxBuffer buf; universe@530: void *space = cxMalloc(&testingAllocator, 16); universe@530: cxBufferInit(&buf, space, 16, &testingAllocator, CX_BUFFER_FREE_CONTENTS); universe@530: EXPECT_EQ(buf.space, space); universe@530: EXPECT_EQ(buf.flags & CX_BUFFER_AUTO_EXTEND, 0); universe@530: EXPECT_EQ(buf.flags & CX_BUFFER_FREE_CONTENTS, CX_BUFFER_FREE_CONTENTS); universe@530: EXPECT_EQ(buf.pos, 0); universe@530: EXPECT_EQ(buf.size, 0); universe@530: EXPECT_EQ(buf.capacity, 16); universe@530: EXPECT_EQ(buf.allocator, &testingAllocator); universe@530: EXPECT_FALSE(testingAllocator.verify()); universe@530: cxBufferDestroy(&buf); universe@530: EXPECT_TRUE(testingAllocator.verify()); universe@530: }