Imported Upstream version 0.14.0
[platform/upstream/check.git] / src / check.c
1 /*
2  * Check: a unit test framework for C
3  * Copyright (C) 2001, 2002 Arien Malec
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
18  * MA 02110-1301, USA.
19  */
20
21 #include "../lib/libcompat.h"
22
23 #include <string.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <stdarg.h>
27 #include <math.h>
28
29 #if defined(HAVE_FORK) && HAVE_FORK==1
30 #include <unistd.h>
31 #endif /* HAVE_FORK */
32
33 #include "check.h"
34 #include "check_error.h"
35 #include "check_list.h"
36 #include "check_impl.h"
37 #include "check_msg.h"
38
39 #ifndef DEFAULT_TIMEOUT
40 #define DEFAULT_TIMEOUT 4
41 #endif
42
43 /*
44  * When a process exits either normally, with exit(), or
45  * by an uncaught signal, The lower 0x377 bits are passed
46  * to the parent. Of those, only the lower 8 bits are
47  * returned by the WEXITSTATUS() macro.
48  */
49 #define WEXITSTATUS_MASK 0xFF
50
51 int check_major_version = CHECK_MAJOR_VERSION;
52 int check_minor_version = CHECK_MINOR_VERSION;
53 int check_micro_version = CHECK_MICRO_VERSION;
54
55 const char* current_test_name = NULL;
56
57 static int non_pass(int val);
58 static Fixture *fixture_create(SFun fun, int ischecked);
59 static void tcase_add_fixture(TCase * tc, SFun setup, SFun teardown,
60                               int ischecked);
61 static void tr_init(TestResult * tr);
62 static void suite_free(Suite * s);
63 static void tcase_free(TCase * tc);
64
65 Suite *suite_create(const char *name)
66 {
67     Suite *s;
68
69     s = (Suite *)emalloc(sizeof(Suite)); /* freed in suite_free */
70     if(name == NULL)
71         s->name = "";
72     else
73         s->name = name;
74     s->tclst = check_list_create();
75     return s;
76 }
77
78 int suite_tcase(Suite * s, const char *tcname)
79 {
80     List *l;
81
82     if(s == NULL)
83         return 0;
84
85     l = s->tclst;
86     for(check_list_front(l); !check_list_at_end(l); check_list_advance(l))
87     {
88         TCase *tc = (TCase *)check_list_val(l);
89         if(strcmp(tcname, tc->name) == 0)
90             return 1;
91     }
92
93     return 0;
94 }
95
96 static void suite_free(Suite * s)
97 {
98     List *l;
99
100     if(s == NULL)
101         return;
102     l = s->tclst;
103     for(check_list_front(l); !check_list_at_end(l); check_list_advance(l))
104     {
105         tcase_free((TCase *)check_list_val(l));
106     }
107     check_list_free(s->tclst);
108     free(s);
109 }
110
111
112 TCase *tcase_create(const char *name)
113 {
114     char *env;
115     double timeout_sec = DEFAULT_TIMEOUT;
116
117     TCase *tc = (TCase *)emalloc(sizeof(TCase)); /*freed in tcase_free */
118
119     if(name == NULL)
120         tc->name = "";
121     else
122         tc->name = name;
123
124     env = getenv("CK_DEFAULT_TIMEOUT");
125     if(env != NULL)
126     {
127         char *endptr = NULL;
128         double tmp = strtod(env, &endptr);
129
130         if(tmp >= 0 && endptr != env && (*endptr) == '\0')
131         {
132             timeout_sec = tmp;
133         }
134     }
135
136     env = getenv("CK_TIMEOUT_MULTIPLIER");
137     if(env != NULL)
138     {
139         char *endptr = NULL;
140         double tmp = strtod(env, &endptr);
141
142         if(tmp >= 0 && endptr != env && (*endptr) == '\0')
143         {
144             timeout_sec = timeout_sec * tmp;
145         }
146     }
147
148     tc->timeout.tv_sec = (time_t) floor(timeout_sec);
149     tc->timeout.tv_nsec =
150         (long)((timeout_sec -
151                 floor(timeout_sec)) * (double)NANOS_PER_SECONDS);
152
153     tc->tflst = check_list_create();
154     tc->unch_sflst = check_list_create();
155     tc->ch_sflst = check_list_create();
156     tc->unch_tflst = check_list_create();
157     tc->ch_tflst = check_list_create();
158     tc->tags = check_list_create();
159
160     return tc;
161 }
162
163 /*
164  * Helper function to create a list of tags from
165  * a space separated string.
166  */
167 List *tag_string_to_list(const char *tags_string)
168 {
169     List *list;
170     char *tags;
171     char *tag;
172
173     list = check_list_create();
174
175     if (NULL == tags_string)
176     {
177         return list;
178     }
179
180     tags = strdup(tags_string);
181     tag = strtok(tags, " ");
182     while (tag)
183     {
184         check_list_add_end(list, strdup(tag));
185         tag = strtok(NULL, " ");
186     }
187     free(tags);
188     return list;
189 }
190
191 void tcase_set_tags(TCase * tc, const char *tags_orig)
192 {
193     /* replace any pre-existing list */
194     if (tc->tags)
195     {
196         check_list_apply(tc->tags, free);
197         check_list_free(tc->tags);
198     }
199     tc->tags = tag_string_to_list(tags_orig);
200 }
201
202 static void tcase_free(TCase * tc)
203 {
204     check_list_apply(tc->tflst, free);
205     check_list_apply(tc->unch_sflst, free);
206     check_list_apply(tc->ch_sflst, free);
207     check_list_apply(tc->unch_tflst, free);
208     check_list_apply(tc->ch_tflst, free);
209     check_list_apply(tc->tags, free);
210     check_list_free(tc->tflst);
211     check_list_free(tc->unch_sflst);
212     check_list_free(tc->ch_sflst);
213     check_list_free(tc->unch_tflst);
214     check_list_free(tc->ch_tflst);
215     check_list_free(tc->tags);
216     free(tc);
217 }
218
219 unsigned int tcase_matching_tag(TCase *tc, List *check_for)
220 {
221
222     if (NULL == check_for)
223     {
224         return 0;
225     }
226
227     for(check_list_front(check_for); !check_list_at_end(check_for);
228         check_list_advance(check_for))
229     {
230         for(check_list_front(tc->tags); !check_list_at_end(tc->tags);
231             check_list_advance(tc->tags))
232         {
233             if (0 == strcmp((const char *)check_list_val(tc->tags),
234                     (const char *)check_list_val(check_for)))
235             {
236             return 1;
237             }
238         }
239     }
240     return 0;
241 }
242
243 void suite_add_tcase(Suite * s, TCase * tc)
244 {
245     if(s == NULL || tc == NULL || check_list_contains(s->tclst, tc))
246     {
247         return;
248     }
249
250     check_list_add_end(s->tclst, tc);
251 }
252
253 void _tcase_add_test(TCase * tc, const TTest * ttest,
254                      int _signal, int allowed_exit_value,
255                      int start, int end)
256 {
257     TF *tf;
258
259     if(tc == NULL || ttest == NULL)
260         return;
261     tf = (TF *)emalloc(sizeof(TF));   /* freed in tcase_free */
262     tf->ttest = ttest;
263     tf->loop_start = start;
264     tf->loop_end = end;
265     tf->signal = _signal;       /* 0 means no signal expected */
266     tf->allowed_exit_value =
267       (WEXITSTATUS_MASK & allowed_exit_value);   /* 0 is default successful exit */
268     check_list_add_end(tc->tflst, tf);
269 }
270
271 static Fixture *fixture_create(SFun fun, int ischecked)
272 {
273     Fixture *f;
274
275     f = (Fixture *)emalloc(sizeof(Fixture));
276     f->fun = fun;
277     f->ischecked = ischecked;
278
279     return f;
280 }
281
282 void tcase_add_unchecked_fixture(TCase * tc, SFun setup, SFun teardown)
283 {
284     tcase_add_fixture(tc, setup, teardown, 0);
285 }
286
287 void tcase_add_checked_fixture(TCase * tc, SFun setup, SFun teardown)
288 {
289     tcase_add_fixture(tc, setup, teardown, 1);
290 }
291
292 static void tcase_add_fixture(TCase * tc, SFun setup, SFun teardown,
293                               int ischecked)
294 {
295     if(setup)
296     {
297         if(ischecked)
298             check_list_add_end(tc->ch_sflst,
299                                fixture_create(setup, ischecked));
300         else
301             check_list_add_end(tc->unch_sflst,
302                                fixture_create(setup, ischecked));
303     }
304
305     /* Add teardowns at front so they are run in reverse order. */
306     if(teardown)
307     {
308         if(ischecked)
309             check_list_add_front(tc->ch_tflst,
310                                  fixture_create(teardown, ischecked));
311         else
312             check_list_add_front(tc->unch_tflst,
313                                  fixture_create(teardown, ischecked));
314     }
315 }
316
317 void tcase_set_timeout(TCase * tc, double timeout)
318 {
319 #if defined(HAVE_FORK)
320     if(timeout >= 0)
321     {
322         char *env = getenv("CK_TIMEOUT_MULTIPLIER");
323
324         if(env != NULL)
325         {
326             char *endptr = NULL;
327             double tmp = strtod(env, &endptr);
328
329             if(tmp >= 0 && endptr != env && (*endptr) == '\0')
330             {
331                 timeout = timeout * tmp;
332             }
333         }
334
335         tc->timeout.tv_sec = (time_t) floor(timeout);
336         tc->timeout.tv_nsec =
337             (long)((timeout - floor(timeout)) * (double)NANOS_PER_SECONDS);
338     }
339 #else
340     (void)tc;
341     (void)timeout;
342     /* Ignoring, as Check is not compiled with fork support. */
343 #endif /* HAVE_FORK */
344 }
345
346 void tcase_fn_start(const char *fname, const char *file,
347                     int line)
348 {
349     send_ctx_info(CK_CTX_TEST);
350     send_loc_info(file, line);
351
352     current_test_name = fname;
353 }
354
355 const char* tcase_name(void)
356 {
357     return current_test_name;
358 }
359
360 void _mark_point(const char *file, int line)
361 {
362     send_loc_info(file, line);
363 }
364
365 void _ck_assert_failed(const char *file, int line, const char *expr, ...)
366 {
367     const char *msg;
368     va_list ap;
369     char buf[BUFSIZ];
370     const char *to_send;
371
372     send_loc_info(file, line);
373
374     va_start(ap, expr);
375     msg = (const char *)va_arg(ap, char *);
376
377     /*
378      * If a message was passed, format it with vsnprintf.
379      * Otherwise, print the expression as is.
380      */
381     if(msg != NULL)
382     {
383         vsnprintf(buf, BUFSIZ, msg, ap);
384         to_send = buf;
385     }
386     else
387     {
388         to_send = expr;
389     }
390
391     va_end(ap);
392     send_failure_info(to_send);
393     if(cur_fork_status() == CK_FORK)
394     {
395 #if defined(HAVE_FORK) && HAVE_FORK==1
396         _exit(1);
397 #endif /* HAVE_FORK */
398     }
399     else
400     {
401         longjmp(error_jmp_buffer, 1);
402     }
403 }
404
405 SRunner *srunner_create(Suite * s)
406 {
407     SRunner *sr = (SRunner *)emalloc(sizeof(SRunner));     /* freed in srunner_free */
408
409     sr->slst = check_list_create();
410     if(s != NULL)
411         check_list_add_end(sr->slst, s);
412     sr->stats = (TestStats *)emalloc(sizeof(TestStats));     /* freed in srunner_free */
413     sr->stats->n_checked = sr->stats->n_failed = sr->stats->n_errors = 0;
414     sr->resultlst = check_list_create();
415     sr->log_fname = NULL;
416     sr->xml_fname = NULL;
417     sr->tap_fname = NULL;
418     sr->loglst = NULL;
419
420 #if defined(HAVE_FORK)
421     sr->fstat = CK_FORK_GETENV;
422 #else
423     /*
424      * Overriding the default of running tests in fork mode,
425      * as this system does not have fork()
426      */
427     sr->fstat = CK_NOFORK;
428 #endif /* HAVE_FORK */
429
430     return sr;
431 }
432
433 void srunner_add_suite(SRunner * sr, Suite * s)
434 {
435     if(s == NULL)
436         return;
437
438     check_list_add_end(sr->slst, s);
439 }
440
441 void srunner_free(SRunner * sr)
442 {
443     List *l;
444
445     if(sr == NULL)
446         return;
447
448     free(sr->stats);
449     l = sr->slst;
450     for(check_list_front(l); !check_list_at_end(l); check_list_advance(l))
451     {
452         suite_free((Suite *)check_list_val(l));
453     }
454     check_list_free(sr->slst);
455
456     l = sr->resultlst;
457     for(check_list_front(l); !check_list_at_end(l); check_list_advance(l))
458     {
459         TestResult *tr = (TestResult *)check_list_val(l);
460         tr_free(tr);
461     }
462     check_list_free(sr->resultlst);
463
464     free(sr);
465 }
466
467 int srunner_ntests_failed(SRunner * sr)
468 {
469     return sr->stats->n_failed + sr->stats->n_errors;
470 }
471
472 int srunner_ntests_run(SRunner * sr)
473 {
474     return sr->stats->n_checked;
475 }
476
477 TestResult **srunner_failures(SRunner * sr)
478 {
479     int i = 0;
480     TestResult **trarray;
481     List *rlst;
482
483     trarray = (TestResult **)emalloc(sizeof(trarray[0]) * srunner_ntests_failed(sr));
484
485     rlst = sr->resultlst;
486     for(check_list_front(rlst); !check_list_at_end(rlst);
487         check_list_advance(rlst))
488     {
489         TestResult *tr = (TestResult *)check_list_val(rlst);
490
491         if(non_pass(tr->rtype))
492             trarray[i++] = tr;
493
494     }
495     return trarray;
496 }
497
498 TestResult **srunner_results(SRunner * sr)
499 {
500     int i = 0;
501     TestResult **trarray;
502     List *rlst;
503
504     trarray =(TestResult **) emalloc(sizeof(trarray[0]) * srunner_ntests_run(sr));
505
506     rlst = sr->resultlst;
507     for(check_list_front(rlst); !check_list_at_end(rlst);
508         check_list_advance(rlst))
509     {
510         trarray[i++] = (TestResult *)check_list_val(rlst);
511     }
512     return trarray;
513 }
514
515 static int non_pass(int val)
516 {
517     return val != CK_PASS;
518 }
519
520 TestResult *tr_create(void)
521 {
522     TestResult *tr;
523
524     tr = (TestResult *)emalloc(sizeof(TestResult));
525     tr_init(tr);
526     return tr;
527 }
528
529 static void tr_init(TestResult * tr)
530 {
531     tr->ctx = CK_CTX_INVALID;
532     tr->line = -1;
533     tr->rtype = CK_TEST_RESULT_INVALID;
534     tr->msg = NULL;
535     tr->file = NULL;
536     tr->tcname = NULL;
537     tr->tname = NULL;
538     tr->duration = -1;
539 }
540
541 void tr_free(TestResult * tr)
542 {
543     free(tr->file);
544     free(tr->msg);
545     free(tr);
546 }
547
548
549 const char *tr_msg(TestResult * tr)
550 {
551     return tr->msg;
552 }
553
554 int tr_lno(TestResult * tr)
555 {
556     return tr->line;
557 }
558
559 const char *tr_lfile(TestResult * tr)
560 {
561     return tr->file;
562 }
563
564 int tr_rtype(TestResult * tr)
565 {
566     return tr->rtype;
567 }
568
569 enum ck_result_ctx tr_ctx(TestResult * tr)
570 {
571     return tr->ctx;
572 }
573
574 const char *tr_tcname(TestResult * tr)
575 {
576     return tr->tcname;
577 }
578
579 static enum fork_status _fstat = CK_FORK;
580
581 void set_fork_status(enum fork_status fstat)
582 {
583     if(fstat == CK_FORK || fstat == CK_NOFORK || fstat == CK_FORK_GETENV)
584         _fstat = fstat;
585     else
586         eprintf("Bad status in set_fork_status", __FILE__, __LINE__);
587 }
588
589 enum fork_status cur_fork_status(void)
590 {
591     return _fstat;
592 }
593
594 /**
595  * Not all systems support the same clockid_t's. This call checks
596  * if the CLOCK_MONOTONIC clockid_t is valid. If so, that is returned,
597  * otherwise, CLOCK_REALTIME is returned.
598  *
599  * The clockid_t that was found to work on the first call is
600  * cached for subsequent calls.
601  */
602 clockid_t check_get_clockid()
603 {
604     static clockid_t clockid = -1;
605
606 /*
607  * Only check if we have librt available. Otherwise, the clockid
608  * will be ignored anyway, as the clock_gettime() and
609  * timer_create() functions will be re-implemented in libcompat.
610  * Worse, if librt and alarm() are unavailable, this check
611  * will result in an assert(0).
612  */
613 #ifdef HAVE_LIBRT
614     timer_t timerid;
615
616     if(timer_create(CLOCK_MONOTONIC, NULL, &timerid) == 0)
617     {
618         timer_delete(timerid);
619         clockid = CLOCK_MONOTONIC;
620     }
621     else
622     {
623         clockid = CLOCK_REALTIME;
624     }
625 #else
626     clockid = CLOCK_MONOTONIC;
627 #endif
628
629     return clockid;
630 }