0001
0002
0003
0004
0005
0006 #include <kunit/test.h>
0007 #include <linux/ioport.h>
0008 #include <linux/kernel.h>
0009 #include <linux/string.h>
0010
0011 #define R0_START 0x0000
0012 #define R0_END 0xffff
0013 #define R1_START 0x1234
0014 #define R1_END 0x2345
0015 #define R2_START 0x4567
0016 #define R2_END 0x5678
0017 #define R3_START 0x6789
0018 #define R3_END 0x789a
0019 #define R4_START 0x2000
0020 #define R4_END 0x7000
0021
0022 static struct resource r0 = { .start = R0_START, .end = R0_END };
0023 static struct resource r1 = { .start = R1_START, .end = R1_END };
0024 static struct resource r2 = { .start = R2_START, .end = R2_END };
0025 static struct resource r3 = { .start = R3_START, .end = R3_END };
0026 static struct resource r4 = { .start = R4_START, .end = R4_END };
0027
0028 struct result {
0029 struct resource *r1;
0030 struct resource *r2;
0031 struct resource r;
0032 bool ret;
0033 };
0034
0035 static struct result results_for_union[] = {
0036 {
0037 .r1 = &r1, .r2 = &r0, .r.start = R0_START, .r.end = R0_END, .ret = true,
0038 }, {
0039 .r1 = &r2, .r2 = &r0, .r.start = R0_START, .r.end = R0_END, .ret = true,
0040 }, {
0041 .r1 = &r3, .r2 = &r0, .r.start = R0_START, .r.end = R0_END, .ret = true,
0042 }, {
0043 .r1 = &r4, .r2 = &r0, .r.start = R0_START, .r.end = R0_END, .ret = true,
0044 }, {
0045 .r1 = &r2, .r2 = &r1, .ret = false,
0046 }, {
0047 .r1 = &r3, .r2 = &r1, .ret = false,
0048 }, {
0049 .r1 = &r4, .r2 = &r1, .r.start = R1_START, .r.end = R4_END, .ret = true,
0050 }, {
0051 .r1 = &r2, .r2 = &r3, .ret = false,
0052 }, {
0053 .r1 = &r2, .r2 = &r4, .r.start = R4_START, .r.end = R4_END, .ret = true,
0054 }, {
0055 .r1 = &r3, .r2 = &r4, .r.start = R4_START, .r.end = R3_END, .ret = true,
0056 },
0057 };
0058
0059 static struct result results_for_intersection[] = {
0060 {
0061 .r1 = &r1, .r2 = &r0, .r.start = R1_START, .r.end = R1_END, .ret = true,
0062 }, {
0063 .r1 = &r2, .r2 = &r0, .r.start = R2_START, .r.end = R2_END, .ret = true,
0064 }, {
0065 .r1 = &r3, .r2 = &r0, .r.start = R3_START, .r.end = R3_END, .ret = true,
0066 }, {
0067 .r1 = &r4, .r2 = &r0, .r.start = R4_START, .r.end = R4_END, .ret = true,
0068 }, {
0069 .r1 = &r2, .r2 = &r1, .ret = false,
0070 }, {
0071 .r1 = &r3, .r2 = &r1, .ret = false,
0072 }, {
0073 .r1 = &r4, .r2 = &r1, .r.start = R4_START, .r.end = R1_END, .ret = true,
0074 }, {
0075 .r1 = &r2, .r2 = &r3, .ret = false,
0076 }, {
0077 .r1 = &r2, .r2 = &r4, .r.start = R2_START, .r.end = R2_END, .ret = true,
0078 }, {
0079 .r1 = &r3, .r2 = &r4, .r.start = R3_START, .r.end = R4_END, .ret = true,
0080 },
0081 };
0082
0083 static void resource_do_test(struct kunit *test, bool ret, struct resource *r,
0084 bool exp_ret, struct resource *exp_r,
0085 struct resource *r1, struct resource *r2)
0086 {
0087 KUNIT_EXPECT_EQ_MSG(test, ret, exp_ret, "Resources %pR %pR", r1, r2);
0088 KUNIT_EXPECT_EQ_MSG(test, r->start, exp_r->start, "Start elements are not equal");
0089 KUNIT_EXPECT_EQ_MSG(test, r->end, exp_r->end, "End elements are not equal");
0090 }
0091
0092 static void resource_do_union_test(struct kunit *test, struct result *r)
0093 {
0094 struct resource result;
0095 bool ret;
0096
0097 memset(&result, 0, sizeof(result));
0098 ret = resource_union(r->r1, r->r2, &result);
0099 resource_do_test(test, ret, &result, r->ret, &r->r, r->r1, r->r2);
0100
0101 memset(&result, 0, sizeof(result));
0102 ret = resource_union(r->r2, r->r1, &result);
0103 resource_do_test(test, ret, &result, r->ret, &r->r, r->r2, r->r1);
0104 }
0105
0106 static void resource_test_union(struct kunit *test)
0107 {
0108 struct result *r = results_for_union;
0109 unsigned int i = 0;
0110
0111 do {
0112 resource_do_union_test(test, &r[i]);
0113 } while (++i < ARRAY_SIZE(results_for_union));
0114 }
0115
0116 static void resource_do_intersection_test(struct kunit *test, struct result *r)
0117 {
0118 struct resource result;
0119 bool ret;
0120
0121 memset(&result, 0, sizeof(result));
0122 ret = resource_intersection(r->r1, r->r2, &result);
0123 resource_do_test(test, ret, &result, r->ret, &r->r, r->r1, r->r2);
0124
0125 memset(&result, 0, sizeof(result));
0126 ret = resource_intersection(r->r2, r->r1, &result);
0127 resource_do_test(test, ret, &result, r->ret, &r->r, r->r2, r->r1);
0128 }
0129
0130 static void resource_test_intersection(struct kunit *test)
0131 {
0132 struct result *r = results_for_intersection;
0133 unsigned int i = 0;
0134
0135 do {
0136 resource_do_intersection_test(test, &r[i]);
0137 } while (++i < ARRAY_SIZE(results_for_intersection));
0138 }
0139
0140 static struct kunit_case resource_test_cases[] = {
0141 KUNIT_CASE(resource_test_union),
0142 KUNIT_CASE(resource_test_intersection),
0143 {}
0144 };
0145
0146 static struct kunit_suite resource_test_suite = {
0147 .name = "resource",
0148 .test_cases = resource_test_cases,
0149 };
0150 kunit_test_suite(resource_test_suite);
0151
0152 MODULE_LICENSE("GPL");