test/test_buffer.cpp

Sun, 24 Apr 2022 17:45:09 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 24 Apr 2022 17:45:09 +0200
changeset 535
2ff6e9184468
parent 530
e866516cac17
child 536
cb9b9739055e
permissions
-rw-r--r--

#170 add several more 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@535 34 TEST(BufferInit, WrapSpace) {
universe@535 35 CxTestingAllocator alloc;
universe@530 36 CxBuffer buf;
universe@535 37 void *space = cxMalloc(&alloc, 16);
universe@535 38 cxBufferInit(&buf, space, 16, &alloc, CX_BUFFER_DEFAULT);
universe@530 39 EXPECT_EQ(buf.space, space);
universe@530 40 EXPECT_EQ(buf.flags & CX_BUFFER_AUTO_EXTEND, 0);
universe@530 41 EXPECT_EQ(buf.flags & CX_BUFFER_FREE_CONTENTS, 0);
universe@530 42 EXPECT_EQ(buf.pos, 0);
universe@530 43 EXPECT_EQ(buf.size, 0);
universe@530 44 EXPECT_EQ(buf.capacity, 16);
universe@535 45 EXPECT_EQ(buf.allocator, &alloc);
universe@530 46 cxBufferDestroy(&buf);
universe@535 47 EXPECT_FALSE(alloc.verify());
universe@535 48 cxFree(&alloc, space);
universe@535 49 EXPECT_TRUE(alloc.verify());
universe@530 50 }
universe@530 51
universe@535 52 TEST(BufferInit, WrapSpaceAutoFree) {
universe@535 53 CxTestingAllocator alloc;
universe@530 54 CxBuffer buf;
universe@535 55 void *space = cxMalloc(&alloc, 16);
universe@535 56 cxBufferInit(&buf, space, 16, &alloc, CX_BUFFER_FREE_CONTENTS);
universe@530 57 EXPECT_EQ(buf.space, space);
universe@530 58 EXPECT_EQ(buf.flags & CX_BUFFER_AUTO_EXTEND, 0);
universe@530 59 EXPECT_EQ(buf.flags & CX_BUFFER_FREE_CONTENTS, CX_BUFFER_FREE_CONTENTS);
universe@530 60 EXPECT_EQ(buf.pos, 0);
universe@530 61 EXPECT_EQ(buf.size, 0);
universe@530 62 EXPECT_EQ(buf.capacity, 16);
universe@535 63 EXPECT_EQ(buf.allocator, &alloc);
universe@535 64 EXPECT_FALSE(alloc.verify());
universe@530 65 cxBufferDestroy(&buf);
universe@535 66 EXPECT_TRUE(alloc.verify());
universe@530 67 }
universe@535 68
universe@535 69 TEST(BufferInit, FreshSpace) {
universe@535 70 CxTestingAllocator alloc;
universe@535 71 CxBuffer buf;
universe@535 72 cxBufferInit(&buf, nullptr, 8, &alloc, CX_BUFFER_DEFAULT);
universe@535 73 EXPECT_NE(buf.space, nullptr);
universe@535 74 EXPECT_EQ(buf.flags & CX_BUFFER_AUTO_EXTEND, 0);
universe@535 75 EXPECT_EQ(buf.flags & CX_BUFFER_FREE_CONTENTS, CX_BUFFER_FREE_CONTENTS);
universe@535 76 EXPECT_EQ(buf.pos, 0);
universe@535 77 EXPECT_EQ(buf.size, 0);
universe@535 78 EXPECT_EQ(buf.capacity, 8);
universe@535 79 EXPECT_EQ(buf.allocator, &alloc);
universe@535 80 EXPECT_FALSE(alloc.verify()); // space is still allocated
universe@535 81 cxBufferDestroy(&buf);
universe@535 82 EXPECT_TRUE(alloc.verify());
universe@535 83 }
universe@535 84
universe@535 85 class BufferShiftFixture : public ::testing::Test {
universe@535 86 protected:
universe@535 87 void SetUp() override {
universe@535 88 ASSERT_TRUE(alloc.verify());
universe@535 89 cxBufferInit(&buf, nullptr, 16, &alloc, CX_BUFFER_DEFAULT);
universe@535 90 memcpy(buf.space, "test____________", 16);
universe@535 91 buf.capacity = 8; // purposely pretend that the buffer has less capacity s.t. we can test beyond the range
universe@535 92 buf.pos = 4;
universe@535 93 buf.size = 4;
universe@535 94 }
universe@535 95
universe@535 96 void TearDown() override {
universe@535 97 cxBufferDestroy(&buf);
universe@535 98 EXPECT_TRUE(alloc.verify());
universe@535 99 }
universe@535 100
universe@535 101 CxTestingAllocator alloc;
universe@535 102 CxBuffer buf{};
universe@535 103 };
universe@535 104
universe@535 105 class BufferShiftLeft : public BufferShiftFixture {
universe@535 106 };
universe@535 107
universe@535 108 TEST_F(BufferShiftLeft, Zero) {
universe@535 109 ASSERT_EQ(buf.pos, 4);
universe@535 110 ASSERT_EQ(buf.size, 4);
universe@535 111 int ret = cxBufferShiftLeft(&buf, 0);
universe@535 112 EXPECT_EQ(ret, 0);
universe@535 113 EXPECT_EQ(buf.pos, 4);
universe@535 114 EXPECT_EQ(buf.size, 4);
universe@535 115 EXPECT_TRUE(memcmp(buf.space, "test________", 8) == 0);
universe@535 116 }
universe@535 117
universe@535 118 TEST_F(BufferShiftLeft, Standard) {
universe@535 119 ASSERT_EQ(buf.pos, 4);
universe@535 120 ASSERT_EQ(buf.size, 4);
universe@535 121 int ret = cxBufferShiftLeft(&buf, 2);
universe@535 122 EXPECT_EQ(ret, 0);
universe@535 123 EXPECT_EQ(buf.pos, 2);
universe@535 124 EXPECT_EQ(buf.size, 2);
universe@535 125 EXPECT_TRUE(memcmp(buf.space, "stst________", 8) == 0);
universe@535 126 }
universe@535 127
universe@535 128 TEST_F(BufferShiftLeft, Overshift) {
universe@535 129 ASSERT_LT(buf.pos, 6);
universe@535 130 ASSERT_LT(buf.size, 6);
universe@535 131 int ret = cxBufferShiftLeft(&buf, 6);
universe@535 132 EXPECT_EQ(ret, 0);
universe@535 133 EXPECT_EQ(buf.pos, 0);
universe@535 134 EXPECT_EQ(buf.size, 0);
universe@535 135 EXPECT_TRUE(memcmp(buf.space, "test________", 8) == 0);
universe@535 136 }
universe@535 137
universe@535 138 TEST_F(BufferShiftLeft, OvershiftPosOnly) {
universe@535 139 buf.pos = 2;
universe@535 140 ASSERT_EQ(buf.size, 4);
universe@535 141 int ret = cxBufferShiftLeft(&buf, 3);
universe@535 142 EXPECT_EQ(ret, 0);
universe@535 143 EXPECT_EQ(buf.pos, 0);
universe@535 144 EXPECT_EQ(buf.size, 1);
universe@535 145 EXPECT_TRUE(memcmp(buf.space, "test________", 8) == 0);
universe@535 146 }
universe@535 147
universe@535 148 TEST_F(BufferShiftLeft, OffsetInterface) {
universe@535 149 buf.pos = 3;
universe@535 150 ASSERT_EQ(buf.size, 4);
universe@535 151 int ret = cxBufferShift(&buf, -2);
universe@535 152 EXPECT_EQ(ret, 0);
universe@535 153 EXPECT_EQ(buf.pos, 1);
universe@535 154 EXPECT_EQ(buf.size, 2);
universe@535 155 EXPECT_TRUE(memcmp(buf.space, "stst________", 8) == 0);
universe@535 156 }
universe@535 157
universe@535 158 class BufferShiftRight : public BufferShiftFixture {
universe@535 159 };
universe@535 160
universe@535 161 TEST_F(BufferShiftRight, Zero) {
universe@535 162 ASSERT_EQ(buf.pos, 4);
universe@535 163 ASSERT_EQ(buf.size, 4);
universe@535 164 int ret = cxBufferShiftRight(&buf, 0);
universe@535 165 EXPECT_EQ(ret, 0);
universe@535 166 EXPECT_EQ(buf.pos, 4);
universe@535 167 EXPECT_EQ(buf.size, 4);
universe@535 168 EXPECT_TRUE(memcmp(buf.space, "test________", 8) == 0);
universe@535 169 }
universe@535 170
universe@535 171 TEST_F(BufferShiftRight, Standard) {
universe@535 172 ASSERT_EQ(buf.pos, 4);
universe@535 173 ASSERT_EQ(buf.size, 4);
universe@535 174 int ret = cxBufferShiftRight(&buf, 3);
universe@535 175 EXPECT_EQ(ret, 0);
universe@535 176 EXPECT_EQ(buf.pos, 7);
universe@535 177 EXPECT_EQ(buf.size, 7);
universe@535 178 EXPECT_TRUE(memcmp(buf.space, "testest_____", 8) == 0);
universe@535 179 }
universe@535 180
universe@535 181 TEST_F(BufferShiftRight, OvershiftDiscard) {
universe@535 182 ASSERT_EQ(buf.pos, 4);
universe@535 183 ASSERT_EQ(buf.size, 4);
universe@535 184 ASSERT_EQ(buf.capacity, 8);
universe@535 185 int ret = cxBufferShiftRight(&buf, 6);
universe@535 186 EXPECT_EQ(ret, 0);
universe@535 187 EXPECT_EQ(buf.pos, 8);
universe@535 188 EXPECT_EQ(buf.size, 8);
universe@535 189 EXPECT_EQ(buf.capacity, 8);
universe@535 190 EXPECT_TRUE(memcmp(buf.space, "test__te____", 8) == 0);
universe@535 191 }
universe@535 192
universe@535 193 TEST_F(BufferShiftRight, OvershiftExtend) {
universe@535 194 ASSERT_EQ(buf.pos, 4);
universe@535 195 ASSERT_EQ(buf.size, 4);
universe@535 196 ASSERT_EQ(buf.capacity, 8);
universe@535 197 buf.flags |= CX_BUFFER_AUTO_EXTEND;
universe@535 198 int ret = cxBufferShiftRight(&buf, 6);
universe@535 199 EXPECT_EQ(ret, 0);
universe@535 200 EXPECT_EQ(buf.pos, 10);
universe@535 201 EXPECT_EQ(buf.size, 10);
universe@535 202 EXPECT_GE(buf.capacity, 10);
universe@535 203 EXPECT_TRUE(memcmp(buf.space, "test__test__", 8) == 0);
universe@535 204 }
universe@535 205
universe@535 206 TEST_F(BufferShiftRight, OffsetInterface) {
universe@535 207 buf.pos = 3;
universe@535 208 ASSERT_EQ(buf.size, 4);
universe@535 209 int ret = cxBufferShift(&buf, 2);
universe@535 210 EXPECT_EQ(ret, 0);
universe@535 211 EXPECT_EQ(buf.pos, 5);
universe@535 212 EXPECT_EQ(buf.size, 6);
universe@535 213 EXPECT_TRUE(memcmp(buf.space, "tetest______", 8) == 0);
universe@535 214 }
universe@535 215
universe@535 216 TEST(BufferMinimumCapacity, Sufficient) {
universe@535 217 CxTestingAllocator alloc;
universe@535 218 auto space = cxMalloc(&alloc, 8);
universe@535 219 CxBuffer buf;
universe@535 220 cxBufferInit(&buf, space, 8, &alloc, CX_BUFFER_FREE_CONTENTS);
universe@535 221 memcpy(space, "Testing", 8);
universe@535 222 buf.size = 8;
universe@535 223 cxBufferMinimumCapacity(&buf, 6);
universe@535 224 EXPECT_EQ(buf.capacity, 8);
universe@535 225 EXPECT_EQ(buf.size, 8);
universe@535 226 EXPECT_TRUE(memcmp(buf.space, "Testing", 8) == 0);
universe@535 227 cxBufferDestroy(&buf);
universe@535 228 EXPECT_TRUE(alloc.verify());
universe@535 229 }
universe@535 230
universe@535 231 TEST(BufferMinimumCapacity, Extend) {
universe@535 232 CxTestingAllocator alloc;
universe@535 233 auto space = cxMalloc(&alloc, 8);
universe@535 234 CxBuffer buf;
universe@535 235 cxBufferInit(&buf, space, 8, &alloc, CX_BUFFER_FREE_CONTENTS); // NO auto extend!
universe@535 236 memcpy(space, "Testing", 8);
universe@535 237 buf.size = 8;
universe@535 238 cxBufferMinimumCapacity(&buf, 16);
universe@535 239 EXPECT_EQ(buf.capacity, 16);
universe@535 240 EXPECT_EQ(buf.size, 8);
universe@535 241 EXPECT_TRUE(memcmp(buf.space, "Testing", 8) == 0);
universe@535 242 cxBufferDestroy(&buf);
universe@535 243 EXPECT_TRUE(alloc.verify());
universe@535 244 }

mercurial