Skip to content

Commit

Permalink
tests: kernel: pipe: second part of testcases to improve pipes coverage
Browse files Browse the repository at this point in the history
Added test_pipe_get_large to cover branches in both k_pipe_put and
k_pipe_get. Added trivial testcases in test_pipe_avail_no_buffer to
cover trivial branches for k_pipe_read_avail and k_pipe_write_avail.
This is the second patch as the continuation of zephyrproject-rtos#31037.

Signed-off-by: Shihao Shen <[email protected]>
  • Loading branch information
SecureSheII authored and nashif committed Mar 18, 2021
1 parent 0f93d58 commit 477f4d4
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 6 deletions.
2 changes: 2 additions & 0 deletions tests/kernel/pipe/pipe_api/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ extern void test_pipe_get_fail(void);
extern void test_pipe_block_put(void);
extern void test_pipe_block_put_sema(void);
extern void test_pipe_get_put(void);
extern void test_pipe_get_large(void);

extern void test_half_pipe_put_get(void);
extern void test_half_pipe_saturating_block_put(void);
Expand Down Expand Up @@ -78,6 +79,7 @@ void test_main(void)
ztest_unit_test(test_pipe_get_fail),
ztest_unit_test(test_half_pipe_put_get),
ztest_unit_test(test_pipe_get_put),
ztest_unit_test(test_pipe_get_large),
ztest_1cpu_unit_test(test_pipe_alloc),
ztest_unit_test(test_pipe_cleanup),
ztest_unit_test(test_pipe_reader_wait),
Expand Down
15 changes: 13 additions & 2 deletions tests/kernel/pipe/pipe_api/src/test_pipe_avail.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@ static struct k_pipe pipe = {

static struct k_pipe bufferless;

static struct k_pipe bufferless1 = {
.buffer = data,
.size = 0,
};

/**
* @brief Ensure that bufferless pipes return 0 bytes available
* @brief Pipes with no buffer or size 0 should return 0 bytes available
*
* Pipes can be created to be bufferless (i.e. @ref k_pipe.buffer is `NULL`
* and @ref k_pipe.size is 0).
* or @ref k_pipe.size is 0).
*
* If either of those conditions is true, then @ref k_pipe_read_avail and
* @ref k_pipe_write_avail should return 0.
Expand All @@ -45,6 +50,12 @@ void test_pipe_avail_no_buffer(void)

w_avail = k_pipe_write_avail(&bufferless);
zassert_equal(w_avail, 0, "write: expected: 0 actual: %u", w_avail);

r_avail = k_pipe_read_avail(&bufferless1);
zassert_equal(r_avail, 0, "read: expected: 0 actual: %u", r_avail);

w_avail = k_pipe_write_avail(&bufferless1);
zassert_equal(w_avail, 0, "write: expected: 0 actual: %u", w_avail);
}

/**
Expand Down
55 changes: 51 additions & 4 deletions tests/kernel/pipe/pipe_api/src/test_pipe_contexts.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ K_PIPE_DEFINE(kpipe, PIPE_LEN, 4);
K_PIPE_DEFINE(khalfpipe, (PIPE_LEN / 2), 4);
K_PIPE_DEFINE(kpipe1, PIPE_LEN, 4);
K_PIPE_DEFINE(pipe_test_alloc, PIPE_LEN, 4);
K_PIPE_DEFINE(ksmallpipe, 10, 2);
struct k_pipe pipe, pipe1;

K_THREAD_STACK_DEFINE(tstack, STACK_SIZE);
Expand Down Expand Up @@ -140,8 +141,7 @@ static void tpipe_put_small_size(struct k_pipe *ppipe, k_timeout_t timeout)

for (int i = 0; i < PIPE_LEN; i += wt_byte) {
/**TESTPOINT: pipe put*/
to_wt = (PIPE_LEN - i) >= 24 ?
24 : (PIPE_LEN - i);
to_wt = 15;
k_pipe_put(ppipe, &data[i], to_wt, &wt_byte, 1, timeout);
}
}
Expand All @@ -154,8 +154,23 @@ static void tpipe_get_small_size(struct k_pipe *ppipe, k_timeout_t timeout)
/*get pipe data from "pipe_put"*/
for (int i = 0; i < PIPE_LEN; i += rd_byte) {
/**TESTPOINT: pipe get*/
to_rd = (PIPE_LEN - i) >= 24 ?
24 : (PIPE_LEN - i);
to_rd = 15;
zassert_false(k_pipe_get(ppipe, &rx_data[i], to_rd,
&rd_byte, 1, timeout), NULL);
}
}


static void tpipe_get_large_size(struct k_pipe *ppipe, k_timeout_t timeout)
{
unsigned char rx_data[PIPE_LEN];
size_t to_rd, rd_byte = 0;

/*get pipe data from "pipe_put"*/
for (int i = 0; i < PIPE_LEN; i += rd_byte) {
/**TESTPOINT: pipe get*/
to_rd = (PIPE_LEN - i) >= 128 ?
128 : (PIPE_LEN - i);
zassert_false(k_pipe_get(ppipe, &rx_data[i], to_rd,
&rd_byte, 1, timeout), NULL);
}
Expand Down Expand Up @@ -332,6 +347,16 @@ static void tThread_half_pipe_get(void *p1, void *p2, void *p3)
*/
void test_half_pipe_put_get(void)
{
unsigned char rx_data[PIPE_LEN];
size_t rd_byte = 0;
int ret;

/* TESTPOINT: min_xfer > bytes_to_read */
ret = k_pipe_put(&kpipe, &rx_data[0], 1, &rd_byte, 24, K_NO_WAIT);
zassert_true(ret == -EINVAL, NULL);
ret = k_pipe_put(&kpipe, &rx_data[0], 24, NULL, 1, K_NO_WAIT);
zassert_true(ret == -EINVAL, NULL);

/**TESTPOINT: thread-thread data passing via pipe*/
k_tid_t tid1 = k_thread_create(&tdata1, tstack1, STACK_SIZE,
tThread_half_pipe_get, &khalfpipe,
Expand Down Expand Up @@ -382,6 +407,28 @@ void test_pipe_get_put(void)
k_thread_abort(tid2);
}

void test_pipe_get_large(void)
{
/**TESTPOINT: thread-thread data passing via pipe*/
k_tid_t tid1 = k_thread_create(&tdata1, tstack1, STACK_SIZE,
tThread_half_pipe_put, &khalfpipe,
NULL, NULL, K_PRIO_PREEMPT(0),
K_INHERIT_PERMS | K_USER, K_NO_WAIT);

k_tid_t tid2 = k_thread_create(&tdata2, tstack2, STACK_SIZE,
tThread_half_pipe_put, &khalfpipe,
NULL, NULL, K_PRIO_PREEMPT(0),
K_INHERIT_PERMS | K_USER, K_NO_WAIT);

k_sleep(K_MSEC(100));
tpipe_get_large_size(&khalfpipe, K_NO_WAIT);

/* clear the spawned thread avoid side effect */
k_thread_abort(tid1);
k_thread_abort(tid2);
}


/**
* @brief Test pending reader in pipe
* @see k_pipe_put(), k_pipe_get()
Expand Down

0 comments on commit 477f4d4

Please sign in to comment.