2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "test-harness.h"
19 #include <sys/types.h>
30 typedef std::map<int, TestCase> RunningTestCases;
34 const char* RED_COLOR="\e[1;31m";
35 const char* GREEN_COLOR="\e[1;32m";
36 const char* ASCII_RESET="\e[0m";
37 const char* ASCII_BOLD="\e[1m";
41 int RunTestCase( struct ::testcase_s& testCase )
43 int result = EXIT_STATUS_TESTCASE_FAILED;
45 // dont want to catch exception as we want to be able to get
46 // gdb stack trace from the first error
47 // by default tests should all always pass with no exceptions
48 if( testCase.startup )
52 result = testCase.function();
53 if( testCase.cleanup )
61 int RunTestCaseInChildProcess( struct ::testcase_s& testCase, bool suppressOutput )
63 int testResult = EXIT_STATUS_TESTCASE_FAILED;
66 if( pid == 0 ) // Child process
73 exit( RunTestCase( testCase ) );
78 exit(EXIT_STATUS_FORK_FAILED);
80 else // Parent process
83 int childPid = waitpid(-1, &status, 0);
87 exit(EXIT_STATUS_WAITPID_FAILED);
89 if( WIFEXITED(status) )
93 testResult = WEXITSTATUS(status);
96 printf("Test case %s failed: %d\n", testCase.name, testResult);
100 else if(WIFSIGNALED(status) )
102 testResult = EXIT_STATUS_TESTCASE_ABORTED;
105 if(WCOREDUMP(status))
107 printf("Test case %s crashed\n", testCase.name);
110 printf("Test case %s exited with signal %s\n", testCase.name, strsignal(WTERMSIG(status)));
112 else if(WIFSTOPPED(status))
114 printf("Test case %s stopped with signal %s\n", testCase.name, strsignal(WSTOPSIG(status)));
120 void OutputStatistics( int numPasses, int numFailures )
122 const char* failureColor = GREEN_COLOR;
123 if( numFailures > 0 )
125 failureColor = RED_COLOR;
127 printf("\rNumber of test passes: %s%4d (%5.2f%%)%s\n", ASCII_BOLD, numPasses, 100.0f * (float)numPasses / (numPasses+numFailures), ASCII_RESET);
128 printf("%sNumber of test failures:%s %s%4d%s\n", failureColor, ASCII_RESET, ASCII_BOLD, numFailures, ASCII_RESET);
133 int RunAll(const char* processName, ::testcase tc_array[], bool reRunFailed)
138 // Run test cases in child process( to kill output/handle signals ), but run serially.
139 for( unsigned int i=0; tc_array[i].name; i++)
141 int result = RunTestCaseInChildProcess( tc_array[i], true );
152 OutputStatistics(numPasses, numFailures);
159 // Constantly runs up to MAX_NUM_CHILDREN processes
160 int RunAllInParallel( const char* processName, ::testcase tc_array[], bool reRunFailed)
165 RunningTestCases children;
166 std::vector<int> failedTestCases;
168 // Fork up to MAX_NUM_CHILDREN processes, then
169 // wait. As soon as a proc completes, fork the next.
171 int nextTestCase = 0;
172 int numRunningChildren = 0;
174 while( tc_array[nextTestCase].name || numRunningChildren > 0)
176 // Create more children (up to the max number or til the end of the array)
177 while( numRunningChildren < MAX_NUM_CHILDREN && tc_array[nextTestCase].name )
180 if( pid == 0 ) // Child process
182 close(STDOUT_FILENO);
183 close(STDERR_FILENO);
184 exit( RunTestCase( tc_array[nextTestCase] ) );
189 exit(EXIT_STATUS_FORK_FAILED);
191 else // Parent process
193 TestCase tc(nextTestCase, tc_array[nextTestCase].name);
196 numRunningChildren++;
200 // Wait for the next child to finish
203 int childPid = waitpid(-1, &status, 0);
207 exit(EXIT_STATUS_WAITPID_FAILED);
210 if( WIFEXITED(status) )
214 int testResult = WEXITSTATUS(status);
217 printf("Test case %s failed: %d\n", children[childPid].testCaseName, testResult);
218 failedTestCases.push_back(children[childPid].testCase);
225 numRunningChildren--;
229 else if( WIFSIGNALED(status) || WIFSTOPPED(status))
231 status = WIFSIGNALED(status)?WTERMSIG(status):WSTOPSIG(status);
235 RunningTestCases::iterator iter = children.find(childPid);
236 if( iter != children.end() )
238 printf("Test case %s exited with signal %s\n", iter->second.testCaseName, strsignal(status));
239 failedTestCases.push_back(iter->second.testCase);
243 printf("Unknown child process: %d signaled %s\n", childPid, strsignal(status));
247 numRunningChildren--;
252 OutputStatistics( numPasses, numFailures );
256 for( unsigned int i=0; i<failedTestCases.size(); i++)
258 char* testCaseStrapline;
259 int numChars = asprintf(&testCaseStrapline, "Test case %s", tc_array[failedTestCases[i]].name );
260 printf("\n%s\n", testCaseStrapline);
261 for(int j=0; j<numChars; j++)
266 RunTestCaseInChildProcess( tc_array[failedTestCases[i] ], false );
275 int FindAndRunTestCase(::testcase tc_array[], const char* testCaseName)
277 int result = EXIT_STATUS_TESTCASE_NOT_FOUND;
279 for( int i = 0; tc_array[i].name; i++ )
281 if( !strcmp(testCaseName, tc_array[i].name) )
283 return RunTestCase( tc_array[i] );
287 printf("Unknown testcase name: \"%s\"\n", testCaseName);
291 void Usage(const char* program)
294 " %s <testcase name>\t\t Execute a test case\n"
295 " %s \t\t Execute all test cases in parallel\n"
296 " %s -r\t\t Execute all test cases in parallel, rerunning failed test cases\n",
297 program, program, program);