annotate tests.c @ 274:f875db8634b6

Implement commandline options for test driver.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 17 Feb 2016 16:56:21 +0200
parents 7f01ad43d257
children 56b0de9f9d44
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"
274
f875db8634b6 Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
2 #include "th_args.h"
254
3d1e2af4e4e6 Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3 #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
4 #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
5 #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
6
3d1e2af4e4e6 Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7 #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
8
259
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
9 char *test_str_header = NULL,
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
10 *test_str_res = NULL;
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
11
265
effb2786f6a6 More work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 264
diff changeset
12 int tests_failed, tests_passed, tests_total, tests_set;
259
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
13
254
3d1e2af4e4e6 Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
14 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
15
3d1e2af4e4e6 Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
16
274
f875db8634b6 Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
17 // Define option arguments
f875db8634b6 Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
18 static const th_optarg_t arg_opts[] =
f875db8634b6 Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
19 {
f875db8634b6 Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
20 { 0, '?', "help", "Show this help", OPT_NONE },
f875db8634b6 Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
21 { 1, 'v', "verbose", "Be more verbose", OPT_NONE },
f875db8634b6 Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
22 };
f875db8634b6 Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
23
f875db8634b6 Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
24 static const int arg_nopts = sizeof(arg_opts) / sizeof(arg_opts[0]);
f875db8634b6 Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
25
f875db8634b6 Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
26
f875db8634b6 Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
27 void arg_show_help(void)
f875db8634b6 Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
28 {
f875db8634b6 Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
29 th_print_banner(stdout, th_prog_name, "[options]");
f875db8634b6 Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
30 th_args_help(stdout, arg_opts, arg_nopts, 0);
f875db8634b6 Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
31 }
f875db8634b6 Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
32
f875db8634b6 Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
33
f875db8634b6 Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
34 BOOL arg_handle_opt(const int optN, char *optArg, char *currArg)
f875db8634b6 Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
35 {
f875db8634b6 Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
36 switch (optN)
f875db8634b6 Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
37 {
f875db8634b6 Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
38 case 0:
f875db8634b6 Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
39 arg_show_help();
f875db8634b6 Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
40 exit(0);
f875db8634b6 Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
41 break;
f875db8634b6 Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
42
f875db8634b6 Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
43 case 1:
f875db8634b6 Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
44 th_verbosityLevel++;
f875db8634b6 Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
45 break;
f875db8634b6 Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
46
f875db8634b6 Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
47 default:
f875db8634b6 Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
48 THERR("Unknown option '%s'.\n", currArg);
f875db8634b6 Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
49 return FALSE;
f875db8634b6 Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
50 }
f875db8634b6 Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
51
f875db8634b6 Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
52 return TRUE;
f875db8634b6 Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
53 }
f875db8634b6 Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
54
f875db8634b6 Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
55
259
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
56 void test_start_v(const char *fmt, va_list ap)
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 tests_total++;
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
59
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
60 th_free_r(&test_str_header);
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
61 th_free_r(&test_str_res);
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
62
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
63 test_str_header = th_strdup_vprintf(fmt, ap);
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
64 }
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 void test_start(const char *fmt, ...)
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
68 {
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
69 va_list ap;
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
70 va_start(ap, fmt);
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
71 test_start_v(fmt, ap);
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
72 va_end(ap);
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
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_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
77 {
259
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
78 if (check)
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
79 {
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
80 THPRINT(1, "%s: OK\n", test_str_header);
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
81 tests_passed++;
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
82 }
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
83 else
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
84 {
263
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
85 THPRINT(0, "%s: FAIL\n", test_str_header);
259
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
86 if (fmt != NULL)
263
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
87 {
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
88 THPRINT(0, " - ");
259
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
89 THPRINT_V(0, fmt, ap);
263
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
90 THPRINT(0, "\n");
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
91 }
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
92 if (test_str_res != NULL)
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
93 THPRINT(0, "%s\n", test_str_res);
259
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
94 tests_failed++;
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
95 }
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
96 }
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
97
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
98
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
99 void test_result_msg(BOOL check, const char *fmt, ...)
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
100 {
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
101 va_list ap;
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
102 va_start(ap, fmt);
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
103 test_result_msg_v(check, fmt, ap);
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
104 va_end(ap);
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
105 }
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
106
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
107
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
108 void test_result(BOOL check)
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
109 {
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
110 test_result_msg_v(check, NULL, NULL);
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
111 }
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
112
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
113
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
114 #define SET_SENTINEL_BYTE 0x0e5
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
115
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
116 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
117 {
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
118 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
119 va_list tmp;
3d1e2af4e4e6 Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
120
263
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
121 // Test basic *printf() functionality
259
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
122 test_start("th_vsnprintf('%s', %s)", fmt, msg);
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
123
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
124 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
125 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
126
3d1e2af4e4e6 Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
127 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
128
3d1e2af4e4e6 Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
129 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
130 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
131
259
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
132 test_result_msg(ret1 == ret2, "retval mismatch %d != %d", ret1, ret2);
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
133 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
134
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
135 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
136 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
137
263
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
138 // Test th_strdup_vprintf()
261
f1decaee6157 Improve tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 259
diff changeset
139 test_start("th_strdup_vprintf('%s')", fmt);
f1decaee6157 Improve tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 259
diff changeset
140 char *str = th_strdup_vprintf(fmt, ap);
f1decaee6157 Improve tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 259
diff changeset
141 test_result_msg(str != NULL, "result NULL");
f1decaee6157 Improve tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 259
diff changeset
142 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
143 }
3d1e2af4e4e6 Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
144
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 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
147 {
3d1e2af4e4e6 Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
148 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
149 va_start(ap, fmt);
259
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
150 va_copy(tmp, ap); test_snprintf_do(0, fmt, tmp, "0");
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
151 va_copy(tmp, ap); test_snprintf_do(1, fmt, tmp, "1");
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
152 va_copy(tmp, ap); test_snprintf_do(2, fmt, tmp, "2");
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
153 va_copy(tmp, ap); test_snprintf_do(16, fmt, tmp, "16");
33652234f162 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 257
diff changeset
154 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
155 va_end(ap);
7549e279fe18 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 254
diff changeset
156 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
157 }
3d1e2af4e4e6 Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
158
3d1e2af4e4e6 Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
159
264
9b6fd2953d8c Add test header printing function.
Matti Hamalainen <ccr@tnsp.org>
parents: 263
diff changeset
160 void tests_header(const char *str)
9b6fd2953d8c Add test header printing function.
Matti Hamalainen <ccr@tnsp.org>
parents: 263
diff changeset
161 {
9b6fd2953d8c Add test header printing function.
Matti Hamalainen <ccr@tnsp.org>
parents: 263
diff changeset
162 THPRINT(0,
9b6fd2953d8c Add test header printing function.
Matti Hamalainen <ccr@tnsp.org>
parents: 263
diff changeset
163 "======================================================\n"
265
effb2786f6a6 More work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 264
diff changeset
164 " Set #%d : %s tests\n"
264
9b6fd2953d8c Add test header printing function.
Matti Hamalainen <ccr@tnsp.org>
parents: 263
diff changeset
165 "======================================================\n",
265
effb2786f6a6 More work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 264
diff changeset
166 ++tests_set,
264
9b6fd2953d8c Add test header printing function.
Matti Hamalainen <ccr@tnsp.org>
parents: 263
diff changeset
167 str);
9b6fd2953d8c Add test header printing function.
Matti Hamalainen <ccr@tnsp.org>
parents: 263
diff changeset
168 }
9b6fd2953d8c Add test header printing function.
Matti Hamalainen <ccr@tnsp.org>
parents: 263
diff changeset
169
9b6fd2953d8c Add test header printing function.
Matti Hamalainen <ccr@tnsp.org>
parents: 263
diff changeset
170
254
3d1e2af4e4e6 Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
171 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
172 {
255
7549e279fe18 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 254
diff changeset
173 //
7549e279fe18 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 254
diff changeset
174 // Initialization
7549e279fe18 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 254
diff changeset
175 //
254
3d1e2af4e4e6 Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
176 th_init("th-test", "th-libs unit tests", "0.0.1", NULL, NULL);
274
f875db8634b6 Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
177 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
178
3d1e2af4e4e6 Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
179 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
180 {
3d1e2af4e4e6 Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
181 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
182 return -1;
3d1e2af4e4e6 Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
183 }
3d1e2af4e4e6 Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
184
265
effb2786f6a6 More work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 264
diff changeset
185 tests_failed = tests_passed = tests_total = tests_set = 0;
254
3d1e2af4e4e6 Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
186
255
7549e279fe18 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 254
diff changeset
187 //
274
f875db8634b6 Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
188 // Parse command line arguments
f875db8634b6 Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
189 //
f875db8634b6 Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
190 if (!th_args_process(argc, argv, arg_opts, arg_nopts,
f875db8634b6 Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
191 arg_handle_opt, NULL, 0))
f875db8634b6 Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
192 return 0;
f875db8634b6 Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
193
f875db8634b6 Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
194
f875db8634b6 Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
195 //
255
7549e279fe18 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 254
diff changeset
196 // Test series #1
7549e279fe18 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 254
diff changeset
197 //
264
9b6fd2953d8c Add test header printing function.
Matti Hamalainen <ccr@tnsp.org>
parents: 263
diff changeset
198 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
199 int i_vals[] = { 2, 612342, -2, -612342, 0x1fff, 0x8000000, };
261
f1decaee6157 Improve tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 259
diff changeset
200 char *i_fmts[] = { "%d", "%x", "%05d", "%5d", "%-5d", "%05x", "%5x", "", };
255
7549e279fe18 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 254
diff changeset
201 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
202
3d1e2af4e4e6 Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
203 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
204 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
205 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
206
261
f1decaee6157 Improve tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 259
diff changeset
207 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
208 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
209
3d1e2af4e4e6 Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
210 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
211 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
212 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
213
257
d56369f65941 Add test for '%c'.
Matti Hamalainen <ccr@tnsp.org>
parents: 255
diff changeset
214 test_snprintf("a%cBC", 'x');
261
f1decaee6157 Improve tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 259
diff changeset
215 test_snprintf("%c", 'x');
f1decaee6157 Improve tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 259
diff changeset
216 test_snprintf("", 'x');
257
d56369f65941 Add test for '%c'.
Matti Hamalainen <ccr@tnsp.org>
parents: 255
diff changeset
217
263
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
218 //
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
219 // String matching functions
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
220 //
264
9b6fd2953d8c Add test header printing function.
Matti Hamalainen <ccr@tnsp.org>
parents: 263
diff changeset
221 tests_header("String matching");
263
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
222
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
223 #define TEST2(fun, str1, str2, ret) do { \
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
224 test_start(# fun "('%s', '%s')", str1, str2); \
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
225 test_result(( fun (str1, str2) == 0) == ret); \
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
226 } while (0)
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
227
266
3a020aa05f28 More tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
228 #define TEST2B(fun, str1, str2, ret) do { \
3a020aa05f28 More tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
229 test_start(# fun "('%s', '%s')", str1, str2); \
3a020aa05f28 More tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
230 test_result( fun (str1, str2) == ret); \
3a020aa05f28 More tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
231 } while (0)
3a020aa05f28 More tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
232
263
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
233 #define TEST3(fun, str1, str2, len, ret) do { \
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
234 test_start(# fun "('%s', '%s', %d)", str1, str2, len); \
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
235 test_result(( fun (str1, str2, len) == 0) == ret); \
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
236 } while (0)
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
237
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
238 TEST2(th_strcasecmp, "aSdFq", "asdfq", TRUE);
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
239 TEST2(th_strcasecmp, "aSdFq", "asFfq", FALSE);
265
effb2786f6a6 More work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 264
diff changeset
240 TEST2(th_strcasecmp, "abcde", "abcde", TRUE);
effb2786f6a6 More work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 264
diff changeset
241 TEST2(th_strcasecmp, "öäå", "öäå", TRUE);
effb2786f6a6 More work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 264
diff changeset
242 TEST2(th_strcasecmp, "aöäå", "aöäå", TRUE);
effb2786f6a6 More work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 264
diff changeset
243
263
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
244 TEST3(th_strncasecmp, "aSdFq", "asFfq", 4, FALSE);
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
245 TEST3(th_strncasecmp, "aSdFq", "asFfq", 2, TRUE);
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
246 TEST3(th_strncasecmp, "aSdFq", "asDfq", 3, TRUE);
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
247 TEST3(th_strncasecmp, "aSdFq", "asDfq", 2, TRUE);
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
248 TEST3(th_strncasecmp, "aSdFq", "asDfq", 0, TRUE);
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
249 TEST3(th_strncasecmp, "aSdFq", "QsDfq", 0, TRUE);
423771158575 Add more tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 261
diff changeset
250 TEST3(th_strncasecmp, "aSdFq", "QsDfq", 1, FALSE);
255
7549e279fe18 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 254
diff changeset
251
266
3a020aa05f28 More tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
252 TEST2B(th_strmatch, "abba ABBAkukka lol", "*lol", TRUE);
3a020aa05f28 More tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
253 TEST2B(th_strmatch, "abba ABBAkukka lol", "*lo*", TRUE);
3a020aa05f28 More tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
254 TEST2B(th_strmatch, "abba ABBAkukka lol", "*lo", FALSE);
3a020aa05f28 More tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
255 TEST2B(th_strmatch, "abba ABBAkukka lol", "abba", FALSE);
3a020aa05f28 More tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
256 TEST2B(th_strmatch, "abba ABBAkukka lol", "abba*", TRUE);
3a020aa05f28 More tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
257 TEST2B(th_strmatch, "abba ABBAkukka lol", "abbak*", FALSE);
3a020aa05f28 More tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
258 TEST2B(th_strcasematch, "abba ABBAkukka lol", "abbak*", FALSE);
3a020aa05f28 More tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
259 TEST2B(th_strcasematch, "abba ABBAkukka lol", "*abbak*", TRUE);
3a020aa05f28 More tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
260 TEST2B(th_strcasematch, "abba ABBAkukka lol", "*abbak?", FALSE);
3a020aa05f28 More tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
261 TEST2B(th_strcasematch, "abba ABBAkukka lol", "?bba?abba*", TRUE);
272
7f01ad43d257 Tests output changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 267
diff changeset
262 TEST2B(th_strmatch, "abba ABBAöökukka lol", "*abbaö?", FALSE);
266
3a020aa05f28 More tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
263
265
effb2786f6a6 More work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 264
diff changeset
264 // Tests that test for things that do not work correctly yet
effb2786f6a6 More work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 264
diff changeset
265 // Unicode / multibyte UTF-8 causes problems here
272
7f01ad43d257 Tests output changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 267
diff changeset
266 tests_header("Invalid");
265
effb2786f6a6 More work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 264
diff changeset
267 TEST2(th_strcasecmp, "ÖÄÅ", "öäå", FALSE); // SHOULD match
effb2786f6a6 More work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 264
diff changeset
268 TEST3(th_strncasecmp, "Aäöå", "aöå", 2, TRUE); // should NOT match
effb2786f6a6 More work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 264
diff changeset
269
267
03c327ff9203 More tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 266
diff changeset
270 TEST2B(th_strmatch, "öriÖRI! lol", "?ri?RI!*", FALSE); // should match
03c327ff9203 More tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 266
diff changeset
271
266
3a020aa05f28 More tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
272
255
7549e279fe18 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 254
diff changeset
273 //
7549e279fe18 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 254
diff changeset
274 // Print summary and exit
7549e279fe18 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 254
diff changeset
275 //
272
7f01ad43d257 Tests output changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 267
diff changeset
276 THPRINT(0,
7f01ad43d257 Tests output changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 267
diff changeset
277 "======================================================\n"
7f01ad43d257 Tests output changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 267
diff changeset
278 "%d tests failed, %d passed (%d main tests), %d test sets.\n",
7f01ad43d257 Tests output changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 267
diff changeset
279 tests_failed, tests_passed, tests_total, tests_set);
255
7549e279fe18 Work on tests.
Matti Hamalainen <ccr@tnsp.org>
parents: 254
diff changeset
280
254
3d1e2af4e4e6 Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
281 return 0;
3d1e2af4e4e6 Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
282 }