Merge "Add support for fatal errors to framework." into lmp-mr1-dev
[platform/upstream/VK-GL-CTS.git] / framework / common / tcuTestCaseWrapper.cpp
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
28 namespace tcu
29 {
30
31 TestCaseWrapper::TestCaseWrapper (TestContext& testCtx)
32         : m_testCtx                     (testCtx)
33         , m_testStartTime       (0)
34 {
35 }
36
37 TestCaseWrapper::~TestCaseWrapper (void)
38 {
39 }
40
41 bool 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                 m_testCtx.setTerminateAfter(true);
60         }
61         catch (const tcu::TestException& e)
62         {
63                 DE_ASSERT(!success);
64                 m_testCtx.setTestResult(e.getTestResult(), e.getMessage());
65                 m_testCtx.setTerminateAfter(e.isFatal());
66                 log << e;
67         }
68         catch (const tcu::Exception& e)
69         {
70                 DE_ASSERT(!success);
71                 m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, e.getMessage());
72                 log << e;
73         }
74
75         DE_ASSERT(success || m_testCtx.getTestResult() != QP_TEST_RESULT_LAST);
76
77         return success;
78 }
79
80 bool TestCaseWrapper::deinitTestCase (TestCase* testCase)
81 {
82         bool deinitOk = false;
83
84         // De-init case.
85         try
86         {
87                 testCase->deinit();
88                 deinitOk = true;
89         }
90         catch (const tcu::Exception& e)
91         {
92                 m_testCtx.getLog() << e
93                                                    << TestLog::Message << "Error in test case deinit, test program will terminate." << TestLog::EndMessage;
94         }
95
96         {
97                 const deInt64 duration = deGetMicroseconds()-m_testStartTime;
98                 m_testStartTime = 0;
99                 m_testCtx.getLog() << TestLog::Integer("TestDuration", "Test case duration in microseconds", "us", QP_KEY_TAG_TIME, duration);
100         }
101
102         return deinitOk;
103 }
104
105 TestNode::IterateResult TestCaseWrapper::iterateTestCase (TestCase* testCase)
106 {
107         // Iterate the sub-case.
108         TestLog&                                log                             = m_testCtx.getLog();
109         TestCase::IterateResult iterateResult   = TestCase::STOP;
110
111         try
112         {
113                 iterateResult = testCase->iterate();
114         }
115         catch (const std::bad_alloc&)
116         {
117                 m_testCtx.setTestResult(QP_TEST_RESULT_RESOURCE_ERROR, "Failed to allocate memory during test execution");
118                 m_testCtx.setTerminateAfter(true);
119         }
120         catch (const tcu::TestException& e)
121         {
122                 log << e;
123                 m_testCtx.setTestResult(e.getTestResult(), e.getMessage());
124                 m_testCtx.setTerminateAfter(e.isFatal());
125         }
126         catch (const tcu::Exception& e)
127         {
128                 log << e;
129                 m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, e.getMessage());
130         }
131
132         return iterateResult;
133 }
134
135 } // tcu