17ec681f3Smrg/*
27ec681f3Smrg * Copyright © Microsoft Corporation
37ec681f3Smrg *
47ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a
57ec681f3Smrg * copy of this software and associated documentation files (the "Software"),
67ec681f3Smrg * to deal in the Software without restriction, including without limitation
77ec681f3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
87ec681f3Smrg * and/or sell copies of the Software, and to permit persons to whom the
97ec681f3Smrg * Software is furnished to do so, subject to the following conditions:
107ec681f3Smrg *
117ec681f3Smrg * The above copyright notice and this permission notice (including the next
127ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the
137ec681f3Smrg * Software.
147ec681f3Smrg *
157ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
167ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
177ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
187ec681f3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
197ec681f3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
207ec681f3Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
217ec681f3Smrg * IN THE SOFTWARE.
227ec681f3Smrg */
237ec681f3Smrg
247ec681f3Smrg#include "dxil_buffer.h"
257ec681f3Smrg#include <assert.h>
267ec681f3Smrg#include <stdio.h>
277ec681f3Smrg
287ec681f3Smrgstatic void
297ec681f3Smrginit()
307ec681f3Smrg{
317ec681f3Smrg   struct dxil_buffer buf;
327ec681f3Smrg   dxil_buffer_init(&buf, 2);
337ec681f3Smrg   assert(!buf.buf);
347ec681f3Smrg   assert(!buf.buf_bits);
357ec681f3Smrg}
367ec681f3Smrg
377ec681f3Smrgstatic void
387ec681f3Smrgassert_blob_data(const struct dxil_buffer *m, const uint8_t *data,
397ec681f3Smrg                   size_t len)
407ec681f3Smrg{
417ec681f3Smrg   if (m->blob.size != len) {
427ec681f3Smrg      fprintf(stderr, "blob-size mismatch, expected %zd, got %zd",
437ec681f3Smrg                      len, m->blob.size);
447ec681f3Smrg      abort();
457ec681f3Smrg   }
467ec681f3Smrg
477ec681f3Smrg   for (size_t i = 0; i < len; ++i) {
487ec681f3Smrg      if (m->blob.data[i] != data[i]) {
497ec681f3Smrg         fprintf(stderr, "blob-data mismatch at index %zd, "
507ec681f3Smrg                         "expected 0x%02x, got 0x%02x", i,
517ec681f3Smrg                         data[i], m->blob.data[i]);
527ec681f3Smrg         abort();
537ec681f3Smrg      }
547ec681f3Smrg   }
557ec681f3Smrg}
567ec681f3Smrg
577ec681f3Smrg#define ASSERT_BLOB_DATA(m, data) \
587ec681f3Smrg   assert_blob_data(m, data, sizeof(data))
597ec681f3Smrg
607ec681f3Smrgstatic void
617ec681f3Smrgalign()
627ec681f3Smrg{
637ec681f3Smrg   struct dxil_buffer buf;
647ec681f3Smrg   dxil_buffer_init(&buf, 2);
657ec681f3Smrg   assert_blob_data(&buf, NULL, 0);
667ec681f3Smrg
677ec681f3Smrg   dxil_buffer_init(&buf, 2);
687ec681f3Smrg   dxil_buffer_emit_bits(&buf, 0xbeef, 16);
697ec681f3Smrg   dxil_buffer_align(&buf);
707ec681f3Smrg   assert(!buf.buf);
717ec681f3Smrg   assert(!buf.buf_bits);
727ec681f3Smrg   uint8_t expected0[] = { 0xef, 0xbe, 0x00, 0x00 };
737ec681f3Smrg   ASSERT_BLOB_DATA(&buf, expected0);
747ec681f3Smrg   dxil_buffer_align(&buf);
757ec681f3Smrg   ASSERT_BLOB_DATA(&buf, expected0);
767ec681f3Smrg}
777ec681f3Smrg
787ec681f3Smrgstatic void
797ec681f3Smrgemit_bits()
807ec681f3Smrg{
817ec681f3Smrg   struct dxil_buffer buf;
827ec681f3Smrg   dxil_buffer_init(&buf, 2);
837ec681f3Smrg   dxil_buffer_emit_bits(&buf, 0xbeef, 16);
847ec681f3Smrg   dxil_buffer_align(&buf);
857ec681f3Smrg   assert(!buf.buf);
867ec681f3Smrg   assert(!buf.buf_bits);
877ec681f3Smrg   uint8_t expected0[] = { 0xef, 0xbe, 0x00, 0x00 };
887ec681f3Smrg   ASSERT_BLOB_DATA(&buf, expected0);
897ec681f3Smrg
907ec681f3Smrg   dxil_buffer_init(&buf, 2);
917ec681f3Smrg   dxil_buffer_emit_bits(&buf, 0xdead, 16);
927ec681f3Smrg   dxil_buffer_emit_bits(&buf, 0xbeef, 16);
937ec681f3Smrg   assert(!buf.buf);
947ec681f3Smrg   assert(!buf.buf_bits);
957ec681f3Smrg   uint8_t expected1[] = { 0xad, 0xde, 0xef, 0xbe };
967ec681f3Smrg   ASSERT_BLOB_DATA(&buf, expected1);
977ec681f3Smrg
987ec681f3Smrg   dxil_buffer_init(&buf, 2);
997ec681f3Smrg   dxil_buffer_emit_bits(&buf, 0x1111111, 28);
1007ec681f3Smrg   dxil_buffer_emit_bits(&buf, 0x22222222, 32);
1017ec681f3Smrg   dxil_buffer_align(&buf);
1027ec681f3Smrg   uint8_t expected2[] = { 0x11, 0x11, 0x11, 0x21, 0x22, 0x22, 0x22, 0x02 };
1037ec681f3Smrg   ASSERT_BLOB_DATA(&buf, expected2);
1047ec681f3Smrg}
1057ec681f3Smrg
1067ec681f3Smrgstatic void
1077ec681f3Smrgemit_vbr_bits()
1087ec681f3Smrg{
1097ec681f3Smrg   struct dxil_buffer buf;
1107ec681f3Smrg   dxil_buffer_init(&buf, 2);
1117ec681f3Smrg   dxil_buffer_emit_vbr_bits(&buf, 0x1a, 8);
1127ec681f3Smrg   dxil_buffer_emit_vbr_bits(&buf, 0x1a, 6);
1137ec681f3Smrg   dxil_buffer_emit_vbr_bits(&buf, 0x00, 2);
1147ec681f3Smrg   dxil_buffer_emit_vbr_bits(&buf, 0x0a, 4);
1157ec681f3Smrg   dxil_buffer_emit_vbr_bits(&buf, 0x04, 2);
1167ec681f3Smrg   dxil_buffer_emit_vbr_bits(&buf, 0x00, 2);
1177ec681f3Smrg   uint8_t expected[] = { 0x1a, 0x1a, 0x1a, 0x1a };
1187ec681f3Smrg   ASSERT_BLOB_DATA(&buf, expected);
1197ec681f3Smrg}
1207ec681f3Smrg
1217ec681f3Smrgint
1227ec681f3Smrgmain()
1237ec681f3Smrg{
1247ec681f3Smrg   init();
1257ec681f3Smrg   align();
1267ec681f3Smrg   emit_bits();
1277ec681f3Smrg   emit_vbr_bits();
1287ec681f3Smrg   return 0;
1297ec681f3Smrg}
130