File: test-threads.c

package info (click to toggle)
libcork 0.15.0%2Bds-16
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,140 kB
  • sloc: ansic: 12,216; python: 206; sh: 126; makefile: 16
file content (332 lines) | stat: -rw-r--r-- 8,297 bytes parent folder | download | duplicates (4)
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/* -*- coding: utf-8 -*-
 * ----------------------------------------------------------------------
 * Copyright © 2012-2014, RedJack, LLC.
 * All rights reserved.
 *
 * Please see the COPYING file in this distribution for license details.
 * ----------------------------------------------------------------------
 */

#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <check.h>

#include "libcork/core/allocator.h"
#include "libcork/core/types.h"
#include "libcork/threads/atomics.h"
#include "libcork/threads/basics.h"

#include "helpers.h"


/*-----------------------------------------------------------------------
 * Atomics
 */

#define test_atomic_op(name, type, fmt, op, expected) \
    do { \
        type  actual = cork_##name##_atomic_##op(&val, 1); \
        fail_unless_equal(#name, fmt, expected, actual); \
    } while (0)

#define test_cas(name, type, fmt, ov, nv) \
    do { \
        type  actual = cork_##name##_cas(&val, ov, nv); \
        fail_unless_equal(#name, fmt, ov, actual); \
    } while (0)

#define test_atomic(name, type, fmt) \
START_TEST(test_atomic_##name) \
{ \
    DESCRIBE_TEST; \
    volatile type  val = 0; \
    test_atomic_op(name, type, fmt, add, 1); \
    test_atomic_op(name, type, fmt, pre_add, 1); \
    test_atomic_op(name, type, fmt, add, 3); \
    test_atomic_op(name, type, fmt, pre_add, 3); \
    fail_unless_equal(#name, fmt, 4, val); \
    test_atomic_op(name, type, fmt, sub, 3); \
    test_atomic_op(name, type, fmt, pre_sub, 3); \
    test_atomic_op(name, type, fmt, sub, 1); \
    test_atomic_op(name, type, fmt, pre_sub, 1); \
    fail_unless_equal(#name, fmt, 0, val); \
    \
    test_cas(name, type, fmt, 0, 1); \
    test_cas(name, type, fmt, 1, 10); \
    test_cas(name, type, fmt, 10, 2); \
    test_cas(name, type, fmt, 2, 0); \
    fail_unless_equal(#name, fmt, 0, val); \
} \
END_TEST

test_atomic(int,  int,          "%d");
test_atomic(uint, unsigned int, "%u");
test_atomic(size, size_t,       "%zu");

START_TEST(test_atomic_ptr)
{
    DESCRIBE_TEST;

    uint64_t  v0 = 0;
    uint64_t  v1 = 0;
    uint64_t  v2 = 0;
    uint64_t  v3 = 0;
    uint64_t * volatile  val = &v0;

    test_cas(ptr, uint64_t *, "%p", &v0, &v1);
    test_cas(ptr, uint64_t *, "%p", &v1, &v2);
    test_cas(ptr, uint64_t *, "%p", &v2, &v3);
    test_cas(ptr, uint64_t *, "%p", &v3, &v0);
    fail_unless_equal("ptr", "%p", &v0, val);
}
END_TEST


/*-----------------------------------------------------------------------
 * Once
 */

START_TEST(test_once)
{
    DESCRIBE_TEST;

    cork_once_barrier(once);
    static size_t  call_count = 0;
    static int  value = 0;

#define go \
    do { \
        call_count++; \
        value = 1; \
    } while (0)

    cork_once(once, go);
    fail_unless_equal("Value", "%d", 1, value);
    cork_once(once, go);
    fail_unless_equal("Value", "%d", 1, value);
    cork_once(once, go);
    fail_unless_equal("Value", "%d", 1, value);
    cork_once(once, go);
    fail_unless_equal("Value", "%d", 1, value);

    fail_unless_equal("Call count", "%zu", 1, call_count);
}
END_TEST

START_TEST(test_once_recursive)
{
    DESCRIBE_TEST;

    cork_once_barrier(once);
    static size_t  call_count = 0;
    static int  value = 0;

#define go \
    do { \
        call_count++; \
        value = 1; \
    } while (0)

    cork_once_recursive(once, go);
    fail_unless_equal("Value", "%d", 1, value);
    cork_once_recursive(once, go);
    fail_unless_equal("Value", "%d", 1, value);
    cork_once_recursive(once, go);
    fail_unless_equal("Value", "%d", 1, value);
    cork_once_recursive(once, go);
    fail_unless_equal("Value", "%d", 1, value);

    fail_unless_equal("Call count", "%zu", 1, call_count);
}
END_TEST


/*-----------------------------------------------------------------------
 * Thread IDs
 */

START_TEST(test_thread_ids)
{
    DESCRIBE_TEST;
    cork_thread_id  id = cork_current_thread_get_id();
    fail_if(id == CORK_THREAD_NONE, "Expected a valid thread ID");
}
END_TEST


/*-----------------------------------------------------------------------
 * Threads
 */

struct cork_test_thread {
    int  *dest;
    int  value;
};

static int
cork_test_thread__run(void *vself)
{
    struct cork_test_thread  *self = vself;
    *self->dest = self->value;
    return 0;
}

static void
cork_test_thread__free(void *vself)
{
    struct cork_test_thread  *self = vself;
    cork_delete(struct cork_test_thread, self);
}

static struct cork_thread *
cork_test_thread_new(const char *name, int *dest, int value)
{
    struct cork_test_thread  *self = cork_new(struct cork_test_thread);
    self->dest = dest;
    self->value = value;
    return cork_thread_new
        (name, self, cork_test_thread__free, cork_test_thread__run);
}


static int
cork_error_thread__run(void *vself)
{
    /* The particular error doesn't matter; just want to make sure it gets
     * propagated from the cork_thread_join call. */
    cork_system_error_set_explicit(ENOMEM);
    return -1;
}


START_TEST(test_threads_01)
{
    struct cork_thread  *t1;
    int  v1 = -1;

    DESCRIBE_TEST;

    fail_if_error(t1 = cork_test_thread_new("test", &v1, 1));
    fail_unless_equal("Values", "%d", -1, v1);
    cork_thread_free(t1);
}
END_TEST

START_TEST(test_threads_02)
{
    struct cork_thread  *t1;
    int  v1 = -1;

    DESCRIBE_TEST;

    fail_if_error(t1 = cork_test_thread_new("test", &v1, 1));
    fail_if_error(cork_thread_start(t1));
    fail_if_error(cork_thread_join(t1));
    fail_unless_equal("Values", "%d", 1, v1);
}
END_TEST

START_TEST(test_threads_03)
{
    struct cork_thread  *t1;
    struct cork_thread  *t2;
    int  v1 = -1;
    int  v2 = -1;

    DESCRIBE_TEST;

    fail_if_error(t1 = cork_test_thread_new("test1", &v1, 1));
    fail_if_error(t2 = cork_test_thread_new("test2", &v2, 2));
    fail_if_error(cork_thread_start(t1));
    fail_if_error(cork_thread_start(t2));
    fail_if_error(cork_thread_join(t1));
    fail_if_error(cork_thread_join(t2));
    fail_unless_equal("Values", "%d", 1, v1);
    fail_unless_equal("Values", "%d", 2, v2);
}
END_TEST

START_TEST(test_threads_04)
{
    struct cork_thread  *t1;
    struct cork_thread  *t2;
    int  v1 = -1;
    int  v2 = -1;

    DESCRIBE_TEST;

    fail_if_error(t1 = cork_test_thread_new("test1", &v1, 1));
    fail_if_error(t2 = cork_test_thread_new("test2", &v2, 2));
    fail_if_error(cork_thread_start(t1));
    fail_if_error(cork_thread_start(t2));
    fail_if_error(cork_thread_join(t2));
    fail_if_error(cork_thread_join(t1));
    fail_unless_equal("Values", "%d", 1, v1);
    fail_unless_equal("Values", "%d", 2, v2);
}
END_TEST

START_TEST(test_threads_error_01)
{
    DESCRIBE_TEST;
    struct cork_thread  *t1;

    fail_if_error(t1 = cork_thread_new
                  ("test", NULL, NULL, cork_error_thread__run));
    fail_if_error(cork_thread_start(t1));
    fail_unless_error(cork_thread_join(t1));
}
END_TEST


/*-----------------------------------------------------------------------
 * Testing harness
 */

Suite *
test_suite()
{
    Suite  *s = suite_create("threads");

    TCase  *tc_atomic = tcase_create("atomic");
    tcase_add_test(tc_atomic, test_atomic_int);
    tcase_add_test(tc_atomic, test_atomic_uint);
    tcase_add_test(tc_atomic, test_atomic_size);
    tcase_add_test(tc_atomic, test_atomic_ptr);
    suite_add_tcase(s, tc_atomic);

    TCase  *tc_basics = tcase_create("basics");
    tcase_add_test(tc_basics, test_once);
    tcase_add_test(tc_basics, test_once_recursive);
    tcase_add_test(tc_basics, test_thread_ids);
    suite_add_tcase(s, tc_basics);

    TCase  *tc_threads = tcase_create("threads");
    tcase_add_test(tc_threads, test_threads_01);
    tcase_add_test(tc_threads, test_threads_02);
    tcase_add_test(tc_threads, test_threads_03);
    tcase_add_test(tc_threads, test_threads_04);
    tcase_add_test(tc_threads, test_threads_error_01);
    suite_add_tcase(s, tc_threads);

    return s;
}


int
main(int argc, const char **argv)
{
    int  number_failed;
    Suite  *suite = test_suite();
    SRunner  *runner = srunner_create(suite);

    setup_allocator();
    srunner_run_all(runner, CK_NORMAL);
    number_failed = srunner_ntests_failed(runner);
    srunner_free(runner);

    return (number_failed == 0)? EXIT_SUCCESS: EXIT_FAILURE;
}