Showing
3 changed files
with
171 additions
and
0 deletions
tests/cclass_test.c
0 → 100644
1 | +#include <sys/types.h> | ||
2 | + | ||
3 | +#include "runtest.h" | ||
4 | + | ||
5 | + | ||
6 | +char testname[] = "cclass_test"; | ||
7 | + | ||
8 | +int | ||
9 | +dummy_ok(void) | ||
10 | +{ | ||
11 | + return TEST_OK; | ||
12 | +} | ||
13 | + | ||
14 | +int | ||
15 | +dummy_failed(void) | ||
16 | +{ | ||
17 | + return TEST_FAILED; | ||
18 | +} | ||
19 | + | ||
20 | +int | ||
21 | +dummy_error(void) | ||
22 | +{ | ||
23 | + return TEST_ERROR; | ||
24 | +} | ||
25 | + | ||
26 | +testfunc tests[] = { | ||
27 | + dummy_ok, | ||
28 | + dummy_failed, | ||
29 | + dummy_error | ||
30 | +}; | ||
31 | +size_t count = FUNCS_COUNT(tests); | ||
32 | + | ||
33 | +// vim: set et ts=4 sw=4: |
tests/runtest.c
0 → 100644
1 | +#include <stdio.h> | ||
2 | +#include <stdlib.h> | ||
3 | +#include <dlfcn.h> | ||
4 | +#include <dirent.h> | ||
5 | +#include <string.h> | ||
6 | +#include <sys/types.h> | ||
7 | + | ||
8 | +#include "runtest.h" | ||
9 | + | ||
10 | +#define TEST_OK_CHAR '.' | ||
11 | +#define TEST_FAILED_CHAR 'F' | ||
12 | +#define TEST_ERROR_CHAR 'E' | ||
13 | + | ||
14 | + | ||
15 | +const char results[3] = { | ||
16 | + TEST_OK_CHAR, | ||
17 | + TEST_FAILED_CHAR, | ||
18 | + TEST_ERROR_CHAR | ||
19 | +}; | ||
20 | + | ||
21 | + | ||
22 | +void * | ||
23 | +load_symbol(void * dlhandle, const char * const symbol) | ||
24 | +{ | ||
25 | + void * sym = dlsym(dlhandle, symbol); | ||
26 | + char * error; | ||
27 | + | ||
28 | + if ((error = dlerror()) != NULL) { | ||
29 | + fprintf(stderr, "%s\n", error); | ||
30 | + exit(EXIT_FAILURE); | ||
31 | + } | ||
32 | + | ||
33 | + return sym; | ||
34 | +} | ||
35 | + | ||
36 | +void | ||
37 | +runtests( | ||
38 | + const char * const filename, | ||
39 | + size_t * _count, | ||
40 | + size_t * failures, | ||
41 | + size_t * errors) | ||
42 | +{ | ||
43 | + size_t * count; | ||
44 | + testfunc * tests; | ||
45 | + const char * const testname; | ||
46 | + //char * const * funcnames; | ||
47 | + | ||
48 | + size_t index; | ||
49 | + void * dlhandle; | ||
50 | + | ||
51 | + dlhandle = dlopen("./cclass.test", RTLD_LAZY); | ||
52 | + if (!dlhandle) { | ||
53 | + fprintf(stderr, "%s\n", dlerror()); | ||
54 | + exit(EXIT_FAILURE); | ||
55 | + } | ||
56 | + | ||
57 | + * (void **) (&count) = load_symbol(dlhandle, "count"); | ||
58 | + * (void **) (&tests) = load_symbol(dlhandle, "tests"); | ||
59 | + * (void **) (&testname) = load_symbol(dlhandle, "testname"); | ||
60 | + // * (void **) (&funcnames) = load_symbol(dlhandle, "funcnames"); | ||
61 | + | ||
62 | + *_count += *count; | ||
63 | + | ||
64 | + printf("running tests for %s\n", testname); | ||
65 | + | ||
66 | + for (index=0; index<*count; index++) { | ||
67 | + int result = tests[index](); | ||
68 | + | ||
69 | + switch (result) { | ||
70 | + case TEST_FAILED: (*failures)++; break; | ||
71 | + case TEST_ERROR: (*errors)++; break; | ||
72 | + } | ||
73 | + | ||
74 | + putchar(results[result]); | ||
75 | + | ||
76 | + if (79 == index%80) { | ||
77 | + putchar('\n'); | ||
78 | + } | ||
79 | + | ||
80 | + fflush(stdout); | ||
81 | + } | ||
82 | + puts("\n"); | ||
83 | + | ||
84 | + dlclose(dlhandle); | ||
85 | +} | ||
86 | + | ||
87 | +int | ||
88 | +main(int argc, char * argv[]) | ||
89 | +{ | ||
90 | + size_t count = 0; | ||
91 | + size_t errors = 0; | ||
92 | + size_t failures = 0; | ||
93 | + size_t assertions = 0; | ||
94 | + | ||
95 | + DIR * dir; | ||
96 | + struct dirent * dirent; | ||
97 | + | ||
98 | + dir = opendir("."); | ||
99 | + | ||
100 | + dirent = readdir(dir); | ||
101 | + while (dirent) { | ||
102 | + if (0 == strcmp(".test", dirent->d_name + strlen(dirent->d_name) - 5)) { | ||
103 | + runtests(dirent->d_name, &count, &failures, &errors); | ||
104 | + } | ||
105 | + | ||
106 | + dirent = readdir(dir); | ||
107 | + } | ||
108 | + closedir(dir); | ||
109 | + | ||
110 | + printf("running %lu tests: %lu - OK, %lu - FAILED, %lu - ERRORS\n", | ||
111 | + count, | ||
112 | + count - errors - failures, | ||
113 | + failures, | ||
114 | + errors); | ||
115 | + | ||
116 | + return 0; | ||
117 | +} | ||
118 | + | ||
119 | +// vim: set et ts=4 sw=4: |
tests/runtest.h
0 → 100644
1 | +#ifndef __RUNTEST_h__ | ||
2 | +#define __RUNTEST_h__ | ||
3 | + | ||
4 | +#include <sys/types.h> | ||
5 | + | ||
6 | + | ||
7 | +enum RESULT_TYPES { | ||
8 | + TEST_OK=0, | ||
9 | + TEST_FAILED, | ||
10 | + TEST_ERROR | ||
11 | +}; | ||
12 | + | ||
13 | +typedef int (* testfunc)(void); | ||
14 | +#define FUNCS_COUNT(array) (sizeof((array)) / sizeof(testfunc)) | ||
15 | + | ||
16 | +extern char testname[]; | ||
17 | + | ||
18 | +#endif//__RUNTEST_h__ | ||
19 | +// vim: set et ts=4 sw=4: |
Please
register
or
login
to post a comment