summaryrefslogtreecommitdiff
path: root/src/microsoft/compiler/dxil_buffer_test.c
blob: 9c5b571cbf243f5295b7dca8044fcbcd1edf8ea9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 * Copyright © Microsoft Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

#include "dxil_buffer.h"
#include <assert.h>
#include <stdio.h>

static void
init()
{
   struct dxil_buffer buf;
   dxil_buffer_init(&buf, 2);
   assert(!buf.buf);
   assert(!buf.buf_bits);
}

static void
assert_blob_data(const struct dxil_buffer *m, const uint8_t *data,
                   size_t len)
{
   if (m->blob.size != len) {
      fprintf(stderr, "blob-size mismatch, expected %zd, got %zd",
                      len, m->blob.size);
      abort();
   }

   for (size_t i = 0; i < len; ++i) {
      if (m->blob.data[i] != data[i]) {
         fprintf(stderr, "blob-data mismatch at index %zd, "
                         "expected 0x%02x, got 0x%02x", i,
                         data[i], m->blob.data[i]);
         abort();
      }
   }
}

#define ASSERT_BLOB_DATA(m, data) \
   assert_blob_data(m, data, sizeof(data))

static void
align()
{
   struct dxil_buffer buf;
   dxil_buffer_init(&buf, 2);
   assert_blob_data(&buf, NULL, 0);

   dxil_buffer_init(&buf, 2);
   dxil_buffer_emit_bits(&buf, 0xbeef, 16);
   dxil_buffer_align(&buf);
   assert(!buf.buf);
   assert(!buf.buf_bits);
   uint8_t expected0[] = { 0xef, 0xbe, 0x00, 0x00 };
   ASSERT_BLOB_DATA(&buf, expected0);
   dxil_buffer_align(&buf);
   ASSERT_BLOB_DATA(&buf, expected0);
}

static void
emit_bits()
{
   struct dxil_buffer buf;
   dxil_buffer_init(&buf, 2);
   dxil_buffer_emit_bits(&buf, 0xbeef, 16);
   dxil_buffer_align(&buf);
   assert(!buf.buf);
   assert(!buf.buf_bits);
   uint8_t expected0[] = { 0xef, 0xbe, 0x00, 0x00 };
   ASSERT_BLOB_DATA(&buf, expected0);

   dxil_buffer_init(&buf, 2);
   dxil_buffer_emit_bits(&buf, 0xdead, 16);
   dxil_buffer_emit_bits(&buf, 0xbeef, 16);
   assert(!buf.buf);
   assert(!buf.buf_bits);
   uint8_t expected1[] = { 0xad, 0xde, 0xef, 0xbe };
   ASSERT_BLOB_DATA(&buf, expected1);

   dxil_buffer_init(&buf, 2);
   dxil_buffer_emit_bits(&buf, 0x1111111, 28);
   dxil_buffer_emit_bits(&buf, 0x22222222, 32);
   dxil_buffer_align(&buf);
   uint8_t expected2[] = { 0x11, 0x11, 0x11, 0x21, 0x22, 0x22, 0x22, 0x02 };
   ASSERT_BLOB_DATA(&buf, expected2);
}

static void
emit_vbr_bits()
{
   struct dxil_buffer buf;
   dxil_buffer_init(&buf, 2);
   dxil_buffer_emit_vbr_bits(&buf, 0x1a, 8);
   dxil_buffer_emit_vbr_bits(&buf, 0x1a, 6);
   dxil_buffer_emit_vbr_bits(&buf, 0x00, 2);
   dxil_buffer_emit_vbr_bits(&buf, 0x0a, 4);
   dxil_buffer_emit_vbr_bits(&buf, 0x04, 2);
   dxil_buffer_emit_vbr_bits(&buf, 0x00, 2);
   uint8_t expected[] = { 0x1a, 0x1a, 0x1a, 0x1a };
   ASSERT_BLOB_DATA(&buf, expected);
}

int
main()
{
   init();
   align();
   emit_bits();
   emit_vbr_bits();
   return 0;
}