OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / uClibc / test / testsuite.h
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Some simple macros for use in test applications.
4  * Copyright (C) 2000-2006 by Erik Andersen <andersen@uclibc.org>
5  *
6  * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
7  */
8
9 #ifndef TESTSUITE_H
10 #define TESTSUITE_H
11
12 #ifdef __NO_TESTCODE__
13 extern size_t test_number;
14 #endif
15
16 extern void init_testsuite(const char* testname);
17 extern void done_testing(void) __attribute__((noreturn));
18 extern void success_msg(int result, const char* command);
19 extern void error_msg(int result, int line, const char* file, const char* command);
20
21 #ifndef __NO_TESTCODE__
22
23 size_t test_number = 0;
24 static int failures = 0;
25
26 void error_msg(int result, int line, const char* file, const char* command)
27 {
28         failures++;
29
30         printf("\nFAILED TEST %lu: \n\t%s\n", (unsigned long)test_number, command);
31         printf("AT LINE: %d, FILE: %s\n\n", line, file);
32 }
33
34 void success_msg(int result, const char* command)
35 {
36 #if 0
37         printf("passed test: %s == 0\n", command);
38 #endif
39 }
40
41 void done_testing(void)
42 {
43     if (0 < failures) {
44                 printf("Failed %d tests\n", failures);
45                 exit(EXIT_FAILURE);
46         } else {
47                 printf("All functions tested sucessfully\n");
48                 exit(EXIT_SUCCESS);
49         }
50 }
51
52 void init_testsuite(const char* testname)
53 {
54         printf("%s", testname);
55         test_number = 0;
56         failures = 0;
57 #if !defined(__UCLIBC__) || defined(__UCLIBC_DYNAMIC_ATEXIT__)
58         atexit(done_testing);
59 #endif
60 }
61
62 #endif /* __NO_TESTCODE__ */
63
64
65 #define TEST_STRING_OUTPUT(command, expected_result) \
66         do { \
67                 int result = strcmp(command, expected_result); \
68                 test_number++; \
69                 if (result == expected_result) { \
70                         success_msg(result, "command"); \
71                 } else { \
72                         error_msg(result, __LINE__, __FILE__, command); \
73                 }; \
74         } while (0)
75
76 #define TEST_NUMERIC(command, expected_result) \
77         do { \
78                 int result = (command); \
79                 test_number++; \
80                 if (result == expected_result) { \
81                         success_msg(result, # command); \
82                 } else { \
83                         error_msg(result, __LINE__, __FILE__, # command); \
84                 }; \
85         } while (0)
86
87 #define TEST(command) \
88         do { \
89                 int result = (command); \
90                 test_number++; \
91                 if (result == 1) { \
92                         success_msg(result, # command); \
93                 } else { \
94                         error_msg(result, __LINE__, __FILE__,  # command); \
95                 }; \
96         } while (0)
97
98 #define STR_CMD(cmd) cmd
99
100 #endif  /* TESTSUITE_H */