testutils.h 3.7 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
# include "virfile.h"
# include "virstring.h"
30

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

extern char *progname;
extern char *abs_srcdir;
36

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

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

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

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

60 61
unsigned int virTestGetDebug(void);
unsigned int virTestGetVerbose(void);
62

63 64
char *virtTestLogContentAndReset(void);

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

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

75 76 77 78 79
# 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;                                               \
80 81 82 83
            if (!virFileIsExecutable(lib)) {                            \
                perror(lib);                                            \
                return EXIT_FAILURE;                                    \
            }                                                           \
84 85 86
            if (virAsprintf(&newenv, "%s%s%s", preload ? preload : "",  \
                            preload ? ":" : "", lib) < 0) {             \
                perror("virAsprintf");                                  \
87
                return EXIT_FAILURE;                                    \
88 89 90 91 92 93 94
            }                                                           \
            setenv("LD_PRELOAD", newenv, 1);                            \
            execv(argv[0], argv);                                       \
        }                                                               \
        return virtTestMain(argc, argv, func);                          \
    }

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