Skip to content

Commit 477f4d4

Browse files
SecureSheIInashif
authored andcommitted
tests: kernel: pipe: second part of testcases to improve pipes coverage
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 #31037. Signed-off-by: Shihao Shen <[email protected]>
1 parent 0f93d58 commit 477f4d4

File tree

3 files changed

+66
-6
lines changed

3 files changed

+66
-6
lines changed

tests/kernel/pipe/pipe_api/src/main.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ extern void test_pipe_get_fail(void);
1818
extern void test_pipe_block_put(void);
1919
extern void test_pipe_block_put_sema(void);
2020
extern void test_pipe_get_put(void);
21+
extern void test_pipe_get_large(void);
2122

2223
extern void test_half_pipe_put_get(void);
2324
extern void test_half_pipe_saturating_block_put(void);
@@ -78,6 +79,7 @@ void test_main(void)
7879
ztest_unit_test(test_pipe_get_fail),
7980
ztest_unit_test(test_half_pipe_put_get),
8081
ztest_unit_test(test_pipe_get_put),
82+
ztest_unit_test(test_pipe_get_large),
8183
ztest_1cpu_unit_test(test_pipe_alloc),
8284
ztest_unit_test(test_pipe_cleanup),
8385
ztest_unit_test(test_pipe_reader_wait),

tests/kernel/pipe/pipe_api/src/test_pipe_avail.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,16 @@ static struct k_pipe pipe = {
2020

2121
static struct k_pipe bufferless;
2222

23+
static struct k_pipe bufferless1 = {
24+
.buffer = data,
25+
.size = 0,
26+
};
27+
2328
/**
24-
* @brief Ensure that bufferless pipes return 0 bytes available
29+
* @brief Pipes with no buffer or size 0 should return 0 bytes available
2530
*
2631
* Pipes can be created to be bufferless (i.e. @ref k_pipe.buffer is `NULL`
27-
* and @ref k_pipe.size is 0).
32+
* or @ref k_pipe.size is 0).
2833
*
2934
* If either of those conditions is true, then @ref k_pipe_read_avail and
3035
* @ref k_pipe_write_avail should return 0.
@@ -45,6 +50,12 @@ void test_pipe_avail_no_buffer(void)
4550

4651
w_avail = k_pipe_write_avail(&bufferless);
4752
zassert_equal(w_avail, 0, "write: expected: 0 actual: %u", w_avail);
53+
54+
r_avail = k_pipe_read_avail(&bufferless1);
55+
zassert_equal(r_avail, 0, "read: expected: 0 actual: %u", r_avail);
56+
57+
w_avail = k_pipe_write_avail(&bufferless1);
58+
zassert_equal(w_avail, 0, "write: expected: 0 actual: %u", w_avail);
4859
}
4960

5061
/**

tests/kernel/pipe/pipe_api/src/test_pipe_contexts.c

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ K_PIPE_DEFINE(kpipe, PIPE_LEN, 4);
2222
K_PIPE_DEFINE(khalfpipe, (PIPE_LEN / 2), 4);
2323
K_PIPE_DEFINE(kpipe1, PIPE_LEN, 4);
2424
K_PIPE_DEFINE(pipe_test_alloc, PIPE_LEN, 4);
25+
K_PIPE_DEFINE(ksmallpipe, 10, 2);
2526
struct k_pipe pipe, pipe1;
2627

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

141142
for (int i = 0; i < PIPE_LEN; i += wt_byte) {
142143
/**TESTPOINT: pipe put*/
143-
to_wt = (PIPE_LEN - i) >= 24 ?
144-
24 : (PIPE_LEN - i);
144+
to_wt = 15;
145145
k_pipe_put(ppipe, &data[i], to_wt, &wt_byte, 1, timeout);
146146
}
147147
}
@@ -154,8 +154,23 @@ static void tpipe_get_small_size(struct k_pipe *ppipe, k_timeout_t timeout)
154154
/*get pipe data from "pipe_put"*/
155155
for (int i = 0; i < PIPE_LEN; i += rd_byte) {
156156
/**TESTPOINT: pipe get*/
157-
to_rd = (PIPE_LEN - i) >= 24 ?
158-
24 : (PIPE_LEN - i);
157+
to_rd = 15;
158+
zassert_false(k_pipe_get(ppipe, &rx_data[i], to_rd,
159+
&rd_byte, 1, timeout), NULL);
160+
}
161+
}
162+
163+
164+
static void tpipe_get_large_size(struct k_pipe *ppipe, k_timeout_t timeout)
165+
{
166+
unsigned char rx_data[PIPE_LEN];
167+
size_t to_rd, rd_byte = 0;
168+
169+
/*get pipe data from "pipe_put"*/
170+
for (int i = 0; i < PIPE_LEN; i += rd_byte) {
171+
/**TESTPOINT: pipe get*/
172+
to_rd = (PIPE_LEN - i) >= 128 ?
173+
128 : (PIPE_LEN - i);
159174
zassert_false(k_pipe_get(ppipe, &rx_data[i], to_rd,
160175
&rd_byte, 1, timeout), NULL);
161176
}
@@ -332,6 +347,16 @@ static void tThread_half_pipe_get(void *p1, void *p2, void *p3)
332347
*/
333348
void test_half_pipe_put_get(void)
334349
{
350+
unsigned char rx_data[PIPE_LEN];
351+
size_t rd_byte = 0;
352+
int ret;
353+
354+
/* TESTPOINT: min_xfer > bytes_to_read */
355+
ret = k_pipe_put(&kpipe, &rx_data[0], 1, &rd_byte, 24, K_NO_WAIT);
356+
zassert_true(ret == -EINVAL, NULL);
357+
ret = k_pipe_put(&kpipe, &rx_data[0], 24, NULL, 1, K_NO_WAIT);
358+
zassert_true(ret == -EINVAL, NULL);
359+
335360
/**TESTPOINT: thread-thread data passing via pipe*/
336361
k_tid_t tid1 = k_thread_create(&tdata1, tstack1, STACK_SIZE,
337362
tThread_half_pipe_get, &khalfpipe,
@@ -382,6 +407,28 @@ void test_pipe_get_put(void)
382407
k_thread_abort(tid2);
383408
}
384409

410+
void test_pipe_get_large(void)
411+
{
412+
/**TESTPOINT: thread-thread data passing via pipe*/
413+
k_tid_t tid1 = k_thread_create(&tdata1, tstack1, STACK_SIZE,
414+
tThread_half_pipe_put, &khalfpipe,
415+
NULL, NULL, K_PRIO_PREEMPT(0),
416+
K_INHERIT_PERMS | K_USER, K_NO_WAIT);
417+
418+
k_tid_t tid2 = k_thread_create(&tdata2, tstack2, STACK_SIZE,
419+
tThread_half_pipe_put, &khalfpipe,
420+
NULL, NULL, K_PRIO_PREEMPT(0),
421+
K_INHERIT_PERMS | K_USER, K_NO_WAIT);
422+
423+
k_sleep(K_MSEC(100));
424+
tpipe_get_large_size(&khalfpipe, K_NO_WAIT);
425+
426+
/* clear the spawned thread avoid side effect */
427+
k_thread_abort(tid1);
428+
k_thread_abort(tid2);
429+
}
430+
431+
385432
/**
386433
* @brief Test pending reader in pipe
387434
* @see k_pipe_put(), k_pipe_get()

0 commit comments

Comments
 (0)