2 * libusb test library helper functions
3 * Copyright © 2012 Toby Gray <toby.gray@realvnc.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 #include "libusb_testlib.h"
30 #if defined(PLATFORM_POSIX)
31 #define NULL_PATH "/dev/null"
32 #elif defined(PLATFORM_WINDOWS)
33 #define NULL_PATH "nul"
37 * Converts a test result code into a human readable string.
39 static const char *test_result_to_str(libusb_testlib_result result)
42 case TEST_STATUS_SUCCESS:
44 case TEST_STATUS_FAILURE:
46 case TEST_STATUS_ERROR:
48 case TEST_STATUS_SKIP:
55 static void print_usage(const char *progname)
57 printf("Usage: %s [-l] [-v] [<test_name> ...]\n", progname);
58 printf(" -l List available tests\n");
59 printf(" -v Don't redirect STDERR before running tests\n");
60 printf(" -h Display this help and exit\n");
63 void libusb_testlib_logf(const char *fmt, ...)
68 vfprintf(stdout, fmt, va);
74 int libusb_testlib_run_tests(int argc, char *argv[],
75 const libusb_testlib_test *tests)
84 /* Setup default mode of operation */
85 char **test_names = NULL;
87 bool list_tests = false;
90 /* Parse command line options */
92 for (int j = 1; j < argc; j++) {
93 const char *argstr = argv[j];
94 size_t arglen = strlen(argstr);
96 if (argstr[0] == '-' || argstr[0] == '/') {
106 print_usage(argv[0]);
111 fprintf(stderr, "Unknown option: '%s'\n", argstr);
112 print_usage(argv[0]);
115 /* End of command line options, remaining must be list of tests to run */
116 test_names = argv + j;
117 test_count = argc - j;
123 /* Validate command line options */
124 if (test_names && list_tests) {
125 fprintf(stderr, "List of tests requested but test list provided\n");
126 print_usage(argv[0]);
130 /* Setup test log output */
132 if (!freopen(NULL_PATH, "w", stderr)) {
133 printf("Failed to open null handle: %d\n", errno);
138 /* Act on any options not related to running tests */
140 while (tests[idx].function)
141 libusb_testlib_logf("%s", tests[idx++].name);
145 /* Run any requested tests */
146 while (tests[idx].function) {
147 const libusb_testlib_test *test = &tests[idx++];
148 libusb_testlib_result test_result;
150 if (test_count > 0) {
151 /* Filtering tests to run, check if this is one of them */
154 for (i = 0; i < test_count; i++) {
155 if (!strcmp(test_names[i], test->name))
156 /* Matches a requested test name */
159 if (i == test_count) {
160 /* Failed to find a test match, so do the next loop iteration */
164 libusb_testlib_logf("Starting test run: %s...", test->name);
165 test_result = test->function();
166 libusb_testlib_logf("%s (%d)", test_result_to_str(test_result), test_result);
167 switch (test_result) {
168 case TEST_STATUS_SUCCESS: pass_count++; break;
169 case TEST_STATUS_FAILURE: fail_count++; break;
170 case TEST_STATUS_ERROR: error_count++; break;
171 case TEST_STATUS_SKIP: skip_count++; break;
176 libusb_testlib_logf("---");
177 libusb_testlib_logf("Ran %d tests", run_count);
178 libusb_testlib_logf("Passed %d tests", pass_count);
179 libusb_testlib_logf("Failed %d tests", fail_count);
180 libusb_testlib_logf("Error in %d tests", error_count);
181 libusb_testlib_logf("Skipped %d tests", skip_count);
183 return pass_count != run_count;