• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2019 Intel Corporation
3  *
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:
10  *
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
13  * Software.
14  *
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
21  * IN THE SOFTWARE.
22  */
23 
24 #undef NDEBUG
25 
26 #include <stdio.h>
27 #include <stdint.h>
28 #include <stdbool.h>
29 #include "gen_decoder.h"
30 
31 static bool quiet = false;
32 
33 struct test_address {
34    uint64_t offset;
35 };
36 
37 __attribute__((unused)) static uint64_t
_test_combine_address(void * data,void * location,struct test_address address,uint32_t delta)38 _test_combine_address(void *data, void *location,
39                       struct test_address address, uint32_t delta)
40 {
41    return address.offset + delta;
42 }
43 
44 #define __gen_user_data void
45 #define __gen_combine_address _test_combine_address
46 #define __gen_address_type struct test_address
47 
48 #include "gentest_pack.h"
49 
50 static void
test_struct(struct gen_spec * spec)51 test_struct(struct gen_spec *spec) {
52    /* Fill struct fields and <group> tag */
53    struct GEN9_TEST_STRUCT test1 = {
54       .number1 = 5,
55       .number2 = 1234,
56    };
57 
58    for (int i = 0; i < 4; i++) {
59       test1.byte[i] = i * 10 + 5;
60    }
61 
62    /* Pack struct into a dw array */
63    uint32_t dw[GEN9_TEST_STRUCT_length];
64    GEN9_TEST_STRUCT_pack(NULL, dw, &test1);
65 
66    /* Now decode the packed struct, and make sure it matches the original */
67    struct gen_group *group;
68    group = gen_spec_find_struct(spec, "TEST_STRUCT");
69 
70    assert(group != NULL);
71 
72    if (!quiet) {
73       printf("\nTEST_STRUCT:\n");
74       gen_print_group(stdout, group, 0, dw, 0, false);
75    }
76 
77    struct gen_field_iterator iter;
78    gen_field_iterator_init(&iter, group, dw, 0, false);
79 
80    while (gen_field_iterator_next(&iter)) {
81       int idx;
82       if (strcmp(iter.name, "number1") == 0) {
83          uint16_t number = iter.raw_value;
84          assert(number == test1.number1);
85       } else if (strcmp(iter.name, "number2") == 0) {
86          uint16_t number = iter.raw_value;
87          assert(number == test1.number2);
88       } else if (sscanf(iter.name, "byte[%d]", &idx) == 1) {
89          uint8_t number = iter.raw_value;
90          assert(number == test1.byte[idx]);
91       }
92    }
93 }
94 
95 static void
test_two_levels(struct gen_spec * spec)96 test_two_levels(struct gen_spec *spec) {
97    struct GEN9_STRUCT_TWO_LEVELS test;
98 
99    for (int i = 0; i < 4; i++) {
100       for (int j = 0; j < 8; j++) {
101          test.byte[i][j] = (i * 10 + j) % 256;
102       }
103    }
104 
105    uint32_t dw[GEN9_STRUCT_TWO_LEVELS_length];
106    GEN9_STRUCT_TWO_LEVELS_pack(NULL, dw, &test);
107 
108    struct gen_group *group;
109    group = gen_spec_find_struct(spec, "STRUCT_TWO_LEVELS");
110 
111    assert(group != NULL);
112 
113    if (!quiet) {
114       printf("\nSTRUCT_TWO_LEVELS\n");
115       gen_print_group(stdout, group, 0, dw, 0, false);
116    }
117 
118    struct gen_field_iterator iter;
119    gen_field_iterator_init(&iter, group, dw, 0, false);
120 
121    while (gen_field_iterator_next(&iter)) {
122       int i, j;
123 
124       assert(sscanf(iter.name, "byte[%d][%d]", &i, &j) == 2);
125       uint8_t number = iter.raw_value;
126       assert(number == test.byte[i][j]);
127    }
128 }
129 
main(int argc,char ** argv)130 int main(int argc, char **argv)
131 {
132    struct gen_spec *spec = gen_spec_load_filename(GENXML_PATH);
133 
134    if (argc > 1 && strcmp(argv[1], "-quiet") == 0)
135       quiet = true;
136 
137    test_struct(spec);
138    test_two_levels(spec);
139 
140    return 0;
141 }
142