testutils.h 3.6 KB
Newer Older
K
Karel Zak 已提交
1 2 3
/*
 * utils.c: test utils
 *
4
 * Copyright (C) 2005, 2008-2013 Red Hat, Inc.
K
Karel Zak 已提交
5
 *
O
Osier Yang 已提交
6 7 8 9 10 11 12 13 14 15 16
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library.  If not, see
O
Osier Yang 已提交
18
 * <http://www.gnu.org/licenses/>.
K
Karel Zak 已提交
19 20 21 22 23
 *
 * Karel Zak <kzak@redhat.com>
 */

#ifndef __VIT_TEST_UTILS_H__
24
# define __VIT_TEST_UTILS_H__
K
Karel Zak 已提交
25

26
# include <stdio.h>
27
# include "viralloc.h"
28

29
# define EXIT_AM_SKIP 77 /* tell Automake we're skipping a test */
E
Eric Blake 已提交
30 31 32 33
# define EXIT_AM_HARDFAIL 99 /* tell Automake that the framework is broken */

extern char *progname;
extern char *abs_srcdir;
34

35 36
double virtTestCountAverage(double *items,
                            int nitems);
K
Karel Zak 已提交
37

38 39
void virtTestResult(const char *name, int ret, const char *msg, ...)
    ATTRIBUTE_FMT_PRINTF(3,4);
40 41 42 43
int virtTestRun(const char *title,
                int nloops,
                int (*body)(const void *data),
                const void *data);
44 45
int virtTestLoadFile(const char *file, char **buf);
int virtTestCaptureProgramOutput(const char *const argv[], char **buf, int maxlen);
46

47 48
int virtTestClearLineRegex(const char *pattern,
                           char *string);
K
Karel Zak 已提交
49

50 51 52
int virtTestDifference(FILE *stream,
                       const char *expect,
                       const char *actual);
53 54 55 56
int virtTestDifferenceBin(FILE *stream,
                          const char *expect,
                          const char *actual,
                          size_t length);
K
Karel Zak 已提交
57

58 59
unsigned int virTestGetDebug(void);
unsigned int virTestGetVerbose(void);
60

61 62
char *virtTestLogContentAndReset(void);

63 64
int virtTestMain(int argc,
                 char **argv,
E
Eric Blake 已提交
65
                 int (*func)(void));
66

E
Eric Blake 已提交
67 68 69 70
/* Setup, then call func() */
# define VIRT_TEST_MAIN(func)                   \
    int main(int argc, char **argv) {           \
        return virtTestMain(argc, argv, func);  \
71 72
    }

73 74 75 76 77
# define VIRT_TEST_MAIN_PRELOAD(func, lib)                              \
    int main(int argc, char **argv) {                                   \
        const char *preload = getenv("LD_PRELOAD");                     \
        if (preload == NULL || strstr(preload, lib) == NULL) {          \
            char *newenv;                                               \
78 79 80 81
            if (!virFileIsExecutable(lib)) {                            \
                perror(lib);                                            \
                return EXIT_FAILURE;                                    \
            }                                                           \
82 83 84
            if (virAsprintf(&newenv, "%s%s%s", preload ? preload : "",  \
                            preload ? ":" : "", lib) < 0) {             \
                perror("virAsprintf");                                  \
85
                return EXIT_FAILURE;                                    \
86 87 88 89 90 91 92
            }                                                           \
            setenv("LD_PRELOAD", newenv, 1);                            \
            execv(argv[0], argv);                                       \
        }                                                               \
        return virtTestMain(argc, argv, func);                          \
    }

K
Karel Zak 已提交
93
#endif /* __VIT_TEST_UTILS_H__ */