1 // SPDX-License-Identifier: GPL-2.0+
3 * Test cases for API provided by resource.c and ioport.h
6 #include <kunit/test.h>
7 #include <linux/ioport.h>
8 #include <linux/kernel.h>
9 #include <linux/string.h>
11 #define R0_START 0x0000
13 #define R1_START 0x1234
15 #define R2_START 0x4567
17 #define R3_START 0x6789
19 #define R4_START 0x2000
22 static struct resource r0 = { .start = R0_START, .end = R0_END };
23 static struct resource r1 = { .start = R1_START, .end = R1_END };
24 static struct resource r2 = { .start = R2_START, .end = R2_END };
25 static struct resource r3 = { .start = R3_START, .end = R3_END };
26 static struct resource r4 = { .start = R4_START, .end = R4_END };
35 static struct result results_for_union[] = {
37 .r1 = &r1, .r2 = &r0, .r.start = R0_START, .r.end = R0_END, .ret = true,
39 .r1 = &r2, .r2 = &r0, .r.start = R0_START, .r.end = R0_END, .ret = true,
41 .r1 = &r3, .r2 = &r0, .r.start = R0_START, .r.end = R0_END, .ret = true,
43 .r1 = &r4, .r2 = &r0, .r.start = R0_START, .r.end = R0_END, .ret = true,
45 .r1 = &r2, .r2 = &r1, .ret = false,
47 .r1 = &r3, .r2 = &r1, .ret = false,
49 .r1 = &r4, .r2 = &r1, .r.start = R1_START, .r.end = R4_END, .ret = true,
51 .r1 = &r2, .r2 = &r3, .ret = false,
53 .r1 = &r2, .r2 = &r4, .r.start = R4_START, .r.end = R4_END, .ret = true,
55 .r1 = &r3, .r2 = &r4, .r.start = R4_START, .r.end = R3_END, .ret = true,
59 static struct result results_for_intersection[] = {
61 .r1 = &r1, .r2 = &r0, .r.start = R1_START, .r.end = R1_END, .ret = true,
63 .r1 = &r2, .r2 = &r0, .r.start = R2_START, .r.end = R2_END, .ret = true,
65 .r1 = &r3, .r2 = &r0, .r.start = R3_START, .r.end = R3_END, .ret = true,
67 .r1 = &r4, .r2 = &r0, .r.start = R4_START, .r.end = R4_END, .ret = true,
69 .r1 = &r2, .r2 = &r1, .ret = false,
71 .r1 = &r3, .r2 = &r1, .ret = false,
73 .r1 = &r4, .r2 = &r1, .r.start = R4_START, .r.end = R1_END, .ret = true,
75 .r1 = &r2, .r2 = &r3, .ret = false,
77 .r1 = &r2, .r2 = &r4, .r.start = R2_START, .r.end = R2_END, .ret = true,
79 .r1 = &r3, .r2 = &r4, .r.start = R3_START, .r.end = R4_END, .ret = true,
83 static void resource_do_test(struct kunit *test, bool ret, struct resource *r,
84 bool exp_ret, struct resource *exp_r,
85 struct resource *r1, struct resource *r2)
87 KUNIT_EXPECT_EQ_MSG(test, ret, exp_ret, "Resources %pR %pR", r1, r2);
88 KUNIT_EXPECT_EQ_MSG(test, r->start, exp_r->start, "Start elements are not equal");
89 KUNIT_EXPECT_EQ_MSG(test, r->end, exp_r->end, "End elements are not equal");
92 static void resource_do_union_test(struct kunit *test, struct result *r)
94 struct resource result;
97 memset(&result, 0, sizeof(result));
98 ret = resource_union(r->r1, r->r2, &result);
99 resource_do_test(test, ret, &result, r->ret, &r->r, r->r1, r->r2);
101 memset(&result, 0, sizeof(result));
102 ret = resource_union(r->r2, r->r1, &result);
103 resource_do_test(test, ret, &result, r->ret, &r->r, r->r2, r->r1);
106 static void resource_test_union(struct kunit *test)
108 struct result *r = results_for_union;
112 resource_do_union_test(test, &r[i]);
113 } while (++i < ARRAY_SIZE(results_for_union));
116 static void resource_do_intersection_test(struct kunit *test, struct result *r)
118 struct resource result;
121 memset(&result, 0, sizeof(result));
122 ret = resource_intersection(r->r1, r->r2, &result);
123 resource_do_test(test, ret, &result, r->ret, &r->r, r->r1, r->r2);
125 memset(&result, 0, sizeof(result));
126 ret = resource_intersection(r->r2, r->r1, &result);
127 resource_do_test(test, ret, &result, r->ret, &r->r, r->r2, r->r1);
130 static void resource_test_intersection(struct kunit *test)
132 struct result *r = results_for_intersection;
136 resource_do_intersection_test(test, &r[i]);
137 } while (++i < ARRAY_SIZE(results_for_intersection));
140 static struct kunit_case resource_test_cases[] = {
141 KUNIT_CASE(resource_test_union),
142 KUNIT_CASE(resource_test_intersection),
146 static struct kunit_suite resource_test_suite = {
148 .test_cases = resource_test_cases,
150 kunit_test_suite(resource_test_suite);
152 MODULE_LICENSE("GPL");