Skip to content

Commit fcb8cc9

Browse files
committed
Update README
Add tests for example serializations
1 parent 2fa61ad commit fcb8cc9

File tree

2 files changed

+159
-18
lines changed

2 files changed

+159
-18
lines changed

README.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,8 @@ The examples below follow the same structure: First writing to a buffer and then
242242
Writing the first 5 bits of an int to the buffer, then reading it back:
243243
```cpp
244244
// Create a writer, referencing the buffer and its size
245-
uint8_t buffer[4]; // Buffer must be a multiple of 4 bytes / 32 bits
246-
bit_writer writer(buffer, 4);
245+
byte_buffer<4> buffer; // Buffer must be a multiple of 4 bytes / 32 bits
246+
bit_writer writer(buffer);
247247
248248
// Write the value
249249
uint32_t value = 27; // We can choose any value below 2^5. Otherwise we need more than 5 bits
@@ -263,18 +263,18 @@ reader.serialize_bits(out_value, 5); // out_value should now have a value of 27
263263
Writing a signed int to the buffer, within a range:
264264
```cpp
265265
// Create a writer, referencing the buffer and its size
266-
uint8_t buffer[4];
267-
bit_writer writer(buffer, 4);
266+
byte_buffer<4> buffer;
267+
bit_writer writer(buffer);
268268

269269
// Write the value
270270
int32_t value = -45; // We can choose any value within the range below
271271
writer.serialize<int32_t>(value, -90, 40); // A lower and upper bound which the value will be quantized between
272272

273273
// Flush the writer's remaining state into the buffer
274-
writer.flush();
274+
uint32_t num_bytes = writer.flush();
275275

276276
// Create a reader by moving and invalidating the writer
277-
bit_reader reader(std::move(writer));
277+
bit_reader reader(buffer, num_bytes);
278278

279279
// Read the value back
280280
int32_t out_value; // We don't have to initialize it yet
@@ -284,18 +284,18 @@ reader.serialize<int32_t>(out_value, -90, 40); // out_value should now have a va
284284
Writing a c-style string into the buffer:
285285
```cpp
286286
// Create a writer, referencing the buffer and its size
287-
uint8_t buffer[32];
288-
bit_writer writer(buffer, 32);
287+
byte_buffer<32> buffer;
288+
bit_writer writer(buffer);
289289
290290
// Write the value
291291
const char* value = "Hello world!";
292292
writer.serialize<const char*>(value, 32U); // The second argument is the maximum size we expect the string to be
293293
294294
// Flush the writer's remaining state into the buffer
295-
writer.flush();
295+
uint32_t num_bytes = writer.flush();
296296
297297
// Create a reader by moving and invalidating the writer
298-
bit_reader reader(std::move(writer));
298+
bit_reader reader(buffer, num_bytes);
299299
300300
// Read the value back
301301
char out_value[32]; // Set the size to the max size
@@ -305,18 +305,18 @@ reader.serialize<const char*>(out_value, 32U); // out_value should now contain "
305305
Writing a std::string into the buffer:
306306
```cpp
307307
// Create a writer, referencing the buffer and its size
308-
uint8_t buffer[32];
309-
bit_writer writer(buffer, 32);
308+
byte_buffer<32> buffer;
309+
bit_writer writer(buffer);
310310

311311
// Write the value
312312
std::string value = "Hello world!";
313313
writer.serialize<std::string>(value, 32U); // The second argument is the maximum size we expect the string to be
314314

315315
// Flush the writer's remaining state into the buffer
316-
writer.flush();
316+
uint32_t num_bytes = writer.flush();
317317

318318
// Create a reader by moving and invalidating the writer
319-
bit_reader reader(std::move(writer));
319+
bit_reader reader(buffer, num_bytes);
320320

