3 #include "tct-dali-core.h"
12 int RunTestCase( struct testcase_s& testCase )
15 if( testCase.startup )
19 result = testCase.function();
20 if( testCase.cleanup )
27 #define MAX_NUM_CHILDREN 16
32 const char* testCaseName;
40 TestCase(int tc, const char* name)
45 TestCase(const TestCase& rhs)
46 : testCase(rhs.testCase),
47 testCaseName(rhs.testCaseName)
50 TestCase& operator=(const TestCase& rhs)
52 testCase = rhs.testCase;
53 testCaseName = rhs.testCaseName;
60 typedef std::map<int, TestCase> RunningTestCases;
62 // Constantly runs up to MAX_NUM_CHILDREN processes
63 int RunAllInParallel(const char* processName, bool reRunFailed)
67 int numTestCases = sizeof(tc_array)/sizeof(struct testcase_s) - 1;
69 RunningTestCases children;
70 std::vector<int> failedTestCases;
72 // Fork up to MAX_NUM_CHILDREN processes, then
73 // wait. As soon as a proc completes, fork the next.
76 int numRunningChildren = 0;
77 while( nextTestCase < numTestCases || numRunningChildren > 0)
79 if( nextTestCase < numTestCases )
81 while( numRunningChildren < MAX_NUM_CHILDREN )
84 if( pid == 0 ) // Child process
88 exit( RunTestCase( tc_array[nextTestCase] ) );
95 else // Parent process
97 TestCase tc(nextTestCase, tc_array[nextTestCase].name);
100 numRunningChildren++;
106 int childPid = waitpid(-1, &status, 0);
113 if( WIFEXITED(status) )
117 int testResult = WEXITSTATUS(status);
120 printf("Test case %s failed: %d\n", children[childPid].testCaseName, testResult);
121 failedTestCases.push_back(children[childPid].testCase);
128 numRunningChildren--;
132 else if( WIFSIGNALED(status) )
136 RunningTestCases::iterator iter = children.find(childPid);
137 if( iter != children.end() )
139 printf("Test case %s exited with signal %d\n", iter->second.testCaseName, WTERMSIG(status));
140 failedTestCases.push_back(iter->second.testCase);
144 printf("Unknown child process: %d signaled %d\n", childPid, WTERMSIG(status));
148 numRunningChildren--;
153 printf("\rNumber of test passes: %d \n", numPasses);
154 printf("Number of test failures: %d\n", numFailures);
158 for( unsigned int i=0; i<failedTestCases.size(); i++)
160 printf("Running test case %s:\n", tc_array[failedTestCases[i]].name );
161 RunTestCase( tc_array[failedTestCases[i] ] );
168 int FindAndRunTestCase(const char* testCaseName)
172 for( int i = 0; tc_array[i].name; i++ )
174 if( !strcmp(testCaseName, tc_array[i].name) )
176 return RunTestCase( tc_array[i] );
180 printf("Unknown testcase name: \"%s\"\n", testCaseName);
184 int main(int argc, char * const argv[])
188 const char* optString = "pr";
189 bool optParallel(false);
190 bool optRerunFailed(false);
195 nextOpt = getopt( argc, argv, optString );
202 optRerunFailed = true;
205 } while( nextOpt != -1 );
209 result = RunAllInParallel(argv[0], optRerunFailed);
214 printf("Usage: %s <testcase name>\n", argv[0]);
217 result = FindAndRunTestCase(argv[1]);