xref: /aosp_15_r20/external/OpenCL-CLHPP/tests/test_openclhpp.cpp (revision 6fee86a4f833e4f32f25770a262884407554133d)
1 #define CL_HPP_UNIT_TEST_ENABLE
2 
3 // We want to support all versions
4 #define CL_HPP_MINIMUM_OPENCL_VERSION 100
5 # include <CL/opencl.hpp>
6 # define TEST_RVALUE_REFERENCES
7 # define VECTOR_CLASS cl::vector
8 # define STRING_CLASS cl::string
9 
10 extern "C"
11 {
12 #include <unity.h>
13 #include <cmock.h>
14 #include "Mockcl.h"
15 #include "Mockcl_ext.h"
16 #include "Mockcl_gl.h"
17 #include <string.h>
18 
19 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
20 
21 /// Creates fake IDs that are easy to identify
22 
make_platform_id(int index)23 static inline cl_platform_id make_platform_id(int index)
24 {
25     return (cl_platform_id) (size_t) (0x1a1a1a1a + index);
26 }
27 
make_context(int index)28 static inline cl_context make_context(int index)
29 {
30     return (cl_context) (size_t) (0xcccccccc + index);
31 }
32 
make_device_id(int index)33 static inline cl_device_id make_device_id(int index)
34 {
35     return (cl_device_id) (size_t) (0xdededede + index);
36 }
37 
make_mem(int index)38 static inline cl_mem make_mem(int index)
39 {
40     return (cl_mem) (size_t) (0x33333333 + index);
41 }
42 
make_command_queue(int index)43 static inline cl_command_queue make_command_queue(int index)
44 {
45     return (cl_command_queue) (size_t) (0xc0c0c0c0 + index);
46 }
47 
make_kernel(int index)48 static inline cl_kernel make_kernel(int index)
49 {
50     return (cl_kernel) (size_t) (0xcececece + index);
51 }
52 
make_program(int index)53 static inline cl_program make_program(int index)
54 {
55     return (cl_program)(size_t)(0xcfcfcfcf + index);
56 }
57 
make_command_buffer_khr(int index)58 static inline cl_command_buffer_khr make_command_buffer_khr(int index)
59 {
60     return (cl_command_buffer_khr)(size_t)(0x8f8f8f8f + index);
61 }
62 
make_event(int index)63 static inline cl_event make_event(int index)
64 {
65     return (cl_event)(size_t)(0xd0d0d0d0 + index);
66 }
67 
68 #if defined(cl_khr_semaphore)
make_semaphore_khr(int index)69 static inline cl_semaphore_khr make_semaphore_khr(int index)
70 {
71     return (cl_semaphore_khr)(size_t)(0xa0b0c0e0 + index);
72 }
73 #endif
74 
75 /* Pools of pre-allocated wrapped objects for tests.
76  */
77 static const int POOL_MAX = 5;
78 static cl::Platform platformPool[POOL_MAX];
79 static cl::Context contextPool[POOL_MAX];
80 static cl::CommandQueue commandQueuePool[POOL_MAX];
81 static cl::Buffer bufferPool[POOL_MAX];
82 static cl::Image2D image2DPool[POOL_MAX];
83 static cl::Image3D image3DPool[POOL_MAX];
84 static cl::Kernel kernelPool[POOL_MAX];
85 static cl::Program programPool[POOL_MAX];
86 #if defined(cl_khr_command_buffer)
87 static cl::CommandBufferKhr commandBufferKhrPool[POOL_MAX];
88 #endif
89 #if defined(cl_khr_semaphore)
90 static cl::Semaphore semaphorePool[POOL_MAX];
91 #endif
92 static cl::Device devicePool[POOL_MAX];
93 
94 /****************************************************************************
95  * Stub functions shared by multiple tests
96  ****************************************************************************/
97 /**
98  * Stub implementation of clGetCommandQueueInfo that returns the first context.
99  */
clGetCommandQueueInfo_context(cl_command_queue id,cl_command_queue_info param_name,size_t param_value_size,void * param_value,size_t * param_value_size_ret,int num_calls)100 static cl_int clGetCommandQueueInfo_context(
101     cl_command_queue id,
102     cl_command_queue_info param_name,
103     size_t param_value_size,
104     void *param_value,
105     size_t *param_value_size_ret,
106     int num_calls)
107 {
108     (void) id;
109     (void) num_calls;
110 
111 
112     TEST_ASSERT_EQUAL_HEX(CL_QUEUE_CONTEXT, param_name);
113     TEST_ASSERT(param_value == nullptr || param_value_size >= sizeof(cl_context));
114     if (param_value_size_ret != nullptr)
115         *param_value_size_ret = sizeof(cl_context);
116     if (param_value != nullptr)
117         *(cl_context *)param_value = make_context(0);
118 
119     return CL_SUCCESS;
120 }
121 
122 /**
123  * Stub implementation of clGetDeviceInfo that just returns the first platform.
124  */
clGetDeviceInfo_platform(cl_device_id id,cl_device_info param_name,size_t param_value_size,void * param_value,size_t * param_value_size_ret,int num_calls)125 static cl_int clGetDeviceInfo_platform(
126     cl_device_id id,
127     cl_device_info param_name,
128     size_t param_value_size,
129     void *param_value,
130     size_t *param_value_size_ret,
131     int num_calls)
132 {
133     (void) id;
134     (void) num_calls;
135 
136     TEST_ASSERT_EQUAL_HEX(CL_DEVICE_PLATFORM, param_name);
137     TEST_ASSERT(param_value == nullptr || param_value_size >= sizeof(cl_platform_id));
138     if (param_value_size_ret != nullptr)
139         *param_value_size_ret = sizeof(cl_platform_id);
140     if (param_value != nullptr)
141         *(cl_platform_id *) param_value = make_platform_id(0);
142     return CL_SUCCESS;
143 }
144 
145 /**
146  * Stub implementation of clGetContextInfo that just returns the first device.
147  */
clGetContextInfo_device(cl_context id,cl_context_info param_name,size_t param_value_size,void * param_value,size_t * param_value_size_ret,int num_calls)148 static cl_int clGetContextInfo_device(
149     cl_context id,
150     cl_context_info param_name,
151     size_t param_value_size,
152     void *param_value,
153     size_t *param_value_size_ret,
154     int num_calls)
155 {
156     (void) id;
157     (void) num_calls;
158 
159     TEST_ASSERT_EQUAL_HEX(CL_CONTEXT_DEVICES, param_name);
160     TEST_ASSERT(param_value == nullptr || param_value_size >= sizeof(cl_device_id));
161     if (param_value_size_ret != nullptr)
162         *param_value_size_ret = sizeof(cl_device_id);
163     if (param_value != nullptr)
164         *(cl_device_id *) param_value = make_device_id(0);
165     return CL_SUCCESS;
166 }
167 
168 
169 /**
170  * Stub implementation of clGetPlatformInfo that returns a specific version.
171  * It also checks that the id is the zeroth platform.
172  */
clGetPlatformInfo_version(cl_platform_id id,cl_platform_info param_name,size_t param_value_size,void * param_value,size_t * param_value_size_ret,const char * version)173 static cl_int clGetPlatformInfo_version(
174     cl_platform_id id,
175     cl_platform_info param_name,
176     size_t param_value_size,
177     void *param_value,
178     size_t *param_value_size_ret,
179     const char *version)
180 {
181     size_t bytes = strlen(version) + 1;
182 
183     TEST_ASSERT_NOT_NULL(id);
184     TEST_ASSERT_EQUAL_PTR(make_platform_id(0), id);
185     TEST_ASSERT_EQUAL_HEX(CL_PLATFORM_VERSION, param_name);
186     TEST_ASSERT(param_value == nullptr || param_value_size >= bytes);
187     if (param_value_size_ret != nullptr)
188         *param_value_size_ret = bytes;
189     if (param_value != nullptr)
190         strcpy((char *) param_value, version);
191     return CL_SUCCESS;
192 }
193 
194 /**
195  * A stub for clGetPlatformInfo that will only support querying
196  * CL_PLATFORM_VERSION, and will return version 1.1.
197  */
clGetPlatformInfo_version_1_1(cl_platform_id id,cl_platform_info param_name,size_t param_value_size,void * param_value,size_t * param_value_size_ret,int num_calls)198 static cl_int clGetPlatformInfo_version_1_1(
199     cl_platform_id id,
200     cl_platform_info param_name,
201     size_t param_value_size,
202     void *param_value,
203     size_t *param_value_size_ret,
204     int num_calls)
205 {
206     (void) num_calls;
207     return clGetPlatformInfo_version(
208         id, param_name, param_value_size, param_value,
209         param_value_size_ret, "OpenCL 1.1 Mock");
210 }
211 
212 /**
213  * A stub for clGetPlatformInfo that will only support querying
214  * CL_PLATFORM_VERSION, and will return version 1.2.
215  */
clGetPlatformInfo_version_1_2(cl_platform_id id,cl_platform_info param_name,size_t param_value_size,void * param_value,size_t * param_value_size_ret,int num_calls)216 static cl_int clGetPlatformInfo_version_1_2(
217     cl_platform_id id,
218     cl_platform_info param_name,
219     size_t param_value_size,
220     void *param_value,
221     size_t *param_value_size_ret,
222     int num_calls)
223 {
224     (void) num_calls;
225     return clGetPlatformInfo_version(
226         id, param_name, param_value_size, param_value,
227         param_value_size_ret, "OpenCL 1.2 Mock");
228 }
229 
230 /**
231  * A stub for clGetPlatformInfo that will only support querying
232  * CL_PLATFORM_VERSION, and will return version 2.0.
233  */
clGetPlatformInfo_version_2_0(cl_platform_id id,cl_platform_info param_name,size_t param_value_size,void * param_value,size_t * param_value_size_ret,int num_calls)234 static cl_int clGetPlatformInfo_version_2_0(
235     cl_platform_id id,
236     cl_platform_info param_name,
237     size_t param_value_size,
238     void *param_value,
239     size_t *param_value_size_ret,
240     int num_calls)
241 {
242     (void) num_calls;
243     return clGetPlatformInfo_version(
244         id, param_name, param_value_size, param_value,
245         param_value_size_ret, "OpenCL 2.0 Mock");
246 }
247 
248 #if CL_HPP_TARGET_OPENCL_VERSION >= 300
249 /**
250  * A stub for clGetPlatformInfo that will only support querying
251  * CL_PLATFORM_VERSION, and will return version 3.0.
252  */
clGetPlatformInfo_version_3_0(cl_platform_id id,cl_platform_info param_name,size_t param_value_size,void * param_value,size_t * param_value_size_ret,int num_calls)253 static cl_int clGetPlatformInfo_version_3_0(
254     cl_platform_id id,
255     cl_platform_info param_name,
256     size_t param_value_size,
257     void *param_value,
258     size_t *param_value_size_ret,
259     int num_calls)
260 {
261     (void) num_calls;
262     return clGetPlatformInfo_version(
263         id, param_name, param_value_size, param_value,
264         param_value_size_ret, "OpenCL 3.0 Mock");
265 }
266 #endif
267 
268 /* Simulated reference counts. The table points to memory held by the caller.
269  * This makes things simpler in the common case of only one object to be
270  * reference counted.
271  */
272 class RefcountTable
273 {
274 private:
275     size_t n; // number of objects
276     void * const *objects; // object IDs
277     int *refcounts;        // current refcounts
278 
find(void * object)279     size_t find(void *object)
280     {
281         size_t idx = 0;
282         while (idx < n && objects[idx] != object)
283             idx++;
284         TEST_ASSERT(idx < n);
285         TEST_ASSERT(refcounts[idx] > 0); // otherwise object has been destroyed
286         return idx;
287     }
288 
289 public:
RefcountTable()290     RefcountTable() : n(0), objects(nullptr), refcounts(nullptr) {}
291 
init(size_t n,void * const * objects,int * refcounts)292     void init(size_t n, void * const *objects, int *refcounts)
293     {
294         this->n = n;
295         this->objects = objects;
296         this->refcounts = refcounts;
297     }
298 
reset()299     void reset()
300     {
301         init(0, nullptr, nullptr);
302     }
303 
retain(void * object)304     cl_int retain(void *object)
305     {
306         size_t idx = find(object);
307         ++refcounts[idx];
308         return CL_SUCCESS;
309     }
310 
release(void * object)311     cl_int release(void *object)
312     {
313         size_t idx = find(object);
314         --refcounts[idx];
315         return CL_SUCCESS;
316     }
317 };
318 
319 /* Stubs for retain/release calls that track reference counts. The stubs
320  * check that the reference count never becomes negative and that a zero
321  * reference count is never incremented.
322  *
323  * Use the prepareRefcount* calls to set up the global variables first.
324  */
325 
326 #define MAKE_REFCOUNT_STUBS(cl_type, retainfunc, releasefunc, table) \
327     static RefcountTable table; \
328     static cl_int retainfunc ## _refcount(cl_type object, int num_calls) \
329     { \
330         (void) num_calls; \
331         return table.retain(object); \
332     } \
333     static cl_int releasefunc ## _refcount(cl_type object, int num_calls) \
334     { \
335         (void) num_calls; \
336         return table.release(object); \
337     } \
338     static void prepare_ ## table(size_t n, cl_type const *objects, int *refcounts) \
339     { \
340         table.init(n, (void * const *) objects, refcounts); \
341         retainfunc ## _StubWithCallback(retainfunc ## _refcount); \
342         releasefunc ## _StubWithCallback(releasefunc ## _refcount); \
343     }
344 
MAKE_REFCOUNT_STUBS(cl_program,clRetainProgram,clReleaseProgram,programRefcounts)345 MAKE_REFCOUNT_STUBS(cl_program, clRetainProgram, clReleaseProgram, programRefcounts)
346 MAKE_REFCOUNT_STUBS(cl_command_queue, clRetainCommandQueue, clReleaseCommandQueue, commandQueueRefcounts)
347 MAKE_REFCOUNT_STUBS(cl_device_id, clRetainDevice, clReleaseDevice, deviceRefcounts)
348 MAKE_REFCOUNT_STUBS(cl_context, clRetainContext, clReleaseContext, contextRefcounts)
349 MAKE_REFCOUNT_STUBS(cl_mem, clRetainMemObject, clReleaseMemObject, memRefcounts)
350 // Deactivated because unused for now.
351 #if defined(cl_khr_command_buffer) && 0
352 MAKE_REFCOUNT_STUBS(cl_command_buffer_khr, clRetainCommandBufferKHR, clReleaseCommandBufferKHR, commandBufferKhrRefcounts)
353 #endif
354 
355 /* The indirection through MAKE_MOVE_TESTS2 with a prefix parameter is to
356  * prevent the simple-minded parser from Unity from identifying tests from the
357  * macro value.
358  */
359 #ifdef TEST_RVALUE_REFERENCES
360 #define MAKE_MOVE_TESTS2(prefix, type, makeFunc, releaseFunc, pool) \
361     void prefix ## MoveAssign ## type ## NonNull(void) \
362     { \
363         releaseFunc ## _ExpectAndReturn(makeFunc(0), CL_SUCCESS); \
364         pool[0] = std::move(pool[1]); \
365         TEST_ASSERT_EQUAL_PTR(makeFunc(1), pool[0]()); \
366         TEST_ASSERT_NULL(pool[1]()); \
367     } \
368     \
369     void prefix ## MoveAssign ## type ## Null(void) \
370     { \
371         pool[0]() = nullptr; \
372         pool[0] = std::move(pool[1]); \
373         TEST_ASSERT_EQUAL_PTR(makeFunc(1), pool[0]()); \
374         TEST_ASSERT_NULL(pool[1]()); \
375     } \
376     \
377     void prefix ## MoveConstruct ## type ## NonNull(void) \
378     { \
379         cl::type tmp(std::move(pool[0])); \
380         TEST_ASSERT_EQUAL_PTR(makeFunc(0), tmp()); \
381         TEST_ASSERT_NULL(pool[0]()); \
382         tmp() = nullptr; \
383     } \
384     \
385     void prefix ## MoveConstruct ## type ## Null(void) \
386     { \
387         cl::type empty; \
388         cl::type tmp(std::move(empty)); \
389         TEST_ASSERT_NULL(tmp()); \
390         TEST_ASSERT_NULL(empty()); \
391     }
392 #else
393 #define MAKE_MOVE_TESTS2(prefix, type, makeFunc, releaseFunc, pool) \
394     void prefix ## MoveAssign ## type ## NonNull(void) {} \
395     void prefix ## MoveAssign ## type ## Null(void) {} \
396     void prefix ## MoveConstruct ## type ## NonNull(void) {} \
397     void prefix ## MoveConstruct ## type ## Null(void) {}
398 #endif // !TEST_RVALUE_REFERENCES
399 #define MAKE_MOVE_TESTS(type, makeFunc, releaseFunc, pool) \
400     MAKE_MOVE_TESTS2(test, type, makeFunc, releaseFunc, pool)
401 
402 void setUp(void)
403 {
404     /* init extensions addresses with mocked functions */
405 #if defined(cl_khr_command_buffer)
406     cl::pfn_clCreateCommandBufferKHR = ::clCreateCommandBufferKHR;
407     cl::pfn_clFinalizeCommandBufferKHR = ::clFinalizeCommandBufferKHR;
408     cl::pfn_clRetainCommandBufferKHR = ::clRetainCommandBufferKHR;
409     cl::pfn_clReleaseCommandBufferKHR = ::clReleaseCommandBufferKHR;
410     cl::pfn_clGetCommandBufferInfoKHR = ::clGetCommandBufferInfoKHR;
411 #endif
412 #if defined(cl_khr_semaphore)
413     cl::pfn_clCreateSemaphoreWithPropertiesKHR = ::clCreateSemaphoreWithPropertiesKHR;
414     cl::pfn_clReleaseSemaphoreKHR = ::clReleaseSemaphoreKHR;
415     cl::pfn_clRetainSemaphoreKHR = ::clRetainSemaphoreKHR;
416     cl::pfn_clEnqueueWaitSemaphoresKHR = ::clEnqueueWaitSemaphoresKHR;
417     cl::pfn_clEnqueueSignalSemaphoresKHR = ::clEnqueueSignalSemaphoresKHR;
418     cl::pfn_clGetSemaphoreInfoKHR = ::clGetSemaphoreInfoKHR;
419 #endif
420 #if defined(cl_khr_external_semaphore)
421     cl::pfn_clGetSemaphoreHandleForTypeKHR = ::clGetSemaphoreHandleForTypeKHR;
422 #endif
423 #ifdef cl_khr_external_memory
424     cl::pfn_clEnqueueAcquireExternalMemObjectsKHR = ::clEnqueueAcquireExternalMemObjectsKHR;
425     cl::pfn_clEnqueueReleaseExternalMemObjectsKHR = ::clEnqueueReleaseExternalMemObjectsKHR;
426 #endif
427 
428 #if defined(cl_ext_image_requirements_info)
429     cl::pfn_clGetImageRequirementsInfoEXT = ::clGetImageRequirementsInfoEXT;
430 #endif
431 
432 #if defined(cl_ext_device_fission)
433     cl::pfn_clCreateSubDevicesEXT = ::clCreateSubDevicesEXT;
434 #endif
435 
436     /* We reach directly into the objects rather than using assignment to
437      * avoid the reference counting functions from being called.
438      */
439     for (int i = 0; i < POOL_MAX; i++)
440     {
441         platformPool[i]() = make_platform_id(i);
442         contextPool[i]() = make_context(i);
443         commandQueuePool[i]() = make_command_queue(i);
444         bufferPool[i]() = make_mem(i);
445         image2DPool[i]() = make_mem(i);
446         image3DPool[i]() = make_mem(i);
447         kernelPool[i]() = make_kernel(i);
448         programPool[i]() = make_program(i);
449 #if defined(cl_khr_command_buffer)
450         commandBufferKhrPool[i]() = make_command_buffer_khr(i);
451 #endif
452 #if defined(cl_khr_semaphore)
453         semaphorePool[i]() = make_semaphore_khr(i);
454 #endif
455         devicePool[i]() = make_device_id(i);
456     }
457 
458     programRefcounts.reset();
459     deviceRefcounts.reset();
460     contextRefcounts.reset();
461     memRefcounts.reset();
462 }
463 
tearDown(void)464 void tearDown(void)
465 {
466     /* Wipe out the internal state to avoid a release call being made */
467     for (int i = 0; i < POOL_MAX; i++)
468     {
469         platformPool[i]() = nullptr;
470         contextPool[i]() = nullptr;
471         commandQueuePool[i]() = nullptr;
472         bufferPool[i]() = nullptr;
473         image2DPool[i]() = nullptr;
474         image3DPool[i]() = nullptr;
475         kernelPool[i]() = nullptr;
476         programPool[i]() = nullptr;
477         devicePool[i]() = nullptr;
478 #if defined(cl_khr_command_buffer)
479         commandBufferKhrPool[i]() = nullptr;
480 #endif
481 #if defined(cl_khr_semaphore)
482         semaphorePool[i]() = nullptr;
483 #endif
484     }
485 
486 #if defined(cl_khr_command_buffer)
487     cl::pfn_clCreateCommandBufferKHR = nullptr;
488     cl::pfn_clFinalizeCommandBufferKHR = nullptr;
489     cl::pfn_clRetainCommandBufferKHR = nullptr;
490     cl::pfn_clReleaseCommandBufferKHR = nullptr;
491     cl::pfn_clGetCommandBufferInfoKHR = nullptr;
492 #endif
493 #if defined(cl_khr_semaphore)
494     cl::pfn_clCreateSemaphoreWithPropertiesKHR = nullptr;
495     cl::pfn_clReleaseSemaphoreKHR = nullptr;
496     cl::pfn_clRetainSemaphoreKHR = nullptr;
497     cl::pfn_clEnqueueWaitSemaphoresKHR = nullptr;
498     cl::pfn_clEnqueueSignalSemaphoresKHR = nullptr;
499     cl::pfn_clGetSemaphoreInfoKHR = nullptr;
500 #endif
501 #ifdef cl_khr_external_memory
502     cl::pfn_clEnqueueAcquireExternalMemObjectsKHR = nullptr;
503     cl::pfn_clEnqueueReleaseExternalMemObjectsKHR = nullptr;
504 #endif
505 }
506 
507 /****************************************************************************
508  * Tests for cl::Context
509  ****************************************************************************/
510 
testCopyContextNonNull(void)511 void testCopyContextNonNull(void)
512 {
513     clReleaseContext_ExpectAndReturn(make_context(0), CL_SUCCESS);
514     clRetainContext_ExpectAndReturn(make_context(1), CL_SUCCESS);
515 
516     contextPool[0] = contextPool[1];
517     TEST_ASSERT_EQUAL_PTR(make_context(1), contextPool[0]());
518 }
519 
520 void testMoveAssignContextNonNull(void);
521 void testMoveAssignContextNull(void);
522 void testMoveConstructContextNonNull(void);
523 void testMoveConstructContextNull(void);
MAKE_MOVE_TESTS(Context,make_context,clReleaseContext,contextPool)524 MAKE_MOVE_TESTS(Context, make_context, clReleaseContext, contextPool)
525 
526 /// Stub for querying CL_CONTEXT_DEVICES that returns two devices
527 static cl_int clGetContextInfo_testContextGetDevices(
528     cl_context context,
529     cl_context_info param_name,
530     size_t param_value_size,
531     void *param_value,
532     size_t *param_value_size_ret,
533     int num_calls)
534 {
535     (void) num_calls;
536     TEST_ASSERT_EQUAL_PTR(make_context(0), context);
537     TEST_ASSERT_EQUAL_HEX(CL_CONTEXT_DEVICES, param_name);
538     TEST_ASSERT(param_value == nullptr || param_value_size >= 2 * sizeof(cl_device_id));
539     if (param_value_size_ret != nullptr)
540         *param_value_size_ret = 2 * sizeof(cl_device_id);
541     if (param_value != nullptr)
542     {
543         cl_device_id *devices = (cl_device_id *) param_value;
544         devices[0] = make_device_id(0);
545         devices[1] = make_device_id(1);
546     }
547     return CL_SUCCESS;
548 }
549 
550 /// Test that queried devices are not refcounted
testContextGetDevices1_1(void)551 void testContextGetDevices1_1(void)
552 {
553     clGetContextInfo_StubWithCallback(clGetContextInfo_testContextGetDevices);
554     clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
555     clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_1_1);
556 
557     VECTOR_CLASS<cl::Device> devices = contextPool[0].getInfo<CL_CONTEXT_DEVICES>();
558     TEST_ASSERT_EQUAL(2, devices.size());
559     TEST_ASSERT_EQUAL_PTR(make_device_id(0), devices[0]());
560     TEST_ASSERT_EQUAL_PTR(make_device_id(1), devices[1]());
561 }
562 
563 /// Test that queried devices are correctly refcounted
testContextGetDevices1_2(void)564 void testContextGetDevices1_2(void)
565 {
566     clGetContextInfo_StubWithCallback(clGetContextInfo_testContextGetDevices);
567     clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
568     clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_1_2);
569 
570     clRetainDevice_ExpectAndReturn(make_device_id(0), CL_SUCCESS);
571     clRetainDevice_ExpectAndReturn(make_device_id(1), CL_SUCCESS);
572 
573     VECTOR_CLASS<cl::Device> devices = contextPool[0].getInfo<CL_CONTEXT_DEVICES>();
574     TEST_ASSERT_EQUAL(2, devices.size());
575     TEST_ASSERT_EQUAL_PTR(make_device_id(0), devices[0]());
576     TEST_ASSERT_EQUAL_PTR(make_device_id(1), devices[1]());
577 
578     // Prevent release in the destructor
579     devices[0]() = nullptr;
580     devices[1]() = nullptr;
581 }
582 
583 #if !defined(__APPLE__) && !defined(__MACOS)
584 // This is used to get a list of all platforms, so expect two calls
585 // First, return to say we have two platforms
586 // Then return the two platform id_s
clGetPlatformIDs_testContextFromType(cl_uint num_entries,cl_platform_id * platforms,cl_uint * num_platforms,int num_calls)587 static cl_int clGetPlatformIDs_testContextFromType(
588     cl_uint num_entries,
589     cl_platform_id *platforms,
590     cl_uint *num_platforms,
591     int num_calls)
592 {
593     if (num_calls == 0)
594     {
595         TEST_ASSERT_NULL(platforms);
596         TEST_ASSERT_NOT_NULL(num_platforms);
597         *num_platforms = 2;
598         return CL_SUCCESS;
599     }
600     else if (num_calls == 1)
601     {
602         TEST_ASSERT_NOT_NULL(platforms);
603         TEST_ASSERT_EQUAL(2, num_entries);
604         platforms[0] = make_platform_id(0);
605         platforms[1] = make_platform_id(1);
606         return CL_SUCCESS;
607     }
608     else
609     {
610         TEST_FAIL_MESSAGE("clGetPlatformIDs called too many times");
611         return CL_INVALID_VALUE;
612     }
613 }
614 #endif
615 
616 #if !defined(__APPLE__) && !defined(__MACOS)
617 // Expect three calls to this
618 // 1. Platform 1, we have no GPUs
619 // 2. Platform 2, we have two GPUs
620 // 3. Here are the two cl_device_id's
clGetDeviceIDs_testContextFromType(cl_platform_id platform,cl_device_type device_type,cl_uint num_entries,cl_device_id * devices,cl_uint * num_devices,int num_calls)621 static cl_int clGetDeviceIDs_testContextFromType(
622     cl_platform_id  platform,
623     cl_device_type  device_type,
624     cl_uint  num_entries,
625     cl_device_id  *devices,
626     cl_uint  *num_devices,
627     int num_calls)
628 {
629     if (num_calls == 0)
630     {
631         TEST_ASSERT_EQUAL_PTR(make_platform_id(0), platform);
632         TEST_ASSERT_EQUAL(CL_DEVICE_TYPE_GPU, device_type);
633         TEST_ASSERT_NOT_NULL(num_devices);
634         return CL_DEVICE_NOT_FOUND;
635     }
636     else if (num_calls == 1)
637     {
638         TEST_ASSERT_EQUAL_PTR(make_platform_id(1), platform);
639         TEST_ASSERT_EQUAL(CL_DEVICE_TYPE_GPU, device_type);
640         TEST_ASSERT_NOT_NULL(num_devices);
641         *num_devices = 2;
642         return CL_SUCCESS;
643     }
644     else if (num_calls == 2)
645     {
646         TEST_ASSERT_EQUAL_PTR(make_platform_id(1), platform);
647         TEST_ASSERT_EQUAL(CL_DEVICE_TYPE_GPU, device_type);
648         TEST_ASSERT_EQUAL(2, num_entries);
649         TEST_ASSERT_NOT_NULL(devices);
650         devices[0] = make_device_id(0);
651         devices[1] = make_device_id(1);
652         return CL_SUCCESS;
653     }
654     else
655     {
656         TEST_FAIL_MESSAGE("clGetDeviceIDs called too many times");
657         return CL_INVALID_VALUE;
658     }
659 }
660 #endif
661 
662 // Stub for clCreateContextFromType
663 // - expect platform 1 with GPUs and non-null properties
clCreateContextFromType_testContextFromType(const cl_context_properties * properties,cl_device_type device_type,void (CL_CALLBACK * pfn_notify)(const char * errinfo,const void * private_info,size_t cb,void * user_data),void * user_data,cl_int * errcode_ret,int num_calls)664 static cl_context clCreateContextFromType_testContextFromType(
665     const cl_context_properties  *properties,
666     cl_device_type  device_type,
667     void  (CL_CALLBACK *pfn_notify) (const char *errinfo,
668     const void  *private_info,
669     size_t  cb,
670     void  *user_data),
671     void  *user_data,
672     cl_int  *errcode_ret,
673     int num_calls)
674 {
675     (void) pfn_notify;
676     (void) user_data;
677     (void) num_calls;
678 
679     TEST_ASSERT_EQUAL(CL_DEVICE_TYPE_GPU, device_type);
680 #if !defined(__APPLE__) && !defined(__MACOS)
681     TEST_ASSERT_NOT_NULL(properties);
682     TEST_ASSERT_EQUAL(CL_CONTEXT_PLATFORM, properties[0]);
683     TEST_ASSERT_EQUAL(make_platform_id(1), properties[1]);
684 #else
685     (void) properties;
686 #endif
687     if (errcode_ret)
688         *errcode_ret = CL_SUCCESS;
689     return make_context(0);
690 }
691 
testContextFromType(void)692 void testContextFromType(void)
693 {
694 #if !defined(__APPLE__) && !defined(__MACOS)
695     clGetPlatformIDs_StubWithCallback(clGetPlatformIDs_testContextFromType);
696     clGetDeviceIDs_StubWithCallback(clGetDeviceIDs_testContextFromType);
697 
698     // The opencl.hpp header will perform an extra retain here to be consistent
699     // with other APIs retaining runtime-owned objects before releasing them
700     clRetainDevice_ExpectAndReturn(make_device_id(0), CL_SUCCESS);
701     clRetainDevice_ExpectAndReturn(make_device_id(1), CL_SUCCESS);
702 
703     clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
704     clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_1_2);
705 
706     // End of scope of vector of devices within constructor
707     clReleaseDevice_ExpectAndReturn(make_device_id(0), CL_SUCCESS);
708     clReleaseDevice_ExpectAndReturn(make_device_id(1), CL_SUCCESS);
709 #endif
710 
711     clCreateContextFromType_StubWithCallback(clCreateContextFromType_testContextFromType);
712 
713     cl::Context context(CL_DEVICE_TYPE_GPU);
714     TEST_ASSERT_EQUAL_PTR(make_context(0), context());
715 
716     clReleaseContext_ExpectAndReturn(make_context(0), CL_SUCCESS);
717 }
718 
testContextFromTypeNonNullProperties(void)719 void testContextFromTypeNonNullProperties(void)
720 {
721     clCreateContextFromType_StubWithCallback(clCreateContextFromType_testContextFromType);
722 
723     const cl_context_properties props[] = {
724         CL_CONTEXT_PLATFORM, (cl_context_properties)make_platform_id(1), 0 };
725     cl::Context context(CL_DEVICE_TYPE_GPU, props);
726     TEST_ASSERT_EQUAL_PTR(make_context(0), context());
727 
728     clReleaseContext_ExpectAndReturn(make_context(0), CL_SUCCESS);
729 }
730 
clCreateContext_testContextNonNullProperties(const cl_context_properties * properties,cl_uint num_devices,const cl_device_id * devices,void (CL_CALLBACK * pfn_notify)(const char * errinfo,const void * private_info,size_t cb,void * user_data),void * user_data,cl_int * errcode_ret,int num_calls)731 static cl_context clCreateContext_testContextNonNullProperties(
732     const cl_context_properties* properties,
733     cl_uint num_devices,
734     const cl_device_id* devices,
735     void  (CL_CALLBACK *pfn_notify) (const char *errinfo, const void  *private_info, size_t  cb, void  *user_data),
736     void  *user_data,
737     cl_int  *errcode_ret,
738     int num_calls)
739 {
740     (void) pfn_notify;
741     (void) user_data;
742     (void) num_calls;
743 
744     TEST_ASSERT_NOT_NULL(properties);
745     TEST_ASSERT_GREATER_THAN(0, num_devices);
746     for (int i = 0; i < (int)num_devices; i++) {
747         TEST_ASSERT_EQUAL(make_device_id(i), devices[i]);
748     }
749     if (errcode_ret != nullptr)
750         *errcode_ret = CL_SUCCESS;
751     return make_context(0);
752 }
753 
testContextWithDeviceNonNullProperties(void)754 void testContextWithDeviceNonNullProperties(void)
755 {
756     clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
757     clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_1_1);
758     clCreateContext_StubWithCallback(clCreateContext_testContextNonNullProperties);
759     clReleaseContext_ExpectAndReturn(make_context(0), CL_SUCCESS);
760 
761     const cl_context_properties props[] = {
762         CL_CONTEXT_PLATFORM, (cl_context_properties)make_platform_id(0), 0 };
763     cl::Device device = cl::Device(make_device_id(0));
764 
765     cl::Context context(device, props);
766     TEST_ASSERT_EQUAL_PTR(make_context(0), context());
767 }
768 
testContextWithDevicesNonNullProperties(void)769 void testContextWithDevicesNonNullProperties(void)
770 {
771     clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
772     clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_1_1);
773     clCreateContext_StubWithCallback(clCreateContext_testContextNonNullProperties);
774     clReleaseContext_ExpectAndReturn(make_context(0), CL_SUCCESS);
775 
776     const cl_context_properties props[] = {
777         CL_CONTEXT_PLATFORM, (cl_context_properties)make_platform_id(0), 0 };
778     cl::Device device0 = cl::Device(make_device_id(0));
779     cl::Device device1 = cl::Device(make_device_id(1));
780 
781     cl::Context context({device0, device1}, props);
782     TEST_ASSERT_EQUAL_PTR(make_context(0), context());
783 }
784 
785 /****************************************************************************
786  * Tests for cl::CommandQueue
787  ****************************************************************************/
788 
789 void testMoveAssignCommandQueueNonNull(void);
790 void testMoveAssignCommandQueueNull(void);
791 void testMoveConstructCommandQueueNonNull(void);
792 void testMoveConstructCommandQueueNull(void);
MAKE_MOVE_TESTS(CommandQueue,make_command_queue,clReleaseCommandQueue,commandQueuePool)793 MAKE_MOVE_TESTS(CommandQueue, make_command_queue, clReleaseCommandQueue, commandQueuePool)
794 
795 // Stub for clGetCommandQueueInfo that returns context 0
796 static cl_int clGetCommandQueueInfo_testCommandQueueGetContext(
797     cl_command_queue command_queue,
798     cl_command_queue_info param_name,
799     size_t param_value_size,
800     void *param_value,
801     size_t *param_value_size_ret,
802     int num_calls)
803 {
804     (void) num_calls;
805     TEST_ASSERT_EQUAL_PTR(make_command_queue(0), command_queue);
806     TEST_ASSERT_EQUAL_HEX(CL_QUEUE_CONTEXT, param_name);
807     TEST_ASSERT(param_value == nullptr || param_value_size >= sizeof(cl_context));
808     if (param_value_size_ret != nullptr)
809         *param_value_size_ret = sizeof(cl_context);
810     if (param_value != nullptr)
811         *(cl_context *) param_value = make_context(0);
812     return CL_SUCCESS;
813 }
814 
testCommandQueueGetContext(void)815 void testCommandQueueGetContext(void)
816 {
817     cl_context expected = make_context(0);
818     int refcount = 1;
819 
820     clGetCommandQueueInfo_StubWithCallback(clGetCommandQueueInfo_testCommandQueueGetContext);
821     prepare_contextRefcounts(1, &expected, &refcount);
822 
823     cl::Context ctx = commandQueuePool[0].getInfo<CL_QUEUE_CONTEXT>();
824     TEST_ASSERT_EQUAL_PTR(expected, ctx());
825     TEST_ASSERT_EQUAL(2, refcount);
826 
827     ctx() = nullptr;
828 }
829 
830 // Stub for clGetCommandQueueInfo that returns device 0
clGetCommandQueueInfo_testCommandQueueGetDevice(cl_command_queue command_queue,cl_command_queue_info param_name,size_t param_value_size,void * param_value,size_t * param_value_size_ret,int num_calls)831 static cl_int clGetCommandQueueInfo_testCommandQueueGetDevice(
832     cl_command_queue command_queue,
833     cl_command_queue_info param_name,
834     size_t param_value_size,
835     void *param_value,
836     size_t *param_value_size_ret,
837     int num_calls)
838 {
839     (void) num_calls;
840     TEST_ASSERT_EQUAL_PTR(make_command_queue(0), command_queue);
841     TEST_ASSERT_EQUAL_HEX(CL_QUEUE_DEVICE, param_name);
842     TEST_ASSERT(param_value == nullptr || param_value_size >= sizeof(cl_device_id));
843     if (param_value_size_ret != nullptr)
844         *param_value_size_ret = sizeof(cl_device_id);
845     if (param_value != nullptr)
846         *(cl_device_id *) param_value = make_device_id(0);
847     return CL_SUCCESS;
848 }
849 
testCommandQueueGetDevice1_1(void)850 void testCommandQueueGetDevice1_1(void)
851 {
852     cl_device_id expected = make_device_id(0);
853 
854     clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
855     clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_1_1);
856     clGetCommandQueueInfo_StubWithCallback(clGetCommandQueueInfo_testCommandQueueGetDevice);
857 
858     cl::Device device = commandQueuePool[0].getInfo<CL_QUEUE_DEVICE>();
859     TEST_ASSERT_EQUAL_PTR(expected, device());
860 
861     device() = nullptr;
862 }
863 
testCommandQueueGetDevice1_2(void)864 void testCommandQueueGetDevice1_2(void)
865 {
866     cl_device_id expected = make_device_id(0);
867     int refcount = 1;
868 
869     clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
870     clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_1_2);
871     clGetCommandQueueInfo_StubWithCallback(clGetCommandQueueInfo_testCommandQueueGetDevice);
872     prepare_deviceRefcounts(1, &expected, &refcount);
873 
874     cl::Device device = commandQueuePool[0].getInfo<CL_QUEUE_DEVICE>();
875     TEST_ASSERT_EQUAL_PTR(expected, device());
876     TEST_ASSERT_EQUAL(2, refcount);
877 
878     device() = nullptr;
879 }
880 
881 #if CL_HPP_TARGET_OPENCL_VERSION < 200
882 // stub for clCreateCommandQueue - returns queue zero
clCreateCommandQueue_testCommandQueueFromSpecifiedContext(cl_context context,cl_device_id device,cl_command_queue_properties properties,cl_int * errcode_ret,int num_calls)883 static cl_command_queue clCreateCommandQueue_testCommandQueueFromSpecifiedContext(
884     cl_context context,
885     cl_device_id device,
886     cl_command_queue_properties properties,
887     cl_int *errcode_ret,
888     int num_calls)
889 {
890     (void) num_calls;
891     TEST_ASSERT_EQUAL_PTR(make_context(0), context);
892     TEST_ASSERT_EQUAL_PTR(make_device_id(0), device);
893     TEST_ASSERT(properties == 0);
894     if (errcode_ret != nullptr)
895         *errcode_ret = CL_SUCCESS;
896     return make_command_queue(0);
897 }
898 #else
899 // stub for clCreateCommandQueueWithProperties - returns queue zero
clCreateCommandQueueWithProperties_testCommandQueueFromSpecifiedContext(cl_context context,cl_device_id device,const cl_queue_properties * properties,cl_int * errcode_ret,int num_calls)900 static cl_command_queue clCreateCommandQueueWithProperties_testCommandQueueFromSpecifiedContext(
901     cl_context context,
902     cl_device_id device,
903     const cl_queue_properties *properties,
904     cl_int *errcode_ret,
905     int num_calls)
906 {
907     (void)num_calls;
908     TEST_ASSERT_EQUAL_PTR(make_context(0), context);
909     TEST_ASSERT_EQUAL_PTR(make_device_id(0), device);
910     TEST_ASSERT(properties[0] == CL_QUEUE_PROPERTIES);
911     TEST_ASSERT(properties[1] == 0);
912     if (errcode_ret != nullptr)
913         *errcode_ret = CL_SUCCESS;
914     return make_command_queue(0);
915 }
916 #endif // #if CL_HPP_TARGET_OPENCL_VERSION < 200
917 
testCommandQueueFromSpecifiedContext(void)918 void testCommandQueueFromSpecifiedContext(void)
919 {
920     cl_command_queue expected = make_command_queue(0);
921     cl_context expected_context =  make_context(0);
922     cl_device_id expected_device = make_device_id(0);
923 
924     int context_refcount = 1;
925     int device_refcount = 1;
926     prepare_contextRefcounts(1, &expected_context, &context_refcount);
927     prepare_deviceRefcounts(1, &expected_device, &device_refcount);
928 
929     // This is the context we will pass in to test
930     cl::Context context = contextPool[0];
931 
932     // Assumes the context contains the fi rst device
933     clGetContextInfo_StubWithCallback(clGetContextInfo_device);
934 
935 #if CL_HPP_TARGET_OPENCL_VERSION >= 200
936     clCreateCommandQueueWithProperties_StubWithCallback(clCreateCommandQueueWithProperties_testCommandQueueFromSpecifiedContext);
937 #else // #if CL_HPP_TARGET_OPENCL_VERSION >= 200
938     clCreateCommandQueue_StubWithCallback(clCreateCommandQueue_testCommandQueueFromSpecifiedContext);
939 #endif // #if CL_HPP_TARGET_OPENCL_VERSION >= 200
940     clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
941 
942 #if CL_HPP_TARGET_OPENCL_VERSION >= 200
943     clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_2_0);
944 #else // #if CL_HPP_TARGET_OPENCL_VERSION >= 200
945     clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_1_2);
946 #endif // #if CL_HPP_TARGET_OPENCL_VERSION >= 200
947     clReleaseCommandQueue_ExpectAndReturn(expected, CL_SUCCESS);
948 
949     cl::CommandQueue queue(context);
950     TEST_ASSERT_EQUAL_PTR(expected, queue());
951 
952     // Context not destroyed yet
953     TEST_ASSERT_EQUAL(2, context_refcount);
954     // Device object destroyed at end of scope
955     TEST_ASSERT_EQUAL(1, device_refcount);
956 
957 }
958 
959 /****************************************************************************
960  * Tests for cl::Device
961  ****************************************************************************/
962 
testCopyDeviceNonNull1_1(void)963 void testCopyDeviceNonNull1_1(void)
964 {
965     clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
966     clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_1_1);
967 
968     cl::Device d0(make_device_id(0));
969     cl::Device d1(make_device_id(1));
970     d0 = d1;
971 }
972 
testCopyDeviceNonNull1_2(void)973 void testCopyDeviceNonNull1_2(void)
974 {
975     clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
976     clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_1_2);
977     clReleaseDevice_ExpectAndReturn(make_device_id(0), CL_SUCCESS);
978     clRetainDevice_ExpectAndReturn(make_device_id(1), CL_SUCCESS);
979 
980     cl::Device d0(make_device_id(0));
981     cl::Device d1(make_device_id(1));
982     d0 = d1;
983 
984     // Prevent destructor from interfering with the test
985     d0() = nullptr;
986     d1() = nullptr;
987 }
988 
testCopyDeviceFromNull1_1(void)989 void testCopyDeviceFromNull1_1(void)
990 {
991     clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
992     clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_1_1);
993     // No other calls expected
994 
995     cl::Device d(make_device_id(0));
996     d = cl::Device();
997 }
998 
testCopyDeviceFromNull1_2(void)999 void testCopyDeviceFromNull1_2(void)
1000 {
1001     clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
1002     clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_1_2);
1003     clReleaseDevice_ExpectAndReturn(make_device_id(0), CL_SUCCESS);
1004 
1005     cl::Device d(make_device_id(0));
1006     d = cl::Device();
1007 }
1008 
testCopyDeviceToNull1_1(void)1009 void testCopyDeviceToNull1_1(void)
1010 {
1011     clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
1012     clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_1_1);
1013     // No other calls expected
1014 
1015     cl::Device d0;
1016     cl::Device d1(make_device_id(0));
1017     d0 = d1;
1018 }
1019 
testCopyDeviceToNull1_2(void)1020 void testCopyDeviceToNull1_2(void)
1021 {
1022     clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
1023     clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_1_2);
1024     clRetainDevice_ExpectAndReturn(make_device_id(0), CL_SUCCESS);
1025 
1026     cl::Device d0;
1027     cl::Device d1(make_device_id(0));
1028     d0 = d1;
1029 
1030     // Prevent destructor from interfering with the test
1031     d0() = nullptr;
1032     d1() = nullptr;
1033 }
1034 
testCopyDeviceSelf(void)1035 void testCopyDeviceSelf(void)
1036 {
1037     // Use 1.2 to check the retain/release calls
1038     clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
1039     clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_1_2);
1040     clReleaseDevice_ExpectAndReturn(make_device_id(0), CL_SUCCESS);
1041     clRetainDevice_ExpectAndReturn(make_device_id(1), CL_SUCCESS);
1042 
1043     cl::Device d0(make_device_id(0));
1044     cl::Device d1(make_device_id(1));
1045     d0 = d1;
1046 
1047     // Prevent destructor from interfering with the test
1048     d0() = nullptr;
1049     d1() = nullptr;
1050 }
1051 
testAssignDeviceNull(void)1052 void testAssignDeviceNull(void)
1053 {
1054     // Any version will do here
1055     clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
1056     clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_1_2);
1057     clReleaseDevice_ExpectAndReturn(make_device_id(0), CL_SUCCESS);
1058 
1059     cl::Device d(make_device_id(0));
1060     d = (cl_device_id) nullptr;
1061 }
1062 
1063 // These tests do not use the MAKE_MOVE_TESTS helper because they need to
1064 // check whether the device is reference-countable, and to check that
1065 // the reference-countable flag is correctly moved.
testMoveAssignDeviceNonNull(void)1066 void testMoveAssignDeviceNonNull(void)
1067 {
1068 #ifdef TEST_RVALUE_REFERENCES
1069     clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
1070     clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_1_2);
1071 
1072     // Release called when trg overwritten
1073     clReleaseDevice_ExpectAndReturn(make_device_id(1), CL_SUCCESS);
1074 
1075     cl::Device src(make_device_id(0));
1076     cl::Device trg(make_device_id(1));
1077     trg = std::move(src);
1078     TEST_ASSERT_EQUAL_PTR(make_device_id(0), trg());
1079     TEST_ASSERT_NULL(src());
1080 
1081     // Prevent destructor from interfering with the test
1082     trg() = nullptr;
1083 #endif
1084 }
1085 
testMoveAssignDeviceNull(void)1086 void testMoveAssignDeviceNull(void)
1087 {
1088 #ifdef TEST_RVALUE_REFERENCES
1089     clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
1090     clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_1_2);
1091 
1092     cl::Device trg;
1093     cl::Device src(make_device_id(1));
1094     trg = std::move(src);
1095     TEST_ASSERT_EQUAL_PTR(make_device_id(1), trg());
1096     TEST_ASSERT_NULL(src());
1097 
1098     // Prevent destructor from interfering with the test
1099     trg() = nullptr;
1100 #endif
1101 }
1102 
testMoveConstructDeviceNonNull(void)1103 void testMoveConstructDeviceNonNull(void)
1104 {
1105 #ifdef TEST_RVALUE_REFERENCES
1106     clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
1107     clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_1_2);
1108 
1109     cl::Device src(make_device_id(0));
1110     cl::Device trg(std::move(src));
1111     TEST_ASSERT_EQUAL_PTR(make_device_id(0), trg());
1112     TEST_ASSERT_NULL(src());
1113 
1114     // Prevent destructor from interfering with the test
1115     trg() = nullptr;
1116 #endif
1117 }
1118 
testMoveConstructDeviceNull(void)1119 void testMoveConstructDeviceNull(void)
1120 {
1121 #ifdef TEST_RVALUE_REFERENCES
1122     cl::Device empty;
1123     cl::Device trg(std::move(empty));
1124     TEST_ASSERT_NULL(trg());
1125     TEST_ASSERT_NULL(empty());
1126 #endif
1127 }
1128 
testDestroyDevice1_1(void)1129 void testDestroyDevice1_1(void)
1130 {
1131     clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
1132     clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_1_1);
1133     // No other calls expected
1134 
1135     cl::Device d(make_device_id(0));
1136 }
1137 
testDestroyDevice1_2(void)1138 void testDestroyDevice1_2(void)
1139 {
1140     clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
1141     clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_1_2);
1142     clReleaseDevice_ExpectAndReturn(make_device_id(0), CL_SUCCESS);
1143 
1144     cl::Device d(make_device_id(0));
1145 }
1146 
clGetDeviceIDs_PlatformWithZeroDevices(cl_platform_id platform,cl_device_type device_type,cl_uint num_entries,cl_device_id * devices,cl_uint * num_devices,int num_calls)1147 static cl_int clGetDeviceIDs_PlatformWithZeroDevices(
1148     cl_platform_id  platform,
1149     cl_device_type  device_type,
1150     cl_uint  num_entries,
1151     cl_device_id  *devices,
1152     cl_uint  *num_devices,
1153     int num_calls)
1154 {
1155     (void) num_entries;
1156     (void) devices;
1157 
1158     if (num_calls == 0)
1159     {
1160         TEST_ASSERT_EQUAL_PTR(make_platform_id(0), platform);
1161         TEST_ASSERT_EQUAL(CL_DEVICE_TYPE_ALL, device_type);
1162         TEST_ASSERT_NOT_NULL(num_devices);
1163         return CL_DEVICE_NOT_FOUND;
1164     }
1165     else
1166     {
1167         TEST_FAIL_MESSAGE("clGetDeviceIDs called too many times");
1168         return CL_INVALID_VALUE;
1169     }
1170 }
1171 
testPlatformWithZeroDevices(void)1172 void testPlatformWithZeroDevices(void)
1173 {
1174     clGetDeviceIDs_StubWithCallback(clGetDeviceIDs_PlatformWithZeroDevices);
1175 
1176     cl::Platform p(make_platform_id(0));
1177     std::vector<cl::Device> devices;
1178 
1179     cl_int errCode = p.getDevices(CL_DEVICE_TYPE_ALL, &devices);
1180     TEST_ASSERT_EQUAL(CL_SUCCESS, errCode);
1181     TEST_ASSERT_EQUAL(0, devices.size());
1182 }
1183 
1184 /****************************************************************************
1185  * Tests for cl::Buffer
1186  ****************************************************************************/
1187 
1188 void testMoveAssignBufferNonNull(void);
1189 void testMoveAssignBufferNull(void);
1190 void testMoveConstructBufferNonNull(void);
1191 void testMoveConstructBufferNull(void);
MAKE_MOVE_TESTS(Buffer,make_mem,clReleaseMemObject,bufferPool)1192 MAKE_MOVE_TESTS(Buffer, make_mem, clReleaseMemObject, bufferPool)
1193 
1194 // Stub of clCreateBuffer for testBufferConstructorContextInterator
1195 // - return the first memory location
1196 
1197 static cl_mem clCreateBuffer_testBufferConstructorContextIterator(
1198     cl_context context,
1199     cl_mem_flags flags,
1200     size_t size,
1201     void *host_ptr,
1202     cl_int *errcode_ret,
1203     int num_calls)
1204 {
1205     (void) num_calls;
1206 
1207     TEST_ASSERT_EQUAL_PTR(make_context(0), context);
1208     TEST_ASSERT_EQUAL(flags, CL_MEM_READ_ONLY);
1209     TEST_ASSERT_EQUAL(sizeof(int)*1024, size);
1210     TEST_ASSERT_NULL(host_ptr);
1211     if (errcode_ret)
1212         *errcode_ret = CL_SUCCESS;
1213     return make_mem(0);
1214 }
1215 
1216 // Declare forward these functions
1217 static void * clEnqueueMapBuffer_testCopyHostToBuffer(
1218     cl_command_queue command_queue,
1219     cl_mem buffer,
1220     cl_bool blocking_map,
1221     cl_map_flags map_flags,
1222     size_t offset,
1223     size_t size,
1224     cl_uint num_events_in_wait_list,
1225     const cl_event *event_wait_list,
1226     cl_event *event,
1227     cl_int *errcode_ret,
1228     int num_calls);
1229 
1230 static cl_int clEnqueueUnmapMemObject_testCopyHostToBuffer(
1231     cl_command_queue  command_queue ,
1232     cl_mem  memobj,
1233     void  *mapped_ptr,
1234     cl_uint  num_events_in_wait_list ,
1235     const cl_event  *event_wait_list ,
1236     cl_event  *event,
1237     int num_calls);
1238 
1239 static cl_int clWaitForEvents_testCopyHostToBuffer(
1240     cl_uint num_events,
1241     const cl_event *event_list,
1242     int num_calls);
1243 
1244 static cl_int clReleaseEvent_testCopyHostToBuffer(
1245     cl_event event,
1246     int num_calls);
1247 
testBufferConstructorContextIterator(void)1248 void testBufferConstructorContextIterator(void)
1249 {
1250     cl_mem expected = make_mem(0);
1251 
1252     // Assume this context includes make_device_id(0) for stub clGetContextInfo_device
1253     cl::Context context(make_context(0));
1254 
1255     clCreateBuffer_StubWithCallback(clCreateBuffer_testBufferConstructorContextIterator);
1256     clGetContextInfo_StubWithCallback(clGetContextInfo_device);
1257     clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
1258 #if CL_HPP_TARGET_OPENCL_VERSION >= 200
1259     clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_2_0);
1260 #else // #if CL_HPP_TARGET_OPENCL_VERSION >= 200
1261     clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_1_2);
1262 #endif //#if CL_HPP_TARGET_OPENCL_VERSION >= 200
1263     clRetainDevice_ExpectAndReturn(make_device_id(0), CL_SUCCESS);
1264 
1265 #if CL_HPP_TARGET_OPENCL_VERSION >= 200
1266     clCreateCommandQueueWithProperties_StubWithCallback(clCreateCommandQueueWithProperties_testCommandQueueFromSpecifiedContext);
1267 #else // #if CL_HPP_TARGET_OPENCL_VERSION >= 200
1268     clCreateCommandQueue_StubWithCallback(clCreateCommandQueue_testCommandQueueFromSpecifiedContext);
1269 #endif //#if CL_HPP_TARGET_OPENCL_VERSION >= 200
1270     clReleaseDevice_ExpectAndReturn(make_device_id(0), CL_SUCCESS);
1271     clEnqueueMapBuffer_StubWithCallback(clEnqueueMapBuffer_testCopyHostToBuffer);
1272     clEnqueueUnmapMemObject_StubWithCallback(clEnqueueUnmapMemObject_testCopyHostToBuffer);
1273     clWaitForEvents_StubWithCallback(clWaitForEvents_testCopyHostToBuffer);
1274     clReleaseEvent_StubWithCallback(clReleaseEvent_testCopyHostToBuffer);
1275     clReleaseCommandQueue_ExpectAndReturn(make_command_queue(0), CL_SUCCESS);
1276 
1277     std::vector<int> host(1024);
1278 
1279     cl::Buffer buffer(context, host.begin(), host.end(), true);
1280 
1281     TEST_ASSERT_EQUAL_PTR(expected, buffer());
1282 
1283     // Tidy up at end of test
1284     clReleaseMemObject_ExpectAndReturn(expected, CL_SUCCESS);
1285     clReleaseContext_ExpectAndReturn(make_context(0), CL_SUCCESS);
1286 }
1287 
testBufferConstructorQueueIterator(void)1288 void testBufferConstructorQueueIterator(void)
1289 {
1290     cl_context expected_context = make_context(0);
1291     int context_refcount = 1;
1292     cl_mem expected = make_mem(0);
1293 
1294     cl::CommandQueue queue(make_command_queue(0));
1295 
1296     prepare_contextRefcounts(1, &expected_context, &context_refcount);
1297     clGetCommandQueueInfo_StubWithCallback(clGetCommandQueueInfo_context);
1298     clCreateBuffer_StubWithCallback(clCreateBuffer_testBufferConstructorContextIterator);
1299 
1300     clEnqueueMapBuffer_StubWithCallback(clEnqueueMapBuffer_testCopyHostToBuffer);
1301     clEnqueueUnmapMemObject_StubWithCallback(clEnqueueUnmapMemObject_testCopyHostToBuffer);
1302     clWaitForEvents_StubWithCallback(clWaitForEvents_testCopyHostToBuffer);
1303     clReleaseEvent_StubWithCallback(clReleaseEvent_testCopyHostToBuffer);
1304 
1305     std::vector<int> host(1024);
1306 
1307     cl::Buffer buffer(queue, host.begin(), host.end(), true);
1308 
1309     TEST_ASSERT_EQUAL_PTR(expected, buffer());
1310     TEST_ASSERT_EQUAL(1, context_refcount);
1311 
1312     // Tidy up at end of test
1313     clReleaseMemObject_ExpectAndReturn(expected, CL_SUCCESS);
1314     clReleaseCommandQueue_ExpectAndReturn(make_command_queue(0), CL_SUCCESS);
1315 }
1316 
1317 #if CL_HPP_TARGET_OPENCL_VERSION >= 300
clCreateBufferWithProperties_testBufferWithProperties(cl_context context,const cl_mem_properties * properties,cl_mem_flags flags,size_t size,void * host_ptr,cl_int * errcode_ret,int num_calls)1318 static cl_mem clCreateBufferWithProperties_testBufferWithProperties(
1319     cl_context context,
1320     const cl_mem_properties *properties,
1321     cl_mem_flags flags,
1322     size_t size,
1323     void *host_ptr,
1324     cl_int *errcode_ret,
1325     int num_calls)
1326 {
1327     TEST_ASSERT_EQUAL(0, num_calls);
1328     TEST_ASSERT_EQUAL_PTR(contextPool[0](), context);
1329     TEST_ASSERT_NOT_NULL(properties);
1330     TEST_ASSERT_EQUAL(11, *properties);
1331     TEST_ASSERT_EQUAL(0, flags);
1332     TEST_ASSERT_EQUAL(0, size);
1333     TEST_ASSERT_NULL(host_ptr);
1334     if (errcode_ret)
1335         *errcode_ret = CL_SUCCESS;
1336 
1337     return make_mem(0);
1338 }
1339 #endif //CL_HPP_TARGET_OPENCL_VERSION >= 300
1340 
testBufferWithProperties(void)1341 void testBufferWithProperties(void)
1342 {
1343 #if CL_HPP_TARGET_OPENCL_VERSION >= 300
1344     clCreateBufferWithProperties_StubWithCallback(clCreateBufferWithProperties_testBufferWithProperties);
1345 
1346     VECTOR_CLASS<cl_mem_properties> props{11};
1347     cl_int err;
1348     cl::Buffer buffer(contextPool[0], props, 0, 0, nullptr, &err);
1349 
1350     TEST_ASSERT_EQUAL_PTR(make_mem(0), buffer());
1351     TEST_ASSERT_EQUAL(CL_SUCCESS, err);
1352 
1353     // prevent destructor from interfering with the test
1354     buffer() = nullptr;
1355 #endif //CL_HPP_TARGET_OPENCL_VERSION >= 300
1356 }
1357 
1358 /****************************************************************************
1359  * Tests for cl::Image1DBuffer
1360  ****************************************************************************/
1361 
1362 /**
1363  * Stub for querying CL_IMAGE_BUFFER and returning make_mem(1).
1364  */
clGetImageInfo_testGetImageInfoBuffer(cl_mem image,cl_image_info param_name,size_t param_value_size,void * param_value,size_t * param_value_size_ret,int num_calls)1365 cl_int clGetImageInfo_testGetImageInfoBuffer(
1366     cl_mem image, cl_image_info param_name,
1367     size_t param_value_size, void *param_value,
1368     size_t *param_value_size_ret,
1369     int num_calls)
1370 {
1371     TEST_ASSERT_EQUAL(0, num_calls);
1372     TEST_ASSERT_EQUAL_PTR(make_mem(0), image);
1373     TEST_ASSERT_EQUAL_HEX(CL_IMAGE_BUFFER, param_name);
1374     TEST_ASSERT_EQUAL(sizeof(cl_mem), param_value_size);
1375 
1376     if (param_value != nullptr)
1377     {
1378         *(cl_mem *) param_value = make_mem(1);
1379     }
1380     if (param_value_size_ret != nullptr)
1381         *param_value_size_ret = sizeof(cl_mem);
1382     return CL_SUCCESS;
1383 }
1384 
testGetImageInfoBuffer(void)1385 void testGetImageInfoBuffer(void)
1386 {
1387 #if CL_HPP_TARGET_OPENCL_VERSION >= 200
1388     cl_mem expected = make_mem(1);
1389     int refcount = 1;
1390 
1391     clGetImageInfo_StubWithCallback(clGetImageInfo_testGetImageInfoBuffer);
1392     prepare_memRefcounts(1, &expected, &refcount);
1393 
1394     cl::Image1DBuffer image(make_mem(0));
1395     const cl::Buffer &buffer = image.getImageInfo<CL_IMAGE_BUFFER>();
1396     TEST_ASSERT_EQUAL_PTR(make_mem(1), buffer());
1397     // Ref count should be 2 here because buffer has not been destroyed yet
1398     TEST_ASSERT_EQUAL(2, refcount);
1399 
1400     // prevent destructor from interfering with the test
1401     image() = nullptr;
1402 #endif
1403 }
1404 
1405 /**
1406  * Stub for querying CL_IMAGE_BUFFER and returning nullptr.
1407  */
clGetImageInfo_testGetImageInfoBufferNull(cl_mem image,cl_image_info param_name,size_t param_value_size,void * param_value,size_t * param_value_size_ret,int num_calls)1408 cl_int clGetImageInfo_testGetImageInfoBufferNull(
1409     cl_mem image, cl_image_info param_name,
1410     size_t param_value_size, void *param_value,
1411     size_t *param_value_size_ret,
1412     int num_calls)
1413 {
1414     TEST_ASSERT_EQUAL(0, num_calls);
1415     TEST_ASSERT_EQUAL_PTR(make_mem(0), image);
1416     TEST_ASSERT_EQUAL_HEX(CL_IMAGE_BUFFER, param_name);
1417     TEST_ASSERT_EQUAL(sizeof(cl_mem), param_value_size);
1418 
1419     if (param_value != nullptr)
1420     {
1421         *(cl_mem *) param_value = nullptr;
1422     }
1423     if (param_value_size_ret != nullptr)
1424         *param_value_size_ret = sizeof(cl_mem);
1425     return CL_SUCCESS;
1426 }
1427 
testGetImageInfoBufferNull(void)1428 void testGetImageInfoBufferNull(void)
1429 {
1430 #if CL_HPP_TARGET_OPENCL_VERSION >= 200
1431     clGetImageInfo_StubWithCallback(clGetImageInfo_testGetImageInfoBufferNull);
1432 
1433     cl::Image2D image(make_mem(0));
1434     cl::Buffer buffer = image.getImageInfo<CL_IMAGE_BUFFER>();
1435     TEST_ASSERT_NULL(buffer());
1436 
1437     // prevent destructor from interfering with the test
1438     image() = nullptr;
1439 #endif
1440 }
1441 
testGetImageInfoBufferOverwrite(void)1442 void testGetImageInfoBufferOverwrite(void)
1443 {
1444     clGetImageInfo_StubWithCallback(clGetImageInfo_testGetImageInfoBuffer);
1445     clReleaseMemObject_ExpectAndReturn(make_mem(2), CL_SUCCESS);
1446     clRetainMemObject_ExpectAndReturn(make_mem(1), CL_SUCCESS);
1447 
1448     cl::Image2D image(make_mem(0));
1449     cl::Buffer buffer(make_mem(2));
1450     cl_int status = image.getImageInfo(CL_IMAGE_BUFFER, &buffer);
1451     TEST_ASSERT_EQUAL(CL_SUCCESS, status);
1452     TEST_ASSERT_EQUAL_PTR(make_mem(1), buffer());
1453 
1454     // prevent destructor from interfering with the test
1455     image() = nullptr;
1456     buffer() = nullptr;
1457 }
1458 
1459 /**
1460  * A stub for clCreateImage that creates an image from a buffer
1461  * passing the buffer's cl_mem straight through.
1462  */
clCreateImage_image1dbuffer(cl_context context,cl_mem_flags flags,const cl_image_format * image_format,const cl_image_desc * image_desc,void * host_ptr,cl_int * errcode_ret,int num_calls)1463 cl_mem clCreateImage_image1dbuffer(
1464     cl_context context,
1465     cl_mem_flags flags,
1466     const cl_image_format *image_format,
1467     const cl_image_desc *image_desc,
1468     void *host_ptr,
1469     cl_int *errcode_ret,
1470     int num_calls)
1471 {
1472     (void) context;
1473     (void) flags;
1474     (void) host_ptr;
1475     (void) num_calls;
1476 
1477     TEST_ASSERT_NOT_NULL(image_format);
1478     TEST_ASSERT_NOT_NULL(image_desc);
1479     TEST_ASSERT_EQUAL_HEX(CL_MEM_OBJECT_IMAGE1D_BUFFER, image_desc->image_type);
1480 
1481     if (errcode_ret != nullptr)
1482         *errcode_ret = CL_SUCCESS;
1483 
1484     // Return the passed buffer as the cl_mem
1485     return image_desc->buffer;
1486 }
1487 
testConstructImageFromBuffer(void)1488 void testConstructImageFromBuffer(void)
1489 {
1490 #if CL_HPP_TARGET_OPENCL_VERSION >= 120
1491     const size_t width = 64;
1492     clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
1493     clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_1_2);
1494     clCreateImage_StubWithCallback(clCreateImage_image1dbuffer);
1495     clReleaseMemObject_ExpectAndReturn(make_mem(0), CL_SUCCESS);
1496     clReleaseContext_ExpectAndReturn(make_context(0), CL_SUCCESS);
1497 
1498     cl::Context context(make_context(0));
1499     cl::Buffer buffer(make_mem(0));
1500     cl::Image1DBuffer image(
1501         context,
1502         CL_MEM_READ_ONLY,
1503         cl::ImageFormat(CL_R, CL_SIGNED_INT32),
1504         width,
1505         buffer);
1506 
1507     // Check that returned buffer matches the original
1508     TEST_ASSERT_EQUAL_PTR(buffer(), image());
1509 
1510     buffer() = nullptr;
1511 #endif
1512 }
1513 
1514 /****************************************************************************
1515  * Tests for cl::Image2D
1516  ****************************************************************************/
1517 
1518 void testMoveAssignImage2DNonNull(void);
1519 void testMoveAssignImage2DNull(void);
1520 void testMoveConstructImage2DNonNull(void);
1521 void testMoveConstructImage2DNull(void);
MAKE_MOVE_TESTS(Image2D,make_mem,clReleaseMemObject,image2DPool)1522 MAKE_MOVE_TESTS(Image2D, make_mem, clReleaseMemObject, image2DPool)
1523 
1524 #ifdef CL_USE_DEPRECATED_OPENCL_1_1_APIS
1525 static cl_mem clCreateImage2D_testCreateImage2D_1_1(
1526     cl_context context,
1527     cl_mem_flags flags,
1528     const cl_image_format *image_format,
1529     size_t image_width,
1530     size_t image_height,
1531     size_t image_row_pitch,
1532     void *host_ptr,
1533     cl_int *errcode_ret,
1534     int num_calls)
1535 {
1536     TEST_ASSERT_EQUAL(0, num_calls);
1537     TEST_ASSERT_EQUAL_PTR(make_context(0), context);
1538     TEST_ASSERT_EQUAL_HEX(CL_MEM_READ_WRITE, flags);
1539 
1540     TEST_ASSERT_NOT_NULL(image_format);
1541     TEST_ASSERT_EQUAL_HEX(CL_R, image_format->image_channel_order);
1542     TEST_ASSERT_EQUAL_HEX(CL_FLOAT, image_format->image_channel_data_type);
1543 
1544     TEST_ASSERT_EQUAL(64, image_width);
1545     TEST_ASSERT_EQUAL(32, image_height);
1546     TEST_ASSERT_EQUAL(256, image_row_pitch);
1547     TEST_ASSERT_NULL(host_ptr);
1548 
1549     if (errcode_ret != nullptr)
1550         *errcode_ret = CL_SUCCESS;
1551     return make_mem(0);
1552 }
1553 #endif
1554 
testCreateImage2D_1_1(void)1555 void testCreateImage2D_1_1(void)
1556 {
1557 #ifdef CL_USE_DEPRECATED_OPENCL_1_1_APIS
1558     clGetContextInfo_StubWithCallback(clGetContextInfo_device);
1559     clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
1560     clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_1_1);
1561     clCreateImage2D_StubWithCallback(clCreateImage2D_testCreateImage2D_1_1);
1562 
1563     cl_int err;
1564     cl::Context context;
1565     context() = make_context(0);
1566     cl::Image2D image(
1567         context, CL_MEM_READ_WRITE,
1568         cl::ImageFormat(CL_R, CL_FLOAT), 64, 32, 256, nullptr, &err);
1569 
1570     TEST_ASSERT_EQUAL(CL_SUCCESS, err);
1571     TEST_ASSERT_EQUAL_PTR(make_mem(0), image());
1572 
1573     context() = nullptr;
1574     image() = nullptr;
1575 #endif
1576 }
1577 
clCreateImage_testCreateImage2D_1_2(cl_context context,cl_mem_flags flags,const cl_image_format * image_format,const cl_image_desc * image_desc,void * host_ptr,cl_int * errcode_ret,int num_calls)1578 static cl_mem clCreateImage_testCreateImage2D_1_2(
1579     cl_context context,
1580     cl_mem_flags flags,
1581     const cl_image_format *image_format,
1582     const cl_image_desc *image_desc,
1583     void *host_ptr,
1584     cl_int *errcode_ret,
1585     int num_calls)
1586 {
1587     TEST_ASSERT_EQUAL(0, num_calls);
1588     TEST_ASSERT_EQUAL_PTR(make_context(0), context);
1589     TEST_ASSERT_EQUAL_HEX(CL_MEM_READ_WRITE, flags);
1590 
1591     TEST_ASSERT_NOT_NULL(image_format);
1592     TEST_ASSERT_EQUAL_HEX(CL_R, image_format->image_channel_order);
1593     TEST_ASSERT_EQUAL_HEX(CL_FLOAT, image_format->image_channel_data_type);
1594 
1595     TEST_ASSERT_NOT_NULL(image_desc);
1596     TEST_ASSERT_EQUAL_HEX(CL_MEM_OBJECT_IMAGE2D, image_desc->image_type);
1597     TEST_ASSERT_EQUAL(64, image_desc->image_width);
1598     TEST_ASSERT_EQUAL(32, image_desc->image_height);
1599     TEST_ASSERT_EQUAL(256, image_desc->image_row_pitch);
1600     TEST_ASSERT_EQUAL(0, image_desc->num_mip_levels);
1601     TEST_ASSERT_EQUAL(0, image_desc->num_samples);
1602     TEST_ASSERT_NULL(image_desc->buffer);
1603 
1604     TEST_ASSERT_NULL(host_ptr);
1605 
1606     if (errcode_ret != nullptr)
1607         *errcode_ret = CL_SUCCESS;
1608     return make_mem(0);
1609 }
1610 
testCreateImage2D_1_2(void)1611 void testCreateImage2D_1_2(void)
1612 {
1613     clGetContextInfo_StubWithCallback(clGetContextInfo_device);
1614     clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
1615     clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_1_2);
1616     clCreateImage_StubWithCallback(clCreateImage_testCreateImage2D_1_2);
1617 
1618     cl_int err;
1619     cl::Context context;
1620     context() = make_context(0);
1621     cl::Image2D image(
1622         context, CL_MEM_READ_WRITE,
1623         cl::ImageFormat(CL_R, CL_FLOAT), 64, 32, 256, nullptr, &err);
1624 
1625     TEST_ASSERT_EQUAL(CL_SUCCESS, err);
1626     TEST_ASSERT_EQUAL_PTR(make_mem(0), image());
1627 
1628     context() = nullptr;
1629     image() = nullptr;
1630 }
1631 
1632 /****************************************************************************
1633  * Tests for cl::Image3D
1634  ****************************************************************************/
1635 
1636 void testMoveAssignImage3DNonNull(void);
1637 void testMoveAssignImage3DNull(void);
1638 void testMoveConstructImage3DNonNull(void);
1639 void testMoveConstructImage3DNull(void);
MAKE_MOVE_TESTS(Image3D,make_mem,clReleaseMemObject,image3DPool)1640 MAKE_MOVE_TESTS(Image3D, make_mem, clReleaseMemObject, image3DPool)
1641 
1642 #ifdef CL_USE_DEPRECATED_OPENCL_1_1_APIS
1643 static cl_mem clCreateImage3D_testCreateImage3D_1_1(
1644     cl_context context,
1645     cl_mem_flags flags,
1646     const cl_image_format *image_format,
1647     size_t image_width,
1648     size_t image_height,
1649     size_t image_depth,
1650     size_t image_row_pitch,
1651     size_t image_slice_pitch,
1652     void *host_ptr,
1653     cl_int *errcode_ret,
1654     int num_calls)
1655 {
1656     TEST_ASSERT_EQUAL(0, num_calls);
1657     TEST_ASSERT_EQUAL_PTR(make_context(0), context);
1658     TEST_ASSERT_EQUAL_HEX(CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, flags);
1659 
1660     TEST_ASSERT_NOT_NULL(image_format);
1661     TEST_ASSERT_EQUAL_HEX(CL_R, image_format->image_channel_order);
1662     TEST_ASSERT_EQUAL_HEX(CL_FLOAT, image_format->image_channel_data_type);
1663 
1664     TEST_ASSERT_EQUAL(64, image_width);
1665     TEST_ASSERT_EQUAL(32, image_height);
1666     TEST_ASSERT_EQUAL(16, image_depth);
1667     TEST_ASSERT_EQUAL(256, image_row_pitch);
1668     TEST_ASSERT_EQUAL(65536, image_slice_pitch);
1669     TEST_ASSERT_EQUAL_PTR((void *)(size_t)0xdeadbeef, host_ptr);
1670 
1671     if (errcode_ret != nullptr)
1672         *errcode_ret = CL_SUCCESS;
1673     return make_mem(0);
1674 }
1675 #endif
1676 
testCreateImage3D_1_1(void)1677 void testCreateImage3D_1_1(void)
1678 {
1679 #ifdef CL_USE_DEPRECATED_OPENCL_1_1_APIS
1680     clGetContextInfo_StubWithCallback(clGetContextInfo_device);
1681     clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
1682     clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_1_1);
1683     clCreateImage3D_StubWithCallback(clCreateImage3D_testCreateImage3D_1_1);
1684 
1685     cl_int err;
1686     cl::Context context;
1687     context() = make_context(0);
1688     cl::Image3D image(
1689         context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,
1690         cl::ImageFormat(CL_R, CL_FLOAT), 64, 32, 16, 256, 65536, (void *)(size_t)0xdeadbeef, &err);
1691 
1692     TEST_ASSERT_EQUAL(CL_SUCCESS, err);
1693     TEST_ASSERT_EQUAL_PTR(make_mem(0), image());
1694 
1695     context() = nullptr;
1696     image() = nullptr;
1697 #endif
1698 }
1699 
clCreateImage_testCreateImage3D_1_2(cl_context context,cl_mem_flags flags,const cl_image_format * image_format,const cl_image_desc * image_desc,void * host_ptr,cl_int * errcode_ret,int num_calls)1700 static cl_mem clCreateImage_testCreateImage3D_1_2(
1701     cl_context context,
1702     cl_mem_flags flags,
1703     const cl_image_format *image_format,
1704     const cl_image_desc *image_desc,
1705     void *host_ptr,
1706     cl_int *errcode_ret,
1707     int num_calls)
1708 {
1709     TEST_ASSERT_EQUAL(0, num_calls);
1710     TEST_ASSERT_EQUAL_PTR(make_context(0), context);
1711     TEST_ASSERT_EQUAL_HEX(CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, flags);
1712 
1713     TEST_ASSERT_NOT_NULL(image_format);
1714     TEST_ASSERT_EQUAL_HEX(CL_R, image_format->image_channel_order);
1715     TEST_ASSERT_EQUAL_HEX(CL_FLOAT, image_format->image_channel_data_type);
1716 
1717     TEST_ASSERT_NOT_NULL(image_desc);
1718     TEST_ASSERT_EQUAL_HEX(CL_MEM_OBJECT_IMAGE3D, image_desc->image_type);
1719     TEST_ASSERT_EQUAL(64, image_desc->image_width);
1720     TEST_ASSERT_EQUAL(32, image_desc->image_height);
1721     TEST_ASSERT_EQUAL(16, image_desc->image_depth);
1722     TEST_ASSERT_EQUAL(256, image_desc->image_row_pitch);
1723     TEST_ASSERT_EQUAL(65536, image_desc->image_slice_pitch);
1724     TEST_ASSERT_EQUAL(0, image_desc->num_mip_levels);
1725     TEST_ASSERT_EQUAL(0, image_desc->num_samples);
1726     TEST_ASSERT_NULL(image_desc->buffer);
1727 
1728     TEST_ASSERT_EQUAL_PTR((void *)(size_t)0xdeadbeef, host_ptr);
1729 
1730     if (errcode_ret != nullptr)
1731         *errcode_ret = CL_SUCCESS;
1732     return make_mem(0);
1733 }
1734 
testCreateImage3D_1_2(void)1735 void testCreateImage3D_1_2(void)
1736 {
1737     clGetContextInfo_StubWithCallback(clGetContextInfo_device);
1738     clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
1739     clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_1_2);
1740     clCreateImage_StubWithCallback(clCreateImage_testCreateImage3D_1_2);
1741 
1742     cl_int err;
1743     cl::Context context;
1744     context() = make_context(0);
1745     cl::Image3D image(
1746         context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,
1747         cl::ImageFormat(CL_R, CL_FLOAT), 64, 32, 16, 256, 65536, (void *)(size_t)0xdeadbeef, &err);
1748 
1749     TEST_ASSERT_EQUAL(CL_SUCCESS, err);
1750     TEST_ASSERT_EQUAL_PTR(make_mem(0), image());
1751 
1752     context() = nullptr;
1753     image() = nullptr;
1754 }
1755 
1756 /****************************************************************************
1757  * Tests for cl::Kernel
1758  ****************************************************************************/
1759 void testMoveAssignKernelNonNull(void);
1760 void testMoveAssignKernelNull(void);
1761 void testMoveConstructKernelNonNull(void);
1762 void testMoveConstructKernelNull(void);
1763 MAKE_MOVE_TESTS(Kernel, make_kernel, clReleaseKernel, kernelPool)
1764 
1765 static cl_int scalarArg;
1766 static cl_int3 vectorArg;
1767 
testKernelSetArgScalar(void)1768 void testKernelSetArgScalar(void)
1769 {
1770     scalarArg = 0xcafebabe;
1771     clSetKernelArg_ExpectAndReturn(make_kernel(0), 3, 4, &scalarArg, CL_SUCCESS);
1772     kernelPool[0].setArg(3, scalarArg);
1773 }
1774 
testKernelSetArgVector(void)1775 void testKernelSetArgVector(void)
1776 {
1777     vectorArg.s[0] = 0x12345678;
1778     vectorArg.s[1] = 0x23456789;
1779     vectorArg.s[2] = 0x87654321;
1780     clSetKernelArg_ExpectAndReturn(make_kernel(0), 2, 16, &vectorArg, CL_SUCCESS);
1781     kernelPool[0].setArg(2, vectorArg);
1782 }
1783 
testKernelSetArgMem(void)1784 void testKernelSetArgMem(void)
1785 {
1786     clSetKernelArg_ExpectAndReturn(make_kernel(0), 1, sizeof(cl_mem), &bufferPool[1](), CL_SUCCESS);
1787     kernelPool[0].setArg(1, bufferPool[1]);
1788 }
1789 
testKernelSetArgLocal(void)1790 void testKernelSetArgLocal(void)
1791 {
1792     clSetKernelArg_ExpectAndReturn(make_kernel(0), 2, 123, nullptr, CL_SUCCESS);
1793     kernelPool[0].setArg(2, cl::Local(123));
1794 }
1795 
testKernelSetArgBySetKernelArgSVMPointerWithUniquePtrType(void)1796 void testKernelSetArgBySetKernelArgSVMPointerWithUniquePtrType(void)
1797 {
1798 #if CL_HPP_TARGET_OPENCL_VERSION >= 200
1799     std::unique_ptr<int> buffer(new int(1000));
1800     clSetKernelArgSVMPointer_ExpectAndReturn(make_kernel(0), 1, buffer.get(), CL_SUCCESS);
1801     TEST_ASSERT_EQUAL(kernelPool[0].setArg(1, buffer), CL_SUCCESS);
1802 #endif
1803 }
1804 
testKernelSetArgBySetKernelArgSVMPointerWithVectorType(void)1805 void testKernelSetArgBySetKernelArgSVMPointerWithVectorType(void)
1806 {
1807 #if CL_HPP_TARGET_OPENCL_VERSION >= 200
1808     VECTOR_CLASS<int> vec(1000);
1809     clSetKernelArgSVMPointer_ExpectAndReturn(make_kernel(1), 2, vec.data(), CL_SUCCESS);
1810     TEST_ASSERT_EQUAL(kernelPool[1].setArg(2, vec), CL_SUCCESS);
1811 #endif
1812 }
1813 
testKernelSetArgBySetKernelArgSVMPointerWithPointerType(void)1814 void testKernelSetArgBySetKernelArgSVMPointerWithPointerType(void)
1815 {
1816 #if CL_HPP_TARGET_OPENCL_VERSION >= 200
1817     cl_mem *memory = &bufferPool[1]();
1818     clSetKernelArgSVMPointer_ExpectAndReturn(make_kernel(2), 3, &bufferPool[1](), CL_SUCCESS);
1819     TEST_ASSERT_EQUAL(kernelPool[2].setArg(3, memory), CL_SUCCESS);
1820 #endif
1821 }
1822 
testKernelSetExecInfo(void)1823 void testKernelSetExecInfo(void)
1824 {
1825 #if CL_HPP_TARGET_OPENCL_VERSION >= 200
1826     cl_bool val = CL_TRUE;
1827     // Using CL_KERNEL_EXEC_INFO_SVM_FINE_GRAIN_SYSTEM in the tests since it's
1828     // defined by the core spec but this function is particularly useful for
1829     // vendor extensions.
1830     clSetKernelExecInfo_ExpectAndReturn(make_kernel(0),
1831                                         CL_KERNEL_EXEC_INFO_SVM_FINE_GRAIN_SYSTEM,
1832                                         sizeof(cl_bool), &val, CL_SUCCESS);
1833     kernelPool[0].setExecInfo(CL_KERNEL_EXEC_INFO_SVM_FINE_GRAIN_SYSTEM, val);
1834     // Also test the typesafe version
1835     clSetKernelExecInfo_ExpectAndReturn(make_kernel(0),
1836                                         CL_KERNEL_EXEC_INFO_SVM_FINE_GRAIN_SYSTEM,
1837                                         sizeof(cl_bool), &val, CL_SUCCESS);
1838     kernelPool[0].setExecInfo<CL_KERNEL_EXEC_INFO_SVM_FINE_GRAIN_SYSTEM>(val);
1839 #endif
1840 }
1841 
1842 
clSetKernelExecInfo_setSVMPointers(cl_kernel kernel,cl_kernel_exec_info param_name,size_t param_value_size,const void * param_value,int cmock_num_calls)1843 cl_int clSetKernelExecInfo_setSVMPointers(cl_kernel kernel,
1844                                 cl_kernel_exec_info param_name,
1845                                 size_t param_value_size,
1846                                 const void *param_value, int cmock_num_calls)
1847 {
1848     (void) cmock_num_calls;
1849 
1850     TEST_ASSERT_EQUAL_PTR(make_kernel(0), kernel);
1851     TEST_ASSERT_EQUAL_HEX(CL_KERNEL_EXEC_INFO_SVM_PTRS, param_name);
1852     TEST_ASSERT(param_value_size == 2 * sizeof(void *));
1853 
1854     int** arr = (int **)param_value;
1855     TEST_ASSERT_EQUAL_PTR(arr[0], 0xaabbccdd);
1856     TEST_ASSERT_EQUAL_PTR(arr[1], 0xddccbbaa);
1857 
1858     return CL_SUCCESS;
1859 }
1860 
testKernelSetSVMPointers(void)1861 void testKernelSetSVMPointers(void)
1862 {
1863 #if CL_HPP_TARGET_OPENCL_VERSION >= 200
1864     clSetKernelExecInfo_StubWithCallback(clSetKernelExecInfo_setSVMPointers);
1865 
1866     cl::vector<void *> vec = { (void *)(size_t)0xaabbccdd, (void *)(size_t)0xddccbbaa };
1867     cl_int ret = kernelPool[0].setSVMPointers(vec);
1868 
1869     cl_int expected = CL_SUCCESS;
1870     TEST_ASSERT_EQUAL_HEX(expected, ret);
1871 #endif
1872 }
clSetKernelExecInfo_EnableFineGrainedSystemSVM(cl_kernel kernel,cl_kernel_exec_info param_name,size_t param_value_size,const void * param_value,int cmock_num_calls)1873 cl_int clSetKernelExecInfo_EnableFineGrainedSystemSVM(cl_kernel kernel,
1874                                  cl_kernel_exec_info param_name,
1875                                  size_t param_value_size,
1876                                  const void *param_value, int cmock_num_calls)
1877 {
1878     (void) cmock_num_calls;
1879 
1880     TEST_ASSERT_EQUAL_PTR(make_kernel(0), kernel);
1881     TEST_ASSERT_EQUAL_HEX(*(cl_bool*)param_value, CL_FALSE);
1882     TEST_ASSERT_EQUAL_HEX(CL_KERNEL_EXEC_INFO_SVM_FINE_GRAIN_SYSTEM,param_name);
1883     TEST_ASSERT(param_value_size == sizeof(cl_bool));
1884 
1885     return CL_SUCCESS;
1886 }
testKernelEnableFineGrainedSystemSVM(void)1887 void testKernelEnableFineGrainedSystemSVM(void)
1888 {
1889 #if CL_HPP_TARGET_OPENCL_VERSION >= 200
1890     clSetKernelExecInfo_StubWithCallback(clSetKernelExecInfo_EnableFineGrainedSystemSVM);
1891     bool svmEnabled = false;
1892     cl_int ret = kernelPool[0].enableFineGrainedSystemSVM(svmEnabled);
1893     cl_int expected = CL_SUCCESS;
1894     TEST_ASSERT_EQUAL_HEX(expected, ret);
1895 #endif
1896 }
1897 
1898 /****************************************************************************
1899  * Tests for cl::copy
1900  ****************************************************************************/
1901 
1902 // This method should allocate some host accesible memory
1903 // so we must do this ourselves
1904 void *some_host_memory;
1905 
clEnqueueMapBuffer_testCopyHostToBuffer(cl_command_queue command_queue,cl_mem buffer,cl_bool blocking_map,cl_map_flags map_flags,size_t offset,size_t size,cl_uint num_events_in_wait_list,const cl_event * event_wait_list,cl_event * event,cl_int * errcode_ret,int num_calls)1906 static void * clEnqueueMapBuffer_testCopyHostToBuffer(
1907     cl_command_queue command_queue,
1908     cl_mem buffer,
1909     cl_bool blocking_map,
1910     cl_map_flags map_flags,
1911     size_t offset,
1912     size_t size,
1913     cl_uint num_events_in_wait_list,
1914     const cl_event *event_wait_list,
1915     cl_event *event,
1916     cl_int *errcode_ret,
1917     int num_calls)
1918 {
1919     (void) offset;
1920     (void) num_events_in_wait_list;
1921     (void) event_wait_list;
1922     (void) num_calls;
1923 
1924     TEST_ASSERT_EQUAL_PTR(make_command_queue(0), command_queue);
1925     TEST_ASSERT_EQUAL_PTR(make_mem(0), buffer);
1926     TEST_ASSERT_EQUAL(CL_TRUE, blocking_map);
1927     TEST_ASSERT_EQUAL(CL_MAP_WRITE, map_flags);
1928     TEST_ASSERT_EQUAL(sizeof(int)*1024, size);
1929 
1930     some_host_memory = malloc(sizeof(int) * 1024);
1931 
1932     // Set the return event
1933     if (event)
1934         *event = nullptr;
1935 
1936     // Set the return error code
1937     if (errcode_ret)
1938         *errcode_ret = CL_SUCCESS;
1939 
1940     return some_host_memory;
1941 }
1942 
clEnqueueUnmapMemObject_testCopyHostToBuffer(cl_command_queue command_queue,cl_mem memobj,void * mapped_ptr,cl_uint num_events_in_wait_list,const cl_event * event_wait_list,cl_event * event,int num_calls)1943 static cl_int clEnqueueUnmapMemObject_testCopyHostToBuffer(
1944     cl_command_queue  command_queue ,
1945     cl_mem  memobj,
1946     void  *mapped_ptr,
1947     cl_uint  num_events_in_wait_list ,
1948     const cl_event  *event_wait_list ,
1949     cl_event  *event,
1950     int num_calls)
1951 {
1952     (void) num_events_in_wait_list;
1953     (void) event_wait_list;
1954     (void) num_calls;
1955 
1956     TEST_ASSERT_EQUAL_PTR(make_command_queue(0), command_queue);
1957     TEST_ASSERT_EQUAL_PTR(make_mem(0), memobj);
1958     TEST_ASSERT_EQUAL_PTR(some_host_memory, mapped_ptr);
1959     TEST_ASSERT_NOT_NULL(event);
1960     return CL_SUCCESS;
1961 }
1962 
clWaitForEvents_testCopyHostToBuffer(cl_uint num_events,const cl_event * event_list,int num_calls)1963 static cl_int clWaitForEvents_testCopyHostToBuffer(
1964     cl_uint num_events,
1965     const cl_event *event_list,
1966     int num_calls)
1967 {
1968     (void) num_calls;
1969 
1970     TEST_ASSERT_NOT_NULL(event_list);
1971     TEST_ASSERT_EQUAL(1, num_events);
1972     return CL_SUCCESS;
1973 }
1974 
clReleaseEvent_testCopyHostToBuffer(cl_event event,int num_calls)1975 static cl_int clReleaseEvent_testCopyHostToBuffer(
1976     cl_event event,
1977     int num_calls)
1978 {
1979     (void) num_calls;
1980 
1981     TEST_ASSERT_NOT_NULL(event);
1982     return CL_SUCCESS;
1983 }
1984 
testCopyHostToBuffer(void)1985 void testCopyHostToBuffer(void)
1986 {
1987     cl_context context_expect = make_context(0);
1988     int context_refcount = 1;
1989     prepare_contextRefcounts(1, &context_expect, &context_refcount);
1990     cl::Context context = contextPool[0];
1991 
1992     cl_mem mem_expect = make_mem(0);
1993     int mem_refcount = 1;
1994     prepare_memRefcounts(1, &mem_expect, &mem_refcount);
1995     cl::Buffer buffer(make_mem(0));
1996 
1997     cl_command_queue queue_expect = make_command_queue(0);
1998     cl::CommandQueue queue(queue_expect);
1999     clReleaseCommandQueue_ExpectAndReturn(queue_expect, CL_SUCCESS);
2000 
2001     // Returns the pointer to host memory
2002     clEnqueueMapBuffer_StubWithCallback(clEnqueueMapBuffer_testCopyHostToBuffer);
2003     clEnqueueUnmapMemObject_StubWithCallback(clEnqueueUnmapMemObject_testCopyHostToBuffer);
2004 
2005     clWaitForEvents_StubWithCallback(clWaitForEvents_testCopyHostToBuffer);
2006     clReleaseEvent_StubWithCallback(clReleaseEvent_testCopyHostToBuffer);
2007 
2008     std::vector<int> host(1024);
2009     for (int i = 0; i < 1024; i++)
2010         host[i] = i;
2011 
2012     cl::copy(queue, host.begin(), host.end(), buffer);
2013 
2014     // Check that the memory was copied to some_host_memory
2015     TEST_ASSERT_EQUAL_MEMORY(&host[0], some_host_memory, sizeof(int) * 1024);
2016 
2017     free(some_host_memory);
2018 
2019 }
2020 
2021 /****************************************************************************
2022 * Tests for building Programs
2023 ****************************************************************************/
2024 
clGetDeviceInfo_testGetBuildInfo(cl_device_id device,cl_device_info param_name,size_t param_value_size,void * param_value,size_t * param_value_size_ret,int num_calls)2025 static cl_int clGetDeviceInfo_testGetBuildInfo(
2026     cl_device_id device,
2027     cl_device_info param_name,
2028     size_t param_value_size,
2029     void *param_value,
2030     size_t *param_value_size_ret,
2031     int num_calls)
2032 {
2033     (void) device;
2034     (void) num_calls;
2035 
2036     TEST_ASSERT_EQUAL(param_name, CL_DEVICE_PLATFORM);
2037     TEST_ASSERT_EQUAL(param_value_size, sizeof(cl_platform_id));
2038     TEST_ASSERT_NOT_EQUAL(param_value, nullptr);
2039     TEST_ASSERT_EQUAL(param_value_size_ret, nullptr);
2040     cl_platform_id temp = make_platform_id(0);
2041     memcpy(param_value, &temp, sizeof(cl_platform_id));
2042     return CL_SUCCESS;
2043 }
2044 
2045 
clGetProgramBuildInfo_testGetBuildInfo(cl_program program,cl_device_id device,cl_program_build_info param_name,size_t param_value_size,void * param_value,size_t * param_value_size_ret,int num_calls)2046 static  cl_int clGetProgramBuildInfo_testGetBuildInfo(
2047     cl_program program,
2048     cl_device_id device,
2049     cl_program_build_info param_name,
2050     size_t param_value_size,
2051     void *param_value,
2052     size_t *param_value_size_ret,
2053     int num_calls)
2054 {
2055     (void) program;
2056     (void) device;
2057     (void) num_calls;
2058 
2059     TEST_ASSERT_EQUAL(param_name, CL_PROGRAM_BUILD_LOG);
2060 
2061     const char returnString[] =
2062         "This is the string returned by the build info function.";
2063     if (param_value) {
2064         ::size_t returnSize = param_value_size;
2065         if (sizeof(returnString) < returnSize) {
2066             returnSize = sizeof(returnString);
2067         }
2068         memcpy(param_value, returnString, returnSize);
2069     }
2070     else {
2071         if (param_value_size_ret) {
2072             *param_value_size_ret = sizeof(returnString);
2073         }
2074     }
2075 
2076     return CL_SUCCESS;
2077 }
2078 
testGetBuildInfo(void)2079 void testGetBuildInfo(void)
2080 {
2081     cl_device_id fakeDevice = make_device_id(0);
2082     clGetDeviceInfo_ExpectAndReturn(fakeDevice, CL_DEVICE_PLATFORM, sizeof(cl_platform_id), nullptr, nullptr, CL_SUCCESS);
2083     clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_testGetBuildInfo);
2084     clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_1_2);
2085     clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_1_2);
2086     clGetProgramBuildInfo_StubWithCallback(clGetProgramBuildInfo_testGetBuildInfo);
2087     clGetProgramBuildInfo_StubWithCallback(clGetProgramBuildInfo_testGetBuildInfo);
2088 
2089     cl::Program prog(make_program(0));
2090     cl::Device dev(fakeDevice);
2091 
2092     cl_int err;
2093     std::string log = prog.getBuildInfo<CL_PROGRAM_BUILD_LOG>(dev, &err);
2094 
2095     prog() = nullptr;
2096     dev() = nullptr;
2097 }
2098 
clBuildProgram_testBuildProgram(cl_program program,cl_uint num_devices,const cl_device_id * device_list,const char * options,void (CL_CALLBACK * pfn_notify)(cl_program program,void * user_data),void * user_data,int num_calls)2099 static cl_int clBuildProgram_testBuildProgram(
2100     cl_program           program,
2101     cl_uint              num_devices,
2102     const cl_device_id * device_list,
2103     const char *         options,
2104     void (CL_CALLBACK *  pfn_notify)(cl_program program, void * user_data),
2105     void *               user_data,
2106     int num_calls)
2107 {
2108     (void) num_calls;
2109 
2110     TEST_ASSERT_EQUAL(program, make_program(0));
2111     TEST_ASSERT_NOT_EQUAL(num_devices, 0);
2112     TEST_ASSERT_NOT_EQUAL(device_list, nullptr);
2113     TEST_ASSERT_EQUAL(options, nullptr);
2114     TEST_ASSERT_EQUAL(pfn_notify, nullptr);
2115     TEST_ASSERT_EQUAL(user_data, nullptr);
2116 
2117     for (cl_uint i = 0; i < num_devices; i++) {
2118         TEST_ASSERT_EQUAL(device_list[i], make_device_id(i));
2119     }
2120 
2121     return CL_SUCCESS;
2122 }
2123 
testBuildProgramSingleDevice(void)2124 void testBuildProgramSingleDevice(void)
2125 {
2126     cl_program program = make_program(0);
2127     cl_device_id device_id = make_device_id(0);
2128 
2129     // Creating a device queries the platform version:
2130     clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
2131     clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_1_2);
2132 
2133     clBuildProgram_StubWithCallback(clBuildProgram_testBuildProgram);
2134 
2135     // Building the program queries the program build log:
2136     clRetainDevice_ExpectAndReturn(make_device_id(0), CL_SUCCESS);
2137     clGetProgramBuildInfo_StubWithCallback(clGetProgramBuildInfo_testGetBuildInfo);
2138     clGetProgramBuildInfo_StubWithCallback(clGetProgramBuildInfo_testGetBuildInfo);
2139     clReleaseDevice_ExpectAndReturn(make_device_id(0), CL_SUCCESS);
2140     clReleaseDevice_ExpectAndReturn(make_device_id(0), CL_SUCCESS);
2141 
2142     clReleaseProgram_ExpectAndReturn(program, CL_SUCCESS);
2143 
2144     cl::Program prog(program);
2145     cl::Device dev(device_id);
2146 
2147     cl_int errcode = prog.build(dev);
2148 
2149     TEST_ASSERT_EQUAL(errcode, CL_SUCCESS);
2150 }
2151 
2152 /**
2153 * Stub implementation of clGetCommandQueueInfo that returns first one image then none
2154 */
clGetSupportedImageFormats_testGetSupportedImageFormats(cl_context context,cl_mem_flags flags,cl_mem_object_type image_type,cl_uint num_entries,cl_image_format * image_formats,cl_uint * num_image_formats,int num_calls)2155 static cl_int clGetSupportedImageFormats_testGetSupportedImageFormats(
2156     cl_context context,
2157     cl_mem_flags flags,
2158     cl_mem_object_type image_type,
2159     cl_uint num_entries,
2160     cl_image_format *image_formats,
2161     cl_uint *num_image_formats,
2162     int num_calls)
2163 {
2164     (void) context;
2165     (void) flags;
2166     (void) image_type;
2167 
2168     // Catch failure case that causes error in bugzilla 13355:
2169     // returns CL_INVALID_VALUE if flags or image_type are not valid,
2170     // or if num_entries is 0 and image_formats is not nullptr.
2171     if (num_entries == 0 && image_formats != nullptr) {
2172         return CL_INVALID_VALUE;
2173     }
2174     if (num_entries == 0)  {
2175         // If num_entries was 0 this is the query for number
2176         if (num_image_formats) {
2177             if (num_calls == 0) {
2178                 *num_image_formats = 1;
2179             }
2180             else {
2181                 *num_image_formats = 0;
2182             }
2183         }
2184     }
2185     else {
2186         // Should return something
2187         TEST_ASSERT_NOT_NULL(image_formats);
2188 
2189         // For first call we should return one format here
2190         if (num_calls == 1) {
2191             TEST_ASSERT_EQUAL(num_entries, 1);
2192             image_formats[0] = cl::ImageFormat(CL_RGB, CL_FLOAT);
2193         }
2194     }
2195 
2196     return CL_SUCCESS;
2197 }
2198 
testGetSupportedImageFormats(void)2199 void testGetSupportedImageFormats(void)
2200 {
2201     cl_context ctx_cl = make_context(0);
2202 
2203     clGetSupportedImageFormats_StubWithCallback(clGetSupportedImageFormats_testGetSupportedImageFormats);
2204     clGetSupportedImageFormats_StubWithCallback(clGetSupportedImageFormats_testGetSupportedImageFormats);
2205     clReleaseContext_ExpectAndReturn(make_context(0), CL_SUCCESS);
2206 
2207     cl::Context ctx(ctx_cl);
2208     std::vector<cl::ImageFormat> formats;
2209     cl_int ret = CL_SUCCESS;
2210 
2211     ret = ctx.getSupportedImageFormats(
2212         CL_MEM_READ_WRITE,
2213         CL_MEM_OBJECT_IMAGE2D,
2214         &formats);
2215     TEST_ASSERT_EQUAL(ret, CL_SUCCESS);
2216     TEST_ASSERT_EQUAL(formats.size(), 1);
2217     ret = ctx.getSupportedImageFormats(
2218         CL_MEM_READ_WRITE,
2219         CL_MEM_OBJECT_IMAGE2D,
2220         &formats);
2221     TEST_ASSERT_EQUAL(formats.size(), 0);
2222     TEST_ASSERT_EQUAL(ret, CL_SUCCESS);
2223 }
2224 
testCreateSubDevice(void)2225 void testCreateSubDevice(void)
2226 {
2227     // TODO
2228 
2229 }
2230 
testGetContextInfoDevices(void)2231 void testGetContextInfoDevices(void)
2232 {
2233     // TODO
2234 }
2235 
2236 #if CL_HPP_TARGET_OPENCL_VERSION >= 200
clCreateImage_testCreateImage2DFromBuffer_2_0(cl_context context,cl_mem_flags flags,const cl_image_format * image_format,const cl_image_desc * image_desc,void * host_ptr,cl_int * errcode_ret,int num_calls)2237 static cl_mem clCreateImage_testCreateImage2DFromBuffer_2_0(
2238     cl_context context,
2239     cl_mem_flags flags,
2240     const cl_image_format *image_format,
2241     const cl_image_desc *image_desc,
2242     void *host_ptr,
2243     cl_int *errcode_ret,
2244     int num_calls)
2245 {
2246     (void) context;
2247     (void) flags;
2248     (void) num_calls;
2249 
2250     TEST_ASSERT_NOT_NULL(image_format);
2251     TEST_ASSERT_NOT_NULL(image_desc);
2252     TEST_ASSERT_NULL(host_ptr);
2253     TEST_ASSERT_EQUAL_HEX(CL_MEM_OBJECT_IMAGE2D, image_desc->image_type);
2254 
2255     // Return the passed buffer as the cl_mem and success for the error code
2256     if (errcode_ret) {
2257         *errcode_ret = CL_SUCCESS;
2258     }
2259     return image_desc->buffer;
2260 }
2261 #endif
2262 
testCreateImage2DFromBuffer_2_0(void)2263 void testCreateImage2DFromBuffer_2_0(void)
2264 {
2265 #if CL_HPP_TARGET_OPENCL_VERSION >= 200
2266     clGetContextInfo_StubWithCallback(clGetContextInfo_device);
2267     clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
2268     clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_2_0);
2269     clCreateImage_StubWithCallback(clCreateImage_testCreateImage2DFromBuffer_2_0);
2270     clReleaseMemObject_ExpectAndReturn(make_mem(0), CL_SUCCESS);
2271     clReleaseContext_ExpectAndReturn(make_context(0), CL_SUCCESS);
2272 
2273     cl_int err;
2274     cl::Context context(make_context(0));
2275 
2276     // Create buffer
2277     // Create image from buffer
2278     cl::Buffer buffer(make_mem(0));
2279     cl::Image2D imageFromBuffer(
2280         context,
2281         cl::ImageFormat(CL_R, CL_FLOAT), buffer, 64, 32, 256, &err);
2282 
2283     TEST_ASSERT_EQUAL_PTR(buffer(), imageFromBuffer());
2284     TEST_ASSERT_EQUAL(CL_SUCCESS, err);
2285 
2286     buffer() = nullptr;
2287 #endif
2288 }
2289 
2290 #if CL_HPP_TARGET_OPENCL_VERSION >= 200
clCreateImage_testCreateImage2D_2_0(cl_context context,cl_mem_flags flags,const cl_image_format * image_format,const cl_image_desc * image_desc,void * host_ptr,cl_int * errcode_ret,int num_calls)2291 static cl_mem clCreateImage_testCreateImage2D_2_0(
2292     cl_context context,
2293     cl_mem_flags flags,
2294     const cl_image_format *image_format,
2295     const cl_image_desc *image_desc,
2296     void *host_ptr,
2297     cl_int *errcode_ret,
2298     int num_calls)
2299 {
2300     TEST_ASSERT_EQUAL(0, num_calls);
2301     TEST_ASSERT_EQUAL_PTR(make_context(0), context);
2302     TEST_ASSERT_EQUAL_HEX(CL_MEM_READ_WRITE, flags);
2303 
2304     TEST_ASSERT_NOT_NULL(image_format);
2305     TEST_ASSERT_EQUAL_HEX(CL_RGBA, image_format->image_channel_order);
2306     TEST_ASSERT_EQUAL_HEX(CL_FLOAT, image_format->image_channel_data_type);
2307 
2308     TEST_ASSERT_NOT_NULL(image_desc);
2309     TEST_ASSERT_EQUAL_HEX(CL_MEM_OBJECT_IMAGE2D, image_desc->image_type);
2310     TEST_ASSERT_EQUAL(64, image_desc->image_width);
2311     TEST_ASSERT_EQUAL(32, image_desc->image_height);
2312     TEST_ASSERT_EQUAL(256, image_desc->image_row_pitch);
2313     TEST_ASSERT_EQUAL(0, image_desc->num_mip_levels);
2314     TEST_ASSERT_EQUAL(0, image_desc->num_samples);
2315     TEST_ASSERT_NULL(image_desc->buffer);
2316 
2317     TEST_ASSERT_NULL(host_ptr);
2318 
2319     if (errcode_ret != nullptr)
2320         *errcode_ret = CL_SUCCESS;
2321     return make_mem(0);
2322 }
2323 #endif
2324 
2325 #if CL_HPP_TARGET_OPENCL_VERSION >= 200
clCreateImage_testCreateImage2DFromImage_2_0(cl_context context,cl_mem_flags flags,const cl_image_format * image_format,const cl_image_desc * image_desc,void * host_ptr,cl_int * errcode_ret,int num_calls)2326 static cl_mem clCreateImage_testCreateImage2DFromImage_2_0(
2327     cl_context context,
2328     cl_mem_flags flags,
2329     const cl_image_format *image_format,
2330     const cl_image_desc *image_desc,
2331     void *host_ptr,
2332     cl_int *errcode_ret,
2333     int num_calls)
2334 {
2335     (void) context;
2336     (void) flags;
2337     (void) num_calls;
2338 
2339     TEST_ASSERT_NOT_NULL(image_format);
2340     TEST_ASSERT_NOT_NULL(image_desc);
2341     TEST_ASSERT_NULL(host_ptr);
2342     TEST_ASSERT_EQUAL_HEX(CL_MEM_OBJECT_IMAGE2D, image_desc->image_type);
2343 
2344     // Return the passed buffer as the cl_mem and success for the error code
2345     if (errcode_ret) {
2346         *errcode_ret = CL_SUCCESS;
2347     }
2348     return image_desc->buffer;
2349 }
2350 #endif
2351 
2352 #if CL_HPP_TARGET_OPENCL_VERSION >= 200
clGetImageInfo_testCreateImage2DFromImage_2_0(cl_mem image,cl_image_info param_name,size_t param_value_size,void * param_value,size_t * param_value_size_ret,int num_calls)2353 static cl_int clGetImageInfo_testCreateImage2DFromImage_2_0(
2354     cl_mem image,
2355     cl_image_info param_name,
2356     size_t param_value_size,
2357     void *param_value,
2358     size_t *param_value_size_ret,
2359     int num_calls)
2360 {
2361     (void) image;
2362     (void) param_name;
2363     (void) param_value_size;
2364     (void) param_value;
2365     (void) param_value_size_ret;
2366 
2367     TEST_ASSERT_INT_WITHIN(6, 0, num_calls);
2368     return CL_SUCCESS;
2369 }
2370 #endif
2371 
testCreateImage2DFromImage_2_0(void)2372 void testCreateImage2DFromImage_2_0(void)
2373 {
2374 #if CL_HPP_TARGET_OPENCL_VERSION >= 200
2375     clGetContextInfo_StubWithCallback(clGetContextInfo_device);
2376     clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
2377     clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_2_0);
2378     clCreateImage_StubWithCallback(clCreateImage_testCreateImage2D_2_0);
2379 
2380 
2381     cl_int err;
2382     cl::Context context(make_context(0));
2383 
2384     // As in 1.2 2D image test, needed as source for image-from-image
2385     cl::Image2D image(
2386         context, CL_MEM_READ_WRITE,
2387         cl::ImageFormat(CL_RGBA, CL_FLOAT), 64, 32, 256, nullptr, &err);
2388 
2389     TEST_ASSERT_EQUAL(CL_SUCCESS, err);
2390     TEST_ASSERT_EQUAL_PTR(make_mem(0), image());
2391 
2392     // Continue state for next phase
2393     clGetImageInfo_StubWithCallback(clGetImageInfo_testCreateImage2DFromImage_2_0);
2394     clCreateImage_StubWithCallback(clCreateImage_testCreateImage2DFromImage_2_0);
2395     clReleaseMemObject_ExpectAndReturn(make_mem(0), CL_SUCCESS);
2396     clReleaseMemObject_ExpectAndReturn(make_mem(0), CL_SUCCESS);
2397     clReleaseContext_ExpectAndReturn(make_context(0), CL_SUCCESS);
2398 
2399     // Create 2D image from 2D Image with a new channel order
2400     cl::Image2D imageFromImage(
2401         context,
2402         CL_sRGB,
2403         image,
2404         &err
2405         );
2406 
2407     TEST_ASSERT_EQUAL(CL_SUCCESS, err);
2408     TEST_ASSERT_EQUAL_PTR(image(), imageFromImage());
2409 
2410     //imageFromImage() = nullptr;
2411     //image() = nullptr;
2412     //context() = nullptr;
2413 #endif
2414 }
2415 
2416 // Note that default tests maintain state when run from the same
2417 // unit process.
2418 // One default setting test will maintain the defaults until the end.
testSetDefaultPlatform(void)2419 void testSetDefaultPlatform(void)
2420 {
2421     cl::Platform p(make_platform_id(1));
2422     cl::Platform p2 = cl::Platform::setDefault(p);
2423     cl::Platform p3 = cl::Platform::getDefault();
2424     TEST_ASSERT_EQUAL(p(), p2());
2425     TEST_ASSERT_EQUAL(p(), p3());
2426 }
2427 
2428 // Note that default tests maintain state when run from the same
2429 // unit process.
2430 // One default setting test will maintain the defaults until the end.
testSetDefaultPlatformTwice(void)2431 void testSetDefaultPlatformTwice(void)
2432 {
2433     cl::Platform p(make_platform_id(2));
2434     cl::Platform p2 = cl::Platform::getDefault();
2435     cl::Platform p3 = cl::Platform::setDefault(p);
2436     // Set default should have failed
2437     TEST_ASSERT_EQUAL(p2(), p3());
2438     TEST_ASSERT_NOT_EQUAL(p(), p3());
2439 }
2440 
2441 // Note that default tests maintain state when run from the same
2442 // unit process.
2443 // One default setting test will maintain the defaults until the end.
testSetDefaultContext(void)2444 void testSetDefaultContext(void)
2445 {
2446 
2447     clRetainContext_ExpectAndReturn(make_context(1), CL_SUCCESS);
2448     clRetainContext_ExpectAndReturn(make_context(1), CL_SUCCESS);
2449     clRetainContext_ExpectAndReturn(make_context(1), CL_SUCCESS);
2450     clReleaseContext_ExpectAndReturn(make_context(1), CL_SUCCESS);
2451     clReleaseContext_ExpectAndReturn(make_context(1), CL_SUCCESS);
2452     clReleaseContext_ExpectAndReturn(make_context(1), CL_SUCCESS);
2453 
2454     cl::Context c(make_context(1));
2455     cl::Context c2 = cl::Context::setDefault(c);
2456     cl::Context c3 = cl::Context::getDefault();
2457     TEST_ASSERT_EQUAL(c(), c2());
2458     TEST_ASSERT_EQUAL(c(), c3());
2459 }
2460 
2461 // Note that default tests maintain state when run from the same
2462 // unit process.
2463 // One default setting test will maintain the defaults until the end.
testSetDefaultCommandQueue(void)2464 void testSetDefaultCommandQueue(void)
2465 {
2466     clRetainCommandQueue_ExpectAndReturn(make_command_queue(1), CL_SUCCESS);
2467     clRetainCommandQueue_ExpectAndReturn(make_command_queue(1), CL_SUCCESS);
2468     clRetainCommandQueue_ExpectAndReturn(make_command_queue(1), CL_SUCCESS);
2469     clReleaseCommandQueue_ExpectAndReturn(make_command_queue(1), CL_SUCCESS);
2470     clReleaseCommandQueue_ExpectAndReturn(make_command_queue(1), CL_SUCCESS);
2471     clReleaseCommandQueue_ExpectAndReturn(make_command_queue(1), CL_SUCCESS);
2472 
2473     cl::CommandQueue c(make_command_queue(1));
2474     cl::CommandQueue c2 = cl::CommandQueue::setDefault(c);
2475     cl::CommandQueue c3 = cl::CommandQueue::getDefault();
2476     TEST_ASSERT_EQUAL(c(), c2());
2477     TEST_ASSERT_EQUAL(c(), c3());
2478 }
2479 
2480 // Note that default tests maintain state when run from the same
2481 // unit process.
2482 // One default setting test will maintain the defaults until the end.
testSetDefaultDevice(void)2483 void testSetDefaultDevice(void)
2484 {
2485     clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
2486     clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_2_0);
2487 
2488     clRetainDevice_ExpectAndReturn(make_device_id(1), CL_SUCCESS);
2489     clRetainDevice_ExpectAndReturn(make_device_id(1), CL_SUCCESS);
2490     clRetainDevice_ExpectAndReturn(make_device_id(1), CL_SUCCESS);
2491     clReleaseDevice_ExpectAndReturn(make_device_id(1), CL_SUCCESS);
2492     clReleaseDevice_ExpectAndReturn(make_device_id(1), CL_SUCCESS);
2493     clReleaseDevice_ExpectAndReturn(make_device_id(1), CL_SUCCESS);
2494 
2495     cl::Device d(make_device_id(1));
2496     cl::Device  d2 = cl::Device::setDefault(d);
2497     cl::Device  d3 = cl::Device::getDefault();
2498     TEST_ASSERT_EQUAL(d(), d2());
2499     TEST_ASSERT_EQUAL(d(), d3());
2500 }
2501 
2502 #if CL_HPP_TARGET_OPENCL_VERSION >= 200
clCreateCommandQueueWithProperties_testCommandQueueDevice(cl_context context,cl_device_id device,const cl_queue_properties * properties,cl_int * errcode_ret,int num_calls)2503 static cl_command_queue clCreateCommandQueueWithProperties_testCommandQueueDevice(
2504     cl_context context,
2505     cl_device_id device,
2506     const cl_queue_properties *properties,
2507     cl_int *errcode_ret,
2508     int num_calls)
2509 {
2510     (void)num_calls;
2511     TEST_ASSERT_EQUAL_PTR(make_context(1), context);
2512     TEST_ASSERT_EQUAL_PTR(make_device_id(1), device);
2513     TEST_ASSERT_EQUAL(properties[0], CL_QUEUE_PROPERTIES);
2514     static cl_command_queue default_ = 0;
2515 
2516     if (errcode_ret != nullptr)
2517         *errcode_ret = CL_SUCCESS;
2518 
2519     if ((properties[1] & CL_QUEUE_ON_DEVICE_DEFAULT) == 0) {
2520         TEST_ASSERT_EQUAL(properties[1], (CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE | CL_QUEUE_ON_DEVICE));
2521         if (properties[2] == CL_QUEUE_SIZE) {
2522             TEST_ASSERT_EQUAL(properties[3], 256);
2523             TEST_ASSERT_EQUAL(properties[4], 0);
2524             return make_command_queue(2);
2525         }
2526         else {
2527             TEST_ASSERT_EQUAL(properties[2], 0);
2528             return make_command_queue(3);
2529         }
2530     }
2531     else {
2532         TEST_ASSERT_EQUAL(properties[1], (CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE | CL_QUEUE_ON_DEVICE | CL_QUEUE_ON_DEVICE_DEFAULT));
2533         if (default_ == 0) {
2534             default_ = make_command_queue(4);
2535         }
2536         return default_;
2537     }
2538 }
2539 #endif
2540 
testCreateDeviceCommandQueue(void)2541 void testCreateDeviceCommandQueue(void)
2542 {
2543 #if CL_HPP_TARGET_OPENCL_VERSION >= 200
2544     clRetainContext_ExpectAndReturn(make_context(1), CL_SUCCESS);
2545     clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
2546     clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_2_0);
2547     clCreateCommandQueueWithProperties_StubWithCallback(clCreateCommandQueueWithProperties_testCommandQueueDevice);
2548     clReleaseCommandQueue_ExpectAndReturn(make_command_queue(4), CL_SUCCESS);
2549     clReleaseCommandQueue_ExpectAndReturn(make_command_queue(4), CL_SUCCESS);
2550     clReleaseCommandQueue_ExpectAndReturn(make_command_queue(2), CL_SUCCESS);
2551     clReleaseCommandQueue_ExpectAndReturn(make_command_queue(3), CL_SUCCESS);
2552     clReleaseDevice_ExpectAndReturn(make_device_id(1), CL_SUCCESS);
2553     clReleaseContext_ExpectAndReturn(make_context(1), CL_SUCCESS);
2554     clReleaseContext_ExpectAndReturn(make_context(1), CL_SUCCESS);
2555 
2556     cl::Context c(make_context(1));
2557     cl::Context c2 = cl::Context::setDefault(c);
2558     cl::Device d(make_device_id(1));
2559 
2560     cl::DeviceCommandQueue dq(c, d);
2561     cl::DeviceCommandQueue dq2(c, d, 256);
2562 
2563     cl::DeviceCommandQueue dqd = cl::DeviceCommandQueue::makeDefault(c, d);
2564     cl::DeviceCommandQueue dqd2 = cl::DeviceCommandQueue::makeDefault(c, d);
2565 
2566     TEST_ASSERT_EQUAL(dqd(), dqd2());
2567 #endif
2568 }
2569 
2570 #if CL_HPP_TARGET_OPENCL_VERSION >= 200
clCreatePipe_testCreatePipe(cl_context context,cl_mem_flags flags,cl_uint packet_size,cl_uint num_packets,const cl_pipe_properties * props,cl_int * errcode_ret,int num_calls)2571 static cl_mem clCreatePipe_testCreatePipe(
2572     cl_context context,
2573     cl_mem_flags flags,
2574     cl_uint packet_size,
2575     cl_uint num_packets,
2576     const cl_pipe_properties *props,
2577     cl_int *errcode_ret,
2578     int num_calls)
2579 {
2580     (void) context;
2581     (void) packet_size;
2582     (void) num_packets;
2583     (void) num_calls;
2584 
2585     if (flags == 0) {
2586         flags = CL_MEM_READ_WRITE | CL_MEM_HOST_NO_ACCESS;
2587     }
2588     TEST_ASSERT_EQUAL(flags, CL_MEM_READ_WRITE | CL_MEM_HOST_NO_ACCESS);
2589     TEST_ASSERT_NULL(props);
2590 
2591     if (errcode_ret)
2592         *errcode_ret = CL_SUCCESS;
2593     return make_mem(0);
2594 }
2595 #endif
2596 
2597 #if CL_HPP_TARGET_OPENCL_VERSION >= 200
clGetPipeInfo_testCreatePipe(cl_mem pipe,cl_pipe_info param_name,size_t param_value_size,void * param_value,size_t * param_value_size_ret,int num_calls)2598 static cl_int clGetPipeInfo_testCreatePipe(
2599     cl_mem pipe,
2600     cl_pipe_info param_name,
2601     size_t param_value_size,
2602     void *param_value,
2603     size_t *param_value_size_ret,
2604     int num_calls)
2605 {
2606     (void) pipe;
2607     (void) num_calls;
2608 
2609     TEST_ASSERT_NOT_NULL(param_value);
2610     if (param_name == CL_PIPE_PACKET_SIZE) {
2611         *static_cast<cl_uint*>(param_value) = 16;
2612         if (param_value_size_ret) {
2613             *param_value_size_ret = param_value_size;
2614         }
2615         return CL_SUCCESS;
2616     }
2617     else if (param_name == CL_PIPE_MAX_PACKETS) {
2618         *static_cast<cl_uint*>(param_value) = 32;
2619         if (param_value_size_ret) {
2620             *param_value_size_ret = param_value_size;
2621         }
2622         return CL_SUCCESS;
2623     }
2624     else {
2625         TEST_FAIL();
2626         return CL_INVALID_VALUE;
2627     }
2628 }
2629 #endif
2630 
testCreatePipe(void)2631 void testCreatePipe(void)
2632 {
2633 #if CL_HPP_TARGET_OPENCL_VERSION >= 200
2634     clCreatePipe_StubWithCallback(clCreatePipe_testCreatePipe);
2635     clGetPipeInfo_StubWithCallback(clGetPipeInfo_testCreatePipe);
2636     clRetainContext_ExpectAndReturn(make_context(1), CL_SUCCESS);
2637     clReleaseContext_ExpectAndReturn(make_context(1), CL_SUCCESS);
2638     clReleaseMemObject_ExpectAndReturn(make_mem(0), CL_SUCCESS);
2639     clReleaseMemObject_ExpectAndReturn(make_mem(0), CL_SUCCESS);
2640     clReleaseContext_ExpectAndReturn(make_context(1), CL_SUCCESS);
2641 
2642     cl::Context c(make_context(1));
2643     cl::Pipe p(c, 16, 32);
2644     cl::Pipe p2(16, 32);
2645 
2646     cl_uint size = p2.getInfo<CL_PIPE_PACKET_SIZE>();
2647     cl_uint packets;
2648     p2.getInfo(CL_PIPE_MAX_PACKETS, &packets);
2649 
2650     TEST_ASSERT_EQUAL(size, 16);
2651     TEST_ASSERT_EQUAL(packets, 32);
2652 #endif
2653 }
2654 
2655 #if CL_HPP_TARGET_OPENCL_VERSION >= 210
clGetKernelSubGroupInfo_testSubGroups(cl_kernel kernel,cl_device_id device,cl_kernel_sub_group_info param_name,size_t input_value_size,const void * input_value,size_t param_value_size,void * param_value,size_t * param_value_size_ret,int num_calls)2656 static cl_int clGetKernelSubGroupInfo_testSubGroups(cl_kernel kernel,
2657     cl_device_id device,
2658     cl_kernel_sub_group_info param_name,
2659     size_t input_value_size,
2660     const void *input_value,
2661     size_t param_value_size,
2662     void *param_value,
2663     size_t *param_value_size_ret,
2664     int num_calls)
2665 {
2666     (void) kernel;
2667     (void) device;
2668     (void) input_value_size;
2669     (void) param_value_size;
2670     (void) num_calls;
2671 
2672     TEST_ASSERT_NOT_NULL(input_value);
2673     TEST_ASSERT_NOT_NULL(param_value);
2674 
2675     if (param_name == CL_KERNEL_MAX_SUB_GROUP_SIZE_FOR_NDRANGE_KHR) {
2676         *static_cast<size_t*>(param_value) = 32;
2677         if (param_value_size_ret) {
2678             *param_value_size_ret = sizeof(size_t);
2679         }
2680         return CL_SUCCESS;
2681     }
2682     else if (param_name == CL_KERNEL_SUB_GROUP_COUNT_FOR_NDRANGE_KHR) {
2683         *static_cast<size_t*>(param_value) = 2;
2684         if (param_value_size_ret) {
2685             *param_value_size_ret = sizeof(size_t);
2686         }
2687         return CL_SUCCESS;
2688     }
2689     else {
2690         TEST_ABORT();
2691         return CL_INVALID_OPERATION;
2692     }
2693 }
2694 #endif
2695 
testSubGroups(void)2696 void testSubGroups(void)
2697 {
2698 // TODO support testing cl_khr_subgroups on 2.0
2699 #if CL_HPP_TARGET_OPENCL_VERSION >= 210
2700     clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
2701     clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_2_0);
2702     clGetKernelSubGroupInfo_StubWithCallback(clGetKernelSubGroupInfo_testSubGroups);
2703     clReleaseDevice_ExpectAndReturn(make_device_id(0), CL_SUCCESS);
2704     clReleaseKernel_ExpectAndReturn(make_kernel(0), CL_SUCCESS);
2705 
2706     cl::Kernel k(make_kernel(0));
2707     cl::Device d(make_device_id(0));
2708     cl_int err;
2709     cl::NDRange ndrange(8, 8);
2710     size_t res1 = k.getSubGroupInfo<CL_KERNEL_MAX_SUB_GROUP_SIZE_FOR_NDRANGE_KHR>(
2711         d, ndrange, &err);
2712     size_t res2 = 0;
2713     err = k.getSubGroupInfo(
2714         d, CL_KERNEL_SUB_GROUP_COUNT_FOR_NDRANGE_KHR, ndrange, &res2);
2715 
2716     TEST_ASSERT_EQUAL(res1, 32);
2717     TEST_ASSERT_EQUAL(res2, 2);
2718 #endif
2719 }
2720 
2721 /**
2722 * Stub implementation of clGetDeviceInfo that returns an absense of builtin kernels
2723 */
clGetDeviceInfo_builtin(cl_device_id id,cl_device_info param_name,size_t param_value_size,void * param_value,size_t * param_value_size_ret,int num_calls)2724 static cl_int clGetDeviceInfo_builtin(
2725     cl_device_id id,
2726     cl_device_info param_name,
2727     size_t param_value_size,
2728     void *param_value,
2729     size_t *param_value_size_ret,
2730     int num_calls)
2731 {
2732     (void) id;
2733     (void) param_value_size;
2734 
2735     // Test to verify case where empty string is returned - so size is 0
2736     (void)num_calls;
2737     TEST_ASSERT_EQUAL_HEX(CL_DEVICE_BUILT_IN_KERNELS, param_name);
2738     if (param_value == nullptr) {
2739         if (param_value_size_ret != nullptr) {
2740             *param_value_size_ret = 0;
2741         }
2742     }
2743     return CL_SUCCESS;
2744 }
2745 
testBuiltInKernels(void)2746 void testBuiltInKernels(void)
2747 {
2748 #if CL_HPP_TARGET_OPENCL_VERSION >= 120
2749     clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
2750     clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_2_0);
2751     clReleaseDevice_ExpectAndReturn(make_device_id(0), CL_SUCCESS);
2752 
2753     cl::Device d0(make_device_id(0));
2754 
2755     clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_builtin);
2756     cl::string s = d0.getInfo<CL_DEVICE_BUILT_IN_KERNELS>();
2757 #endif
2758 }
2759 
2760 #if CL_HPP_TARGET_OPENCL_VERSION >= 210
2761 /**
2762  * Stub implementation of clCloneKernel that returns a new kernel object
2763  */
clCloneKernel_simplecopy(cl_kernel k,cl_int * errcode_ret,int num_calls)2764 static cl_kernel clCloneKernel_simplecopy(
2765     cl_kernel k,
2766     cl_int *errcode_ret,
2767     int num_calls)
2768 {
2769     (void) k;
2770     (void) num_calls;
2771 
2772     // Test to verify case where empty string is returned - so size is 0
2773     if (errcode_ret != nullptr)
2774         *errcode_ret = CL_SUCCESS;
2775     return make_kernel(POOL_MAX);
2776 }
2777 #endif
2778 
testCloneKernel(void)2779 void testCloneKernel(void)
2780 {
2781 #if CL_HPP_TARGET_OPENCL_VERSION >= 210
2782     clCloneKernel_StubWithCallback(clCloneKernel_simplecopy);
2783     clReleaseKernel_ExpectAndReturn(make_kernel(POOL_MAX), CL_SUCCESS);
2784     cl::Kernel clone = kernelPool[0].clone();
2785     TEST_ASSERT_EQUAL(clone(), make_kernel(POOL_MAX));
2786 #endif
2787 }
2788 
testEnqueueMapSVM(void)2789 void testEnqueueMapSVM(void)
2790 {
2791 #if CL_HPP_TARGET_OPENCL_VERSION >= 200
2792     std::vector<int> vec(7);
2793     clEnqueueSVMMap_ExpectAndReturn(commandQueuePool[0].get(), CL_TRUE, CL_MAP_READ|CL_MAP_WRITE, static_cast<void*>(vec.data()), vec.size()*sizeof(int), 0, nullptr, nullptr, CL_SUCCESS);
2794     TEST_ASSERT_EQUAL(commandQueuePool[0].enqueueMapSVM(vec, CL_TRUE, CL_MAP_READ|CL_MAP_WRITE), CL_SUCCESS);
2795 #endif
2796 }
2797 
testMapSVM(void)2798 void testMapSVM(void)
2799 {
2800 #if CL_HPP_TARGET_OPENCL_VERSION >= 200
2801     std::vector<int> vec(1);
2802     clRetainCommandQueue_ExpectAndReturn(make_command_queue(1), CL_SUCCESS);
2803     clEnqueueSVMMap_ExpectAndReturn(make_command_queue(1), CL_TRUE, CL_MAP_READ|CL_MAP_WRITE, static_cast<void*>(vec.data()), vec.size()*sizeof(int), 0, nullptr, nullptr, CL_SUCCESS);
2804     clReleaseCommandQueue_ExpectAndReturn(make_command_queue(1), CL_SUCCESS);
2805     TEST_ASSERT_EQUAL(cl::mapSVM(vec), CL_SUCCESS);
2806 #endif
2807 }
2808 
2809 // Run after other tests to clear the default state in the header
2810 // using special unit test bypasses.
2811 // We cannot remove the once_flag, so this is a hard fix
2812 // but it means we won't hit cmock release callbacks at the end.
2813 // This is a lot like tearDown but for the header default
2814 // so we do not want to run it for every test.
2815 // The alternative would be to manually modify the test runner
2816 // but we avoid that for now.
testCleanupHeaderState(void)2817 void testCleanupHeaderState(void)
2818 {
2819     clReleaseCommandQueue_ExpectAndReturn(make_command_queue(1), CL_SUCCESS);
2820     clReleaseContext_ExpectAndReturn(make_context(1), CL_SUCCESS);
2821     clReleaseDevice_ExpectAndReturn(make_device_id(1), CL_SUCCESS);
2822 
2823     cl::CommandQueue::unitTestClearDefault();
2824     cl::Context::unitTestClearDefault();
2825     cl::Device::unitTestClearDefault();
2826     cl::Platform::unitTestClearDefault();
2827 }
2828 
2829 // OpenCL 2.2 APIs:
2830 
2831 #if CL_HPP_TARGET_OPENCL_VERSION >= 220
test_program_release_callback(cl_program,void *)2832 static void CL_CALLBACK test_program_release_callback(
2833     cl_program,
2834     void*)
2835 {
2836 }
2837 #endif
2838 
2839 #if CL_HPP_TARGET_OPENCL_VERSION >= 220
clSetProgramReleaseCallback_set(cl_program program,void (CL_CALLBACK * pfn_notify)(cl_program program,void * user_data),void * user_data,int num_calls)2840 static cl_int clSetProgramReleaseCallback_set(
2841     cl_program program,
2842     void (CL_CALLBACK * pfn_notify)(cl_program program, void * user_data),
2843     void *user_data,
2844     int num_calls)
2845 {
2846     (void) user_data;
2847     (void) num_calls;
2848 
2849     TEST_ASSERT_EQUAL_PTR(make_program(0), program);
2850     TEST_ASSERT_EQUAL_PTR(pfn_notify, test_program_release_callback);
2851 
2852     return CL_SUCCESS;
2853 }
2854 #endif
2855 
testSetProgramReleaseCallback(void)2856 void testSetProgramReleaseCallback(void)
2857 {
2858 #if CL_HPP_TARGET_OPENCL_VERSION >= 220
2859     cl_program program = make_program(0);
2860     int user_data = 0;
2861 
2862     clSetProgramReleaseCallback_StubWithCallback(clSetProgramReleaseCallback_set);
2863     clReleaseProgram_ExpectAndReturn(program, CL_SUCCESS);
2864 
2865     cl::Program prog(program);
2866 
2867     prog.setReleaseCallback(test_program_release_callback, &user_data);
2868 #endif
2869 }
2870 
testSetProgramSpecializationConstantScalar(void)2871 void testSetProgramSpecializationConstantScalar(void)
2872 {
2873 #if CL_HPP_TARGET_OPENCL_VERSION >= 220
2874     cl_program program = make_program(0);
2875     int sc = 0;
2876 
2877     clSetProgramSpecializationConstant_ExpectAndReturn(program, 0, sizeof(sc), &sc, CL_SUCCESS);
2878     clReleaseProgram_ExpectAndReturn(program, CL_SUCCESS);
2879 
2880     cl::Program prog(program);
2881 
2882     prog.setSpecializationConstant(0, sc);
2883 #endif
2884 }
2885 
2886 #if CL_HPP_TARGET_OPENCL_VERSION >= 220
2887 /// Stub for testing boolean specialization constants
clSetProgramSpecializationConstant_testBool(cl_program program,cl_uint spec_id,size_t spec_size,const void * spec_value,int num_calls)2888 static cl_int clSetProgramSpecializationConstant_testBool(
2889     cl_program program,
2890     cl_uint spec_id,
2891     size_t spec_size,
2892     const void* spec_value,
2893     int num_calls)
2894 {
2895     (void) num_calls;
2896 
2897     TEST_ASSERT_EQUAL_PTR(make_program(0), program);
2898     TEST_ASSERT(spec_id == 0 || spec_id == 1);
2899     TEST_ASSERT_EQUAL(spec_size, 1);
2900     if (spec_id == 0)
2901     {
2902         const cl_uchar *uc_value = (const cl_uchar*)spec_value;
2903         TEST_ASSERT_EQUAL_HEX(uc_value[0], 0);
2904     }
2905     if (spec_id == 1)
2906     {
2907         const cl_uchar *uc_value = (const cl_uchar*)spec_value;
2908         TEST_ASSERT_EQUAL_HEX(uc_value[0], CL_UCHAR_MAX);
2909     }
2910     return CL_SUCCESS;
2911 }
2912 #endif
2913 
testSetProgramSpecializationConstantBool(void)2914 void testSetProgramSpecializationConstantBool(void)
2915 {
2916 #if CL_HPP_TARGET_OPENCL_VERSION >= 220
2917     // Spec constant "false" should turn into a call with size one and no bits set.
2918     // Spec constant "true" should turn into a call with size one and all bits set.
2919     cl_program program = make_program(0);
2920     bool scFalse = false;
2921     bool scTrue = true;
2922 
2923     clSetProgramSpecializationConstant_StubWithCallback(clSetProgramSpecializationConstant_testBool);
2924 
2925     clReleaseProgram_ExpectAndReturn(program, CL_SUCCESS);
2926 
2927     cl::Program prog(program);
2928 
2929     prog.setSpecializationConstant(0, scFalse);
2930     prog.setSpecializationConstant(1, scTrue);
2931 #endif
2932 }
2933 
testSetProgramSpecializationConstantPointer(void)2934 void testSetProgramSpecializationConstantPointer(void)
2935 {
2936 #if CL_HPP_TARGET_OPENCL_VERSION >= 220
2937     cl_program program = make_program(0);
2938     int scArray[5] = {};
2939 
2940     clSetProgramSpecializationConstant_ExpectAndReturn(program, 0, sizeof(scArray), &scArray, CL_SUCCESS);
2941     clReleaseProgram_ExpectAndReturn(program, CL_SUCCESS);
2942 
2943     cl::Program prog(program);
2944 
2945     prog.setSpecializationConstant(0, sizeof(scArray), scArray);
2946 #endif
2947 }
2948 
2949 // OpenCL 3.0 and cl_khr_extended_versioning Queries
2950 
2951 // Assumes the core enums, structures, and macros exactly match
2952 // the extension enums, structures, and macros:
2953 
2954 static_assert(CL_PLATFORM_NUMERIC_VERSION == CL_PLATFORM_NUMERIC_VERSION_KHR,
2955     "CL_PLATFORM_NUMERIC_VERSION mismatch");
2956 static_assert(CL_PLATFORM_EXTENSIONS_WITH_VERSION == CL_PLATFORM_EXTENSIONS_WITH_VERSION_KHR,
2957     "CL_PLATFORM_EXTENSIONS_WITH_VERSION mismatch");
2958 
2959 static_assert(CL_DEVICE_NUMERIC_VERSION == CL_DEVICE_NUMERIC_VERSION_KHR,
2960     "CL_DEVICE_NUMERIC_VERSION mismatch");
2961 static_assert(CL_DEVICE_EXTENSIONS_WITH_VERSION == CL_DEVICE_EXTENSIONS_WITH_VERSION_KHR,
2962     "CL_DEVICE_EXTENSIONS_WITH_VERSION mismatch");
2963 static_assert(CL_DEVICE_ILS_WITH_VERSION == CL_DEVICE_ILS_WITH_VERSION_KHR,
2964     "CL_DEVICE_ILS_WITH_VERSION mismatch");
2965 static_assert(CL_DEVICE_BUILT_IN_KERNELS_WITH_VERSION == CL_DEVICE_BUILT_IN_KERNELS_WITH_VERSION_KHR,
2966     "CL_DEVICE_BUILT_IN_KERNELS_WITH_VERSION mismatch");
2967 
2968 static_assert(sizeof(cl_name_version) == sizeof(cl_name_version_khr),
2969     "cl_name_version mismatch");
2970 
2971 static_assert(CL_MAKE_VERSION(1, 2, 3) == CL_MAKE_VERSION_KHR(1, 2, 3),
2972     "CL_MAKE_VERSION mismatch");
2973 
clGetPlatformInfo_extended_versioning(cl_platform_id id,cl_platform_info param_name,size_t param_value_size,void * param_value,size_t * param_value_size_ret,int num_calls)2974 static cl_int clGetPlatformInfo_extended_versioning(
2975     cl_platform_id id,
2976     cl_platform_info param_name,
2977     size_t param_value_size,
2978     void *param_value,
2979     size_t *param_value_size_ret,
2980     int num_calls)
2981 {
2982     (void) id;
2983     (void) num_calls;
2984 
2985     switch (param_name) {
2986     case CL_PLATFORM_NUMERIC_VERSION:
2987     {
2988         if (param_value_size == sizeof(cl_version) && param_value) {
2989             *static_cast<cl_version*>(param_value) = CL_MAKE_VERSION(1, 2, 3);
2990         }
2991         if (param_value_size_ret) {
2992             *param_value_size_ret = sizeof(cl_version);
2993         }
2994         return CL_SUCCESS;
2995     }
2996     case CL_PLATFORM_EXTENSIONS_WITH_VERSION:
2997     {
2998         static cl_name_version extension = {
2999             CL_MAKE_VERSION(10, 11, 12),
3000             "cl_dummy_extension",
3001         };
3002         if (param_value_size == sizeof(cl_name_version) && param_value) {
3003             *static_cast<cl_name_version*>(param_value) = extension;
3004         }
3005         if (param_value_size_ret) {
3006             *param_value_size_ret = sizeof(extension);
3007         }
3008         return CL_SUCCESS;
3009     }
3010     default: break;
3011     }
3012     TEST_FAIL();
3013     return CL_INVALID_OPERATION;
3014 }
3015 
testPlatformExtendedVersioning_3_0(void)3016 void testPlatformExtendedVersioning_3_0(void)
3017 {
3018 #if CL_HPP_TARGET_OPENCL_VERSION >= 300
3019     cl::Platform p(make_platform_id(1));
3020 
3021     clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_extended_versioning);
3022 
3023     cl_version platformVersion = p.getInfo<CL_PLATFORM_NUMERIC_VERSION>();
3024     TEST_ASSERT_EQUAL_HEX(platformVersion, CL_MAKE_VERSION(1, 2, 3));
3025 
3026     std::vector<cl_name_version> extensions = p.getInfo<CL_PLATFORM_EXTENSIONS_WITH_VERSION>();
3027     TEST_ASSERT_EQUAL(extensions.size(), 1);
3028     TEST_ASSERT_EQUAL_HEX(extensions[0].version, CL_MAKE_VERSION(10, 11, 12));
3029     TEST_ASSERT_EQUAL_STRING(extensions[0].name, "cl_dummy_extension");
3030 #endif // CL_HPP_TARGET_OPENCL_VERSION >= 300
3031 }
3032 
testPlatformExtendedVersioning_KHR(void)3033 void testPlatformExtendedVersioning_KHR(void)
3034 {
3035 #if CL_HPP_TARGET_OPENCL_VERSION < 300
3036     cl::Platform p(make_platform_id(1));
3037 
3038     clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_extended_versioning);
3039 
3040     cl_version_khr platformVersion = p.getInfo<CL_PLATFORM_NUMERIC_VERSION_KHR>();
3041     TEST_ASSERT_EQUAL_HEX(platformVersion, CL_MAKE_VERSION_KHR(1, 2, 3));
3042 
3043     std::vector<cl_name_version_khr> extensions = p.getInfo<CL_PLATFORM_EXTENSIONS_WITH_VERSION_KHR>();
3044     TEST_ASSERT_EQUAL(extensions.size(), 1);
3045     TEST_ASSERT_EQUAL_HEX(extensions[0].version, CL_MAKE_VERSION_KHR(10, 11, 12));
3046     TEST_ASSERT_EQUAL_STRING(extensions[0].name, "cl_dummy_extension");
3047 #endif // CL_HPP_TARGET_OPENCL_VERSION < 300
3048 }
3049 
3050 
3051 // Note: This assumes the core enums, structures, and macros exactly match
3052 // the extension enums, structures, and macros.
3053 
clGetDeviceInfo_extended_versioning(cl_device_id id,cl_device_info param_name,size_t param_value_size,void * param_value,size_t * param_value_size_ret,int num_calls)3054 static cl_int clGetDeviceInfo_extended_versioning(
3055     cl_device_id id,
3056     cl_device_info param_name,
3057     size_t param_value_size,
3058     void *param_value,
3059     size_t *param_value_size_ret,
3060     int num_calls)
3061 {
3062     (void) id;
3063     (void) num_calls;
3064 
3065     switch (param_name) {
3066     case CL_DEVICE_NUMERIC_VERSION:
3067     {
3068         if (param_value_size == sizeof(cl_version) && param_value) {
3069             *static_cast<cl_version*>(param_value) = CL_MAKE_VERSION(1, 2, 3);
3070         }
3071         if (param_value_size_ret) {
3072             *param_value_size_ret = sizeof(cl_version);
3073         }
3074         return CL_SUCCESS;
3075     }
3076     case CL_DEVICE_OPENCL_C_NUMERIC_VERSION_KHR:
3077     {
3078         if (param_value_size == sizeof(cl_version_khr) && param_value) {
3079             *static_cast<cl_version_khr*>(param_value) = CL_MAKE_VERSION_KHR(4, 5, 6);
3080         }
3081         if (param_value_size_ret) {
3082             *param_value_size_ret = sizeof(cl_version_khr);
3083         }
3084         return CL_SUCCESS;
3085     }
3086     case CL_DEVICE_EXTENSIONS_WITH_VERSION:
3087     {
3088         static cl_name_version extension = {
3089             CL_MAKE_VERSION(10, 11, 12),
3090             "cl_dummy_extension",
3091         };
3092         if (param_value_size == sizeof(cl_name_version) && param_value) {
3093             *static_cast<cl_name_version*>(param_value) = extension;
3094         }
3095         if (param_value_size_ret) {
3096             *param_value_size_ret = sizeof(extension);
3097         }
3098         return CL_SUCCESS;
3099     }
3100     case CL_DEVICE_ILS_WITH_VERSION:
3101     {
3102         static cl_name_version il = {
3103             CL_MAKE_VERSION(20, 21, 22),
3104             "DUMMY_IR",
3105         };
3106         if (param_value_size == sizeof(cl_name_version) && param_value) {
3107             *static_cast<cl_name_version*>(param_value) = il;
3108         }
3109         if (param_value_size_ret) {
3110             *param_value_size_ret = sizeof(il);
3111         }
3112         return CL_SUCCESS;
3113     }
3114     case CL_DEVICE_BUILT_IN_KERNELS_WITH_VERSION:
3115     {
3116         // Test no built-in kernels:
3117         if (param_value_size_ret) {
3118             *param_value_size_ret = 0;
3119         }
3120         return CL_SUCCESS;
3121     }
3122     case CL_DEVICE_OPENCL_C_ALL_VERSIONS:
3123     {
3124         static cl_name_version opencl_c = {
3125             CL_MAKE_VERSION(30, 31, 32),
3126             "OpenCL C",
3127         };
3128         if (param_value_size == sizeof(cl_name_version) && param_value) {
3129             *static_cast<cl_name_version*>(param_value) = opencl_c;
3130         }
3131         if (param_value_size_ret) {
3132             *param_value_size_ret = sizeof(opencl_c);
3133         }
3134         return CL_SUCCESS;
3135     }
3136     case CL_DEVICE_OPENCL_C_FEATURES:
3137     {
3138         static cl_name_version opencl_c_features[] = {
3139             {
3140                 CL_MAKE_VERSION(40, 41, 42),
3141                 "__opencl_c_feature",
3142             },
3143             {
3144                 CL_MAKE_VERSION(40, 43, 44),
3145                 "__opencl_c_fancy_feature",
3146             },
3147         };
3148         if (param_value_size == sizeof(opencl_c_features) && param_value) {
3149             cl_name_version* feature = static_cast<cl_name_version*>(param_value);
3150             const int numFeatures = ARRAY_SIZE(opencl_c_features);
3151             for (int i = 0; i < numFeatures; i++) {
3152                 feature[i] = opencl_c_features[i];
3153             }
3154         }
3155         if (param_value_size_ret) {
3156             *param_value_size_ret = sizeof(opencl_c_features);
3157         }
3158         return CL_SUCCESS;
3159     }
3160     default: break;
3161     }
3162     TEST_FAIL();
3163     return CL_INVALID_OPERATION;
3164 }
3165 
testDeviceExtendedVersioning_3_0(void)3166 void testDeviceExtendedVersioning_3_0(void)
3167 {
3168 #if CL_HPP_TARGET_OPENCL_VERSION >= 300
3169     clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
3170     clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_3_0);
3171     clReleaseDevice_ExpectAndReturn(make_device_id(0), CL_SUCCESS);
3172 
3173     cl::Device d0(make_device_id(0));
3174 
3175     clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_extended_versioning);
3176 
3177     cl_version deviceVersion = d0.getInfo<CL_DEVICE_NUMERIC_VERSION>();
3178     TEST_ASSERT_EQUAL_HEX(deviceVersion, CL_MAKE_VERSION(1, 2, 3));
3179 
3180     std::vector<cl_name_version> extensions = d0.getInfo<CL_DEVICE_EXTENSIONS_WITH_VERSION>();
3181     TEST_ASSERT_EQUAL(extensions.size(), 1);
3182     TEST_ASSERT_EQUAL_HEX(extensions[0].version, CL_MAKE_VERSION(10, 11, 12));
3183     TEST_ASSERT_EQUAL_STRING(extensions[0].name, "cl_dummy_extension");
3184 
3185     std::vector<cl_name_version> ils = d0.getInfo<CL_DEVICE_ILS_WITH_VERSION>();
3186     TEST_ASSERT_EQUAL(ils.size(), 1);
3187     TEST_ASSERT_EQUAL_HEX(ils[0].version, CL_MAKE_VERSION(20, 21, 22));
3188     TEST_ASSERT_EQUAL_STRING(ils[0].name, "DUMMY_IR");
3189 
3190     std::vector<cl_name_version> opencl_c = d0.getInfo<CL_DEVICE_OPENCL_C_ALL_VERSIONS>();
3191     TEST_ASSERT_EQUAL(opencl_c.size(), 1);
3192     TEST_ASSERT_EQUAL_HEX(opencl_c[0].version, CL_MAKE_VERSION(30, 31, 32));
3193     TEST_ASSERT_EQUAL_STRING(opencl_c[0].name, "OpenCL C");
3194 
3195     std::vector<cl_name_version> opencl_c_features = d0.getInfo<CL_DEVICE_OPENCL_C_FEATURES>();
3196     TEST_ASSERT_EQUAL(opencl_c_features.size(), 2);
3197     TEST_ASSERT_EQUAL_HEX(opencl_c_features[0].version, CL_MAKE_VERSION(40, 41, 42));
3198     TEST_ASSERT_EQUAL_STRING(opencl_c_features[0].name, "__opencl_c_feature");
3199     TEST_ASSERT_EQUAL_HEX(opencl_c_features[1].version, CL_MAKE_VERSION(40, 43, 44));
3200     TEST_ASSERT_EQUAL_STRING(opencl_c_features[1].name, "__opencl_c_fancy_feature");
3201 
3202     std::vector<cl_name_version> builtInKernels = d0.getInfo<CL_DEVICE_BUILT_IN_KERNELS_WITH_VERSION>();
3203     TEST_ASSERT_EQUAL(builtInKernels.size(), 0);
3204 #endif // CL_HPP_TARGET_OPENCL_VERSION >= 300
3205 }
3206 
testDeviceExtendedVersioning_KHR(void)3207 void testDeviceExtendedVersioning_KHR(void)
3208 {
3209 #if CL_HPP_TARGET_OPENCL_VERSION < 300
3210     clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
3211     clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_2_0);
3212     clReleaseDevice_ExpectAndReturn(make_device_id(0), CL_SUCCESS);
3213 
3214     cl::Device d0(make_device_id(0));
3215 
3216     clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_extended_versioning);
3217 
3218     cl_version_khr deviceVersion = d0.getInfo<CL_DEVICE_NUMERIC_VERSION_KHR>();
3219     TEST_ASSERT_EQUAL_HEX(deviceVersion, CL_MAKE_VERSION_KHR(1, 2, 3));
3220 
3221     cl_version_khr cVersion = d0.getInfo<CL_DEVICE_OPENCL_C_NUMERIC_VERSION_KHR>();
3222     TEST_ASSERT_EQUAL_HEX(cVersion, CL_MAKE_VERSION_KHR(4, 5, 6));
3223 
3224     std::vector<cl_name_version_khr> extensions = d0.getInfo<CL_DEVICE_EXTENSIONS_WITH_VERSION_KHR>();
3225     TEST_ASSERT_EQUAL(extensions.size(), 1);
3226     TEST_ASSERT_EQUAL_HEX(extensions[0].version, CL_MAKE_VERSION_KHR(10, 11, 12));
3227     TEST_ASSERT_EQUAL_STRING(extensions[0].name, "cl_dummy_extension");
3228 
3229     std::vector<cl_name_version_khr> ils = d0.getInfo<CL_DEVICE_ILS_WITH_VERSION_KHR>();
3230     TEST_ASSERT_EQUAL(ils.size(), 1);
3231     TEST_ASSERT_EQUAL_HEX(ils[0].version, CL_MAKE_VERSION_KHR(20, 21, 22));
3232     TEST_ASSERT_EQUAL_STRING(ils[0].name, "DUMMY_IR");
3233 
3234     std::vector<cl_name_version_khr> builtInKernels = d0.getInfo<CL_DEVICE_BUILT_IN_KERNELS_WITH_VERSION_KHR>();
3235     TEST_ASSERT_EQUAL(builtInKernels.size(), 0);
3236 #endif // CL_HPP_TARGET_OPENCL_VERSION < 300
3237 }
3238 
clGetDeviceInfo_uuid_pci_bus_info(cl_device_id id,cl_device_info param_name,size_t param_value_size,void * param_value,size_t * param_value_size_ret,int num_calls)3239 static cl_int clGetDeviceInfo_uuid_pci_bus_info(
3240     cl_device_id id,
3241     cl_device_info param_name,
3242     size_t param_value_size,
3243     void *param_value,
3244     size_t *param_value_size_ret,
3245     int num_calls)
3246 {
3247     (void) id;
3248     (void) num_calls;
3249 
3250     switch (param_name) {
3251 #if defined(cl_khr_device_uuid)
3252     case CL_DEVICE_UUID_KHR:
3253     case CL_DRIVER_UUID_KHR:
3254     {
3255         if (param_value_size == CL_UUID_SIZE_KHR && param_value) {
3256             cl_uchar* pUUID = static_cast<cl_uchar*>(param_value);
3257             cl_uchar start =
3258                 (param_name == CL_DEVICE_UUID_KHR) ? 1 :
3259                 (param_name == CL_DRIVER_UUID_KHR) ? 2 :
3260                 0;
3261             for (int i = 0; i < CL_UUID_SIZE_KHR; i++) {
3262                 pUUID[i] = i + start;
3263             }
3264         }
3265         if (param_value_size_ret) {
3266             *param_value_size_ret = CL_UUID_SIZE_KHR;
3267         }
3268         return CL_SUCCESS;
3269     }
3270     case CL_DEVICE_LUID_VALID_KHR:
3271     {
3272         if (param_value_size == sizeof(cl_bool) && param_value) {
3273             *static_cast<cl_bool*>(param_value) = CL_TRUE;
3274         }
3275         if (param_value_size_ret) {
3276             *param_value_size_ret = sizeof(cl_bool);
3277         }
3278         return CL_SUCCESS;
3279     }
3280     case CL_DEVICE_LUID_KHR:
3281     {
3282         if (param_value_size == CL_LUID_SIZE_KHR && param_value) {
3283             cl_uchar* pLUID = static_cast<cl_uchar*>(param_value);
3284             cl_uchar start = 3;
3285             for (int i = 0; i < CL_LUID_SIZE_KHR; i++) {
3286                 pLUID[i] = i + start;
3287             }
3288         }
3289         if (param_value_size_ret) {
3290             *param_value_size_ret = CL_LUID_SIZE_KHR;
3291         }
3292         return CL_SUCCESS;
3293     }
3294     case CL_DEVICE_NODE_MASK_KHR:
3295     {
3296         if (param_value_size == sizeof(cl_uint) && param_value) {
3297             *static_cast<cl_uint*>(param_value) = 0xA5A5;
3298         }
3299         if (param_value_size_ret) {
3300             *param_value_size_ret = sizeof(cl_uint);
3301         }
3302         return CL_SUCCESS;
3303     }
3304 #endif
3305 #if defined(cl_khr_pci_bus_info)
3306     case CL_DEVICE_PCI_BUS_INFO_KHR:
3307     {
3308         if (param_value_size == sizeof(cl_device_pci_bus_info_khr) && param_value) {
3309             cl_device_pci_bus_info_khr* pInfo = static_cast<cl_device_pci_bus_info_khr*>(param_value);
3310             pInfo->pci_domain = 0x11;
3311             pInfo->pci_bus = 0x22;
3312             pInfo->pci_device = 0x33;
3313             pInfo->pci_function = 0x44;
3314         }
3315         if (param_value_size_ret) {
3316             *param_value_size_ret = sizeof(cl_device_pci_bus_info_khr);
3317         }
3318         return CL_SUCCESS;
3319     }
3320 #endif
3321     default: break;
3322     }
3323     TEST_FAIL();
3324     return CL_INVALID_OPERATION;
3325 }
3326 
testDeviceUUID_KHR(void)3327 void testDeviceUUID_KHR(void)
3328 {
3329 #if defined(cl_khr_device_uuid)
3330     clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
3331     clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_2_0);
3332     clReleaseDevice_ExpectAndReturn(make_device_id(0), CL_SUCCESS);
3333 
3334     cl::Device d0(make_device_id(0));
3335 
3336     clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_uuid_pci_bus_info);
3337 
3338     std::array<cl_uchar, CL_UUID_SIZE_KHR> dev_uuid = d0.getInfo<CL_DEVICE_UUID_KHR>();
3339     for (int i = 0; i < CL_UUID_SIZE_KHR; i++) {
3340         TEST_ASSERT_EQUAL_UINT8(i + 1, dev_uuid[i]);
3341     }
3342     std::array<cl_uchar, CL_UUID_SIZE_KHR> drv_uuid = d0.getInfo<CL_DRIVER_UUID_KHR>();
3343     for (int i = 0; i < CL_UUID_SIZE_KHR; i++) {
3344         TEST_ASSERT_EQUAL_UINT8(i + 2, drv_uuid[i]);
3345     }
3346 
3347     cl_bool valid = d0.getInfo<CL_DEVICE_LUID_VALID_KHR>();
3348     TEST_ASSERT_EQUAL(CL_TRUE, valid);
3349     std::array<cl_uchar, CL_LUID_SIZE_KHR> luid = d0.getInfo<CL_DEVICE_LUID_KHR>();
3350     for (int i = 0; i < CL_LUID_SIZE_KHR; i++) {
3351         TEST_ASSERT_EQUAL_UINT8(i + 3, luid[i]);
3352     }
3353 
3354     cl_uint nodeMask = d0.getInfo<CL_DEVICE_NODE_MASK_KHR>();
3355     TEST_ASSERT_EQUAL(0xA5A5, nodeMask);
3356 #endif
3357 }
3358 
testDevicePCIBusInfo_KHR(void)3359 void testDevicePCIBusInfo_KHR(void)
3360 {
3361 #if defined(cl_khr_pci_bus_info)
3362     clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
3363     clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_2_0);
3364     clReleaseDevice_ExpectAndReturn(make_device_id(0), CL_SUCCESS);
3365 
3366     cl::Device d0(make_device_id(0));
3367 
3368     clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_uuid_pci_bus_info);
3369 
3370     cl_device_pci_bus_info_khr info = d0.getInfo<CL_DEVICE_PCI_BUS_INFO_KHR>();
3371     TEST_ASSERT_EQUAL_HEX(0x11, info.pci_domain);
3372     TEST_ASSERT_EQUAL_HEX(0x22, info.pci_bus);
3373     TEST_ASSERT_EQUAL_HEX(0x33, info.pci_device);
3374     TEST_ASSERT_EQUAL_HEX(0x44, info.pci_function);
3375 #endif
3376 }
3377 
clGetProgramInfo_testProgramGetContext(cl_program program,cl_program_build_info param_name,size_t param_value_size,void * param_value,size_t * param_value_size_ret,int)3378 static  cl_int clGetProgramInfo_testProgramGetContext(cl_program program,
3379     cl_program_build_info param_name,
3380     size_t param_value_size,
3381     void *param_value,
3382     size_t *param_value_size_ret,
3383     int /*num_calls*/)
3384 {
3385     TEST_ASSERT_EQUAL_PTR(make_program(0), program);
3386     TEST_ASSERT_EQUAL_HEX(CL_PROGRAM_CONTEXT, param_name);
3387     TEST_ASSERT(param_value == nullptr || param_value_size >= sizeof(cl_context));
3388     if (param_value_size_ret != nullptr)
3389         *param_value_size_ret = sizeof(cl_context);
3390     if (param_value != nullptr)
3391         *static_cast<cl_context *>(param_value) = make_context(0);
3392     return CL_SUCCESS;
3393 }
3394 
clLinkProgram_testLinkProgram(cl_context context,cl_uint num_devices,const cl_device_id * device_list,const char * options,cl_uint num_input_programs,const cl_program * input_programs,void (CL_CALLBACK * pfn_notify)(cl_program program,void * user_data),void * user_data,cl_int * errcode_ret,int)3395 static cl_program clLinkProgram_testLinkProgram(cl_context context,
3396     cl_uint              num_devices,
3397     const cl_device_id * device_list,
3398     const char *         options,
3399     cl_uint              num_input_programs,
3400     const cl_program *   input_programs,
3401     void (CL_CALLBACK *  pfn_notify)(cl_program program, void * user_data),
3402     void *               user_data,
3403     cl_int *             errcode_ret,
3404     int                 /*num_calls*/)
3405 {
3406     TEST_ASSERT_EQUAL_PTR(context, make_context(0));
3407     TEST_ASSERT_EQUAL(num_devices, 0);
3408     TEST_ASSERT_EQUAL(device_list, nullptr);
3409     TEST_ASSERT_EQUAL(options, nullptr);
3410     TEST_ASSERT_NOT_EQUAL(num_input_programs, 0);
3411     for (int i=0; i<(int)num_input_programs; i++)
3412         TEST_ASSERT_EQUAL_PTR(input_programs[i], make_program(i));
3413     TEST_ASSERT_EQUAL(pfn_notify, nullptr);
3414     TEST_ASSERT_EQUAL(user_data, nullptr);
3415 
3416     *errcode_ret = CL_SUCCESS;
3417     return make_program(0);
3418 }
3419 
testLinkProgram(void)3420 void testLinkProgram(void)
3421 {
3422 #if CL_HPP_TARGET_OPENCL_VERSION >= 120
3423     cl_int errcode;
3424     int refcount[] = {1,1};
3425 
3426     // verify if class cl::Program was not modified
3427     TEST_ASSERT_EQUAL(sizeof(cl_program), sizeof(cl::Program));
3428 
3429     clGetProgramInfo_StubWithCallback(clGetProgramInfo_testProgramGetContext);
3430     clLinkProgram_StubWithCallback(clLinkProgram_testLinkProgram);
3431 
3432     clRetainContext_ExpectAndReturn(make_context(0), CL_SUCCESS);
3433     clReleaseContext_ExpectAndReturn(make_context(0), CL_SUCCESS);
3434     prepare_programRefcounts(2, reinterpret_cast<cl_program *>(programPool), refcount);
3435 
3436     cl::Program prog = cl::linkProgram(cl::Program(make_program(0)), cl::Program(make_program(1)),
3437         nullptr, nullptr, nullptr, &errcode);
3438 
3439     TEST_ASSERT_EQUAL_PTR(prog(), make_program(0));
3440     TEST_ASSERT_EQUAL(errcode, CL_SUCCESS);
3441 
3442     prog() = nullptr;
3443 #endif
3444 }
3445 
testLinkProgramWithVectorProgramInput(void)3446 void testLinkProgramWithVectorProgramInput(void)
3447 {
3448 #if CL_HPP_TARGET_OPENCL_VERSION >= 120
3449     cl_int errcode;
3450     VECTOR_CLASS<cl::Program> prog_vec;
3451     std::array<int, ARRAY_SIZE(programPool)> refcount;
3452     for (int i=0;i<(int)ARRAY_SIZE(programPool);i++) {
3453         prog_vec.push_back(cl::Program(programPool[i]()));
3454         refcount[i] = 1;
3455     }
3456 
3457     // verify if class cl::Program was not modified
3458     TEST_ASSERT_EQUAL(sizeof(cl_program), sizeof(cl::Program));
3459 
3460     clGetProgramInfo_StubWithCallback(clGetProgramInfo_testProgramGetContext);
3461     clLinkProgram_StubWithCallback(clLinkProgram_testLinkProgram);
3462     prepare_programRefcounts(prog_vec.size(), reinterpret_cast<cl_program *>(prog_vec.data()), refcount.data());
3463 
3464     clRetainContext_ExpectAndReturn(make_context(0), CL_SUCCESS);
3465     clReleaseContext_ExpectAndReturn(make_context(0), CL_SUCCESS);
3466 
3467     cl::Program prog = linkProgram(prog_vec, nullptr, nullptr, nullptr, &errcode);
3468 
3469     TEST_ASSERT_EQUAL_PTR(prog(), make_program(0));
3470     TEST_ASSERT_EQUAL(errcode, CL_SUCCESS);
3471 
3472     prog() = nullptr;
3473 #endif
3474 }
3475 
testGetPlatformVersion_1_1(void)3476 void testGetPlatformVersion_1_1(void)
3477 {
3478     cl_platform_id platform = make_platform_id(0);
3479 
3480     clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_1_1);
3481 
3482     cl_uint version = cl::detail::getPlatformVersion(platform);
3483     TEST_ASSERT_EQUAL_UINT32(0x10001, version);
3484 }
3485 
testGetPlatformVersion_1_2(void)3486 void testGetPlatformVersion_1_2(void)
3487 {
3488     cl_platform_id platform = make_platform_id(0);
3489 
3490     clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_1_2);
3491 
3492     cl_uint version = cl::detail::getPlatformVersion(platform);
3493     TEST_ASSERT_EQUAL_UINT32(0x10002, version);
3494 }
3495 
testGetPlatformVersion_2_0(void)3496 void testGetPlatformVersion_2_0(void)
3497 {
3498     cl_platform_id platform = make_platform_id(0);
3499 
3500     clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_2_0);
3501 
3502     cl_uint version = cl::detail::getPlatformVersion(platform);
3503     TEST_ASSERT_EQUAL_UINT32(0x20000, version);
3504 }
3505 
3506 
testGetPlatformVersion_3_0(void)3507 void testGetPlatformVersion_3_0(void)
3508 {
3509 #if CL_HPP_TARGET_OPENCL_VERSION >= 300
3510     cl_platform_id platform = make_platform_id(0);
3511 
3512     clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_3_0);
3513 
3514     cl_uint version = cl::detail::getPlatformVersion(platform);
3515     TEST_ASSERT_EQUAL_UINT32(0x30000, version);
3516 #endif // CL_HPP_TARGET_OPENCL_VERSION >= 300
3517 }
3518 
3519 /****************************************************************************
3520  * Tests for cl::CommandBufferKhr
3521  ****************************************************************************/
3522 #if defined(cl_khr_command_buffer)
3523 void testMoveAssignCommandBufferKhrNonNull(void);
3524 void testMoveAssignCommandBufferKhrNull(void);
3525 void testMoveConstructCommandBufferKhrNonNull(void);
3526 void testMoveConstructCommandBufferKhrNull(void);
MAKE_MOVE_TESTS(CommandBufferKhr,make_command_buffer_khr,clReleaseCommandBufferKHR,commandBufferKhrPool)3527 MAKE_MOVE_TESTS(CommandBufferKhr, make_command_buffer_khr, clReleaseCommandBufferKHR, commandBufferKhrPool)
3528 #else
3529 void testMoveAssignCommandBufferKhrNonNull(void) {}
3530 void testMoveAssignCommandBufferKhrNull(void) {}
3531 void testMoveConstructCommandBufferKhrNonNull(void) {}
3532 void testMoveConstructCommandBufferKhrNull(void) {}
3533 #endif
3534 
3535 // Stub for clGetCommandBufferInfoKHR that returns 1
3536 static cl_int clGetCommandBufferInfoKHR_testCommandBufferKhrGetNumQueues(
3537     cl_command_buffer_khr command_buffer,
3538     cl_command_buffer_info_khr param_name,
3539     size_t param_value_size,
3540     void *param_value,
3541     size_t *param_value_size_ret,
3542     int /*num_calls*/)
3543 {
3544     TEST_ASSERT_EQUAL_PTR(make_command_buffer_khr(0), command_buffer);
3545     TEST_ASSERT_EQUAL_HEX(CL_COMMAND_BUFFER_NUM_QUEUES_KHR, param_name);
3546     TEST_ASSERT(param_value == nullptr || param_value_size >= sizeof(cl_uint));
3547     if (param_value_size_ret != nullptr)
3548         *param_value_size_ret = sizeof(cl_uint);
3549     if (param_value != nullptr)
3550         *static_cast<cl_uint *> (param_value) = 1;
3551     return CL_SUCCESS;
3552 }
3553 
testCommandBufferInfoKHRNumQueues(void)3554 void testCommandBufferInfoKHRNumQueues(void)
3555 {
3556 #if defined(cl_khr_command_buffer)
3557     cl_uint expected = 1;
3558 
3559     clGetCommandBufferInfoKHR_StubWithCallback(clGetCommandBufferInfoKHR_testCommandBufferKhrGetNumQueues);
3560 
3561     cl_uint num = commandBufferKhrPool[0].getInfo<CL_COMMAND_BUFFER_NUM_QUEUES_KHR>();
3562     TEST_ASSERT_EQUAL_HEX(expected, num);
3563 #endif
3564 }
3565 
3566 // Stub for clGetCommandBufferInfoKHR that returns command queues array
clGetCommandBufferInfoKHR_testCommandBufferKhrGetCommandQueues(cl_command_buffer_khr command_buffer,cl_command_buffer_info_khr param_name,size_t param_value_size,void * param_value,size_t * param_value_size_ret,int)3567 static cl_int clGetCommandBufferInfoKHR_testCommandBufferKhrGetCommandQueues(
3568     cl_command_buffer_khr command_buffer,
3569     cl_command_buffer_info_khr param_name,
3570     size_t param_value_size,
3571     void *param_value,
3572     size_t *param_value_size_ret,
3573     int /*num_calls*/)
3574 {
3575     TEST_ASSERT_EQUAL_PTR(make_command_buffer_khr(0), command_buffer);
3576     TEST_ASSERT_EQUAL_HEX(CL_COMMAND_BUFFER_QUEUES_KHR, param_name);
3577     TEST_ASSERT(param_value == nullptr || param_value_size >= 3 * sizeof(cl_command_queue));
3578     if (param_value_size_ret != nullptr)
3579         *param_value_size_ret = 3 * sizeof(cl_command_queue);
3580     if (param_value != nullptr)
3581     {
3582         cl_command_queue *command_queues = static_cast<cl_command_queue *> (param_value);
3583         command_queues[0] = make_command_queue(0);
3584         command_queues[1] = make_command_queue(1);
3585         command_queues[2] = make_command_queue(2);
3586     }
3587     return CL_SUCCESS;
3588 }
3589 
testCommandBufferInfoKHRCommandQueues(void)3590 void testCommandBufferInfoKHRCommandQueues(void)
3591 {
3592 #if defined(cl_khr_command_buffer)
3593     // creat expected values for refcounter
3594     VECTOR_CLASS<cl_command_queue> expected_queue_vec;
3595     std::array<int, 3> refcount;
3596     for (int i=0;i<3;i++) {
3597         expected_queue_vec.push_back(commandQueuePool[i]());
3598         refcount[i] = 1;
3599     }
3600 
3601     clGetCommandBufferInfoKHR_StubWithCallback(clGetCommandBufferInfoKHR_testCommandBufferKhrGetCommandQueues);
3602     prepare_commandQueueRefcounts(expected_queue_vec.size(), expected_queue_vec.data(), refcount.data());
3603 
3604     VECTOR_CLASS<cl::CommandQueue> command_queues = commandBufferKhrPool[0].getInfo<CL_COMMAND_BUFFER_QUEUES_KHR>();
3605     TEST_ASSERT_EQUAL(3, command_queues.size());
3606     TEST_ASSERT_EQUAL_PTR(make_command_queue(0), command_queues[0]());
3607     TEST_ASSERT_EQUAL_PTR(make_command_queue(1), command_queues[1]());
3608     TEST_ASSERT_EQUAL_PTR(make_command_queue(2), command_queues[2]());
3609 #endif
3610 }
3611 // Tests for Device::GetInfo
clGetInfo_testDeviceGetInfoCLDeviceVendorId(cl_device_id device,cl_device_info param_name,size_t param_value_size,void * param_value,size_t * param_value_size_ret,int cmock_num_calls)3612 static cl_int clGetInfo_testDeviceGetInfoCLDeviceVendorId(
3613     cl_device_id device, cl_device_info param_name, size_t param_value_size,
3614     void *param_value, size_t *param_value_size_ret, int cmock_num_calls)
3615 {
3616     (void) cmock_num_calls;
3617 
3618     TEST_ASSERT_EQUAL_PTR(make_device_id(0), device);
3619     TEST_ASSERT_EQUAL_HEX(CL_DEVICE_VENDOR_ID, param_name);
3620     TEST_ASSERT(param_value == nullptr || param_value_size >= sizeof(cl_uint));
3621     if (param_value_size_ret != nullptr)
3622         *param_value_size_ret = sizeof(cl_uint);
3623     if (param_value != nullptr)
3624     {
3625         *static_cast<cl_uint *>(param_value) = 0xABCD;
3626     }
3627     return CL_SUCCESS;
3628 }
testDevice_GetInfo_CLDeviceVendorID(void)3629 void testDevice_GetInfo_CLDeviceVendorID(void)
3630 {
3631     cl_uint expected = 0xABCD;
3632     clGetDeviceInfo_StubWithCallback(
3633         clGetInfo_testDeviceGetInfoCLDeviceVendorId);
3634     cl_uint vendorID = devicePool[0].getInfo<CL_DEVICE_VENDOR_ID>();
3635     TEST_ASSERT_EQUAL_HEX(expected, vendorID);
3636 }
clGetInfo_testDeviceGetInfoCLDeviceImageSupport(cl_device_id device,cl_device_info param_name,size_t param_value_size,void * param_value,size_t * param_value_size_ret,int cmock_num_calls)3637 static cl_int clGetInfo_testDeviceGetInfoCLDeviceImageSupport(
3638     cl_device_id device, cl_device_info param_name, size_t param_value_size,
3639     void *param_value, size_t *param_value_size_ret, int cmock_num_calls)
3640 {
3641     (void) cmock_num_calls;
3642 
3643     TEST_ASSERT_EQUAL_PTR(make_device_id(0), device);
3644     TEST_ASSERT_EQUAL_HEX(CL_DEVICE_IMAGE_SUPPORT, param_name);
3645     TEST_ASSERT(param_value == nullptr || param_value_size >= sizeof(cl_bool));
3646     if (param_value_size_ret != nullptr)
3647         *param_value_size_ret = sizeof(cl_bool);
3648     if (param_value != nullptr)
3649     {
3650         *static_cast<cl_bool *>(param_value) = true;
3651     }
3652     return CL_SUCCESS;
3653 }
testDevice_GetInfo_CLDeviceImageSupport(void)3654 void testDevice_GetInfo_CLDeviceImageSupport(void)
3655 {
3656     cl_bool expected = true;
3657     clGetDeviceInfo_StubWithCallback(
3658         clGetInfo_testDeviceGetInfoCLDeviceImageSupport);
3659     cl_bool deviceImageSupport =
3660         devicePool[0].getInfo<CL_DEVICE_IMAGE_SUPPORT>();
3661     TEST_ASSERT_EQUAL_HEX(expected, deviceImageSupport);
3662 }
clGetInfo_testDeviceGetInfoCLDeviceName(cl_device_id device,cl_device_info param_name,size_t param_value_size,void * param_value,size_t * param_value_size_ret,int cmock_num_calls)3663 static cl_int clGetInfo_testDeviceGetInfoCLDeviceName(
3664     cl_device_id device, cl_device_info param_name, size_t param_value_size,
3665     void *param_value, size_t *param_value_size_ret, int cmock_num_calls)
3666 {
3667     (void) cmock_num_calls;
3668 
3669     static char testDeviceName[] = "testDeviceName";
3670     TEST_ASSERT_EQUAL_PTR(make_device_id(0), device);
3671     TEST_ASSERT_EQUAL_HEX(CL_DEVICE_NAME, param_name);
3672     TEST_ASSERT(param_value == nullptr || param_value_size >= sizeof(testDeviceName));
3673 
3674     if (param_value_size_ret != nullptr)
3675         *param_value_size_ret = sizeof(testDeviceName);
3676     if (param_value != nullptr)
3677     {
3678         strcpy((char*)param_value, testDeviceName);
3679     }
3680     return CL_SUCCESS;
3681 }
testDevice_GetInfo_CLDeviceName(void)3682 void testDevice_GetInfo_CLDeviceName(void)
3683 {
3684     cl::string expected = "testDeviceName";
3685     clGetDeviceInfo_StubWithCallback(clGetInfo_testDeviceGetInfoCLDeviceName);
3686     cl::string deviceName = devicePool[0].getInfo<CL_DEVICE_NAME>();
3687     TEST_ASSERT_EQUAL_STRING(expected.c_str(), deviceName.c_str());
3688 }
3689 
3690 #if defined(cl_ext_device_fission)
clCreateSubDevicesEXT_testDevice_createSubDevices(cl_device_id device_in,const cl_device_partition_property_ext * properties,cl_uint n,cl_device_id * out_devices,cl_uint * num,int num_calls)3691 static cl_int clCreateSubDevicesEXT_testDevice_createSubDevices(
3692     cl_device_id device_in, const cl_device_partition_property_ext *properties,
3693     cl_uint n, cl_device_id *out_devices, cl_uint *num, int num_calls) {
3694   cl_int ret = CL_SUCCESS;
3695 
3696   TEST_ASSERT_EQUAL(CL_DEVICE_PARTITION_EQUALLY_EXT, *properties);
3697   if(nullptr != out_devices){
3698     out_devices[0] = make_device_id(0);
3699   }
3700   if (nullptr != num)
3701   {
3702       *num = 1;
3703   }
3704   if (device_in == make_device_id(0)) {
3705     return CL_SUCCESS;
3706   } else if (device_in == make_device_id(1)) {
3707     return CL_INVALID_DEVICE;
3708   } else {
3709     return CL_SUCCESS;
3710   }
3711 }
3712 
testDevice_createSubDevices()3713 void testDevice_createSubDevices() {
3714 #ifndef CL_HPP_ENABLE_EXCEPTIONS
3715   const cl_device_partition_property_ext properties =
3716       CL_DEVICE_PARTITION_EQUALLY_EXT;
3717   std::vector<cl::Device> devices(1);
3718 
3719   clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
3720   clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_1_1);
3721 
3722   clCreateSubDevicesEXT_StubWithCallback(
3723       clCreateSubDevicesEXT_testDevice_createSubDevices);
3724 
3725   cl_int ret = devicePool[0].createSubDevices(&properties, &devices);
3726   TEST_ASSERT_EQUAL(CL_SUCCESS, ret);
3727   ret = devicePool[1].createSubDevices(&properties, &devices);
3728   TEST_ASSERT_EQUAL(CL_INVALID_DEVICE , ret);
3729   ret = devicePool[2].createSubDevices(&properties, &devices);
3730   TEST_ASSERT_EQUAL(CL_SUCCESS, ret);
3731   TEST_ASSERT_EQUAL(devices[0].get(), make_device_id(0));
3732 #endif /*CL_HPP_ENABLE_EXCEPTIONS*/
3733 }
3734 #endif /*cl_ext_device_fission*/
3735 
3736 /****************************************************************************
3737  * Tests for cl::Semaphore
3738  ****************************************************************************/
3739 #if defined(cl_khr_semaphore)
3740 void testMoveAssignSemaphoreNonNull(void);
3741 void testMoveAssignSemaphoreNull(void);
3742 void testMoveConstructSemaphoreNonNull(void);
3743 void testMoveConstructSemaphoreNull(void);
3744 MAKE_MOVE_TESTS(Semaphore, make_semaphore_khr, clReleaseSemaphoreKHR, semaphorePool);
3745 #else
testMoveAssignSemaphoreNonNull(void)3746 void testMoveAssignSemaphoreNonNull(void) {}
testMoveAssignSemaphoreNull(void)3747 void testMoveAssignSemaphoreNull(void) {}
testMoveConstructSemaphoreNonNull(void)3748 void testMoveConstructSemaphoreNonNull(void) {}
testMoveConstructSemaphoreNull(void)3749 void testMoveConstructSemaphoreNull(void) {}
3750 #endif
3751 
3752 #if defined(cl_khr_semaphore)
clEnqueueWaitSemaphoresKHR_testEnqueueWaitSemaphores(cl_command_queue command_queue,cl_uint num_sema_objects,const cl_semaphore_khr * sema_objects,const cl_semaphore_payload_khr * sema_payload_list,cl_uint num_events_in_wait_list,const cl_event * event_wait_list,cl_event * event,int num_calls)3753 static cl_int clEnqueueWaitSemaphoresKHR_testEnqueueWaitSemaphores(
3754     cl_command_queue command_queue,
3755     cl_uint num_sema_objects,
3756     const cl_semaphore_khr* sema_objects,
3757     const cl_semaphore_payload_khr* sema_payload_list,
3758     cl_uint num_events_in_wait_list,
3759     const cl_event* event_wait_list,
3760     cl_event* event,
3761     int num_calls)
3762 {
3763     TEST_ASSERT_EQUAL(0, num_calls);
3764     TEST_ASSERT_EQUAL_PTR(commandQueuePool[1](), command_queue);
3765     TEST_ASSERT_EQUAL(1, num_sema_objects);
3766     TEST_ASSERT_NOT_NULL(sema_objects);
3767     TEST_ASSERT_EQUAL(make_semaphore_khr(1), *sema_objects);
3768     TEST_ASSERT_NOT_NULL(sema_payload_list);
3769     TEST_ASSERT_EQUAL(0, num_events_in_wait_list);
3770     TEST_ASSERT_NULL(event_wait_list);
3771 
3772     if (event != nullptr)
3773     {
3774         *event = make_event(1);
3775     }
3776 
3777     return CL_SUCCESS;
3778 }
3779 
testEnqueueWaitSemaphores(void)3780 void testEnqueueWaitSemaphores(void)
3781 {
3782     clEnqueueWaitSemaphoresKHR_StubWithCallback(clEnqueueWaitSemaphoresKHR_testEnqueueWaitSemaphores);
3783 
3784     VECTOR_CLASS<cl::Semaphore> sema_objects;
3785     sema_objects.emplace_back(make_semaphore_khr(1));
3786     VECTOR_CLASS<cl_semaphore_payload_khr> sema_payloads(1);
3787     cl::Event event;
3788 
3789     cl_int status = commandQueuePool[1].enqueueWaitSemaphores(sema_objects, sema_payloads, nullptr, &event);
3790     TEST_ASSERT_EQUAL(CL_SUCCESS, status);
3791     TEST_ASSERT_EQUAL_PTR(make_event(1), event());
3792 
3793     // prevent destructor from interfering with the test
3794     event() = nullptr;
3795     sema_objects[0]() = nullptr;
3796 }
3797 
clEnqueueSignalSemaphoresKHR_testEnqueueSignalSemaphores(cl_command_queue command_queue,cl_uint num_sema_objects,const cl_semaphore_khr * sema_objects,const cl_semaphore_payload_khr * sema_payload_list,cl_uint num_events_in_wait_list,const cl_event * event_wait_list,cl_event * event,int num_calls)3798 static cl_int clEnqueueSignalSemaphoresKHR_testEnqueueSignalSemaphores(
3799     cl_command_queue command_queue,
3800     cl_uint num_sema_objects,
3801     const cl_semaphore_khr* sema_objects,
3802     const cl_semaphore_payload_khr* sema_payload_list,
3803     cl_uint num_events_in_wait_list,
3804     const cl_event* event_wait_list,
3805     cl_event* event,
3806     int num_calls)
3807 {
3808     TEST_ASSERT_EQUAL(0, num_calls);
3809     TEST_ASSERT_EQUAL_PTR(commandQueuePool[1](), command_queue);
3810     TEST_ASSERT_EQUAL(1, num_sema_objects);
3811     TEST_ASSERT_NOT_NULL(sema_objects);
3812     TEST_ASSERT_EQUAL(make_semaphore_khr(2), *sema_objects);
3813     TEST_ASSERT_NOT_NULL(sema_payload_list);
3814     TEST_ASSERT_EQUAL(0, num_events_in_wait_list);
3815     TEST_ASSERT_NULL(event_wait_list);
3816 
3817     if (event != nullptr)
3818     {
3819         *event = make_event(2);
3820     }
3821 
3822     return CL_SUCCESS;
3823 }
3824 
testEnqueueSignalSemaphores(void)3825 void testEnqueueSignalSemaphores(void)
3826 {
3827     clEnqueueSignalSemaphoresKHR_StubWithCallback(clEnqueueSignalSemaphoresKHR_testEnqueueSignalSemaphores);
3828 
3829     VECTOR_CLASS<cl::Semaphore> sema_objects;
3830     sema_objects.emplace_back(make_semaphore_khr(2));
3831     VECTOR_CLASS<cl_semaphore_payload_khr> sema_payloads(1);
3832     cl::Event event;
3833 
3834     cl_int status = commandQueuePool[1].enqueueSignalSemaphores(sema_objects, sema_payloads, nullptr, &event);
3835     TEST_ASSERT_EQUAL(CL_SUCCESS, status);
3836     TEST_ASSERT_EQUAL_PTR(make_event(2), event());
3837 
3838     // prevent destructor from interfering with the test
3839     event() = nullptr;
3840     sema_objects[0]() = nullptr;
3841 }
3842 
clCreateSemaphoreWithProperties_testSemaphoreWithProperties(cl_context context,const cl_semaphore_properties_khr * sema_props,cl_int * errcode_ret,int num_calls)3843 cl_semaphore_khr clCreateSemaphoreWithProperties_testSemaphoreWithProperties(
3844     cl_context context,
3845     const cl_semaphore_properties_khr* sema_props,
3846     cl_int* errcode_ret,
3847     int num_calls)
3848 {
3849     TEST_ASSERT_EQUAL(0, num_calls);
3850     TEST_ASSERT_EQUAL_PTR(context, contextPool[0]());
3851     TEST_ASSERT_NOT_NULL(sema_props);
3852     TEST_ASSERT_EQUAL(CL_SEMAPHORE_TYPE_KHR, *sema_props);
3853     TEST_ASSERT_NOT_NULL(errcode_ret);
3854     *errcode_ret = CL_SUCCESS;
3855     return make_semaphore_khr(1);
3856 }
3857 
testSemaphoreWithProperties(void)3858 void testSemaphoreWithProperties(void)
3859 {
3860     cl_device_id expected_device = make_device_id(0);
3861     int device_refcount = 1;
3862 
3863     clGetContextInfo_StubWithCallback(clGetContextInfo_device);
3864     clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
3865     clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_1_1);
3866     prepare_deviceRefcounts(1, &expected_device, &device_refcount);
3867 
3868     clCreateSemaphoreWithPropertiesKHR_StubWithCallback(clCreateSemaphoreWithProperties_testSemaphoreWithProperties);
3869 
3870     VECTOR_CLASS<cl_semaphore_properties_khr> sema_props{CL_SEMAPHORE_TYPE_KHR};
3871     cl_int err = CL_INVALID_OPERATION;
3872     cl::Semaphore sem(contextPool[0], sema_props, &err);
3873 
3874     TEST_ASSERT_EQUAL(CL_SUCCESS, err);
3875     TEST_ASSERT_EQUAL_PTR(make_semaphore_khr(1), sem());
3876 
3877     // prevent destructor from interfering with the test
3878     sem() = nullptr;
3879 }
3880 
clGetSemaphoreInfoKHR_testSemaphoreGetContext(cl_semaphore_khr sema_object,cl_semaphore_info_khr param_name,size_t param_value_size,void * param_value,size_t * param_value_size_ret,int num_calls)3881 static cl_int clGetSemaphoreInfoKHR_testSemaphoreGetContext(
3882     cl_semaphore_khr sema_object,
3883     cl_semaphore_info_khr param_name,
3884     size_t param_value_size,
3885     void* param_value,
3886     size_t* param_value_size_ret,
3887     int num_calls)
3888 {
3889     (void) num_calls;
3890     TEST_ASSERT_EQUAL_PTR(semaphorePool[0](), sema_object);
3891     TEST_ASSERT_EQUAL_HEX(CL_SEMAPHORE_CONTEXT_KHR, param_name);
3892     TEST_ASSERT(param_value == nullptr || param_value_size >= sizeof(cl_context));
3893     if (param_value_size_ret != nullptr)
3894         *param_value_size_ret = sizeof(cl_context);
3895     if (param_value != nullptr)
3896         *static_cast<cl_context *>(param_value) = make_context(0);
3897 
3898     return CL_SUCCESS;
3899 }
3900 
testSemaphoreGetInfoContext(void)3901 void testSemaphoreGetInfoContext(void)
3902 {
3903     cl_context expected_context = make_context(0);
3904     int context_refcount = 1;
3905     clGetSemaphoreInfoKHR_StubWithCallback(clGetSemaphoreInfoKHR_testSemaphoreGetContext);
3906     prepare_contextRefcounts(1, &expected_context, &context_refcount);
3907 
3908     cl_int err = CL_INVALID_OPERATION;
3909 
3910     cl::Context ctx = semaphorePool[0].getInfo<CL_SEMAPHORE_CONTEXT_KHR>(&err);
3911 
3912     TEST_ASSERT_EQUAL(CL_SUCCESS, err);
3913     TEST_ASSERT_EQUAL_PTR(make_context(0), ctx());
3914 }
3915 
clGetSemaphoreInfoKHR_testSemaphoreGetReferenceCount(cl_semaphore_khr sema_object,cl_semaphore_info_khr param_name,size_t param_value_size,void * param_value,size_t * param_value_size_ret,int num_calls)3916 static cl_int clGetSemaphoreInfoKHR_testSemaphoreGetReferenceCount(
3917     cl_semaphore_khr sema_object,
3918     cl_semaphore_info_khr param_name,
3919     size_t param_value_size,
3920     void* param_value,
3921     size_t* param_value_size_ret,
3922     int num_calls)
3923 {
3924     (void) num_calls;
3925     TEST_ASSERT_EQUAL_PTR(semaphorePool[0](), sema_object);
3926     TEST_ASSERT_EQUAL_HEX(CL_SEMAPHORE_REFERENCE_COUNT_KHR, param_name);
3927     TEST_ASSERT(param_value == nullptr || param_value_size >= sizeof(cl_uint));
3928     if (param_value_size_ret != nullptr)
3929         *param_value_size_ret = sizeof(cl_uint);
3930     if (param_value != nullptr)
3931         *static_cast<cl_uint *>(param_value) = 1;
3932 
3933     return CL_SUCCESS;
3934 }
3935 
testSemaphoreGetInfoReferenceCount(void)3936 void testSemaphoreGetInfoReferenceCount(void)
3937 {
3938     clGetSemaphoreInfoKHR_StubWithCallback(clGetSemaphoreInfoKHR_testSemaphoreGetReferenceCount);
3939 
3940     cl_int err = CL_INVALID_OPERATION;
3941 
3942     cl_uint ret = semaphorePool[0].getInfo<CL_SEMAPHORE_REFERENCE_COUNT_KHR>(&err);
3943 
3944     TEST_ASSERT_EQUAL(CL_SUCCESS, err);
3945     TEST_ASSERT_EQUAL(1, ret);
3946 }
3947 
clGetSemaphoreInfoKHR_testSemaphoreGetProperties(cl_semaphore_khr sema_object,cl_semaphore_info_khr param_name,size_t param_value_size,void * param_value,size_t * param_value_size_ret,int num_calls)3948 static cl_int clGetSemaphoreInfoKHR_testSemaphoreGetProperties(
3949     cl_semaphore_khr sema_object,
3950     cl_semaphore_info_khr param_name,
3951     size_t param_value_size,
3952     void* param_value,
3953     size_t* param_value_size_ret,
3954     int num_calls)
3955 {
3956     (void) num_calls;
3957     static const cl_semaphore_properties_khr test_properties[] =
3958         {CL_SEMAPHORE_TYPE_KHR,
3959          CL_SEMAPHORE_TYPE_BINARY_KHR};
3960     TEST_ASSERT_EQUAL_PTR(semaphorePool[0](), sema_object);
3961     TEST_ASSERT_EQUAL_HEX(CL_SEMAPHORE_PROPERTIES_KHR, param_name);
3962     TEST_ASSERT(param_value == nullptr || param_value_size >= sizeof(test_properties));
3963     if (param_value_size_ret != nullptr)
3964         *param_value_size_ret = sizeof(test_properties);
3965     if (param_value != nullptr) {
3966         static_cast<cl_semaphore_properties_khr *>(param_value)[0] = test_properties[0];
3967         static_cast<cl_semaphore_properties_khr *>(param_value)[1] = test_properties[1];
3968     }
3969 
3970     return CL_SUCCESS;
3971 }
3972 
testSemaphoreGetInfoProperties(void)3973 void testSemaphoreGetInfoProperties(void)
3974 {
3975     clGetSemaphoreInfoKHR_StubWithCallback(clGetSemaphoreInfoKHR_testSemaphoreGetProperties);
3976 
3977     cl_int err = CL_INVALID_OPERATION;
3978 
3979     VECTOR_CLASS<cl_semaphore_properties_khr> ret = semaphorePool[0].getInfo<CL_SEMAPHORE_PROPERTIES_KHR>(&err);
3980 
3981     TEST_ASSERT_EQUAL(CL_SUCCESS, err);
3982     TEST_ASSERT_EQUAL(2, ret.size());
3983     TEST_ASSERT_EQUAL(CL_SEMAPHORE_TYPE_KHR, ret[0]);
3984     TEST_ASSERT_EQUAL(CL_SEMAPHORE_TYPE_BINARY_KHR, ret[1]);
3985 }
3986 
clGetSemaphoreInfoKHR_testSemaphoreGetType(cl_semaphore_khr sema_object,cl_semaphore_info_khr param_name,size_t param_value_size,void * param_value,size_t * param_value_size_ret,int num_calls)3987 static cl_int clGetSemaphoreInfoKHR_testSemaphoreGetType(
3988     cl_semaphore_khr sema_object,
3989     cl_semaphore_info_khr param_name,
3990     size_t param_value_size,
3991     void* param_value,
3992     size_t* param_value_size_ret,
3993     int num_calls)
3994 {
3995     (void) num_calls;
3996     TEST_ASSERT_EQUAL_PTR(semaphorePool[0](), sema_object);
3997     TEST_ASSERT_EQUAL_HEX(CL_SEMAPHORE_TYPE_KHR, param_name);
3998     TEST_ASSERT(param_value == nullptr || param_value_size >= sizeof(cl_semaphore_type_khr));
3999     if (param_value_size_ret != nullptr)
4000         *param_value_size_ret = sizeof(cl_semaphore_type_khr);
4001     if (param_value != nullptr)
4002         *static_cast<cl_semaphore_type_khr *>(param_value) = CL_SEMAPHORE_TYPE_BINARY_KHR;
4003 
4004     return CL_SUCCESS;
4005 }
4006 
testSemaphoreGetInfoType(void)4007 void testSemaphoreGetInfoType(void)
4008 {
4009     clGetSemaphoreInfoKHR_StubWithCallback(clGetSemaphoreInfoKHR_testSemaphoreGetType);
4010 
4011     cl_int err = CL_INVALID_OPERATION;
4012 
4013     cl_semaphore_type_khr ret = semaphorePool[0].getInfo<CL_SEMAPHORE_TYPE_KHR>(&err);
4014 
4015     TEST_ASSERT_EQUAL(CL_SUCCESS, err);
4016     TEST_ASSERT_EQUAL(CL_SEMAPHORE_TYPE_BINARY_KHR, ret);
4017 }
4018 
clGetSemaphoreInfoKHR_testSemaphoreGetPayload(cl_semaphore_khr sema_object,cl_semaphore_info_khr param_name,size_t param_value_size,void * param_value,size_t * param_value_size_ret,int num_calls)4019 static cl_int clGetSemaphoreInfoKHR_testSemaphoreGetPayload(
4020     cl_semaphore_khr sema_object,
4021     cl_semaphore_info_khr param_name,
4022     size_t param_value_size,
4023     void* param_value,
4024     size_t* param_value_size_ret,
4025     int num_calls)
4026 {
4027     (void) num_calls;
4028     TEST_ASSERT_EQUAL_PTR(semaphorePool[0](), sema_object);
4029     TEST_ASSERT_EQUAL_HEX(CL_SEMAPHORE_PAYLOAD_KHR, param_name);
4030     TEST_ASSERT(param_value == nullptr || param_value_size >= sizeof(cl_semaphore_payload_khr));
4031     if (param_value_size_ret != nullptr)
4032         *param_value_size_ret = sizeof(cl_semaphore_payload_khr);
4033     if (param_value != nullptr)
4034         *static_cast<cl_semaphore_payload_khr *>(param_value) = 1;
4035 
4036     return CL_SUCCESS;
4037 }
4038 
testSemaphoreGetInfoPayload(void)4039 void testSemaphoreGetInfoPayload(void)
4040 {
4041     clGetSemaphoreInfoKHR_StubWithCallback(clGetSemaphoreInfoKHR_testSemaphoreGetPayload);
4042 
4043     cl_int err = CL_INVALID_OPERATION;
4044 
4045     cl_semaphore_payload_khr ret = semaphorePool[0].getInfo<CL_SEMAPHORE_PAYLOAD_KHR>(&err);
4046 
4047     TEST_ASSERT_EQUAL(CL_SUCCESS, err);
4048     TEST_ASSERT_EQUAL(1, ret);
4049 }
4050 
4051 #if defined(CL_SEMAPHORE_DEVICE_HANDLE_LIST_KHR)
clGetSemaphoreInfoKHR_testSemaphoreGetDevices(cl_semaphore_khr sema_object,cl_semaphore_info_khr param_name,size_t param_value_size,void * param_value,size_t * param_value_size_ret,int num_calls)4052 static cl_int clGetSemaphoreInfoKHR_testSemaphoreGetDevices(
4053     cl_semaphore_khr sema_object,
4054     cl_semaphore_info_khr param_name,
4055     size_t param_value_size,
4056     void* param_value,
4057     size_t* param_value_size_ret,
4058     int num_calls)
4059 {
4060     (void) num_calls;
4061     static const cl_device_id test_devices[] =
4062         {make_device_id(0), make_device_id(1)};
4063     TEST_ASSERT_EQUAL_PTR(semaphorePool[0](), sema_object);
4064     TEST_ASSERT_EQUAL_HEX(CL_SEMAPHORE_DEVICE_HANDLE_LIST_KHR, param_name);
4065     TEST_ASSERT(param_value == nullptr || param_value_size >= sizeof(test_devices));
4066     if (param_value_size_ret != nullptr)
4067         *param_value_size_ret = sizeof(test_devices);
4068     if (param_value != nullptr) {
4069         static_cast<cl_device_id *>(param_value)[0] = test_devices[0];
4070         static_cast<cl_device_id *>(param_value)[1] = test_devices[1];
4071     }
4072 
4073     return CL_SUCCESS;
4074 }
4075 
testSemaphoreGetInfoDevicesList(void)4076 void testSemaphoreGetInfoDevicesList(void)
4077 {
4078     cl_device_id expected_devices[] = {make_device_id(0), make_device_id(1)};
4079     int device_refcounts[] = {1, 1};
4080 
4081     clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
4082     clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_1_1);
4083     prepare_deviceRefcounts(ARRAY_SIZE(expected_devices), expected_devices, device_refcounts);
4084 
4085     clGetSemaphoreInfoKHR_StubWithCallback(clGetSemaphoreInfoKHR_testSemaphoreGetDevices);
4086 
4087     cl_int err = CL_INVALID_OPERATION;
4088 
4089     VECTOR_CLASS<cl::Device> ret = semaphorePool[0].getInfo<CL_SEMAPHORE_DEVICE_HANDLE_LIST_KHR>(&err);
4090 
4091     TEST_ASSERT_EQUAL(CL_SUCCESS, err);
4092     TEST_ASSERT_EQUAL(2, ret.size());
4093     TEST_ASSERT_EQUAL(make_device_id(0), ret[0]());
4094     TEST_ASSERT_EQUAL(make_device_id(1), ret[1]());
4095 }
4096 #else
testSemaphoreGetInfoDevicesList(void)4097 void testSemaphoreGetInfoDevicesList(void) {}
4098 #endif
4099 
testSemaphoreRetain(void)4100 void testSemaphoreRetain(void)
4101 {
4102     clRetainSemaphoreKHR_ExpectAndReturn(semaphorePool[0](), CL_SUCCESS);
4103 
4104     cl_int status = semaphorePool[0].retain();
4105     TEST_ASSERT_EQUAL(CL_SUCCESS, status);
4106 }
4107 
testSemaphoreRelease(void)4108 void testSemaphoreRelease(void)
4109 {
4110     clReleaseSemaphoreKHR_ExpectAndReturn(semaphorePool[0](), CL_SUCCESS);
4111 
4112     cl_int status = semaphorePool[0].release();
4113     TEST_ASSERT_EQUAL(CL_SUCCESS, status);
4114 }
4115 
4116 #else
testEnqueueWaitSemaphores(void)4117 void testEnqueueWaitSemaphores(void) {}
testEnqueueSignalSemaphores(void)4118 void testEnqueueSignalSemaphores(void) {}
testSemaphoreWithProperties(void)4119 void testSemaphoreWithProperties(void) {}
testSemaphoreGetInfoContext(void)4120 void testSemaphoreGetInfoContext(void) {}
testSemaphoreGetInfoReferenceCount(void)4121 void testSemaphoreGetInfoReferenceCount(void) {}
testSemaphoreGetInfoProperties(void)4122 void testSemaphoreGetInfoProperties(void) {}
testSemaphoreGetInfoType(void)4123 void testSemaphoreGetInfoType(void) {}
testSemaphoreGetInfoPayload(void)4124 void testSemaphoreGetInfoPayload(void) {}
testSemaphoreGetInfoDevicesList(void)4125 void testSemaphoreGetInfoDevicesList(void) {}
testSemaphoreRetain(void)4126 void testSemaphoreRetain(void) {}
testSemaphoreRelease(void)4127 void testSemaphoreRelease(void) {}
4128 #endif // cl_khr_semaphore
4129 // Tests for external semaphores:
4130 #if defined(cl_khr_external_semaphore)
4131 
make_external_semaphore_handle(cl_external_semaphore_handle_type_khr handle_type)4132 static void* make_external_semaphore_handle(
4133     cl_external_semaphore_handle_type_khr handle_type )
4134 {
4135     return (void*)(uintptr_t)(handle_type << 16 | 0x1111);
4136 }
4137 
make_external_semaphore_fd(cl_external_semaphore_handle_type_khr handle_type)4138 static int make_external_semaphore_fd(
4139     cl_external_semaphore_handle_type_khr handle_type)
4140 {
4141     return (int)(handle_type << 16 | 0x2222);
4142 }
4143 
clGetSemaphoreHandleForTypeKHR_GetHandles(cl_semaphore_khr sema_object,cl_device_id device,cl_external_semaphore_handle_type_khr handle_type,size_t handle_size,void * handle_ptr,size_t * handle_size_ret,int num_calls)4144 static cl_int clGetSemaphoreHandleForTypeKHR_GetHandles(
4145     cl_semaphore_khr sema_object,
4146     cl_device_id device,
4147     cl_external_semaphore_handle_type_khr handle_type,
4148     size_t handle_size,
4149     void* handle_ptr,
4150     size_t* handle_size_ret,
4151     int num_calls)
4152 {
4153     (void) sema_object;
4154     (void) device;
4155     (void) num_calls;
4156 
4157     switch (handle_type) {
4158 #if defined(cl_khr_external_semaphore_dx_fence)
4159     case CL_SEMAPHORE_HANDLE_D3D12_FENCE_KHR:
4160     {
4161         void* ret = make_external_semaphore_handle(handle_type);
4162         if (handle_size == sizeof(ret) && handle_ptr) {
4163             void** pHandle = static_cast<void**>(handle_ptr);
4164             *pHandle = ret;
4165         }
4166         if (handle_size_ret) {
4167             *handle_size_ret = sizeof(ret);
4168         }
4169         return CL_SUCCESS;
4170     }
4171 #endif
4172 #if defined(cl_khr_external_semaphore_win32)
4173     case CL_SEMAPHORE_HANDLE_OPAQUE_WIN32_KHR:
4174     case CL_SEMAPHORE_HANDLE_OPAQUE_WIN32_KMT_KHR:
4175     {
4176         void* ret = make_external_semaphore_handle(handle_type);
4177         if (handle_size == sizeof(ret) && handle_ptr) {
4178             void** pHandle = static_cast<void**>(handle_ptr);
4179             *pHandle = ret;
4180         }
4181         if (handle_size_ret) {
4182             *handle_size_ret = sizeof(ret);
4183         }
4184         return CL_SUCCESS;
4185     }
4186 #endif
4187 #if defined(cl_khr_external_semaphore_opaque_fd)
4188     case CL_SEMAPHORE_HANDLE_OPAQUE_FD_KHR:
4189     {
4190         int ret = make_external_semaphore_fd(handle_type);
4191         if (handle_size == sizeof(ret) && handle_ptr) {
4192             int* pHandle = static_cast<int*>(handle_ptr);
4193             *pHandle = ret;
4194         }
4195         if (handle_size_ret) {
4196             *handle_size_ret = sizeof(ret);
4197         }
4198         return CL_SUCCESS;
4199     }
4200 #endif
4201 #if defined(cl_khr_external_semaphore_opaque_fd)
4202     case CL_SEMAPHORE_HANDLE_SYNC_FD_KHR:
4203     {
4204         int ret = make_external_semaphore_fd(handle_type);
4205         if (handle_size == sizeof(ret) && handle_ptr) {
4206             int* pHandle = static_cast<int*>(handle_ptr);
4207             *pHandle = ret;
4208         }
4209         if (handle_size_ret) {
4210             *handle_size_ret = sizeof(ret);
4211         }
4212         return CL_SUCCESS;
4213     }
4214 #endif
4215     default: break;
4216     }
4217     TEST_FAIL();
4218     return CL_INVALID_OPERATION;
4219 }
4220 
testTemplateGetSemaphoreHandleForTypeKHR(void)4221 void testTemplateGetSemaphoreHandleForTypeKHR(void)
4222 {
4223     clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
4224     clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_2_0);
4225     clReleaseDevice_ExpectAndReturn(make_device_id(0), CL_SUCCESS);
4226 
4227     cl::Device device(make_device_id(0));
4228 
4229     clGetSemaphoreHandleForTypeKHR_StubWithCallback(clGetSemaphoreHandleForTypeKHR_GetHandles);
4230 
4231     cl::Semaphore semaphore;
4232 #if defined(cl_khr_external_semaphore_dx_fence)
4233     {
4234         auto handle0 = semaphore.getHandleForTypeKHR<cl::ExternalSemaphoreType::D3D12Fence>(device);
4235         TEST_ASSERT_EQUAL(handle0, make_external_semaphore_handle(cl::ExternalSemaphoreType::D3D12Fence));
4236     }
4237 #endif
4238 #if defined(cl_khr_external_semaphore_opaque_fd)
4239     {
4240         auto fd0 = semaphore.getHandleForTypeKHR<cl::ExternalSemaphoreType::OpaqueFd>(device);
4241         TEST_ASSERT_EQUAL(fd0, make_external_semaphore_fd(cl::ExternalSemaphoreType::OpaqueFd));
4242     }
4243 #endif
4244 #if defined(cl_khr_external_semaphore_sync_fd)
4245     {
4246         auto fd1 = semaphore.getHandleForTypeKHR<cl::ExternalSemaphoreType::SyncFd>(device);
4247         TEST_ASSERT_EQUAL(fd1, make_external_semaphore_fd(cl::ExternalSemaphoreType::SyncFd));
4248     }
4249 #endif
4250 #if defined(cl_khr_external_semaphore_win32)
4251     {
4252         auto handle1 = semaphore.getHandleForTypeKHR<cl::ExternalSemaphoreType::OpaqueWin32>(device);
4253         TEST_ASSERT_EQUAL(handle1, make_external_semaphore_handle(cl::ExternalSemaphoreType::OpaqueWin32));
4254         auto handle2 = semaphore.getHandleForTypeKHR<cl::ExternalSemaphoreType::OpaqueWin32Kmt>(device);
4255         TEST_ASSERT_EQUAL(handle2, make_external_semaphore_handle(cl::ExternalSemaphoreType::OpaqueWin32Kmt));
4256     }
4257 #endif
4258 }
4259 #else
testTemplateGetSemaphoreHandleForTypeKHR()4260 void testTemplateGetSemaphoreHandleForTypeKHR() {}
4261 #endif // defined(cl_khr_external_semaphore)
4262 
4263 /****************************************************************************
4264  * Tests for cl_khr_external_memory
4265  ****************************************************************************/
4266 #ifdef cl_khr_external_memory
clEnqueueAcquireExternalMemObjectsKHR_testEnqueueAcquireExternalMemObjects(cl_command_queue command_queue,cl_uint num_mem_objects,const cl_mem * mem_objects,cl_uint num_events_in_wait_list,const cl_event * event_wait_list,cl_event * event,int num_calls)4267 static cl_int clEnqueueAcquireExternalMemObjectsKHR_testEnqueueAcquireExternalMemObjects(
4268     cl_command_queue command_queue,
4269     cl_uint num_mem_objects,
4270     const cl_mem* mem_objects,
4271     cl_uint num_events_in_wait_list,
4272     const cl_event* event_wait_list,
4273     cl_event* event,
4274     int num_calls)
4275 {
4276     TEST_ASSERT_EQUAL(0, num_calls);
4277     TEST_ASSERT_EQUAL_PTR(commandQueuePool[0](), command_queue);
4278     TEST_ASSERT_EQUAL(1, num_mem_objects);
4279     TEST_ASSERT_EQUAL(make_mem(0), *mem_objects);
4280     TEST_ASSERT_EQUAL(0, num_events_in_wait_list);
4281     TEST_ASSERT_NULL(event_wait_list);
4282 
4283     if (event != nullptr)
4284     {
4285         *event = make_event(0);
4286     }
4287 
4288     return CL_SUCCESS;
4289 }
4290 
testEnqueueAcquireExternalMemObjects(void)4291 void testEnqueueAcquireExternalMemObjects(void)
4292 {
4293     clGetCommandQueueInfo_StubWithCallback(clGetCommandQueueInfo_testCommandQueueGetDevice);
4294     clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
4295     clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_1_1);
4296 
4297     clEnqueueAcquireExternalMemObjectsKHR_StubWithCallback(clEnqueueAcquireExternalMemObjectsKHR_testEnqueueAcquireExternalMemObjects);
4298 
4299     VECTOR_CLASS<cl::Memory> mem_objects;
4300     mem_objects.emplace_back(make_mem(0), false);
4301     cl::Event event;
4302 
4303     cl_int status = commandQueuePool[0].enqueueAcquireExternalMemObjects(mem_objects, nullptr, &event);
4304 
4305     TEST_ASSERT_EQUAL(CL_SUCCESS, status);
4306     TEST_ASSERT_EQUAL_PTR(make_event(0), event());
4307 
4308     // prevent destructor from interfering with the test
4309     event() = nullptr;
4310     mem_objects[0]() = nullptr;
4311 }
4312 
clEnqueueReleaseExternalMemObjectsKHR_testEnqueueReleaseExternalMemObjects(cl_command_queue command_queue,cl_uint num_mem_objects,const cl_mem * mem_objects,cl_uint num_events_in_wait_list,const cl_event * event_wait_list,cl_event * event,int num_calls)4313 static cl_int clEnqueueReleaseExternalMemObjectsKHR_testEnqueueReleaseExternalMemObjects(
4314     cl_command_queue command_queue,
4315     cl_uint num_mem_objects,
4316     const cl_mem* mem_objects,
4317     cl_uint num_events_in_wait_list,
4318     const cl_event* event_wait_list,
4319     cl_event* event,
4320     int num_calls)
4321 {
4322     TEST_ASSERT_EQUAL(0, num_calls);
4323     TEST_ASSERT_EQUAL_PTR(commandQueuePool[0](), command_queue);
4324     TEST_ASSERT_EQUAL(1, num_mem_objects);
4325     TEST_ASSERT_EQUAL(make_mem(0), *mem_objects);
4326     TEST_ASSERT_EQUAL(0, num_events_in_wait_list);
4327     TEST_ASSERT_NULL(event_wait_list);
4328 
4329     if (event != nullptr)
4330     {
4331         *event = make_event(0);
4332     }
4333 
4334     return CL_SUCCESS;
4335 }
4336 
testEnqueueReleaseExternalMemObjects(void)4337 void testEnqueueReleaseExternalMemObjects(void)
4338 {
4339     clGetCommandQueueInfo_StubWithCallback(clGetCommandQueueInfo_testCommandQueueGetDevice);
4340     clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
4341     clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_1_1);
4342 
4343     clEnqueueReleaseExternalMemObjectsKHR_StubWithCallback(clEnqueueReleaseExternalMemObjectsKHR_testEnqueueReleaseExternalMemObjects);
4344 
4345     VECTOR_CLASS<cl::Memory> mem_objects;
4346     mem_objects.emplace_back(make_mem(0), false);
4347     cl::Event event;
4348 
4349     cl_int status = commandQueuePool[0].enqueueReleaseExternalMemObjects(mem_objects, nullptr, &event);
4350 
4351     TEST_ASSERT_EQUAL(CL_SUCCESS, status);
4352     TEST_ASSERT_EQUAL_PTR(make_event(0), event());
4353 
4354     // prevent destructor from interfering with the test
4355     event() = nullptr;
4356     mem_objects[0]() = nullptr;
4357 }
4358 
4359 #else
testEnqueueAcquireExternalMemObjects(void)4360 void testEnqueueAcquireExternalMemObjects(void) {}
testEnqueueReleaseExternalMemObjects(void)4361 void testEnqueueReleaseExternalMemObjects(void) {}
4362 #endif // cl_khr_external_memory
4363 
4364 /****************************************************************************
4365  * Tests for allocate and deallocate API (stubs for clSVMAlloc, clSVMFree, clEnqueueSVMMap)
4366  ****************************************************************************/
4367 int *testMemory;
clSVMAllocARM_stubForMemoryAllocation(cl_context context,cl_svm_mem_flags flags,size_t size,cl_uint alignment,int cmock_num_calls)4368 void *clSVMAllocARM_stubForMemoryAllocation(cl_context context,cl_svm_mem_flags flags,
4369                          size_t size, cl_uint alignment, int cmock_num_calls)
4370 {
4371     (void) context;
4372     (void) flags;
4373     (void) alignment;
4374     (void) cmock_num_calls;
4375 
4376     TEST_ASSERT_EQUAL_HEX(size, 3 * sizeof(int));
4377     testMemory = new int[size / sizeof(int)];
4378     return testMemory;
4379 }
clEnqueueSVMMap_stubForMemoryAllocation(cl_command_queue command_queue,cl_bool blocking_map,cl_map_flags flags,void * svm_ptr,size_t size,cl_uint num_events_in_wait_list,const cl_event * event_wait_list,cl_event * event,int cmock_num_calls)4380 cl_int clEnqueueSVMMap_stubForMemoryAllocation(cl_command_queue command_queue, cl_bool blocking_map, cl_map_flags flags, void* svm_ptr, size_t size, cl_uint num_events_in_wait_list, const cl_event* event_wait_list, cl_event* event, int cmock_num_calls)
4381 {
4382     (void) command_queue;
4383     (void) blocking_map;
4384     (void) flags;
4385     (void) num_events_in_wait_list;
4386     (void) event_wait_list;
4387     (void) event;
4388     (void) cmock_num_calls;
4389 
4390     TEST_ASSERT_EQUAL_PTR(svm_ptr, testMemory);
4391     TEST_ASSERT_EQUAL_HEX(size, 3 * sizeof(int));
4392     return CL_SUCCESS;
4393 }
clSVMFree_stubForMemoryAllocation(cl_context context,void * svm_pointer,int cmock_num_calls)4394 void clSVMFree_stubForMemoryAllocation(cl_context context, void *svm_pointer,
4395     int cmock_num_calls)
4396 {
4397     (void) context;
4398     (void) cmock_num_calls;
4399 
4400     TEST_ASSERT_EQUAL_PTR(svm_pointer, testMemory);
4401     delete[] (int*) svm_pointer;
4402 }
testSVMMemoryAllocation(void)4403 void testSVMMemoryAllocation(void)
4404 {
4405 #if CL_HPP_TARGET_OPENCL_VERSION >= 200
4406     cl::SVMAllocator<int, cl::SVMTraitCoarse<>> svmAllocator;
4407 
4408     clSVMAlloc_StubWithCallback(clSVMAllocARM_stubForMemoryAllocation);
4409     clEnqueueSVMMap_StubWithCallback(clEnqueueSVMMap_stubForMemoryAllocation);
4410     clSVMFree_StubWithCallback(clSVMFree_stubForMemoryAllocation);
4411 
4412     int *ptr = svmAllocator.allocate(3);
4413     TEST_ASSERT_EQUAL_PTR(ptr,testMemory);
4414     clSVMFree_Expect(0, ptr);
4415     svmAllocator.deallocate(ptr,3);
4416 #endif
4417 }
4418 
4419 #if defined(cl_ext_image_requirements_info)
4420 constexpr size_t TEST_SIZE_TYPE_VALUE  = 4;
4421 constexpr cl_uint TEST_UINT_VALUE  = 256;
4422 
clGetImageRequirementsInfoEXT_GetInfo(cl_context context,const cl_mem_properties * properties,cl_mem_flags flags,const cl_image_format * image_format,const cl_image_desc * image_desc,cl_image_requirements_info_ext param_name,size_t param_value_size,void * param_value,size_t * param_value_size_ret,int num_calls)4423 static cl_int clGetImageRequirementsInfoEXT_GetInfo(
4424     cl_context context,
4425     const cl_mem_properties* properties,
4426     cl_mem_flags flags,
4427     const cl_image_format* image_format,
4428     const cl_image_desc* image_desc,
4429     cl_image_requirements_info_ext param_name,
4430     size_t param_value_size,
4431     void* param_value,
4432     size_t* param_value_size_ret,
4433     int num_calls)
4434 {
4435     (void) context;
4436     (void) properties;
4437     (void) flags;
4438     (void) image_format;
4439     (void) image_desc;
4440     (void) num_calls;
4441 
4442     switch(param_name)
4443     {
4444         case CL_IMAGE_REQUIREMENTS_ROW_PITCH_ALIGNMENT_EXT:
4445         case CL_IMAGE_REQUIREMENTS_BASE_ADDRESS_ALIGNMENT_EXT:
4446 #if defined(cl_ext_image_from_buffer)
4447         case CL_IMAGE_REQUIREMENTS_SLICE_PITCH_ALIGNMENT_EXT:
4448 #endif
4449         case CL_IMAGE_REQUIREMENTS_SIZE_EXT:
4450         {
4451             size_t ret = 4;
4452             if (param_value_size == sizeof(ret) && param_value) {
4453                 *static_cast<size_t *>(param_value) = ret;
4454             }
4455 
4456             if (param_value_size_ret) {
4457                 *param_value_size_ret = sizeof(ret);
4458             }
4459 
4460             return CL_SUCCESS;
4461         }
4462         case CL_IMAGE_REQUIREMENTS_MAX_WIDTH_EXT:
4463         case CL_IMAGE_REQUIREMENTS_MAX_HEIGHT_EXT:
4464         case CL_IMAGE_REQUIREMENTS_MAX_DEPTH_EXT:
4465         case CL_IMAGE_REQUIREMENTS_MAX_ARRAY_SIZE_EXT:
4466         {
4467             cl_uint ret = TEST_UINT_VALUE;
4468             if (param_value_size == sizeof(ret) && param_value) {
4469                 *static_cast<cl_uint *>(param_value) = ret;
4470             }
4471 
4472             if (param_value_size_ret) {
4473                 *param_value_size_ret = sizeof(ret);
4474             }
4475 
4476             return CL_SUCCESS;
4477         }
4478         default: break;
4479     }
4480     TEST_FAIL();
4481     return CL_INVALID_OPERATION;
4482 }
4483 
testTemplateGetImageRequirementsInfo(void)4484 void testTemplateGetImageRequirementsInfo(void)
4485 {
4486     cl::Context context(make_context(0));
4487     cl_device_id device_expect = make_device_id(0);
4488     int device_refcount = 1;
4489 
4490     prepare_deviceRefcounts(1, &device_expect, &device_refcount);
4491 
4492     clGetContextInfo_StubWithCallback(clGetContextInfo_device);
4493     clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
4494     clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_2_0);
4495     clReleaseContext_ExpectAndReturn(make_context(0), CL_SUCCESS);
4496 
4497     clGetImageRequirementsInfoEXT_StubWithCallback(clGetImageRequirementsInfoEXT_GetInfo);
4498 
4499     auto info0 = context.getImageRequirementsInfoExt<cl::ImageRequirementsInfoExt::RowPitchAlign>();
4500     TEST_ASSERT_EQUAL(info0, TEST_SIZE_TYPE_VALUE);
4501 
4502     auto info1 = context.getImageRequirementsInfoExt<cl::ImageRequirementsInfoExt::BaseAddAlign>();
4503     TEST_ASSERT_EQUAL(info1, TEST_SIZE_TYPE_VALUE);
4504 
4505     auto info2 = context.getImageRequirementsInfoExt<cl::ImageRequirementsInfoExt::Size>();
4506     TEST_ASSERT_EQUAL(info2, TEST_SIZE_TYPE_VALUE);
4507 
4508 #if defined(cl_ext_image_from_buffer)
4509     auto info3 = context.getImageRequirementsInfoExt<cl::ImageRequirementsInfoExt::SlicePitchAlign>();
4510     TEST_ASSERT_EQUAL(info3, TEST_SIZE_TYPE_VALUE);
4511 #endif
4512 
4513     auto info4 = context.getImageRequirementsInfoExt<cl::ImageRequirementsInfoExt::MaxWidth>();
4514     TEST_ASSERT_EQUAL(info4, TEST_UINT_VALUE);
4515 
4516     auto info5 = context.getImageRequirementsInfoExt<cl::ImageRequirementsInfoExt::MaxHeight>();
4517     TEST_ASSERT_EQUAL(info5, TEST_UINT_VALUE);
4518 
4519     auto info6 = context.getImageRequirementsInfoExt<cl::ImageRequirementsInfoExt::MaxDepth>();
4520     TEST_ASSERT_EQUAL(info6, TEST_UINT_VALUE);
4521 
4522     auto info7 = context.getImageRequirementsInfoExt<cl::ImageRequirementsInfoExt::MaxArraySize>();
4523     TEST_ASSERT_EQUAL(info7, TEST_UINT_VALUE);
4524 }
4525 #else
testTemplateGetImageRequirementsInfo()4526 void testTemplateGetImageRequirementsInfo() {}
4527 #endif // cl_ext_image_requirements_info
4528 
clCreateFromGLBuffer_testgetObjectInfo(cl_context context,cl_mem_flags flags,cl_GLuint bufobj,cl_int * errcode_ret,int num_calls)4529 static cl_mem clCreateFromGLBuffer_testgetObjectInfo(cl_context context,
4530                                                      cl_mem_flags flags,
4531                                                      cl_GLuint bufobj,
4532                                                      cl_int *errcode_ret,
4533                                                      int num_calls)
4534 {
4535     TEST_ASSERT_EQUAL(0, bufobj);
4536     TEST_ASSERT_EQUAL_PTR(make_context(0), context);
4537     TEST_ASSERT_EQUAL(0, flags);
4538     if (errcode_ret)
4539         *errcode_ret = CL_SUCCESS;
4540     return make_mem(0);
4541 }
4542 
clGetGLObjectInfo_testgetObjectInfo(cl_mem memobj,cl_gl_object_type * type,cl_GLuint * gl_object_name,int num)4543 static cl_int clGetGLObjectInfo_testgetObjectInfo(cl_mem memobj,
4544                                                   cl_gl_object_type *type,
4545                                                   cl_GLuint *gl_object_name,
4546                                                   int num)
4547 {
4548     TEST_ASSERT_EQUAL(memobj, make_mem(0));
4549     *type = CL_GL_OBJECT_BUFFER;
4550 
4551     *gl_object_name = 0;
4552     return CL_SUCCESS;
4553 }
4554 
testgetObjectInfo()4555 void testgetObjectInfo() {
4556     cl_mem_flags flags = 0;
4557     cl_int err = 0;
4558     cl_GLuint bufobj = 0;
4559     cl_mem memobj = make_mem(0);
4560     cl_gl_object_type type = CL_GL_OBJECT_TEXTURE2D_ARRAY;
4561     clGetGLObjectInfo_StubWithCallback(clGetGLObjectInfo_testgetObjectInfo);
4562     clCreateFromGLBuffer_StubWithCallback(
4563         clCreateFromGLBuffer_testgetObjectInfo);
4564     clReleaseMemObject_ExpectAndReturn(make_mem(0), CL_SUCCESS);
4565     cl::BufferGL buffer(contextPool[0], flags, bufobj, &err);
4566 
4567     TEST_ASSERT_EQUAL_PTR(make_mem(0), buffer());
4568     TEST_ASSERT_EQUAL(CL_SUCCESS, err);
4569 
4570     TEST_ASSERT_EQUAL(buffer.getObjectInfo(&type, &bufobj), CL_SUCCESS);
4571     TEST_ASSERT_EQUAL(type, CL_GL_OBJECT_BUFFER);
4572     TEST_ASSERT_EQUAL(bufobj, 0);
4573 }
4574 #if CL_HPP_TARGET_OPENCL_VERSION >= 210
clGetHostTimer_testgetHostTimer(cl_device_id device,cl_ulong * host_timestamp,int num_calls)4575 static cl_int clGetHostTimer_testgetHostTimer(cl_device_id device,
4576                                               cl_ulong *host_timestamp,
4577                                               int num_calls) {
4578     TEST_ASSERT_EQUAL_PTR(devicePool[0](), device);
4579     TEST_ASSERT_EQUAL(0, num_calls);
4580     *host_timestamp = 1;
4581     return 0;
4582 }
4583 
testgetHostTimer()4584 void testgetHostTimer() {
4585     cl_ulong retVal = 0;
4586     cl_int *error = nullptr;
4587 
4588     clGetHostTimer_StubWithCallback(clGetHostTimer_testgetHostTimer);
4589     retVal = devicePool[0].getHostTimer(error);
4590     TEST_ASSERT_EQUAL(retVal, 1);
4591 }
4592 #else
testgetHostTimer()4593 void testgetHostTimer() {}
4594 #endif // CL_HPP_TARGET_OPENCL_VERSION >= 210
4595 } // extern "C"
4596