1 // SPDX-License-Identifier: GPL-2.0-or-later
3 // self-id-sequence-helper-test.c - An application of Kunit to test helpers of self ID sequence.
5 // Copyright (c) 2024 Takashi Sakamoto
7 #include <kunit/test.h>
9 #include "phy-packet-definitions.h"
11 static void test_self_id_sequence_enumerator_valid(struct kunit
*test
)
13 static const u32 valid_sequences
[] = {
15 0x00000001, 0x00800000,
16 0x00000001, 0x00800001, 0x00900000,
19 struct self_id_sequence_enumerator enumerator
;
21 unsigned int quadlet_count
;
23 enumerator
.cursor
= valid_sequences
;
24 enumerator
.quadlet_count
= ARRAY_SIZE(valid_sequences
);
26 entry
= self_id_sequence_enumerator_next(&enumerator
, &quadlet_count
);
27 KUNIT_EXPECT_PTR_EQ(test
, entry
, &valid_sequences
[0]);
28 KUNIT_EXPECT_EQ(test
, quadlet_count
, 1);
29 KUNIT_EXPECT_EQ(test
, enumerator
.quadlet_count
, 6);
31 entry
= self_id_sequence_enumerator_next(&enumerator
, &quadlet_count
);
32 KUNIT_EXPECT_PTR_EQ(test
, entry
, &valid_sequences
[1]);
33 KUNIT_EXPECT_EQ(test
, quadlet_count
, 2);
34 KUNIT_EXPECT_EQ(test
, enumerator
.quadlet_count
, 4);
36 entry
= self_id_sequence_enumerator_next(&enumerator
, &quadlet_count
);
37 KUNIT_EXPECT_PTR_EQ(test
, entry
, &valid_sequences
[3]);
38 KUNIT_EXPECT_EQ(test
, quadlet_count
, 3);
39 KUNIT_EXPECT_EQ(test
, enumerator
.quadlet_count
, 1);
41 entry
= self_id_sequence_enumerator_next(&enumerator
, &quadlet_count
);
42 KUNIT_EXPECT_PTR_EQ(test
, entry
, &valid_sequences
[6]);
43 KUNIT_EXPECT_EQ(test
, quadlet_count
, 1);
44 KUNIT_EXPECT_EQ(test
, enumerator
.quadlet_count
, 0);
46 entry
= self_id_sequence_enumerator_next(&enumerator
, &quadlet_count
);
47 KUNIT_EXPECT_EQ(test
, PTR_ERR(entry
), -ENODATA
);
50 static void test_self_id_sequence_enumerator_invalid(struct kunit
*test
)
52 static const u32 invalid_sequences
[] = {
55 struct self_id_sequence_enumerator enumerator
;
59 enumerator
.cursor
= invalid_sequences
;
60 enumerator
.quadlet_count
= ARRAY_SIZE(invalid_sequences
);
62 entry
= self_id_sequence_enumerator_next(&enumerator
, &count
);
63 KUNIT_EXPECT_EQ(test
, PTR_ERR(entry
), -EPROTO
);
66 static void test_self_id_sequence_get_port_status(struct kunit
*test
)
68 static const u32 expected
[] = {
80 enum phy_packet_self_id_port_status port_status
[28];
81 unsigned int port_capacity
;
82 unsigned int port_index
;
84 KUNIT_ASSERT_EQ(test
, ARRAY_SIZE(expected
), ARRAY_SIZE(quadlets
));
86 // With an extra port.
87 port_capacity
= self_id_sequence_get_port_capacity(ARRAY_SIZE(expected
)) + 1;
88 KUNIT_ASSERT_EQ(test
, port_capacity
, ARRAY_SIZE(port_status
));
90 for (port_index
= 0; port_index
< port_capacity
; ++port_index
) {
91 port_status
[port_index
] =
92 self_id_sequence_get_port_status(expected
, ARRAY_SIZE(expected
), port_index
);
93 self_id_sequence_set_port_status(quadlets
, ARRAY_SIZE(quadlets
), port_index
,
94 port_status
[port_index
]);
98 KUNIT_EXPECT_EQ(test
, PHY_PACKET_SELF_ID_PORT_STATUS_CHILD
, port_status
[0]);
99 KUNIT_EXPECT_EQ(test
, PHY_PACKET_SELF_ID_PORT_STATUS_PARENT
, port_status
[1]);
100 KUNIT_EXPECT_EQ(test
, PHY_PACKET_SELF_ID_PORT_STATUS_NCONN
, port_status
[2]);
103 KUNIT_EXPECT_EQ(test
, PHY_PACKET_SELF_ID_PORT_STATUS_CHILD
, port_status
[3]);
104 KUNIT_EXPECT_EQ(test
, PHY_PACKET_SELF_ID_PORT_STATUS_PARENT
, port_status
[4]);
105 KUNIT_EXPECT_EQ(test
, PHY_PACKET_SELF_ID_PORT_STATUS_NCONN
, port_status
[5]);
106 KUNIT_EXPECT_EQ(test
, PHY_PACKET_SELF_ID_PORT_STATUS_CHILD
, port_status
[6]);
107 KUNIT_EXPECT_EQ(test
, PHY_PACKET_SELF_ID_PORT_STATUS_PARENT
, port_status
[7]);
108 KUNIT_EXPECT_EQ(test
, PHY_PACKET_SELF_ID_PORT_STATUS_NCONN
, port_status
[8]);
109 KUNIT_EXPECT_EQ(test
, PHY_PACKET_SELF_ID_PORT_STATUS_CHILD
, port_status
[9]);
110 KUNIT_EXPECT_EQ(test
, PHY_PACKET_SELF_ID_PORT_STATUS_PARENT
, port_status
[10]);
113 KUNIT_EXPECT_EQ(test
, PHY_PACKET_SELF_ID_PORT_STATUS_NCONN
, port_status
[11]);
114 KUNIT_EXPECT_EQ(test
, PHY_PACKET_SELF_ID_PORT_STATUS_CHILD
, port_status
[12]);
115 KUNIT_EXPECT_EQ(test
, PHY_PACKET_SELF_ID_PORT_STATUS_PARENT
, port_status
[13]);
116 KUNIT_EXPECT_EQ(test
, PHY_PACKET_SELF_ID_PORT_STATUS_NCONN
, port_status
[14]);
117 KUNIT_EXPECT_EQ(test
, PHY_PACKET_SELF_ID_PORT_STATUS_CHILD
, port_status
[15]);
118 KUNIT_EXPECT_EQ(test
, PHY_PACKET_SELF_ID_PORT_STATUS_PARENT
, port_status
[16]);
119 KUNIT_EXPECT_EQ(test
, PHY_PACKET_SELF_ID_PORT_STATUS_NCONN
, port_status
[17]);
120 KUNIT_EXPECT_EQ(test
, PHY_PACKET_SELF_ID_PORT_STATUS_CHILD
, port_status
[18]);
123 KUNIT_EXPECT_EQ(test
, PHY_PACKET_SELF_ID_PORT_STATUS_PARENT
, port_status
[19]);
124 KUNIT_EXPECT_EQ(test
, PHY_PACKET_SELF_ID_PORT_STATUS_NCONN
, port_status
[20]);
125 KUNIT_EXPECT_EQ(test
, PHY_PACKET_SELF_ID_PORT_STATUS_CHILD
, port_status
[21]);
126 KUNIT_EXPECT_EQ(test
, PHY_PACKET_SELF_ID_PORT_STATUS_PARENT
, port_status
[22]);
127 KUNIT_EXPECT_EQ(test
, PHY_PACKET_SELF_ID_PORT_STATUS_NCONN
, port_status
[23]);
128 KUNIT_EXPECT_EQ(test
, PHY_PACKET_SELF_ID_PORT_STATUS_CHILD
, port_status
[24]);
129 KUNIT_EXPECT_EQ(test
, PHY_PACKET_SELF_ID_PORT_STATUS_PARENT
, port_status
[25]);
130 KUNIT_EXPECT_EQ(test
, PHY_PACKET_SELF_ID_PORT_STATUS_NCONN
, port_status
[26]);
133 KUNIT_EXPECT_EQ(test
, PHY_PACKET_SELF_ID_PORT_STATUS_NONE
, port_status
[27]);
135 KUNIT_EXPECT_MEMEQ(test
, quadlets
, expected
, sizeof(expected
));
138 static struct kunit_case self_id_sequence_helper_test_cases
[] = {
139 KUNIT_CASE(test_self_id_sequence_enumerator_valid
),
140 KUNIT_CASE(test_self_id_sequence_enumerator_invalid
),
141 KUNIT_CASE(test_self_id_sequence_get_port_status
),
145 static struct kunit_suite self_id_sequence_helper_test_suite
= {
146 .name
= "self-id-sequence-helper",
147 .test_cases
= self_id_sequence_helper_test_cases
,
149 kunit_test_suite(self_id_sequence_helper_test_suite
);
151 MODULE_DESCRIPTION("Unit test suite for helpers of self ID sequence");
152 MODULE_LICENSE("GPL");