summaryrefslogtreecommitdiff
path: root/tests/cl/api/create-program-with-source.c
blob: f0d6f3c3da6d20e39d279a712929559c75170afd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
 * Copyright © 2012 Blaž Tomažič <blaz.tomazic@gmail.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

/**
 * @file create-program-with-source.c
 *
 * Test API function:
 *
 *   cl_program clCreateProgramWithSource (cl_context context,
 *                                         cl_uint count,
 *                                         const char **strings,
 *                                         const size_t *lengths,
 *                                         cl_int *errcode_ret)
 */

#include "piglit-framework-cl-api.h"


PIGLIT_CL_API_TEST_CONFIG_BEGIN

	config.name = "clCreateProgramWithSource";
	config.version_min = 10;

	config.run_per_platform = true;
	config.create_context = true;

PIGLIT_CL_API_TEST_CONFIG_END


char* dummy_function = "void dummy_function() {}";
char* dummy_kernel = "kernel void dummy_kernel() { dummy_function(); }";

static void
test(cl_context cl_ctx,
     cl_uint count,
     const char **strings,
     const size_t *lengths,
     cl_int expected_error,
     enum piglit_result* result,
     const char* test_str) {
	cl_int errNo;
	cl_program program;

	/* with errNo */
	program = clCreateProgramWithSource(cl_ctx,
	                                    count,
	                                    strings,
	                                    lengths,
	                                    &errNo);
	
	if(!piglit_cl_check_error(errNo, expected_error)) {
		fprintf(stderr, "Failed (error code: %s): %s.\n",
		        piglit_cl_get_error_name(errNo), test_str);
		piglit_merge_result(result, PIGLIT_FAIL);
		return;
	}
	if(expected_error == CL_SUCCESS) {
		if(program == NULL) {
			printf("Expecting non-NULL cl_program\n");
			fprintf(stderr, "Failed (NULL value returned): %s.\n", test_str);
			piglit_merge_result(result, PIGLIT_FAIL);
			return;
		}
		clReleaseProgram(program);
	} else if (program != NULL) {
		printf("Expecting NULL cl_program\n");
		fprintf(stderr, "Failed (non-NULL value returned): %s.\n", test_str);
		piglit_merge_result(result, PIGLIT_FAIL);
		return;
	}

	/* without errNo */
	program = clCreateProgramWithSource(cl_ctx,
	                                    count,
	                                    strings,
	                                    lengths,
	                                    NULL);
	
	if(expected_error == CL_SUCCESS) {
		if(program == NULL) {
			printf("Expecting non-NULL cl_program\n");
			fprintf(stderr, "Failed (NULL value returned): %s.\n", test_str);
			piglit_merge_result(result, PIGLIT_FAIL);
			return;
		}
		clReleaseProgram(program);
	} else if (program != NULL) {
		printf("Expecting NULL cl_program\n");
		fprintf(stderr, "Failed (non-NULL value returned): %s.\n", test_str);
		piglit_merge_result(result, PIGLIT_FAIL);
		return;
	}
}

enum piglit_result
piglit_cl_test(const int argc,
               const char** argv,
               const struct piglit_cl_api_test_config* config,
               const struct piglit_cl_api_test_env* env)
{
	enum piglit_result result = PIGLIT_PASS;

	int i;

	const char* null = NULL;
	const char** strings = malloc(2 * sizeof(char*));
	size_t* lengths = malloc(2 * sizeof(size_t));

	strings[0] = dummy_function;
	strings[1] = dummy_kernel;
	lengths[0] = strlen(dummy_function);
	lengths[1] = strlen(dummy_kernel);

	/*** Normal usage ***/

	for(i = 0; i < 2; i++) {
		size_t* partial_lengths = malloc(2 * sizeof(size_t));

		/* separate */
		test(env->context->cl_ctx, 1, &strings[i], &lengths[i],
		     CL_SUCCESS, &result,
		     "Create program with 1 source string and defined length");
		test(env->context->cl_ctx, 1, &strings[i], NULL,
		     CL_SUCCESS, &result,
		     "Create program with 1 source string and lenghts == NULL");
		
		/* all, i-th length is 0 */
		partial_lengths[i] = 0;
		partial_lengths[(i+1)%2] = lengths[(i+1)%2];
		
		test(env->context->cl_ctx, 2, strings, partial_lengths,
		     CL_SUCCESS, &result,
		     "Create program with multiple source strings and only some lenghts defined (others are NULL)");

		free(partial_lengths);
	}

	/* all */
	test(env->context->cl_ctx, 2, strings, lengths,
	     CL_SUCCESS, &result,
	     "Create program with multiple source strings and defined lengths");
	test(env->context->cl_ctx, 2, strings, NULL,
	     CL_SUCCESS, &result,
	     "Create program with multiple source strings and lenghts == NULL");
	
	
	/*** Errors ***/

	/*
	 * CL_INVALID_CONTEXT if context is not a valid context.
	 */
	test(NULL, 2, strings, NULL,
	     CL_INVALID_CONTEXT, &result,
	     "Trigger CL_INVALID_CONTEXT when context is not a valid context");

	/*
	 * CL_INVALID_VALUE if count is zero or if strings or
	 * any entry in strings is NULL.
	 */
	test(env->context->cl_ctx, 0, strings, NULL,
	     CL_INVALID_VALUE, &result,
	     "Trigger CL_INVALID_VALUE when count is zero");
	test(env->context->cl_ctx, 0, NULL, NULL,
	     CL_INVALID_VALUE, &result,
	     "Trigger CL_INVALID_VALUE when strings is NULL");
	test(env->context->cl_ctx, 1, &null, NULL,
	     CL_INVALID_VALUE, &result,
	     "Trigger CL_INVALID_VALUE when any entry in strings is NULL");

	free(strings);
	free(lengths);

	return result;
}