[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 | |
---|
[a6065b9] | 8 | #ifdef HAVE_UNISTD_H |
---|
| 9 | #include <unistd.h> // unlink, close |
---|
| 10 | #endif |
---|
| 11 | |
---|
[abe67e1] | 12 | #ifdef HAVE_LIMITS_H |
---|
| 13 | #include <limits.h> // PATH_MAX |
---|
| 14 | #endif /* HAVE_LIMITS_H */ |
---|
| 15 | #ifndef PATH_MAX |
---|
| 16 | #define PATH_MAX 1024 |
---|
| 17 | #endif |
---|
| 18 | |
---|
[dc08c4f] | 19 | #ifdef HAVE_C99_VARARGS_MACROS |
---|
| 20 | #define PRINT_ERR(...) fprintf(stderr, "AUBIO-TESTS ERROR: " __VA_ARGS__) |
---|
| 21 | #define PRINT_MSG(...) fprintf(stdout, __VA_ARGS__) |
---|
| 22 | #define PRINT_DBG(...) fprintf(stderr, __VA_ARGS__) |
---|
| 23 | #define PRINT_WRN(...) fprintf(stderr, "AUBIO-TESTS WARNING: " __VA_ARGS__) |
---|
| 24 | #else |
---|
[474a573] | 25 | #define PRINT_ERR(format, args...) fprintf(stderr, "AUBIO-TESTS ERROR: " format , ##args) |
---|
[248da64] | 26 | #define PRINT_MSG(format, args...) fprintf(stdout, format , ##args) |
---|
| 27 | #define PRINT_DBG(format, args...) fprintf(stderr, format , ##args) |
---|
[474a573] | 28 | #define PRINT_WRN(format, args...) fprintf(stderr, "AUBIO-TESTS WARNING: " format, ##args) |
---|
[dc08c4f] | 29 | #endif |
---|
[474a573] | 30 | |
---|
[8b42c7d] | 31 | #ifndef M_PI |
---|
| 32 | #define M_PI (3.14159265358979323846) |
---|
| 33 | #endif |
---|
| 34 | |
---|
| 35 | #ifndef RAND_MAX |
---|
| 36 | #define RAND_MAX 32767 |
---|
| 37 | #endif |
---|
| 38 | |
---|
[da632d3] | 39 | // are we on windows ? or are we using -std=c99 ? |
---|
| 40 | #if defined(HAVE_WIN_HACKS) || defined(__STRICT_ANSI__) |
---|
[06dba46] | 41 | // http://en.wikipedia.org/wiki/Linear_congruential_generator |
---|
| 42 | // no srandom/random on win32 |
---|
| 43 | |
---|
| 44 | uint_t srandom_seed = 1029; |
---|
| 45 | |
---|
| 46 | void srandom(uint_t new_seed) { |
---|
| 47 | srandom_seed = new_seed; |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | uint_t random(void) { |
---|
| 51 | srandom_seed = 1664525 * srandom_seed + 1013904223; |
---|
| 52 | return srandom_seed; |
---|
| 53 | } |
---|
| 54 | #endif |
---|
| 55 | |
---|
[158e031] | 56 | void utils_init_random (void); |
---|
| 57 | |
---|
| 58 | void utils_init_random (void) { |
---|
[474a573] | 59 | time_t now = time(0); |
---|
| 60 | struct tm *tm_struct = localtime(&now); |
---|
| 61 | int seed = tm_struct->tm_sec; |
---|
| 62 | //PRINT_WRN("current seed: %d\n", seed); |
---|
| 63 | srandom (seed); |
---|
| 64 | } |
---|
[abe67e1] | 65 | |
---|
[aa15080] | 66 | // create_temp_sink / close_temp_sink |
---|
| 67 | #if defined(__GNUC__) // mkstemp |
---|
| 68 | |
---|
| 69 | int create_temp_sink(char *sink_path) |
---|
| 70 | { |
---|
| 71 | return mkstemp(sink_path); |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | int close_temp_sink(char *sink_path, int sink_fildes) |
---|
| 75 | { |
---|
| 76 | int err; |
---|
| 77 | if ((err = close(sink_fildes)) != 0) return err; |
---|
| 78 | if ((err = unlink(sink_path)) != 0) return err; |
---|
| 79 | return err; |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | #elif (defined(HAVE_WIN_HACKS) //&& !defined(__GNUC__)) |
---|
| 83 | // windows workaround, where mkstemp does not exist... |
---|
| 84 | int create_temp_sink(char *templ) |
---|
| 85 | { |
---|
| 86 | int i = 0; |
---|
| 87 | static const char letters[] = "abcdefg0123456789"; |
---|
| 88 | int letters_len = strlen(letters); |
---|
| 89 | int templ_len = strlen(templ); |
---|
| 90 | if (templ_len == 0) return 0; |
---|
| 91 | utils_init_random(); |
---|
| 92 | for (i = 0; i < 6; i++) |
---|
| 93 | { |
---|
| 94 | templ[templ_len - i] = letters[rand() % letters_len]; |
---|
| 95 | } |
---|
| 96 | return 1; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | int close_temp_sink(char* sink_path, int sink_fildes) { |
---|
| 100 | // the file should be closed when not using mkstemp, no need to open it |
---|
| 101 | if (sink_fildes == 0) return 1; |
---|
| 102 | return _unlink(sink_path); |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | #else // windows workaround |
---|
| 106 | // otherwise, we don't really know what to do yet |
---|
| 107 | #error "mkstemp undefined, but not on windows. additional workaround required." |
---|
| 108 | #endif |
---|
| 109 | |
---|
| 110 | |
---|
[abe67e1] | 111 | int run_on_default_sink( int main(int, char**) ) |
---|
| 112 | { |
---|
| 113 | int argc = 2, err; |
---|
| 114 | char* argv[argc]; |
---|
| 115 | char sink_path[PATH_MAX] = "tmp_aubio_XXXXXX"; |
---|
[aa15080] | 116 | int fd = create_temp_sink(sink_path); |
---|
[abe67e1] | 117 | if (!fd) return 1; |
---|
[aa15080] | 118 | argv[0] = __FILE__; |
---|
[abe67e1] | 119 | argv[1] = sink_path; |
---|
| 120 | err = main(argc, argv); |
---|
[aa15080] | 121 | close_temp_sink(sink_path, fd); |
---|
[abe67e1] | 122 | return err; |
---|
| 123 | } |
---|