1// Copyright 2016 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <assert.h>
6#include <errno.h>
7#include <zircon/syscalls.h>
8#include <unittest/unittest.h>
9#include <pthread.h>
10#include <stdio.h>
11
12static void* do_test(void* arg) {
13    int thread_no = *(int*)arg;
14    unittest_printf("do_test for thread: %d\n", thread_no);
15    errno = -thread_no;
16    zx_nanosleep(zx_deadline_after(ZX_MSEC(300)));
17    unittest_printf("comparing result for: %d\n", thread_no);
18    EXPECT_EQ(errno, -thread_no, "Incorrect errno for this thread");
19    return NULL;
20}
21
22bool errno_test(void) {
23    BEGIN_TEST;
24    int main_thread = 1, thread_1 = 2, thread_2 = 3;
25
26    pthread_t thread2, thread3;
27
28    unittest_printf("creating thread: %d\n", thread_1);
29    pthread_create(&thread2, NULL, do_test, &thread_1);
30
31    unittest_printf("creating thread: %d\n", thread_2);
32    pthread_create(&thread3, NULL, do_test, &thread_2);
33
34    do_test(&main_thread);
35
36    unittest_printf("joining thread: %d\n", thread_1);
37    pthread_join(thread2, NULL);
38
39    unittest_printf("joining thread: %d\n", thread_2);
40    pthread_join(thread3, NULL);
41
42    END_TEST;
43}
44
45BEGIN_TEST_CASE(errno_tests)
46RUN_TEST(errno_test)
47END_TEST_CASE(errno_tests)
48
49int main(int argc, char** argv) {
50    return unittest_run_all_tests(argc, argv) ? 0 : -1;
51}
52