Line data Source code
1 : /*
2 : * Copyright (c) 2025 Project CHIP Authors
3 : *
4 : * Licensed under the Apache License, Version 2.0 (the "License");
5 : * you may not use this file except in compliance with the License.
6 : * You may obtain a copy of the License at
7 : *
8 : * http://www.apache.org/licenses/LICENSE-2.0
9 : *
10 : * Unless required by applicable law or agreed to in writing, software
11 : * distributed under the License is distributed on an "AS IS" BASIS,
12 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 : * See the License for the specific language governing permissions and
14 : * limitations under the License.
15 : */
16 :
17 : #include "AttributeTesting.h"
18 :
19 : #include <app/data-model-provider/MetadataTypes.h>
20 : #include <map>
21 : #include <set>
22 :
23 : namespace chip {
24 : namespace Testing {
25 :
26 77 : bool EqualAttributeSets(Span<const app::DataModel::AttributeEntry> a, Span<const app::DataModel::AttributeEntry> b)
27 : {
28 :
29 77 : std::map<AttributeId, const app::DataModel::AttributeEntry *> entriesA;
30 77 : std::map<AttributeId, const app::DataModel::AttributeEntry *> entriesB;
31 :
32 815 : for (const app::DataModel::AttributeEntry & entry : a)
33 : {
34 738 : if (!entriesA.emplace(entry.attributeId, &entry).second)
35 : {
36 0 : ChipLogError(Test, "Duplicate attribute ID in span A: 0x%08X", static_cast<unsigned int>(entry.attributeId));
37 0 : return false;
38 : }
39 : }
40 :
41 815 : for (const app::DataModel::AttributeEntry & entry : b)
42 : {
43 738 : if (!entriesB.emplace(entry.attributeId, &entry).second)
44 : {
45 0 : ChipLogError(Test, "Duplicate attribute ID in span B: 0x%08X", static_cast<unsigned int>(entry.attributeId));
46 0 : return false;
47 : }
48 : }
49 :
50 77 : if (entriesA.size() != entriesB.size())
51 : {
52 0 : ChipLogError(Test, "Sets of different sizes.");
53 :
54 0 : for (const auto it : entriesA)
55 : {
56 0 : if (entriesB.find(it.first) == entriesB.end())
57 : {
58 0 : ChipLogError(Test, "Attribute 0x%08X missing in B", static_cast<unsigned int>(it.first));
59 : }
60 : }
61 :
62 0 : for (const auto it : entriesB)
63 : {
64 0 : if (entriesA.find(it.first) == entriesA.end())
65 : {
66 0 : ChipLogError(Test, "Attribute 0x%08X missing in A", static_cast<unsigned int>(it.first));
67 : }
68 : }
69 :
70 0 : return false;
71 : }
72 :
73 815 : for (const auto it : entriesA)
74 : {
75 738 : const auto other = entriesB.find(it.first);
76 738 : if (other == entriesB.end())
77 : {
78 :
79 0 : ChipLogError(Test, "Missing entry: 0x%08X", static_cast<unsigned int>(it.first));
80 0 : return false;
81 : }
82 :
83 738 : if (*it.second != *other->second)
84 : {
85 :
86 0 : ChipLogError(Test, "Different content (different flags?): 0x%08X", static_cast<unsigned int>(it.first));
87 0 : return false;
88 : }
89 : }
90 : // set sizes are the same and all entriesA have a corresponding entriesB, so sets should match
91 77 : return true;
92 77 : }
93 :
94 42 : bool EqualAcceptedCommandSets(Span<const app::DataModel::AcceptedCommandEntry> a,
95 : Span<const app::DataModel::AcceptedCommandEntry> b)
96 : {
97 :
98 42 : std::map<CommandId, const app::DataModel::AcceptedCommandEntry *> entriesA;
99 42 : std::map<CommandId, const app::DataModel::AcceptedCommandEntry *> entriesB;
100 :
101 134 : for (const app::DataModel::AcceptedCommandEntry & entry : a)
102 : {
103 92 : if (!entriesA.emplace(entry.commandId, &entry).second)
104 : {
105 0 : ChipLogError(Test, "Duplicate command ID in span A: 0x%08X", static_cast<unsigned int>(entry.commandId));
106 0 : return false;
107 : }
108 : }
109 :
110 134 : for (const app::DataModel::AcceptedCommandEntry & entry : b)
111 : {
112 92 : if (!entriesB.emplace(entry.commandId, &entry).second)
113 : {
114 0 : ChipLogError(Test, "Duplicate command ID in span B: 0x%08X", static_cast<unsigned int>(entry.commandId));
115 0 : return false;
116 : }
117 : }
118 :
119 42 : if (entriesA.size() != entriesB.size())
120 : {
121 0 : ChipLogError(Test, "Sets of different sizes.");
122 :
123 0 : for (const auto it : entriesA)
124 : {
125 0 : if (entriesB.find(it.first) == entriesB.end())
126 : {
127 0 : ChipLogError(Test, "Command 0x%08X missing in B", static_cast<unsigned int>(it.first));
128 : }
129 : }
130 :
131 0 : for (const auto it : entriesB)
132 : {
133 0 : if (entriesA.find(it.first) == entriesA.end())
134 : {
135 0 : ChipLogError(Test, "Command 0x%08X missing in A", static_cast<unsigned int>(it.first));
136 : }
137 : }
138 :
139 0 : return false;
140 : }
141 :
142 134 : for (const auto it : entriesA)
143 : {
144 92 : const auto other = entriesB.find(it.first);
145 92 : if (other == entriesB.end())
146 : {
147 :
148 0 : ChipLogError(Test, "Missing entry: 0x%08X", static_cast<unsigned int>(it.first));
149 0 : return false;
150 : }
151 :
152 92 : if (*it.second != *other->second)
153 : {
154 :
155 0 : ChipLogError(Test, "Different content (different flags?): 0x%08X", static_cast<unsigned int>(it.first));
156 0 : return false;
157 : }
158 : }
159 : // set sizes are the same and all entriesA have a corresponding entriesB, so sets should match
160 42 : return true;
161 42 : }
162 :
163 7 : bool EqualGeneratedCommandSets(Span<const CommandId> a, Span<const CommandId> b)
164 : {
165 7 : std::set<CommandId> entriesA;
166 7 : std::set<CommandId> entriesB;
167 :
168 12 : for (CommandId entry : a)
169 : {
170 5 : if (!entriesA.insert(entry).second)
171 : {
172 0 : ChipLogError(Test, "Duplicate command ID in span A: 0x%08X", static_cast<unsigned int>(entry));
173 0 : return false;
174 : }
175 : }
176 :
177 12 : for (CommandId entry : b)
178 : {
179 5 : if (!entriesB.insert(entry).second)
180 : {
181 0 : ChipLogError(Test, "Duplicate command ID in span B: 0x%08X", static_cast<unsigned int>(entry));
182 0 : return false;
183 : }
184 : }
185 :
186 7 : if (entriesA.size() != entriesB.size())
187 : {
188 0 : ChipLogError(Test, "Sets of different sizes.");
189 :
190 0 : for (const auto id : entriesA)
191 : {
192 0 : if (entriesB.find(id) == entriesB.end())
193 : {
194 0 : ChipLogError(Test, "Command 0x%08X missing in B", static_cast<unsigned int>(id));
195 : }
196 : }
197 :
198 0 : for (const auto id : entriesB)
199 : {
200 0 : if (entriesA.find(id) == entriesA.end())
201 : {
202 0 : ChipLogError(Test, "Command 0x%08X missing in A", static_cast<unsigned int>(id));
203 : }
204 : }
205 :
206 0 : return false;
207 : }
208 :
209 12 : for (const auto id : entriesA)
210 : {
211 5 : if (entriesB.find(id) == entriesB.end())
212 : {
213 0 : ChipLogError(Test, "Missing entry: 0x%08X", static_cast<unsigned int>(id));
214 0 : return false;
215 : }
216 : }
217 : // set sizes are the same and all entriesA have a corresponding entriesB, so sets should match
218 7 : return true;
219 7 : }
220 :
221 : } // namespace Testing
222 : } // namespace chip
|