2 * Copyright © 2019 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
27 #include "gen_decoder.h"
29 static bool quiet
= false;
35 __attribute__((unused
)) static uint64_t
36 _test_combine_address(void *data
, void *location
,
37 struct test_address address
, uint32_t delta
)
39 return address
.offset
+ delta
;
42 #define __gen_user_data void
43 #define __gen_combine_address _test_combine_address
44 #define __gen_address_type struct test_address
46 #include "gentest_pack.h"
49 test_struct(struct gen_spec
*spec
) {
50 /* Fill struct fields and <group> tag */
51 struct GEN9_TEST_STRUCT test1
= {
56 for (int i
= 0; i
< 4; i
++) {
57 test1
.byte
[i
] = i
* 10 + 5;
60 /* Pack struct into a dw array */
61 uint32_t dw
[GEN9_TEST_STRUCT_length
];
62 GEN9_TEST_STRUCT_pack(NULL
, dw
, &test1
);
64 /* Now decode the packed struct, and make sure it matches the original */
65 struct gen_group
*group
;
66 group
= gen_spec_find_struct(spec
, "TEST_STRUCT");
68 assert(group
!= NULL
);
71 printf("\nTEST_STRUCT:\n");
72 gen_print_group(stdout
, group
, 0, dw
, 0, false);
75 struct gen_field_iterator iter
;
76 gen_field_iterator_init(&iter
, group
, dw
, 0, false);
78 while (gen_field_iterator_next(&iter
)) {
80 if (strcmp(iter
.name
, "number1") == 0) {
81 uint16_t number
= iter
.raw_value
;
82 assert(number
== test1
.number1
);
83 } else if (strcmp(iter
.name
, "number2") == 0) {
84 uint16_t number
= iter
.raw_value
;
85 assert(number
== test1
.number2
);
86 } else if (sscanf(iter
.name
, "byte[%d]", &idx
) == 1) {
87 uint8_t number
= iter
.raw_value
;
88 assert(number
== test1
.byte
[idx
]);
94 test_two_levels(struct gen_spec
*spec
) {
95 struct GEN9_STRUCT_TWO_LEVELS test
;
97 for (int i
= 0; i
< 4; i
++) {
98 for (int j
= 0; j
< 8; j
++) {
99 test
.byte
[i
][j
] = (i
* 10 + j
) % 256;
103 uint32_t dw
[GEN9_STRUCT_TWO_LEVELS_length
];
104 GEN9_STRUCT_TWO_LEVELS_pack(NULL
, dw
, &test
);
106 struct gen_group
*group
;
107 group
= gen_spec_find_struct(spec
, "STRUCT_TWO_LEVELS");
109 assert(group
!= NULL
);
112 printf("\nSTRUCT_TWO_LEVELS\n");
113 gen_print_group(stdout
, group
, 0, dw
, 0, false);
116 struct gen_field_iterator iter
;
117 gen_field_iterator_init(&iter
, group
, dw
, 0, false);
119 while (gen_field_iterator_next(&iter
)) {
122 assert(sscanf(iter
.name
, "byte[%d][%d]", &i
, &j
) == 2);
123 uint8_t number
= iter
.raw_value
;
124 assert(number
== test
.byte
[i
][j
]);
128 int main(int argc
, char **argv
)
130 struct gen_spec
*spec
= gen_spec_load_filename(GENXML_PATH
);
132 if (argc
> 1 && strcmp(argv
[1], "-quiet") == 0)
136 test_two_levels(spec
);