1/*
2 * Copyright © Microsoft 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#include "dxil_buffer.h"
25#include <assert.h>
26#include <stdio.h>
27
28static void
29init()
30{
31   struct dxil_buffer buf;
32   dxil_buffer_init(&buf, 2);
33   assert(!buf.buf);
34   assert(!buf.buf_bits);
35}
36
37static void
38assert_blob_data(const struct dxil_buffer *m, const uint8_t *data,
39                   size_t len)
40{
41   if (m->blob.size != len) {
42      fprintf(stderr, "blob-size mismatch, expected %zd, got %zd",
43                      len, m->blob.size);
44      abort();
45   }
46
47   for (size_t i = 0; i < len; ++i) {
48      if (m->blob.data[i] != data[i]) {
49         fprintf(stderr, "blob-data mismatch at index %zd, "
50                         "expected 0x%02x, got 0x%02x", i,
51                         data[i], m->blob.data[i]);
52         abort();
53      }
54   }
55}
56
57#define ASSERT_BLOB_DATA(m, data) \
58   assert_blob_data(m, data, sizeof(data))
59
60static void
61align()
62{
63   struct dxil_buffer buf;
64   dxil_buffer_init(&buf, 2);
65   assert_blob_data(&buf, NULL, 0);
66
67   dxil_buffer_init(&buf, 2);
68   dxil_buffer_emit_bits(&buf, 0xbeef, 16);
69   dxil_buffer_align(&buf);
70   assert(!buf.buf);
71   assert(!buf.buf_bits);
72   uint8_t expected0[] = { 0xef, 0xbe, 0x00, 0x00 };
73   ASSERT_BLOB_DATA(&buf, expected0);
74   dxil_buffer_align(&buf);
75   ASSERT_BLOB_DATA(&buf, expected0);
76}
77
78static void
79emit_bits()
80{
81   struct dxil_buffer buf;
82   dxil_buffer_init(&buf, 2);
83   dxil_buffer_emit_bits(&buf, 0xbeef, 16);
84   dxil_buffer_align(&buf);
85   assert(!buf.buf);
86   assert(!buf.buf_bits);
87   uint8_t expected0[] = { 0xef, 0xbe, 0x00, 0x00 };
88   ASSERT_BLOB_DATA(&buf, expected0);
89
90   dxil_buffer_init(&buf, 2);
91   dxil_buffer_emit_bits(&buf, 0xdead, 16);
92   dxil_buffer_emit_bits(&buf, 0xbeef, 16);
93   assert(!buf.buf);
94   assert(!buf.buf_bits);
95   uint8_t expected1[] = { 0xad, 0xde, 0xef, 0xbe };
96   ASSERT_BLOB_DATA(&buf, expected1);
97
98   dxil_buffer_init(&buf, 2);
99   dxil_buffer_emit_bits(&buf, 0x1111111, 28);
100   dxil_buffer_emit_bits(&buf, 0x22222222, 32);
101   dxil_buffer_align(&buf);
102   uint8_t expected2[] = { 0x11, 0x11, 0x11, 0x21, 0x22, 0x22, 0x22, 0x02 };
103   ASSERT_BLOB_DATA(&buf, expected2);
104}
105
106static void
107emit_vbr_bits()
108{
109   struct dxil_buffer buf;
110   dxil_buffer_init(&buf, 2);
111   dxil_buffer_emit_vbr_bits(&buf, 0x1a, 8);
112   dxil_buffer_emit_vbr_bits(&buf, 0x1a, 6);
113   dxil_buffer_emit_vbr_bits(&buf, 0x00, 2);
114   dxil_buffer_emit_vbr_bits(&buf, 0x0a, 4);
115   dxil_buffer_emit_vbr_bits(&buf, 0x04, 2);
116   dxil_buffer_emit_vbr_bits(&buf, 0x00, 2);
117   uint8_t expected[] = { 0x1a, 0x1a, 0x1a, 0x1a };
118   ASSERT_BLOB_DATA(&buf, expected);
119}
120
121int
122main()
123{
124   init();
125   align();
126   emit_bits();
127   emit_vbr_bits();
128   return 0;
129}
130