summaryrefslogtreecommitdiff
path: root/tests/test_context.cpp
blob: 22ae7257b3b744303b62748a98f8632349cfff6c (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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include "test_context.h"
#include "CL/cl.h"

START_TEST (test_create_context)
{
    cl_platform_id platform = 0;
    cl_device_id device, wrong_device;
    cl_int result;
    cl_context ctx;

    struct __attribute__((packed)) {
        cl_context_properties prop_platform;
        cl_platform_id platform;
        cl_context_properties null;
    } _properties;

    const cl_context_properties *properties =
        (const cl_context_properties *)&_properties;

    result = clGetDeviceIDs(platform, CL_DEVICE_TYPE_DEFAULT, 1, &device, 0);
    fail_if(
        result != CL_SUCCESS,
        "unable to get a device"
    );

    _properties.prop_platform = CL_CONTEXT_PLATFORM;
    _properties.null = 0;

    ctx = clCreateContext(properties, 1, 0, 0, 0, &result);
    fail_if(
        result != CL_INVALID_VALUE || ctx != 0,
        "devices cannot be NULL"
    );

    ctx = clCreateContext(properties, 0, &device, 0, 0, &result);
    fail_if(
        result != CL_INVALID_VALUE || ctx != 0,
        "num_devices cannot be 0"
    );

    _properties.platform = (cl_platform_id)1337;

    ctx = clCreateContext(properties, 1, &device, 0, 0, &result);
    fail_if(
        result != CL_INVALID_PLATFORM || ctx != 0,
        "1337 is not a valid platform"
    );

    _properties.platform = platform;
    _properties.prop_platform = 1337;

    ctx = clCreateContext(properties, 1, &device, 0, 0, &result);
    fail_if(
        result != CL_INVALID_PROPERTY || ctx != 0,
        "1337 is not a valid cl_context_properties"
    );

    _properties.prop_platform = CL_CONTEXT_PLATFORM;

    ctx = clCreateContext(properties, 1, &device, 0, (void *)&device, &result);
    fail_if(
        result != CL_INVALID_VALUE || ctx != 0,
        "user_data must be NULL if pfn_notify is NULL"
    );

    wrong_device = 0;

    ctx = clCreateContext(properties, 1, &wrong_device, 0, 0, &result);
    fail_if(
        result != CL_INVALID_DEVICE || ctx != 0,
        "0 is not a valid device"
    );

    ctx = clCreateContext(properties, 1, &device, 0, 0, &result);
    fail_if(
        result != CL_SUCCESS || ctx == 0,
        "unable to create a valid context"
    );

    clReleaseContext(ctx);

    ctx = clCreateContext(properties, 1, &device, 0, 0, 0);
    fail_if(
        ctx == 0,
        "errcode_ret can be NULL"
    );
}
END_TEST

START_TEST (test_create_context_from_type)
{
    cl_context ctx;
    cl_int result;

    ctx = clCreateContextFromType(0, CL_DEVICE_TYPE_DEFAULT, 0, 0, &result);
    fail_if(
        result != CL_SUCCESS || ctx == 0,
        "unable to create a valid context with a device of type default"
    );
}
END_TEST

START_TEST (test_get_context_info)
{
    cl_context ctx;
    cl_int result;
    size_t size_ret;

    union {
        cl_uint refcount, num_devices;
        cl_device_id device;
        struct __attribute__((packed)) {
            cl_context_properties prop_platform;
            cl_platform_id platform;
            cl_context_properties null;
        } properties;
    } context_info;

    const cl_context_properties *properties =
        (const cl_context_properties *)&context_info.properties;

    // Test for a dummy context
    ctx = clCreateContextFromType(0, CL_DEVICE_TYPE_DEFAULT, 0, 0, &result);
    fail_if(
        result != CL_SUCCESS || ctx == 0,
        "unable to create a valid context with a device of type default"
    );

    result = clGetContextInfo(0, CL_CONTEXT_REFERENCE_COUNT, 0, 0, &size_ret);
    fail_if(
        result != CL_INVALID_CONTEXT,
        "0 is not a valid context"
    );

    result = clGetContextInfo(ctx, 1337, 0, 0, &size_ret);
    fail_if(
        result != CL_INVALID_VALUE,
        "1337 is not a valid param_name"
    );

    result = clGetContextInfo(ctx, CL_CONTEXT_REFERENCE_COUNT, 0, &context_info,
                              &size_ret);
    fail_if(
        result != CL_INVALID_VALUE,
        "param_value_size is too small to contain a cl_uint"
    );

    result = clGetContextInfo(ctx, CL_CONTEXT_REFERENCE_COUNT, 0, 0, &size_ret);
    fail_if(
        result != CL_SUCCESS || size_ret != sizeof(cl_uint),
        "we must succeed and say that we'll return a cl_uint"
    );

    // Use a real context and check the return values
    clReleaseContext(ctx);

    context_info.properties.prop_platform = CL_CONTEXT_PLATFORM;
    context_info.properties.platform = 0;
    context_info.properties.null = 0;

    ctx = clCreateContextFromType(properties, CL_DEVICE_TYPE_DEFAULT, 0, 0,
                                  &result);
    fail_if(
        result != CL_SUCCESS || ctx == 0,
        "unable to create a valid context with a device of type default"
    );

    // This call clobbers context_info.properties, so we also check that
    // they are properly std::memcpy'ed by Coal::Context.
    result = clGetContextInfo(ctx, CL_CONTEXT_REFERENCE_COUNT, sizeof(cl_uint),
                              &context_info, &size_ret);
    fail_if(
        result != CL_SUCCESS || context_info.refcount != 1,
        "context's reference count must be 1 here"
    );

    clRetainContext(ctx);

    result = clGetContextInfo(ctx, CL_CONTEXT_REFERENCE_COUNT, sizeof(cl_uint),
                              &context_info, &size_ret);
    fail_if(
        result != CL_SUCCESS || size_ret != sizeof(cl_uint) ||
        context_info.refcount != 2,
        "context's reference count must be 2 here"
    );

    result = clGetContextInfo(ctx, CL_CONTEXT_NUM_DEVICES, sizeof(cl_uint),
                              &context_info, &size_ret);
    fail_if(
        result != CL_SUCCESS || size_ret != sizeof(cl_uint) ||
        context_info.num_devices != 1,
        "we currently support only one device : CPU"
    );

    result = clGetContextInfo(ctx, CL_CONTEXT_DEVICES, sizeof(cl_device_id),
                              &context_info, &size_ret);
    fail_if(
        result != CL_SUCCESS || size_ret != sizeof(cl_device_id) ||
        context_info.device == 0,
        "this context must have a device"
    );

    result = clGetContextInfo(ctx, CL_CONTEXT_PROPERTIES,
                              sizeof(context_info.properties), &context_info,
                              &size_ret);
    fail_if(
        result != CL_SUCCESS || size_ret != sizeof(context_info.properties) ||
        context_info.properties.prop_platform != CL_CONTEXT_PLATFORM,
        "this context must have a valid CL_CONTEXT_PLATFORM property"
    );
}
END_TEST

TCase *cl_context_tcase_create(void)
{
    TCase *tc = NULL;
    tc = tcase_create("context");
    tcase_add_test(tc, test_create_context);
    tcase_add_test(tc, test_create_context_from_type);
    tcase_add_test(tc, test_get_context_info);
    return tc;
}