Blame library/test.h

Packit Service 6d40f9
/*
Packit Service 6d40f9
 * Copyright (c) 2013, Red Hat Inc.
Packit Service 6d40f9
 *
Packit Service 6d40f9
 * Redistribution and use in source and binary forms, with or without
Packit Service 6d40f9
 * modification, are permitted provided that the following conditions
Packit Service 6d40f9
 * are met:
Packit Service 6d40f9
 *
Packit Service 6d40f9
 *     * Redistributions of source code must retain the above
Packit Service 6d40f9
 *       copyright notice, this list of conditions and the
Packit Service 6d40f9
 *       following disclaimer.
Packit Service 6d40f9
 *     * Redistributions in binary form must reproduce the
Packit Service 6d40f9
 *       above copyright notice, this list of conditions and
Packit Service 6d40f9
 *       the following disclaimer in the documentation and/or
Packit Service 6d40f9
 *       other materials provided with the distribution.
Packit Service 6d40f9
 *     * The names of contributors to this software may not be
Packit Service 6d40f9
 *       used to endorse or promote products derived from this
Packit Service 6d40f9
 *       software without specific prior written permission.
Packit Service 6d40f9
 *
Packit Service 6d40f9
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit Service 6d40f9
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit Service 6d40f9
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
Packit Service 6d40f9
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
Packit Service 6d40f9
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
Packit Service 6d40f9
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
Packit Service 6d40f9
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
Packit Service 6d40f9
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
Packit Service 6d40f9
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
Packit Service 6d40f9
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
Packit Service 6d40f9
 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
Packit Service 6d40f9
 * DAMAGE.
Packit Service 6d40f9
 *
Packit Service 6d40f9
 * Author: Stef Walter <stefw@redhat.com>
Packit Service 6d40f9
 */
Packit Service 6d40f9
Packit Service 6d40f9
#ifndef TEST_H_
Packit Service 6d40f9
#define TEST_H_
Packit Service 6d40f9
Packit Service 6d40f9
#if !defined(__cplusplus) && (__GNUC__ > 2)
Packit Service 6d40f9
#define GNUC_PRINTF(x, y) __attribute__((__format__(__printf__, x, y)))
Packit Service 6d40f9
#else
Packit Service 6d40f9
#define GNUC_PRINTF(x, y)
Packit Service 6d40f9
#endif
Packit Service 6d40f9
Packit Service 6d40f9
/* For detecting clang features */
Packit Service 6d40f9
#ifndef __has_feature
Packit Service 6d40f9
#define __has_feature(x) 0
Packit Service 6d40f9
#endif
Packit Service 6d40f9
Packit Service 6d40f9
#ifndef CLANG_ANALYZER_NORETURN
Packit Service 6d40f9
#if __has_feature(attribute_analyzer_noreturn)
Packit Service 6d40f9
#define CLANG_ANALYZER_NORETURN __attribute__((analyzer_noreturn))
Packit Service 6d40f9
#else
Packit Service 6d40f9
#define CLANG_ANALYZER_NORETURN
Packit Service 6d40f9
#endif
Packit Service 6d40f9
#endif
Packit Service 6d40f9
Packit Service 6d40f9
#ifndef TEST_SOURCE
Packit Service 6d40f9
Packit Service 6d40f9
#include <string.h>
Packit Service 6d40f9
Packit Service 6d40f9
#ifdef assert_not_reached
Packit Service 6d40f9
#undef assert_not_reached
Packit Service 6d40f9
#endif
Packit Service 6d40f9
Packit Service 6d40f9
#ifdef assert
Packit Service 6d40f9
#undef assert
Packit Service 6d40f9
#endif
Packit Service 6d40f9
Packit Service 6d40f9
#define assert(expr) \
Packit Service 6d40f9
	assert_true(expr)
