1 // Copyright 2020 The Tint Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "src/writer/spirv/test_helper.h"
16
17 namespace tint {
18 namespace writer {
19 namespace spirv {
20 namespace {
21
22 using BinaryWriterTest = TestHelper;
23
TEST_F(BinaryWriterTest,Preamble)24 TEST_F(BinaryWriterTest, Preamble) {
25 BinaryWriter bw;
26 bw.WriteHeader(5);
27
28 auto res = bw.result();
29 ASSERT_EQ(res.size(), 5u);
30 EXPECT_EQ(res[0], spv::MagicNumber);
31 EXPECT_EQ(res[1], 0x00010300u); // SPIR-V 1.3
32 EXPECT_EQ(res[2], 23u << 16); // Generator ID
33 EXPECT_EQ(res[3], 5u); // ID Bound
34 EXPECT_EQ(res[4], 0u); // Reserved
35 }
36
TEST_F(BinaryWriterTest,Float)37 TEST_F(BinaryWriterTest, Float) {
38 spirv::Builder& b = Build();
39
40 b.push_annot(spv::Op::OpKill, {Operand::Float(2.4f)});
41 BinaryWriter bw;
42 bw.WriteBuilder(&b);
43
44 auto res = bw.result();
45 ASSERT_EQ(res.size(), 2u);
46 float f;
47 memcpy(&f, res.data() + 1, 4);
48 EXPECT_EQ(f, 2.4f);
49 }
50
TEST_F(BinaryWriterTest,Int)51 TEST_F(BinaryWriterTest, Int) {
52 spirv::Builder& b = Build();
53
54 b.push_annot(spv::Op::OpKill, {Operand::Int(2)});
55 BinaryWriter bw;
56 bw.WriteBuilder(&b);
57
58 auto res = bw.result();
59 ASSERT_EQ(res.size(), 2u);
60 EXPECT_EQ(res[1], 2u);
61 }
62
TEST_F(BinaryWriterTest,String)63 TEST_F(BinaryWriterTest, String) {
64 spirv::Builder& b = Build();
65
66 b.push_annot(spv::Op::OpKill, {Operand::String("my_string")});
67 BinaryWriter bw;
68 bw.WriteBuilder(&b);
69
70 auto res = bw.result();
71 ASSERT_EQ(res.size(), 4u);
72
73 uint8_t* v = reinterpret_cast<uint8_t*>(res.data() + 1);
74 EXPECT_EQ(v[0], 'm');
75 EXPECT_EQ(v[1], 'y');
76 EXPECT_EQ(v[2], '_');
77 EXPECT_EQ(v[3], 's');
78 EXPECT_EQ(v[4], 't');
79 EXPECT_EQ(v[5], 'r');
80 EXPECT_EQ(v[6], 'i');
81 EXPECT_EQ(v[7], 'n');
82 EXPECT_EQ(v[8], 'g');
83 EXPECT_EQ(v[9], '\0');
84 EXPECT_EQ(v[10], '\0');
85 EXPECT_EQ(v[11], '\0');
86 }
87
TEST_F(BinaryWriterTest,String_Multiple4Length)88 TEST_F(BinaryWriterTest, String_Multiple4Length) {
89 spirv::Builder& b = Build();
90
91 b.push_annot(spv::Op::OpKill, {Operand::String("mystring")});
92 BinaryWriter bw;
93 bw.WriteBuilder(&b);
94
95 auto res = bw.result();
96 ASSERT_EQ(res.size(), 4u);
97
98 uint8_t* v = reinterpret_cast<uint8_t*>(res.data() + 1);
99 EXPECT_EQ(v[0], 'm');
100 EXPECT_EQ(v[1], 'y');
101 EXPECT_EQ(v[2], 's');
102 EXPECT_EQ(v[3], 't');
103 EXPECT_EQ(v[4], 'r');
104 EXPECT_EQ(v[5], 'i');
105 EXPECT_EQ(v[6], 'n');
106 EXPECT_EQ(v[7], 'g');
107 EXPECT_EQ(v[8], '\0');
108 EXPECT_EQ(v[9], '\0');
109 EXPECT_EQ(v[10], '\0');
110 EXPECT_EQ(v[11], '\0');
111 }
112
TEST_F(BinaryWriterTest,TestInstructionWriter)113 TEST_F(BinaryWriterTest, TestInstructionWriter) {
114 Instruction i1{spv::Op::OpKill, {Operand::Int(2)}};
115 Instruction i2{spv::Op::OpKill, {Operand::Int(4)}};
116
117 BinaryWriter bw;
118 bw.WriteInstruction(i1);
119 bw.WriteInstruction(i2);
120
121 auto res = bw.result();
122 ASSERT_EQ(res.size(), 4u);
123 EXPECT_EQ(res[1], 2u);
124 EXPECT_EQ(res[3], 4u);
125 }
126
127 } // namespace
128 } // namespace spirv
129 } // namespace writer
130 } // namespace tint
131