test/test_buffer.cpp

Sun, 24 Apr 2022 16:23:44 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 24 Apr 2022 16:23:44 +0200
changeset 531
1b8624c8448e
parent 530
e866516cac17
child 535
2ff6e9184468
permissions
-rw-r--r--

fix erroneous doc for cxBufferShift

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

mercurial