Regex fuzzer: identifier mutation

Mutates a WGSL-like string by replacing a randomly-selected identifier
with a different randomly-selected identifier.

Change-Id: Iecf45ad2800677cf3609b30d415520e5f2a05ba0
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/60561
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Alastair Donaldson <afdx@google.com>
Commit-Queue: Alastair Donaldson <afdx@google.com>
This commit is contained in:
egj
2021-08-04 07:12:20 +00:00
committed by Tint LUCI CQ
parent 51750f15d2
commit 98fbf241d8
3 changed files with 206 additions and 57 deletions

View File

@@ -25,13 +25,13 @@ namespace {
// Swaps two non-consecutive regions in the edge
TEST(SwapRegionsTest, SwapIntervalsEdgeNonConsecutive) {
std::string R1 = ";region1;", R2 = ";regionregion2",
std::string R1 = ";region1;", R2 = ";regionregion2;",
R3 = ";regionregionregion3;";
std::string all_regions = R1 + R2 + R3;
// this call should swap R1 with R3.
SwapIntervals(0, R1.length() - 1, R1.length() + R2.length(),
all_regions.length() - 1, all_regions);
SwapIntervals(0, R1.length(), R1.length() + R2.length(), R3.length(),
all_regions);
ASSERT_EQ(R3 + R2 + R1, all_regions);
}
@@ -44,15 +44,15 @@ TEST(SwapRegionsTest, SwapIntervalsNonConsecutiveNonEdge) {
std::string all_regions = R1 + R2 + R3 + R4 + R5;
// this call should swap R2 with R4.
SwapIntervals(R1.length(), R1.length() + R2.length() - 1,
R1.length() + R2.length() + R3.length(),
R1.length() + R2.length() + R3.length() + R4.length() - 1,
SwapIntervals(R1.length(), R2.length(),
R1.length() + R2.length() + R3.length(), R4.length(),
all_regions);
ASSERT_EQ(R1 + R4 + R3 + R2 + R5, all_regions);
}
// Swaps two consecutive regions not in the edge (sorrounded by other regions)
// Swaps two consecutive regions not in the edge (sorrounded by other
// regions)
TEST(SwapRegionsTest, SwapIntervalsConsecutiveEdge) {
std::string R1 = ";region1;", R2 = ";regionregion2;",
R3 = ";regionregionregion3;", R4 = ";regionregionregionregion4;",
@@ -60,9 +60,8 @@ TEST(SwapRegionsTest, SwapIntervalsConsecutiveEdge) {
std::string all_regions = R1 + R2 + R3 + R4;
// this call should swap R2 with R3.
SwapIntervals(R1.length(), R1.length() + R2.length() - 1,
R1.length() + R2.length(),
R1.length() + R2.length() + R3.length() - 1, all_regions);
SwapIntervals(R1.length(), R2.length(), R1.length() + R2.length(),
R3.length(), all_regions);
ASSERT_EQ(R1 + R3 + R2 + R4, all_regions);
}
@@ -76,12 +75,9 @@ TEST(SwapRegionsTest, SwapIntervalsConsecutiveNonEdge) {
std::string all_regions = R1 + R2 + R3 + R4 + R5;
// this call should swap R4 with R5.
SwapIntervals(
R1.length() + R2.length() + R3.length(),
R1.length() + R2.length() + R3.length() + R4.length() - 1,
R1.length() + R2.length() + R3.length() + R4.length(),
R1.length() + R2.length() + R3.length() + R4.length() + R5.length() - 1,
all_regions);
SwapIntervals(R1.length() + R2.length() + R3.length(), R4.length(),
R1.length() + R2.length() + R3.length() + R4.length(),
R5.length(), all_regions);
ASSERT_EQ(R1 + R2 + R3 + R5 + R4, all_regions);
}
@@ -94,7 +90,7 @@ TEST(DeleteRegionTest, DeleteFirstRegion) {
std::string all_regions = R1 + R2 + R3 + R4 + R5;
// This call should delete R1.
DeleteInterval(0, R1.length() - 1, all_regions);
DeleteInterval(0, R1.length(), all_regions);
ASSERT_EQ(";" + R2 + R3 + R4 + R5, all_regions);
}
@@ -108,7 +104,7 @@ TEST(DeleteRegionTest, DeleteLastRegion) {
// This call should delete R5.
DeleteInterval(R1.length() + R2.length() + R3.length() + R4.length(),
all_regions.length() - 1, all_regions);
R5.length(), all_regions);
ASSERT_EQ(R1 + R2 + R3 + R4 + ";", all_regions);
}
@@ -121,8 +117,7 @@ TEST(DeleteRegionTest, DeleteMiddleRegion) {
std::string all_regions = R1 + R2 + R3 + R4 + R5;
// This call should delete R3.
DeleteInterval(R1.length() + R2.length(),
R1.length() + R2.length() + R3.length() - 1, all_regions);
DeleteInterval(R1.length() + R2.length(), R3.length(), all_regions);
ASSERT_EQ(R1 + R2 + ";" + R4 + R5, all_regions);
}
@@ -134,7 +129,7 @@ TEST(InsertRegionTest, InsertRegionTest1) {
std::string all_regions = R1 + R2 + R3 + R4 + R5;
// This call should insert R2 after R4.
DuplicateInterval(R1.length(), R1.length() + R2.length() - 1,
DuplicateInterval(R1.length(), R2.length(),
R1.length() + R2.length() + R3.length() + R4.length() - 1,
all_regions);
@@ -149,9 +144,8 @@ TEST(InsertRegionTest, InsertRegionTest2) {
std::string all_regions = R1 + R2 + R3 + R4 + R5;
// This call should insert R3 after R1.
DuplicateInterval(R1.length() + R2.length(),
R1.length() + R2.length() + R3.length() - 1,
R1.length() - 1, all_regions);
DuplicateInterval(R1.length() + R2.length(), R3.length(), R1.length() - 1,
all_regions);
ASSERT_EQ(R1 + R3.substr(1, R3.length() - 1) + R2 + R3 + R4 + R5,
all_regions);
@@ -165,13 +159,73 @@ TEST(InsertRegionTest, InsertRegionTest3) {
std::string all_regions = R1 + R2 + R3 + R4 + R5;
// This call should insert R2 after R5.
DuplicateInterval(R1.length(), R1.length() + R2.length() - 1,
all_regions.length() - 1, all_regions);
DuplicateInterval(R1.length(), R2.length(), all_regions.length() - 1,
all_regions);
ASSERT_EQ(R1 + R2 + R3 + R4 + R5 + R2.substr(1, R2.length() - 1),
all_regions);
}
TEST(ReplaceIdentifierTest, ReplaceIdentifierTest1) {
std::string R1 = "|region1|", R2 = "; region2;",
R3 = "---------region3---------", R4 = "++region4++",
R5 = "***region5***";
std::string all_regions = R1 + R2 + R3 + R4 + R5;
// Replaces R3 with R1.
ReplaceRegion(0, R1.length(), R1.length() + R2.length(), R3.length(),
all_regions);
ASSERT_EQ(R1 + R2 + R1 + R4 + R5, all_regions);
}
TEST(ReplaceIdentifierTest, ReplaceIdentifierTest2) {
std::string R1 = "|region1|", R2 = "; region2;",
R3 = "---------region3---------", R4 = "++region4++",
R5 = "***region5***";
std::string all_regions = R1 + R2 + R3 + R4 + R5;
// Replaces R5 with R3.
ReplaceRegion(R1.length() + R2.length(), R3.length(),
R1.length() + R2.length() + R3.length() + R4.length(),
R5.length(), all_regions);
ASSERT_EQ(R1 + R2 + R3 + R4 + R3, all_regions);
}
TEST(GetIdentifierTest, GetIdentifierTest1) {
std::string wgsl_code =
"fn clamp_0acf8f() {"
"var res: vec2<f32> = clamp(vec2<f32>(), vec2<f32>(), vec2<f32>());}"
"[[stage(vertex)]]"
"fn vertex_main() -> [[builtin(position)]] vec4<f32> {"
" clamp_0acf8f();"
" return vec4<f32>();}"
"[[stage(fragment)]]"
"fn fragment_main() {"
" clamp_0acf8f();}"
"[[stage(compute), workgroup_size(1)]]"
"fn compute_main() {"
"var<private> foo: f32 = 0.0;"
" clamp_0acf8f();}";
std::vector<std::pair<size_t, size_t>> identifiers_pos =
GetIdentifiers(wgsl_code);
std::vector<std::pair<size_t, size_t>> ground_truth = {
std::make_pair(3, 12), std::make_pair(19, 3), std::make_pair(28, 4),
std::make_pair(40, 5), std::make_pair(51, 3), std::make_pair(59, 4),
std::make_pair(72, 4), std::make_pair(88, 5), std::make_pair(103, 2),
std::make_pair(113, 4), std::make_pair(125, 7), std::make_pair(145, 4),
std::make_pair(158, 12), std::make_pair(175, 6), std::make_pair(187, 3),
std::make_pair(197, 5), std::make_pair(214, 2), std::make_pair(226, 4),
std::make_pair(236, 12), std::make_pair(254, 5), std::make_pair(270, 14),
std::make_pair(289, 2), std::make_pair(300, 4), std::make_pair(308, 3),
std::make_pair(321, 3), std::make_pair(326, 3), std::make_pair(338, 12)};
ASSERT_EQ(ground_truth, identifiers_pos);
}
} // namespace
} // namespace regex_fuzzer
} // namespace fuzzers