test/test_buffer.cpp

Mon, 25 Jul 2022 15:34:19 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 25 Jul 2022 15:34:19 +0200
changeset 568
8eda32d09e3d
parent 567
f90a7cfe2480
child 569
cb63f3d1236a
permissions
-rw-r--r--

#170 test cxBufferGet

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
548
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
34 class BufferFixture : public ::testing::Test {
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
35 protected:
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
36 void SetUp() override {
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
37 cxBufferInit(&buf, nullptr, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT);
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
38 buf.size = 6;
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
39 buf.pos = 3;
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
40 }
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
41
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
42 void TearDown() override {
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
43 cxBufferDestroy(&buf);
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
44 }
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
45
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
46 CxBuffer buf{};
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
47 };
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
48
539
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
49 static void expect_default_flush_config(CxBuffer *buf) {
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
50 EXPECT_EQ(buf->flush_blkmax, 0);
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
51 EXPECT_EQ(buf->flush_blksize, 4096);
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
52 EXPECT_EQ(buf->flush_threshold, SIZE_MAX);
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
53 EXPECT_EQ(buf->flush_func, nullptr);
541
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
54 EXPECT_EQ(buf->flush_target, nullptr);
539
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
55 }
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
56
535
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
57 TEST(BufferInit, WrapSpace) {
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
58 CxTestingAllocator alloc;
530
e866516cac17 #170 first buffer tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
59 CxBuffer buf;
535
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
60 void *space = cxMalloc(&alloc, 16);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
61 cxBufferInit(&buf, space, 16, &alloc, CX_BUFFER_DEFAULT);
539
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
62 expect_default_flush_config(&buf);
530
e866516cac17 #170 first buffer tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
63 EXPECT_EQ(buf.space, space);
e866516cac17 #170 first buffer tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
64 EXPECT_EQ(buf.flags & CX_BUFFER_AUTO_EXTEND, 0);
e866516cac17 #170 first buffer tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
65 EXPECT_EQ(buf.flags & CX_BUFFER_FREE_CONTENTS, 0);
e866516cac17 #170 first buffer tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
66 EXPECT_EQ(buf.pos, 0);
e866516cac17 #170 first buffer tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
67 EXPECT_EQ(buf.size, 0);
e866516cac17 #170 first buffer tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
68 EXPECT_EQ(buf.capacity, 16);
535
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
69 EXPECT_EQ(buf.allocator, &alloc);
530
e866516cac17 #170 first buffer tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
70 cxBufferDestroy(&buf);
535
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
71 EXPECT_FALSE(alloc.verify());
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
72 cxFree(&alloc, space);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
73 EXPECT_TRUE(alloc.verify());
530
e866516cac17 #170 first buffer tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
74 }
e866516cac17 #170 first buffer tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
75
539
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
76 TEST(BufferInit, WrapSpaceAutoExtend) {
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
77 CxTestingAllocator alloc;
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
78 CxBuffer buf;
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
79 void *space = cxMalloc(&alloc, 16);
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
80 cxBufferInit(&buf, space, 16, &alloc, CX_BUFFER_AUTO_EXTEND);
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
81 expect_default_flush_config(&buf);
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
82 EXPECT_EQ(buf.space, space);
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
83 EXPECT_EQ(buf.flags & CX_BUFFER_AUTO_EXTEND, CX_BUFFER_AUTO_EXTEND);
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
84 EXPECT_EQ(buf.flags & CX_BUFFER_FREE_CONTENTS, 0);
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
85 EXPECT_EQ(buf.pos, 0);
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
86 EXPECT_EQ(buf.size, 0);
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
87 EXPECT_EQ(buf.capacity, 16);
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
88 EXPECT_EQ(buf.allocator, &alloc);
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
89 cxBufferDestroy(&buf);
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
90 EXPECT_FALSE(alloc.verify());
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
91 cxFree(&alloc, space);
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
92 EXPECT_TRUE(alloc.verify());
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
93 }
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
94
535
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
95 TEST(BufferInit, WrapSpaceAutoFree) {
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
96 CxTestingAllocator alloc;
530
e866516cac17 #170 first buffer tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
97 CxBuffer buf;
535
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
98 void *space = cxMalloc(&alloc, 16);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
99 cxBufferInit(&buf, space, 16, &alloc, CX_BUFFER_FREE_CONTENTS);
539
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
100 expect_default_flush_config(&buf);
530
e866516cac17 #170 first buffer tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
101 EXPECT_EQ(buf.space, space);
e866516cac17 #170 first buffer tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
102 EXPECT_EQ(buf.flags & CX_BUFFER_AUTO_EXTEND, 0);
e866516cac17 #170 first buffer tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
103 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
104 EXPECT_EQ(buf.pos, 0);
e866516cac17 #170 first buffer tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
105 EXPECT_EQ(buf.size, 0);
e866516cac17 #170 first buffer tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
106 EXPECT_EQ(buf.capacity, 16);
535
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
107 EXPECT_EQ(buf.allocator, &alloc);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
108 EXPECT_FALSE(alloc.verify());
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
109 cxBufferDestroy(&buf);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
110 EXPECT_TRUE(alloc.verify());
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
111 }
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
112
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
113 TEST(BufferInit, FreshSpace) {
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
114 CxTestingAllocator alloc;
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
115 CxBuffer buf;
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
116 cxBufferInit(&buf, nullptr, 8, &alloc, CX_BUFFER_DEFAULT);
539
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
117 expect_default_flush_config(&buf);
535
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
118 EXPECT_NE(buf.space, nullptr);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
119 EXPECT_EQ(buf.flags & CX_BUFFER_AUTO_EXTEND, 0);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
120 EXPECT_EQ(buf.flags & CX_BUFFER_FREE_CONTENTS, CX_BUFFER_FREE_CONTENTS);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
121 EXPECT_EQ(buf.pos, 0);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
122 EXPECT_EQ(buf.size, 0);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
123 EXPECT_EQ(buf.capacity, 8);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
124 EXPECT_EQ(buf.allocator, &alloc);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
125 EXPECT_FALSE(alloc.verify()); // space is still allocated
530
e866516cac17 #170 first buffer tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
126 cxBufferDestroy(&buf);
535
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
127 EXPECT_TRUE(alloc.verify());
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
128 }
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
129
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
130 class BufferShiftFixture : public ::testing::Test {
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
131 protected:
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
132 void SetUp() override {
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
133 ASSERT_TRUE(alloc.verify());
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
134 cxBufferInit(&buf, nullptr, 16, &alloc, CX_BUFFER_DEFAULT);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
135 memcpy(buf.space, "test____________", 16);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
136 buf.capacity = 8; // purposely pretend that the buffer has less capacity s.t. we can test beyond the range
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
137 buf.pos = 4;
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
138 buf.size = 4;
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
139 }
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
140
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
141 void TearDown() override {
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
142 cxBufferDestroy(&buf);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
143 EXPECT_TRUE(alloc.verify());
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
144 }
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
145
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
146 CxTestingAllocator alloc;
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
147 CxBuffer buf{};
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
148 };
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
149
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
150 class BufferShiftLeft : public BufferShiftFixture {
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
151 };
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
152
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
153 TEST_F(BufferShiftLeft, Zero) {
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
154 ASSERT_EQ(buf.pos, 4);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
155 ASSERT_EQ(buf.size, 4);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
156 int ret = cxBufferShiftLeft(&buf, 0);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
157 EXPECT_EQ(ret, 0);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
158 EXPECT_EQ(buf.pos, 4);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
159 EXPECT_EQ(buf.size, 4);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
160 EXPECT_TRUE(memcmp(buf.space, "test________", 8) == 0);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
161 }
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
162
547
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
163 TEST_F(BufferShiftLeft, ZeroOffsetInterface) {
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
164 ASSERT_EQ(buf.pos, 4);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
165 ASSERT_EQ(buf.size, 4);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
166 int ret = cxBufferShift(&buf, -0);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
167 EXPECT_EQ(ret, 0);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
168 EXPECT_EQ(buf.pos, 4);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
169 EXPECT_EQ(buf.size, 4);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
170 EXPECT_TRUE(memcmp(buf.space, "test________", 8) == 0);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
171 }
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
172
535
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
173 TEST_F(BufferShiftLeft, Standard) {
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
174 ASSERT_EQ(buf.pos, 4);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
175 ASSERT_EQ(buf.size, 4);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
176 int ret = cxBufferShiftLeft(&buf, 2);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
177 EXPECT_EQ(ret, 0);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
178 EXPECT_EQ(buf.pos, 2);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
179 EXPECT_EQ(buf.size, 2);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
180 EXPECT_TRUE(memcmp(buf.space, "stst________", 8) == 0);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
181 }
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
182
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
183 TEST_F(BufferShiftLeft, Overshift) {
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
184 ASSERT_LT(buf.pos, 6);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
185 ASSERT_LT(buf.size, 6);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
186 int ret = cxBufferShiftLeft(&buf, 6);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
187 EXPECT_EQ(ret, 0);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
188 EXPECT_EQ(buf.pos, 0);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
189 EXPECT_EQ(buf.size, 0);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
190 EXPECT_TRUE(memcmp(buf.space, "test________", 8) == 0);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
191 }
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
192
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
193 TEST_F(BufferShiftLeft, OvershiftPosOnly) {
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
194 buf.pos = 2;
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
195 ASSERT_EQ(buf.size, 4);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
196 int ret = cxBufferShiftLeft(&buf, 3);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
197 EXPECT_EQ(ret, 0);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
198 EXPECT_EQ(buf.pos, 0);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
199 EXPECT_EQ(buf.size, 1);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
200 EXPECT_TRUE(memcmp(buf.space, "test________", 8) == 0);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
201 }
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
202
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
203 TEST_F(BufferShiftLeft, OffsetInterface) {
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
204 buf.pos = 3;
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
205 ASSERT_EQ(buf.size, 4);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
206 int ret = cxBufferShift(&buf, -2);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
207 EXPECT_EQ(ret, 0);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
208 EXPECT_EQ(buf.pos, 1);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
209 EXPECT_EQ(buf.size, 2);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
210 EXPECT_TRUE(memcmp(buf.space, "stst________", 8) == 0);
530
e866516cac17 #170 first buffer tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
211 }
535
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
212
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
213 class BufferShiftRight : public BufferShiftFixture {
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
214 };
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
215
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
216 TEST_F(BufferShiftRight, Zero) {
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
217 ASSERT_EQ(buf.pos, 4);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
218 ASSERT_EQ(buf.size, 4);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
219 int ret = cxBufferShiftRight(&buf, 0);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
220 EXPECT_EQ(ret, 0);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
221 EXPECT_EQ(buf.pos, 4);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
222 EXPECT_EQ(buf.size, 4);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
223 EXPECT_TRUE(memcmp(buf.space, "test________", 8) == 0);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
224 }
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
225
547
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
226 TEST_F(BufferShiftRight, ZeroOffsetInterface) {
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
227 ASSERT_EQ(buf.pos, 4);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
228 ASSERT_EQ(buf.size, 4);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
229 int ret = cxBufferShift(&buf, +0);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
230 EXPECT_EQ(ret, 0);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
231 EXPECT_EQ(buf.pos, 4);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
232 EXPECT_EQ(buf.size, 4);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
233 EXPECT_TRUE(memcmp(buf.space, "test________", 8) == 0);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
234 }
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
235
535
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
236 TEST_F(BufferShiftRight, Standard) {
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
237 ASSERT_EQ(buf.pos, 4);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
238 ASSERT_EQ(buf.size, 4);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
239 int ret = cxBufferShiftRight(&buf, 3);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
240 EXPECT_EQ(ret, 0);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
241 EXPECT_EQ(buf.pos, 7);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
242 EXPECT_EQ(buf.size, 7);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
243 EXPECT_TRUE(memcmp(buf.space, "testest_____", 8) == 0);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
244 }
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
245
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
246 TEST_F(BufferShiftRight, OvershiftDiscard) {
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
247 ASSERT_EQ(buf.pos, 4);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
248 ASSERT_EQ(buf.size, 4);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
249 ASSERT_EQ(buf.capacity, 8);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
250 int ret = cxBufferShiftRight(&buf, 6);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
251 EXPECT_EQ(ret, 0);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
252 EXPECT_EQ(buf.pos, 8);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
253 EXPECT_EQ(buf.size, 8);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
254 EXPECT_EQ(buf.capacity, 8);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
255 EXPECT_TRUE(memcmp(buf.space, "test__te____", 8) == 0);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
256 }
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
257
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
258 TEST_F(BufferShiftRight, OvershiftExtend) {
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
259 ASSERT_EQ(buf.pos, 4);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
260 ASSERT_EQ(buf.size, 4);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
261 ASSERT_EQ(buf.capacity, 8);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
262 buf.flags |= CX_BUFFER_AUTO_EXTEND;
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
263 int ret = cxBufferShiftRight(&buf, 6);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
264 EXPECT_EQ(ret, 0);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
265 EXPECT_EQ(buf.pos, 10);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
266 EXPECT_EQ(buf.size, 10);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
267 EXPECT_GE(buf.capacity, 10);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
268 EXPECT_TRUE(memcmp(buf.space, "test__test__", 8) == 0);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
269 }
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
270
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
271 TEST_F(BufferShiftRight, OffsetInterface) {
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
272 buf.pos = 3;
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
273 ASSERT_EQ(buf.size, 4);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
274 int ret = cxBufferShift(&buf, 2);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
275 EXPECT_EQ(ret, 0);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
276 EXPECT_EQ(buf.pos, 5);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
277 EXPECT_EQ(buf.size, 6);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
278 EXPECT_TRUE(memcmp(buf.space, "tetest______", 8) == 0);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
279 }
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
280
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
281 TEST(BufferMinimumCapacity, Sufficient) {
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
282 CxTestingAllocator alloc;
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
283 auto space = cxMalloc(&alloc, 8);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
284 CxBuffer buf;
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
285 cxBufferInit(&buf, space, 8, &alloc, CX_BUFFER_FREE_CONTENTS);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
286 memcpy(space, "Testing", 8);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
287 buf.size = 8;
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
288 cxBufferMinimumCapacity(&buf, 6);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
289 EXPECT_EQ(buf.capacity, 8);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
290 EXPECT_EQ(buf.size, 8);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
291 EXPECT_TRUE(memcmp(buf.space, "Testing", 8) == 0);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
292 cxBufferDestroy(&buf);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
293 EXPECT_TRUE(alloc.verify());
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
294 }
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
295
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
296 TEST(BufferMinimumCapacity, Extend) {
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
297 CxTestingAllocator alloc;
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
298 auto space = cxMalloc(&alloc, 8);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
299 CxBuffer buf;
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
300 cxBufferInit(&buf, space, 8, &alloc, CX_BUFFER_FREE_CONTENTS); // NO auto extend!
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
301 memcpy(space, "Testing", 8);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
302 buf.size = 8;
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
303 cxBufferMinimumCapacity(&buf, 16);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
304 EXPECT_EQ(buf.capacity, 16);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
305 EXPECT_EQ(buf.size, 8);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
306 EXPECT_TRUE(memcmp(buf.space, "Testing", 8) == 0);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
307 cxBufferDestroy(&buf);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
308 EXPECT_TRUE(alloc.verify());
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
309 }
536
cb9b9739055e #170 add cxBufferClear test
Mike Becker <universe@uap-core.de>
parents: 535
diff changeset
310
cb9b9739055e #170 add cxBufferClear test
Mike Becker <universe@uap-core.de>
parents: 535
diff changeset
311 TEST(BufferClear, Test) {
cb9b9739055e #170 add cxBufferClear test
Mike Becker <universe@uap-core.de>
parents: 535
diff changeset
312 char space[16];
cb9b9739055e #170 add cxBufferClear test
Mike Becker <universe@uap-core.de>
parents: 535
diff changeset
313 strcpy(space, "clear test");
cb9b9739055e #170 add cxBufferClear test
Mike Becker <universe@uap-core.de>
parents: 535
diff changeset
314 CxBuffer buf;
cb9b9739055e #170 add cxBufferClear test
Mike Becker <universe@uap-core.de>
parents: 535
diff changeset
315 cxBufferInit(&buf, space, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT);
cb9b9739055e #170 add cxBufferClear test
Mike Becker <universe@uap-core.de>
parents: 535
diff changeset
316 ASSERT_EQ(buf.size, 0);
cb9b9739055e #170 add cxBufferClear test
Mike Becker <universe@uap-core.de>
parents: 535
diff changeset
317 // only clear the used part of the buffer
cb9b9739055e #170 add cxBufferClear test
Mike Becker <universe@uap-core.de>
parents: 535
diff changeset
318 cxBufferClear(&buf);
cb9b9739055e #170 add cxBufferClear test
Mike Becker <universe@uap-core.de>
parents: 535
diff changeset
319 EXPECT_EQ(memcmp(space, "clear test", 10), 0);
cb9b9739055e #170 add cxBufferClear test
Mike Becker <universe@uap-core.de>
parents: 535
diff changeset
320 buf.size = 5;
cb9b9739055e #170 add cxBufferClear test
Mike Becker <universe@uap-core.de>
parents: 535
diff changeset
321 buf.pos = 3;
cb9b9739055e #170 add cxBufferClear test
Mike Becker <universe@uap-core.de>
parents: 535
diff changeset
322 cxBufferClear(&buf);
cb9b9739055e #170 add cxBufferClear test
Mike Becker <universe@uap-core.de>
parents: 535
diff changeset
323 EXPECT_EQ(memcmp(space, "\0\0\0\0\0 test", 10), 0);
cb9b9739055e #170 add cxBufferClear test
Mike Becker <universe@uap-core.de>
parents: 535
diff changeset
324 EXPECT_EQ(buf.size, 0);
cb9b9739055e #170 add cxBufferClear test
Mike Becker <universe@uap-core.de>
parents: 535
diff changeset
325 EXPECT_EQ(buf.pos, 0);
538
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
326 cxBufferDestroy(&buf);
536
cb9b9739055e #170 add cxBufferClear test
Mike Becker <universe@uap-core.de>
parents: 535
diff changeset
327 }
538
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
328
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
329 class BufferWrite : public ::testing::Test {
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
330 protected:
545
3700ac4bd9a3 #184 #170 first basic flush test
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
331 CxBuffer buf{}, target{};
538
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
332
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
333 void SetUp() override {
545
3700ac4bd9a3 #184 #170 first basic flush test
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
334 cxBufferInit(&target, nullptr, 16, cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND);
538
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
335 cxBufferInit(&buf, nullptr, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT);
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
336 buf.capacity = 8; // artificially reduce capacity to check OOB writes
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
337 memset(buf.space, 0, 16);
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
338 memcpy(buf.space, "prep", 4);
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
339 buf.size = buf.pos = 4;
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
340 }
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
341
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
342 void TearDown() override {
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
343 cxBufferDestroy(&buf);
545
3700ac4bd9a3 #184 #170 first basic flush test
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
344 cxBufferDestroy(&target);
3700ac4bd9a3 #184 #170 first basic flush test
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
345 }
3700ac4bd9a3 #184 #170 first basic flush test
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
346
3700ac4bd9a3 #184 #170 first basic flush test
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
347 void enableFlushing() {
3700ac4bd9a3 #184 #170 first basic flush test
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
348 buf.flush_target = &target;
3700ac4bd9a3 #184 #170 first basic flush test
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
349 buf.flush_func = reinterpret_cast<cx_write_func>(cxBufferWrite);
3700ac4bd9a3 #184 #170 first basic flush test
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
350 buf.flush_blkmax = 1;
538
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
351 }
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
352 };
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
353
567
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
354 static size_t mock_write_limited_rate(
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
355 void const *ptr,
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
356 size_t size,
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
357 __attribute__((unused)) size_t nitems,
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
358 CxBuffer *buffer
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
359 ) {
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
360 // simulate limited target drain capacity
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
361 static bool full = false;
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
362 if (full) {
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
363 full = false;
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
364 return 0;
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
365 } else {
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
366 full = true;
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
367 return cxBufferWrite(ptr, size, nitems > 2 ? 2 : nitems, buffer);
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
368 }
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
369 }
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
370
538
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
371 TEST_F(BufferWrite, SizeOneFit) {
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
372 const char *data = "test";
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
373 ASSERT_EQ(buf.capacity, 8);
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
374 ASSERT_EQ(buf.pos, 4);
541
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
375 ASSERT_EQ(buf.size, 4);
538
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
376 size_t written = cxBufferWrite(data, 1, 4, &buf);
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
377 EXPECT_EQ(written, 4);
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
378 EXPECT_EQ(buf.size, 8);
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
379 EXPECT_EQ(buf.pos, 8);
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
380 EXPECT_EQ(buf.capacity, 8);
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
381 EXPECT_EQ(memcmp(buf.space, "preptest", 8), 0);
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
382 }
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
383
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
384 TEST_F(BufferWrite, SizeOneDiscard) {
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
385 const char *data = "testing";
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
386 ASSERT_EQ(buf.capacity, 8);
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
387 ASSERT_EQ(buf.pos, 4);
541
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
388 ASSERT_EQ(buf.size, 4);
538
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
389 size_t written = cxBufferWrite(data, 1, 7, &buf);
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
390 EXPECT_EQ(written, 4);
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
391 EXPECT_EQ(buf.size, 8);
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
392 EXPECT_EQ(buf.pos, 8);
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
393 EXPECT_EQ(buf.capacity, 8);
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
394 EXPECT_EQ(memcmp(buf.space, "preptest\0", 9), 0);
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
395 }
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
396
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
397 TEST_F(BufferWrite, SizeOneExtend) {
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
398 buf.flags |= CX_BUFFER_AUTO_EXTEND;
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
399 const char *data = "testing";
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
400 ASSERT_EQ(buf.capacity, 8);
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
401 ASSERT_EQ(buf.pos, 4);
541
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
402 ASSERT_EQ(buf.size, 4);
538
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
403 size_t written = cxBufferWrite(data, 1, 7, &buf);
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
404 EXPECT_EQ(written, 7);
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
405 EXPECT_EQ(buf.size, 11);
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
406 EXPECT_EQ(buf.pos, 11);
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
407 EXPECT_GE(buf.capacity, 11);
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
408 EXPECT_EQ(memcmp(buf.space, "preptesting", 11), 0);
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
409 }
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
410
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
411 TEST_F(BufferWrite, MultibyteFit) {
541
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
412 const char *data = "test";
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
413 ASSERT_EQ(buf.capacity, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
414 ASSERT_EQ(buf.pos, 4);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
415 ASSERT_EQ(buf.size, 4);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
416 size_t written = cxBufferWrite(data, 2, 2, &buf);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
417 EXPECT_EQ(written, 2);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
418 EXPECT_EQ(buf.size, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
419 EXPECT_EQ(buf.pos, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
420 EXPECT_EQ(buf.capacity, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
421 EXPECT_EQ(memcmp(buf.space, "preptest", 8), 0);
538
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
422 }
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
423
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
424 TEST_F(BufferWrite, MultibyteDiscard) {
542
45bcfd152f85 #170 no bug never existed
Mike Becker <universe@uap-core.de>
parents: 541
diff changeset
425 const char *data = "testing";
541
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
426 ASSERT_EQ(buf.capacity, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
427 ASSERT_EQ(buf.size, 4);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
428 buf.pos = 3;
542
45bcfd152f85 #170 no bug never existed
Mike Becker <universe@uap-core.de>
parents: 541
diff changeset
429 size_t written = cxBufferWrite(data, 2, 4, &buf);
538
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
430 // remember: whole elements are discarded if they do not fit
541
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
431 EXPECT_EQ(written, 2);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
432 EXPECT_EQ(buf.size, 7);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
433 EXPECT_EQ(buf.pos, 7);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
434 EXPECT_EQ(buf.capacity, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
435 EXPECT_EQ(memcmp(buf.space, "pretest\0", 8), 0);
538
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
436 }
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
437
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
438 TEST_F(BufferWrite, MultibyteExtend) {
541
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
439 buf.flags |= CX_BUFFER_AUTO_EXTEND;
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
440 const char *data = "tester";
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
441 ASSERT_EQ(buf.capacity, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
442 ASSERT_EQ(buf.size, 4);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
443 buf.pos = 3;
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
444 size_t written = cxBufferWrite(data, 2, 3, &buf);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
445 // remember: whole elements are discarded if they do not fit
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
446 EXPECT_EQ(written, 3);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
447 EXPECT_EQ(buf.size, 9);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
448 EXPECT_EQ(buf.pos, 9);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
449 EXPECT_GE(buf.capacity, 9);
566
d3100c987a39 fix two tests reading one byte too much
Mike Becker <universe@uap-core.de>
parents: 565
diff changeset
450 EXPECT_EQ(memcmp(buf.space, "pretester", 9), 0);
538
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
451 }
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
452
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
453 TEST_F(BufferWrite, PutcWrapperFit) {
541
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
454 ASSERT_EQ(buf.capacity, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
455 ASSERT_EQ(buf.pos, 4);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
456 ASSERT_EQ(buf.size, 4);
565
d756746ea94f fix putc tests to make more sense of the bitwise or
Mike Becker <universe@uap-core.de>
parents: 548
diff changeset
457 int c = cxBufferPut(&buf, 0x200 | 'a');
541
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
458 EXPECT_EQ(c, 'a');
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
459 EXPECT_EQ(buf.size, 5);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
460 EXPECT_EQ(buf.pos, 5);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
461 EXPECT_EQ(buf.capacity, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
462 EXPECT_EQ(memcmp(buf.space, "prepa\0", 6), 0);
538
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
463 }
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
464
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
465 TEST_F(BufferWrite, PutcWrapperDiscard) {
541
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
466 ASSERT_EQ(buf.capacity, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
467 ASSERT_EQ(buf.size, 4);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
468 buf.pos = 8;
565
d756746ea94f fix putc tests to make more sense of the bitwise or
Mike Becker <universe@uap-core.de>
parents: 548
diff changeset
469 int c = cxBufferPut(&buf, 0x200 | 'a');
541
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
470 EXPECT_EQ(c, EOF);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
471 EXPECT_EQ(buf.size, 4);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
472 EXPECT_EQ(buf.pos, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
473 EXPECT_EQ(buf.capacity, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
474 EXPECT_EQ(memcmp(buf.space, "prep\0\0\0\0\0", 9), 0);
538
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
475 }
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
476
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
477 TEST_F(BufferWrite, PutcWrapperExtend) {
541
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
478 buf.flags |= CX_BUFFER_AUTO_EXTEND;
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
479 ASSERT_EQ(buf.capacity, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
480 ASSERT_EQ(buf.size, 4);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
481 buf.pos = 8;
565
d756746ea94f fix putc tests to make more sense of the bitwise or
Mike Becker <universe@uap-core.de>
parents: 548
diff changeset
482 int c = cxBufferPut(&buf, 0x200 | 'a');
541
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
483 EXPECT_EQ(c, 'a');
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
484 EXPECT_EQ(buf.size, 9);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
485 EXPECT_EQ(buf.pos, 9);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
486 EXPECT_GE(buf.capacity, 9);
566
d3100c987a39 fix two tests reading one byte too much
Mike Becker <universe@uap-core.de>
parents: 565
diff changeset
487 EXPECT_EQ(memcmp(buf.space, "prep\0\0\0\0a", 9), 0);
538
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
488 }
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
489
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
490 TEST_F(BufferWrite, PutStringWrapperFit) {
541
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
491 const char *data = "test";
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
492 ASSERT_EQ(buf.capacity, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
493 ASSERT_EQ(buf.pos, 4);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
494 ASSERT_EQ(buf.size, 4);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
495 size_t written = cxBufferPutString(&buf, data);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
496 EXPECT_EQ(written, 4);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
497 EXPECT_EQ(buf.size, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
498 EXPECT_EQ(buf.pos, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
499 EXPECT_EQ(buf.capacity, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
500 EXPECT_EQ(memcmp(buf.space, "preptest", 8), 0);
538
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
501 }
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
502
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
503 TEST_F(BufferWrite, PutStringWrapperDiscard) {
541
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
504 const char *data = "testing";
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
505 ASSERT_EQ(buf.capacity, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
506 ASSERT_EQ(buf.pos, 4);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
507 ASSERT_EQ(buf.size, 4);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
508 size_t written = cxBufferPutString(&buf, data);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
509 EXPECT_EQ(written, 4);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
510 EXPECT_EQ(buf.size, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
511 EXPECT_EQ(buf.pos, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
512 EXPECT_EQ(buf.capacity, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
513 EXPECT_EQ(memcmp(buf.space, "preptest\0", 9), 0);
538
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
514 }
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
515
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
516 TEST_F(BufferWrite, PutStringWrapperExtend) {
541
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
517 buf.flags |= CX_BUFFER_AUTO_EXTEND;
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
518 const char *data = "testing";
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
519 ASSERT_EQ(buf.capacity, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
520 ASSERT_EQ(buf.pos, 4);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
521 ASSERT_EQ(buf.size, 4);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
522 size_t written = cxBufferPutString(&buf, data);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
523 EXPECT_EQ(written, 7);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
524 EXPECT_EQ(buf.size, 11);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
525 EXPECT_EQ(buf.pos, 11);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
526 EXPECT_GE(buf.capacity, 11);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
527 EXPECT_EQ(memcmp(buf.space, "preptesting", 11), 0);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
528 }
538
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
529
541
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
530 TEST_F(BufferWrite, MultOverflow) {
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
531 const char *data = "testing";
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
532 ASSERT_EQ(buf.capacity, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
533 ASSERT_EQ(buf.pos, 4);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
534 ASSERT_EQ(buf.size, 4);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
535 size_t written = cxBufferWrite(data, 8, SIZE_MAX / 4, &buf);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
536 EXPECT_EQ(written, 0);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
537 EXPECT_EQ(buf.capacity, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
538 EXPECT_EQ(buf.pos, 4);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
539 EXPECT_EQ(buf.size, 4);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
540 EXPECT_EQ(memcmp(buf.space, "prep\0", 5), 0);
538
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
541 }
541
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
542
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
543 TEST_F(BufferWrite, MaxCapaOverflow) {
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
544 buf.flags |= CX_BUFFER_AUTO_EXTEND;
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
545 const char *data = "testing";
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
546 ASSERT_EQ(buf.capacity, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
547 ASSERT_EQ(buf.pos, 4);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
548 ASSERT_EQ(buf.size, 4);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
549 size_t written = cxBufferWrite(data, 1, SIZE_MAX - 2, &buf);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
550 EXPECT_EQ(written, 0);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
551 EXPECT_EQ(buf.capacity, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
552 EXPECT_EQ(buf.pos, 4);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
553 EXPECT_EQ(buf.size, 4);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
554 EXPECT_EQ(memcmp(buf.space, "prep\0", 5), 0);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
555 }
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
556
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
557 TEST_F(BufferWrite, OnlyOverwrite) {
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
558 buf.flags |= CX_BUFFER_AUTO_EXTEND;
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
559 ASSERT_EQ(buf.capacity, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
560 memcpy(buf.space, "preptest", 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
561 buf.pos = 3;
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
562 buf.size = 8;
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
563 size_t written = cxBufferWrite("XXX", 2, 2, &buf);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
564 EXPECT_EQ(written, 2);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
565 EXPECT_EQ(buf.capacity, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
566 EXPECT_EQ(buf.size, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
567 EXPECT_EQ(buf.pos, 7);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
568 EXPECT_EQ(memcmp(buf.space, "preXXX\0t", 8), 0);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
569 }
545
3700ac4bd9a3 #184 #170 first basic flush test
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
570
3700ac4bd9a3 #184 #170 first basic flush test
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
571 TEST_F(BufferWrite, FlushAtCapacity) {
3700ac4bd9a3 #184 #170 first basic flush test
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
572 enableFlushing();
3700ac4bd9a3 #184 #170 first basic flush test
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
573 ASSERT_EQ(buf.capacity, 8);
3700ac4bd9a3 #184 #170 first basic flush test
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
574 ASSERT_EQ(buf.pos, 4);
3700ac4bd9a3 #184 #170 first basic flush test
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
575 size_t written = cxBufferWrite("foo", 1, 3, &buf);
3700ac4bd9a3 #184 #170 first basic flush test
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
576 EXPECT_EQ(written, 3);
3700ac4bd9a3 #184 #170 first basic flush test
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
577 ASSERT_EQ(buf.pos, 7);
3700ac4bd9a3 #184 #170 first basic flush test
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
578 ASSERT_EQ(buf.size, 7);
3700ac4bd9a3 #184 #170 first basic flush test
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
579 ASSERT_EQ(target.pos, 0);
3700ac4bd9a3 #184 #170 first basic flush test
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
580 ASSERT_EQ(target.size, 0);
3700ac4bd9a3 #184 #170 first basic flush test
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
581 written = cxBufferWrite("hello", 1, 5, &buf);
3700ac4bd9a3 #184 #170 first basic flush test
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
582 EXPECT_EQ(written, 5);
3700ac4bd9a3 #184 #170 first basic flush test
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
583 EXPECT_EQ(buf.pos, 0);
3700ac4bd9a3 #184 #170 first basic flush test
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
584 EXPECT_EQ(buf.size, 0);
3700ac4bd9a3 #184 #170 first basic flush test
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
585 EXPECT_EQ(buf.capacity, 8);
3700ac4bd9a3 #184 #170 first basic flush test
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
586 EXPECT_EQ(target.pos, 12);
3700ac4bd9a3 #184 #170 first basic flush test
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
587 ASSERT_EQ(target.size, 12);
3700ac4bd9a3 #184 #170 first basic flush test
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
588 EXPECT_EQ(memcmp(target.space, "prepfoohello", 12), 0);
3700ac4bd9a3 #184 #170 first basic flush test
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
589 }
546
900795d59d03 #184 #170 flush at threshold test
Mike Becker <universe@uap-core.de>
parents: 545
diff changeset
590
900795d59d03 #184 #170 flush at threshold test
Mike Becker <universe@uap-core.de>
parents: 545
diff changeset
591 TEST_F(BufferWrite, FlushAtThreshold) {
900795d59d03 #184 #170 flush at threshold test
Mike Becker <universe@uap-core.de>
parents: 545
diff changeset
592 enableFlushing();
900795d59d03 #184 #170 flush at threshold test
Mike Becker <universe@uap-core.de>
parents: 545
diff changeset
593 buf.flush_threshold = 12;
900795d59d03 #184 #170 flush at threshold test
Mike Becker <universe@uap-core.de>
parents: 545
diff changeset
594 buf.flags |= CX_BUFFER_AUTO_EXTEND;
900795d59d03 #184 #170 flush at threshold test
Mike Becker <universe@uap-core.de>
parents: 545
diff changeset
595 ASSERT_EQ(buf.capacity, 8);
900795d59d03 #184 #170 flush at threshold test
Mike Becker <universe@uap-core.de>
parents: 545
diff changeset
596 ASSERT_EQ(buf.pos, 4);
900795d59d03 #184 #170 flush at threshold test
Mike Becker <universe@uap-core.de>
parents: 545
diff changeset
597 size_t written = cxBufferWrite("foobar", 1, 6, &buf);
900795d59d03 #184 #170 flush at threshold test
Mike Becker <universe@uap-core.de>
parents: 545
diff changeset
598 EXPECT_EQ(written, 6);
900795d59d03 #184 #170 flush at threshold test
Mike Becker <universe@uap-core.de>
parents: 545
diff changeset
599 ASSERT_EQ(buf.pos, 10);
900795d59d03 #184 #170 flush at threshold test
Mike Becker <universe@uap-core.de>
parents: 545
diff changeset
600 ASSERT_EQ(buf.size, 10);
900795d59d03 #184 #170 flush at threshold test
Mike Becker <universe@uap-core.de>
parents: 545
diff changeset
601 ASSERT_GE(buf.capacity, 10);
900795d59d03 #184 #170 flush at threshold test
Mike Becker <universe@uap-core.de>
parents: 545
diff changeset
602 ASSERT_LE(buf.capacity, 12);
900795d59d03 #184 #170 flush at threshold test
Mike Becker <universe@uap-core.de>
parents: 545
diff changeset
603 ASSERT_EQ(target.pos, 0);
900795d59d03 #184 #170 flush at threshold test
Mike Becker <universe@uap-core.de>
parents: 545
diff changeset
604 ASSERT_EQ(target.size, 0);
900795d59d03 #184 #170 flush at threshold test
Mike Becker <universe@uap-core.de>
parents: 545
diff changeset
605 written = cxBufferWrite("hello", 1, 5, &buf);
900795d59d03 #184 #170 flush at threshold test
Mike Becker <universe@uap-core.de>
parents: 545
diff changeset
606 EXPECT_EQ(written, 5);
900795d59d03 #184 #170 flush at threshold test
Mike Becker <universe@uap-core.de>
parents: 545
diff changeset
607 EXPECT_EQ(buf.pos, 0);
900795d59d03 #184 #170 flush at threshold test
Mike Becker <universe@uap-core.de>
parents: 545
diff changeset
608 EXPECT_EQ(buf.size, 0);
900795d59d03 #184 #170 flush at threshold test
Mike Becker <universe@uap-core.de>
parents: 545
diff changeset
609 EXPECT_LE(buf.capacity, 12);
900795d59d03 #184 #170 flush at threshold test
Mike Becker <universe@uap-core.de>
parents: 545
diff changeset
610 EXPECT_EQ(target.pos, 15);
900795d59d03 #184 #170 flush at threshold test
Mike Becker <universe@uap-core.de>
parents: 545
diff changeset
611 ASSERT_EQ(target.size, 15);
900795d59d03 #184 #170 flush at threshold test
Mike Becker <universe@uap-core.de>
parents: 545
diff changeset
612 EXPECT_EQ(memcmp(target.space, "prepfoobarhello", 15), 0);
900795d59d03 #184 #170 flush at threshold test
Mike Becker <universe@uap-core.de>
parents: 545
diff changeset
613 }
547
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
614
567
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
615 TEST_F(BufferWrite, FlushRateLimited) {
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
616 enableFlushing();
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
617 // limit the rate of the flush function and the capacity of the target
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
618 target.capacity = 16;
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
619 target.flags &= ~CX_BUFFER_AUTO_EXTEND;
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
620 buf.flush_func = (cx_write_func) mock_write_limited_rate;
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
621 ASSERT_EQ(buf.capacity, 8);
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
622 ASSERT_EQ(buf.pos, 4);
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
623 size_t written = cxBufferWrite("foo", 1, 3, &buf);
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
624 EXPECT_EQ(written, 3);
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
625 ASSERT_EQ(buf.pos, 7);
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
626 ASSERT_EQ(buf.size, 7);
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
627 ASSERT_EQ(target.pos, 0);
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
628 ASSERT_EQ(target.size, 0);
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
629 written = cxBufferWrite("hello, world!", 1, 13, &buf);
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
630 // " world!" fits into this buffer, the remaining stuff is flushed out
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
631 EXPECT_EQ(written, 13);
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
632 EXPECT_EQ(buf.pos, 7);
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
633 EXPECT_EQ(buf.size, 7);
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
634 EXPECT_EQ(buf.capacity, 8);
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
635 EXPECT_EQ(memcmp(buf.space, " world!", 7), 0);
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
636 EXPECT_EQ(target.pos, 13);
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
637 ASSERT_EQ(target.size, 13);
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
638 EXPECT_EQ(target.capacity, 16);
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
639 EXPECT_EQ(memcmp(target.space, "prepfoohello,", 13), 0);
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
640 }
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
641
548
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
642 class BufferSeek : public BufferFixture {
547
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
643 };
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
644
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
645 TEST_F(BufferSeek, SetZero) {
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
646 int result = cxBufferSeek(&buf, 0, SEEK_SET);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
647 EXPECT_EQ(result, 0);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
648 EXPECT_EQ(buf.pos, 0);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
649 }
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
650
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
651 TEST_F(BufferSeek, SetValid) {
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
652 int result = cxBufferSeek(&buf, 5, SEEK_SET);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
653 EXPECT_EQ(result, 0);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
654 EXPECT_EQ(buf.pos, 5);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
655 }
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
656
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
657 TEST_F(BufferSeek, SetInvalid) {
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
658 ASSERT_EQ(buf.pos, 3);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
659 int result = cxBufferSeek(&buf, 6, SEEK_SET);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
660 EXPECT_NE(result, 0);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
661 EXPECT_EQ(buf.pos, 3);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
662 }
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
663
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
664 TEST_F(BufferSeek, CurZero) {
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
665 ASSERT_EQ(buf.pos, 3);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
666 int result = cxBufferSeek(&buf, 0, SEEK_CUR);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
667 EXPECT_EQ(result, 0);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
668 EXPECT_EQ(buf.pos, 3);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
669 }
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
670
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
671 TEST_F(BufferSeek, CurValidPositive) {
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
672 ASSERT_EQ(buf.pos, 3);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
673 int result = cxBufferSeek(&buf, 2, SEEK_CUR);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
674 EXPECT_EQ(result, 0);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
675 EXPECT_EQ(buf.pos, 5);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
676 }
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
677
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
678 TEST_F(BufferSeek, CurValidNegative) {
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
679 ASSERT_EQ(buf.pos, 3);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
680 int result = cxBufferSeek(&buf, -3, SEEK_CUR);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
681 EXPECT_EQ(result, 0);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
682 EXPECT_EQ(buf.pos, 0);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
683 }
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
684
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
685 TEST_F(BufferSeek, CurInvalidPositive) {
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
686 ASSERT_EQ(buf.pos, 3);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
687 int result = cxBufferSeek(&buf, 3, SEEK_CUR);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
688 EXPECT_NE(result, 0);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
689 EXPECT_EQ(buf.pos, 3);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
690 }
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
691
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
692 TEST_F(BufferSeek, CurInvalidNegative) {
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
693 ASSERT_EQ(buf.pos, 3);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
694 int result = cxBufferSeek(&buf, -4, SEEK_CUR);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
695 EXPECT_NE(result, 0);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
696 EXPECT_EQ(buf.pos, 3);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
697 }
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
698
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
699 TEST_F(BufferSeek, EndZero) {
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
700 ASSERT_EQ(buf.size, 6);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
701 int result = cxBufferSeek(&buf, 0, SEEK_END);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
702 // the (past-the-)end position is always invalid
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
703 EXPECT_NE(result, 0);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
704 EXPECT_EQ(buf.pos, 3);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
705 }
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
706
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
707 TEST_F(BufferSeek, EndValid) {
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
708 ASSERT_EQ(buf.size, 6);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
709 int result = cxBufferSeek(&buf, -6, SEEK_END);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
710 EXPECT_EQ(result, 0);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
711 EXPECT_EQ(buf.pos, 0);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
712 }
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
713
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
714 TEST_F(BufferSeek, EndInvalid) {
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
715 ASSERT_EQ(buf.size, 6);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
716 int result = cxBufferSeek(&buf, 1, SEEK_END);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
717 EXPECT_NE(result, 0);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
718 EXPECT_EQ(buf.pos, 3);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
719 }
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
720
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
721 TEST_F(BufferSeek, WhenceInvalid) {
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
722 ASSERT_EQ(buf.size, 6);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
723 ASSERT_EQ(buf.pos, 3);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
724 int result = cxBufferSeek(&buf, 2, 9000);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
725 EXPECT_NE(result, 0);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
726 EXPECT_EQ(buf.size, 6);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
727 EXPECT_EQ(buf.pos, 3);
548
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
728 }
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
729
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
730 class BufferEof : public BufferFixture {
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
731 };
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
732
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
733 TEST_F(BufferEof, Reached) {
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
734 buf.pos = buf.size;
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
735 EXPECT_TRUE(cxBufferEof(&buf));
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
736 buf.pos = buf.size - 1;
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
737 ASSERT_FALSE(cxBufferEof(&buf));
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
738 cxBufferPut(&buf, 'a');
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
739 EXPECT_TRUE(cxBufferEof(&buf));
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
740 }
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
741
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
742 TEST_F(BufferEof, NotReached) {
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
743 buf.pos = buf.size - 1;
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
744 EXPECT_FALSE(cxBufferEof(&buf));
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
745 buf.pos = 0;
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
746 cxBufferWrite("test", 1, 5, &buf);
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
747 EXPECT_FALSE(cxBufferEof(&buf));
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
748 }
568
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
749
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
750 class BufferRead : public ::testing::Test {
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
751 protected:
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
752 CxBuffer buf{};
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
753
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
754 void SetUp() override {
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
755 cxBufferInit(&buf, nullptr, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT);
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
756 buf.capacity = 8; // artificially reduce capacity to check OOB writes
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
757 memset(buf.space, 0, 16);
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
758 memcpy(buf.space, "some data", 9);
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
759 buf.size = 9;
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
760 }
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
761
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
762 void TearDown() override {
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
763 cxBufferDestroy(&buf);
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
764 }
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
765 };
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
766
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
767 TEST_F(BufferRead, GetByte) {
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
768 buf.pos = 2;
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
769 EXPECT_EQ(cxBufferGet(&buf), 'm');
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
770 EXPECT_EQ(cxBufferGet(&buf), 'e');
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
771 EXPECT_EQ(cxBufferGet(&buf), ' ');
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
772 EXPECT_EQ(cxBufferGet(&buf), 'd');
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
773 EXPECT_EQ(buf.pos, 6);
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
774 }
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
775
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
776 TEST_F(BufferRead, GetEof) {
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
777 buf.pos = buf.size;
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
778 EXPECT_EQ(cxBufferGet(&buf), EOF);
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
779 }

mercurial