1 // Copyright 2020 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 #include "pw_bytes/byte_builder.h"
16 
17 #include <array>
18 #include <cstddef>
19 
20 #include "gtest/gtest.h"
21 
22 using std::byte;
23 
24 template <typename... Args>
MakeBytes(Args...args)25 constexpr std::array<byte, sizeof...(Args)> MakeBytes(Args... args) noexcept {
26   return {static_cast<byte>(args)...};
27 }
28 
29 namespace pw {
30 namespace {
31 
TEST(ByteBuilder,EmptyBuffer_SizeAndMaxSizeAreCorrect)32 TEST(ByteBuilder, EmptyBuffer_SizeAndMaxSizeAreCorrect) {
33   ByteBuilder bb(ByteSpan{});
34 
35   EXPECT_TRUE(bb.empty());
36   EXPECT_EQ(0u, bb.size());
37   EXPECT_EQ(0u, bb.max_size());
38 }
39 
TEST(ByteBuilder,NonEmptyBufferOfSize0_SizeAndMaxSizeAreCorrect)40 TEST(ByteBuilder, NonEmptyBufferOfSize0_SizeAndMaxSizeAreCorrect) {
41   std::array<byte, 3> buffer = MakeBytes(0x01, 0x02, 0x03);
42   ByteBuilder bb(buffer);
43 
44   EXPECT_TRUE(bb.empty());
45   EXPECT_EQ(0u, bb.size());
46   EXPECT_EQ(3u, bb.max_size());
47 }
48 
TEST(ByteBuilder,Constructor_InsertsEmptyBuffer)49 TEST(ByteBuilder, Constructor_InsertsEmptyBuffer) {
50   std::array<byte, 3> buffer = MakeBytes(0x01, 0x02, 0x03);
51   ByteBuilder bb(buffer);
52 
53   EXPECT_TRUE(bb.empty());
54 }
55 
TEST(ByteBuilder,EmptyBuffer_Append)56 TEST(ByteBuilder, EmptyBuffer_Append) {
57   ByteBuilder bb(ByteSpan{});
58   EXPECT_TRUE(bb.empty());
59 
60   constexpr auto kBytesTestLiteral = MakeBytes(0x04, 0x05);
61 
62   EXPECT_FALSE(bb.append(kBytesTestLiteral.data(), 2).ok());
63   EXPECT_EQ(0u, bb.size());
64   EXPECT_EQ(0u, bb.max_size());
65 }
66 
TEST(ByteBuilder,NonEmptyBufferOfSize0_Append)67 TEST(ByteBuilder, NonEmptyBufferOfSize0_Append) {
68   std::array<byte, 3> buffer = MakeBytes(0x01, 0x02, 0x03);
69   ByteBuilder bb(buffer);
70   EXPECT_TRUE(bb.empty());
71 
72   constexpr auto kBytesTestLiteral = MakeBytes(0x04, 0x05);
73 
74   EXPECT_TRUE(bb.append(kBytesTestLiteral.data(), 2).ok());
75   EXPECT_EQ(byte{0x04}, bb.data()[0]);
76   EXPECT_EQ(byte{0x05}, bb.data()[1]);
77 }
78 
TEST(ByteBuilder,NonEmptyBufferOfSize0_Append_Partial_NotResourceExhausted)79 TEST(ByteBuilder, NonEmptyBufferOfSize0_Append_Partial_NotResourceExhausted) {
80   std::array<byte, 3> buffer = MakeBytes(0x01, 0x02, 0x03);
81   ByteBuilder bb(buffer);
82 
83   EXPECT_TRUE(bb.empty());
84 
85   constexpr auto kBytesTestLiteral = MakeBytes(0x04, 0x05, 0x06, 0x07);
86 
87   EXPECT_TRUE(bb.append(kBytesTestLiteral.data(), 3).ok());
88   EXPECT_EQ(byte{0x04}, bb.data()[0]);
89   EXPECT_EQ(byte{0x05}, bb.data()[1]);
90   EXPECT_EQ(byte{0x06}, bb.data()[2]);
91 }
92 
TEST(ByteBuilder,NonEmptyBufferOfSize0_Append_Partial_ResourceExhausted)93 TEST(ByteBuilder, NonEmptyBufferOfSize0_Append_Partial_ResourceExhausted) {
94   std::array<byte, 3> buffer = MakeBytes(0x01, 0x02, 0x03);
95   ByteBuilder bb(buffer);
96 
97   EXPECT_TRUE(bb.empty());
98 
99   constexpr auto kBytesTestLiteral = MakeBytes(0x04, 0x05, 0x06, 0x07);
100 
101   EXPECT_FALSE(bb.append(kBytesTestLiteral.data(), 4).ok());
102   EXPECT_EQ(Status::ResourceExhausted(), bb.status());
103   EXPECT_EQ(0u, bb.size());
104 }
105 
TEST(ByteBuilder,Append_RepeatedBytes)106 TEST(ByteBuilder, Append_RepeatedBytes) {
107   ByteBuffer<8> bb;
108   EXPECT_TRUE(bb.empty());
109 
110   EXPECT_TRUE(bb.append(7, byte{0x04}).ok());
111 
112   for (size_t i = 0; i < 7; i++) {
113     EXPECT_EQ(byte{0x04}, bb.data()[i]);
114   }
115 }
116 
TEST(ByteBuilder,Append_Bytes_Full)117 TEST(ByteBuilder, Append_Bytes_Full) {
118   ByteBuffer<8> bb;
119 
120   EXPECT_EQ(8u, bb.max_size() - bb.size());
121 
122   EXPECT_TRUE(bb.append(8, byte{0x04}).ok());
123 
124   for (size_t i = 0; i < 8; i++) {
125     EXPECT_EQ(byte{0x04}, bb.data()[i]);
126   }
127 }
128 
TEST(ByteBuilder,Append_Bytes_Exhausted)129 TEST(ByteBuilder, Append_Bytes_Exhausted) {
130   ByteBuffer<8> bb;
131 
132   EXPECT_EQ(Status::ResourceExhausted(), bb.append(9, byte{0x04}).status());
133   EXPECT_EQ(0u, bb.size());
134 }
135 
TEST(ByteBuilder,Append_Partial)136 TEST(ByteBuilder, Append_Partial) {
137   std::array<byte, 3> buffer = MakeBytes(0x01, 0x02, 0x03);
138   ByteBuffer<12> bb;
139 
140   EXPECT_TRUE(bb.append(buffer.data(), 2).ok());
141   EXPECT_EQ(2u, bb.size());
142   EXPECT_EQ(byte{0x01}, bb.data()[0]);
143   EXPECT_EQ(byte{0x02}, bb.data()[1]);
144 }
145 
TEST(ByteBuilder,EmptyBuffer_Resize_WritesNothing)146 TEST(ByteBuilder, EmptyBuffer_Resize_WritesNothing) {
147   std::array<byte, 3> buffer = MakeBytes(0x01, 0x02, 0x03);
148   ByteBuilder bb(buffer);
149 
150   bb.resize(0);
151   EXPECT_TRUE(bb.ok());
152 }
153 
TEST(ByteBuilder,EmptyBuffer_Resize_Larger_Fails)154 TEST(ByteBuilder, EmptyBuffer_Resize_Larger_Fails) {
155   std::array<byte, 3> buffer = MakeBytes(0x01, 0x02, 0x03);
156   ByteBuilder bb(buffer);
157 
158   bb.resize(1);
159   EXPECT_EQ(Status::OutOfRange(), bb.append(9, byte{0x04}).status());
160 }
161 
TEST(ByteBuilder,Resize_Smaller)162 TEST(ByteBuilder, Resize_Smaller) {
163   std::array<byte, 3> buffer = MakeBytes(0x01, 0x02, 0x03);
164   ByteBuffer<8> bb;
165 
166   EXPECT_TRUE(bb.append(buffer).ok());
167 
168   bb.resize(1);
169   EXPECT_TRUE(bb.ok());
170   EXPECT_EQ(1u, bb.size());
171   EXPECT_EQ(byte{0x01}, bb.data()[0]);
172 }
173 
TEST(ByteBuilder,Resize_Clear)174 TEST(ByteBuilder, Resize_Clear) {
175   std::array<byte, 3> buffer = MakeBytes(0x01, 0x02, 0x03);
176   ByteBuffer<8> bb;
177 
178   EXPECT_TRUE(bb.append(buffer).ok());
179 
180   bb.resize(0);
181   EXPECT_TRUE(bb.ok());
182   EXPECT_EQ(0u, bb.size());
183   EXPECT_TRUE(bb.empty());
184 }
185 
TEST(ByteBuilder,Resize_Larger_Fails)186 TEST(ByteBuilder, Resize_Larger_Fails) {
187   std::array<byte, 3> buffer = MakeBytes(0x01, 0x02, 0x03);
188   ByteBuffer<8> bb;
189 
190   EXPECT_TRUE(bb.append(buffer).ok());
191 
192   EXPECT_EQ(3u, bb.size());
193   bb.resize(5);
194   EXPECT_EQ(3u, bb.size());
195   EXPECT_EQ(bb.status(), Status::OutOfRange());
196 }
197 
TEST(ByteBuilder,Status_StartsOk)198 TEST(ByteBuilder, Status_StartsOk) {
199   ByteBuffer<16> bb;
200   EXPECT_EQ(OkStatus(), bb.status());
201 }
202 
TEST(ByteBuilder,Status_StatusUpdate)203 TEST(ByteBuilder, Status_StatusUpdate) {
204   std::array<byte, 3> buffer = MakeBytes(0x01, 0x02, 0x03);
205   ByteBuffer<2> bb;
206 
207   EXPECT_FALSE(bb.append(buffer).ok());
208   EXPECT_EQ(Status::ResourceExhausted(), bb.status());
209 
210   bb.resize(4);
211   EXPECT_EQ(Status::OutOfRange(), bb.status());
212 
213   EXPECT_FALSE(bb.append(buffer.data(), 0).ok());
214   EXPECT_EQ(Status::OutOfRange(), bb.status());
215 }
216 
TEST(ByteBuilder,Status_ClearStatus_SetsStatusToOk)217 TEST(ByteBuilder, Status_ClearStatus_SetsStatusToOk) {
218   std::array<byte, 3> buffer = MakeBytes(0x01, 0x02, 0x03);
219   ByteBuffer<2> bb;
220 
221   EXPECT_FALSE(bb.append(buffer).ok());
222   EXPECT_EQ(Status::ResourceExhausted(), bb.status());
223 
224   bb.clear_status();
225   EXPECT_EQ(OkStatus(), bb.status());
226 }
227 
TEST(ByteBuilder,PushBack)228 TEST(ByteBuilder, PushBack) {
229   ByteBuffer<12> bb;
230   bb.push_back(byte{0x01});
231   EXPECT_EQ(OkStatus(), bb.status());
232   EXPECT_EQ(1u, bb.size());
233   EXPECT_EQ(byte{0x01}, bb.data()[0]);
234 }
235 
TEST(ByteBuilder,PushBack_Full)236 TEST(ByteBuilder, PushBack_Full) {
237   ByteBuffer<1> bb;
238   bb.push_back(byte{0x01});
239   EXPECT_EQ(OkStatus(), bb.status());
240   EXPECT_EQ(1u, bb.size());
241 }
242 
TEST(ByteBuilder,PushBack_Full_ResourceExhausted)243 TEST(ByteBuilder, PushBack_Full_ResourceExhausted) {
244   ByteBuffer<1> bb;
245   bb.push_back(byte{0x01});
246   bb.push_back(byte{0x01});
247 
248   EXPECT_EQ(Status::ResourceExhausted(), bb.status());
249   EXPECT_EQ(1u, bb.size());
250 }
251 
TEST(ByteBuilder,PopBack)252 TEST(ByteBuilder, PopBack) {
253   std::array<byte, 3> buffer = MakeBytes(0x01, 0x02, 0x03);
254   ByteBuffer<3> bb;
255 
256   bb.append(buffer.data(), 3);
257 
258   bb.pop_back();
259   EXPECT_EQ(OkStatus(), bb.status());
260   EXPECT_EQ(2u, bb.size());
261   EXPECT_EQ(byte{0x01}, bb.data()[0]);
262   EXPECT_EQ(byte{0x02}, bb.data()[1]);
263 }
264 
TEST(ByteBuilder,PopBack_Empty)265 TEST(ByteBuilder, PopBack_Empty) {
266   std::array<byte, 3> buffer = MakeBytes(0x01, 0x02, 0x03);
267   ByteBuffer<3> bb;
268   bb.append(buffer.data(), 3);
269 
270   bb.pop_back();
271   bb.pop_back();
272   bb.pop_back();
273   EXPECT_EQ(OkStatus(), bb.status());
274   EXPECT_EQ(0u, bb.size());
275   EXPECT_TRUE(bb.empty());
276 }
277 
TEST(ByteBuffer,Assign)278 TEST(ByteBuffer, Assign) {
279   std::array<byte, 3> buffer = MakeBytes(0x01, 0x02, 0x03);
280   ByteBuffer<10> one;
281   ByteBuffer<10> two;
282 
283   one.append(buffer.data(), 3);
284   EXPECT_EQ(byte{0x01}, one.data()[0]);
285   EXPECT_EQ(byte{0x02}, one.data()[1]);
286   EXPECT_EQ(byte{0x03}, one.data()[2]);
287 
288   two = one;
289   EXPECT_EQ(byte{0x01}, two.data()[0]);
290   EXPECT_EQ(byte{0x02}, two.data()[1]);
291   EXPECT_EQ(byte{0x03}, two.data()[2]);
292 
293   constexpr auto kBytesTestLiteral = MakeBytes(0x04, 0x05, 0x06, 0x07);
294   one.append(kBytesTestLiteral.data(), 2);
295   two.append(kBytesTestLiteral.data(), 4);
296   EXPECT_EQ(5u, one.size());
297   EXPECT_EQ(7u, two.size());
298   EXPECT_EQ(byte{0x04}, one.data()[3]);
299   EXPECT_EQ(byte{0x05}, one.data()[4]);
300   EXPECT_EQ(byte{0x04}, two.data()[3]);
301   EXPECT_EQ(byte{0x05}, two.data()[4]);
302   EXPECT_EQ(byte{0x06}, two.data()[5]);
303   EXPECT_EQ(byte{0x07}, two.data()[6]);
304 
305   two.push_back(byte{0x01});
306   two.push_back(byte{0x01});
307   two.push_back(byte{0x01});
308   two.push_back(byte{0x01});
309   ASSERT_EQ(Status::ResourceExhausted(), two.status());
310 
311   one = two;
312   EXPECT_EQ(byte{0x01}, two.data()[7]);
313   EXPECT_EQ(byte{0x01}, two.data()[8]);
314   EXPECT_EQ(byte{0x01}, two.data()[9]);
315   EXPECT_EQ(Status::ResourceExhausted(), one.status());
316 }
317 
TEST(ByteBuffer,CopyConstructFromSameSize)318 TEST(ByteBuffer, CopyConstructFromSameSize) {
319   ByteBuffer<10> one;
320   std::array<byte, 3> buffer = MakeBytes(0x01, 0x02, 0x03);
321 
322   one.append(buffer.data(), 3);
323   EXPECT_EQ(byte{0x01}, one.data()[0]);
324   EXPECT_EQ(byte{0x02}, one.data()[1]);
325   EXPECT_EQ(byte{0x03}, one.data()[2]);
326 
327   ByteBuffer<10> two(one);
328   EXPECT_EQ(byte{0x01}, two.data()[0]);
329   EXPECT_EQ(byte{0x02}, two.data()[1]);
330   EXPECT_EQ(byte{0x03}, two.data()[2]);
331 }
332 
TEST(ByteBuffer,CopyConstructFromSmaller)333 TEST(ByteBuffer, CopyConstructFromSmaller) {
334   std::array<byte, 3> buffer = MakeBytes(0x01, 0x02, 0x03);
335   ByteBuffer<2> one;
336   one.append(buffer.data(), 2);
337   ByteBuffer<3> two(one);
338 
339   EXPECT_EQ(byte{0x01}, two.data()[0]);
340   EXPECT_EQ(byte{0x02}, two.data()[1]);
341   EXPECT_EQ(OkStatus(), two.status());
342 }
343 
TEST(ByteBuilder,ResizeError_NoDataAddedAfter)344 TEST(ByteBuilder, ResizeError_NoDataAddedAfter) {
345   std::array<byte, 3> buffer = MakeBytes(0x01, 0x02, 0x03);
346   ByteBuffer<8> bb;
347 
348   EXPECT_TRUE(bb.append(buffer).ok());
349 
350   EXPECT_EQ(3u, bb.size());
351   bb.resize(5);
352   EXPECT_EQ(3u, bb.size());
353   EXPECT_EQ(bb.status(), Status::OutOfRange());
354 
355   bb.PutInt8(0xFE);
356   EXPECT_EQ(3u, bb.size());
357   EXPECT_EQ(bb.status(), Status::OutOfRange());
358 }
359 
TEST(ByteBuilder,AddingNoBytesToZeroSizedByteBuffer)360 TEST(ByteBuilder, AddingNoBytesToZeroSizedByteBuffer) {
361   std::array<byte, 3> buffer = MakeBytes(0x01, 0x02, 0x03);
362   ByteBuffer<0> bb;
363 
364   EXPECT_TRUE(bb.append(buffer.data(), 0).ok());
365   EXPECT_EQ(0u, bb.size());
366 }
367 
TEST(ByteBuffer,Putting8ByteInts_Full)368 TEST(ByteBuffer, Putting8ByteInts_Full) {
369   ByteBuffer<2> bb;
370   bb.PutInt8(0xFE);
371   bb.PutUint8(0x02);
372 
373   EXPECT_EQ(byte{0xFE}, bb.data()[0]);
374   EXPECT_EQ(byte{0x02}, bb.data()[1]);
375   EXPECT_EQ(OkStatus(), bb.status());
376 }
377 
TEST(ByteBuffer,Putting8ByteInts_Exhausted)378 TEST(ByteBuffer, Putting8ByteInts_Exhausted) {
379   ByteBuffer<2> bb;
380   bb.PutInt8(0xFE);
381   bb.PutUint8(0x02);
382   bb.PutUint8(0x05);
383 
384   EXPECT_EQ(byte{0xFE}, bb.data()[0]);
385   EXPECT_EQ(byte{0x02}, bb.data()[1]);
386   EXPECT_EQ(Status::ResourceExhausted(), bb.status());
387 }
388 
TEST(ByteBuffer,Putting16ByteInts_Full_kLittleEndian)389 TEST(ByteBuffer, Putting16ByteInts_Full_kLittleEndian) {
390   ByteBuffer<4> bb;
391   bb.PutInt16(0xFFF7);
392   bb.PutUint16(0x0008);
393 
394   EXPECT_EQ(byte{0xF7}, bb.data()[0]);
395   EXPECT_EQ(byte{0xFF}, bb.data()[1]);
396   EXPECT_EQ(byte{0x08}, bb.data()[2]);
397   EXPECT_EQ(byte{0x00}, bb.data()[3]);
398 
399   EXPECT_EQ(OkStatus(), bb.status());
400 }
401 
TEST(ByteBuffer,Putting16ByteInts_Exhausted_kBigEndian)402 TEST(ByteBuffer, Putting16ByteInts_Exhausted_kBigEndian) {
403   ByteBuffer<5> bb;
404   bb.PutInt16(0xFFF7, std::endian::big);
405   bb.PutUint16(0x0008, std::endian::big);
406 
407   EXPECT_EQ(byte{0xFF}, bb.data()[0]);
408   EXPECT_EQ(byte{0xF7}, bb.data()[1]);
409   EXPECT_EQ(byte{0x00}, bb.data()[2]);
410   EXPECT_EQ(byte{0x08}, bb.data()[3]);
411 
412   bb.PutInt16(0xFAFA, std::endian::big);
413   EXPECT_EQ(4u, bb.size());
414   EXPECT_EQ(Status::ResourceExhausted(), bb.status());
415 }
416 
TEST(ByteBuffer,Putting32ByteInts_Full_kLittleEndian)417 TEST(ByteBuffer, Putting32ByteInts_Full_kLittleEndian) {
418   ByteBuffer<8> bb;
419   bb.PutInt32(0xFFFFFFF1);
420   bb.PutUint32(0x00000014);
421 
422   EXPECT_EQ(byte{0xF1}, bb.data()[0]);
423   EXPECT_EQ(byte{0xFF}, bb.data()[1]);
424   EXPECT_EQ(byte{0xFF}, bb.data()[2]);
425   EXPECT_EQ(byte{0xFF}, bb.data()[3]);
426   EXPECT_EQ(byte{0x14}, bb.data()[4]);
427   EXPECT_EQ(byte{0x00}, bb.data()[5]);
428   EXPECT_EQ(byte{0x00}, bb.data()[6]);
429   EXPECT_EQ(byte{0x00}, bb.data()[7]);
430 
431   EXPECT_EQ(OkStatus(), bb.status());
432 }
433 
TEST(ByteBuffer,Putting32ByteInts_Exhausted_kBigEndian)434 TEST(ByteBuffer, Putting32ByteInts_Exhausted_kBigEndian) {
435   ByteBuffer<10> bb;
436   bb.PutInt32(0xF92927B2, std::endian::big);
437   bb.PutUint32(0x0C90739E, std::endian::big);
438 
439   EXPECT_EQ(byte{0xF9}, bb.data()[0]);
440   EXPECT_EQ(byte{0x29}, bb.data()[1]);
441   EXPECT_EQ(byte{0x27}, bb.data()[2]);
442   EXPECT_EQ(byte{0xB2}, bb.data()[3]);
443   EXPECT_EQ(byte{0x0C}, bb.data()[4]);
444   EXPECT_EQ(byte{0x90}, bb.data()[5]);
445   EXPECT_EQ(byte{0x73}, bb.data()[6]);
446   EXPECT_EQ(byte{0x9E}, bb.data()[7]);
447 
448   bb.PutInt32(-114743374, std::endian::big);
449   EXPECT_EQ(8u, bb.size());
450   EXPECT_EQ(Status::ResourceExhausted(), bb.status());
451 }
452 
TEST(ByteBuffer,Putting64ByteInts_Full_kLittleEndian)453 TEST(ByteBuffer, Putting64ByteInts_Full_kLittleEndian) {
454   ByteBuffer<16> bb;
455   bb.PutInt64(0x000001E8A7A0D569);
456   bb.PutUint64(0xFFFFFE17585F2A97);
457 
458   EXPECT_EQ(byte{0x69}, bb.data()[0]);
459   EXPECT_EQ(byte{0xD5}, bb.data()[1]);
460   EXPECT_EQ(byte{0xA0}, bb.data()[2]);
461   EXPECT_EQ(byte{0xA7}, bb.data()[3]);
462   EXPECT_EQ(byte{0xE8}, bb.data()[4]);
463   EXPECT_EQ(byte{0x01}, bb.data()[5]);
464   EXPECT_EQ(byte{0x00}, bb.data()[6]);
465   EXPECT_EQ(byte{0x00}, bb.data()[7]);
466   EXPECT_EQ(byte{0x97}, bb.data()[8]);
467   EXPECT_EQ(byte{0x2A}, bb.data()[9]);
468   EXPECT_EQ(byte{0x5F}, bb.data()[10]);
469   EXPECT_EQ(byte{0x58}, bb.data()[11]);
470   EXPECT_EQ(byte{0x17}, bb.data()[12]);
471   EXPECT_EQ(byte{0xFE}, bb.data()[13]);
472   EXPECT_EQ(byte{0xFF}, bb.data()[14]);
473   EXPECT_EQ(byte{0xFF}, bb.data()[15]);
474 
475   EXPECT_EQ(OkStatus(), bb.status());
476 }
477 
TEST(ByteBuffer,Putting64ByteInts_Exhausted_kBigEndian)478 TEST(ByteBuffer, Putting64ByteInts_Exhausted_kBigEndian) {
479   ByteBuffer<20> bb;
480   bb.PutUint64(0x000001E8A7A0D569, std::endian::big);
481   bb.PutInt64(0xFFFFFE17585F2A97, std::endian::big);
482 
483   EXPECT_EQ(byte{0x00}, bb.data()[0]);
484   EXPECT_EQ(byte{0x00}, bb.data()[1]);
485   EXPECT_EQ(byte{0x01}, bb.data()[2]);
486   EXPECT_EQ(byte{0xE8}, bb.data()[3]);
487   EXPECT_EQ(byte{0xA7}, bb.data()[4]);
488   EXPECT_EQ(byte{0xA0}, bb.data()[5]);
489   EXPECT_EQ(byte{0xD5}, bb.data()[6]);
490   EXPECT_EQ(byte{0x69}, bb.data()[7]);
491   EXPECT_EQ(byte{0xFF}, bb.data()[8]);
492   EXPECT_EQ(byte{0xFF}, bb.data()[9]);
493   EXPECT_EQ(byte{0xFE}, bb.data()[10]);
494   EXPECT_EQ(byte{0x17}, bb.data()[11]);
495   EXPECT_EQ(byte{0x58}, bb.data()[12]);
496   EXPECT_EQ(byte{0x5F}, bb.data()[13]);
497   EXPECT_EQ(byte{0x2A}, bb.data()[14]);
498   EXPECT_EQ(byte{0x97}, bb.data()[15]);
499 
500   bb.PutInt64(-6099875637501324530, std::endian::big);
501   EXPECT_EQ(16u, bb.size());
502   EXPECT_EQ(Status::ResourceExhausted(), bb.status());
503 }
504 
TEST(ByteBuffer,PuttingInts_MixedTypes_MixedEndian)505 TEST(ByteBuffer, PuttingInts_MixedTypes_MixedEndian) {
506   ByteBuffer<16> bb;
507   bb.PutUint8(0x03);
508   bb.PutInt16(0xFD6D, std::endian::big);
509   bb.PutUint32(0x482B3D9E);
510   bb.PutInt64(0x9A1C3641843DF317, std::endian::big);
511   bb.PutInt8(0xFB);
512 
513   EXPECT_EQ(byte{0x03}, bb.data()[0]);
514   EXPECT_EQ(byte{0xFD}, bb.data()[1]);
515   EXPECT_EQ(byte{0x6D}, bb.data()[2]);
516   EXPECT_EQ(byte{0x9E}, bb.data()[3]);
517   EXPECT_EQ(byte{0x3D}, bb.data()[4]);
518   EXPECT_EQ(byte{0x2B}, bb.data()[5]);
519   EXPECT_EQ(byte{0x48}, bb.data()[6]);
520   EXPECT_EQ(byte{0x9A}, bb.data()[7]);
521   EXPECT_EQ(byte{0x1C}, bb.data()[8]);
522   EXPECT_EQ(byte{0x36}, bb.data()[9]);
523   EXPECT_EQ(byte{0x41}, bb.data()[10]);
524   EXPECT_EQ(byte{0x84}, bb.data()[11]);
525   EXPECT_EQ(byte{0x3D}, bb.data()[12]);
526   EXPECT_EQ(byte{0xF3}, bb.data()[13]);
527   EXPECT_EQ(byte{0x17}, bb.data()[14]);
528   EXPECT_EQ(byte{0xFB}, bb.data()[15]);
529 
530   EXPECT_EQ(OkStatus(), bb.status());
531 }
532 
TEST(ByteBuffer,Iterator)533 TEST(ByteBuffer, Iterator) {
534   std::array<byte, 3> buffer = MakeBytes(0x01, 0x02, 0x03);
535   ByteBuffer<8> bb;
536   EXPECT_TRUE(bb.append(buffer).ok());
537 
538   auto it = bb.begin();
539   EXPECT_EQ(*it, byte{0x01});
540 
541   ++it;
542   EXPECT_EQ(*it, byte{0x02});
543   EXPECT_EQ(it - bb.begin(), 1);
544 
545   ++it;
546   EXPECT_EQ(*it, byte{0x03});
547   EXPECT_EQ(it - bb.begin(), 2);
548 
549   ++it;
550   EXPECT_EQ(it, bb.end());
551   EXPECT_EQ(static_cast<size_t>(it - bb.begin()), bb.size());
552 }
553 
TEST(ByteBuffer,Iterator_PreIncrement)554 TEST(ByteBuffer, Iterator_PreIncrement) {
555   std::array<byte, 3> buffer = MakeBytes(0x01, 0x02, 0x03);
556   ByteBuffer<8> bb;
557   EXPECT_TRUE(bb.append(buffer).ok());
558 
559   auto it = bb.begin();
560   EXPECT_EQ(*(++it), byte{0x02});
561 }
562 
TEST(ByteBuffer,Iterator_PostIncrement)563 TEST(ByteBuffer, Iterator_PostIncrement) {
564   std::array<byte, 3> buffer = MakeBytes(0x01, 0x02, 0x03);
565   ByteBuffer<8> bb;
566   EXPECT_TRUE(bb.append(buffer).ok());
567 
568   auto it = bb.begin();
569   EXPECT_EQ(*(it++), byte{0x01});
570   EXPECT_EQ(*it, byte{0x02});
571   EXPECT_EQ(*(it++), byte{0x02});
572   EXPECT_EQ(*it, byte{0x03});
573 }
574 
TEST(ByteBuffer,Iterator_PreDecrement)575 TEST(ByteBuffer, Iterator_PreDecrement) {
576   std::array<byte, 3> buffer = MakeBytes(0x01, 0x02, 0x03);
577   ByteBuffer<8> bb;
578   EXPECT_TRUE(bb.append(buffer).ok());
579   auto it = bb.begin();
580 
581   EXPECT_EQ(*it, byte{0x01});
582   ++it;
583   EXPECT_EQ(*it, byte{0x02});
584   ++it;
585   EXPECT_EQ(*it, byte{0x03});
586   ++it;
587   EXPECT_EQ(it, bb.end());
588 
589   --it;
590   EXPECT_EQ(*it, byte{0x03});
591   --it;
592   EXPECT_EQ(*it, byte{0x02});
593   --it;
594   EXPECT_EQ(*it, byte{0x01});
595   EXPECT_EQ(it, bb.begin());
596 }
597 
TEST(ByteBuffer,Iterator_PostDecrement)598 TEST(ByteBuffer, Iterator_PostDecrement) {
599   std::array<byte, 3> buffer = MakeBytes(0x01, 0x02, 0x03);
600   ByteBuffer<8> bb;
601   EXPECT_TRUE(bb.append(buffer).ok());
602   auto it = bb.begin();
603 
604   it += 2;
605   EXPECT_EQ(*it, byte{0x03});
606 
607   EXPECT_EQ(*(it--), byte{0x03});
608   EXPECT_EQ(*it, byte{0x02});
609   EXPECT_EQ(*(it--), byte{0x02});
610   EXPECT_EQ(*it, byte{0x01});
611 }
612 
TEST(ByteBuffer,Iterator_PlusEquals)613 TEST(ByteBuffer, Iterator_PlusEquals) {
614   std::array<byte, 3> buffer = MakeBytes(0x01, 0x02, 0x03);
615   ByteBuffer<8> bb;
616   EXPECT_TRUE(bb.append(buffer).ok());
617   auto it = bb.begin();
618 
619   it += 2;
620   EXPECT_EQ(*it, byte{0x03});
621 
622   it += -1;
623   EXPECT_EQ(*it, byte{0x02});
624 
625   it += 1;
626   EXPECT_EQ(*it, byte{0x03});
627 
628   it += -2;
629   EXPECT_EQ(*it, byte{0x01});
630 }
631 
TEST(ByteBuffer,Iterator_MinusEquals)632 TEST(ByteBuffer, Iterator_MinusEquals) {
633   std::array<byte, 3> buffer = MakeBytes(0x01, 0x02, 0x03);
634   ByteBuffer<8> bb;
635   EXPECT_TRUE(bb.append(buffer).ok());
636   auto it = bb.begin();
637 
638   it -= -2;
639   EXPECT_EQ(*it, byte{0x03});
640 
641   it -= +1;
642   EXPECT_EQ(*it, byte{0x02});
643 
644   it -= -1;
645   EXPECT_EQ(*it, byte{0x03});
646 
647   it -= +2;
648   EXPECT_EQ(*it, byte{0x01});
649 }
650 
TEST(ByteBuffer,Iterator_Plus)651 TEST(ByteBuffer, Iterator_Plus) {
652   std::array<byte, 3> buffer = MakeBytes(0x01, 0x02, 0x03);
653   ByteBuffer<8> bb;
654   EXPECT_TRUE(bb.append(buffer).ok());
655   auto it = bb.begin();
656 
657   auto test = it + 2;
658   EXPECT_EQ(*test, byte{0x03});
659 
660   test = test + -1;
661   EXPECT_EQ(*test, byte{0x02});
662 
663   test = test + 1;
664   EXPECT_EQ(*test, byte{0x03});
665 
666   test = test + (-2);
667   EXPECT_EQ(*test, byte{0x01});
668 }
669 
TEST(ByteBuffer,Iterator_Minus)670 TEST(ByteBuffer, Iterator_Minus) {
671   std::array<byte, 3> buffer = MakeBytes(0x01, 0x02, 0x03);
672   ByteBuffer<8> bb;
673   EXPECT_TRUE(bb.append(buffer).ok());
674   auto it = bb.begin();
675 
676   auto test = it - (-2);
677   EXPECT_EQ(*test, byte{0x03});
678 
679   test = test - 1;
680   EXPECT_EQ(*test, byte{0x02});
681 
682   test = test - (-1);
683   EXPECT_EQ(*test, byte{0x03});
684 
685   test = test - 2;
686   EXPECT_EQ(*test, byte{0x01});
687 }
688 
TEST(ByteBuffer,Iterator_LessThan)689 TEST(ByteBuffer, Iterator_LessThan) {
690   std::array<byte, 3> buffer = MakeBytes(0x01, 0x02, 0x03);
691   ByteBuffer<8> bb;
692   EXPECT_TRUE(bb.append(buffer).ok());
693 
694   auto it_1 = bb.begin();
695   auto it_2 = it_1 + 2;
696   EXPECT_EQ(*it_1, byte{0x01});
697   EXPECT_EQ(*it_2, byte{0x03});
698   EXPECT_TRUE(it_1 < it_2);
699 
700   it_1++;
701   it_2--;
702   EXPECT_EQ(*it_1, byte{0x02});
703   EXPECT_EQ(*it_2, byte{0x02});
704   EXPECT_FALSE(it_1 < it_2);
705 
706   it_1++;
707   it_2--;
708   EXPECT_EQ(*it_1, byte{0x03});
709   EXPECT_EQ(*it_2, byte{0x01});
710   EXPECT_FALSE(it_1 < it_2);
711   EXPECT_TRUE(it_2 < it_1);
712 }
713 
TEST(ByteBuffer,Iterator_GreaterThan)714 TEST(ByteBuffer, Iterator_GreaterThan) {
715   std::array<byte, 3> buffer = MakeBytes(0x01, 0x02, 0x03);
716   ByteBuffer<8> bb;
717   EXPECT_TRUE(bb.append(buffer).ok());
718 
719   auto it_1 = bb.begin();
720   auto it_2 = it_1 + 2;
721   EXPECT_EQ(*it_1, byte{0x01});
722   EXPECT_EQ(*it_2, byte{0x03});
723   EXPECT_FALSE(it_1 > it_2);
724 
725   it_1++;
726   it_2--;
727   EXPECT_EQ(*it_1, byte{0x02});
728   EXPECT_EQ(*it_2, byte{0x02});
729   EXPECT_FALSE(it_1 > it_2);
730 
731   it_1++;
732   it_2--;
733   EXPECT_EQ(*it_1, byte{0x03});
734   EXPECT_EQ(*it_2, byte{0x01});
735   EXPECT_TRUE(it_1 > it_2);
736   EXPECT_FALSE(it_2 > it_1);
737 }
738 
TEST(ByteBuffer,Iterator_LessThanEqual_GreaterThanEqual)739 TEST(ByteBuffer, Iterator_LessThanEqual_GreaterThanEqual) {
740   std::array<byte, 3> buffer = MakeBytes(0x01, 0x02, 0x03);
741   ByteBuffer<8> bb;
742   EXPECT_TRUE(bb.append(buffer).ok());
743 
744   auto it_1 = bb.begin();
745   auto it_2 = it_1 + 2;
746   EXPECT_EQ(*it_1, byte{0x01});
747   EXPECT_EQ(*it_2, byte{0x03});
748   EXPECT_FALSE(it_1 >= it_2);
749   EXPECT_TRUE(it_1 <= it_2);
750 
751   it_1++;
752   it_2--;
753   EXPECT_EQ(*it_1, byte{0x02});
754   EXPECT_EQ(*it_2, byte{0x02});
755   EXPECT_TRUE(it_1 >= it_2);
756   EXPECT_TRUE(it_1 <= it_2);
757 
758   it_1++;
759   it_2--;
760   EXPECT_EQ(*it_1, byte{0x03});
761   EXPECT_EQ(*it_2, byte{0x01});
762   EXPECT_FALSE(it_1 <= it_2);
763   EXPECT_TRUE(it_1 >= it_2);
764 }
765 
TEST(ByteBuffer,Iterator_Indexing)766 TEST(ByteBuffer, Iterator_Indexing) {
767   std::array<byte, 3> buffer = MakeBytes(0x01, 0x02, 0x03);
768   ByteBuffer<8> bb;
769   EXPECT_TRUE(bb.append(buffer).ok());
770 
771   auto it = bb.begin();
772   EXPECT_EQ(it[0], byte{0x01});
773   EXPECT_EQ(it[1], byte{0x02});
774   EXPECT_EQ(it[2], byte{0x03});
775 }
776 
TEST(ByteBuffer,Iterator_PeekValues_1Byte)777 TEST(ByteBuffer, Iterator_PeekValues_1Byte) {
778   ByteBuffer<3> bb;
779   bb.PutInt8(0xF2);
780   bb.PutUint8(0xE5);
781   bb.PutInt8(0x5F);
782 
783   auto it = bb.begin();
784   EXPECT_EQ(it.PeekInt8(), int8_t(0xF2));
785   it = it + 1;
786   EXPECT_EQ(it.PeekUint8(), uint8_t(0xE5));
787   it = it + 1;
788   EXPECT_EQ(it.PeekInt8(), int8_t(0x5F));
789 }
790 
TEST(ByteBuffer,Iterator_PeekValues_2Bytes)791 TEST(ByteBuffer, Iterator_PeekValues_2Bytes) {
792   ByteBuffer<4> bb;
793   bb.PutInt16(0xA7F1);
794   bb.PutUint16(0xF929, std::endian::big);
795 
796   auto it = bb.begin();
797   EXPECT_EQ(it.PeekInt16(), int16_t(0xA7F1));
798   it = it + 2;
799   EXPECT_EQ(it.PeekUint16(std::endian::big), uint16_t(0xF929));
800 }
801 
TEST(ByteBuffer,Iterator_PeekValues_4Bytes)802 TEST(ByteBuffer, Iterator_PeekValues_4Bytes) {
803   ByteBuffer<8> bb;
804   bb.PutInt32(0xFFFFFFF1);
805   bb.PutUint32(0xF92927B2, std::endian::big);
806 
807   auto it = bb.begin();
808   EXPECT_EQ(it.PeekInt32(), int32_t(0xFFFFFFF1));
809   it = it + 4;
810   EXPECT_EQ(it.PeekUint32(std::endian::big), uint32_t(0xF92927B2));
811 }
812 
TEST(ByteBuffer,Iterator_PeekValues_8Bytes)813 TEST(ByteBuffer, Iterator_PeekValues_8Bytes) {
814   ByteBuffer<16> bb;
815   bb.PutUint64(0x000001E8A7A0D569);
816   bb.PutInt64(0xFFFFFE17585F2A97, std::endian::big);
817 
818   auto it = bb.begin();
819   EXPECT_EQ(it.PeekUint64(), uint64_t(0x000001E8A7A0D569));
820   it = it + 8;
821   EXPECT_EQ(it.PeekInt64(std::endian::big), int64_t(0xFFFFFE17585F2A97));
822 }
823 
TEST(ByteBuffer,Iterator_ReadValues_1Byte)824 TEST(ByteBuffer, Iterator_ReadValues_1Byte) {
825   ByteBuffer<3> bb;
826   bb.PutInt8(0xF2);
827   bb.PutUint8(0xE5);
828   bb.PutInt8(0x5F);
829 
830   auto it = bb.begin();
831   EXPECT_EQ(it.ReadInt8(), int8_t(0xF2));
832   EXPECT_EQ(it.ReadUint8(), uint8_t(0xE5));
833   EXPECT_EQ(it.ReadInt8(), int8_t(0x5F));
834 }
835 
TEST(ByteBuffer,Iterator_ReadValues_2Bytes)836 TEST(ByteBuffer, Iterator_ReadValues_2Bytes) {
837   ByteBuffer<4> bb;
838   bb.PutInt16(0xA7F1);
839   bb.PutUint16(0xF929, std::endian::big);
840 
841   auto it = bb.begin();
842   EXPECT_EQ(it.ReadInt16(), int16_t(0xA7F1));
843   EXPECT_EQ(it.ReadUint16(std::endian::big), uint16_t(0xF929));
844 }
845 
TEST(ByteBuffer,Iterator_ReadValues_4Bytes)846 TEST(ByteBuffer, Iterator_ReadValues_4Bytes) {
847   ByteBuffer<8> bb;
848   bb.PutInt32(0xFFFFFFF1);
849   bb.PutUint32(0xF92927B2, std::endian::big);
850 
851   auto it = bb.begin();
852   EXPECT_EQ(it.ReadInt32(), int32_t(0xFFFFFFF1));
853   EXPECT_EQ(it.ReadUint32(std::endian::big), uint32_t(0xF92927B2));
854 }
855 
TEST(ByteBuffer,Iterator_ReadValues_8Bytes)856 TEST(ByteBuffer, Iterator_ReadValues_8Bytes) {
857   ByteBuffer<16> bb;
858   bb.PutUint64(0x000001E8A7A0D569);
859   bb.PutInt64(0xFFFFFE17585F2A97, std::endian::big);
860 
861   auto it = bb.begin();
862   EXPECT_EQ(it.ReadUint64(), uint64_t(0x000001E8A7A0D569));
863   EXPECT_EQ(it.ReadInt64(std::endian::big), int64_t(0xFFFFFE17585F2A97));
864 }
865 }  // namespace
866 }  // namespace pw
867