lin
2025-08-14 dae8bad597b6607a449b32bf76c523423f7720ed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*-------------------------------------------------------------------------
 * drawElements Quality Program Tester Core
 * ----------------------------------------
 *
 * Copyright 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *//*!
 * \file
 * \brief Test executor.
 *//*--------------------------------------------------------------------*/
 
#include "tcuTestSessionExecutor.hpp"
#include "tcuCommandLine.hpp"
#include "tcuTestLog.hpp"
 
#include "deClock.h"
 
namespace tcu
{
 
using std::vector;
 
static qpTestCaseType nodeTypeToTestCaseType (TestNodeType nodeType)
{
   switch (nodeType)
   {
       case NODETYPE_SELF_VALIDATE:    return QP_TEST_CASE_TYPE_SELF_VALIDATE;
       case NODETYPE_PERFORMANCE:        return QP_TEST_CASE_TYPE_PERFORMANCE;
       case NODETYPE_CAPABILITY:        return QP_TEST_CASE_TYPE_CAPABILITY;
       case NODETYPE_ACCURACY:            return QP_TEST_CASE_TYPE_ACCURACY;
       default:
           DE_ASSERT(false);
           return QP_TEST_CASE_TYPE_LAST;
   }
}
 
TestSessionExecutor::TestSessionExecutor (TestPackageRoot& root, TestContext& testCtx)
   : m_testCtx                (testCtx)
   , m_inflater            (testCtx)
   , m_caseListFilter        (testCtx.getCommandLine().createCaseListFilter(testCtx.getArchive()))
   , m_iterator            (root, m_inflater, *m_caseListFilter)
   , m_state                (STATE_TRAVERSE_HIERARCHY)
   , m_abortSession        (false)
   , m_isInTestCase        (false)
   , m_testStartTime        (0)
   , m_packageStartTime    (0)
{
}
 
TestSessionExecutor::~TestSessionExecutor (void)
{
}
 
bool TestSessionExecutor::iterate (void)
{
   while (!m_abortSession)
   {
       switch (m_state)
       {
           case STATE_TRAVERSE_HIERARCHY:
           {
               const TestHierarchyIterator::State    hierIterState    = m_iterator.getState();
 
               if (hierIterState == TestHierarchyIterator::STATE_ENTER_NODE ||
                   hierIterState == TestHierarchyIterator::STATE_LEAVE_NODE)
               {
                   TestNode* const        curNode        = m_iterator.getNode();
                   const TestNodeType    nodeType    = curNode->getNodeType();
                   const bool            isEnter        = hierIterState == TestHierarchyIterator::STATE_ENTER_NODE;
 
                   switch (nodeType)
                   {
                       case NODETYPE_PACKAGE:
                       {
                           TestPackage* const testPackage = static_cast<TestPackage*>(curNode);
                           isEnter ? enterTestPackage(testPackage) : leaveTestPackage(testPackage);
                           break;
                       }
 
                       case NODETYPE_GROUP:
                       {
                           isEnter ? enterTestGroup(m_iterator.getNodePath()) : leaveTestGroup(m_iterator.getNodePath());
                           break; // nada
                       }
 
                       case NODETYPE_SELF_VALIDATE:
                       case NODETYPE_PERFORMANCE:
                       case NODETYPE_CAPABILITY:
                       case NODETYPE_ACCURACY:
                       {
                           TestCase* const testCase = static_cast<TestCase*>(curNode);
 
                           if (isEnter)
                           {
                               if (enterTestCase(testCase, m_iterator.getNodePath()))
                                   m_state = STATE_EXECUTE_TEST_CASE;
                               // else remain in TRAVERSING_HIERARCHY => node will be exited from in the next iteration
                           }
                           else
                               leaveTestCase(testCase);
 
                           break;
                       }
 
                       default:
                           DE_ASSERT(false);
                           break;
                   }
 
                   m_iterator.next();
                   break;
               }
               else
               {
                   DE_ASSERT(hierIterState == TestHierarchyIterator::STATE_FINISHED);
                   m_status.isComplete = true;
                   return false;
               }
           }
 
           case STATE_EXECUTE_TEST_CASE:
           {
               DE_ASSERT(m_iterator.getState() == TestHierarchyIterator::STATE_LEAVE_NODE &&
                         isTestNodeTypeExecutable(m_iterator.getNode()->getNodeType()));
 
               TestCase* const                    testCase    = static_cast<TestCase*>(m_iterator.getNode());
               const TestCase::IterateResult    iterResult    = iterateTestCase(testCase);
 
               if (iterResult == TestCase::STOP)
                   m_state = STATE_TRAVERSE_HIERARCHY;
 
               return true;
           }
 
           default:
               DE_ASSERT(false);
               break;
       }
   }
 
   return false;
}
 
void TestSessionExecutor::enterTestPackage (TestPackage* testPackage)
{
   // Create test case wrapper
   DE_ASSERT(!m_caseExecutor);
   m_caseExecutor = de::MovePtr<TestCaseExecutor>(testPackage->createExecutor());
   m_packageStartTime    = deGetMicroseconds();
}
 
void TestSessionExecutor::leaveTestPackage (TestPackage* testPackage)
{
   DE_UNREF(testPackage);
   m_caseExecutor.clear();
   m_testCtx.getLog().startTestsCasesTime();
 
   {
       const deInt64 duration = deGetMicroseconds() - m_packageStartTime;
       m_packageStartTime = 0;
       m_testCtx.getLog() << TestLog::Integer(testPackage->getName(), "Total tests case duration in microseconds", "us", QP_KEY_TAG_TIME, duration);
   }
 
   for(std::map<std::string, deUint64>::iterator it=m_groupsDurationTime.begin(); it != m_groupsDurationTime.end(); ++it)
       m_testCtx.getLog() << TestLog::Integer(it->first, "The test group case duration in microseconds", "us", QP_KEY_TAG_TIME, it->second);
 
   m_testCtx.getLog().endTestsCasesTime();
}
 
void TestSessionExecutor::enterTestGroup (const std::string& casePath)
{
   m_groupsDurationTime[casePath] = deGetMicroseconds();
}
 
void TestSessionExecutor::leaveTestGroup (const std::string& casePath)
{
   m_groupsDurationTime[casePath] = deGetMicroseconds() - m_groupsDurationTime[casePath];
}
 
bool TestSessionExecutor::enterTestCase (TestCase* testCase, const std::string& casePath)
{
   TestLog&                log            = m_testCtx.getLog();
   const qpTestCaseType    caseType    = nodeTypeToTestCaseType(testCase->getNodeType());
   bool                    initOk        = false;
 
   print("\nTest case '%s'..\n", casePath.c_str());
 
   m_testCtx.setTestResult(QP_TEST_RESULT_LAST, "");
   m_testCtx.setTerminateAfter(false);
   log.startCase(casePath.c_str(), caseType);
 
   m_isInTestCase    = true;
   m_testStartTime    = deGetMicroseconds();
 
   try
   {
       m_caseExecutor->init(testCase, casePath);
       initOk = true;
   }
   catch (const std::bad_alloc&)
   {
       DE_ASSERT(!initOk);
       m_testCtx.setTestResult(QP_TEST_RESULT_RESOURCE_ERROR, "Failed to allocate memory in test case init");
       m_testCtx.setTerminateAfter(true);
   }
   catch (const tcu::TestException& e)
   {
       DE_ASSERT(!initOk);
       DE_ASSERT(e.getTestResult() != QP_TEST_RESULT_LAST);
       m_testCtx.setTestResult(e.getTestResult(), e.getMessage());
       m_testCtx.setTerminateAfter(e.isFatal());
       log << e;
   }
   catch (const tcu::Exception& e)
   {
       DE_ASSERT(!initOk);
       m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, e.getMessage());
       log << e;
   }
 
