Fix warning in util.h (signed vs unsigned comparison)
[libgit2.git] / tests / test_lib.h
blob78f9923320d8c6ff69015ffa8c0410f6599a3499
1 /*
2 * This file is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2,
4 * as published by the Free Software Foundation.
6 * In addition to the permissions in the GNU General Public License,
7 * the authors give you unlimited permission to link the compiled
8 * version of this file into combinations with other programs,
9 * and to distribute those combinations without any restriction
10 * coming from the use of this file. (The General Public License
11 * restrictions do apply in other respects; for example, they cover
12 * modification of the file, and distribution when not linked into
13 * a combined executable.)
15 * This file is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; see the file COPYING. If not, write to
22 * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
23 * Boston, MA 02110-1301, USA.
26 #include "common.h"
27 #include <git/common.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <stdarg.h>
32 #include <string.h>
34 /** Declare a function never returns to the caller. */
35 #ifdef __GNUC__
36 # define NORETURN __attribute__((__noreturn__))
37 #elif defined(_MSC_VER)
38 # define NORETURN __declspec(noreturn)
39 #else
40 # define NORETURN /* noreturn */
41 #endif
43 /**
44 * Declares a new test block starting, with the specified name.
45 * @param name C symbol to assign to this test's function.
47 #define BEGIN_TEST(name) \
48 void testfunc__##name(void) \
49 { \
50 test_begin(#name, __FILE__, __LINE__); \
53 /** Denote the end of a test. */
54 #define END_TEST \
55 } \
56 test_end(); \
59 /* These are internal functions for BEGIN_TEST, END_TEST. */
60 extern void test_begin(const char *, const char *, int);
61 extern void test_end(void);
63 /**
64 * Abort the current test suite.
66 * This function terminates the current test suite
67 * and does not return to the caller.
69 * @param fmt printf style format string.
71 extern void NORETURN test_die(const char *fmt, ...)
72 GIT_FORMAT_PRINTF(1, 2);
74 /**
75 * Evaluate a library function which must return success.
77 * The definition of "success" is the classical 0 return value.
78 * This macro makes the test suite fail if the expression evaluates
79 * to a non-zero result. It is suitable for testing most API
80 * functions in the library.
82 * @param expr the expression to evaluate, and test the result of.
84 #define must_pass(expr) \
85 if (expr) test_die("line %d: %s", __LINE__, #expr)
87 /**
88 * Evaluate a library function which must return an error.
90 * The definition of "failure" is the classical non-0 return value.
91 * This macro makes the test suite fail if the expression evaluates
92 * to 0 (aka success). It is suitable for testing most API
93 * functions in the library.
95 * @param expr the expression to evaluate, and test the result of.
97 #define must_fail(expr) \
98 if (!(expr)) test_die("line %d: %s", __LINE__, #expr)
101 * Evaluate an expression which must produce a true result.
103 * @param expr the expression to evaluate, and test the result of.
105 #define must_be_true(expr) \
106 if (!(expr)) test_die("line %d: %s", __LINE__, #expr)