Thread (9 messages) flat view 9 messages, 3 authors, 4d ago
COOLING4d

[PATCH 4/5] rtla/tests: Add unit test for cpu_list_iterate()

From: Tomas Glozar <tglozar@redhat.com>
Date: 2026-08-14 13:55:53
Also in: lkml
Subsystem: real-time linux analysis (rtla) tools, the rest · Maintainers: Steven Rostedt, Tomas Glozar, Linus Torvalds

cpu_list_iterate() was split out of parse_cpu_set() to hold shared code
between it and the newly added function get_possible_cpus().

As its semantics are more complex than parse_cpu_set() - it calls a
callback on each element of the list, with possible abort on failure,
while parse_cpu_set() cares only about the set defined by the list - it
deserves its own test.

Test the callback being called correctly as well as the return value and
early break on different combinations of comma-separated numbers and
ranges.

Signed-off-by: Tomas Glozar <tglozar@redhat.com>
---
 tools/tracing/rtla/tests/unit/utils.c | 62 +++++++++++++++++++++++++++
 1 file changed, 62 insertions(+)
diff --git a/tools/tracing/rtla/tests/unit/utils.c b/tools/tracing/rtla/tests/unit/utils.c
index ce53cab494575..0cb5e217c56eb 100644
--- a/tools/tracing/rtla/tests/unit/utils.c
+++ b/tools/tracing/rtla/tests/unit/utils.c
@@ -34,6 +34,67 @@ START_TEST(test_strtoi)
 }
 END_TEST
 
+struct cpu_list_iterate_cb_data {
+	int index;
+	int *values;
+};
+
+static int cpu_list_iterate_callback(int cpu, void *data)
+{
+	struct cpu_list_iterate_cb_data *cb_data = data;
+
+	ck_assert_int_eq(cpu, cb_data->values[cb_data->index++]);
+
+	return 0;
+}
+
+static int cpu_list_iterate_callback_error(int cpu, void *data)
+{
+	struct cpu_list_iterate_cb_data *cb_data = data;
+
+	if (cpu > 10)
+		return -42;
+
+	ck_assert_int_eq(cpu, cb_data->values[cb_data->index++]);
+
+	return 0;
+}
+
+START_TEST(test_cpu_list_iterate)
+{
+	struct cpu_list_iterate_cb_data cb_data;
+	int test_data_1[] = {1, 2, 3, 4};
+	int test_data_2[] = {1, 2, 10, 11, 12};
+
+	cb_data.index = 0;
+
+	cb_data.values = test_data_1;
+	ck_assert_int_eq(cpu_list_iterate("1,2,3,4", cpu_list_iterate_callback, &cb_data), 4);
+	ck_assert_int_eq(cb_data.index, 4);
+	cb_data.index = 0;
+	ck_assert_int_eq(cpu_list_iterate("1-4", cpu_list_iterate_callback, &cb_data), 4);
+	ck_assert_int_eq(cb_data.index, 4);
+	cb_data.index = 0;
+	ck_assert_int_eq(cpu_list_iterate("1,2-3,4", cpu_list_iterate_callback, &cb_data), 4);
+	ck_assert_int_eq(cb_data.index, 4);
+	cb_data.index = 0;
+	ck_assert_int_eq(cpu_list_iterate("1-3,4", cpu_list_iterate_callback, &cb_data), 4);
+	ck_assert_int_eq(cb_data.index, 4);
+	cb_data.index = 0;
+	ck_assert_int_eq(cpu_list_iterate("1,2-4", cpu_list_iterate_callback, &cb_data), 4);
+	ck_assert_int_eq(cb_data.index, 4);
+
+	cb_data.index = 0;
+	ck_assert_int_eq(cpu_list_iterate("1,2-4", cpu_list_iterate_callback_error, &cb_data), 4);
+	ck_assert_int_eq(cb_data.index, 4);
+	cb_data.index = 0;
+	cb_data.values = test_data_2;
+	ck_assert_int_eq(cpu_list_iterate("1,2,10-12", cpu_list_iterate_callback_error, &cb_data),
+			 -42);
+	ck_assert_int_eq(cb_data.index, 3);
+}
+END_TEST
+
 START_TEST(test_parse_cpu_set)
 {
 	cpu_set_t set;
@@ -98,6 +159,7 @@ Suite *utils_suite(void)
 	TCase *tc = tcase_create("core");
 
 	tcase_add_test(tc, test_strtoi);
+	tcase_add_test(tc, test_cpu_list_iterate);
 	tcase_add_test(tc, test_parse_cpu_set);
 	tcase_add_test(tc, test_parse_prio);
 
-- 
2.55.0
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help