xref: /aosp_15_r20/external/OpenCL-CTS/test_common/harness/propertyHelpers.cpp (revision 6467f958c7de8070b317fc65bcb0f6472e388d82)
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 <cinttypes>
23 #include <vector>
24 
findProperty(const std::vector<cl_properties> & props,cl_properties prop,cl_properties & value)25 static bool findProperty(const std::vector<cl_properties>& props,
26                          cl_properties prop, cl_properties& value)
27 {
28     // This function assumes properties are valid:
29     assert(props.size() == 0 || props.back() == 0);
30     assert(props.size() == 0 || props.size() % 2 == 1);
31 
32     for (cl_uint i = 0; i < props.size(); i = i + 2)
33     {
34         cl_properties check_prop = props[i];
35 
36         if (check_prop == 0)
37         {
38             break;
39         }
40 
41         if (check_prop == prop)
42         {
43             value = props[i + 1];
44             return true;
45         }
46     }
47 
48     return false;
49 }
50 
compareProperties(const std::vector<cl_properties> & queried,const std::vector<cl_properties> & check)51 int compareProperties(const std::vector<cl_properties>& queried,
52                       const std::vector<cl_properties>& check)
53 {
54     if (queried.size() != 0)
55     {
56         if (queried.back() != 0)
57         {
58             log_error("ERROR: queried properties do not end with 0!\n");
59             return TEST_FAIL;
60         }
61         if (queried.size() % 2 != 1)
62         {
63             log_error("ERROR: queried properties does not consist of "
64                       "property-value pairs!\n");
65             return TEST_FAIL;
66         }
67     }
68     if (check.size() != 0)
69     {
70         if (check.back() != 0)
71         {
72             log_error("ERROR: check properties do not end with 0!\n");
73             return TEST_FAIL;
74         }
75         if (check.size() % 2 != 1)
76         {
77             log_error("ERROR: check properties does not consist of "
78                       "property-value pairs!\n");
79             return TEST_FAIL;
80         }
81     }
82 
83     if (queried != check)
84     {
85         for (cl_uint i = 0; i < check.size(); i = i + 2)
86         {
87             cl_properties check_prop = check[i];
88 
89             if (check_prop == 0)
90             {
91                 break;
92             }
93 
94             cl_properties check_value = check[i + 1];
95             cl_properties queried_value = 0;
96 
97             bool found = findProperty(queried, check_prop, queried_value);
98 
99             if (!found)
100             {
101                 log_error("ERROR: expected property 0x%" PRIx64 " not found!\n",
102                           check_prop);
103                 return TEST_FAIL;
104             }
105             else if (check_value != queried_value)
106             {
107                 log_error("ERROR: mis-matched value for property 0x%" PRIx64
108                           ": wanted "
109                           "0x%" PRIx64 ", got 0x%" PRIx64 "\n",
110                           check_prop, check_value, queried_value);
111                 return TEST_FAIL;
112             }
113         }
114 
115         if (queried.size() > check.size())
116         {
117             log_error("ERROR: all properties found but there are extra "
118                       "properties: expected %zu, got %zu.\n",
119                       check.size(), queried.size());
120             return TEST_FAIL;
121         }
122 
123         log_error("ERROR: properties were returned in the wrong order.\n");
124         return TEST_FAIL;
125     }
126 
127     return TEST_PASS;
128 }
129