Sun, 24 Apr 2022 15:15:39 +0200
#170 first buffer tests
universe@530 | 1 | /* |
universe@530 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
universe@530 | 3 | * |
universe@530 | 4 | * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. |
universe@530 | 5 | * |
universe@530 | 6 | * Redistribution and use in source and binary forms, with or without |
universe@530 | 7 | * modification, are permitted provided that the following conditions are met: |
universe@530 | 8 | * |
universe@530 | 9 | * 1. Redistributions of source code must retain the above copyright |
universe@530 | 10 | * notice, this list of conditions and the following disclaimer. |
universe@530 | 11 | * |
universe@530 | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@530 | 13 | * notice, this list of conditions and the following disclaimer in the |
universe@530 | 14 | * documentation and/or other materials provided with the distribution. |
universe@530 | 15 | * |
universe@530 | 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@530 | 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@530 | 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
universe@530 | 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
universe@530 | 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
universe@530 | 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
universe@530 | 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
universe@530 | 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
universe@530 | 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
universe@530 | 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
universe@530 | 26 | * POSSIBILITY OF SUCH DAMAGE. |
universe@530 | 27 | */ |
universe@530 | 28 | |
universe@530 | 29 | #include "cx/buffer.h" |
universe@530 | 30 | |
universe@530 | 31 | #include <gtest/gtest.h> |
universe@530 | 32 | #include "util_allocator.h" |
universe@530 | 33 | |
universe@530 | 34 | class Buffer : public ::testing::Test { |
universe@530 | 35 | protected: |
universe@530 | 36 | void TearDown() override { |
universe@530 | 37 | EXPECT_TRUE(testingAllocator.verify()); |
universe@530 | 38 | } |
universe@530 | 39 | |
universe@530 | 40 | CxTestingAllocator testingAllocator; |
universe@530 | 41 | }; |
universe@530 | 42 | |
universe@530 | 43 | TEST_F(Buffer, WrapSpace) { |
universe@530 | 44 | CxBuffer buf; |
universe@530 | 45 | void *space = cxMalloc(&testingAllocator, 16); |
universe@530 | 46 | cxBufferInit(&buf, space, 16, &testingAllocator, CX_BUFFER_DEFAULT); |
universe@530 | 47 | EXPECT_EQ(buf.space, space); |
universe@530 | 48 | EXPECT_EQ(buf.flags & CX_BUFFER_AUTO_EXTEND, 0); |
universe@530 | 49 | EXPECT_EQ(buf.flags & CX_BUFFER_FREE_CONTENTS, 0); |
universe@530 | 50 | EXPECT_EQ(buf.pos, 0); |
universe@530 | 51 | EXPECT_EQ(buf.size, 0); |
universe@530 | 52 | EXPECT_EQ(buf.capacity, 16); |
universe@530 | 53 | EXPECT_EQ(buf.allocator, &testingAllocator); |
universe@530 | 54 | cxBufferDestroy(&buf); |
universe@530 | 55 | EXPECT_FALSE(testingAllocator.verify()); |
universe@530 | 56 | cxFree(&testingAllocator, space); |
universe@530 | 57 | EXPECT_TRUE(testingAllocator.verify()); |
universe@530 | 58 | } |
universe@530 | 59 | |
universe@530 | 60 | TEST_F(Buffer, WrapSpaceAutoFree) { |
universe@530 | 61 | CxBuffer buf; |
universe@530 | 62 | void *space = cxMalloc(&testingAllocator, 16); |
universe@530 | 63 | cxBufferInit(&buf, space, 16, &testingAllocator, CX_BUFFER_FREE_CONTENTS); |
universe@530 | 64 | EXPECT_EQ(buf.space, space); |
universe@530 | 65 | EXPECT_EQ(buf.flags & CX_BUFFER_AUTO_EXTEND, 0); |
universe@530 | 66 | EXPECT_EQ(buf.flags & CX_BUFFER_FREE_CONTENTS, CX_BUFFER_FREE_CONTENTS); |
universe@530 | 67 | EXPECT_EQ(buf.pos, 0); |
universe@530 | 68 | EXPECT_EQ(buf.size, 0); |
universe@530 | 69 | EXPECT_EQ(buf.capacity, 16); |
universe@530 | 70 | EXPECT_EQ(buf.allocator, &testingAllocator); |
universe@530 | 71 | EXPECT_FALSE(testingAllocator.verify()); |
universe@530 | 72 | cxBufferDestroy(&buf); |
universe@530 | 73 | EXPECT_TRUE(testingAllocator.verify()); |
universe@530 | 74 | } |