xref: /linux/include/rv/kunit.h (revision 8da2a88383658dac97769ed8f807ef6100a69480)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (C) 2026-2029 Red Hat, Inc. Gabriele Monaco <gmonaco@redhat.com>
4  *
5  * Declaration of utilities to run KUnit tests.
6  */
7 
8 #ifndef _RV_KUNIT_H
9 #define _RV_KUNIT_H
10 
11 #if IS_ENABLED(CONFIG_RV_MONITORS_KUNIT_TEST)
12 
13 #include <kunit/test.h>
14 #include <kunit/test-bug.h>
15 #include <linux/delay.h>
16 
17 int rv_set_testing(struct kunit_suite *suite);
18 void rv_clear_testing(struct kunit_suite *suite);
19 
20 #define RV_KUNIT_MAX_MOCK_TASKS 8
21 
22 struct rv_kunit_ctx {
23 	int reactions, expected;
24 	int mock_task_count;
25 	struct task_struct *mock_tasks[RV_KUNIT_MAX_MOCK_TASKS];
26 };
27 
28 #define RV_KUNIT_EXPECT_REACTION(test, ctx)                             \
29 	do {                                                            \
30 		KUNIT_EXPECT_EQ(test, ctx->reactions, ++ctx->expected); \
31 		if (ctx->reactions != ctx->expected)                    \
32 			ctx->expected = ctx->reactions;                 \
33 	} while (0)
34 
35 #define RV_KUNIT_EXPECT_NO_REACTION(test, ctx)                        \
36 	do {                                                          \
37 		KUNIT_EXPECT_EQ(test, ctx->reactions, ctx->expected); \
38 		if (ctx->reactions != ctx->expected)                  \
39 			ctx->expected = ctx->reactions;               \
40 	} while (0)
41 
42 #define RV_KUNIT_EXPECT_REACTION_HERE(test, ctx)                             \
43 	for (int __done = ({ RV_KUNIT_EXPECT_NO_REACTION(test, ctx); 0; });  \
44 	     !__done;                                                        \
45 	     __done = ({ RV_KUNIT_EXPECT_REACTION(test, ctx); 1; }))
46 
47 struct rv_kunit_mon {
48 	struct rv_monitor *rv_this;
49 	int (*monitor_init)(void);
50 	void (*monitor_destroy)(void);
51 	bool is_per_task;
52 	int *task_slot;
53 	void (*task_reset)(struct task_struct *task);
54 };
55 
56 void prepare_test(struct kunit *test, const struct rv_kunit_mon *mon);
57 void teardown_test(void *arg);
58 struct task_struct *rv_kunit_alloc_mock_task(struct kunit *test);
59 
60 #endif /* CONFIG_RV_MONITORS_KUNIT_TEST */
61 #endif /* _RV_KUNIT_H */
62