Packit Service 6d40f9
#define assert_true(expr) \
Packit Service 6d40f9
	do { if (expr) ; else \
Packit Service 6d40f9
		test_fail (__FILE__, __LINE__, __FUNCTION__, "assertion failed (%s)", #expr); \
Packit Service 6d40f9
	} while (0)
Packit Service 6d40f9
#define assert_false(expr) \
Packit Service 6d40f9
	do { if (expr) \
Packit Service 6d40f9
		test_fail (__FILE__, __LINE__, __FUNCTION__, "assertion failed (!(%s))", #expr); \
Packit Service 6d40f9
	} while (0)
Packit Service 6d40f9
#define assert_fail(msg, detail) \
Packit Service 6d40f9
	do { const char *__s = (detail); \
Packit Service 6d40f9
		test_fail (__FILE__, __LINE__, __FUNCTION__, "%s%s%s", (msg), __s ? ": ": "", __s ? __s : ""); \
Packit Service 6d40f9
	} while (0)
Packit Service 6d40f9
#define assert_not_reached(msg) \
Packit Service 6d40f9
	do { \
Packit Service 6d40f9
		test_fail (__FILE__, __LINE__, __FUNCTION__, "code should not be reached"); \
Packit Service 6d40f9
	} while (0)
Packit Service 6d40f9
#define assert_ptr_not_null(ptr) \
Packit Service 6d40f9
	do { if ((ptr) != NULL) ; else \
Packit Service 6d40f9
		test_fail (__FILE__, __LINE__, __FUNCTION__, "assertion failed (%s != NULL)", #ptr); \
Packit Service 6d40f9
	} while (0)
Packit Service 6d40f9
#define assert_num_cmp(a1, cmp, a2) \
Packit Service 6d40f9
	do { unsigned long __n1 = (a1); \
Packit Service 6d40f9
	     unsigned long __n2 = (a2); \
Packit Service 6d40f9
	     if (__n1 cmp __n2) ; else \
Packit Service 6d40f9
		test_fail (__FILE__, __LINE__, __FUNCTION__, "assertion failed (%s %s %s): (%lu %s %lu)", \
Packit Service 6d40f9
		           #a1, #cmp, #a2, __n1, #cmp, __n2); \
Packit Service 6d40f9
	} while (0)
Packit Service 6d40f9
#define assert_num_eq(a1, a2) \
Packit Service 6d40f9
	assert_num_cmp(a1, ==, a2)
Packit Service 6d40f9
#define assert_str_cmp(a1, cmp, a2) \
Packit Service 6d40f9
	do { const char *__s1 = (a1); \
Packit Service 6d40f9
	     const char *__s2 = (a2); \
Packit Service 6d40f9
	     if (__s1 && __s2 && strcmp (__s1, __s2) cmp 0) ; else \
Packit Service 6d40f9
	         test_fail (__FILE__, __LINE__, __FUNCTION__, "assertion failed (%s %s %s): (%s %s %s)", \
Packit Service 6d40f9
	                    #a1, #cmp, #a2, __s1 ? __s1 : "(null)", #cmp, __s2 ? __s2 : "(null)"); \
Packit Service 6d40f9
	} while (0)
Packit Service 6d40f9
#define assert_str_eq(a1, a2) \
Packit Service 6d40f9
	assert_str_cmp(a1, ==, a2)
Packit Service 6d40f9
#define assert_ptr_eq(a1, a2) \
Packit Service 6d40f9
	do { const void *__p1 = (a1); \
Packit Service 6d40f9
	     const void *__p2 = (a2); \
Packit Service 6d40f9
	     if (__p1 == __p2) ; else \
Packit Service 6d40f9
	         test_fail (__FILE__, __LINE__, __FUNCTION__, "assertion failed (%s == %s): (0x%08lx == 0x%08lx)", \
Packit Service 6d40f9
	                    #a1, #a2, (unsigned long)(size_t)__p1, (unsigned long)(size_t)__p2); \
Packit Service 6d40f9
	} while (0)
Packit Service 6d40f9
Packit Service 6d40f9
#define assert_str_contains(expr, needle) \
Packit Service 6d40f9
	do { const char *__str = (expr); \
Packit Service 6d40f9
	     if (__str && strstr (__str, needle)) ; else \
Packit Service 6d40f9
	         test_fail (__FILE__, __LINE__, __FUNCTION__, "assertion failed (%s): '%s' does not contain '%s'", \
Packit Service 6d40f9
	                    #expr, __str, needle); \
Packit Service 6d40f9
	} while (0)
Packit Service 6d40f9
Packit Service 6d40f9
#endif /* !TEST_SOURCE */
Packit Service 6d40f9
Packit Service 6d40f9
Packit Service 6d40f9
void        test_fail               (const char *filename,
Packit Service 6d40f9
                                     int line,
Packit Service 6d40f9
                                     const char *function,
Packit Service 6d40f9
                                     const char *message,
Packit Service 6d40f9
                                     ...) GNUC_PRINTF(4, 5)
Packit Service 6d40f9
                                     CLANG_ANALYZER_NORETURN;
Packit Service 6d40f9
Packit Service 6d40f9
void        test_func               (void (* function) (void),
Packit Service 6d40f9
                                     const char *name,
Packit Service 6d40f9
                                     ...) GNUC_PRINTF(2, 3);
Packit Service 6d40f9
Packit Service 6d40f9
void        test_funcx              (void (* function) (void *),
Packit Service 6d40f9
                                     void *argument,
Packit Service 6d40f9
                                     const char *name,
Packit Service 6d40f9
                                     ...) GNUC_PRINTF(3, 4);
Packit Service 6d40f9
Packit Service 6d40f9
void        test_fixture            (void (* setup) (void *),
Packit Service 6d40f9
                                     void (* teardown) (void *));
Packit Service 6d40f9
Packit Service 6d40f9
int         test_run                (int argc,
Packit Service 6d40f9
                                     char **argv);
Packit Service 6d40f9
Packit Service 6d40f9
#endif /* TEST_H_ */