blob: 94fe68fb560050eadd38c624aa3e1cc23bc01ec3 [file] [log] [blame]
egjcc6d5b42021-07-28 11:11:26 +00001// Copyright 2021 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 <string>
16
17#include "gtest/gtest.h"
18
19#include "fuzzers/tint_regex_fuzzer/wgsl_mutator.h"
20
21namespace tint {
22namespace fuzzers {
23namespace regex_fuzzer {
24namespace {
25
26// Swaps two non-consecutive regions in the edge
27TEST(SwapRegionsTest, SwapIntervalsEdgeNonConsecutive) {
egj98fbf242021-08-04 07:12:20 +000028 std::string R1 = ";region1;", R2 = ";regionregion2;",
egjb75e4b92021-08-02 20:56:39 +000029 R3 = ";regionregionregion3;";
egjcc6d5b42021-07-28 11:11:26 +000030 std::string all_regions = R1 + R2 + R3;
31
32 // this call should swap R1 with R3.
egj98fbf242021-08-04 07:12:20 +000033 SwapIntervals(0, R1.length(), R1.length() + R2.length(), R3.length(),
34 all_regions);
egjcc6d5b42021-07-28 11:11:26 +000035
36 ASSERT_EQ(R3 + R2 + R1, all_regions);
37}
38
39// Swaps two non-consecutive regions not in the edge
40TEST(SwapRegionsTest, SwapIntervalsNonConsecutiveNonEdge) {
egjb75e4b92021-08-02 20:56:39 +000041 std::string R1 = ";region1;", R2 = ";regionregion2;",
42 R3 = ";regionregionregion3;", R4 = ";regionregionregionregion4;",
43 R5 = ";regionregionregionregionregion5;";
egjcc6d5b42021-07-28 11:11:26 +000044 std::string all_regions = R1 + R2 + R3 + R4 + R5;
45
46 // this call should swap R2 with R4.
egj98fbf242021-08-04 07:12:20 +000047 SwapIntervals(R1.length(), R2.length(),
48 R1.length() + R2.length() + R3.length(), R4.length(),
egjf5490c72021-07-29 15:40:57 +000049 all_regions);
egjcc6d5b42021-07-28 11:11:26 +000050
51 ASSERT_EQ(R1 + R4 + R3 + R2 + R5, all_regions);
52}
53
egj98fbf242021-08-04 07:12:20 +000054// Swaps two consecutive regions not in the edge (sorrounded by other
55// regions)
egjcc6d5b42021-07-28 11:11:26 +000056TEST(SwapRegionsTest, SwapIntervalsConsecutiveEdge) {
egjb75e4b92021-08-02 20:56:39 +000057 std::string R1 = ";region1;", R2 = ";regionregion2;",
58 R3 = ";regionregionregion3;", R4 = ";regionregionregionregion4;",
59 R5 = ";regionregionregionregionregion5;";
egjcc6d5b42021-07-28 11:11:26 +000060 std::string all_regions = R1 + R2 + R3 + R4;
61
62 // this call should swap R2 with R3.
egj98fbf242021-08-04 07:12:20 +000063 SwapIntervals(R1.length(), R2.length(), R1.length() + R2.length(),
64 R3.length(), all_regions);
egjcc6d5b42021-07-28 11:11:26 +000065
66 ASSERT_EQ(R1 + R3 + R2 + R4, all_regions);
67}
68
69// Swaps two consecutive regions not in the edge (not sorrounded by other
70// regions)
71TEST(SwapRegionsTest, SwapIntervalsConsecutiveNonEdge) {
egjb75e4b92021-08-02 20:56:39 +000072 std::string R1 = ";region1;", R2 = ";regionregion2;",
73 R3 = ";regionregionregion3;", R4 = ";regionregionregionregion4;",
74 R5 = ";regionregionregionregionregion5;";
egjcc6d5b42021-07-28 11:11:26 +000075 std::string all_regions = R1 + R2 + R3 + R4 + R5;
76
77 // this call should swap R4 with R5.
egj98fbf242021-08-04 07:12:20 +000078 SwapIntervals(R1.length() + R2.length() + R3.length(), R4.length(),
79 R1.length() + R2.length() + R3.length() + R4.length(),
80 R5.length(), all_regions);
egjcc6d5b42021-07-28 11:11:26 +000081
82 ASSERT_EQ(R1 + R2 + R3 + R5 + R4, all_regions);
83}
84
egjf5490c72021-07-29 15:40:57 +000085// Deletes the first region.
86TEST(DeleteRegionTest, DeleteFirstRegion) {
egjb75e4b92021-08-02 20:56:39 +000087 std::string R1 = ";region1;", R2 = ";regionregion2;",
88 R3 = ";regionregionregion3;", R4 = ";regionregionregionregion4;",
89 R5 = ";regionregionregionregionregion5;";
egjf5490c72021-07-29 15:40:57 +000090 std::string all_regions = R1 + R2 + R3 + R4 + R5;
91
92 // This call should delete R1.
egj98fbf242021-08-04 07:12:20 +000093 DeleteInterval(0, R1.length(), all_regions);
egjf5490c72021-07-29 15:40:57 +000094
egjb75e4b92021-08-02 20:56:39 +000095 ASSERT_EQ(";" + R2 + R3 + R4 + R5, all_regions);
egjf5490c72021-07-29 15:40:57 +000096}
97
98// Deletes the last region.
99TEST(DeleteRegionTest, DeleteLastRegion) {
egjb75e4b92021-08-02 20:56:39 +0000100 std::string R1 = ";region1;", R2 = ";regionregion2;",
101 R3 = ";regionregionregion3;", R4 = ";regionregionregionregion4;",
102 R5 = ";regionregionregionregionregion5;";
egjf5490c72021-07-29 15:40:57 +0000103 std::string all_regions = R1 + R2 + R3 + R4 + R5;
104
105 // This call should delete R5.
106 DeleteInterval(R1.length() + R2.length() + R3.length() + R4.length(),
egj98fbf242021-08-04 07:12:20 +0000107 R5.length(), all_regions);
egjf5490c72021-07-29 15:40:57 +0000108
egjb75e4b92021-08-02 20:56:39 +0000109 ASSERT_EQ(R1 + R2 + R3 + R4 + ";", all_regions);
egjf5490c72021-07-29 15:40:57 +0000110}
111
112// Deletes the middle region.
113TEST(DeleteRegionTest, DeleteMiddleRegion) {
egjb75e4b92021-08-02 20:56:39 +0000114 std::string R1 = ";region1;", R2 = ";regionregion2;",
115 R3 = ";regionregionregion3;", R4 = ";regionregionregionregion4;",
116 R5 = ";regionregionregionregionregion5;";
egjf5490c72021-07-29 15:40:57 +0000117 std::string all_regions = R1 + R2 + R3 + R4 + R5;
118
119 // This call should delete R3.
egj98fbf242021-08-04 07:12:20 +0000120 DeleteInterval(R1.length() + R2.length(), R3.length(), all_regions);
egjf5490c72021-07-29 15:40:57 +0000121
egjb75e4b92021-08-02 20:56:39 +0000122 ASSERT_EQ(R1 + R2 + ";" + R4 + R5, all_regions);
egjf5490c72021-07-29 15:40:57 +0000123}
124
125TEST(InsertRegionTest, InsertRegionTest1) {
egjb75e4b92021-08-02 20:56:39 +0000126 std::string R1 = ";region1;", R2 = ";regionregion2;",
127 R3 = ";regionregionregion3;", R4 = ";regionregionregionregion4;",
128 R5 = ";regionregionregionregionregion5;";
egjf5490c72021-07-29 15:40:57 +0000129 std::string all_regions = R1 + R2 + R3 + R4 + R5;
130
131 // This call should insert R2 after R4.
egj98fbf242021-08-04 07:12:20 +0000132 DuplicateInterval(R1.length(), R2.length(),
egjf5490c72021-07-29 15:40:57 +0000133 R1.length() + R2.length() + R3.length() + R4.length() - 1,
134 all_regions);
135
egjb75e4b92021-08-02 20:56:39 +0000136 ASSERT_EQ(R1 + R2 + R3 + R4 + R2.substr(1, R2.size() - 1) + R5, all_regions);
egjf5490c72021-07-29 15:40:57 +0000137}
138
139TEST(InsertRegionTest, InsertRegionTest2) {
egjb75e4b92021-08-02 20:56:39 +0000140 std::string R1 = ";region1;", R2 = ";regionregion2;",
141 R3 = ";regionregionregion3;", R4 = ";regionregionregionregion4;",
142 R5 = ";regionregionregionregionregion5;";
143
egjf5490c72021-07-29 15:40:57 +0000144 std::string all_regions = R1 + R2 + R3 + R4 + R5;
145
146 // This call should insert R3 after R1.
egj98fbf242021-08-04 07:12:20 +0000147 DuplicateInterval(R1.length() + R2.length(), R3.length(), R1.length() - 1,
148 all_regions);
egjf5490c72021-07-29 15:40:57 +0000149
egjb75e4b92021-08-02 20:56:39 +0000150 ASSERT_EQ(R1 + R3.substr(1, R3.length() - 1) + R2 + R3 + R4 + R5,
151 all_regions);
egjf5490c72021-07-29 15:40:57 +0000152}
153
154TEST(InsertRegionTest, InsertRegionTest3) {
egjb75e4b92021-08-02 20:56:39 +0000155 std::string R1 = ";region1;", R2 = ";regionregion2;",
156 R3 = ";regionregionregion3;", R4 = ";regionregionregionregion4;",
157 R5 = ";regionregionregionregionregion5;";
158
egjf5490c72021-07-29 15:40:57 +0000159 std::string all_regions = R1 + R2 + R3 + R4 + R5;
160
161 // This call should insert R2 after R5.
egj98fbf242021-08-04 07:12:20 +0000162 DuplicateInterval(R1.length(), R2.length(), all_regions.length() - 1,
163 all_regions);
egjf5490c72021-07-29 15:40:57 +0000164
egjb75e4b92021-08-02 20:56:39 +0000165 ASSERT_EQ(R1 + R2 + R3 + R4 + R5 + R2.substr(1, R2.length() - 1),
166 all_regions);
egjf5490c72021-07-29 15:40:57 +0000167}
168
egj98fbf242021-08-04 07:12:20 +0000169TEST(ReplaceIdentifierTest, ReplaceIdentifierTest1) {
170 std::string R1 = "|region1|", R2 = "; region2;",
171 R3 = "---------region3---------", R4 = "++region4++",
172 R5 = "***region5***";
173 std::string all_regions = R1 + R2 + R3 + R4 + R5;
174
175 // Replaces R3 with R1.
176 ReplaceRegion(0, R1.length(), R1.length() + R2.length(), R3.length(),
177 all_regions);
178
179 ASSERT_EQ(R1 + R2 + R1 + R4 + R5, all_regions);
180}
181
182TEST(ReplaceIdentifierTest, ReplaceIdentifierTest2) {
183 std::string R1 = "|region1|", R2 = "; region2;",
184 R3 = "---------region3---------", R4 = "++region4++",
185 R5 = "***region5***";
186 std::string all_regions = R1 + R2 + R3 + R4 + R5;
187
188 // Replaces R5 with R3.
189 ReplaceRegion(R1.length() + R2.length(), R3.length(),
190 R1.length() + R2.length() + R3.length() + R4.length(),
191 R5.length(), all_regions);
192
193 ASSERT_EQ(R1 + R2 + R3 + R4 + R3, all_regions);
194}
195
196TEST(GetIdentifierTest, GetIdentifierTest1) {
197 std::string wgsl_code =
198 "fn clamp_0acf8f() {"
199 "var res: vec2<f32> = clamp(vec2<f32>(), vec2<f32>(), vec2<f32>());}"
200 "[[stage(vertex)]]"
201 "fn vertex_main() -> [[builtin(position)]] vec4<f32> {"
202 " clamp_0acf8f();"
203 " return vec4<f32>();}"
204 "[[stage(fragment)]]"
205 "fn fragment_main() {"
206 " clamp_0acf8f();}"
207 "[[stage(compute), workgroup_size(1)]]"
208 "fn compute_main() {"
209 "var<private> foo: f32 = 0.0;"
210 " clamp_0acf8f();}";
211
212 std::vector<std::pair<size_t, size_t>> identifiers_pos =
213 GetIdentifiers(wgsl_code);
214
215 std::vector<std::pair<size_t, size_t>> ground_truth = {
216 std::make_pair(3, 12), std::make_pair(19, 3), std::make_pair(28, 4),
217 std::make_pair(40, 5), std::make_pair(51, 3), std::make_pair(59, 4),
218 std::make_pair(72, 4), std::make_pair(88, 5), std::make_pair(103, 2),
219 std::make_pair(113, 4), std::make_pair(125, 7), std::make_pair(145, 4),
220 std::make_pair(158, 12), std::make_pair(175, 6), std::make_pair(187, 3),
221 std::make_pair(197, 5), std::make_pair(214, 2), std::make_pair(226, 4),
222 std::make_pair(236, 12), std::make_pair(254, 5), std::make_pair(270, 14),
223 std::make_pair(289, 2), std::make_pair(300, 4), std::make_pair(308, 3),
224 std::make_pair(321, 3), std::make_pair(326, 3), std::make_pair(338, 12)};
225
226 ASSERT_EQ(ground_truth, identifiers_pos);
227}
228
egj608738d2021-08-05 22:24:20 +0000229TEST(TestGetLiteralsValues, TestGetLiteralsValues1) {
230 std::string wgsl_code =
231 "fn clamp_0acf8f() {"
232 "var res: vec2<f32> = clamp(vec2<f32>(), vec2<f32>(), vec2<f32>());}"
233 "[[stage(vertex)]]"
234 "fn vertex_main() -> [[builtin(position)]] vec4<f32> {"
235 " clamp_0acf8f();"
236 "var foo_1: i32 = 3;"
237 " return vec4<f32>();}"
238 "[[stage(fragment)]]"
239 "fn fragment_main() {"
240 " clamp_0acf8f();}"
241 "[[stage(compute), workgroup_size(1)]]"
242 "fn compute_main() {"
243 "var<private> foo: f32 = 0.0;"
244 "var foo_2: i32 = 10;"
245 " clamp_0acf8f();}"
246 "foo_1 = 5 + 7;"
247 "var foo_3 : i32 = -20;";
248
249 std::vector<std::pair<size_t, size_t>> literals_pos =
250 GetIntLiterals(wgsl_code);
251
252 std::vector<std::string> ground_truth = {"3", "10", "5", "7", "-20"};
253
254 std::vector<std::string> result;
255
256 for (auto pos : literals_pos) {
257 result.push_back(wgsl_code.substr(pos.first, pos.second));
258 }
259
260 ASSERT_EQ(ground_truth, result);
261}
262
egjcc6d5b42021-07-28 11:11:26 +0000263} // namespace
264} // namespace regex_fuzzer
265} // namespace fuzzers
266} // namespace tint