/+junk/libbreezy

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/%2Bjunk/libbreezy

« back to all changes in this revision

Viewing changes to tests/test_macros.h

  • Committer: Gustav Hartvigsson
  • Date: 2021-01-13 15:54:51 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20210113155451-b8bj2e5saxiysvqc
the start of this dumb experiment

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#pragma once
 
2
 
 
3
#include <stdlib.h>
 
4
#include <stdio.h>
 
5
#include <time.h>
 
6
#include <stdatomic.h>
 
7
#include <sys/time.h>
 
8
 
 
9
#ifndef RESET
 
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
/*
 
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));
 
42
   snprintf(ret_val, 50, fmt, tv.tv_usec);
 
43
   return ret_val;
 
44
}
 
45
 
 
46
 
 
47
/******************************************************************************/
 
48
 
 
49
#define setup_suite(sn)                                         \
 
50
  char * suit_name = sn;                                        \
 
51
  char * t_str = test_current_time ();                           \
 
52
  fprintf (stdout, MAGENTA "[%s]\n[STARTING TEST SUITE] %s\n"    \
 
53
  RESET, t_str, suit_name);                                     \
 
54
  s_free (t_str);                                               \
 
55
  unsigned int total_fails = 0;                                 \
 
56
  unsigned int test_suites_failed = 0;                          \
 
57
  unsigned int tmp_val = 0;
 
58
 
 
59
#define end_suite()                                            \
 
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,   \
 
67
      suit_name);                                              \
 
68
  }                                                            \
 
69
  return total_fails + test_suites_failed;
 
70
 
 
71
/******************************************************************************/
 
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, ...)                                     \
 
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
    }
 
88
 
 
89
 
 
90
/******************************************************************************/
 
91
 
 
92
 
 
93
#define test_unit(fun, name){                                             \
 
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