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;
47 if( testCase.startup )
51 result = testCase.function();
52 if( testCase.cleanup )
59 printf("Caught exception in test case.\n");
60 result = EXIT_STATUS_TESTCASE_ABORTED;
66 int RunTestCaseInChildProcess( struct ::testcase_s& testCase, bool suppressOutput )
68 int testResult = EXIT_STATUS_TESTCASE_FAILED;
71 if( pid == 0 ) // Child process
78 exit( RunTestCase( testCase ) );
83 exit(EXIT_STATUS_FORK_FAILED);
85 else // Parent process
88 int childPid = waitpid(-1, &status, 0);
92 exit(EXIT_STATUS_WAITPID_FAILED);
94 if( WIFEXITED(status) )
98 testResult = WEXITSTATUS(status);
101 printf("Test case %s failed: %d\n", testCase.name, testResult);
105 else if(WIFSIGNALED(status) )
107 testResult = EXIT_STATUS_TESTCASE_ABORTED;
110 if(WCOREDUMP(status))
112 printf("Test case %s crashed\n", testCase.name);
115 printf("Test case %s exited with signal %s\n", testCase.name, strsignal(WTERMSIG(status)));
117 else if(WIFSTOPPED(status))
119 printf("Test case %s stopped with signal %s\n", testCase.name, strsignal(WSTOPSIG(status)));
125 void OutputStatistics( int numPasses, int numFailures )
127 const char* failureColor = GREEN_COLOR;
128 if( numFailures > 0 )
130 failureColor = RED_COLOR;
132 printf("\rNumber of test passes: %s%4d (%5.2f%%)%s\n", ASCII_BOLD, numPasses, 100.0f * (float)numPasses / (numPasses+numFailures), ASCII_RESET);
133 printf("%sNumber of test failures:%s %s%4d%s\n", failureColor, ASCII_RESET, ASCII_BOLD, numFailures, ASCII_RESET);
138 int RunAll(const char* processName, ::testcase tc_array[], bool reRunFailed)
143 // Run test cases in child process( to kill output/handle signals ), but run serially.
144 for( unsigned int i=0; tc_array[i].name; i++)
146 int result = RunTestCaseInChildProcess( tc_array[i], true );
157 OutputStatistics(numPasses, numFailures);
164 // Constantly runs up to MAX_NUM_CHILDREN processes
165 int RunAllInParallel( const char* processName, ::testcase tc_array[], bool reRunFailed)
170 RunningTestCases children;
171 std::vector<int> failedTestCases;
173 // Fork up to MAX_NUM_CHILDREN processes, then
174 // wait. As soon as a proc completes, fork the next.
176 int nextTestCase = 0;
177 int numRunningChildren = 0;
179 while( tc_array[nextTestCase].name || numRunningChildren > 0)
181 // Create more children (up to the max number or til the end of the array)
182 while( numRunningChildren < MAX_NUM_CHILDREN && tc_array[nextTestCase].name )
185 if( pid == 0 ) // Child process
187 close(STDOUT_FILENO);
188 close(STDERR_FILENO);
189 exit( RunTestCase( tc_array[nextTestCase] ) );
194 exit(EXIT_STATUS_FORK_FAILED);
196 else // Parent process
198 TestCase tc(nextTestCase, tc_array[nextTestCase].name);
201 numRunningChildren++;
205 // Wait for the next child to finish
208 int childPid = waitpid(-1, &status, 0);
212 exit(EXIT_STATUS_WAITPID_FAILED);
215 if( WIFEXITED(status) )
219 int testResult = WEXITSTATUS(status);
222 printf("Test case %s failed: %d\n", children[childPid].testCaseName, testResult);
223 failedTestCases.push_back(children[childPid].testCase);
230 numRunningChildren--;
234 else if( WIFSIGNALED(status) || WIFSTOPPED(status))
236 status = WIFSIGNALED(status)?WTERMSIG(status):WSTOPSIG(status);
240 RunningTestCases::iterator iter = children.find(childPid);
241 if( iter != children.end() )
243 printf("Test case %s exited with signal %s\n", iter->second.testCaseName, strsignal(status));
244 failedTestCases.push_back(iter->second.testCase);
248 printf("Unknown child process: %d signaled %s\n", childPid, strsignal(status));
252 numRunningChildren--;
257 OutputStatistics( numPasses, numFailures );
261 for( unsigned int i=0; i<failedTestCases.size(); i++)
263 char* testCaseStrapline;
264 int numChars = asprintf(&testCaseStrapline, "Test case %s", tc_array[failedTestCases[i]].name );
265 printf("\n%s\n", testCaseStrapline);
266 for(int j=0; j<numChars; j++)
271 RunTestCaseInChildProcess( tc_array[failedTestCases[i] ], false );
280 int FindAndRunTestCase(::testcase tc_array[], const char* testCaseName)
282 int result = EXIT_STATUS_TESTCASE_NOT_FOUND;
284 for( int i = 0; tc_array[i].name; i++ )
286 if( !strcmp(testCaseName, tc_array[i].name) )
288 return RunTestCase( tc_array[i] );
292 printf("Unknown testcase name: \"%s\"\n", testCaseName);
296 void Usage(const char* program)
299 " %s <testcase name>\t\t Execute a test case\n"
300 " %s \t\t Execute all test cases in parallel\n"
301 " %s -r\t\t Execute all test cases in parallel, rerunning failed test cases\n",
302 program, program, program);