1 //
2 // Copyright (c) 2020 The Khronos Group Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 #include "propertyHelpers.h"
17 #include "errorHelpers.h"
18
19 #include <assert.h>
20
21 #include <algorithm>
22 #include <vector>
23
findProperty(const std::vector<cl_properties> & props,cl_properties prop,cl_properties & value)24 static bool findProperty(const std::vector<cl_properties>& props,
25 cl_properties prop, cl_properties& value)
26 {
27 // This function assumes properties are valid:
28 assert(props.size() == 0 || props.back() == 0);
29 assert(props.size() == 0 || props.size() % 2 == 1);
30
31 for (cl_uint i = 0; i < props.size(); i = i + 2)
32 {
33 cl_properties check_prop = props[i];
34
35 if (check_prop == 0)
36 {
37 break;
38 }
39
40 if (check_prop == prop)
41 {
42 value = props[i + 1];
43 return true;
44 }
45 }
46
47 return false;
48 }
49
compareProperties(const std::vector<cl_properties> & queried,const std::vector<cl_properties> & check)50 int compareProperties(const std::vector<cl_properties>& queried,
51 const std::vector<cl_properties>& check)
52 {
53 if (queried.size() != 0)
54 {
55 if (queried.back() != 0)
56 {
57 log_error("ERROR: queried properties do not end with 0!\n");
58 return TEST_FAIL;
59 }
60 if (queried.size() % 2 != 1)
61 {
62 log_error("ERROR: queried properties does not consist of "
63 "property-value pairs!\n");
64 return TEST_FAIL;
65 }
66 }
67 if (check.size() != 0)
68 {
69 if (check.back() != 0)
70 {
71 log_error("ERROR: check properties do not end with 0!\n");
72 return TEST_FAIL;
73 }
74 if (check.size() % 2 != 1)
75 {
76 log_error("ERROR: check properties does not consist of "
77 "property-value pairs!\n");
78 return TEST_FAIL;
79 }
80 }
81
82 if (queried != check)
83 {
84 for (cl_uint i = 0; i < check.size(); i = i + 2)
85 {
86 cl_properties check_prop = check[i];
87
88 if (check_prop == 0)
89 {
90 break;
91 }
92
93 cl_properties check_value = check[i + 1];
94 cl_properties queried_value = 0;
95
96 bool found = findProperty(queried, check_prop, queried_value);
97
98 if (!found)
99 {
100 log_error("ERROR: expected property 0x%x not found!\n",
101 check_prop);
102 return TEST_FAIL;
103 }
104 else if (check_value != queried_value)
105 {
106 log_error("ERROR: mis-matched value for property 0x%x: wanted "
107 "0x%x, got 0x%x\n",
108 check_prop, check_value, queried_value);
109 return TEST_FAIL;
110 }
111 }
112
113 if (queried.size() > check.size())
114 {
115 log_error("ERROR: all properties found but there are extra "
116 "properties: expected %d, got %d.\n",
117 check.size(), queried.size());
118 return TEST_FAIL;
119 }
120
121 log_error("ERROR: properties were returned in the wrong order.\n");
122 return TEST_FAIL;
123 }
124
125 return TEST_PASS;
126 }
127