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