Debian Codegraph @

crt/aws-c-common/tests/byte_order_test.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/common/byte_order.h>
7
8 #include <aws/testing/aws_test_harness.h>
9
10 #ifdef _MSC_VER
11 # pragma warning(disable : 4324) /* structure was padded due to alignment specifier */
12 #endif
13
14 static int s_byte_swap_test_fn(struct aws_allocator *allocator, void *ctx) {
15 (void)allocator;
16 (void)ctx;
17
18 uint64_t ans_x = 0x1122334455667788ULL;
19 uint32_t ans_y = 0xaabbccdd;
20 uint16_t ans_w = 0xeeff;
21
22 uint8_t x[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88};
23 uint8_t y[] = {0xaa, 0xbb, 0xcc, 0xdd};
24 uint8_t w[] = {0xee, 0xff};
25
26 uint64_t x64;
27 uint32_t y32;
28 uint16_t w16;
29
30 memcpy(&x64, x, sizeof(x));
31 memcpy(&y32, y, sizeof(y));
32 memcpy(&w16, w, sizeof(w));
33
34 ASSERT_UINT_EQUALS(aws_ntoh64(x64), ans_x);
35 ASSERT_UINT_EQUALS(aws_hton64(x64), ans_x);
36
37 ASSERT_UINT_EQUALS(aws_ntoh32(y32), ans_y);
38 ASSERT_UINT_EQUALS(aws_hton32(y32), ans_y);
39
40 ASSERT_UINT_EQUALS(aws_ntoh16(w16), ans_w);
41 ASSERT_UINT_EQUALS(aws_hton16(w16), ans_w);
42
43 return 0;
44 }
45 AWS_TEST_CASE(byte_swap_test, s_byte_swap_test_fn);
46
47 AWS_ALIGNED_TYPEDEF(uint8_t, aligned32_storage[64], 32);
48
49 struct padding32_disaster {
50 uint8_t dumb;
51 aligned32_storage b;
52 };
53
54 static int s_alignment32_test_fn(struct aws_allocator *allocator, void *ctx) {
55 (void)allocator;
56 (void)ctx;
57
58 size_t spacing = offsetof(struct padding32_disaster, b) - offsetof(struct padding32_disaster, dumb);
59 ASSERT_UINT_EQUALS(0, spacing % 32);
60
61 return 0;
62 }
63 AWS_TEST_CASE(alignment32_test, s_alignment32_test_fn)
64
65 AWS_ALIGNED_TYPEDEF(uint8_t, aligned16_storage[64], 16);
66
67 struct padding16_disaster {
68 uint8_t dumb;
69 aligned16_storage b;
70 };
71
72 static int s_alignment16_test_fn(struct aws_allocator *allocator, void *ctx) {
73 (void)allocator;
74 (void)ctx;
75
76 size_t spacing = offsetof(struct padding32_disaster, b) - offsetof(struct padding32_disaster, dumb);
77 ASSERT_UINT_EQUALS(0, spacing % 16);
78
79 return 0;
80 }
81 AWS_TEST_CASE(alignment16_test, s_alignment16_test_fn)