Mercurial > hg > th-libs
annotate tests.c @ 261:f1decaee6157
Improve tests.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 17 Feb 2016 12:59:55 +0200 |
parents | 33652234f162 |
children | 423771158575 |
rev | line source |
---|---|
254
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
1 #include "th_types.h" |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
2 #include "th_util.h" |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
3 #include "th_string.h" |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
4 #include "th_crypto.h" |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
5 |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
6 #define SET_BUF_SIZE 128 |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
7 |
259 | 8 char *test_str_header = NULL, |
9 *test_str_res = NULL; | |
10 | |
11 int tests_failed, tests_passed, tests_total; | |
12 | |
254
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
13 char buf1[SET_BUF_SIZE+2], buf2[SET_BUF_SIZE+2]; |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
14 |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
15 |
259 | 16 void test_start_v(const char *fmt, va_list ap) |
17 { | |
18 tests_total++; | |
19 | |
20 th_free_r(&test_str_header); | |
21 th_free_r(&test_str_res); | |
22 | |
23 test_str_header = th_strdup_vprintf(fmt, ap); | |
24 } | |
25 | |
26 | |
27 void test_start(const char *fmt, ...) | |
28 { | |
29 va_list ap; | |
30 va_start(ap, fmt); | |
31 test_start_v(fmt, ap); | |
32 va_end(ap); | |
33 } | |
34 | |
35 | |
36 void test_result_msg_v(BOOL check, const char *fmt, va_list ap) | |
254
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
37 { |
259 | 38 if (check) |
39 { | |
40 THPRINT(1, "%s: OK\n", test_str_header); | |
41 tests_passed++; | |
42 } | |
43 else | |
44 { | |
45 THPRINT(0, "%s: ", test_str_header); | |
46 if (fmt != NULL) | |
47 THPRINT_V(0, fmt, ap); | |
48 THPRINT(0, "\n%s\n", test_str_res); | |
49 tests_failed++; | |
50 } | |
51 } | |
52 | |
53 | |
54 void test_result_msg(BOOL check, const char *fmt, ...) | |
55 { | |
56 va_list ap; | |
57 va_start(ap, fmt); | |
58 test_result_msg_v(check, fmt, ap); | |
59 va_end(ap); | |
60 } | |
61 | |
62 | |
63 void test_result(BOOL check) | |
64 { | |
65 test_result_msg_v(check, NULL, NULL); | |
66 } | |
67 | |
68 | |
69 #define SET_SENTINEL_BYTE 0x0e5 | |
70 | |
71 void test_snprintf_do(size_t len, const char *fmt, va_list ap, const char *msg) | |
72 { | |
73 int ret1, ret2; | |
254
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
74 va_list tmp; |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
75 |
259 | 76 test_start("th_vsnprintf('%s', %s)", fmt, msg); |
77 | |
78 memset(buf1, SET_SENTINEL_BYTE, SET_BUF_SIZE+2); buf1[SET_BUF_SIZE+1] = 0; | |
79 memset(buf2, SET_SENTINEL_BYTE, SET_BUF_SIZE+2); buf2[SET_BUF_SIZE+1] = 0; | |
254
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
80 |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
81 va_copy(tmp, ap); |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
82 |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
83 ret1 = th_vsnprintf(buf1, len, fmt, ap); |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
84 ret2 = vsnprintf(buf2, len, fmt, tmp); |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
85 |
259 | 86 test_result_msg(ret1 == ret2, "retval mismatch %d != %d", ret1, ret2); |
87 test_result_msg(strcmp(buf1, buf2) == 0, "result mismatch '%s' != '%s'", buf1, buf2); | |
88 | |
89 test_result_msg((unsigned char) buf1[len] == SET_SENTINEL_BYTE, "buffer #1 overflow, sentinel 0x%02x", buf1[len]); | |
90 test_result_msg((unsigned char) buf2[len] == SET_SENTINEL_BYTE, "buffer #2 overflow, sentinel 0x%02x", buf2[len]); | |
261 | 91 |
92 test_start("th_strdup_vprintf('%s')", fmt); | |
93 char *str = th_strdup_vprintf(fmt, ap); | |
94 test_result_msg(str != NULL, "result NULL"); | |
95 th_free(str); | |
254
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
96 } |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
97 |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
98 |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
99 void test_snprintf(const char *fmt, ...) |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
100 { |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
101 va_list ap, tmp; |
255 | 102 THPRINT(1, "----------------------------------------------\n"); |
254
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
103 va_start(ap, fmt); |
259 | 104 va_copy(tmp, ap); test_snprintf_do(0, fmt, tmp, "0"); |
105 va_copy(tmp, ap); test_snprintf_do(1, fmt, tmp, "1"); | |
106 va_copy(tmp, ap); test_snprintf_do(2, fmt, tmp, "2"); | |
107 va_copy(tmp, ap); test_snprintf_do(16, fmt, tmp, "16"); | |
108 va_copy(tmp, ap); test_snprintf_do(SET_BUF_SIZE, fmt, tmp, "SET_BUF_SIZE"); | |
255 | 109 va_end(ap); |
110 THPRINT(1, "----------------------------------------------\n"); | |
254
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
111 } |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
112 |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
113 |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
114 int main(int argc, char *argv[]) |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
115 { |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
116 (void) argc; |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
117 (void) argv; |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
118 |
255 | 119 // |
120 // Initialization | |
121 // | |
254
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
122 th_init("th-test", "th-libs unit tests", "0.0.1", NULL, NULL); |
261 | 123 th_verbosityLevel = 0; |
254
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
124 |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
125 if (sizeof(char) != sizeof(unsigned char)) |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
126 { |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
127 THERR("sizeof(char) != sizeof(unsigned char)???\n"); |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
128 return -1; |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
129 } |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
130 |
259 | 131 tests_failed = tests_passed = tests_total = 0; |
254
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
132 |
255 | 133 // |
134 // Test series #1 | |
135 // | |
136 THPRINT(0, "printf() family function tests.\n"); | |
254
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
137 int i_vals[] = { 2, 612342, -2, -612342, 0x1fff, 0x8000000, }; |
261 | 138 char *i_fmts[] = { "%d", "%x", "%05d", "%5d", "%-5d", "%05x", "%5x", "", }; |
255 | 139 size_t i1, i2; |
254
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
140 |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
141 for (i1 = 0; i1 < sizeof(i_vals) / sizeof(i_vals[0]); i1++) |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
142 for (i2 = 0; i2 < sizeof(i_fmts) / sizeof(i_fmts[0]); i2++) |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
143 test_snprintf(i_fmts[i2], i_vals); |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
144 |
261 | 145 char *s_vals[] = { "", "asdf", "xxx yyy zzz ppp fff", NULL, "X", "abcde", }; |
255 | 146 char *s_fmts[] = { "%s", "%2s", "%-2s", "%5s", "%-5s", "%16s", "%-16s", "%1s", "%-1s", }; |
254
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
147 |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
148 for (i1 = 0; i1 < sizeof(s_vals) / sizeof(s_vals[0]); i1++) |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
149 for (i2 = 0; i2 < sizeof(s_fmts) / sizeof(s_fmts[0]); i2++) |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
150 test_snprintf(s_fmts[i2], s_vals); |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
151 |
257 | 152 test_snprintf("a%cBC", 'x'); |
261 | 153 test_snprintf("%c", 'x'); |
154 test_snprintf("", 'x'); | |
257 | 155 |
261 | 156 THPRINT(0, "printf() family function tests.\n"); |
255 | 157 |
158 // | |
159 // Print summary and exit | |
160 // | |
259 | 161 THPRINT(0, "%d tests failed, %d passed (%d main tests).\n", |
162 tests_failed, tests_passed, tests_total); | |
255 | 163 |
254
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
164 return 0; |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
165 } |