Path: blob/21.2-virgl/src/intel/common/tests/genxml_test.c
4547 views
/*1* Copyright © 2019 Intel Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223#undef NDEBUG2425#include <stdio.h>26#include <stdint.h>27#include <stdbool.h>28#include "intel_decoder.h"2930static bool quiet = false;3132struct test_address {33uint64_t offset;34};3536__attribute__((unused)) static uint64_t37_test_combine_address(void *data, void *location,38struct test_address address, uint32_t delta)39{40return address.offset + delta;41}4243#define __gen_user_data void44#define __gen_combine_address _test_combine_address45#define __gen_address_type struct test_address4647#include "gentest_pack.h"4849static void50test_struct(struct intel_spec *spec) {51/* Fill struct fields and <group> tag */52struct GFX9_TEST_STRUCT test1 = {53.number1 = 5,54.number2 = 1234,55};5657for (int i = 0; i < 4; i++) {58test1.byte[i] = i * 10 + 5;59}6061/* Pack struct into a dw array */62uint32_t dw[GFX9_TEST_STRUCT_length];63GFX9_TEST_STRUCT_pack(NULL, dw, &test1);6465/* Now decode the packed struct, and make sure it matches the original */66struct intel_group *group;67group = intel_spec_find_struct(spec, "TEST_STRUCT");6869assert(group != NULL);7071if (!quiet) {72printf("\nTEST_STRUCT:\n");73intel_print_group(stdout, group, 0, dw, 0, false);74}7576struct intel_field_iterator iter;77intel_field_iterator_init(&iter, group, dw, 0, false);7879while (intel_field_iterator_next(&iter)) {80int idx;81if (strcmp(iter.name, "number1") == 0) {82uint16_t number = iter.raw_value;83assert(number == test1.number1);84} else if (strcmp(iter.name, "number2") == 0) {85uint16_t number = iter.raw_value;86assert(number == test1.number2);87} else if (sscanf(iter.name, "byte[%d]", &idx) == 1) {88uint8_t number = iter.raw_value;89assert(number == test1.byte[idx]);90}91}92}9394static void95test_two_levels(struct intel_spec *spec) {96struct GFX9_STRUCT_TWO_LEVELS test;9798for (int i = 0; i < 4; i++) {99for (int j = 0; j < 8; j++) {100test.byte[i][j] = (i * 10 + j) % 256;101}102}103104uint32_t dw[GFX9_STRUCT_TWO_LEVELS_length];105GFX9_STRUCT_TWO_LEVELS_pack(NULL, dw, &test);106107struct intel_group *group;108group = intel_spec_find_struct(spec, "STRUCT_TWO_LEVELS");109110assert(group != NULL);111112if (!quiet) {113printf("\nSTRUCT_TWO_LEVELS\n");114intel_print_group(stdout, group, 0, dw, 0, false);115}116117struct intel_field_iterator iter;118intel_field_iterator_init(&iter, group, dw, 0, false);119120while (intel_field_iterator_next(&iter)) {121int i, j;122123assert(sscanf(iter.name, "byte[%d][%d]", &i, &j) == 2);124uint8_t number = iter.raw_value;125assert(number == test.byte[i][j]);126}127}128129int main(int argc, char **argv)130{131struct intel_spec *spec = intel_spec_load_filename(GENXML_PATH);132133if (argc > 1 && strcmp(argv[1], "-quiet") == 0)134quiet = true;135136test_struct(spec);137test_two_levels(spec);138139intel_spec_destroy(spec);140141return 0;142}143144145