Line data Source code
1 : // Copyright 2024 Accenture.
2 :
3 : #include "estd/little_endian.h"
4 :
5 : #include "estd/memory.h"
6 :
7 : #include <gmock/gmock.h>
8 :
9 : using namespace ::testing;
10 :
11 1 : void example_little_endian_read()
12 : {
13 : // [EXAMPLE_BEGIN read le]
14 : // Example demonstrating reading little-endian values of various types
15 1 : uint8_t const b1[] = {0xAB, 0x14};
16 1 : EXPECT_EQ(0x14ABU, ::estd::read_le<uint16_t>(&b1[0]));
17 :
18 1 : uint8_t const b2[] = {0x13, 0x42, 0x32, 0x05};
19 1 : EXPECT_EQ(0x05324213U, ::estd::read_le<uint32_t>(&b2[0]));
20 :
21 1 : uint8_t const b3[] = {0x13, 0xAB};
22 1 : EXPECT_EQ(-21741, ::estd::read_le<int16_t>(&b3[0]));
23 :
24 1 : uint8_t const b4[] = {0x49, 0x56, 0xA6, 0x5B, 0x34, 0xBB, 0x66, 0xFF};
25 1 : EXPECT_EQ(-43141312863119799, ::estd::read_le<int64_t>(&b4[0]));
26 : // [EXAMPLE_END read le]
27 1 : }
28 :
29 1 : void example_little_endian_write()
30 : {
31 : // [EXAMPLE_BEGIN write le]
32 : // Example demonstrating writing little-endian values of various types
33 1 : uint8_t b1[1] = {0};
34 1 : ::estd::write_le<uint8_t>(&b1[0], 0x14);
35 1 : EXPECT_THAT(b1, ElementsAre(0x14));
36 :
37 1 : uint8_t b2[2] = {0};
38 1 : ::estd::write_le<uint16_t>(&b2[0], 0x1435);
39 1 : EXPECT_THAT(b2, ElementsAre(0x35, 0x14));
40 :
41 1 : uint8_t b3[8] = {0};
42 1 : ::estd::write_le<uint64_t>(&b3[0], 0x9876543210012409);
43 1 : EXPECT_THAT(b3, ElementsAre(0x09, 0x24, 0x01, 0x10, 0x32, 0x54, 0x76, 0x98));
44 :
45 1 : uint8_t b4[4] = {0};
46 1 : ::estd::write_le<int32_t>(&b4[0], -12345678);
47 1 : EXPECT_THAT(b4, ElementsAre(0xB2, 0x9E, 0x43, 0xFF));
48 : // [EXAMPLE_END write le]
49 1 : }
50 :
51 1 : void example_little_endian_assignment()
52 : {
53 : // [EXAMPLE_BEGIN assignment]
54 : // Assigning a little-endian value to a structure and checking the byte representation
55 1 : ::estd::le_uint64_t t;
56 1 : t = 0x1432968812610350;
57 1 : EXPECT_THAT(t.bytes, ElementsAre(0x50, 0x03, 0x61, 0x12, 0x88, 0x96, 0x32, 0x14));
58 : // [EXAMPLE_END assignment]
59 1 : }
60 :
61 1 : void example_little_endian_cast_to_primitive()
62 : {
63 : // [EXAMPLE_BEGIN cast to primitive]
64 : // Casting a little-endian structure to a primitive type and checking the result
65 1 : ::estd::le_uint64_t const t = {0xFF, 0xFF, 0xAB, 0xBA, 0x00, 0x09, 0x67, 0xE4};
66 1 : EXPECT_EQ(0xE4670900BAABFFFFU, t);
67 : // [EXAMPLE_END cast to primitive]
68 1 : }
69 :
70 1 : void example_little_endian_make()
71 : {
72 : // [EXAMPLE_BEGIN make]
73 : // Creating a little-endian structure from a primitive value and checking the byte
74 : // representation
75 1 : ::estd::le_uint64_t const t = ::estd::le_uint64_t::make(0x0798349078563412);
76 1 : EXPECT_THAT(t.bytes, ElementsAre(0x12, 0x34, 0x56, 0x78, 0x90, 0x34, 0x98, 0x07));
77 : // [EXAMPLE_END make]
78 1 : }
79 :
80 1 : void example_little_endian_make_le()
81 : {
82 : // [EXAMPLE_BEGIN make le]
83 : // Creating a little-endian structure from a primitive value using a template function and
84 : // checking the byte representation
85 1 : ::estd::le_uint64_t const t = ::estd::make_le<uint64_t>(0x0798349078563412);
86 1 : EXPECT_THAT(t.bytes, ElementsAre(0x12, 0x34, 0x56, 0x78, 0x90, 0x34, 0x98, 0x07));
87 : // [EXAMPLE_END make le]
88 1 : }
89 :
90 1 : void example_little_endian_read_le_24()
91 : {
92 : // [EXAMPLE_BEGIN read le 24]
93 : // Reading a little-endian 24-bit value from a byte array and checking the result
94 1 : uint8_t const buffer[] = {0x13, 0x32, 0x05};
95 1 : EXPECT_EQ(0x00053213U, ::estd::read_le_24(&buffer[0]));
96 : // [EXAMPLE_END read le 24]
97 1 : }
98 :
99 1 : void example_little_endian_write_le_24()
100 : {
101 : // [EXAMPLE_BEGIN write le 24]
102 : // Writing a little-endian 24-bit value to a byte array and checking the result
103 1 : uint8_t buffer[3] = {0};
104 1 : ::estd::write_le_24(&buffer[0], 0x13562897U);
105 1 : EXPECT_THAT(buffer, ElementsAre(0x97, 0x28, 0x56));
106 : // [EXAMPLE_END write le 24]
107 1 : }
108 :
109 1 : void example_little_endian_read_le_48()
110 : {
111 : // [EXAMPLE_BEGIN read le 48]
112 : // Reading a little-endian 48-bit value from a byte array and checking the result
113 1 : uint8_t const buffer[] = {0x13, 0x32, 0x11, 0x44, 0x59, 0x73};
114 1 : EXPECT_EQ(0x0000735944113213U, ::estd::read_le_48(&buffer[0]));
115 : // [EXAMPLE_END read le 48]
116 1 : }
117 :
118 1 : void example_little_endian_write_le_48()
119 : {
120 : // [EXAMPLE_BEGIN write le 48]
121 : // Writing a little-endian 48-bit value to a byte array and checking the result
122 1 : uint8_t buffer[6] = {0};
123 1 : ::estd::write_le_48(&buffer[0], 0x5199881234567899U);
124 1 : EXPECT_THAT(buffer, ElementsAre(0x99, 0x78, 0x56, 0x34, 0x12, 0x88));
125 : // [EXAMPLE_END write le 48]
126 1 : }
127 :
128 1 : void example_little_endian_read_le_bits()
129 : {
130 : // [EXAMPLE_BEGIN read le bits]
131 1 : constexpr auto NUM_BITS = sizeof(uint8_t) * 8;
132 1 : uint8_t const buffer[NUM_BITS * 4] = {0xA5, 0xA5, 0xA5, 0xA5};
133 :
134 : // Reading 8 bits from the buffer starting at position 8 and expecting the result to be 0xA5
135 1 : EXPECT_EQ(0xA5, ::estd::read_le_bits<uint8_t>(&buffer[0], 8, 8));
136 : // Reading 8 bits from the buffer starting at position 12 and expecting the result to be 0xA5
137 1 : EXPECT_EQ(0xA5, ::estd::read_le_bits<uint8_t>(&buffer[0], 12, 8));
138 : // Reading 8 bits from the buffer starting at position 16 and expecting the result to be 0xA5
139 1 : EXPECT_EQ(0xA5, ::estd::read_le_bits<uint8_t>(&buffer[0], 16, 8));
140 :
141 : // Reading 16 bits from the buffer starting at position 12 and expecting the result to be 0xAA55
142 1 : EXPECT_EQ(0xAA55, ::estd::read_le_bits<uint16_t>(&buffer[0], 12, 16));
143 : // Reading 16 bits from the buffer starting at position 16 and expecting the result to be 0xAA55
144 1 : EXPECT_EQ(0xA5A5, ::estd::read_le_bits<uint16_t>(&buffer[0], 16, 16));
145 : // [EXAMPLE_END read le bits]
146 1 : }
147 :
148 1 : void example_little_endian_write_le_bits()
149 : {
150 : // [EXAMPLE_BEGIN write le bits]
151 :
152 1 : constexpr auto NUM_BYTES = sizeof(uint32_t);
153 1 : uint8_t dest[NUM_BYTES];
154 : // Writing 24 bits (3 bytes) to the destination array starting at position 8
155 1 : ::estd::memory::set(static_cast<::estd::slice<uint8_t>>(dest), 0);
156 1 : ::estd::write_le_bits<uint64_t>(&dest[0], 0x32589945, 8, 24);
157 1 : EXPECT_THAT(dest, ElementsAre(0x00, 0x45, 0x99, 0x58));
158 : // Writing 24 bits (3 bytes) to the destination array starting at position 4
159 1 : ::estd::memory::set(static_cast<::estd::slice<uint8_t>>(dest), 0);
160 1 : ::estd::write_le_bits<uint64_t>(&dest[0], 0x32589945, 4, 24);
161 1 : EXPECT_THAT(dest, ElementsAre(0x05, 0x94, 0x89, 0x50));
162 :
163 : // [EXAMPLE_END write le bits]
164 1 : }
165 :
166 3 : TEST(little_endian, run_examples)
167 : {
168 1 : example_little_endian_read();
169 1 : example_little_endian_write();
170 1 : example_little_endian_assignment();
171 1 : example_little_endian_cast_to_primitive();
172 1 : example_little_endian_make();
173 1 : example_little_endian_make_le();
174 1 : example_little_endian_read_le_24();
175 1 : example_little_endian_write_le_24();
176 1 : example_little_endian_read_le_48();
177 1 : example_little_endian_write_le_48();
178 1 : example_little_endian_read_le_bits();
179 1 : example_little_endian_write_le_bits();
180 1 : }
|