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