#include "source/fuzz/comparator_deep_blocks_first.h"
#include "gtest/gtest.h"
#include "source/fuzz/fact_manager/fact_manager.h"
#include "source/fuzz/fuzzer_util.h"
#include "source/fuzz/pseudo_random_generator.h"
#include "source/fuzz/transformation_context.h"
#include "test/fuzz/fuzz_test_util.h"
namespace spvtools {
namespace fuzz {
namespace {
std::string shader = R"(
OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %2 "main"
OpExecutionMode %2 OriginUpperLeft
OpSource ESSL 310
%3 = OpTypeVoid
%4 = OpTypeFunction %3
%5 = OpTypeBool
%6 = OpConstantTrue %5
%7 = OpTypeInt 32 1
%8 = OpTypePointer Function %7
%9 = OpConstant %7 1
%10 = OpConstant %7 10
%11 = OpConstant %7 2
%2 = OpFunction %3 None %4
%12 = OpLabel
OpSelectionMerge %13 None
OpBranchConditional %6 %14 %15
%14 = OpLabel
OpBranch %13
%15 = OpLabel
OpBranch %16
%16 = OpLabel
OpLoopMerge %17 %18 None
OpBranch %19
%19 = OpLabel
OpBranchConditional %6 %20 %17
%20 = OpLabel
OpSelectionMerge %21 None
OpBranchConditional %6 %22 %23
%22 = OpLabel
OpBranch %21
%23 = OpLabel
OpBranch %21
%21 = OpLabel
OpBranch %18
%18 = OpLabel
OpBranch %16
%17 = OpLabel
OpBranch %13
%13 = OpLabel
OpReturn
OpFunctionEnd
)";
TEST(ComparatorDeepBlocksFirstTest, Compare) {
const auto env = SPV_ENV_UNIVERSAL_1_5;
const auto consumer = nullptr;
const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
spvtools::ValidatorOptions validator_options;
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
kConsoleMessageConsumer));
TransformationContext transformation_context(
MakeUnique<FactManager>(context.get()), validator_options);
auto is_deeper = ComparatorDeepBlocksFirst(context.get());
ASSERT_FALSE(is_deeper(12, 12));
ASSERT_FALSE(is_deeper(12, 13));
ASSERT_FALSE(is_deeper(12, 14));
ASSERT_FALSE(is_deeper(12, 18));
ASSERT_FALSE(is_deeper(12, 22));
ASSERT_TRUE(is_deeper(14, 12));
ASSERT_FALSE(is_deeper(14, 15));
ASSERT_FALSE(is_deeper(15, 14));
ASSERT_FALSE(is_deeper(14, 18));
ASSERT_TRUE(is_deeper(18, 12));
ASSERT_TRUE(is_deeper(18, 16));
}
TEST(ComparatorDeepBlocksFirstTest, Sort) {
const auto env = SPV_ENV_UNIVERSAL_1_5;
const auto consumer = nullptr;
const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
spvtools::ValidatorOptions validator_options;
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
kConsoleMessageConsumer));
TransformationContext transformation_context(
MakeUnique<FactManager>(context.get()), validator_options);
std::vector<opt::BasicBlock*> blocks = {context->get_instr_block(17),
context->get_instr_block(20),
context->get_instr_block(13)};
std::sort(blocks.begin(), blocks.end(),
ComparatorDeepBlocksFirst(context.get()));
ASSERT_EQ(blocks[0]->id(), 20);
ASSERT_EQ(blocks[1]->id(), 17);
ASSERT_EQ(blocks[2]->id(), 13);
}
} } }