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