• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2018 Google LLC
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 #ifndef TEST_REDUCE_REDUCE_TEST_UTIL_H_
16 #define TEST_REDUCE_REDUCE_TEST_UTIL_H_
17 
18 #include "gtest/gtest.h"
19 
20 #include "source/opt/ir_context.h"
21 #include "source/reduce/reduction_opportunity.h"
22 #include "spirv-tools/libspirv.h"
23 
24 namespace spvtools {
25 namespace reduce {
26 
27 // A helper class that subclasses a given reduction pass class in order to
28 // provide a wrapper for its protected methods.
29 template <class ReductionPassT>
30 class TestSubclass : public ReductionPassT {
31  public:
32   // Creates an instance of the reduction pass subclass with respect to target
33   // environment |env|.
TestSubclass(const spv_target_env env)34   explicit TestSubclass(const spv_target_env env) : ReductionPassT(env) {}
35   ~TestSubclass() = default;
36 
37   // A wrapper for GetAvailableOpportunities(...)
38   std::vector<std::unique_ptr<ReductionOpportunity>>
WrapGetAvailableOpportunities(opt::IRContext * context)39   WrapGetAvailableOpportunities(opt::IRContext* context) const {
40     return ReductionPassT::GetAvailableOpportunities(context);
41   }
42 };
43 
44 // Checks whether the given binaries are bit-wise equal.
45 void CheckEqual(spv_target_env env,
46                 const std::vector<uint32_t>& expected_binary,
47                 const std::vector<uint32_t>& actual_binary);
48 
49 // Assembles the given text and check whether the resulting binary is bit-wise
50 // equal to the given binary.
51 void CheckEqual(spv_target_env env, const std::string& expected_text,
52                 const std::vector<uint32_t>& actual_binary);
53 
54 // Assembles the given text and turns the given IR into binary, then checks
55 // whether the resulting binaries are bit-wise equal.
56 void CheckEqual(spv_target_env env, const std::string& expected_text,
57                 const opt::IRContext* actual_ir);
58 
59 // Assembles the given IR context and checks whether the resulting binary is
60 // valid.
61 void CheckValid(spv_target_env env, const opt::IRContext* ir);
62 
63 // Assembles the given IR context, then returns its disassembly as a string.
64 // Useful for debugging.
65 std::string ToString(spv_target_env env, const opt::IRContext* ir);
66 
67 // Assembly options for writing reduction tests.  It simplifies matters if
68 // numeric ids do not change.
69 const uint32_t kReduceAssembleOption =
70     SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS;
71 // Disassembly options for writing reduction tests.
72 const uint32_t kReduceDisassembleOption =
73     SPV_BINARY_TO_TEXT_OPTION_NO_HEADER | SPV_BINARY_TO_TEXT_OPTION_INDENT;
74 
75 // Don't print reducer info during testing.
76 void NopDiagnostic(spv_message_level_t /*level*/, const char* /*source*/,
77                    const spv_position_t& /*position*/, const char* /*message*/);
78 
79 }  // namespace reduce
80 }  // namespace spvtools
81 
82 #endif  // TEST_REDUCE_REDUCE_TEST_UTIL_H_
83