Toby Gray | 21cf6e4 | 2012-11-21 14:00:31 +0000 | [diff] [blame] | 1 | /* |
hjelmn@cs.unm.edu | 1eff220 | 2014-01-08 23:50:34 +0000 | [diff] [blame] | 2 | * libusb test library helper functions |
Toby Gray | 21cf6e4 | 2012-11-21 14:00:31 +0000 | [diff] [blame] | 3 | * Copyright © 2012 Toby Gray <toby.gray@realvnc.com> |
| 4 | * |
| 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. |
| 9 | * |
| 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. |
| 14 | * |
| 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 |
| 18 | */ |
| 19 | |
hjelmn@cs.unm.edu | 1eff220 | 2014-01-08 23:50:34 +0000 | [diff] [blame] | 20 | #ifndef LIBUSB_TESTLIB_H |
| 21 | #define LIBUSB_TESTLIB_H |
Toby Gray | 21cf6e4 | 2012-11-21 14:00:31 +0000 | [diff] [blame] | 22 | |
| 23 | #include <stdio.h> |
| 24 | |
| 25 | #if !defined(bool) |
| 26 | #define bool int |
| 27 | #endif |
| 28 | #if !defined(true) |
| 29 | #define true (1 == 1) |
| 30 | #endif |
| 31 | #if !defined(false) |
| 32 | #define false (!true) |
| 33 | #endif |
| 34 | |
| 35 | /** Values returned from a test function to indicate test result */ |
| 36 | typedef enum { |
| 37 | /** Indicates that the test ran successfully. */ |
| 38 | TEST_STATUS_SUCCESS, |
| 39 | /** Indicates that the test failed one or more test. */ |
| 40 | TEST_STATUS_FAILURE, |
| 41 | /** Indicates that an unexpected error occurred. */ |
| 42 | TEST_STATUS_ERROR, |
| 43 | /** Indicates that the test can't be run. For example this may be |
| 44 | * due to no suitable device being connected to perform the tests.*/ |
| 45 | TEST_STATUS_SKIP |
hjelmn@cs.unm.edu | 1eff220 | 2014-01-08 23:50:34 +0000 | [diff] [blame] | 46 | } libusb_testlib_result; |
Toby Gray | 21cf6e4 | 2012-11-21 14:00:31 +0000 | [diff] [blame] | 47 | |
| 48 | /** |
| 49 | * Context for test library functions |
| 50 | */ |
| 51 | typedef struct { |
| 52 | char ** test_names; |
| 53 | int test_count; |
| 54 | bool list_tests; |
| 55 | bool verbose; |
Pete Batard | 76eecc6 | 2013-02-21 00:24:18 +0000 | [diff] [blame] | 56 | int old_stdout; |
| 57 | int old_stderr; |
Toby Gray | 21cf6e4 | 2012-11-21 14:00:31 +0000 | [diff] [blame] | 58 | FILE* output_file; |
| 59 | int null_fd; |
hjelmn@cs.unm.edu | 1eff220 | 2014-01-08 23:50:34 +0000 | [diff] [blame] | 60 | } libusb_testlib_ctx; |
Toby Gray | 21cf6e4 | 2012-11-21 14:00:31 +0000 | [diff] [blame] | 61 | |
| 62 | /** |
| 63 | * Logs some test information or state |
| 64 | */ |
hjelmn@cs.unm.edu | 1eff220 | 2014-01-08 23:50:34 +0000 | [diff] [blame] | 65 | void libusb_testlib_logf(libusb_testlib_ctx * ctx, |
Toby Gray | 21cf6e4 | 2012-11-21 14:00:31 +0000 | [diff] [blame] | 66 | const char* fmt, ...); |
| 67 | |
| 68 | /** |
hjelmn@cs.unm.edu | 1eff220 | 2014-01-08 23:50:34 +0000 | [diff] [blame] | 69 | * Function pointer for a libusb test function. |
Toby Gray | 21cf6e4 | 2012-11-21 14:00:31 +0000 | [diff] [blame] | 70 | * |
| 71 | * Should return TEST_STATUS_SUCCESS on success or another TEST_STATUS value. |
| 72 | */ |
hjelmn@cs.unm.edu | 1eff220 | 2014-01-08 23:50:34 +0000 | [diff] [blame] | 73 | typedef libusb_testlib_result |
| 74 | (*libusb_testlib_test_function)(libusb_testlib_ctx * ctx); |
Toby Gray | 21cf6e4 | 2012-11-21 14:00:31 +0000 | [diff] [blame] | 75 | |
| 76 | /** |
| 77 | * Structure holding a test description. |
| 78 | */ |
| 79 | typedef struct { |
| 80 | /** Human readable name of the test. */ |
| 81 | const char * name; |
| 82 | /** The test library will call this function to run the test. */ |
hjelmn@cs.unm.edu | 1eff220 | 2014-01-08 23:50:34 +0000 | [diff] [blame] | 83 | libusb_testlib_test_function function; |
| 84 | } libusb_testlib_test; |
Toby Gray | 21cf6e4 | 2012-11-21 14:00:31 +0000 | [diff] [blame] | 85 | |
| 86 | /** |
| 87 | * Value to use at the end of a test array to indicate the last |
| 88 | * element. |
| 89 | */ |
hjelmn@cs.unm.edu | 1eff220 | 2014-01-08 23:50:34 +0000 | [diff] [blame] | 90 | #define LIBUSB_NULL_TEST {NULL, NULL} |
Toby Gray | 21cf6e4 | 2012-11-21 14:00:31 +0000 | [diff] [blame] | 91 | |
| 92 | /** |
| 93 | * Runs the tests provided. |
| 94 | * |
| 95 | * Before running any tests argc and argv will be processed |
| 96 | * to determine the mode of operation. |
| 97 | * |
| 98 | * \param argc The argc from main |
| 99 | * \param argv The argv from main |
| 100 | * \param tests A NULL_TEST terminated array of tests |
| 101 | * \return 0 on success, non-zero on failure |
| 102 | */ |
hjelmn@cs.unm.edu | 1eff220 | 2014-01-08 23:50:34 +0000 | [diff] [blame] | 103 | int libusb_testlib_run_tests(int argc, |
Toby Gray | 21cf6e4 | 2012-11-21 14:00:31 +0000 | [diff] [blame] | 104 | char ** argv, |
hjelmn@cs.unm.edu | 1eff220 | 2014-01-08 23:50:34 +0000 | [diff] [blame] | 105 | const libusb_testlib_test * tests); |
Toby Gray | 21cf6e4 | 2012-11-21 14:00:31 +0000 | [diff] [blame] | 106 | |
hjelmn@cs.unm.edu | 1eff220 | 2014-01-08 23:50:34 +0000 | [diff] [blame] | 107 | #endif //LIBUSB_TESTLIB_H |