Skip to content

Commit 423c8df

Browse files
Niranjhana NAnas Nashif
authored andcommitted
tests: kernel: posix: add pthread tests
This test verifies pthread_equal returns the expected values. Signed-off-by: Niranjhana N <[email protected]>
1 parent b56aeec commit 423c8df

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
2+
project(NONE)
3+
4+
target_include_directories(app PRIVATE $ENV{ZEPHYR_BASE}/include/posix)
5+
FILE(GLOB app_sources src/*.c)
6+
target_sources(app PRIVATE ${app_sources})
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CONFIG_ZTEST=y
2+
CONFIG_PTHREAD_IPC=y
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright (c) 2018 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <pthread.h>
8+
#include <ztest.h>
9+
10+
#define STACKSZ 1024
11+
12+
pthread_t thread;
13+
14+
K_THREAD_STACK_ARRAY_DEFINE(stacks, 1, STACKSZ);
15+
16+
void *thread_top(void *p1)
17+
{
18+
thread = pthread_self();
19+
pthread_exit(NULL);
20+
return NULL;
21+
}
22+
23+
void test_pthread_equal(void)
24+
{
25+
int ret = 1;
26+
pthread_attr_t attr;
27+
struct sched_param schedparam;
28+
pthread_t newthread;
29+
30+
pthread_attr_init(&attr);
31+
schedparam.priority = 2;
32+
pthread_attr_setschedparam(&attr, &schedparam);
33+
pthread_attr_setstack(&attr, &stacks[0][0], STACKSZ);
34+
35+
ret = pthread_create(&newthread, &attr, thread_top,
36+
(void *)0);
37+
38+
/*TESTPOINT: Check if thread is created*/
39+
zassert_false(ret, "attempt to create thread failed\n");
40+
41+
pthread_join(newthread, NULL);
42+
43+
/*TESTPOINT: Check if threads are equal*/
44+
zassert_true(pthread_equal(newthread, thread),
45+
"thread IDs should be equal! exiting...\n");
46+
47+
/*TESTPOINT: Check case when threads are not equal*/
48+
zassert_false(pthread_equal(newthread, k_current_get()),
49+
"thread IDs cannot be equal! exiting...\n");
50+
}
51+
52+
void test_main(void)
53+
{
54+
ztest_test_suite(test_pthreads_equal,
55+
ztest_unit_test(test_pthread_equal));
56+
ztest_run_test_suite(test_pthreads_equal);
57+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
tests:
2+
test:
3+
tags: core

0 commit comments

Comments
 (0)