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
error_msg(int result,int line,const char * file,const char * command)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\nResult: %d",
31 (unsigned long)test_number, command, result);
32 printf("AT LINE: %d, FILE: %s\n\n", line, file);
33 }
34
success_msg(int result,const char * command)35 void success_msg(int result __attribute__((unused)), const char* command __attribute__((unused)))
36 {
37 #if 0
38 printf("passed test: %s == 0\n", command);
39 #endif
40 }
41
done_testing(void)42 void done_testing(void)
43 {
44 if (0 < failures) {
45 printf("Failed %d tests\n", failures);
46 exit(EXIT_FAILURE);
47 } else {
48 printf("All functions tested sucessfully\n");
49 exit(EXIT_SUCCESS);
50 }
51 }
52
init_testsuite(const char * testname)53 void init_testsuite(const char* testname)
54 {
55 printf("%s", testname);
56 test_number = 0;
57 failures = 0;
58 #if !defined(__UCLIBC__) || defined(__UCLIBC_DYNAMIC_ATEXIT__)
59 atexit(done_testing);
60 #endif
61 }
62
63 #endif /* __NO_TESTCODE__ */
64
65
66 #define TEST_STRING_OUTPUT(command, expected_result) \
67 do { \
68 int result = strcmp(command, expected_result); \
69 test_number++; \
70 if (result == expected_result) { \
71 success_msg(result, "command"); \
72 } else { \
73 error_msg(result, __LINE__, __FILE__, command); \
74 }; \
75 } while (0)
76
77 #define TEST_NUMERIC(command, expected_result) \
78 do { \
79 int result = (command); \
80 test_number++; \
81 if (result == expected_result) { \
82 success_msg(result, # command); \
83 } else { \
84 error_msg(result, __LINE__, __FILE__, # command); \
85 }; \
86 } while (0)
87
88 #define TEST(command) \
89 do { \
90 int result = (command); \
91 test_number++; \
92 if (result == 1) { \
93 success_msg(result, # command); \
94 } else { \
95 error_msg(result, __LINE__, __FILE__, # command); \
96 }; \
97 } while (0)
98
99 #define STR_CMD(cmd) cmd
100
101 #endif /* TESTSUITE_H */
102