Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove useless double parentheses in function calls.
[simgrid.git] / win32_test_app / src / TTestCaseContext.c
1 #include <TTestCaseContext.h>
2
3 /*
4  * Implementation of s_TestCaseContext connected functions.
5  */
6
7 /* 
8  * Create a new s_TestCaseContext and returns a pointer to self.
9  */
10 TestCaseContext_t TestCaseContext_new(void)
11 {
12   TestCaseContext_t context = calloc(1, sizeof(s_TestCaseContext_t));
13
14   if (NULL == context) {
15     setErrno(E_TEST_CASE_CONTEXT_ALLOCATION_FAILED);
16     return NULL;
17   }
18
19   context->inputBuffer = Buffer_new();
20
21   if (NULL == context->inputBuffer) {
22     TestCaseContext_free(context);
23     return NULL;
24   }
25
26   context->outputBuffer = Buffer_new();
27
28   if (NULL == context->outputBuffer) {
29     TestCaseContext_free(context);
30     return NULL;
31   }
32
33   context->expectedOutputBuffer = Buffer_new();
34
35   if (NULL == context->expectedOutputBuffer) {
36     TestCaseContext_free(context);
37     return NULL;
38   }
39
40   context->commandLineBuffer = Buffer_new();
41
42   if (NULL == context->commandLineBuffer) {
43     TestCaseContext_free(context);
44     return NULL;
45   }
46
47
48   context->isOutputCheckingEnabled = DEFAULT_OUTPUT_CHECKING_MODE;
49   context->isPostOutputCheckingEnabled = DEFAULT_POST_OUTPUT_CHECKING_MODE;
50   context->timeoutValue = DEFAULT_TIMEOUT_VALUE;
51   context->expectedExitCode = INVALID_EXIT_CODE;
52   context->exitCode = INVALID_EXIT_CODE;
53   context->name = NULL;
54
55   context->runThread = true;
56   context->hThread = NULL;
57   context->hOutputRead = NULL;
58   context->hInputWrite = NULL;
59   context->hChildStdInRead = NULL;
60   context->hChildStdOutWrite = NULL;
61   context->hChildStderr = NULL;
62   context->hChildStdoutReadTmp = NULL;
63   context->hChildStdinWriteTmp = NULL;
64   context->hConsole = NULL;
65
66   context->createConsole = false;
67   context->exitCodeCheckingEnabled = false;
68
69   context->started = false;
70
71   memset(&(context->pi), 0, sizeof(PROCESS_INFORMATION));
72
73   return context;
74 }
75
76 /* 
77  * Destroy the s_TestCaseContext referenced by context. 
78  */
79 void TestCaseContext_free(TestCaseContext_t context)
80 {
81   if (NULL == context)
82     return;
83
84   if (NULL != context->inputBuffer)
85     Buffer_free(context->inputBuffer);
86
87   if (NULL != context->outputBuffer)
88     Buffer_free(context->outputBuffer);
89
90   if (NULL != context->expectedOutputBuffer)
91     Buffer_free(context->expectedOutputBuffer);
92
93   if (NULL != context->commandLineBuffer)
94     Buffer_free(context->commandLineBuffer);
95
96   if (NULL == context->name)
97     free(context->name);
98
99
100   /* Close all pipe handles. */
101   if (context->hChildStdoutReadTmp)
102     CloseHandle(context->hChildStdoutReadTmp);
103
104   if (context->hChildStdInRead)
105     CloseHandle(context->hChildStdInRead);
106
107   if (context->hChildStdinWriteTmp)
108     CloseHandle(context->hChildStdinWriteTmp);
109
110   if (context->hChildStdOutWrite)
111     CloseHandle(context->hChildStdOutWrite);
112
113   if (context->hOutputRead)
114     CloseHandle(context->hOutputRead);
115
116   if (context->pi.hThread)
117     CloseHandle(context->pi.hThread);
118
119   /* Use some violence, no choice. */
120   if (context->pi.hProcess) {
121     /* Kill the child process. */
122     TerminateProcess(context->pi.hProcess, 0);
123   }
124
125   if (context->hThread) {
126     /* Terminate the thread */
127     TerminateThread(context->hThread, 0);
128   }
129
130   if (context->hInputWrite)
131     CloseHandle(context->hInputWrite);
132
133   if (context->hChildStderr)
134     CloseHandle(context->hChildStderr);
135
136   free(context);
137   context = NULL;
138 }
139
140 /* 
141  * Set the timeout of the test case context.
142  */
143 void TestCaseContext_setTimeout(TestCaseContext_t context, int timeout)
144 {
145   context->timeoutValue = timeout;
146 }
147
148 /*
149  * Enable the output checking of the test case context.
150  */
151 void TestCaseContext_enableOutputChecking(TestCaseContext_t context)
152 {
153   context->isOutputCheckingEnabled = true;
154 }
155
156 /*
157  * Enable the output checking of the test case context.
158  */
159 void TestCaseContext_disableOutputChecking(TestCaseContext_t context)
160 {
161   /* If the post output checking mode is enable, disable it */
162   context->isPostOutputCheckingEnabled = false;
163   context->isOutputCheckingEnabled = false;
164 }
165
166 /*
167  * Enable the post output checking of the test case context.
168  */
169 void TestCaseContext_enable_post_output_checking(TestCaseContext_t context)
170 {
171   /* enable the post output checking mode if the output checking mode is enabled */
172   if (context->isOutputCheckingEnabled)
173     context->isPostOutputCheckingEnabled = true;
174 }
175
176 /*
177  * Disable the post output checking of the test case context.
178  */
179 void TestCaseContext_disablePostOutputChecking(TestCaseContext_t context)
180 {
181   context->isPostOutputCheckingEnabled = false;
182 }
183
184 void TestCaseContext_createConsole(TestCaseContext_t context)
185 {
186   context->createConsole = true;
187 }
188
189 void TestCaseContext_createNoConsole(TestCaseContext_t context)
190 {
191   context->createConsole = false;
192 }
193
194 /*
195  * Set the expected exit code of the test case context.
196  */
197 void TestCaseContext_setExpectedExitCode(TestCaseContext_t context,
198                                          int expected_code)
199 {
200   context->expectedExitCode = expected_code;
201 }
202
203 /*
204  * Return true if the output checking mode is enabled for this
205  * test case context. Otherwise the functions returns false.
206  */
207 bool TestCaseContext_isOutputCheckingEnabled(TestCaseContext_t context)
208 {
209   return context->isOutputCheckingEnabled;
210 }
211
212 void TestCaseContext_enableExitCodeChecking(TestCaseContext_t context)
213 {
214   context->exitCodeCheckingEnabled = true;
215 }
216
217 void TestCaseContext_disableExitCodeChecking(TestCaseContext_t context)
218 {
219   context->exitCodeCheckingEnabled = false;
220 }
221
222 void TestCaseContext_setCommandLine(TestCaseContext_t context,
223                                     char *cmdLine)
224 {
225   Buffer_append(context->commandLineBuffer, cmdLine);
226 }
227
228
229 /*
230  * Append a child output to check in the 
231  * test case context.
232  */
233 void TestCaseContext_appendExpectedOutput(TestCaseContext_t context,
234                                           char *expected_output)
235 {
236   Buffer_append(context->expectedOutputBuffer, expected_output);
237 }
238
239 /*
240  * Append a child output to check in the 
241  * test case context.
242  */
243 void TestCaseContext_appendChildInput(TestCaseContext_t context,
244                                       char *input)
245 {
246   Buffer_append(context->inputBuffer, input);
247 }
248
249 /*
250  * Set the name of the test case name.
251  */
252 void TestCaseContext_setName(TestCaseContext_t context, char *name)
253 {
254   size_t size;
255
256   if (NULL != context->name) {
257     free(context->name);
258   }
259
260   context->name = strdup(name);
261   size = strlen(name);
262
263   while ((context->name[size - 1] == '\n')
264          || (context->name[size - 1] == '\r')) {
265     context->name[size - 1] = '\0';
266
267     if (size)
268       size--;
269   }
270
271   /*context->name[strlen(context->name) - 1] ='\0'; */
272 }
273
274 /* 
275  * Clear the s_TestCaseContext referenced by context.
276  */
277 void TestCaseContext_clear(TestCaseContext_t context)
278 {
279   if (!Buffer_empty(context->inputBuffer))
280     Buffer_clear(context->inputBuffer);
281
282   if (!Buffer_empty(context->outputBuffer))
283     Buffer_clear(context->outputBuffer);
284
285   if (!Buffer_empty(context->expectedOutputBuffer))
286     Buffer_clear(context->expectedOutputBuffer);
287
288   if (!Buffer_empty(context->commandLineBuffer))
289     Buffer_clear(context->commandLineBuffer);
290
291   if (NULL == context->name) {
292     free(context->name);
293     context->name = NULL;
294   }
295
296   context->isOutputCheckingEnabled = DEFAULT_OUTPUT_CHECKING_MODE;
297   context->isPostOutputCheckingEnabled = DEFAULT_POST_OUTPUT_CHECKING_MODE;
298   context->timeoutValue = DEFAULT_TIMEOUT_VALUE;
299   context->expectedExitCode = INVALID_EXIT_CODE;
300   context->exitCode = INVALID_EXIT_CODE;
301 }