663658410dbbb4615e84e350e94eabd96d2d492e
[platform/core/uifw/dali-adaptor.git] / automated-tests / src / dali-platform-abstraction / tct-dali-platform-abstraction-core.cpp
1 #include <stdio.h>
2 #include <string.h>
3 #include "tct-dali-platform-abstraction-core.h"
4 #include <stdlib.h>
5 #include <getopt.h>
6 #include <sys/types.h>
7 #include <sys/wait.h>
8 #include <unistd.h>
9 #include <vector>
10 #include <map>
11
12 int RunTestCase( struct testcase_s& testCase )
13 {
14   int result = 1;
15   if( testCase.startup )
16   {
17     testCase.startup();
18   }
19   result = testCase.function();
20   if( testCase.cleanup )
21   {
22     testCase.cleanup();
23   }
24   return result;
25 }
26
27 #define MAX_NUM_CHILDREN 16
28
29 struct TestCase
30 {
31   int testCase;
32   const char* testCaseName;
33
34   TestCase()
35   : testCase(0),
36     testCaseName(NULL)
37   {
38   }
39
40   TestCase(int tc, const char* name)
41   : testCase(tc),
42     testCaseName(name)
43   {
44   }
45   TestCase(const TestCase& rhs)
46   : testCase(rhs.testCase),
47     testCaseName(rhs.testCaseName)
48   {
49   }
50   TestCase& operator=(const TestCase& rhs)
51   {
52     testCase = rhs.testCase;
53     testCaseName = rhs.testCaseName;
54     return *this;
55
56   }
57 };
58
59
60 typedef std::map<int, TestCase> RunningTestCases;
61
62 int RunAll(const char* processName, bool reRunFailed)
63 {
64   int numFailures = 0;
65   int numPasses = 0;
66   unsigned int numTestCases = sizeof(tc_array)/sizeof(struct testcase_s) - 1;
67
68   // Run test cases in child process( to kill output ), but run serially.
69   for( unsigned int i=0; i<numTestCases; i++)
70   {
71     int pid = fork();
72     if( pid == 0 ) // Child process
73     {
74       close(STDOUT_FILENO);
75       close(STDERR_FILENO);
76       exit( RunTestCase( tc_array[i] ) );
77     }
78     else if(pid == -1)
79     {
80       perror("fork");
81       exit(2);
82     }
83     else // Parent process
84     {
85       int status = 0;
86       int childPid = waitpid(-1, &status, 0);
87       if( childPid == -1 )
88       {
89         perror("waitpid");
90         exit(2);
91       }
92       if( WIFEXITED(status) )
93       {
94         if( childPid > 0 )
95         {
96           int testResult = WEXITSTATUS(status);
97           if( testResult )
98           {
99             printf("Test case %s failed: %d\n", tc_array[i].name, testResult);
100             numFailures++;
101           }
102           else
103           {
104             numPasses++;
105           }
106         }
107       }
108       else if(WIFSIGNALED(status) )
109       {
110         if( childPid > 0 )
111         {
112           printf("Test case %s exited with signal %d\n", tc_array[i].name, WTERMSIG(status));
113           numFailures++;
114         }
115       }
116     }
117   }
118
119
120   printf("\rNumber of test passes: %d\n", numPasses);
121   printf("Number of test failures: %d\n", numFailures);
122
123   return numFailures;
124 }
125
126 int FindAndRunTestCase(const char* testCaseName)
127 {
128   int result = 2;
129
130   for( int i = 0; tc_array[i].name; i++ )
131   {
132     if( !strcmp(testCaseName, tc_array[i].name) )
133     {
134       return RunTestCase( tc_array[i] );
135     }
136   }
137
138   printf("Unknown testcase name: \"%s\"\n", testCaseName);
139   return result;
140 }
141
142 int main(int argc, char * const argv[])
143 {
144   int result = -1;
145
146   const char* optString = "pr";
147   bool optParallel(false);
148   bool optRerunFailed(false);
149
150   int nextOpt = 0;
151   do
152   {
153     nextOpt = getopt( argc, argv, optString );
154     switch(nextOpt)
155     {
156       case 'p':
157         optParallel = true;
158         break;
159       case 'r':
160         optRerunFailed = true;
161         break;
162     }
163   } while( nextOpt != -1 );
164
165   if( optParallel )
166   {
167     // For this test harness, run tests only in serial ( but without output )
168     result = RunAll(argv[0], optRerunFailed);
169   }
170   else
171   {
172     if (argc != 2) {
173       printf("Usage: %s <testcase name>\n", argv[0]);
174       return 2;
175     }
176     result = FindAndRunTestCase(argv[1]);
177   }
178   return result;
179 }