SuperSCS  1.3.2
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
unit_test_util.h
Go to the documentation of this file.
1 /*
2  * The MIT License (MIT)
3  *
4  * Copyright (c) 2017 Pantelis Sopasakis (https://alphaville.github.io),
5  * Krina Menounou (https://www.linkedin.com/in/krinamenounou),
6  * Panagiotis Patrinos (http://homes.esat.kuleuven.be/~ppatrino)
7  * Copyright (c) 2012 Brendan O'Donoghue (bodonoghue85@gmail.com)
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a copy
10  * of this software and associated documentation files (the "Software"), to deal
11  * in the Software without restriction, including without limitation the rights
12  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13  * copies of the Software, and to permit persons to whom the Software is
14  * furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be included in all
17  * copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25  * SOFTWARE.
26  *
27  */
28 
29 #ifndef UNITTESTS_H
30 #define UNITTESTS_H
31 
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include "glbopts.h"
36 #include <math.h>
37 #include "linAlg.h"
38 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 #ifdef bool
44 #undef bool
45 #endif
46 
47 #ifdef true
48 #undef true
49 #endif
50 
51 #ifdef false
52 #undef false
53 #endif
54 
55  typedef int bool;
56 #define true 1
57 #define false 0
58 
59 
61 
62 #define TEST_SUCCESS 0
63 #define TEST_FAILURE 1
64 #define MESSAGE_OK "OK"
65 #define TEST_PASS_FLAG "\x1B[92m[PASS]\x1B[39m "
66 #define TEST_FAIL_FLAG "\x1B[31m<FAIL>\x1B[39m "
67 #define TEST_MESSAGE_BUFF_SIZE 500
68 #define TEST_ERROR_MESSAGE_SIZE 100
69 
72 #define FAIL_WITH_MESSAGE(str, message)\
73  *str = (char*)(message);\
74  return TEST_FAILURE
75 
76 
77 #define ASSERT_TRUE_OR_FAIL(p, str, message)\
78  number_of_assertions++;\
79  if (!(p)) { \
80  FAIL_WITH_MESSAGE((str), (message));\
81  }
82 
86 #define ASSERT_EQUAL_INT_OR_FAIL(val, expected, str, message)\
87  number_of_assertions++;\
88  if (!scs_assert_equals_int((val),(expected))) { \
89  {\
90  char buff[TEST_MESSAGE_BUFF_SIZE];\
91  char error_msg[TEST_ERROR_MESSAGE_SIZE];\
92  sprintf(error_msg, "\n\tExpected: %d, Actual %d", expected, val);\
93  strncpy(buff, message, TEST_ERROR_MESSAGE_SIZE);\
94  strncat(buff, error_msg, TEST_ERROR_MESSAGE_SIZE);\
95  FAIL_WITH_MESSAGE((str), (buff)); \
96  }\
97  }
98 
102 #define ASSERT_EQUAL_FLOAT_OR_FAIL(val, expected, tol, str, message)\
103  number_of_assertions++;\
104  if (!scs_assert_equals_float((val), (expected), (tol))) {\
105  char buff[TEST_MESSAGE_BUFF_SIZE];\
106  char error_msg[TEST_ERROR_MESSAGE_SIZE];\
107  sprintf(error_msg, "\n\tExpected: %g, Actual %g (tol=%g)", expected, val, tol);\
108  strncpy(buff, message, TEST_ERROR_MESSAGE_SIZE);\
109  strncat(buff, error_msg, TEST_ERROR_MESSAGE_SIZE);\
110  FAIL_WITH_MESSAGE((str), (buff)); \
111  }
112 
116 #define ASSERT_EQUAL_ARRAY_OR_FAIL(val,expected,len,tol,str,message)\
117  number_of_assertions++;\
118  if (!scs_assert_equals_array((val),(expected),(len),(tol))){\
119  FAIL_WITH_MESSAGE((str), (message));\
120  }
121 
125 #define ASSERT_EQUAL_ARRAY_INT_OR_FAIL(val,expected,len,str,message)\
126  number_of_assertions++;\
127  if (!scs_assert_equals_array_int((val),(expected),(len))){\
128  FAIL_WITH_MESSAGE((str), (message));\
129  }
130 
134 #define SUCCEED(str)\
135  *str = (char*) MESSAGE_OK;\
136  return TEST_SUCCESS
137 
147  typedef bool (*unitTest_t)(char**);
148 
149 
156  bool scs_test(const unitTest_t ut, const char* name);
157 
164  bool scs_assert_equals_int(const scs_int a, const scs_int b);
165 
173  bool scs_assert_equals_float(const scs_float a, const scs_float b, const scs_float tol);
174 
186  const scs_float * a,
187  const scs_float * b,
188  scs_int n,
189  const scs_float tol);
190 
201  const scs_int * a,
202  const scs_int * b,
203  scs_int n);
204 
205 
206 #ifdef __cplusplus
207 }
208 #endif
209 
210 #endif /* UNITTESTS_H */
bool scs_test(const unitTest_t ut, const char *name)
bool(* unitTest_t)(char **)
Definition: unit_test_util.h:147
bool scs_assert_equals_array(const scs_float *a, const scs_float *b, scs_int n, const scs_float tol)
bool scs_assert_equals_float(const scs_float a, const scs_float b, const scs_float tol)
bool scs_assert_equals_array_int(const scs_int *a, const scs_int *b, scs_int n)
int scs_int
Definition: glbopts.h:96
int bool
Definition: unit_test_util.h:55
int number_of_assertions
Definition: unit_test_util.h:60
double scs_float
Definition: glbopts.h:100
bool scs_assert_equals_int(const scs_int a, const scs_int b)