annotate tests.c @ 264:9b6fd2953d8c

Add test header printing function.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 17 Feb 2016 13:30:50 +0200
parents 423771158575
children effb2786f6a6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
8 char *test_str_header = NULL,
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
9 *test_str_res = NULL;
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
10
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
11 int tests_failed, tests_passed, tests_total;
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
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
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
16 void test_start_v(const char *fmt, va_list ap)
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
17 {
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
18 tests_total++;
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
19
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
20 th_free_r(&test_str_header);
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
21 th_free_r(&test_str_res);
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
22
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
23 test_str_header = th_strdup_vprintf(fmt, ap);
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
24 }
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
25
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
26
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
27 void test_start(const char *fmt, ...)
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
28 {
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
29 va_list ap;
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
30 va_start(ap, fmt);
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
31 test_start_v(fmt, ap);
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
32 va_end(ap);
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
33 }
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
34
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
35
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
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
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
38 if (check)
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
39 {
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
40 THPRINT(1, "%s: OK\n", test_str_header);
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
41 tests_passed++;
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
42 }
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
43 else
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
44 {
263
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
45 THPRINT(0, "%s: FAIL\n", test_str_header);
259
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
46 if (fmt != NULL)
263
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
47 {
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
48 THPRINT(0, " - ");
259
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
49 THPRINT_V(0, fmt, ap);
263
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
50 THPRINT(0, "\n");
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
51 }
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
52 if (test_str_res != NULL)
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
53 THPRINT(0, "%s\n", test_str_res);
259
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
54 tests_failed++;
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
55 }
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
56 }
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
57
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
58
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
59 void test_result_msg(BOOL check, const char *fmt, ...)
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
60 {
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
61 va_list ap;
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
62 va_start(ap, fmt);
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
63 test_result_msg_v(check, fmt, ap);
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
64 va_end(ap);
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
65 }
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
66
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
67
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
68 void test_result(BOOL check)
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
69 {
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
70 test_result_msg_v(check, NULL, NULL);
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
71 }
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
72
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
73
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
74 #define SET_SENTINEL_BYTE 0x0e5
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
75
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
76 void test_snprintf_do(size_t len, const char *fmt, va_list ap, const char *msg)
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
77 {
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
78 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
79 va_list tmp;
3d1e2af4e4e6 Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
80
263
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
81 // Test basic *printf() functionality
259
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
82 test_start("th_vsnprintf('%s', %s)", fmt, msg);
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
83
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
84 memset(buf1, SET_SENTINEL_BYTE, SET_BUF_SIZE+2); buf1[SET_BUF_SIZE+1] = 0;
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
85 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
86
3d1e2af4e4e6 Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
87 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
88
3d1e2af4e4e6 Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
89 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
90 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
91
259
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
92 test_result_msg(ret1 == ret2, "retval mismatch %d != %d", ret1, ret2);
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
93 test_result_msg(strcmp(buf1, buf2) == 0, "result mismatch '%s' != '%s'", buf1, buf2);
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
94
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
95 test_result_msg((unsigned char) buf1[len] == SET_SENTINEL_BYTE, "buffer #1 overflow, sentinel 0x%02x", buf1[len]);
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
96 test_result_msg((unsigned char) buf2[len] == SET_SENTINEL_BYTE, "buffer #2 overflow, sentinel 0x%02x", buf2[len]);
261
f1decaee6157 Improve tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 259
diff changeset
97
263
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
98 // Test th_strdup_vprintf()
261
f1decaee6157 Improve tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 259
diff changeset
99 test_start("th_strdup_vprintf('%s')", fmt);
f1decaee6157 Improve tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 259
diff changeset
100 char *str = th_strdup_vprintf(fmt, ap);
f1decaee6157 Improve tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 259
diff changeset
101 test_result_msg(str != NULL, "result NULL");
f1decaee6157 Improve tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 259
diff changeset
102 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
103 }
3d1e2af4e4e6 Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
104
3d1e2af4e4e6 Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
105
3d1e2af4e4e6 Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
106 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
107 {
3d1e2af4e4e6 Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
108 va_list ap, tmp;
3d1e2af4e4e6 Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
109 va_start(ap, fmt);
259
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
110 va_copy(tmp, ap); test_snprintf_do(0, fmt, tmp, "0");
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
111 va_copy(tmp, ap); test_snprintf_do(1, fmt, tmp, "1");
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
112 va_copy(tmp, ap); test_snprintf_do(2, fmt, tmp, "2");
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
113 va_copy(tmp, ap); test_snprintf_do(16, fmt, tmp, "16");
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
114 va_copy(tmp, ap); test_snprintf_do(SET_BUF_SIZE, fmt, tmp, "SET_BUF_SIZE");
255
7549e279fe18 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 254
diff changeset
115 va_end(ap);
7549e279fe18 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 254
diff changeset
116 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
117 }
3d1e2af4e4e6 Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
118
3d1e2af4e4e6 Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
119
264
9b6fd2953d8c Add test header printing function.
Matti Hamalainen <ccr@tnsp.org>
parents: 263
diff changeset
120 void tests_header(const char *str)
9b6fd2953d8c Add test header printing function.
Matti Hamalainen <ccr@tnsp.org>
parents: 263
diff changeset
121 {
9b6fd2953d8c Add test header printing function.
Matti Hamalainen <ccr@tnsp.org>
parents: 263
diff changeset
122 THPRINT(0,
9b6fd2953d8c Add test header printing function.
Matti Hamalainen <ccr@tnsp.org>
parents: 263
diff changeset
123 "======================================================\n"
9b6fd2953d8c Add test header printing function.
Matti Hamalainen <ccr@tnsp.org>
parents: 263
diff changeset
124 " %s tests\n"
9b6fd2953d8c Add test header printing function.
Matti Hamalainen <ccr@tnsp.org>
parents: 263
diff changeset
125 "======================================================\n",
9b6fd2953d8c Add test header printing function.
Matti Hamalainen <ccr@tnsp.org>
parents: 263
diff changeset
126 str);
9b6fd2953d8c Add test header printing function.
Matti Hamalainen <ccr@tnsp.org>
parents: 263
diff changeset
127 }
9b6fd2953d8c Add test header printing function.
Matti Hamalainen <ccr@tnsp.org>
parents: 263
diff changeset
128
9b6fd2953d8c Add test header printing function.
Matti Hamalainen <ccr@tnsp.org>
parents: 263
diff changeset
129
254
3d1e2af4e4e6 Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
130 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
131 {
3d1e2af4e4e6 Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
132 (void) argc;
3d1e2af4e4e6 Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
133 (void) argv;
3d1e2af4e4e6 Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
134
255
7549e279fe18 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 254
diff changeset
135 //
7549e279fe18 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 254
diff changeset
136 // Initialization
7549e279fe18 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 254
diff changeset
137 //
254
3d1e2af4e4e6 Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
138 th_init("th-test", "th-libs unit tests", "0.0.1", NULL, NULL);
263
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
139 th_verbosityLevel = 1;
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 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
142 {
3d1e2af4e4e6 Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
143 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
144 return -1;
3d1e2af4e4e6 Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
145 }
3d1e2af4e4e6 Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
146
259
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
147 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
148
255
7549e279fe18 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 254
diff changeset
149 //
7549e279fe18 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 254
diff changeset
150 // Test series #1
7549e279fe18 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 254
diff changeset
151 //
264
9b6fd2953d8c Add test header printing function.
Matti Hamalainen <ccr@tnsp.org>
parents: 263
diff changeset
152 tests_header("printf() function family");
254
3d1e2af4e4e6 Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
153 int i_vals[] = { 2, 612342, -2, -612342, 0x1fff, 0x8000000, };
261
f1decaee6157 Improve tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 259
diff changeset
154 char *i_fmts[] = { "%d", "%x", "%05d", "%5d", "%-5d", "%05x", "%5x", "", };
255
7549e279fe18 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 254
diff changeset
155 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
156
3d1e2af4e4e6 Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
157 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
158 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
159 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
160
261
f1decaee6157 Improve tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 259
diff changeset
161 char *s_vals[] = { "", "asdf", "xxx yyy zzz ppp fff", NULL, "X", "abcde", };
255
7549e279fe18 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 254
diff changeset
162 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
163
3d1e2af4e4e6 Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
164 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
165 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
166 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
167
257
d56369f65941 Add test for '%c'.
Matti Hamalainen <ccr@tnsp.org>
parents: 255
diff changeset
168 test_snprintf("a%cBC", 'x');
261
f1decaee6157 Improve tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 259
diff changeset
169 test_snprintf("%c", 'x');
f1decaee6157 Improve tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 259
diff changeset
170 test_snprintf("", 'x');
257
d56369f65941 Add test for '%c'.
Matti Hamalainen <ccr@tnsp.org>
parents: 255
diff changeset
171
263
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
172 //
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
173 // String matching functions
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
174 //
264
9b6fd2953d8c Add test header printing function.
Matti Hamalainen <ccr@tnsp.org>
parents: 263
diff changeset
175 tests_header("String matching");
263
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
176
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
177 #define TEST2(fun, str1, str2, ret) do { \
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
178 test_start(# fun "('%s', '%s')", str1, str2); \
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
179 test_result(( fun (str1, str2) == 0) == ret); \
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
180 } while (0)
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
181
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
182 #define TEST3(fun, str1, str2, len, ret) do { \
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
183 test_start(# fun "('%s', '%s', %d)", str1, str2, len); \
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
184 test_result(( fun (str1, str2, len) == 0) == ret); \
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
185 } while (0)
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
186
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
187 TEST2(th_strcasecmp, "aSdFq", "asdfq", TRUE);
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
188 TEST2(th_strcasecmp, "aSdFq", "asFfq", FALSE);
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
189 TEST3(th_strncasecmp, "aSdFq", "asFfq", 4, FALSE);
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
190 TEST3(th_strncasecmp, "aSdFq", "asFfq", 2, TRUE);
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
191 TEST3(th_strncasecmp, "aSdFq", "asDfq", 3, TRUE);
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
192 TEST3(th_strncasecmp, "aSdFq", "asDfq", 2, TRUE);
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
193 TEST3(th_strncasecmp, "aSdFq", "asDfq", 0, TRUE);
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
194 TEST3(th_strncasecmp, "aSdFq", "QsDfq", 0, TRUE);
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
195 TEST3(th_strncasecmp, "aSdFq", "QsDfq", 1, FALSE);
255
7549e279fe18 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 254
diff changeset
196
7549e279fe18 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 254
diff changeset
197 //
7549e279fe18 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 254
diff changeset
198 // Print summary and exit
7549e279fe18 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 254
diff changeset
199 //
259
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
200 THPRINT(0, "%d tests failed, %d passed (%d main tests).\n",
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
201 tests_failed, tests_passed, tests_total);
255
7549e279fe18 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 254
diff changeset
202
254
3d1e2af4e4e6 Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
203 return 0;
3d1e2af4e4e6 Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
204 }