1/*-------------------------------------------------------------------------
2 * drawElements Quality Program Tester Core
3 * ----------------------------------------
4 *
5 * Copyright 2014 The Android Open Source Project
6 *
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
10 *
11 *      http://www.apache.org/licenses/LICENSE-2.0
12 *
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.
18 *
19 *//*!
20 * \file
21 * \brief Base class for a test case.
22 *//*--------------------------------------------------------------------*/
23
24#include "tcuTestCase.hpp"
25#include "tcuPlatform.hpp"
26
27#include "deString.h"
28
29namespace tcu
30{
31
32using namespace std;
33
34// TestNode.
35
36inline bool isValidCaseName (const char* name)
37{
38	for (const char* p = name; *p != '\0'; p++)
39	{
40		if (!isValidTestCaseNameChar(*p))
41			return false;
42	}
43	return true;
44}
45
46TestNode::TestNode (TestContext& testCtx, TestNodeType nodeType, const char* name, const char* description)
47	: m_testCtx		(testCtx)
48	, m_name		(name)
49	, m_description	(description)
50	, m_nodeType	(nodeType)
51{
52	DE_ASSERT(isValidCaseName(name));
53}
54
55TestNode::TestNode (TestContext& testCtx, TestNodeType nodeType, const char* name, const char* description, const vector<TestNode*>& children)
56	: m_testCtx		(testCtx)
57	, m_name		(name)
58	, m_description	(description)
59	, m_nodeType	(nodeType)
60{
61	DE_ASSERT(isValidCaseName(name));
62	for (int i = 0; i < (int)children.size(); i++)
63		addChild(children[i]);
64}
65
66TestNode::~TestNode (void)
67{
68	TestNode::deinit();
69}
70
71void TestNode::getChildren (vector<TestNode*>& res)
72{
73	res.clear();
74	for (int i = 0; i < (int)m_children.size(); i++)
75		res.push_back(m_children[i]);
76}
77
78void TestNode::addChild (TestNode* node)
79{
80	// Child names must be unique!
81	// \todo [petri] O(n^2) algorithm, but shouldn't really matter..
82#if defined(DE_DEBUG)
83	for (int i = 0; i < (int)m_children.size(); i++)
84	{
85		if (deStringEqual(node->getName(), m_children[i]->getName()))
86			throw tcu::InternalError(std::string("Test case with non-unique name '") + node->getName() + "' added to group '" + getName() + "'.");
87	}
88#endif
89
90	// children only in group nodes
91	DE_ASSERT(getTestNodeTypeClass(m_nodeType) == NODECLASS_GROUP);
92
93	// children must have the same class
94	if (!m_children.empty())
95		DE_ASSERT(getTestNodeTypeClass(m_children.front()->getNodeType()) == getTestNodeTypeClass(node->getNodeType()));
96
97	m_children.push_back(node);
98}
99
100void TestNode::init (void)
101{
102}
103
104void TestNode::deinit (void)
105{
106	for (int i = 0; i < (int)m_children.size(); i++)
107		delete m_children[i];
108	m_children.clear();
109}
110
111// TestCaseGroup
112
113TestCaseGroup::TestCaseGroup (TestContext& testCtx, const char* name, const char* description)
114	: TestNode(testCtx, NODETYPE_GROUP, name, description)
115{
116}
117
118TestCaseGroup::TestCaseGroup (TestContext& testCtx, const char* name, const char* description, const vector<TestNode*>& children)
119	: TestNode(testCtx, NODETYPE_GROUP, name, description, children)
120{
121}
122
123TestCaseGroup::~TestCaseGroup (void)
124{
125}
126
127TestCase::IterateResult TestCaseGroup::iterate (void)
128{
129	DE_ASSERT(DE_FALSE); // should never be here!
130	throw InternalError("TestCaseGroup::iterate() called!", "", __FILE__, __LINE__);
131}
132
133// TestCase
134
135TestCase::TestCase (TestContext& testCtx, const char* name, const char* description)
136	: TestNode(testCtx, NODETYPE_SELF_VALIDATE, name, description)
137{
138}
139
140TestCase::TestCase (TestContext& testCtx, TestNodeType nodeType, const char* name, const char* description)
141	: TestNode(testCtx, nodeType, name, description)
142{
143	DE_ASSERT(isTestNodeTypeExecutable(nodeType));
144}
145
146TestCase::~TestCase (void)
147{
148}
149
150} // tcu
151