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 Test case wrapper for test execution.
22 *//*--------------------------------------------------------------------*/
23
24#include "tcuTestCaseWrapper.hpp"
25#include "tcuTestLog.hpp"
26#include "deClock.h"
27
28namespace tcu
29{
30
31TestCaseWrapper::TestCaseWrapper (TestContext& testCtx)
32	: m_testCtx			(testCtx)
33	, m_testStartTime	(0)
34{
35}
36
37TestCaseWrapper::~TestCaseWrapper (void)
38{
39}
40
41bool TestCaseWrapper::initTestCase (TestCase* testCase)
42{
43	// Initialize test case.
44	TestLog&	log		= m_testCtx.getLog();
45	bool		success	= false;
46
47	// Record test start time.
48	m_testStartTime = deGetMicroseconds();
49
50	try
51	{
52		testCase->init();
53		success = true;
54	}
55	catch (const std::bad_alloc&)
56	{
57		DE_ASSERT(!success);
58		m_testCtx.setTestResult(QP_TEST_RESULT_RESOURCE_ERROR, "Failed to allocate memory in test case init");
59	}
60	catch (const tcu::ResourceError& e)
61	{
62		DE_ASSERT(!success);
63		m_testCtx.setTestResult(QP_TEST_RESULT_RESOURCE_ERROR, e.getMessage());
64		log << e;
65	}
66	catch (const tcu::NotSupportedError& e)
67	{
68		DE_ASSERT(!success);
69		m_testCtx.setTestResult(QP_TEST_RESULT_NOT_SUPPORTED, e.getMessage());
70		log << e;
71	}
72	catch (const tcu::InternalError& e)
73	{
74		DE_ASSERT(!success);
75		m_testCtx.setTestResult(QP_TEST_RESULT_INTERNAL_ERROR, e.getMessage());
76		log << e;
77	}
78	catch (const tcu::Exception& e)
79	{
80		DE_ASSERT(!success);
81		m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, e.getMessage());
82		log << e;
83	}
84
85	DE_ASSERT(success || m_testCtx.getTestResult() != QP_TEST_RESULT_LAST);
86
87	return success;
88}
89
90bool TestCaseWrapper::deinitTestCase (TestCase* testCase)
91{
92	bool deinitOk = false;
93
94	// De-init case.
95	try
96	{
97		testCase->deinit();
98		deinitOk = true;
99	}
100	catch (const tcu::Exception& e)
101	{
102		m_testCtx.getLog() << e
103						   << TestLog::Message << "Error in test case deinit, test program will terminate." << TestLog::EndMessage;
104	}
105
106	{
107		const deInt64 duration = deGetMicroseconds()-m_testStartTime;
108		m_testStartTime = 0;
109		m_testCtx.getLog() << TestLog::Integer("TestDuration", "Test case duration in microseconds", "us", QP_KEY_TAG_TIME, duration);
110	}
111
112	return deinitOk;
113}
114
115TestNode::IterateResult TestCaseWrapper::iterateTestCase (TestCase* testCase)
116{
117	// Iterate the sub-case.
118	TestLog&				log				= m_testCtx.getLog();
119	TestCase::IterateResult	iterateResult	= TestCase::STOP;
120
121	try
122	{
123		iterateResult = testCase->iterate();
124	}
125	catch (const std::bad_alloc&)
126	{
127		m_testCtx.setTestResult(QP_TEST_RESULT_RESOURCE_ERROR, "Failed to allocate memory during test execution");
128	}
129	catch (const tcu::ResourceError& e)
130	{
131		log << e;
132		m_testCtx.setTestResult(QP_TEST_RESULT_RESOURCE_ERROR, e.getMessage());
133	}
134	catch (const tcu::NotSupportedError& e)
135	{
136		log << e;
137		m_testCtx.setTestResult(QP_TEST_RESULT_NOT_SUPPORTED, e.getMessage());
138	}
139	catch (const tcu::InternalError& e)
140	{
141		log << e;
142		m_testCtx.setTestResult(QP_TEST_RESULT_INTERNAL_ERROR, e.getMessage());
143	}
144	catch (const tcu::Exception& e)
145	{
146		log << e;
147		m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, e.getMessage());
148	}
149
150	return iterateResult;
151}
152
153} // tcu
154