321321
// Read the value back
322322
std::string out_value; // The string will be resized if the output doesn't fit
@@ -326,19 +326,19 @@ reader.serialize<std::string>(out_value, 32U); // out_value should now contain "
326326
Writing a float into the buffer with a bounded range and precision:
327327
```cpp
328328
// Create a writer, referencing the buffer and its size
329-
uint8_t buffer[4];
330-
bit_writer writer(buffer, 4);
329+
byte_buffer<4> buffer;
330+
bit_writer writer(buffer);
331331
332332
// Write the value
333333
bounded_range range(1.0f, 4.0f, 1.0f / 128.0f); // Min, Max, Precision
334334
float value = 1.2345678f;
335335
writer.serialize<bounded_range>(range, value);
336336
337337
// Flush the writer's remaining state into the buffer
338-
writer.flush();
338+
uint32_t num_bytes = writer.flush();
339339
340340
// Create a reader by moving and invalidating the writer
341-
bit_reader reader(std::move(writer));
341+
bit_reader reader(buffer, num_bytes);
342342
343343
// Read the value back
344344
float out_value;

src/test/examples_test.cpp

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
#include "../shared/assert.h"
2+
#include "../shared/test.h"
3+
4+
#include <bitstream/stream/bit_reader.h>
5+
#include <bitstream/stream/bit_writer.h>
6+
#include <bitstream/utility/bits.h>
7+
8+
#include <bitstream/traits/integral_traits.h>
9+
#include <bitstream/traits/quantization_traits.h>
10+
#include <bitstream/traits/string_traits.h>
11+
12+
namespace bitstream::test::example
13+
{
14+
// Writing the first 5 bits of an int to the buffer, then reading it back
15+
BS_ADD_TEST(test_example1)
16+
{
17+
// Create a writer, referencing the buffer and its size
18+
byte_buffer<4> buffer; // Buffer must be a multiple of 4 bytes / 32 bits
19+
bit_writer writer(buffer);
20+
21+
// Write the value
22+
uint32_t value = 27; // We can choose any value below 2^5. Otherwise we need more than 5 bits
23+
BS_TEST_ASSERT(writer.serialize_bits(value, 5));
24+
25+
// Flush the writer's remaining state into the buffer
26+
uint32_t num_bytes = writer.flush();
27+
28+
BS_TEST_ASSERT_OPERATION(num_bytes, == , 1);
29+
30+
// Create a reader, referencing the buffer and bytes written
31+
bit_reader reader(buffer, num_bytes);
32+
33+
// Read the value back
34+
uint32_t out_value; // We don't have to initialize it yet
35+
BS_TEST_ASSERT(reader.serialize_bits(out_value, 5)); // out_value should now have a value of 27
36+
37+
BS_TEST_ASSERT_OPERATION(out_value, == , value);
38+
}
39+
40+
// Writing a signed int to the buffer, within a range
41+
BS_ADD_TEST(test_example2)
42+
{
43+
// Create a writer, referencing the buffer and its size
44+
byte_buffer<4> buffer;
45+
bit_writer writer(buffer);
46+
47+
// Write the value
48+
int32_t value = -45; // We can choose any value within the range below
49+
BS_TEST_ASSERT(writer.serialize<int32_t>(value, -90, 40)); // A lower and upper bound which the value will be quantized between
50+
51+
// Flush the writer's remaining state into the buffer
52+
uint32_t num_bytes = writer.flush();
53+
54+
BS_TEST_ASSERT_OPERATION(num_bytes, <= , 4);
55+
56+
// Create a reader by moving and invalidating the writer
57+
bit_reader reader(buffer, num_bytes);
58+
59+
// Read the value back
60+
int32_t out_value; // We don't have to initialize it yet
61+
BS_TEST_ASSERT(reader.serialize<int32_t>(out_value, -90, 40)); // out_value should now have a value of -45
62+
63+
BS_TEST_ASSERT_OPERATION(out_value, == , value);
64+
}
65+
66+
// Writing a c-style string into the buffer
67+
BS_ADD_TEST(test_example3)
68+
{
69+
// Create a writer, referencing the buffer and its size
70+
byte_buffer<32> buffer;
71+
bit_writer writer(buffer);
72+
73+
// Write the value
74+
const char* value = "Hello world!";
75+
BS_TEST_ASSERT(writer.serialize<const char*>(value, 32U)); // The second argument is the maximum size we expect the string to be
76+
77+
// Flush the writer's remaining state into the buffer
78+
uint32_t num_bytes = writer.flush();
79+
80+
// Create a reader by moving and invalidating the writer
81+
bit_reader reader(buffer, num_bytes);
82+
83+
// Read the value back
84+
char out_value[32]; // Set the size to the max size
85+
BS_TEST_ASSERT(reader.serialize<const char*>(out_value, 32U)); // out_value should now contain "Hello world!\0"
86+
87+
BS_TEST_ASSERT(strcmp(out_value, value) == 0);
88+
}
89+
90+
// Writing a std::string into the buffer
91+
BS_ADD_TEST(test_example4)
92+
{
93+
// Create a writer, referencing the buffer and its size
94+
byte_buffer<32> buffer;
95+
bit_writer writer(buffer);
96+
97+
// Write the value
98+
std::string value = "Hello world!";
99+
BS_TEST_ASSERT(writer.serialize<std::string>(value, 32U)); // The second argument is the maximum size we expect the string to be
100+
101+
// Flush the writer's remaining state into the buffer
102+
uint32_t num_bytes = writer.flush();
103+
104+
// Create a reader by moving and invalidating the writer
105+
bit_reader reader(buffer, num_bytes);
106+
107+
// Read the value back
108+
std::string out_value; // The string will be resized if the output doesn't fit
109+
BS_TEST_ASSERT(reader.serialize<std::string>(out_value, 32U)); // out_value should now contain "Hello world!"
110+
111+
BS_TEST_ASSERT_OPERATION(out_value, == , value);
112+
}
113+
114+
// Writing a float into the buffer with a bounded range and precision
115+
BS_ADD_TEST(test_example5)
116+
{
117+
// Create a writer, referencing the buffer and its size
118+
byte_buffer<4> buffer;
119+
bit_writer writer(buffer);
120+
121+
// Write the value
122+
bounded_range range(1.0f, 4.0f, 1.0f / 128.0f); // Min, Max, Precision
123+
float value = 1.2345678f;
124+
writer.serialize<bounded_range>(range, value);
125+
126+
// Flush the writer's remaining state into the buffer
127+
uint32_t num_bytes = writer.flush();
128+
129+
BS_TEST_ASSERT_OPERATION(num_bytes, <= , 4);
130+
131+
// Create a reader by moving and invalidating the writer
132+
bit_reader reader(buffer, num_bytes);
133+
134+
// Read the value back
135+
float out_value;
136+
reader.serialize<bounded_range>(range, out_value); // out_value should now be a value close to 1.2345678f
137+
138+
BS_TEST_ASSERT_OPERATION(std::abs(value - out_value), <= , range.get_precision());
139+
BS_TEST_ASSERT_OPERATION(range.get_bits_required(), < , 32);
140+
}
141+
}

0 commit comments

Comments
 (0)