Imported Upstream version 0.8.4
[platform/upstream/multipath-tools.git] / tests / unaligned.c
1 #include <inttypes.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <stdbool.h>
5 #include <stdarg.h>
6 #include <stddef.h>
7 #include <setjmp.h>
8 #include <stdlib.h>
9 #include <cmocka.h>
10 #include "unaligned.h"
11
12 #define SIZE 16
13 static const char memory[8] = {
14         0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef
15 };
16
17 static const uint64_t intval64 = 0x0123456789abcdef;
18 static const uint32_t intval32 = 0x01234567;
19 static const uint16_t intval16 = 0x0123;
20
21 #include "globals.c"
22
23 static int setup(void **state)
24 {
25         return posix_memalign(state, 16, 2 * SIZE);
26 }
27
28 static int teardown(void **state)
29 {
30         free(*state);
31         return 0;
32 }
33
34
35 #define make_test(bits, offset) \
36         static void test_ ## bits ## _ ## offset(void **state)  \
37 {                                                               \
38         int len = bits/8;                                       \
39         uint8_t *c = *state;                                    \
40         uint8_t *p = *state + SIZE;                             \
41         uint64_t u;                                             \
42                                                                 \
43         assert_in_range(len, 1, SIZE);                          \
44         assert_in_range(offset + len, 1, SIZE);                 \
45         memset(c, 0, 2 * SIZE);                                 \
46         memcpy(c + offset, memory, len);                        \
47                                                                 \
48         u = get_unaligned_be##bits(c + offset);                 \
49         assert_int_equal(u, intval##bits);                      \
50         put_unaligned_be##bits(u, p + offset);                  \
51         assert_memory_equal(c + offset, p  + offset, len);      \
52 }
53
54 make_test(16, 0);
55 make_test(16, 1);
56 make_test(32, 0);
57 make_test(32, 1);
58 make_test(32, 2);
59 make_test(32, 3);
60 make_test(64, 0);
61 make_test(64, 1);
62 make_test(64, 2);
63 make_test(64, 3);
64 make_test(64, 4);
65 make_test(64, 5);
66 make_test(64, 6);
67 make_test(64, 7);
68
69 int test_unaligned(void)
70 {
71         const struct CMUnitTest tests[] = {
72                 cmocka_unit_test(test_16_0),
73                 cmocka_unit_test(test_16_1),
74                 cmocka_unit_test(test_32_0),
75                 cmocka_unit_test(test_32_1),
76                 cmocka_unit_test(test_32_2),
77                 cmocka_unit_test(test_32_3),
78                 cmocka_unit_test(test_64_0),
79                 cmocka_unit_test(test_64_1),
80                 cmocka_unit_test(test_64_2),
81                 cmocka_unit_test(test_64_3),
82                 cmocka_unit_test(test_64_4),
83                 cmocka_unit_test(test_64_5),
84                 cmocka_unit_test(test_64_6),
85                 cmocka_unit_test(test_64_7),
86         };
87         return cmocka_run_group_tests(tests, setup, teardown);
88 }
89
90 int main(void)
91 {
92         int ret = 0;
93
94         ret += test_unaligned();
95         return ret;
96 }