Files
linux/tools/tracing/rtla/tests/unit/utils.c
Tomas Glozar e5d8f22758 rtla/tests: Add unit tests for actions module
Add unit tests covering all functions in the actions module, including
both valid and invalid inputs and all action types, except for
actions_perform(), where only shell and continue actions are tested.

To support testing multiple modules, the unit test build was modified so
that it links the entire rtla-in.o file. For this to work, the main()
function in rtla.c was declared weak, so that the unit test main is able
to override it.

Other included minor changes to unit tests are:

- Make unit test output verbose to show which tests are being run, now
  that we have more than 3 tests.
- Add unit_tests file to .gitignore.
- Split unit test sources to one file per test suite, and keep only
  main() function in unit_tests.c.
- Fix Makefile dependencies so that "make unit-tests" will rebuild the
  binary with the changes in the commit.

Also with the linking the entire rtla-in.o file, it now has rtla's
nr_cpus symbol, so the declaration in utils unit tests is made extern.

Assisted-by: Composer:composer-2-fast
Link: https://lore.kernel.org/r/20260424140244.958495-1-tglozar@redhat.com
Signed-off-by: Tomas Glozar <tglozar@redhat.com>
2026-05-18 11:00:52 +02:00

107 lines
2.6 KiB
C

// SPDX-License-Identifier: GPL-2.0
#define _GNU_SOURCE
#include <check.h>
#include <stdio.h>
#include <stdlib.h>
#include <sched.h>
#include <limits.h>
#include <unistd.h>
#include <sys/sysinfo.h>
#include "../../src/utils.h"
extern int nr_cpus;
START_TEST(test_strtoi)
{
int result;
char buf[64];
ck_assert_int_eq(strtoi("123", &result), 0);
ck_assert_int_eq(result, 123);
ck_assert_int_eq(strtoi(" -456", &result), 0);
ck_assert_int_eq(result, -456);
snprintf(buf, sizeof(buf), "%d", INT_MAX);
ck_assert_int_eq(strtoi(buf, &result), 0);
snprintf(buf, sizeof(buf), "%ld", (long)INT_MAX + 1);
ck_assert_int_eq(strtoi(buf, &result), -1);
ck_assert_int_eq(strtoi("", &result), -1);
ck_assert_int_eq(strtoi("123abc", &result), -1);
ck_assert_int_eq(strtoi("123 ", &result), -1);
}
END_TEST
START_TEST(test_parse_cpu_set)
{
cpu_set_t set;
nr_cpus = 8;
ck_assert_int_eq(parse_cpu_set("0", &set), 0);
ck_assert(CPU_ISSET(0, &set));
ck_assert(!CPU_ISSET(1, &set));
ck_assert_int_eq(parse_cpu_set("0,2", &set), 0);
ck_assert(CPU_ISSET(0, &set));
ck_assert(CPU_ISSET(2, &set));
ck_assert_int_eq(parse_cpu_set("0-3", &set), 0);
ck_assert(CPU_ISSET(0, &set));
ck_assert(CPU_ISSET(1, &set));
ck_assert(CPU_ISSET(2, &set));
ck_assert(CPU_ISSET(3, &set));
ck_assert_int_eq(parse_cpu_set("1-3,5", &set), 0);
ck_assert(!CPU_ISSET(0, &set));
ck_assert(CPU_ISSET(1, &set));
ck_assert(CPU_ISSET(2, &set));
ck_assert(CPU_ISSET(3, &set));
ck_assert(!CPU_ISSET(4, &set));
ck_assert(CPU_ISSET(5, &set));
ck_assert_int_eq(parse_cpu_set("-1", &set), 1);
ck_assert_int_eq(parse_cpu_set("abc", &set), 1);
ck_assert_int_eq(parse_cpu_set("9999", &set), 1);
}
END_TEST
START_TEST(test_parse_prio)
{
struct sched_attr attr;
ck_assert_int_eq(parse_prio("f:50", &attr), 0);
ck_assert_uint_eq(attr.sched_policy, SCHED_FIFO);
ck_assert_uint_eq(attr.sched_priority, 50U);
ck_assert_int_eq(parse_prio("r:30", &attr), 0);
ck_assert_uint_eq(attr.sched_policy, SCHED_RR);
ck_assert_int_eq(parse_prio("o:0", &attr), 0);
ck_assert_uint_eq(attr.sched_policy, SCHED_OTHER);
ck_assert_int_eq(attr.sched_nice, 0);
ck_assert_int_eq(parse_prio("d:10ms:100ms", &attr), 0);
ck_assert_uint_eq(attr.sched_policy, 6U);
ck_assert_int_eq(parse_prio("f:999", &attr), -1);
ck_assert_int_eq(parse_prio("o:-20", &attr), -1);
ck_assert_int_eq(parse_prio("d:100ms:10ms", &attr), -1);
ck_assert_int_eq(parse_prio("x:50", &attr), -1);
}
END_TEST
Suite *utils_suite(void)
{
Suite *s = suite_create("utils");
TCase *tc = tcase_create("core");
tcase_add_test(tc, test_strtoi);
tcase_add_test(tc, test_parse_cpu_set);
tcase_add_test(tc, test_parse_prio);
suite_add_tcase(s, tc);
return s;
}