Commit 477f4d4e authored by Shihao Shen's avatar Shihao Shen Committed by Anas Nashif
Browse files

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: default avatarShihao Shen <shihao.shen@intel.com>
parent 0f93d589
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -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);
@@ -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),
+13 −2
Original line number Diff line number Diff line
@@ -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.
@@ -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);
}

/**
+51 −4
Original line number Diff line number Diff line
@@ -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);
@@ -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);
	}
}
@@ -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);
	}
@@ -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,
@@ -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()