e272c2ad1380075cb26fa8d4e0ce03f00f382261
[aubio.git] / tests / utils_tests.h
1 #include <time.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <math.h>
5 #include <assert.h>
6 #include "config.h"
7
8 #ifdef HAVE_STRING_H
9 #include <string.h>
10 #endif
11
12 #ifdef HAVE_UNISTD_H
13 #include <unistd.h> // unlink, close
14 #endif
15
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
23 // This macro is used to pass a string to msvc compiler: since msvc's -D flag
24 // strips the quotes, we define the string without quotes and re-add them with
25 // this macro.
26
27 #define REDEFINESTRING(x) #x
28 #define DEFINEDSTRING(x) REDEFINESTRING(x)
29
30 #ifndef AUBIO_TESTS_SOURCE
31 #error "AUBIO_TESTS_SOURCE is not defined"
32 #endif
33
34 #ifdef HAVE_C99_VARARGS_MACROS
35 #define PRINT_ERR(...)   fprintf(stderr, "AUBIO-TESTS ERROR: " __VA_ARGS__)
36 #define PRINT_MSG(...)   fprintf(stdout, __VA_ARGS__)
37 #define PRINT_DBG(...)   fprintf(stderr, __VA_ARGS__)
38 #define PRINT_WRN(...)   fprintf(stderr, "AUBIO-TESTS WARNING: " __VA_ARGS__)
39 #else
40 #define PRINT_ERR(format, args...)   fprintf(stderr, "AUBIO-TESTS ERROR: " format , ##args)
41 #define PRINT_MSG(format, args...)   fprintf(stdout, format , ##args)
42 #define PRINT_DBG(format, args...)   fprintf(stderr, format , ##args)
43 #define PRINT_WRN(format, args...)   fprintf(stderr, "AUBIO-TESTS WARNING: " format, ##args)
44 #endif
45
46 #ifndef M_PI
47 #define M_PI         (3.14159265358979323846)
48 #endif
49
50 #ifndef RAND_MAX
51 #define RAND_MAX 32767
52 #endif
53
54 // are we on windows ? or are we using -std=c99 ?
55 #if defined(HAVE_WIN_HACKS) || defined(__STRICT_ANSI__)
56 // http://en.wikipedia.org/wiki/Linear_congruential_generator
57 // no srandom/random on win32
58
59 uint_t srandom_seed = 1029;
60
61 void srandom(uint_t new_seed) {
62     srandom_seed = new_seed;
63 }
64
65 uint_t random(void) {
66     srandom_seed = 1664525 * srandom_seed + 1013904223;
67     return srandom_seed;
68 }
69 #endif
70
71 void utils_init_random (void);
72
73 void utils_init_random (void) {
74   time_t now = time(0);
75   struct tm *tm_struct = localtime(&now);
76   size_t **tm_address = (void*)&tm_struct;
77   int seed = tm_struct->tm_sec + (size_t)tm_address;
78   //PRINT_WRN("current seed: %d\n", seed);
79   srandom ((unsigned int)seed);
80 }
81
82 // create_temp_sink / close_temp_sink
83 #if defined(__GNUC__) // mkstemp
84
85 int check_source(char *source_path)
86 {
87   return access(source_path, R_OK);
88 }
89
90 int create_temp_sink(char *sink_path)
91 {
92   return mkstemp(sink_path);
93 }
94
95 int close_temp_sink(char *sink_path, int sink_fildes)
96 {
97   int err;
98   if ((err = close(sink_fildes)) != 0) return err;
99   if ((err = unlink(sink_path)) != 0) return err;
100   return err;
101 }
102
103 #elif defined(HAVE_WIN_HACKS) //&& !defined(__GNUC__)
104 // windows workaround, where mkstemp does not exist...
105
106 int check_source(char *source_path)
107 {
108   return _access(source_path, 04);
109 }
110
111 int create_temp_sink(char *templ)
112 {
113   int i = 0;
114   static const char letters[] = "abcdefg0123456789";
115   int letters_len = strlen(letters);
116   int templ_len = strlen(templ);
117   if (templ_len == 0) return 0;
118   utils_init_random();
119   for (i = 0; i < 6; i++)
120   {
121     templ[templ_len - i] = letters[rand() % letters_len];
122   }
123   return 1;
124 }
125
126 int close_temp_sink(char* sink_path, int sink_fildes) {
127   // the file should be closed when not using mkstemp, no need to open it
128   if (sink_fildes == 0) return 1;
129   return _unlink(sink_path);
130 }
131
132 #else // windows workaround
133 // otherwise, we don't really know what to do yet
134 #error "mkstemp undefined, but not on windows. additional workaround required."
135 #endif
136
137 // pass progname / default
138 int run_on_default_source( int main(int, char**) )
139 {
140   const int argc = 2;
141   int err = 0;
142   char** argv = (char**)calloc(argc, sizeof(char*));
143   argv[0] = __FILE__;
144   argv[1] = DEFINEDSTRING(AUBIO_TESTS_SOURCE);
145   // check if the file can be read
146   if ( check_source(argv[1]) ) return 1;
147   err = main(argc, argv);
148   if (argv) free(argv);
149   return err;
150 }
151
152 int run_on_default_sink( int main(int, char**) )
153 {
154   const int argc = 2;
155   int err = 0;
156   char** argv = (char**)calloc(argc, sizeof(char*));
157   char sink_path[PATH_MAX] = "tmp_aubio_XXXXXX";
158   int fd = create_temp_sink(sink_path);
159   if (!fd) return 1;
160   argv[0] = __FILE__;
161   argv[1] = sink_path;
162   err = main(argc, argv);
163   close_temp_sink(sink_path, fd);
164   if (argv) free(argv);
165   return err;
166 }