1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program Test Executor
3 * ------------------------------------------
5 * Copyright 2014 The Android Open Source Project
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
22 *//*--------------------------------------------------------------------*/
24 #include "xeTestCase.hpp"
31 const char* getTestCaseTypeName (TestCaseType caseType)
35 case TESTCASETYPE_SELF_VALIDATE: return "SelfValidate";
36 case TESTCASETYPE_CAPABILITY: return "Capability";
37 case TESTCASETYPE_ACCURACY: return "Accuracy";
38 case TESTCASETYPE_PERFORMANCE: return "Performance";
45 static inline int getFirstComponentLength (const char* path)
48 while (path[compLen] != 0 && path[compLen] != '.')
53 static bool compareNameToPathComponent (const char* name, const char* path, int compLen)
55 for (int pos = 0; pos < compLen; pos++)
57 if (name[pos] != path[pos])
61 if (name[compLen] != 0)
67 static void splitPath (const char* path, std::vector<std::string>& components)
69 std::string pathStr (path);
72 for (int pos = 0; pos < (int)pathStr.length(); pos++)
74 if (pathStr[pos] == '.')
76 components.push_back(pathStr.substr(compStart, pos-compStart));
81 DE_ASSERT(compStart < (int)pathStr.length());
82 components.push_back(pathStr.substr(compStart));
87 TestNode::TestNode (TestGroup* parent, TestNodeType nodeType, const char* name, const char* desc)
89 , m_nodeType (nodeType)
91 , m_description (desc)
95 // Verify that the name is unique.
96 if (parent->m_childNames.find(name) != parent->m_childNames.end())
97 throw Error(std::string("Duplicate node '") + name + "' in '" + parent->getFullPath());
99 m_parent->m_children.push_back(this);
100 m_parent->m_childNames.insert(name);
104 void TestNode::getFullPath (std::string& dst) const
109 const TestNode* curNode = this;
113 nameLen += (int)curNode->m_name.length();
115 DE_ASSERT(curNode->m_parent);
116 if (curNode->m_parent->getNodeType() != TESTNODETYPE_ROOT)
119 curNode = curNode->m_parent;
132 std::copy(curNode->m_name.begin(), curNode->m_name.end(), dst.begin()+(pos-curNode->m_name.length()));
133 pos -= (int)curNode->m_name.length();
135 DE_ASSERT(curNode->m_parent);
136 if (curNode->m_parent->getNodeType() != TESTNODETYPE_ROOT)
139 curNode = curNode->m_parent;
146 const TestNode* TestNode::find (const char* path) const
148 if (m_nodeType == TESTNODETYPE_ROOT)
150 // Don't need to consider current node.
151 return static_cast<const TestGroup*>(this)->findChildNode(path);
155 // Check if first component matches this node.
156 int compLen = getFirstComponentLength(path);
157 XE_CHECK(compLen > 0);
159 if (compareNameToPathComponent(getName(), path, compLen))
161 if (path[compLen] == 0)
163 else if (getNodeType() == TESTNODETYPE_GROUP)
164 return static_cast<const TestGroup*>(this)->findChildNode(path + compLen + 1);
173 TestNode* TestNode::find (const char* path)
175 return const_cast<TestNode*>(const_cast<const TestNode*>(this)->find(path));
180 TestGroup::TestGroup (TestGroup* parent, TestNodeType nodeType, const char* name, const char* description)
181 : TestNode(parent, nodeType, name, description)
183 DE_ASSERT(nodeType == TESTNODETYPE_GROUP || nodeType == TESTNODETYPE_ROOT);
184 DE_ASSERT(!parent == (nodeType == TESTNODETYPE_ROOT));
187 TestGroup::~TestGroup (void)
189 for (std::vector<TestNode*>::iterator i = m_children.begin(); i != m_children.end(); i++)
193 TestGroup* TestGroup::createGroup (const char* name, const char* description)
195 return new TestGroup(this, TESTNODETYPE_GROUP, name, description);
198 TestCase* TestGroup::createCase (TestCaseType caseType, const char* name, const char* description)
200 return TestCase::createAsChild(this, caseType, name, description);
203 const TestNode* TestGroup::findChildNode (const char* path) const
205 int compLen = getFirstComponentLength(path);
206 XE_CHECK(compLen > 0);
208 // Try to find matching children.
209 const TestNode* matchingNode = DE_NULL;
210 for (vector<TestNode*>::const_iterator iter = m_children.begin(); iter != m_children.end(); iter++)
212 if (compareNameToPathComponent((*iter)->getName(), path, compLen))
214 matchingNode = *iter;
221 if (path[compLen] == 0)
222 return matchingNode; // Last element in path, return matching node.
223 else if (matchingNode->getNodeType() == TESTNODETYPE_GROUP)
224 return static_cast<const TestGroup*>(matchingNode)->findChildNode(path + compLen + 1);
234 TestRoot::TestRoot (void)
235 : TestGroup(DE_NULL, TESTNODETYPE_ROOT, "", "")
241 TestCase* TestCase::createAsChild(TestGroup* parent, TestCaseType caseType, const char *name, const char *description)
243 return new TestCase(parent, caseType, name, description);
246 TestCase::TestCase (TestGroup* parent, TestCaseType caseType, const char* name, const char* description)
247 : TestNode (parent, TESTNODETYPE_TEST_CASE, name, description)
248 , m_caseType (caseType)
252 TestCase::~TestCase (void)
256 // TestHierarchyBuilder helpers
258 void addChildGroupsToMap (std::map<std::string, TestGroup*>& groupMap, TestGroup* group)
260 for (int ndx = 0; ndx < group->getNumChildren(); ndx++)
262 TestNode* node = group->getChild(ndx);
263 if (node->getNodeType() == TESTNODETYPE_GROUP)
265 TestGroup* childGroup = static_cast<TestGroup*>(node);
266 std::string fullPath;
267 childGroup->getFullPath(fullPath);
269 groupMap.insert(std::make_pair(fullPath, childGroup));
270 addChildGroupsToMap(groupMap, childGroup);
275 // TestHierarchyBuilder
277 TestHierarchyBuilder::TestHierarchyBuilder (TestRoot* root)
280 addChildGroupsToMap(m_groupMap, root);
283 TestHierarchyBuilder::~TestHierarchyBuilder (void)
287 TestCase* TestHierarchyBuilder::createCase (const char* path, TestCaseType caseType)
289 // \todo [2012-09-05 pyry] This can be done with less string manipulations.
290 std::vector<std::string> components;
291 splitPath(path, components);
292 DE_ASSERT(!components.empty());
294 // Create all parents if necessary.
295 TestGroup* curGroup = m_root;
296 std::string curGroupPath;
297 for (int ndx = 0; ndx < (int)components.size()-1; ndx++)
299 if (!curGroupPath.empty())
301 curGroupPath += components[ndx];
303 std::map<std::string, TestGroup*>::const_iterator groupPos = m_groupMap.find(curGroupPath);
304 if (groupPos == m_groupMap.end())
306 TestGroup* newGroup = curGroup->createGroup(components[ndx].c_str(), "" /* description */);
307 m_groupMap.insert(std::make_pair(curGroupPath, newGroup));
311 curGroup = groupPos->second;
314 return curGroup->createCase(caseType, components.back().c_str(), "" /* description */);
319 static void addNodeAndParents (std::set<const TestNode*>& nodeSet, const TestNode* node)
321 while (node != DE_NULL)
323 nodeSet.insert(node);
324 node = node->getParent();
328 static void addChildren (std::set<const TestNode*>& nodeSet, const TestGroup* group)
330 for (int ndx = 0; ndx < group->getNumChildren(); ndx++)
332 const TestNode* child = group->getChild(ndx);
333 nodeSet.insert(child);
335 if (child->getNodeType() == TESTNODETYPE_GROUP)
336 addChildren(nodeSet, static_cast<const TestGroup*>(child));
340 static void removeChildren (std::set<const TestNode*>& nodeSet, const TestGroup* group)
342 for (int ndx = 0; ndx < group->getNumChildren(); ndx++)
344 const TestNode* child = group->getChild(ndx);
345 nodeSet.erase(child);
347 if (child->getNodeType() == TESTNODETYPE_GROUP)
348 removeChildren(nodeSet, static_cast<const TestGroup*>(child));
352 static bool hasChildrenInSet (const std::set<const TestNode*>& nodeSet, const TestGroup* group)
354 for (int ndx = 0; ndx < group->getNumChildren(); ndx++)
356 if (nodeSet.find(group->getChild(ndx)) != nodeSet.end())
362 static void removeEmptyGroups (std::set<const TestNode*>& nodeSet, const TestGroup* group)
364 if (!hasChildrenInSet(nodeSet, group))
366 nodeSet.erase(group);
367 if (group->getParent() != DE_NULL)
368 removeEmptyGroups(nodeSet, group->getParent());
374 void TestSet::add (const TestNode* node)
376 if (node->getNodeType() == TESTNODETYPE_TEST_CASE)
377 addCase(static_cast<const TestCase*>(node));
380 XE_CHECK(node->getNodeType() == TESTNODETYPE_GROUP ||
381 node->getNodeType() == TESTNODETYPE_ROOT);
382 addGroup(static_cast<const TestGroup*>(node));
386 void TestSet::addCase (const TestCase* testCase)
388 addNodeAndParents(m_set, testCase);
391 void TestSet::addGroup (const TestGroup* testGroup)
393 addNodeAndParents(m_set, testGroup);
394 addChildren(m_set, testGroup);
397 void TestSet::remove (const TestNode* node)
399 if (node->getNodeType() == TESTNODETYPE_TEST_CASE)
400 removeCase(static_cast<const TestCase*>(node));
403 XE_CHECK(node->getNodeType() == TESTNODETYPE_GROUP ||
404 node->getNodeType() == TESTNODETYPE_ROOT);
405 removeGroup(static_cast<const TestGroup*>(node));
409 void TestSet::removeCase (const TestCase* testCase)
411 if (m_set.find(testCase) != m_set.end())
413 m_set.erase(testCase);
414 removeEmptyGroups(m_set, testCase->getParent());
418 void TestSet::removeGroup (const TestGroup* testGroup)
420 if (m_set.find(testGroup) != m_set.end())
422 m_set.erase(testGroup);
423 removeChildren(m_set, testGroup);
424 if (testGroup->getParent() != DE_NULL)
425 removeEmptyGroups(m_set, testGroup->getParent());
429 // ConstTestNodeIterator
431 ConstTestNodeIterator::ConstTestNodeIterator (const TestNode* root)
436 ConstTestNodeIterator ConstTestNodeIterator::begin (const TestNode* root)
438 ConstTestNodeIterator iter(root);
439 iter.m_iterStack.push_back(GroupState(DE_NULL));
443 ConstTestNodeIterator ConstTestNodeIterator::end (const TestNode* root)
446 return ConstTestNodeIterator(root);
449 ConstTestNodeIterator& ConstTestNodeIterator::operator++ (void)
451 DE_ASSERT(!m_iterStack.empty());
453 const TestNode* curNode = **this;
454 TestNodeType curNodeType = curNode->getNodeType();
456 if ((curNodeType == TESTNODETYPE_GROUP || curNodeType == TESTNODETYPE_ROOT) &&
457 static_cast<const TestGroup*>(curNode)->getNumChildren() > 0)
459 m_iterStack.push_back(GroupState(static_cast<const TestGroup*>(curNode)));
465 const TestGroup* group = m_iterStack.back().group;
466 int& childNdx = m_iterStack.back().childNdx;
467 int numChildren = group ? group->getNumChildren() : 1;
470 if (childNdx == numChildren)
472 m_iterStack.pop_back();
473 if (m_iterStack.empty())
484 ConstTestNodeIterator ConstTestNodeIterator::operator++ (int)
486 ConstTestNodeIterator copy(*this);
491 const TestNode* ConstTestNodeIterator::operator* (void) const
493 DE_ASSERT(!m_iterStack.empty());
494 if (m_iterStack.size() == 1)
496 DE_ASSERT(m_iterStack[0].group == DE_NULL && m_iterStack[0].childNdx == 0);
500 return m_iterStack.back().group->getChild(m_iterStack.back().childNdx);
503 bool ConstTestNodeIterator::operator!= (const ConstTestNodeIterator& other) const
505 return m_root != other.m_root || m_iterStack != other.m_iterStack;