forked from stevegolton/libbitpack
/
main.c
163 lines (117 loc) · 3.99 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
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include "bit_twiddle.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_uint32_be_byaligned( void **state );
static void test_unpack_uint32_le_byaligned( void **state );
static void test_unpack_uint32_be_unaligned( void **state );
static void test_unpack_uint32_le_unaligned( void **state );
int main(void)
{
const struct CMUnitTest tests[] =
{
cmocka_unit_test( test_unpack_uint64_be_aligned ),
cmocka_unit_test( test_unpack_uint64_le_aligned ),
cmocka_unit_test( test_unpack_uint32_be_byaligned ),
cmocka_unit_test( test_unpack_uint32_le_byaligned ),
cmocka_unit_test( test_unpack_uint32_be_unaligned ),
cmocka_unit_test( test_unpack_uint32_le_unaligned ),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}
static void test_unpack_uint64_be_aligned( void **state )
{
const uint8_t input[8] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF };
const uint64_t expected = 0x0123456789ABCDEF;
uint64_t result;
(void) state; /* unused */
print_array_raw( input, 8 );
/* Unpack that big word we just wrote in */
int err = UnpackUint64( input, 0, 64, eByteOrder_MSBFirst, &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] = { 0xEF, 0xCD, 0xAB, 0x89, 0x67, 0x45, 0x23, 0x01 };
const uint64_t expected = 0x0123456789ABCDEF;
uint64_t result;
(void) state; /* unused */
print_array_raw( input, 8 );
/* Unpack that big word we just wrote in */
int err = UnpackUint64( input, 0, 64, eByteOrder_LSBFirst, &result );
assert_int_equal( 0, err );
assert_int_equal( expected, result );
}
static void test_unpack_uint32_be_byaligned( void **state )
{
const uint8_t input[8] = { 0xFF, 0x01, 0x23, 0x45, 0x67, 0xFF, 0xFF, 0xFF };
const uint64_t expected = 0x01234567;
uint64_t result;
(void) state; /* unused */
print_array_raw( input, 8 );
/* Unpack that big word we just wrote in */
int err = UnpackUint64( input, 8, 32, eByteOrder_MSBFirst, &result );
assert_int_equal( 0, err );
assert_int_equal( expected, result );
}
static void test_unpack_uint32_le_byaligned( void **state )
{
const uint8_t input[8] = { 0xFF, 0x67, 0x45, 0x23, 0x01, 0xFF, 0xFF, 0xFF };
const uint64_t expected = 0x01234567;
uint64_t result;
(void) state; /* unused */
print_array_raw( input, 8 );
/* Unpack that big word we just wrote in */
int err = UnpackUint64( input, 8, 32, eByteOrder_LSBFirst, &result );
assert_int_equal( 0, err );
assert_int_equal( expected, result );
}
static void test_unpack_uint32_be_unaligned( void **state )
{
const uint8_t input[8] = { 0x00, 0x02, 0x46, 0x8A, 0xCE, 0x10, 0x00, 0x00 };
const uint64_t expected = 0x01234567;
uint64_t result;
(void) state; /* unused */
print_array_raw( input, 8 );
/* Unpack that big word we just wrote in */
int err = UnpackUint64( input, 9, 32, eByteOrder_MSBFirst, &result );
assert_int_equal( 0, err );
assert_int_equal( expected, result );
}
static void test_unpack_uint32_le_unaligned( void **state )
{
const uint8_t input[8] = { 0xFF, 0x7F, 0x56, 0x34, 0x12, 0xF0, 0xFF, 0xFF };
const uint64_t expected = 0x01234567;
uint64_t result;
(void) state; /* unused */
print_array_raw( input, 8 );
/* Unpack that big word we just wrote in */
int err = UnpackUint64( input, 12, 32, eByteOrder_LSBFirst, &result );
assert_int_equal( 0, err );
assert_int_equal( expected, result );
}
void print_array_raw( const uint8_t *bytes, const int len )
{
int idx;
int bitoffset;
printf( "Raw test array = " );
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");
}
}