   DE_ASSERT(initOk || m_testCtx.getTestResult() != QP_TEST_RESULT_LAST);
 
   return initOk;
}
 
void TestSessionExecutor::leaveTestCase (TestCase* testCase)
{
   TestLog&    log        = m_testCtx.getLog();
 
   // De-init case.
   try
   {
       m_caseExecutor->deinit(testCase);
   }
   catch (const tcu::Exception& e)
   {
       log << e << TestLog::Message << "Error in test case deinit, test program will terminate." << TestLog::EndMessage;
       m_testCtx.setTerminateAfter(true);
   }
 
   {
       const deInt64 duration = deGetMicroseconds()-m_testStartTime;
       m_testStartTime = 0;
       m_testCtx.getLog() << TestLog::Integer("TestDuration", "Test case duration in microseconds", "us", QP_KEY_TAG_TIME, duration);
   }
 
   {
       const qpTestResult    testResult        = m_testCtx.getTestResult();
       const char* const    testResultDesc    = m_testCtx.getTestResultDesc();
       const bool            terminateAfter    = m_testCtx.getTerminateAfter();
       DE_ASSERT(testResult != QP_TEST_RESULT_LAST);
 
       m_isInTestCase = false;
       m_testCtx.getLog().endCase(testResult, testResultDesc);
 
       // Update statistics.
       print("  %s (%s)\n", qpGetTestResultName(testResult), testResultDesc);
 
       m_status.numExecuted += 1;
       switch (testResult)
       {
           case QP_TEST_RESULT_PASS:                    m_status.numPassed            += 1;    break;
           case QP_TEST_RESULT_NOT_SUPPORTED:            m_status.numNotSupported    += 1;    break;
           case QP_TEST_RESULT_QUALITY_WARNING:        m_status.numWarnings        += 1;    break;
           case QP_TEST_RESULT_COMPATIBILITY_WARNING:    m_status.numWarnings        += 1;    break;
           default:                                    m_status.numFailed            += 1;    break;
       }
 
       // terminateAfter, Resource error or any error in deinit means that execution should end
       if (terminateAfter || testResult == QP_TEST_RESULT_RESOURCE_ERROR)
           m_abortSession = true;
   }
 
   if (m_testCtx.getWatchDog())
       qpWatchDog_reset(m_testCtx.getWatchDog());
}
 
TestCase::IterateResult TestSessionExecutor::iterateTestCase (TestCase* testCase)
{
   TestLog&                log                = m_testCtx.getLog();
   TestCase::IterateResult    iterateResult    = TestCase::STOP;
 
   m_testCtx.touchWatchdog();
 
   try
   {
       iterateResult = m_caseExecutor->iterate(testCase);
   }
   catch (const std::bad_alloc&)
   {
       m_testCtx.setTestResult(QP_TEST_RESULT_RESOURCE_ERROR, "Failed to allocate memory during test execution");
       m_testCtx.setTerminateAfter(true);
   }
   catch (const tcu::TestException& e)
   {
       log << e;
       m_testCtx.setTestResult(e.getTestResult(), e.getMessage());
       m_testCtx.setTerminateAfter(e.isFatal());
   }
   catch (const tcu::Exception& e)
   {
       log << e;
       m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, e.getMessage());
   }
 
   return iterateResult;
}
 
} // tcu