1 /** @file
2 
3     Catch-based unit tests for MIOBufferWriter class.
4 
5     @section license License
6 
7     Licensed to the Apache Software Foundation (ASF) under one
8     or more contributor license agreements.  See the NOTICE file
9     distributed with this work for additional information
10     regarding copyright ownership.  The ASF licenses this file
11     to you under the Apache License, Version 2.0 (the
12     "License"); you may not use this file except in compliance
13     with the License.  You may obtain a copy of the License at
14 
15     http://www.apache.org/licenses/LICENSE-2.0
16 
17     Unless required by applicable law or agreed to in writing, software
18     distributed under the License is distributed on an "AS IS" BASIS,
19     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20     See the License for the specific language governing permissions and
21     limitations under the License.
22  */
23 
24 #define CATCH_CONFIG_MAIN
25 #include "catch.hpp"
26 
27 #include <cstdint>
28 
29 struct IOBufferBlock {
30   std::int64_t write_avail();
31 
32   char *end();
33 
34   void fill(int64_t);
35 };
36 
37 struct MIOBuffer {
38   IOBufferBlock *first_write_block();
39 
40   void add_block();
41 };
42 
43 #define UNIT_TEST_BUFFER_WRITER
44 #include "I_MIOBufferWriter.h"
45 #include "MIOBufferWriter.cc"
46 
47 IOBufferBlock iobb[1];
48 int iobbIdx{0};
49 
50 const int BlockSize = 11 * 11;
51 char block[BlockSize];
52 int blockUsed{0};
53 
54 std::int64_t
write_avail()55 IOBufferBlock::write_avail()
56 {
57   REQUIRE(this == (iobb + iobbIdx));
58   return BlockSize - blockUsed;
59 }
60 
61 char *
end()62 IOBufferBlock::end()
63 {
64   REQUIRE(this == (iobb + iobbIdx));
65   return block + blockUsed;
66 }
67 
68 void
fill(int64_t len)69 IOBufferBlock::fill(int64_t len)
70 {
71   static std::uint8_t dataCheck;
72 
73   REQUIRE(this == (iobb + iobbIdx));
74 
75   while (len-- and (blockUsed < BlockSize)) {
76     REQUIRE(block[blockUsed] == static_cast<char>(dataCheck));
77 
78     ++blockUsed;
79 
80     dataCheck += 7;
81   }
82 
83   REQUIRE(len == -1);
84 }
85 
86 MIOBuffer theMIOBuffer;
87 
88 IOBufferBlock *
first_write_block()89 MIOBuffer::first_write_block()
90 {
91   REQUIRE(this == &theMIOBuffer);
92 
93   REQUIRE(blockUsed <= BlockSize);
94 
95   if (blockUsed == BlockSize) {
96     return nullptr;
97   }
98 
99   return iobb + iobbIdx;
100 }
101 
102 void
add_block()103 MIOBuffer::add_block()
104 {
105   REQUIRE(this == &theMIOBuffer);
106 
107   REQUIRE(blockUsed == BlockSize);
108 
109   blockUsed = 0;
110 
111   ++iobbIdx;
112 }
113 
114 std::string
genData(int numBytes)115 genData(int numBytes)
116 {
117   static std::uint8_t genData;
118 
119   std::string s(numBytes, ' ');
120 
121   for (int i{0}; i < numBytes; ++i) {
122     s[i] = genData;
123     genData += 7;
124   }
125 
126   return s;
127 }
128 
129 void
writeOnce(MIOBufferWriter & bw,std::size_t len)130 writeOnce(MIOBufferWriter &bw, std::size_t len)
131 {
132   static bool toggle;
133 
134   std::string s{genData(len)};
135 
136   if (len == 1) {
137     bw.write(s[0]);
138 
139   } else if (toggle) {
140     std::size_t cap{bw.auxBufferCapacity()};
141 
142     if (cap >= len) {
143       memcpy(bw.auxBuffer(), s.data(), len);
144       bw.fill(len);
145 
146     } else {
147       memcpy(bw.auxBuffer(), s.data(), cap);
148       bw.fill(cap);
149       bw.write(s.data() + cap, len - cap);
150     }
151   } else {
152     bw.write(s.data(), len);
153   }
154 
155   toggle = !toggle;
156 
157   REQUIRE(bw.auxBufferCapacity() <= BlockSize);
158 }
159 
160 class InkAssertExcept
161 {
162 };
163 
164 TEST_CASE("MIOBufferWriter", "[MIOBW]")
165 {
166   MIOBufferWriter bw(&theMIOBuffer);
167 
168   REQUIRE(bw.auxBufferCapacity() == BlockSize);
169 
170   writeOnce(bw, 0);
171   writeOnce(bw, 1);
172   writeOnce(bw, 1);
173   writeOnce(bw, 1);
174   writeOnce(bw, 10);
175   writeOnce(bw, 1000);
176   writeOnce(bw, 1);
177   writeOnce(bw, 0);
178   writeOnce(bw, 1);
179   writeOnce(bw, 2000);
180   writeOnce(bw, 69);
181   writeOnce(bw, 666);
182 
183   for (int i = 0; i < 3000; i += 13) {
184     writeOnce(bw, i);
185   }
186 
187   writeOnce(bw, 0);
188   writeOnce(bw, 1);
189 
190   REQUIRE(bw.extent() == ((iobbIdx * BlockSize) + blockUsed));
191 
192   REQUIRE_THROWS_AS(bw.fill(bw.auxBufferCapacity() + 1), InkAssertExcept);
193   REQUIRE_THROWS_AS(bw.data(), InkAssertExcept);
194 }
195 
196 void
_ink_assert(const char * a,const char * f,int l)197 _ink_assert(const char *a, const char *f, int l)
198 {
199   throw InkAssertExcept();
200 }
201