[474a573] | 1 | #include <time.h> |
---|
[248da64] | 2 | #include <stdlib.h> |
---|
| 3 | #include <stdio.h> |
---|
[474a573] | 4 | #include <math.h> |
---|
| 5 | #include <assert.h> |
---|
[06dba46] | 6 | #include "config.h" |
---|
[248da64] | 7 | |
---|
[d22b568] | 8 | #ifdef HAVE_STRING_H |
---|
| 9 | #include <string.h> |
---|
| 10 | #endif |
---|
| 11 | |
---|
[a6065b9] | 12 | #ifdef HAVE_UNISTD_H |
---|
| 13 | #include <unistd.h> // unlink, close |
---|
| 14 | #endif |
---|
| 15 | |
---|
[abe67e1] | 16 | #ifdef HAVE_LIMITS_H |
---|
| 17 | #include <limits.h> // PATH_MAX |
---|
| 18 | #endif /* HAVE_LIMITS_H */ |
---|
| 19 | #ifndef PATH_MAX |
---|
| 20 | #define PATH_MAX 1024 |
---|
| 21 | #endif |
---|
| 22 | |
---|
[752ce6d] | 23 | #if defined(HAVE_WIN_HACKS) && !defined(__GNUC__) |
---|
| 24 | #include <io.h> // _access |
---|
| 25 | #endif |
---|
| 26 | |
---|
[586383d] | 27 | // This macro is used to pass a string to msvc compiler: since msvc's -D flag |
---|
| 28 | // strips the quotes, we define the string without quotes and re-add them with |
---|
| 29 | // this macro. |
---|
| 30 | |
---|
| 31 | #define REDEFINESTRING(x) #x |
---|
| 32 | #define DEFINEDSTRING(x) REDEFINESTRING(x) |
---|
| 33 | |
---|
[c6bb567a] | 34 | #ifndef AUBIO_TESTS_SOURCE |
---|
| 35 | #error "AUBIO_TESTS_SOURCE is not defined" |
---|
| 36 | #endif |
---|
| 37 | |
---|
[dc08c4f] | 38 | #ifdef HAVE_C99_VARARGS_MACROS |
---|
| 39 | #define PRINT_ERR(...) fprintf(stderr, "AUBIO-TESTS ERROR: " __VA_ARGS__) |
---|
| 40 | #define PRINT_MSG(...) fprintf(stdout, __VA_ARGS__) |
---|
| 41 | #define PRINT_DBG(...) fprintf(stderr, __VA_ARGS__) |
---|
| 42 | #define PRINT_WRN(...) fprintf(stderr, "AUBIO-TESTS WARNING: " __VA_ARGS__) |
---|
| 43 | #else |
---|
[474a573] | 44 | #define PRINT_ERR(format, args...) fprintf(stderr, "AUBIO-TESTS ERROR: " format , ##args) |
---|
[248da64] | 45 | #define PRINT_MSG(format, args...) fprintf(stdout, format , ##args) |
---|
| 46 | #define PRINT_DBG(format, args...) fprintf(stderr, format , ##args) |
---|
[474a573] | 47 | #define PRINT_WRN(format, args...) fprintf(stderr, "AUBIO-TESTS WARNING: " format, ##args) |
---|
[dc08c4f] | 48 | #endif |
---|
[474a573] | 49 | |
---|
[8b42c7d] | 50 | #ifndef M_PI |
---|
| 51 | #define M_PI (3.14159265358979323846) |
---|
| 52 | #endif |
---|
| 53 | |
---|
| 54 | #ifndef RAND_MAX |
---|
| 55 | #define RAND_MAX 32767 |
---|
| 56 | #endif |
---|
| 57 | |
---|
[0512fca] | 58 | #if defined(HAVE_WIN_HACKS) |
---|
[06dba46] | 59 | |
---|
[0512fca] | 60 | // use srand/rand on windows |
---|
| 61 | #define srandom srand |
---|
| 62 | #define random rand |
---|
[06dba46] | 63 | |
---|
[0512fca] | 64 | #elif defined(__STRICT_ANSI__) |
---|
| 65 | |
---|
| 66 | // workaround to build with -std=c99 (for instance with older cygwin), |
---|
| 67 | // assuming libbc is recent enough to supports these functions. |
---|
| 68 | extern void srandom(unsigned); |
---|
| 69 | extern int random(void); |
---|
| 70 | extern char mkstemp(const char *pat); |
---|
[06dba46] | 71 | |
---|
| 72 | #endif |
---|
| 73 | |
---|
[158e031] | 74 | void utils_init_random (void); |
---|
| 75 | |
---|
| 76 | void utils_init_random (void) { |
---|
[474a573] | 77 | time_t now = time(0); |
---|
| 78 | struct tm *tm_struct = localtime(&now); |
---|
[cb288f2] | 79 | size_t **tm_address = (void*)&tm_struct; |
---|
| 80 | int seed = tm_struct->tm_sec + (size_t)tm_address; |
---|
[474a573] | 81 | //PRINT_WRN("current seed: %d\n", seed); |
---|
[e7edf92] | 82 | srandom ((unsigned int)seed); |
---|
[474a573] | 83 | } |
---|
[abe67e1] | 84 | |
---|
[aa15080] | 85 | // create_temp_sink / close_temp_sink |
---|
| 86 | #if defined(__GNUC__) // mkstemp |
---|
| 87 | |
---|
[c6bb567a] | 88 | int check_source(char *source_path) |
---|
| 89 | { |
---|
| 90 | return access(source_path, R_OK); |
---|
| 91 | } |
---|
| 92 | |
---|
[aa15080] | 93 | int create_temp_sink(char *sink_path) |
---|
| 94 | { |
---|
| 95 | return mkstemp(sink_path); |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | int close_temp_sink(char *sink_path, int sink_fildes) |
---|
| 99 | { |
---|
| 100 | int err; |
---|
| 101 | if ((err = close(sink_fildes)) != 0) return err; |
---|
| 102 | if ((err = unlink(sink_path)) != 0) return err; |
---|
| 103 | return err; |
---|
| 104 | } |
---|
| 105 | |
---|
[6a91a9d] | 106 | #elif defined(HAVE_WIN_HACKS) //&& !defined(__GNUC__) |
---|
[aa15080] | 107 | // windows workaround, where mkstemp does not exist... |
---|
[c6bb567a] | 108 | |
---|
| 109 | int check_source(char *source_path) |
---|
| 110 | { |
---|
| 111 | return _access(source_path, 04); |
---|
| 112 | } |
---|
| 113 | |
---|
[aa15080] | 114 | int create_temp_sink(char *templ) |
---|
| 115 | { |
---|
| 116 | int i = 0; |
---|
| 117 | static const char letters[] = "abcdefg0123456789"; |
---|
| 118 | int letters_len = strlen(letters); |
---|
| 119 | int templ_len = strlen(templ); |
---|
| 120 | if (templ_len == 0) return 0; |
---|
| 121 | utils_init_random(); |
---|
| 122 | for (i = 0; i < 6; i++) |
---|
| 123 | { |
---|
| 124 | templ[templ_len - i] = letters[rand() % letters_len]; |
---|
| 125 | } |
---|
| 126 | return 1; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | int close_temp_sink(char* sink_path, int sink_fildes) { |
---|
| 130 | // the file should be closed when not using mkstemp, no need to open it |
---|
| 131 | if (sink_fildes == 0) return 1; |
---|
| 132 | return _unlink(sink_path); |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | #else // windows workaround |
---|
| 136 | // otherwise, we don't really know what to do yet |
---|
| 137 | #error "mkstemp undefined, but not on windows. additional workaround required." |
---|
| 138 | #endif |
---|
| 139 | |
---|
[c6bb567a] | 140 | // pass progname / default |
---|
| 141 | int run_on_default_source( int main(int, char**) ) |
---|
| 142 | { |
---|
| 143 | const int argc = 2; |
---|
| 144 | int err = 0; |
---|
| 145 | char** argv = (char**)calloc(argc, sizeof(char*)); |
---|
| 146 | argv[0] = __FILE__; |
---|
[586383d] | 147 | argv[1] = DEFINEDSTRING(AUBIO_TESTS_SOURCE); |
---|
[c6bb567a] | 148 | // check if the file can be read |
---|
| 149 | if ( check_source(argv[1]) ) return 1; |
---|
| 150 | err = main(argc, argv); |
---|
| 151 | if (argv) free(argv); |
---|
| 152 | return err; |
---|
| 153 | } |
---|
[aa15080] | 154 | |
---|
[abe67e1] | 155 | int run_on_default_sink( int main(int, char**) ) |
---|
| 156 | { |
---|
[d22b568] | 157 | const int argc = 2; |
---|
| 158 | int err = 0; |
---|
[d94afb3] | 159 | char** argv = (char**)calloc(argc, sizeof(char*)); |
---|
[abe67e1] | 160 | char sink_path[PATH_MAX] = "tmp_aubio_XXXXXX"; |
---|
[aa15080] | 161 | int fd = create_temp_sink(sink_path); |
---|
[abe67e1] | 162 | if (!fd) return 1; |
---|
[aa15080] | 163 | argv[0] = __FILE__; |
---|
[abe67e1] | 164 | argv[1] = sink_path; |
---|
| 165 | err = main(argc, argv); |
---|
[aa15080] | 166 | close_temp_sink(sink_path, fd); |
---|
[d94afb3] | 167 | if (argv) free(argv); |
---|
[abe67e1] | 168 | return err; |
---|
| 169 | } |
---|
[8386dbe] | 170 | |
---|
| 171 | int run_on_default_source_and_sink( int main(int, char**) ) |
---|
| 172 | { |
---|
| 173 | const int argc = 3; |
---|
| 174 | int err = 0; |
---|
| 175 | char** argv = (char**)calloc(argc, sizeof(char*)); |
---|
| 176 | char sink_path[PATH_MAX] = "tmp_aubio_XXXXXX"; |
---|
| 177 | int fd = create_temp_sink(sink_path); |
---|
| 178 | if (!fd) return 1; |
---|
| 179 | argv[0] = __FILE__; |
---|
| 180 | argv[1] = DEFINEDSTRING(AUBIO_TESTS_SOURCE); |
---|
| 181 | argv[2] = sink_path; |
---|
| 182 | // check if the file can be read |
---|
| 183 | if ( check_source(argv[1]) ) return 1; |
---|
| 184 | err = main(argc, argv); |
---|
| 185 | close_temp_sink(sink_path, fd); |
---|
| 186 | if (argv) free(argv); |
---|
| 187 | return err; |
---|
| 188 | } |
---|