1 // Copyright (c) 2015-2016 The Khronos Group Inc.
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 // Common validation fixtures for unit tests
16 
17 #ifndef TEST_VAL_VAL_FIXTURES_H_
18 #define TEST_VAL_VAL_FIXTURES_H_
19 
20 #include <memory>
21 #include <string>
22 
23 #include "source/val/validation_state.h"
24 #include "test/test_fixture.h"
25 #include "test/unit_spirv.h"
26 
27 namespace spvtest {
28 
29 template <typename T>
30 class ValidateBase : public ::testing::Test,
31                      public ::testing::WithParamInterface<T> {
32  public:
33   ValidateBase();
34 
35   virtual void TearDown();
36 
37   // Returns the a spv_const_binary struct
38   spv_const_binary get_const_binary();
39 
40   // Checks that 'code' is valid SPIR-V text representation and stores the
41   // binary version for further method calls.
42   void CompileSuccessfully(std::string code,
43                            spv_target_env env = SPV_ENV_UNIVERSAL_1_0);
44 
45   // Overwrites the word at index 'index' with the given word.
46   // For testing purposes, it is often useful to be able to manipulate the
47   // assembled binary before running the validator on it.
48   // This function overwrites the word at the given index with a new word.
49   void OverwriteAssembledBinary(uint32_t index, uint32_t word);
50 
51   // Performs validation on the SPIR-V code.
52   spv_result_t ValidateInstructions(spv_target_env env = SPV_ENV_UNIVERSAL_1_0);
53 
54   // Performs validation. Returns the status and stores validation state into
55   // the vstate_ member.
56   spv_result_t ValidateAndRetrieveValidationState(
57       spv_target_env env = SPV_ENV_UNIVERSAL_1_0);
58 
59   std::string getDiagnosticString();
60   spv_position_t getErrorPosition();
61   spv_validator_options getValidatorOptions();
62 
63   spv_binary binary_;
64   spv_diagnostic diagnostic_;
65   spv_validator_options options_;
66   std::unique_ptr<spvtools::val::ValidationState_t> vstate_;
67 };
68 
69 template <typename T>
ValidateBase()70 ValidateBase<T>::ValidateBase() : binary_(), diagnostic_() {
71   // Initialize to default command line options. Different tests can then
72   // specialize specific options as necessary.
73   options_ = spvValidatorOptionsCreate();
74 }
75 
76 template <typename T>
get_const_binary()77 spv_const_binary ValidateBase<T>::get_const_binary() {
78   return spv_const_binary(binary_);
79 }
80 
81 template <typename T>
TearDown()82 void ValidateBase<T>::TearDown() {
83   if (diagnostic_) {
84     spvDiagnosticPrint(diagnostic_);
85   }
86   spvDiagnosticDestroy(diagnostic_);
87   spvBinaryDestroy(binary_);
88   spvValidatorOptionsDestroy(options_);
89 }
90 
91 template <typename T>
CompileSuccessfully(std::string code,spv_target_env env)92 void ValidateBase<T>::CompileSuccessfully(std::string code,
93                                           spv_target_env env) {
94   spv_diagnostic diagnostic = nullptr;
95   ASSERT_EQ(SPV_SUCCESS,
96             spvTextToBinary(ScopedContext(env).context, code.c_str(),
97                             code.size(), &binary_, &diagnostic))
98       << "ERROR: " << diagnostic->error
99       << "\nSPIR-V could not be compiled into binary:\n"
100       << code;
101 }
102 
103 template <typename T>
OverwriteAssembledBinary(uint32_t index,uint32_t word)104 void ValidateBase<T>::OverwriteAssembledBinary(uint32_t index, uint32_t word) {
105   ASSERT_TRUE(index < binary_->wordCount)
106       << "OverwriteAssembledBinary: The given index is larger than the binary "
107          "word count.";
108   binary_->code[index] = word;
109 }
110 
111 template <typename T>
ValidateInstructions(spv_target_env env)112 spv_result_t ValidateBase<T>::ValidateInstructions(spv_target_env env) {
113   return spvValidateWithOptions(ScopedContext(env).context, options_,
114                                 get_const_binary(), &diagnostic_);
115 }
116 
117 template <typename T>
ValidateAndRetrieveValidationState(spv_target_env env)118 spv_result_t ValidateBase<T>::ValidateAndRetrieveValidationState(
119     spv_target_env env) {
120   return spvtools::val::ValidateBinaryAndKeepValidationState(
121       ScopedContext(env).context, options_, get_const_binary()->code,
122       get_const_binary()->wordCount, &diagnostic_, &vstate_);
123 }
124 
125 template <typename T>
getDiagnosticString()126 std::string ValidateBase<T>::getDiagnosticString() {
127   return diagnostic_ == nullptr ? std::string()
128                                 : std::string(diagnostic_->error);
129 }
130 
131 template <typename T>
getValidatorOptions()132 spv_validator_options ValidateBase<T>::getValidatorOptions() {
133   return options_;
134 }
135 
136 template <typename T>
getErrorPosition()137 spv_position_t ValidateBase<T>::getErrorPosition() {
138   return diagnostic_ == nullptr ? spv_position_t() : diagnostic_->position;
139 }
140 
141 }  // namespace spvtest
142 
143 #endif  // TEST_VAL_VAL_FIXTURES_H_
144