Linux Trace Kernel
 help / color / mirror / Atom feed
From: Tomas Glozar <tglozar@redhat.com>
To: Steven Rostedt <rostedt@goodmis.org>, Tomas Glozar <tglozar@redhat.com>
Cc: John Kacur <jkacur@redhat.com>,
	Luis Goncalves <lgoncalv@redhat.com>,
	Crystal Wood <crwood@redhat.com>,
	Costa Shulyupin <costa.shul@redhat.com>,
	Wander Lairson Costa <wander@redhat.com>,
	LKML <linux-kernel@vger.kernel.org>,
	linux-trace-kernel <linux-trace-kernel@vger.kernel.org>
Subject: [PATCH 4/5] rtla/tests: Add unit test for cpu_list_iterate()
Date: Fri, 14 Aug 2026 15:55:10 +0200	[thread overview]
Message-ID: <20260814135511.2207190-5-tglozar@redhat.com> (raw)
In-Reply-To: <20260814135511.2207190-1-tglozar@redhat.com>

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


  parent reply	other threads:[~2026-08-14 13:55 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-14 13:55 [PATCH 0/5] rtla: Implement more robust nr_cpus handling Tomas Glozar
2026-08-14 13:55 ` [PATCH 1/5] rtla: Replace get_nprocs_conf() with sysfs possible cpus Tomas Glozar
2026-08-14 13:55 ` [PATCH 2/5] rtla: Discard trace entries with cpu >= nr_cpus Tomas Glozar
2026-08-14 14:02   ` sashiko-bot
2026-08-14 13:55 ` [PATCH 3/5] rtla: Abort on nr_cpus mismatch with tracer Tomas Glozar
2026-08-14 13:55 ` Tomas Glozar [this message]
2026-08-14 13:55 ` [PATCH 5/5] rtla/tests: Add unit test for get_max_cpu_from_list() Tomas Glozar
2026-08-14 14:30 ` [PATCH 0/5] rtla: Implement more robust nr_cpus handling Steven Rostedt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260814135511.2207190-5-tglozar@redhat.com \
    --to=tglozar@redhat.com \
    --cc=costa.shul@redhat.com \
    --cc=crwood@redhat.com \
    --cc=jkacur@redhat.com \
    --cc=lgoncalv@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=rostedt@goodmis.org \
    --cc=wander@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox