• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "val_fixtures.h"
18 
19 #include <functional>
20 #include <tuple>
21 #include <utility>
22 
23 #include "test_fixture.h"
24 
25 namespace spvtest {
26 
27 template <typename T>
ValidateBase()28 ValidateBase<T>::ValidateBase() : binary_(), diagnostic_() {
29   // Initialize to default command line options. Different tests can then
30   // specialize specific options as necessary.
31   options_ = spvValidatorOptionsCreate();
32 }
33 
34 template <typename T>
get_const_binary()35 spv_const_binary ValidateBase<T>::get_const_binary() {
36   return spv_const_binary(binary_);
37 }
38 
39 template <typename T>
TearDown()40 void ValidateBase<T>::TearDown() {
41   if (diagnostic_) {
42     spvDiagnosticPrint(diagnostic_);
43   }
44   spvDiagnosticDestroy(diagnostic_);
45   spvBinaryDestroy(binary_);
46   spvValidatorOptionsDestroy(options_);
47 }
48 
49 template <typename T>
CompileSuccessfully(std::string code,spv_target_env env)50 void ValidateBase<T>::CompileSuccessfully(std::string code,
51                                           spv_target_env env) {
52   spv_diagnostic diagnostic = nullptr;
53   ASSERT_EQ(SPV_SUCCESS,
54             spvTextToBinary(ScopedContext(env).context, code.c_str(),
55                             code.size(), &binary_, &diagnostic))
56       << "ERROR: " << diagnostic->error
57       << "\nSPIR-V could not be compiled into binary:\n"
58       << code;
59 }
60 
61 template <typename T>
OverwriteAssembledBinary(uint32_t index,uint32_t word)62 void ValidateBase<T>::OverwriteAssembledBinary(uint32_t index, uint32_t word) {
63   ASSERT_TRUE(index < binary_->wordCount)
64       << "OverwriteAssembledBinary: The given index is larger than the binary "
65          "word count.";
66   binary_->code[index] = word;
67 }
68 
69 template <typename T>
ValidateInstructions(spv_target_env env)70 spv_result_t ValidateBase<T>::ValidateInstructions(spv_target_env env) {
71   return spvValidateWithOptions(ScopedContext(env).context, options_,
72                                 get_const_binary(), &diagnostic_);
73 }
74 
75 template <typename T>
ValidateAndRetrieveValidationState(spv_target_env env)76 spv_result_t ValidateBase<T>::ValidateAndRetrieveValidationState(
77     spv_target_env env) {
78   return spvtools::ValidateBinaryAndKeepValidationState(
79       ScopedContext(env).context, options_, get_const_binary()->code,
80       get_const_binary()->wordCount, &diagnostic_, &vstate_);
81 }
82 
83 template <typename T>
getDiagnosticString()84 std::string ValidateBase<T>::getDiagnosticString() {
85   return diagnostic_ == nullptr ?
86       std::string() : std::string(diagnostic_->error);
87 }
88 
89 template <typename T>
getValidatorOptions()90 spv_validator_options ValidateBase<T>::getValidatorOptions() {
91   return options_;
92 }
93 
94 template <typename T>
getErrorPosition()95 spv_position_t ValidateBase<T>::getErrorPosition() {
96   return diagnostic_ == nullptr ? spv_position_t() : diagnostic_->position;
97 }
98 
99 template class spvtest::ValidateBase<bool>;
100 template class spvtest::ValidateBase<int>;
101 template class spvtest::ValidateBase<std::string>;
102 template class spvtest::ValidateBase<std::pair<std::string, bool>>;
103 template class spvtest::ValidateBase<
104     std::tuple<std::string, std::pair<std::string, std::vector<std::string>>>>;
105 template class spvtest::ValidateBase<
106     std::tuple<int, std::tuple<std::string, std::function<spv_result_t(int)>,
107                                std::function<spv_result_t(int)>>>>;
108 template class spvtest::ValidateBase<SpvCapability>;
109 template class spvtest::ValidateBase<std::pair<std::string, std::string>>;
110 }
111