Skip to content

Commit c3cc2e4

Browse files
ycsincarlescufi
authored andcommitted
posix: pthread: test the priority conversion functions
Made the conversion functions non-static and added ztests for them to make sure that they work across the full range of Zephyr priorities. Signed-off-by: Yong Cong Sin <[email protected]>
1 parent 5ad7f4b commit c3cc2e4

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

lib/posix/pthread.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,8 @@ static bool is_posix_policy_prio_valid(uint32_t priority, int policy)
227227
return false;
228228
}
229229

230-
static int zephyr_to_posix_priority(int z_prio, int *policy)
230+
/* Non-static so that they can be tested in ztest */
231+
int zephyr_to_posix_priority(int z_prio, int *policy)
231232
{
232233
if (z_prio < 0) {
233234
__ASSERT_NO_MSG(-z_prio <= CONFIG_NUM_COOP_PRIORITIES);
@@ -239,7 +240,8 @@ static int zephyr_to_posix_priority(int z_prio, int *policy)
239240
return ZEPHYR_TO_POSIX_PRIORITY(z_prio);
240241
}
241242

242-
static int posix_to_zephyr_priority(int priority, int policy)
243+
/* Non-static so that they can be tested in ztest */
244+
int posix_to_zephyr_priority(int priority, int policy)
243245
{
244246
if (policy == SCHED_FIFO) {
245247
/* COOP: highest [-CONFIG_NUM_COOP_PRIORITIES, -1] lowest */

tests/posix/common/src/pthread.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,39 @@ static void *thread_top_term(void *p1)
215215
return NULL;
216216
}
217217

218+
/* Test the internal priority conversion functions */
219+
int zephyr_to_posix_priority(int z_prio, int *policy);
220+
int posix_to_zephyr_priority(int priority, int policy);
221+
ZTEST(pthread, test_pthread_priority_conversion)
222+
{
223+
/*
224+
* ZEPHYR [-CONFIG_NUM_COOP_PRIORITIES, -1]
225+
* TO
226+
* POSIX(FIFO) [0, CONFIG_NUM_COOP_PRIORITIES - 1]
227+
*/
228+
for (int z_prio = -CONFIG_NUM_COOP_PRIORITIES, prio = CONFIG_NUM_COOP_PRIORITIES - 1,
229+
p_prio, policy;
230+
z_prio <= -1; z_prio++, prio--) {
231+
p_prio = zephyr_to_posix_priority(z_prio, &policy);
232+
zassert_equal(policy, SCHED_FIFO);
233+
zassert_equal(p_prio, prio, "%d %d\n", p_prio, prio);
234+
zassert_equal(z_prio, posix_to_zephyr_priority(p_prio, SCHED_FIFO));
235+
}
236+
237+
/*
238+
* ZEPHYR [0, CONFIG_NUM_PREEMPT_PRIORITIES - 1]
239+
* TO
240+
* POSIX(RR) [0, CONFIG_NUM_PREEMPT_PRIORITIES - 1]
241+
*/
242+
for (int z_prio = 0, prio = CONFIG_NUM_PREEMPT_PRIORITIES - 1, p_prio, policy;
243+
z_prio < CONFIG_NUM_PREEMPT_PRIORITIES; z_prio++, prio--) {
244+
p_prio = zephyr_to_posix_priority(z_prio, &policy);
245+
zassert_equal(policy, SCHED_RR);
246+
zassert_equal(p_prio, prio, "%d %d\n", p_prio, prio);
247+
zassert_equal(z_prio, posix_to_zephyr_priority(p_prio, SCHED_RR));
248+
}
249+
}
250+
218251
ZTEST(pthread, test_pthread_execution)
219252
{
220253
int i, ret;

0 commit comments

Comments
 (0)