Path: blob/21.2-virgl/src/microsoft/compiler/dxil_buffer_test.c
4564 views
/*1* Copyright © Microsoft 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#include "dxil_buffer.h"24#include <assert.h>25#include <stdio.h>2627static void28init()29{30struct dxil_buffer buf;31dxil_buffer_init(&buf, 2);32assert(!buf.buf);33assert(!buf.buf_bits);34}3536static void37assert_blob_data(const struct dxil_buffer *m, const uint8_t *data,38size_t len)39{40if (m->blob.size != len) {41fprintf(stderr, "blob-size mismatch, expected %zd, got %zd",42len, m->blob.size);43abort();44}4546for (size_t i = 0; i < len; ++i) {47if (m->blob.data[i] != data[i]) {48fprintf(stderr, "blob-data mismatch at index %zd, "49"expected 0x%02x, got 0x%02x", i,50data[i], m->blob.data[i]);51abort();52}53}54}5556#define ASSERT_BLOB_DATA(m, data) \57assert_blob_data(m, data, sizeof(data))5859static void60align()61{62struct dxil_buffer buf;63dxil_buffer_init(&buf, 2);64assert_blob_data(&buf, NULL, 0);6566dxil_buffer_init(&buf, 2);67dxil_buffer_emit_bits(&buf, 0xbeef, 16);68dxil_buffer_align(&buf);69assert(!buf.buf);70assert(!buf.buf_bits);71uint8_t expected0[] = { 0xef, 0xbe, 0x00, 0x00 };72ASSERT_BLOB_DATA(&buf, expected0);73dxil_buffer_align(&buf);74ASSERT_BLOB_DATA(&buf, expected0);75}7677static void78emit_bits()79{80struct dxil_buffer buf;81dxil_buffer_init(&buf, 2);82dxil_buffer_emit_bits(&buf, 0xbeef, 16);83dxil_buffer_align(&buf);84assert(!buf.buf);85assert(!buf.buf_bits);86uint8_t expected0[] = { 0xef, 0xbe, 0x00, 0x00 };87ASSERT_BLOB_DATA(&buf, expected0);8889dxil_buffer_init(&buf, 2);90dxil_buffer_emit_bits(&buf, 0xdead, 16);91dxil_buffer_emit_bits(&buf, 0xbeef, 16);92assert(!buf.buf);93assert(!buf.buf_bits);94uint8_t expected1[] = { 0xad, 0xde, 0xef, 0xbe };95ASSERT_BLOB_DATA(&buf, expected1);9697dxil_buffer_init(&buf, 2);98dxil_buffer_emit_bits(&buf, 0x1111111, 28);99dxil_buffer_emit_bits(&buf, 0x22222222, 32);100dxil_buffer_align(&buf);101uint8_t expected2[] = { 0x11, 0x11, 0x11, 0x21, 0x22, 0x22, 0x22, 0x02 };102ASSERT_BLOB_DATA(&buf, expected2);103}104105static void106emit_vbr_bits()107{108struct dxil_buffer buf;109dxil_buffer_init(&buf, 2);110dxil_buffer_emit_vbr_bits(&buf, 0x1a, 8);111dxil_buffer_emit_vbr_bits(&buf, 0x1a, 6);112dxil_buffer_emit_vbr_bits(&buf, 0x00, 2);113dxil_buffer_emit_vbr_bits(&buf, 0x0a, 4);114dxil_buffer_emit_vbr_bits(&buf, 0x04, 2);115dxil_buffer_emit_vbr_bits(&buf, 0x00, 2);116uint8_t expected[] = { 0x1a, 0x1a, 0x1a, 0x1a };117ASSERT_BLOB_DATA(&buf, expected);118}119120int121main()122{123init();124align();125emit_bits();126emit_vbr_bits();127return 0;128}129130131