13 #include <unistd.h> // unlink, close
17 #include <limits.h> // PATH_MAX
18 #endif /* HAVE_LIMITS_H */
23 #if defined(HAVE_WIN_HACKS) && !defined(__GNUC__)
24 #include <io.h> // _access
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
31 #define REDEFINESTRING(x) #x
32 #define DEFINEDSTRING(x) REDEFINESTRING(x)
34 #ifndef AUBIO_TESTS_SOURCE
35 #error "AUBIO_TESTS_SOURCE is not defined"
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__)
44 #define PRINT_ERR(format, args...) fprintf(stderr, "AUBIO-TESTS ERROR: " format , ##args)
45 #define PRINT_MSG(format, args...) fprintf(stdout, format , ##args)
46 #define PRINT_DBG(format, args...) fprintf(stderr, format , ##args)
47 #define PRINT_WRN(format, args...) fprintf(stderr, "AUBIO-TESTS WARNING: " format, ##args)
51 #define M_PI (3.14159265358979323846)
55 #define RAND_MAX 32767
58 // are we on windows ? or are we using -std=c99 ?
59 #if defined(HAVE_WIN_HACKS) || defined(__STRICT_ANSI__)
60 // http://en.wikipedia.org/wiki/Linear_congruential_generator
61 // no srandom/random on win32
63 uint_t srandom_seed = 1029;
65 void srandom(uint_t new_seed) {
66 srandom_seed = new_seed;
70 srandom_seed = 1664525 * srandom_seed + 1013904223;
75 void utils_init_random (void);
77 void utils_init_random (void) {
79 struct tm *tm_struct = localtime(&now);
80 size_t **tm_address = (void*)&tm_struct;
81 int seed = tm_struct->tm_sec + (size_t)tm_address;
82 //PRINT_WRN("current seed: %d\n", seed);
83 srandom ((unsigned int)seed);
86 // create_temp_sink / close_temp_sink
87 #if defined(__GNUC__) // mkstemp
89 int check_source(char *source_path)
91 return access(source_path, R_OK);
94 int create_temp_sink(char *sink_path)
96 return mkstemp(sink_path);
99 int close_temp_sink(char *sink_path, int sink_fildes)
102 if ((err = close(sink_fildes)) != 0) return err;
103 if ((err = unlink(sink_path)) != 0) return err;
107 #elif defined(HAVE_WIN_HACKS) //&& !defined(__GNUC__)
108 // windows workaround, where mkstemp does not exist...
110 int check_source(char *source_path)
112 return _access(source_path, 04);
115 int create_temp_sink(char *templ)
118 static const char letters[] = "abcdefg0123456789";
119 int letters_len = strlen(letters);
120 int templ_len = strlen(templ);
121 if (templ_len == 0) return 0;
123 for (i = 0; i < 6; i++)
125 templ[templ_len - i] = letters[rand() % letters_len];
130 int close_temp_sink(char* sink_path, int sink_fildes) {
131 // the file should be closed when not using mkstemp, no need to open it
132 if (sink_fildes == 0) return 1;
133 return _unlink(sink_path);
136 #else // windows workaround
137 // otherwise, we don't really know what to do yet
138 #error "mkstemp undefined, but not on windows. additional workaround required."
141 // pass progname / default
142 int run_on_default_source( int main(int, char**) )
146 char** argv = (char**)calloc(argc, sizeof(char*));
148 argv[1] = DEFINEDSTRING(AUBIO_TESTS_SOURCE);
149 // check if the file can be read
150 if ( check_source(argv[1]) ) return 1;
151 err = main(argc, argv);
152 if (argv) free(argv);
156 int run_on_default_sink( int main(int, char**) )
160 char** argv = (char**)calloc(argc, sizeof(char*));
161 char sink_path[PATH_MAX] = "tmp_aubio_XXXXXX";
162 int fd = create_temp_sink(sink_path);
166 err = main(argc, argv);
167 close_temp_sink(sink_path, fd);
168 if (argv) free(argv);
172 int run_on_default_source_and_sink( int main(int, char**) )
176 char** argv = (char**)calloc(argc, sizeof(char*));
177 char sink_path[PATH_MAX] = "tmp_aubio_XXXXXX";
178 int fd = create_temp_sink(sink_path);
181 argv[1] = DEFINEDSTRING(AUBIO_TESTS_SOURCE);
183 // check if the file can be read
184 if ( check_source(argv[1]) ) return 1;
185 err = main(argc, argv);
186 close_temp_sink(sink_path, fd);
187 if (argv) free(argv);