Skip to content

Commit d3c7cfc

Browse files
andy-shevbrgl
authored andcommitted
lib/cmdline_kunit: add a new test case for get_options()
Add a test case for get_options() which is provided by cmdline.c. Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
1 parent 1d10243 commit d3c7cfc

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

lib/cmdline_kunit.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,26 @@ static const int cmdline_test_values[] = {
1818
1, 3, 2, 1, 1, 1, 3, 1,
1919
};
2020

21+
static_assert(ARRAY_SIZE(cmdline_test_strings) == ARRAY_SIZE(cmdline_test_values));
22+
23+
static const char *cmdline_test_range_strings[] = {
24+
"-7" , "--7" , "-1-2" , "7--9",
25+
"7-" , "-7--9", "7-9," , "9-7" ,
26+
"5-a", "a-5" , "5-8" , ",8-5",
27+
"+,1", "-,4" , "-3,0-1,6", "4,-" ,
28+
" +2", " -9" , "0-1,-3,6", "- 9" ,
29+
};
30+
31+
static const int cmdline_test_range_values[][16] = {
32+
{ 1, -7, }, { 0, -0, }, { 4, -1, 0, +1, 2, }, { 0, 7, },
33+
{ 0, +7, }, { 0, -7, }, { 3, +7, 8, +9, 0, }, { 0, 9, },
34+
{ 0, +5, }, { 0, -0, }, { 4, +5, 6, +7, 8, }, { 0, 0, },
35+
{ 0, +0, }, { 0, -0, }, { 4, -3, 0, +1, 6, }, { 1, 4, },
36+
{ 0, +0, }, { 0, -0, }, { 4, +0, 1, -3, 6, }, { 0, 0, },
37+
};
38+
39+
static_assert(ARRAY_SIZE(cmdline_test_range_strings) == ARRAY_SIZE(cmdline_test_range_values));
40+
2141
static void cmdline_do_one_test(struct kunit *test, const char *in, int rc, int offset)
2242
{
2343
const char *fmt = "Pattern: %s";
@@ -84,10 +104,37 @@ static void cmdline_test_tail_int(struct kunit *test)
84104
} while (++i < ARRAY_SIZE(cmdline_test_strings));
85105
}
86106

107+
static void cmdline_do_one_range_test(struct kunit *test, const char *in,
108+
unsigned int n, const int *e)
109+
{
110+
unsigned int i;
111+
int r[16];
112+
113+
memset(r, 0, sizeof(r));
114+
get_options(in, ARRAY_SIZE(r), r);
115+
KUNIT_EXPECT_EQ_MSG(test, r[0], e[0], "in test %u expected %d numbers, got %d",
116+
n, e[0], r[0]);
117+
for (i = 1; i < ARRAY_SIZE(r); i++)
118+
KUNIT_EXPECT_EQ_MSG(test, r[i], e[i], "in test %u at %u", n, i);
119+
}
120+
121+
static void cmdline_test_range(struct kunit *test)
122+
{
123+
unsigned int i = 0;
124+
125+
do {
126+
const char *str = cmdline_test_range_strings[i];
127+
const int *e = cmdline_test_range_values[i];
128+
129+
cmdline_do_one_range_test(test, str, i, e);
130+
} while (++i < ARRAY_SIZE(cmdline_test_range_strings));
131+
}
132+
87133
static struct kunit_case cmdline_test_cases[] = {
88134
KUNIT_CASE(cmdline_test_noint),
89135
KUNIT_CASE(cmdline_test_lead_int),
90136
KUNIT_CASE(cmdline_test_tail_int),
137+
KUNIT_CASE(cmdline_test_range),
91138
{}
92139
};
93140

0 commit comments

Comments
 (0)