xref: /kvm-unit-tests/lib/libcflat.h (revision d3aacb4f57d05f74f2030dbe12e7dfd6aa1b273d)
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License, version 2, as
4  * published by the Free Software Foundation.
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9  * GNU General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public License
12  * along with this program; if not, write to the Free Software
13  * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
14  *
15  * Copyright IBM Corp. 2008
16  *
17  * Authors: Hollis Blanchard <hollisb@us.ibm.com>
18  */
19 
20 #ifndef __LIBCFLAT_H
21 #define __LIBCFLAT_H
22 
23 #include <stdarg.h>
24 #include <stddef.h>
25 #include <stdint.h>
26 #include <string.h>
27 
28 #define __unused __attribute__((__unused__))
29 
30 #define xstr(s) xxstr(s)
31 #define xxstr(s) #s
32 
33 typedef uint8_t		u8;
34 typedef int8_t		s8;
35 typedef uint16_t	u16;
36 typedef int16_t		s16;
37 typedef uint32_t	u32;
38 typedef int32_t		s32;
39 typedef uint64_t	u64;
40 typedef int64_t		s64;
41 typedef unsigned long	ulong;
42 
43 typedef _Bool		bool;
44 #define false 0
45 #define true  1
46 
47 extern void puts(const char *s);
48 extern void exit(int code);
49 extern void abort(void);
50 
51 extern int printf(const char *fmt, ...);
52 extern int snprintf(char *buf, int size, const char *fmt, ...);
53 extern int vsnprintf(char *buf, int size, const char *fmt, va_list va);
54 extern long atol(const char *ptr);
55 
56 void report(const char *msg_fmt, bool pass, ...);
57 void report_xfail(const char *msg_fmt, bool xfail, bool pass, ...);
58 int report_summary(void);
59 
60 #define ARRAY_SIZE(_a) (sizeof(_a)/sizeof((_a)[0]))
61 
62 #define container_of(ptr, type, member) ({				\
63 	const typeof( ((type *)0)->member ) *__mptr = (ptr);		\
64 	(type *)( (char *)__mptr - offsetof(type,member) );})
65 
66 #define assert(cond)							\
67 do {									\
68 	if (!(cond))							\
69 		printf("%s:%d: assert failed\n", __FILE__, __LINE__),	\
70 		abort();						\
71 } while (0)
72 
73 #endif
74