tests/test_buffer.cpp

Fri, 29 Dec 2023 17:17:43 +0100

author
Mike Becker <universe@uap-core.de>
date
Fri, 29 Dec 2023 17:17:43 +0100
changeset 779
e9611e4d0213
parent 761
61d5197d612b
child 789
9b2f5661bebd
permissions
-rw-r--r--

revert the insanely stupid idea to automatically run the test program on default build

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
683
aa0d09f2d81c add convenience functions for allocating a buffer on the heap
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
130 TEST(BufferInit, OnHeap) {
aa0d09f2d81c add convenience functions for allocating a buffer on the heap
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
131 CxTestingAllocator alloc;
aa0d09f2d81c add convenience functions for allocating a buffer on the heap
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
132 CxBuffer *buf;
aa0d09f2d81c add convenience functions for allocating a buffer on the heap
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
133 void *space = cxMalloc(&alloc, 16);
aa0d09f2d81c add convenience functions for allocating a buffer on the heap
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
134 buf = cxBufferCreate(space, 16, &alloc, CX_BUFFER_FREE_CONTENTS);
aa0d09f2d81c add convenience functions for allocating a buffer on the heap
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
135 EXPECT_NE(buf, nullptr);
aa0d09f2d81c add convenience functions for allocating a buffer on the heap
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
136 expect_default_flush_config(buf);
aa0d09f2d81c add convenience functions for allocating a buffer on the heap
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
137 EXPECT_EQ(buf->space, space);
aa0d09f2d81c add convenience functions for allocating a buffer on the heap
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
138 EXPECT_EQ(buf->flags & CX_BUFFER_AUTO_EXTEND, 0);
aa0d09f2d81c add convenience functions for allocating a buffer on the heap
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
139 EXPECT_EQ(buf->flags & CX_BUFFER_FREE_CONTENTS, CX_BUFFER_FREE_CONTENTS);
aa0d09f2d81c add convenience functions for allocating a buffer on the heap
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
140 EXPECT_EQ(buf->pos, 0);
aa0d09f2d81c add convenience functions for allocating a buffer on the heap
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
141 EXPECT_EQ(buf->size, 0);
aa0d09f2d81c add convenience functions for allocating a buffer on the heap
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
142 EXPECT_EQ(buf->capacity, 16);
aa0d09f2d81c add convenience functions for allocating a buffer on the heap
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
143 EXPECT_EQ(buf->allocator, &alloc);
aa0d09f2d81c add convenience functions for allocating a buffer on the heap
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
144 cxBufferFree(buf);
aa0d09f2d81c add convenience functions for allocating a buffer on the heap
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
145 EXPECT_TRUE(alloc.verify());
aa0d09f2d81c add convenience functions for allocating a buffer on the heap
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
146 }
aa0d09f2d81c add convenience functions for allocating a buffer on the heap
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
147
535
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
148 class BufferShiftFixture : public ::testing::Test {
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
149 protected:
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
150 void SetUp() override {
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
151 ASSERT_TRUE(alloc.verify());
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
152 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
153 memcpy(buf.space, "test____________", 16);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
154 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
155 buf.pos = 4;
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
156 buf.size = 4;
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
157 }
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
158
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
159 void TearDown() override {
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
160 cxBufferDestroy(&buf);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
161 EXPECT_TRUE(alloc.verify());
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
162 }
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
163
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
164 CxTestingAllocator alloc;
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
165 CxBuffer buf{};
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
166 };
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
167
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
168 class BufferShiftLeft : public BufferShiftFixture {
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
169 };
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
170
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
171 TEST_F(BufferShiftLeft, Zero) {
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
172 ASSERT_EQ(buf.pos, 4);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
173 ASSERT_EQ(buf.size, 4);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
174 int ret = cxBufferShiftLeft(&buf, 0);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
175 EXPECT_EQ(ret, 0);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
176 EXPECT_EQ(buf.pos, 4);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
177 EXPECT_EQ(buf.size, 4);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
178 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
179 }
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
180
547
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
181 TEST_F(BufferShiftLeft, ZeroOffsetInterface) {
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
182 ASSERT_EQ(buf.pos, 4);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
183 ASSERT_EQ(buf.size, 4);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
184 int ret = cxBufferShift(&buf, -0);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
185 EXPECT_EQ(ret, 0);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
186 EXPECT_EQ(buf.pos, 4);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
187 EXPECT_EQ(buf.size, 4);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
188 EXPECT_TRUE(memcmp(buf.space, "test________", 8) == 0);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
189 }
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
190
535
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
191 TEST_F(BufferShiftLeft, Standard) {
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
192 ASSERT_EQ(buf.pos, 4);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
193 ASSERT_EQ(buf.size, 4);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
194 int ret = cxBufferShiftLeft(&buf, 2);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
195 EXPECT_EQ(ret, 0);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
196 EXPECT_EQ(buf.pos, 2);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
197 EXPECT_EQ(buf.size, 2);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
198 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
199 }
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
200
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
201 TEST_F(BufferShiftLeft, Overshift) {
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
202 ASSERT_LT(buf.pos, 6);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
203 ASSERT_LT(buf.size, 6);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
204 int ret = cxBufferShiftLeft(&buf, 6);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
205 EXPECT_EQ(ret, 0);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
206 EXPECT_EQ(buf.pos, 0);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
207 EXPECT_EQ(buf.size, 0);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
208 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
209 }
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
210
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
211 TEST_F(BufferShiftLeft, OvershiftPosOnly) {
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
212 buf.pos = 2;
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
213 ASSERT_EQ(buf.size, 4);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
214 int ret = cxBufferShiftLeft(&buf, 3);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
215 EXPECT_EQ(ret, 0);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
216 EXPECT_EQ(buf.pos, 0);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
217 EXPECT_EQ(buf.size, 1);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
218 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
219 }
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
220
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
221 TEST_F(BufferShiftLeft, OffsetInterface) {
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
222 buf.pos = 3;
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
223 ASSERT_EQ(buf.size, 4);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
224 int ret = cxBufferShift(&buf, -2);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
225 EXPECT_EQ(ret, 0);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
226 EXPECT_EQ(buf.pos, 1);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
227 EXPECT_EQ(buf.size, 2);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
228 EXPECT_TRUE(memcmp(buf.space, "stst________", 8) == 0);
530
e866516cac17 #170 first buffer tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
229 }
535
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
230
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
231 class BufferShiftRight : public BufferShiftFixture {
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
232 };
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
233
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
234 TEST_F(BufferShiftRight, Zero) {
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
235 ASSERT_EQ(buf.pos, 4);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
236 ASSERT_EQ(buf.size, 4);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
237 int ret = cxBufferShiftRight(&buf, 0);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
238 EXPECT_EQ(ret, 0);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
239 EXPECT_EQ(buf.pos, 4);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
240 EXPECT_EQ(buf.size, 4);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
241 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
242 }
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
243
547
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
244 TEST_F(BufferShiftRight, ZeroOffsetInterface) {
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
245 ASSERT_EQ(buf.pos, 4);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
246 ASSERT_EQ(buf.size, 4);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
247 int ret = cxBufferShift(&buf, +0);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
248 EXPECT_EQ(ret, 0);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
249 EXPECT_EQ(buf.pos, 4);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
250 EXPECT_EQ(buf.size, 4);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
251 EXPECT_TRUE(memcmp(buf.space, "test________", 8) == 0);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
252 }
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
253
535
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
254 TEST_F(BufferShiftRight, Standard) {
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
255 ASSERT_EQ(buf.pos, 4);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
256 ASSERT_EQ(buf.size, 4);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
257 int ret = cxBufferShiftRight(&buf, 3);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
258 EXPECT_EQ(ret, 0);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
259 EXPECT_EQ(buf.pos, 7);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
260 EXPECT_EQ(buf.size, 7);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
261 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
262 }
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
263
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
264 TEST_F(BufferShiftRight, OvershiftDiscard) {
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
265 ASSERT_EQ(buf.pos, 4);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
266 ASSERT_EQ(buf.size, 4);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
267 ASSERT_EQ(buf.capacity, 8);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
268 int ret = cxBufferShiftRight(&buf, 6);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
269 EXPECT_EQ(ret, 0);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
270 EXPECT_EQ(buf.pos, 8);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
271 EXPECT_EQ(buf.size, 8);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
272 EXPECT_EQ(buf.capacity, 8);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
273 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
274 }
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
275
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
276 TEST_F(BufferShiftRight, OvershiftExtend) {
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
277 ASSERT_EQ(buf.pos, 4);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
278 ASSERT_EQ(buf.size, 4);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
279 ASSERT_EQ(buf.capacity, 8);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
280 buf.flags |= CX_BUFFER_AUTO_EXTEND;
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
281 int ret = cxBufferShiftRight(&buf, 6);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
282 EXPECT_EQ(ret, 0);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
283 EXPECT_EQ(buf.pos, 10);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
284 EXPECT_EQ(buf.size, 10);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
285 EXPECT_GE(buf.capacity, 10);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
286 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
287 }
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
288
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
289 TEST_F(BufferShiftRight, OffsetInterface) {
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
290 buf.pos = 3;
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
291 ASSERT_EQ(buf.size, 4);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
292 int ret = cxBufferShift(&buf, 2);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
293 EXPECT_EQ(ret, 0);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
294 EXPECT_EQ(buf.pos, 5);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
295 EXPECT_EQ(buf.size, 6);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
296 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
297 }
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
298
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
299 TEST(BufferMinimumCapacity, Sufficient) {
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
300 CxTestingAllocator alloc;
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
301 auto space = cxMalloc(&alloc, 8);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
302 CxBuffer buf;
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
303 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
304 memcpy(space, "Testing", 8);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
305 buf.size = 8;
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
306 cxBufferMinimumCapacity(&buf, 6);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
307 EXPECT_EQ(buf.capacity, 8);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
308 EXPECT_EQ(buf.size, 8);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
309 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
310 cxBufferDestroy(&buf);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
311 EXPECT_TRUE(alloc.verify());
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
312 }
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
313
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
314 TEST(BufferMinimumCapacity, Extend) {
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
315 CxTestingAllocator alloc;
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
316 auto space = cxMalloc(&alloc, 8);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
317 CxBuffer buf;
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
318 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
319 memcpy(space, "Testing", 8);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
320 buf.size = 8;
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
321 cxBufferMinimumCapacity(&buf, 16);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
322 EXPECT_EQ(buf.capacity, 16);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
323 EXPECT_EQ(buf.size, 8);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
324 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
325 cxBufferDestroy(&buf);
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
326 EXPECT_TRUE(alloc.verify());
2ff6e9184468 #170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
327 }
536
cb9b9739055e #170 add cxBufferClear test
Mike Becker <universe@uap-core.de>
parents: 535
diff changeset
328
cb9b9739055e #170 add cxBufferClear test
Mike Becker <universe@uap-core.de>
parents: 535
diff changeset
329 TEST(BufferClear, Test) {
cb9b9739055e #170 add cxBufferClear test
Mike Becker <universe@uap-core.de>
parents: 535
diff changeset
330 char space[16];
cb9b9739055e #170 add cxBufferClear test
Mike Becker <universe@uap-core.de>
parents: 535
diff changeset
331 strcpy(space, "clear test");
cb9b9739055e #170 add cxBufferClear test
Mike Becker <universe@uap-core.de>
parents: 535
diff changeset
332 CxBuffer buf;
cb9b9739055e #170 add cxBufferClear test
Mike Becker <universe@uap-core.de>
parents: 535
diff changeset
333 cxBufferInit(&buf, space, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT);
cb9b9739055e #170 add cxBufferClear test
Mike Becker <universe@uap-core.de>
parents: 535
diff changeset
334 ASSERT_EQ(buf.size, 0);
cb9b9739055e #170 add cxBufferClear test
Mike Becker <universe@uap-core.de>
parents: 535
diff changeset
335 // only clear the used part of the buffer
cb9b9739055e #170 add cxBufferClear test
Mike Becker <universe@uap-core.de>
parents: 535
diff changeset
336 cxBufferClear(&buf);
cb9b9739055e #170 add cxBufferClear test
Mike Becker <universe@uap-core.de>
parents: 535
diff changeset
337 EXPECT_EQ(memcmp(space, "clear test", 10), 0);
cb9b9739055e #170 add cxBufferClear test
Mike Becker <universe@uap-core.de>
parents: 535
diff changeset
338 buf.size = 5;
cb9b9739055e #170 add cxBufferClear test
Mike Becker <universe@uap-core.de>
parents: 535
diff changeset
339 buf.pos = 3;
cb9b9739055e #170 add cxBufferClear test
Mike Becker <universe@uap-core.de>
parents: 535
diff changeset
340 cxBufferClear(&buf);
cb9b9739055e #170 add cxBufferClear test
Mike Becker <universe@uap-core.de>
parents: 535
diff changeset
341 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
342 EXPECT_EQ(buf.size, 0);
cb9b9739055e #170 add cxBufferClear test
Mike Becker <universe@uap-core.de>
parents: 535
diff changeset
343 EXPECT_EQ(buf.pos, 0);
538
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
344 cxBufferDestroy(&buf);
536
cb9b9739055e #170 add cxBufferClear test
Mike Becker <universe@uap-core.de>
parents: 535
diff changeset
345 }
538
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
346
761
61d5197d612b add cxBufferReset() - resolves #338
Mike Becker <universe@uap-core.de>
parents: 683
diff changeset
347 TEST(BufferReset, Test) {
61d5197d612b add cxBufferReset() - resolves #338
Mike Becker <universe@uap-core.de>
parents: 683
diff changeset
348 char space[16];
61d5197d612b add cxBufferReset() - resolves #338
Mike Becker <universe@uap-core.de>
parents: 683
diff changeset
349 strcpy(space, "reset test");
61d5197d612b add cxBufferReset() - resolves #338
Mike Becker <universe@uap-core.de>
parents: 683
diff changeset
350 CxBuffer buf;
61d5197d612b add cxBufferReset() - resolves #338
Mike Becker <universe@uap-core.de>
parents: 683
diff changeset
351 cxBufferInit(&buf, space, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT);
61d5197d612b add cxBufferReset() - resolves #338
Mike Becker <universe@uap-core.de>
parents: 683
diff changeset
352 buf.size = 5;
61d5197d612b add cxBufferReset() - resolves #338
Mike Becker <universe@uap-core.de>
parents: 683
diff changeset
353 buf.pos = 3;
61d5197d612b add cxBufferReset() - resolves #338
Mike Becker <universe@uap-core.de>
parents: 683
diff changeset
354 cxBufferReset(&buf);
61d5197d612b add cxBufferReset() - resolves #338
Mike Becker <universe@uap-core.de>
parents: 683
diff changeset
355 EXPECT_EQ(memcmp(space, "reset test", 10), 0);
61d5197d612b add cxBufferReset() - resolves #338
Mike Becker <universe@uap-core.de>
parents: 683
diff changeset
356 EXPECT_EQ(buf.size, 0);
61d5197d612b add cxBufferReset() - resolves #338
Mike Becker <universe@uap-core.de>
parents: 683
diff changeset
357 EXPECT_EQ(buf.pos, 0);
61d5197d612b add cxBufferReset() - resolves #338
Mike Becker <universe@uap-core.de>
parents: 683
diff changeset
358 cxBufferDestroy(&buf);
61d5197d612b add cxBufferReset() - resolves #338
Mike Becker <universe@uap-core.de>
parents: 683
diff changeset
359 }
61d5197d612b add cxBufferReset() - resolves #338
Mike Becker <universe@uap-core.de>
parents: 683
diff changeset
360
538
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
361 class BufferWrite : public ::testing::Test {
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
362 protected:
545
3700ac4bd9a3 #184 #170 first basic flush test
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
363 CxBuffer buf{}, target{};
538
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
364
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
365 void SetUp() override {
545
3700ac4bd9a3 #184 #170 first basic flush test
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
366 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
367 cxBufferInit(&buf, nullptr, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT);
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
368 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
369 memset(buf.space, 0, 16);
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
370 memcpy(buf.space, "prep", 4);
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
371 buf.size = buf.pos = 4;
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
372 }
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
373
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
374 void TearDown() override {
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
375 cxBufferDestroy(&buf);
545
3700ac4bd9a3 #184 #170 first basic flush test
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
376 cxBufferDestroy(&target);
3700ac4bd9a3 #184 #170 first basic flush test
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
377 }
3700ac4bd9a3 #184 #170 first basic flush test
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
378
3700ac4bd9a3 #184 #170 first basic flush test
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
379 void enableFlushing() {
3700ac4bd9a3 #184 #170 first basic flush test
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
380 buf.flush_target = &target;
3700ac4bd9a3 #184 #170 first basic flush test
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
381 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
382 buf.flush_blkmax = 1;
538
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 };
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
385
567
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
386 static size_t mock_write_limited_rate(
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
387 void const *ptr,
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
388 size_t size,
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
389 __attribute__((unused)) size_t nitems,
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
390 CxBuffer *buffer
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
391 ) {
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
392 // simulate limited target drain capacity
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
393 static bool full = false;
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
394 if (full) {
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
395 full = false;
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
396 return 0;
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
397 } else {
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
398 full = true;
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
399 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
400 }
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
401 }
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
402
538
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
403 TEST_F(BufferWrite, SizeOneFit) {
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
404 const char *data = "test";
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
405 ASSERT_EQ(buf.capacity, 8);
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
406 ASSERT_EQ(buf.pos, 4);
541
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
407 ASSERT_EQ(buf.size, 4);
538
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
408 size_t written = cxBufferWrite(data, 1, 4, &buf);
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
409 EXPECT_EQ(written, 4);
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
410 EXPECT_EQ(buf.size, 8);
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
411 EXPECT_EQ(buf.pos, 8);
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
412 EXPECT_EQ(buf.capacity, 8);
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
413 EXPECT_EQ(memcmp(buf.space, "preptest", 8), 0);
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
414 }
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
415
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
416 TEST_F(BufferWrite, SizeOneDiscard) {
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
417 const char *data = "testing";
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
418 ASSERT_EQ(buf.capacity, 8);
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
419 ASSERT_EQ(buf.pos, 4);
541
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
420 ASSERT_EQ(buf.size, 4);
538
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
421 size_t written = cxBufferWrite(data, 1, 7, &buf);
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
422 EXPECT_EQ(written, 4);
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
423 EXPECT_EQ(buf.size, 8);
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
424 EXPECT_EQ(buf.pos, 8);
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
425 EXPECT_EQ(buf.capacity, 8);
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
426 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
427 }
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
428
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
429 TEST_F(BufferWrite, SizeOneExtend) {
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
430 buf.flags |= CX_BUFFER_AUTO_EXTEND;
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
431 const char *data = "testing";
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
432 ASSERT_EQ(buf.capacity, 8);
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
433 ASSERT_EQ(buf.pos, 4);
541
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
434 ASSERT_EQ(buf.size, 4);
538
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
435 size_t written = cxBufferWrite(data, 1, 7, &buf);
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
436 EXPECT_EQ(written, 7);
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
437 EXPECT_EQ(buf.size, 11);
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
438 EXPECT_EQ(buf.pos, 11);
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
439 EXPECT_GE(buf.capacity, 11);
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
440 EXPECT_EQ(memcmp(buf.space, "preptesting", 11), 0);
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
441 }
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
442
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
443 TEST_F(BufferWrite, MultibyteFit) {
541
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
444 const char *data = "test";
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
445 ASSERT_EQ(buf.capacity, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
446 ASSERT_EQ(buf.pos, 4);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
447 ASSERT_EQ(buf.size, 4);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
448 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
449 EXPECT_EQ(written, 2);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
450 EXPECT_EQ(buf.size, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
451 EXPECT_EQ(buf.pos, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
452 EXPECT_EQ(buf.capacity, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
453 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
454 }
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
455
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
456 TEST_F(BufferWrite, MultibyteDiscard) {
542
45bcfd152f85 #170 no bug never existed
Mike Becker <universe@uap-core.de>
parents: 541
diff changeset
457 const char *data = "testing";
541
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
458 ASSERT_EQ(buf.capacity, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
459 ASSERT_EQ(buf.size, 4);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
460 buf.pos = 3;
542
45bcfd152f85 #170 no bug never existed
Mike Becker <universe@uap-core.de>
parents: 541
diff changeset
461 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
462 // 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
463 EXPECT_EQ(written, 2);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
464 EXPECT_EQ(buf.size, 7);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
465 EXPECT_EQ(buf.pos, 7);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
466 EXPECT_EQ(buf.capacity, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
467 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
468 }
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
469
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
470 TEST_F(BufferWrite, MultibyteExtend) {
541
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
471 buf.flags |= CX_BUFFER_AUTO_EXTEND;
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
472 const char *data = "tester";
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
473 ASSERT_EQ(buf.capacity, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
474 ASSERT_EQ(buf.size, 4);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
475 buf.pos = 3;
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
476 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
477 // 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
478 EXPECT_EQ(written, 3);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
479 EXPECT_EQ(buf.size, 9);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
480 EXPECT_EQ(buf.pos, 9);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
481 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
482 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
483 }
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
484
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
485 TEST_F(BufferWrite, PutcWrapperFit) {
541
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
486 ASSERT_EQ(buf.capacity, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
487 ASSERT_EQ(buf.pos, 4);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
488 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
489 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
490 EXPECT_EQ(c, 'a');
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
491 EXPECT_EQ(buf.size, 5);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
492 EXPECT_EQ(buf.pos, 5);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
493 EXPECT_EQ(buf.capacity, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
494 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
495 }
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
496
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
497 TEST_F(BufferWrite, PutcWrapperDiscard) {
541
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
498 ASSERT_EQ(buf.capacity, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
499 ASSERT_EQ(buf.size, 4);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
500 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
501 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
502 EXPECT_EQ(c, EOF);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
503 EXPECT_EQ(buf.size, 4);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
504 EXPECT_EQ(buf.pos, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
505 EXPECT_EQ(buf.capacity, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
506 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
507 }
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
508
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
509 TEST_F(BufferWrite, PutcWrapperExtend) {
541
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
510 buf.flags |= CX_BUFFER_AUTO_EXTEND;
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
511 ASSERT_EQ(buf.capacity, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
512 ASSERT_EQ(buf.size, 4);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
513 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
514 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
515 EXPECT_EQ(c, 'a');
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
516 EXPECT_EQ(buf.size, 9);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
517 EXPECT_EQ(buf.pos, 9);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
518 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
519 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
520 }
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
521
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
522 TEST_F(BufferWrite, PutStringWrapperFit) {
541
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
523 const char *data = "test";
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
524 ASSERT_EQ(buf.capacity, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
525 ASSERT_EQ(buf.pos, 4);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
526 ASSERT_EQ(buf.size, 4);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
527 size_t written = cxBufferPutString(&buf, data);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
528 EXPECT_EQ(written, 4);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
529 EXPECT_EQ(buf.size, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
530 EXPECT_EQ(buf.pos, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
531 EXPECT_EQ(buf.capacity, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
532 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
533 }
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
534
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
535 TEST_F(BufferWrite, PutStringWrapperDiscard) {
541
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
536 const char *data = "testing";
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
537 ASSERT_EQ(buf.capacity, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
538 ASSERT_EQ(buf.pos, 4);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
539 ASSERT_EQ(buf.size, 4);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
540 size_t written = cxBufferPutString(&buf, data);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
541 EXPECT_EQ(written, 4);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
542 EXPECT_EQ(buf.size, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
543 EXPECT_EQ(buf.pos, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
544 EXPECT_EQ(buf.capacity, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
545 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
546 }
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
547
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
548 TEST_F(BufferWrite, PutStringWrapperExtend) {
541
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
549 buf.flags |= CX_BUFFER_AUTO_EXTEND;
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
550 const char *data = "testing";
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
551 ASSERT_EQ(buf.capacity, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
552 ASSERT_EQ(buf.pos, 4);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
553 ASSERT_EQ(buf.size, 4);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
554 size_t written = cxBufferPutString(&buf, data);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
555 EXPECT_EQ(written, 7);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
556 EXPECT_EQ(buf.size, 11);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
557 EXPECT_EQ(buf.pos, 11);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
558 EXPECT_GE(buf.capacity, 11);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
559 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
560 }
538
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
561
541
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
562 TEST_F(BufferWrite, MultOverflow) {
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
563 const char *data = "testing";
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
564 ASSERT_EQ(buf.capacity, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
565 ASSERT_EQ(buf.pos, 4);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
566 ASSERT_EQ(buf.size, 4);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
567 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
568 EXPECT_EQ(written, 0);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
569 EXPECT_EQ(buf.capacity, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
570 EXPECT_EQ(buf.pos, 4);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
571 EXPECT_EQ(buf.size, 4);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
572 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
573 }
541
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
574
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
575 TEST_F(BufferWrite, MaxCapaOverflow) {
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
576 buf.flags |= CX_BUFFER_AUTO_EXTEND;
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
577 const char *data = "testing";
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
578 ASSERT_EQ(buf.capacity, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
579 ASSERT_EQ(buf.pos, 4);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
580 ASSERT_EQ(buf.size, 4);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
581 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
582 EXPECT_EQ(written, 0);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
583 EXPECT_EQ(buf.capacity, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
584 EXPECT_EQ(buf.pos, 4);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
585 EXPECT_EQ(buf.size, 4);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
586 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
587 }
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
588
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
589 TEST_F(BufferWrite, OnlyOverwrite) {
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
590 buf.flags |= CX_BUFFER_AUTO_EXTEND;
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
591 ASSERT_EQ(buf.capacity, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
592 memcpy(buf.space, "preptest", 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
593 buf.pos = 3;
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
594 buf.size = 8;
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
595 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
596 EXPECT_EQ(written, 2);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
597 EXPECT_EQ(buf.capacity, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
598 EXPECT_EQ(buf.size, 8);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
599 EXPECT_EQ(buf.pos, 7);
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 539
diff changeset
600 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
601 }
545
3700ac4bd9a3 #184 #170 first basic flush test
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
602
3700ac4bd9a3 #184 #170 first basic flush test
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
603 TEST_F(BufferWrite, FlushAtCapacity) {
3700ac4bd9a3 #184 #170 first basic flush test
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
604 enableFlushing();
3700ac4bd9a3 #184 #170 first basic flush test
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
605 ASSERT_EQ(buf.capacity, 8);
3700ac4bd9a3 #184 #170 first basic flush test
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
606 ASSERT_EQ(buf.pos, 4);
3700ac4bd9a3 #184 #170 first basic flush test
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
607 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
608 EXPECT_EQ(written, 3);
3700ac4bd9a3 #184 #170 first basic flush test
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
609 ASSERT_EQ(buf.pos, 7);
3700ac4bd9a3 #184 #170 first basic flush test
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
610 ASSERT_EQ(buf.size, 7);
3700ac4bd9a3 #184 #170 first basic flush test
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
611 ASSERT_EQ(target.pos, 0);
3700ac4bd9a3 #184 #170 first basic flush test
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
612 ASSERT_EQ(target.size, 0);
3700ac4bd9a3 #184 #170 first basic flush test
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
613 written = cxBufferWrite("hello", 1, 5, &buf);
3700ac4bd9a3 #184 #170 first basic flush test
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
614 EXPECT_EQ(written, 5);
3700ac4bd9a3 #184 #170 first basic flush test
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
615 EXPECT_EQ(buf.pos, 0);
3700ac4bd9a3 #184 #170 first basic flush test
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
616 EXPECT_EQ(buf.size, 0);
3700ac4bd9a3 #184 #170 first basic flush test
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
617 EXPECT_EQ(buf.capacity, 8);
3700ac4bd9a3 #184 #170 first basic flush test
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
618 EXPECT_EQ(target.pos, 12);
3700ac4bd9a3 #184 #170 first basic flush test
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
619 ASSERT_EQ(target.size, 12);
3700ac4bd9a3 #184 #170 first basic flush test
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
620 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
621 }
546
900795d59d03 #184 #170 flush at threshold test
Mike Becker <universe@uap-core.de>
parents: 545
diff changeset
622
900795d59d03 #184 #170 flush at threshold test
Mike Becker <universe@uap-core.de>
parents: 545
diff changeset
623 TEST_F(BufferWrite, FlushAtThreshold) {
900795d59d03 #184 #170 flush at threshold test
Mike Becker <universe@uap-core.de>
parents: 545
diff changeset
624 enableFlushing();
900795d59d03 #184 #170 flush at threshold test
Mike Becker <universe@uap-core.de>
parents: 545
diff changeset
625 buf.flush_threshold = 12;
900795d59d03 #184 #170 flush at threshold test
Mike Becker <universe@uap-core.de>
parents: 545
diff changeset
626 buf.flags |= CX_BUFFER_AUTO_EXTEND;
900795d59d03 #184 #170 flush at threshold test
Mike Becker <universe@uap-core.de>
parents: 545
diff changeset
627 ASSERT_EQ(buf.capacity, 8);
900795d59d03 #184 #170 flush at threshold test
Mike Becker <universe@uap-core.de>
parents: 545
diff changeset
628 ASSERT_EQ(buf.pos, 4);
900795d59d03 #184 #170 flush at threshold test
Mike Becker <universe@uap-core.de>
parents: 545
diff changeset
629 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
630 EXPECT_EQ(written, 6);
900795d59d03 #184 #170 flush at threshold test
Mike Becker <universe@uap-core.de>
parents: 545
diff changeset
631 ASSERT_EQ(buf.pos, 10);
900795d59d03 #184 #170 flush at threshold test
Mike Becker <universe@uap-core.de>
parents: 545
diff changeset
632 ASSERT_EQ(buf.size, 10);
900795d59d03 #184 #170 flush at threshold test
Mike Becker <universe@uap-core.de>
parents: 545
diff changeset
633 ASSERT_GE(buf.capacity, 10);
900795d59d03 #184 #170 flush at threshold test
Mike Becker <universe@uap-core.de>
parents: 545
diff changeset
634 ASSERT_LE(buf.capacity, 12);
900795d59d03 #184 #170 flush at threshold test
Mike Becker <universe@uap-core.de>
parents: 545
diff changeset
635 ASSERT_EQ(target.pos, 0);
900795d59d03 #184 #170 flush at threshold test
Mike Becker <universe@uap-core.de>
parents: 545
diff changeset
636 ASSERT_EQ(target.size, 0);
900795d59d03 #184 #170 flush at threshold test
Mike Becker <universe@uap-core.de>
parents: 545
diff changeset
637 written = cxBufferWrite("hello", 1, 5, &buf);
900795d59d03 #184 #170 flush at threshold test
Mike Becker <universe@uap-core.de>
parents: 545
diff changeset
638 EXPECT_EQ(written, 5);
900795d59d03 #184 #170 flush at threshold test
Mike Becker <universe@uap-core.de>
parents: 545
diff changeset
639 EXPECT_EQ(buf.pos, 0);
900795d59d03 #184 #170 flush at threshold test
Mike Becker <universe@uap-core.de>
parents: 545
diff changeset
640 EXPECT_EQ(buf.size, 0);
900795d59d03 #184 #170 flush at threshold test
Mike Becker <universe@uap-core.de>
parents: 545
diff changeset
641 EXPECT_LE(buf.capacity, 12);
900795d59d03 #184 #170 flush at threshold test
Mike Becker <universe@uap-core.de>
parents: 545
diff changeset
642 EXPECT_EQ(target.pos, 15);
900795d59d03 #184 #170 flush at threshold test
Mike Becker <universe@uap-core.de>
parents: 545
diff changeset
643 ASSERT_EQ(target.size, 15);
900795d59d03 #184 #170 flush at threshold test
Mike Becker <universe@uap-core.de>
parents: 545
diff changeset
644 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
645 }
547
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
646
567
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
647 TEST_F(BufferWrite, FlushRateLimited) {
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
648 enableFlushing();
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
649 // 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
650 target.capacity = 16;
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
651 target.flags &= ~CX_BUFFER_AUTO_EXTEND;
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
652 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
653 ASSERT_EQ(buf.capacity, 8);
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
654 ASSERT_EQ(buf.pos, 4);
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
655 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
656 EXPECT_EQ(written, 3);
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
657 ASSERT_EQ(buf.pos, 7);
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
658 ASSERT_EQ(buf.size, 7);
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
659 ASSERT_EQ(target.pos, 0);
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
660 ASSERT_EQ(target.size, 0);
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
661 written = cxBufferWrite("hello, world!", 1, 13, &buf);
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
662 // " 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
663 EXPECT_EQ(written, 13);
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
664 EXPECT_EQ(buf.pos, 7);
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
665 EXPECT_EQ(buf.size, 7);
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
666 EXPECT_EQ(buf.capacity, 8);
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
667 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
668 EXPECT_EQ(target.pos, 13);
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
669 ASSERT_EQ(target.size, 13);
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
670 EXPECT_EQ(target.capacity, 16);
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
671 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
672 }
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 566
diff changeset
673
548
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
674 class BufferSeek : public BufferFixture {
547
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
675 };
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 TEST_F(BufferSeek, SetZero) {
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
678 int result = cxBufferSeek(&buf, 0, SEEK_SET);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
679 EXPECT_EQ(result, 0);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
680 EXPECT_EQ(buf.pos, 0);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
681 }
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
682
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
683 TEST_F(BufferSeek, SetValid) {
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
684 int result = cxBufferSeek(&buf, 5, SEEK_SET);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
685 EXPECT_EQ(result, 0);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
686 EXPECT_EQ(buf.pos, 5);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
687 }
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
688
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
689 TEST_F(BufferSeek, SetInvalid) {
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
690 ASSERT_EQ(buf.pos, 3);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
691 int result = cxBufferSeek(&buf, 6, SEEK_SET);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
692 EXPECT_NE(result, 0);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
693 EXPECT_EQ(buf.pos, 3);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
694 }
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
695
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
696 TEST_F(BufferSeek, CurZero) {
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
697 ASSERT_EQ(buf.pos, 3);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
698 int result = cxBufferSeek(&buf, 0, SEEK_CUR);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
699 EXPECT_EQ(result, 0);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
700 EXPECT_EQ(buf.pos, 3);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
701 }
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
702
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
703 TEST_F(BufferSeek, CurValidPositive) {
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
704 ASSERT_EQ(buf.pos, 3);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
705 int result = cxBufferSeek(&buf, 2, SEEK_CUR);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
706 EXPECT_EQ(result, 0);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
707 EXPECT_EQ(buf.pos, 5);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
708 }
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
709
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
710 TEST_F(BufferSeek, CurValidNegative) {
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
711 ASSERT_EQ(buf.pos, 3);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
712 int result = cxBufferSeek(&buf, -3, SEEK_CUR);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
713 EXPECT_EQ(result, 0);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
714 EXPECT_EQ(buf.pos, 0);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
715 }
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
716
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
717 TEST_F(BufferSeek, CurInvalidPositive) {
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
718 ASSERT_EQ(buf.pos, 3);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
719 int result = cxBufferSeek(&buf, 3, SEEK_CUR);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
720 EXPECT_NE(result, 0);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
721 EXPECT_EQ(buf.pos, 3);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
722 }
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
723
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
724 TEST_F(BufferSeek, CurInvalidNegative) {
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
725 ASSERT_EQ(buf.pos, 3);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
726 int result = cxBufferSeek(&buf, -4, SEEK_CUR);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
727 EXPECT_NE(result, 0);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
728 EXPECT_EQ(buf.pos, 3);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
729 }
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
730
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
731 TEST_F(BufferSeek, EndZero) {
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
732 ASSERT_EQ(buf.size, 6);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
733 int result = cxBufferSeek(&buf, 0, SEEK_END);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
734 // the (past-the-)end position is always invalid
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
735 EXPECT_NE(result, 0);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
736 EXPECT_EQ(buf.pos, 3);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
737 }
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
738
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
739 TEST_F(BufferSeek, EndValid) {
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
740 ASSERT_EQ(buf.size, 6);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
741 int result = cxBufferSeek(&buf, -6, SEEK_END);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
742 EXPECT_EQ(result, 0);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
743 EXPECT_EQ(buf.pos, 0);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
744 }
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
745
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
746 TEST_F(BufferSeek, EndInvalid) {
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
747 ASSERT_EQ(buf.size, 6);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
748 int result = cxBufferSeek(&buf, 1, SEEK_END);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
749 EXPECT_NE(result, 0);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
750 EXPECT_EQ(buf.pos, 3);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
751 }
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
752
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
753 TEST_F(BufferSeek, WhenceInvalid) {
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
754 ASSERT_EQ(buf.size, 6);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
755 ASSERT_EQ(buf.pos, 3);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
756 int result = cxBufferSeek(&buf, 2, 9000);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
757 EXPECT_NE(result, 0);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
758 EXPECT_EQ(buf.size, 6);
9e933920dcf9 #170 test buffer seek
Mike Becker <universe@uap-core.de>
parents: 546
diff changeset
759 EXPECT_EQ(buf.pos, 3);
548
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
760 }
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
761
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
762 class BufferEof : public BufferFixture {
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
763 };
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
764
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
765 TEST_F(BufferEof, Reached) {
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
766 buf.pos = buf.size;
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
767 EXPECT_TRUE(cxBufferEof(&buf));
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
768 buf.pos = buf.size - 1;
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
769 ASSERT_FALSE(cxBufferEof(&buf));
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
770 cxBufferPut(&buf, 'a');
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
771 EXPECT_TRUE(cxBufferEof(&buf));
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
772 }
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
773
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
774 TEST_F(BufferEof, NotReached) {
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
775 buf.pos = buf.size - 1;
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
776 EXPECT_FALSE(cxBufferEof(&buf));
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
777 buf.pos = 0;
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
778 cxBufferWrite("test", 1, 5, &buf);
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
779 EXPECT_FALSE(cxBufferEof(&buf));
459bca1cdf8d #170 test buffer eof
Mike Becker <universe@uap-core.de>
parents: 547
diff changeset
780 }
568
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
781
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
782 class BufferRead : public ::testing::Test {
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
783 protected:
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
784 CxBuffer buf{};
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
785
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
786 void SetUp() override {
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
787 cxBufferInit(&buf, nullptr, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT);
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
788 buf.capacity = 8; // artificially reduce capacity to check OOB writes
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
789 memset(buf.space, 0, 16);
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
790 memcpy(buf.space, "some data", 9);
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
791 buf.size = 9;
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
792 }
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
793
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
794 void TearDown() override {
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
795 cxBufferDestroy(&buf);
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
796 }
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
797 };
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
798
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
799 TEST_F(BufferRead, GetByte) {
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
800 buf.pos = 2;
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
801 EXPECT_EQ(cxBufferGet(&buf), 'm');
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
802 EXPECT_EQ(cxBufferGet(&buf), 'e');
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
803 EXPECT_EQ(cxBufferGet(&buf), ' ');
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
804 EXPECT_EQ(cxBufferGet(&buf), 'd');
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
805 EXPECT_EQ(buf.pos, 6);
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
806 }
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
807
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
808 TEST_F(BufferRead, GetEof) {
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
809 buf.pos = buf.size;
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
810 EXPECT_EQ(cxBufferGet(&buf), EOF);
8eda32d09e3d #170 test cxBufferGet
Mike Becker <universe@uap-core.de>
parents: 567
diff changeset
811 }
569
cb63f3d1236a #170 test cxBufferRead
Mike Becker <universe@uap-core.de>
parents: 568
diff changeset
812
cb63f3d1236a #170 test cxBufferRead
Mike Becker <universe@uap-core.de>
parents: 568
diff changeset
813 TEST_F(BufferRead, ReadWithinBounds) {
cb63f3d1236a #170 test cxBufferRead
Mike Becker <universe@uap-core.de>
parents: 568
diff changeset
814 buf.pos = 2;
cb63f3d1236a #170 test cxBufferRead
Mike Becker <universe@uap-core.de>
parents: 568
diff changeset
815 char target[4];
cb63f3d1236a #170 test cxBufferRead
Mike Becker <universe@uap-core.de>
parents: 568
diff changeset
816 auto read = cxBufferRead(&target, 1, 4, &buf);
cb63f3d1236a #170 test cxBufferRead
Mike Becker <universe@uap-core.de>
parents: 568
diff changeset
817 ASSERT_EQ(read, 4);
cb63f3d1236a #170 test cxBufferRead
Mike Becker <universe@uap-core.de>
parents: 568
diff changeset
818 EXPECT_EQ(memcmp(&target, "me d", 4), 0);
cb63f3d1236a #170 test cxBufferRead
Mike Becker <universe@uap-core.de>
parents: 568
diff changeset
819 EXPECT_EQ(buf.pos, 6);
cb63f3d1236a #170 test cxBufferRead
Mike Becker <universe@uap-core.de>
parents: 568
diff changeset
820 }
cb63f3d1236a #170 test cxBufferRead
Mike Becker <universe@uap-core.de>
parents: 568
diff changeset
821
cb63f3d1236a #170 test cxBufferRead
Mike Becker <universe@uap-core.de>
parents: 568
diff changeset
822 TEST_F(BufferRead, ReadOutOfBounds) {
cb63f3d1236a #170 test cxBufferRead
Mike Becker <universe@uap-core.de>
parents: 568
diff changeset
823 buf.pos = 6;
cb63f3d1236a #170 test cxBufferRead
Mike Becker <universe@uap-core.de>
parents: 568
diff changeset
824 char target[4];
cb63f3d1236a #170 test cxBufferRead
Mike Becker <universe@uap-core.de>
parents: 568
diff changeset
825 auto read = cxBufferRead(&target, 1, 4, &buf);
cb63f3d1236a #170 test cxBufferRead
Mike Becker <universe@uap-core.de>
parents: 568
diff changeset
826 ASSERT_EQ(read, 3);
cb63f3d1236a #170 test cxBufferRead
Mike Becker <universe@uap-core.de>
parents: 568
diff changeset
827 EXPECT_EQ(memcmp(&target, "ata", 3), 0);
cb63f3d1236a #170 test cxBufferRead
Mike Becker <universe@uap-core.de>
parents: 568
diff changeset
828 EXPECT_EQ(buf.pos, 9);
cb63f3d1236a #170 test cxBufferRead
Mike Becker <universe@uap-core.de>
parents: 568
diff changeset
829 }
cb63f3d1236a #170 test cxBufferRead
Mike Becker <universe@uap-core.de>
parents: 568
diff changeset
830
cb63f3d1236a #170 test cxBufferRead
Mike Becker <universe@uap-core.de>
parents: 568
diff changeset
831 TEST_F(BufferRead, ReadOutOfBoundsMultibyte) {
cb63f3d1236a #170 test cxBufferRead
Mike Becker <universe@uap-core.de>
parents: 568
diff changeset
832 buf.pos = 6;
cb63f3d1236a #170 test cxBufferRead
Mike Becker <universe@uap-core.de>
parents: 568
diff changeset
833 char target[4];
cb63f3d1236a #170 test cxBufferRead
Mike Becker <universe@uap-core.de>
parents: 568
diff changeset
834 target[2] = '\0';
cb63f3d1236a #170 test cxBufferRead
Mike Becker <universe@uap-core.de>
parents: 568
diff changeset
835 auto read = cxBufferRead(&target, 2, 2, &buf);
cb63f3d1236a #170 test cxBufferRead
Mike Becker <universe@uap-core.de>
parents: 568
diff changeset
836 ASSERT_EQ(read, 1);
cb63f3d1236a #170 test cxBufferRead
Mike Becker <universe@uap-core.de>
parents: 568
diff changeset
837 EXPECT_EQ(memcmp(&target, "at\0", 3), 0);
cb63f3d1236a #170 test cxBufferRead
Mike Becker <universe@uap-core.de>
parents: 568
diff changeset
838 EXPECT_EQ(buf.pos, 8);
cb63f3d1236a #170 test cxBufferRead
Mike Becker <universe@uap-core.de>
parents: 568
diff changeset
839 }
cb63f3d1236a #170 test cxBufferRead
Mike Becker <universe@uap-core.de>
parents: 568
diff changeset
840
cb63f3d1236a #170 test cxBufferRead
Mike Becker <universe@uap-core.de>
parents: 568
diff changeset
841 TEST_F(BufferRead, ReadEof) {
cb63f3d1236a #170 test cxBufferRead
Mike Becker <universe@uap-core.de>
parents: 568
diff changeset
842 buf.pos = 9;
cb63f3d1236a #170 test cxBufferRead
Mike Becker <universe@uap-core.de>
parents: 568
diff changeset
843 char target[4];
cb63f3d1236a #170 test cxBufferRead
Mike Becker <universe@uap-core.de>
parents: 568
diff changeset
844 auto read = cxBufferRead(&target, 1, 1, &buf);
cb63f3d1236a #170 test cxBufferRead
Mike Becker <universe@uap-core.de>
parents: 568
diff changeset
845 ASSERT_EQ(read, 0);
cb63f3d1236a #170 test cxBufferRead
Mike Becker <universe@uap-core.de>
parents: 568
diff changeset
846 EXPECT_EQ(buf.pos, 9);
cb63f3d1236a #170 test cxBufferRead
Mike Becker <universe@uap-core.de>
parents: 568
diff changeset
847 }

mercurial