Use _exit() instead of exit() in child processes
[piglit.git] / tests / cl / api / create-context-from-type.c
blob46095d67fce2270685154b4c95f204774261d3d3
1 /*
2 * Copyright © 2012 Blaž Tomažič <blaz.tomazic@gmail.com>
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 /**
25 * @file create-context-from-type.c
27 * Test API function:
29 * cl_context clCreateContextFromType(cl_context_properties *properties,
30 * cl_device_type device_type,
31 * void *pfn_notify (
32 * const char *errinfo,
33 * const void *private_info,
34 * size_t cb,
35 * void *user_data
36 * ),
37 * void *user_data,
38 * cl_int *errcode_ret)
41 #include "piglit-framework-cl-api.h"
44 PIGLIT_CL_API_TEST_CONFIG_BEGIN
46 config.name = "clCreateContextFromType";
47 config.version_min = 10;
49 config.run_per_platform = true;
51 PIGLIT_CL_API_TEST_CONFIG_END
54 static void
55 test(cl_context_properties *properties,
56 const cl_device_type device_type,
57 void pfn_notify (
58 const char *errinfo,
59 const void *private_info,
60 size_t cb,
61 void *user_data
63 void *user_data,
64 cl_int expected_error,
65 enum piglit_result* result,
66 const char* test_str) {
67 cl_int errNo;
68 cl_context cl_ctx;
70 /* with errNo */
71 cl_ctx = clCreateContextFromType(properties,
72 device_type,
73 pfn_notify,
74 user_data,
75 &errNo);
77 if(errNo != CL_DEVICE_NOT_FOUND) {
78 if(!piglit_cl_check_error(errNo, expected_error)) {
79 fprintf(stderr, "Failed (error code: %s): %s.\n",
80 piglit_cl_get_error_name(errNo), test_str);
81 piglit_merge_result(result, PIGLIT_FAIL);
82 return;
84 if(expected_error == CL_SUCCESS) {
85 if(cl_ctx == NULL) {
86 printf("Expecting non-NULL cl_context\n");
87 fprintf(stderr, "Failed (NULL value returned): %s.\n", test_str);
88 piglit_merge_result(result, PIGLIT_FAIL);
89 return;
91 clReleaseContext(cl_ctx);
92 } else if(cl_ctx != NULL) {
93 printf("Expecting NULL cl_context\n");
94 fprintf(stderr, "Failed (non-NULL value returned): %s.\n", test_str);
95 piglit_merge_result(result, PIGLIT_FAIL);
96 return;
100 /* without errNo */
101 cl_ctx = clCreateContextFromType(properties,
102 device_type,
103 pfn_notify,
104 user_data,
105 NULL);
107 if(errNo != CL_DEVICE_NOT_FOUND) {
108 if(expected_error == CL_SUCCESS) {
109 if(cl_ctx == NULL) {
110 printf("Expecting non-NULL cl_context\n");
111 fprintf(stderr, "Failed (NULL value returned): %s.\n", test_str);
112 piglit_merge_result(result, PIGLIT_FAIL);
113 return;
115 clReleaseContext(cl_ctx);
116 } else if(cl_ctx != NULL) {
117 printf("Expecting NULL cl_context\n");
118 fprintf(stderr, "Failed (non-NULL value returned): %s.\n", test_str);
119 piglit_merge_result(result, PIGLIT_FAIL);
120 return;
126 * @param mask Defines which fields to use
128 static cl_device_type
129 get_mixed_device_types(int mask, const cl_device_type device_types[]) {
130 int i = 0;
131 cl_device_type mixed_device_types = 0;
133 while(mask > 0) {
134 if(mask%2 == 1) {
135 mixed_device_types |= device_types[i];
137 mask >>= 1;
138 i++;
141 return mixed_device_types;
145 enum piglit_result
146 piglit_cl_test(const int argc,
147 const char** argv,
148 const struct piglit_cl_api_test_config* config,
149 const struct piglit_cl_api_test_env* env)
151 enum piglit_result result = PIGLIT_PASS;
152 char test_str[1024];
154 int i, mask;
155 cl_device_type mixed_device_types;
157 bool found_invalid_platform = false;
158 cl_platform_id* platform_ids;
159 unsigned int num_platform_ids;
160 cl_platform_id invalid_platform_id;
162 //TODO: test also CL_CONTEXT_INTEROP_USER_SYNC
163 cl_context_properties context_properties[] = {
164 CL_CONTEXT_PLATFORM, (cl_context_properties)env->platform_id,
167 cl_context_properties invalid_context_properties[] = {
168 CL_DEVICE_NAME, (cl_context_properties)env->platform_id,
171 cl_context_properties multiple_platform_context_properties[] = {
172 CL_CONTEXT_PLATFORM, (cl_context_properties)env->platform_id,
173 CL_CONTEXT_PLATFORM, (cl_context_properties)env->platform_id,
177 int num_device_types = PIGLIT_CL_ENUM_NUM(cl_device_type, env->version);
178 const cl_device_type *device_types = PIGLIT_CL_ENUM_ARRAY(cl_device_type);
180 /* Find invalid platform_id */
181 invalid_platform_id = 0;
182 num_platform_ids = piglit_cl_get_platform_ids(&platform_ids);
183 while(!found_invalid_platform) {
184 found_invalid_platform = true;
185 invalid_platform_id = (cl_platform_id)1;
186 for(i = 0; i < num_platform_ids; i++) {
187 if(invalid_platform_id == platform_ids[i]) {
188 found_invalid_platform = false;
189 break;
193 free(platform_ids);
195 /*** Normal usage ***/
198 * For each device types mix.
199 * There are 2^(num_device_types)-1 possible options.
201 for(mask = 1; mask < (1 << num_device_types); mask++) {
202 mixed_device_types = get_mixed_device_types(mask, device_types);
204 sprintf(test_str,
205 "Create context using 0x%X as device types",
206 (unsigned int)mixed_device_types);
208 test(context_properties, mixed_device_types, NULL, NULL,
209 CL_SUCCESS, &result, test_str);
210 //TODO: test callback functions
214 /*** Errors ***/
217 * CL_INVALID_VALUE if context property name in properties is
218 * not a supported property name, or if pfn_notify is NULL but
219 * user_data is not NULL.
221 * Version: 1.0
223 * CL_INVALID_VALUE if pfn_notify is NULL but user_data is
224 * not NULL.
226 * Version 1.1
228 if(env->version <= 10) {
229 test(invalid_context_properties, CL_DEVICE_TYPE_ALL, NULL, NULL,
230 CL_INVALID_VALUE, &result,
231 "Trigger CL_INVALID_VALUE if context property name in properties is not a supported property name");
234 test(context_properties, CL_DEVICE_TYPE_ALL, NULL, &context_properties,
235 CL_INVALID_VALUE, &result,
236 "Trigger CL_INVALID_VALUE if pfn_notify is NULL and user_data is not NULL");
240 * CL_INVALID_PROPERTY if context property name in properties
241 * is not a supported property name, if the value specified for
242 * a supported property name is not valid, or if the same
243 * property name is specified more than once.
245 * Version: 1.1
247 * Note: 'if the value specified for a supported property name is
248 * not valid' was already tested
250 #if defined(CL_VERSION_1_1)
251 if(env->version >= 11) {
252 test(multiple_platform_context_properties, CL_DEVICE_TYPE_ALL,
253 NULL, NULL,
254 CL_INVALID_PROPERTY, &result,
255 "Trigger CL_INVALID_PROPERTY if the same property name is pecified more than once");
257 #endif
260 * CL_DEVICE_NOT_AVAILABLE if a device in devices is currently
261 * not available even though the device was returned by clGetDeviceIDs.
263 * Note: Can not test
266 return result;