-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.c
272 lines (216 loc) · 6.16 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include "bit_pack.h"
void print_array_raw( const uint8_t *bytes, int len );
static void test_unpack_uint64_be_aligned( void **state );
static void test_unpack_uint64_le_aligned( void **state );
static void test_unpack_uint64_be_unaligned( void **state );
static void test_unpack_uint64_le_unaligned( void **state );
static void test_pack_uint64_le_aligned( void **state );
static void test_pack_uint64_be_aligned( void **state );
struct test
{
uint64_t padding : 3;
uint64_t element1 : 32;
};
int main(void)
{
struct test teststruct = { 0, 0xBEEFFACE };
printf( "Printing a example of how a 32 bit field is packed using a C bit packed structure on your system:\n" );
print_array_raw( (uint8_t*)&teststruct, 8 );
const struct UnitTest tests[] =
{
unit_test( test_unpack_uint64_be_aligned ),
unit_test( test_unpack_uint64_le_aligned ),
unit_test( test_unpack_uint64_be_unaligned ),
unit_test( test_unpack_uint64_le_unaligned ),
unit_test( test_pack_uint64_le_aligned ),
unit_test( test_pack_uint64_be_aligned ),
};
return run_tests(tests);
}
static void test_unpack_uint64_be_aligned( void **state )
{
const uint8_t input[8] = { 0x00,
0xBE,
0xEF,
0xFA,
0xCE,
0x00,
0x00,
0x00 };
const uint64_t expected = 0xBEEFFACE;
uint64_t result;
(void) state; /* unused */
print_array_raw( input, 8 );
/* Unpack that big word we just wrote in */
int err = unpack_uint64_be( input, sizeof( input ), 24, 32, &result );
assert_int_equal( 0, err );
assert_int_equal( expected, result );
}
static void test_unpack_uint64_le_aligned( void **state )
{
const uint8_t input[8] = { 0x00,
0xCE,
0xFA,
0xEF,
0xBE,
0x00,
0x00,
0x00 };
const uint64_t expected = 0xBEEFFACE;
uint64_t result;
(void) state; /* unused */
print_array_raw( input, 8 );
/* Unpack that big word we just wrote in */
int err = unpack_uint64_le( input, sizeof( input ), 8, 32, &result );
assert_int_equal( 0, err );
assert_int_equal( expected, result );
}
static void test_unpack_uint64_be_unaligned( void **state )
{
const uint8_t input[8] = { 0x00,
0x0B,
0xEE,
0xFF,
0xAC,
0xE0,
0x00,
0x00 };
const uint64_t expected = 0xBEEFFACE;
uint64_t result;
int err;
(void) state; /* unused */
print_array_raw( input, 8 );
/* Unpack that big word we just wrote in */
err = unpack_uint64_be( input, sizeof( input ), 20, 32, &result );
assert_int_equal( 0, err );
assert_int_equal( expected, result );
/* Unpack that big word we just wrote in */
err = unpack_uint64_be_rev( input, sizeof( input ), 12, 32, &result );
assert_int_equal( 0, err );
assert_int_equal( expected, result );
}
static void test_unpack_uint64_le_unaligned( void **state )
{
const uint8_t input[8] = { 0x70,
0xd6,
0x7f,
0xf7,
0xfd,
0x7f,
0x00,
0x00 };
const uint64_t expected = 0xBEEFFACE;
uint64_t result;
(void) state; /* unused */
print_array_raw( input, 8 );
/* Unpack that bitest_pack_uint64_le_alignedg word we just wrote in */
int err = unpack_uint64_le( input, sizeof( input ), 3, 32, &result );
assert_int_equal( 0, err );
assert_int_equal( expected, result );
}
static void test_pack_uint64_be_aligned( void **state )
{
const uint8_t expected[8] = { 0x00,
0xBE,
0xEF,
0xFA,
0xCE,
0x00,
0x00,
0x00 };
uint8_t result[8] = { 0, };
const uint64_t input = 0xBEEFFACE;
(void) state; /* unused */
/* Unpack that big word we just wrote in */
int err = pack_uint64_be( result, sizeof( result ), 24, 32, input );
print_array_raw( result, sizeof( result ) );
assert_int_equal( 0, err );
assert_memory_equal( expected, result, sizeof( expected ) );
}
static void test_pack_uint64_le_aligned( void **state )
{
const uint8_t expected[8] = { 0x00,
0xCE,
0xFA,
0xEF,
0xBE,
0x00,
0x00,
0x00 };
uint8_t result[8] = { 0, };
const uint64_t input = 0xBEEFFACE;
(void) state; /* unused */
/* Unpack that big word we just wrote in */
int err = pack_uint64_le( result, sizeof( result ), 8, 32, input );
print_array_raw( result, sizeof( result ) );
assert_int_equal( 0, err );
assert_memory_equal( expected, result, sizeof( expected ) );
}
static void test_pack_uint64_be_unaligned( void **state )
{
const uint8_t expected[8] = { 0x00,
0x0B,
0xEE,
0xFF,
0xAC,
0xE0,
0x00,
0x00 };
uint8_t result[8] = { 0, };
const uint64_t input = 0xBEEFFACE;
int err;
(void) state; /* unused */
/* Unpack that big word we just wrote in */
err = pack_uint64_be( result, sizeof( result ), 20, 32, input );
print_array_raw( result, sizeof( result ) );
assert_int_equal( 0, err );
assert_memory_equal( expected, result, sizeof( expected ) );
/* Unpack that big word we just wrote in */
err = pack_uint64_be_rev( result, sizeof( result ), 12, 32, input );
print_array_raw( result, sizeof( result ) );
assert_int_equal( 0, err );
assert_memory_equal( expected, result, sizeof( expected ) );
}
static void test_pack_uint64_le_unaligned( void **state )
{
const uint8_t expected[8] = { 0x70,
0xd6,
0x7f,
0xf7,
0xfd,
0x7f,
0x00,
0x00 };
uint8_t result[8] = { 0, };
const uint64_t input = 0xBEEFFACE;
(void) state; /* unused */
/* Unpack that big word we just wrote in */
int err = pack_uint64_le( result, sizeof( result ), 3, 32, input );
print_array_raw( result, sizeof( result ) );
assert_int_equal( 0, err );
assert_memory_equal( expected, result, sizeof( expected ) );
}
void print_array_raw( const uint8_t *bytes, const int len )
{
int idx;
int bitoffset;
//printf( "Raw test array:\n" );
for ( idx = 0; idx < len; idx++ )
{
printf("[");
for ( bitoffset = 7; bitoffset >= 0; bitoffset-- )
{
printf( "%01u", 0x1 & (bytes[idx] >> bitoffset ) );
}
printf("]");
printf("[%02x]", bytes[idx]);
printf("\n");
}
}