1 /*
2  * Copyright (C) 2016 Jakub Kruszona-Zawadzki, Core Technology Sp. z o.o.
3  *
4  * This file is part of MooseFS.
5  *
6  * MooseFS is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, version 2 (only).
9  *
10  * MooseFS 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
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with MooseFS; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA
18  * or visit http://www.gnu.org/licenses/gpl-2.0.html
19  */
20 
21 #ifndef _MFSTEST_H_
22 #define _MFSTEST_H_
23 
24 #include <inttypes.h>
25 
26 #define STR_AUX(x) #x
27 #define STR(x) STR_AUX(x)
28 
29 static uint32_t mfstest_passed;
30 static uint32_t mfstest_failed;
31 static uint8_t mfstest_result;
32 
33 #define mfstest_assert(type,pri,arg1,op,arg2) { \
34 	type _arg1,_arg2; \
35 	_arg1 = (arg1); \
36 	_arg2 = (arg2); \
37 	if (_arg1 op _arg2) { \
38 		mfstest_passed++; \
39 	} else { \
40 		mfstest_failed++; \
41 		mfstest_result = 1; \
42 		printf("%s","Assertion '" STR(arg1) "" STR(op) "" STR(arg2) "' failed: \n"); \
43 		printf("'%s' == %" pri ", '%s' == %" pri "\n",STR(arg1),_arg1,STR(arg2),_arg2); \
44 	} \
45 }
46 
47 #define mfstest_start(name) { \
48 	printf("%s","Starting test: " STR(name) "\n"); \
49 	mfstest_passed = mfstest_failed = 0; \
50 }
51 
52 #define mfstest_end() printf("%u%%: Checks: %" PRIu32 ", Failures: %" PRIu32 "\n",100*mfstest_passed/(mfstest_passed+mfstest_failed),(mfstest_passed+mfstest_failed),mfstest_failed)
53 
54 #define PRIDOUBLE ".10lf"
55 
56 #define mfstest_assert_uint8(x,op,y) mfstest_assert(uint8_t,PRIu8,x,op,y)
57 #define mfstest_assert_uint16(x,op,y) mfstest_assert(uint16_t,PRIu16,x,op,y)
58 #define mfstest_assert_uint32(x,op,y) mfstest_assert(uint32_t,PRIu32,x,op,y)
59 #define mfstest_assert_uint64(x,op,y) mfstest_assert(uint64_t,PRIu64,x,op,y)
60 #define mfstest_assert_int8(x,op,y) mfstest_assert(int8_t,PRId8,x,op,y)
61 #define mfstest_assert_int16(x,op,y) mfstest_assert(int16_t,PRId16,x,op,y)
62 #define mfstest_assert_int32(x,op,y) mfstest_assert(int32_t,PRId32,x,op,y)
63 #define mfstest_assert_int64(x,op,y) mfstest_assert(int64_t,PRId64,x,op,y)
64 #define mfstest_assert_double(x,op,y) mfstest_assert(double,PRIDOUBLE,x,op,y)
65 
66 #define mfstest_assert_uint8_eq(x,y) mfstest_assert_uint8(x,==,y)
67 #define mfstest_assert_uint16_eq(x,y) mfstest_assert_uint16(x,==,y)
68 #define mfstest_assert_uint32_eq(x,y) mfstest_assert_uint32(x,==,y)
69 #define mfstest_assert_uint64_eq(x,y) mfstest_assert_uint64(x,==,y)
70 #define mfstest_assert_int8_eq(x,y) mfstest_assert_int8(x,==,y)
71 #define mfstest_assert_int16_eq(x,y) mfstest_assert_int16(x,==,y)
72 #define mfstest_assert_int32_eq(x,y) mfstest_assert_int32(x,==,y)
73 #define mfstest_assert_int64_eq(x,y) mfstest_assert_int64(x,==,y)
74 #define mfstest_assert_double_eq(x,y) mfstest_assert_double(x,==,y)
75 
76 #define mfstest_assert_uint8_ne(x,y) mfstest_assert_uint8(x,!=,y)
77 #define mfstest_assert_uint16_ne(x,y) mfstest_assert_uint16(x,!=,y)
78 #define mfstest_assert_uint32_ne(x,y) mfstest_assert_uint32(x,!=,y)
79 #define mfstest_assert_uint64_ne(x,y) mfstest_assert_uint64(x,!=,y)
80 #define mfstest_assert_int8_ne(x,y) mfstest_assert_int8(x,!=,y)
81 #define mfstest_assert_int16_ne(x,y) mfstest_assert_int16(x,!=,y)
82 #define mfstest_assert_int32_ne(x,y) mfstest_assert_int32(x,!=,y)
83 #define mfstest_assert_int64_ne(x,y) mfstest_assert_int64(x,!=,y)
84 #define mfstest_assert_double_ne(x,y) mfstest_assert_double(x,!=,y)
85 
86 #define mfstest_assert_uint8_lt(x,y) mfstest_assert_uint8(x,<,y)
87 #define mfstest_assert_uint16_lt(x,y) mfstest_assert_uint16(x,<,y)
88 #define mfstest_assert_uint32_lt(x,y) mfstest_assert_uint32(x,<,y)
89 #define mfstest_assert_uint64_lt(x,y) mfstest_assert_uint64(x,<,y)
90 #define mfstest_assert_int8_lt(x,y) mfstest_assert_int8(x,<,y)
91 #define mfstest_assert_int16_lt(x,y) mfstest_assert_int16(x,<,y)
92 #define mfstest_assert_int32_lt(x,y) mfstest_assert_int32(x,<,y)
93 #define mfstest_assert_int64_lt(x,y) mfstest_assert_int64(x,<,y)
94 #define mfstest_assert_double_lt(x,y) mfstest_assert_double(x,<,y)
95 
96 #define mfstest_assert_uint8_le(x,y) mfstest_assert_uint8(x,<=,y)
97 #define mfstest_assert_uint16_le(x,y) mfstest_assert_uint16(x,<=,y)
98 #define mfstest_assert_uint32_le(x,y) mfstest_assert_uint32(x,<=,y)
99 #define mfstest_assert_uint64_le(x,y) mfstest_assert_uint64(x,<=,y)
100 #define mfstest_assert_int8_le(x,y) mfstest_assert_int8(x,<=,y)
101 #define mfstest_assert_int16_le(x,y) mfstest_assert_int16(x,<=,y)
102 #define mfstest_assert_int32_le(x,y) mfstest_assert_int32(x,<=,y)
103 #define mfstest_assert_int64_le(x,y) mfstest_assert_int64(x,<=,y)
104 #define mfstest_assert_double_le(x,y) mfstest_assert_double(x,<=,y)
105 
106 #define mfstest_assert_uint8_gt(x,y) mfstest_assert_uint8(x,>,y)
107 #define mfstest_assert_uint16_gt(x,y) mfstest_assert_uint16(x,>,y)
108 #define mfstest_assert_uint32_gt(x,y) mfstest_assert_uint32(x,>,y)
109 #define mfstest_assert_uint64_gt(x,y) mfstest_assert_uint64(x,>,y)
110 #define mfstest_assert_int8_gt(x,y) mfstest_assert_int8(x,>,y)
111 #define mfstest_assert_int16_gt(x,y) mfstest_assert_int16(x,>,y)
112 #define mfstest_assert_int32_gt(x,y) mfstest_assert_int32(x,>,y)
113 #define mfstest_assert_int64_gt(x,y) mfstest_assert_int64(x,>,y)
114 #define mfstest_assert_double_gt(x,y) mfstest_assert_double(x,>,y)
115 
116 #define mfstest_assert_uint8_ge(x,y) mfstest_assert_uint8(x,>=,y)
117 #define mfstest_assert_uint16_ge(x,y) mfstest_assert_uint16(x,>=,y)
118 #define mfstest_assert_uint32_ge(x,y) mfstest_assert_uint32(x,>=,y)
119 #define mfstest_assert_uint64_ge(x,y) mfstest_assert_uint64(x,>=,y)
120 #define mfstest_assert_int8_ge(x,y) mfstest_assert_int8(x,>=,y)
121 #define mfstest_assert_int16_ge(x,y) mfstest_assert_int16(x,>=,y)
122 #define mfstest_assert_int32_ge(x,y) mfstest_assert_int32(x,>=,y)
123 #define mfstest_assert_int64_ge(x,y) mfstest_assert_int64(x,>=,y)
124 #define mfstest_assert_double_ge(x,y) mfstest_assert_double(x,>=,y)
125 
126 #define mfstest_init() mfstest_result=0
127 
128 #define mfstest_return() return mfstest_result
129 
130 #endif
131