/simpletypesystem/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/simpletypesystem/trunk
32 by Gustav Hartvigsson
* Added some compile options to the root CMakeLists.txt
1
#pragma once
142 by Gustav Hartvigsson
* fiddeled with the test macros to make them easyer to read
2
32 by Gustav Hartvigsson
* Added some compile options to the root CMakeLists.txt
3
#include <stdio.h>
4
#include <time.h>
39 by Gustav Hartvigsson
* Added "check" target for testing.
5
#include <stdatomic.h>
6
32 by Gustav Hartvigsson
* Added some compile options to the root CMakeLists.txt
7
#ifndef RESET
33 by Gustav Hartvigsson
* made test_macros.h a lil' bit more portable
8
/* Colour definitions for console prints */
9
#define RESET   "\033[0m"
10
#define BLACK   "\033[30m"      /* Black */
11
#define RED     "\033[31m"      /* Red */
12
#define GREEN   "\033[32m"      /* Green */
13
#define YELLOW  "\033[33m"      /* Yellow */
14
#define BLUE    "\033[34m"      /* Blue */
15
#define MAGENTA "\033[35m"      /* Magenta */
16
#define CYAN    "\033[36m"      /* Cyan */
17
#define WHITE   "\033[37m"      /* White */
18
#define BOLDBLACK   "\033[1m\033[30m"      /* Bold Black */
19
#define BOLDRED     "\033[1m\033[31m"      /* Bold Red */
20
#define BOLDGREEN   "\033[1m\033[32m"      /* Bold Green */
21
#define BOLDYELLOW  "\033[1m\033[33m"      /* Bold Yellow */
22
#define BOLDBLUE    "\033[1m\033[34m"      /* Bold Blue */
23
#define BOLDMAGENTA "\033[1m\033[35m"      /* Bold Magenta */
24
#define BOLDCYAN    "\033[1m\033[36m"      /* Bold Cyan */
25
#define BOLDWHITE   "\033[1m\033[37m"      /* Bold White */
26
#endif
27
28
/*
142 by Gustav Hartvigsson
* fiddeled with the test macros to make them easyer to read
29
 * http://stackoverflow.com/a/1551641
30
 */
31
static inline
32
char *
33
test_current_time () {
34
   char * ret_val = malloc (50);
35
   char fmt[50];
36
   time_t t = time (NULL);
37
   struct timeval tv;
38
   gettimeofday (&tv, NULL);
39
   strftime (fmt, 50,"%Y-%m-%dT%H:%M:%S.%%i%z", localtime (&t));
146 by Gustav Hartvigsson
* Reorderd includes in SimpleTypeSystem.h
40
   snprintf(ret_val, 50, fmt, tv.tv_usec);
142 by Gustav Hartvigsson
* fiddeled with the test macros to make them easyer to read
41
   return ret_val;
42
}
43
44
39 by Gustav Hartvigsson
* Added "check" target for testing.
45
/******************************************************************************/
46
47
#define setup_suite(sn)                                         \
48
  char * suit_name = sn;                                        \
49
  char * t_str = test_current_time ();                           \
142 by Gustav Hartvigsson
* fiddeled with the test macros to make them easyer to read
50
  fprintf (stdout, MAGENTA "[%s]\n[STARTING TEST SUITE] %s\n"    \
51
  RESET, t_str, suit_name);                                     \
39 by Gustav Hartvigsson
* Added "check" target for testing.
52
  s_free (t_str);                                               \
142 by Gustav Hartvigsson
* fiddeled with the test macros to make them easyer to read
53
  unsigned int total_fails = 0;                                 \
39 by Gustav Hartvigsson
* Added "check" target for testing.
54
  unsigned int test_suites_failed = 0;                          \
55
  unsigned int tmp_val = 0;
56
34 by Gustav Hartvigsson
* Finnished test suite
57
#define end_suite()                                            \
38 by Gustav Hartvigsson
* Formated the macros a bit
58
  if (test_suites_failed > 0){                                 \
59
    fprintf (stderr, RED "[TEST SUITE FAILED]\n"               \
60
    " Number of failed suits: %d\n"                            \
61
    " Number of tests failed (total): %d\n" RESET,             \
62
    test_suites_failed, total_fails);                          \
63
  } else {                                                     \
64
    fprintf (stdout, GREEN "[TEST SUITE PASSED] %s\n" RESET,   \
39 by Gustav Hartvigsson
* Added "check" target for testing.
65
      suit_name);                                              \
66
  }                                                            \
38 by Gustav Hartvigsson
* Formated the macros a bit
67
  return total_fails + test_suites_failed;
34 by Gustav Hartvigsson
* Finnished test suite
68
69
/******************************************************************************/
39 by Gustav Hartvigsson
* Added "check" target for testing.
70
71
#define setup_unit()            \
72
  unsigned int unit_retval = 0;
73
74
#define end_unit()    \
75
  return unit_retval;
76
77
#define test_case(cond, p, ...)                                     \
47 by Gustav Hartvigsson
* Added a few skeletal functions to Callback.h
78
    if (cond) {                                                     \
79
      fprintf (stdout, GREEN "[PASS] " p " \n" RESET,               \
80
               ##__VA_ARGS__);                                      \
81
    } else {                                                        \
82
      fprintf (stderr, RED "[FAILED][%s:%d, %s] " p " \n" RESET,    \
83
      __FILE__, __LINE__, __func__ , ##__VA_ARGS__);                \
84
      unit_retval++;                                                \
85
    }
32 by Gustav Hartvigsson
* Added some compile options to the root CMakeLists.txt
86
87
39 by Gustav Hartvigsson
* Added "check" target for testing.
88
/******************************************************************************/
89
90
91
#define test_unit(fun, name){                                             \
142 by Gustav Hartvigsson
* fiddeled with the test macros to make them easyer to read
92
  t_str = test_current_time ();                                           \
93
  fprintf (stdout, BLUE "[%s]\n[STARTING TEST UNIT] %s\n" RESET, t_str, name);\
94
  tmp_val = fun ();                                                       \
95
  free (t_str); t_str = test_current_time ();                             \
96
  if (tmp_val > 0) {                                                      \
97
    total_fails =+ tmp_val;                                               \
98
    test_suites_failed++;                                                 \
99
    fprintf (stderr, RED "[%s]\n[TEST UNIT FAIL] %s\n" RESET,             \
100
     t_str, name);                                                        \
101
  } else {                                                                \
102
    fprintf (stdout, GREEN "[%s]\n[TEST UNIT PASS] %s\n" RESET,           \
103
      t_str, name);                                                       \
104
  }                                                                       \
105
  free (t_str);                                                           \
106
}
107
34 by Gustav Hartvigsson
* Finnished test suite
108