source: tests/utils_tests.h @ bab4611

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5
Last change on this file since bab4611 was bab4611, checked in by Paul Brossier <piem@piem.org>, 6 years ago

[tests] fix unmatched parenthesis on windows

  • Property mode set to 100644
File size: 4.3 KB
Line 
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#define DEFAULT_TEST_FILE "python/tests/sounds/44100Hz_44100f_sine441.wav"
24
25#ifdef HAVE_C99_VARARGS_MACROS
26#define PRINT_ERR(...)   fprintf(stderr, "AUBIO-TESTS ERROR: " __VA_ARGS__)
27#define PRINT_MSG(...)   fprintf(stdout, __VA_ARGS__)
28#define PRINT_DBG(...)   fprintf(stderr, __VA_ARGS__)
29#define PRINT_WRN(...)   fprintf(stderr, "AUBIO-TESTS WARNING: " __VA_ARGS__)
30#else
31#define PRINT_ERR(format, args...)   fprintf(stderr, "AUBIO-TESTS ERROR: " format , ##args)
32#define PRINT_MSG(format, args...)   fprintf(stdout, format , ##args)
33#define PRINT_DBG(format, args...)   fprintf(stderr, format , ##args)
34#define PRINT_WRN(format, args...)   fprintf(stderr, "AUBIO-TESTS WARNING: " format, ##args)
35#endif
36
37#ifndef M_PI
38#define M_PI         (3.14159265358979323846)
39#endif
40
41#ifndef RAND_MAX
42#define RAND_MAX 32767
43#endif
44
45// are we on windows ? or are we using -std=c99 ?
46#if defined(HAVE_WIN_HACKS) || defined(__STRICT_ANSI__)
47// http://en.wikipedia.org/wiki/Linear_congruential_generator
48// no srandom/random on win32
49
50uint_t srandom_seed = 1029;
51
52void srandom(uint_t new_seed) {
53    srandom_seed = new_seed;
54}
55
56uint_t random(void) {
57    srandom_seed = 1664525 * srandom_seed + 1013904223;
58    return srandom_seed;
59}
60#endif
61
62void utils_init_random (void);
63
64void utils_init_random (void) {
65  time_t now = time(0);
66  struct tm *tm_struct = localtime(&now);
67  size_t **tm_address = (void*)&tm_struct;
68  int seed = tm_struct->tm_sec + (size_t)tm_address;
69  //PRINT_WRN("current seed: %d\n", seed);
70  srandom ((unsigned int)seed);
71}
72
73// create_temp_sink / close_temp_sink
74#if defined(__GNUC__) // mkstemp
75
76int create_temp_sink(char *sink_path)
77{
78  return mkstemp(sink_path);
79}
80
81int close_temp_sink(char *sink_path, int sink_fildes)
82{
83  int err;
84  if ((err = close(sink_fildes)) != 0) return err;
85  if ((err = unlink(sink_path)) != 0) return err;
86  return err;
87}
88
89#elif defined(HAVE_WIN_HACKS) //&& !defined(__GNUC__)
90// windows workaround, where mkstemp does not exist...
91int create_temp_sink(char *templ)
92{
93  int i = 0;
94  static const char letters[] = "abcdefg0123456789";
95  int letters_len = strlen(letters);
96  int templ_len = strlen(templ);
97  if (templ_len == 0) return 0;
98  utils_init_random();
99  for (i = 0; i < 6; i++)
100  {
101    templ[templ_len - i] = letters[rand() % letters_len];
102  }
103  return 1;
104}
105
106int close_temp_sink(char* sink_path, int sink_fildes) {
107  // the file should be closed when not using mkstemp, no need to open it
108  if (sink_fildes == 0) return 1;
109  return _unlink(sink_path);
110}
111
112#else // windows workaround
113// otherwise, we don't really know what to do yet
114#error "mkstemp undefined, but not on windows. additional workaround required."
115#endif
116
117// pass progname / default
118int run_on_default_source( int main(int, char**) )
119{
120  int argc = 2;
121  char* argv[argc];
122  argv[0] = __FILE__;
123  // when running from waf build
124  argv[1] = "../../" DEFAULT_TEST_FILE;
125  // when running from source root directory
126  if ( access(argv[1], R_OK) )
127      argv[1] = DEFAULT_TEST_FILE;
128  // no file found
129  if ( access(argv[1], R_OK) != 0 )
130      return 1;
131  return main(argc, argv);
132}
133
134int run_on_default_source_and_sink( int main(int, char**) )
135{
136  int argc = 3, err;
137  char* argv[argc];
138  argv[0] = __FILE__;
139  // when running from waf build
140  argv[1] = "../../" DEFAULT_TEST_FILE;
141  // when running from source root directory
142  if ( access(argv[1], R_OK) )
143      argv[1] = DEFAULT_TEST_FILE;
144  // no file found
145  if ( access(argv[1], R_OK) != 0 )
146      return 1;
147  char sink_path[PATH_MAX] = "tmp_aubio_XXXXXX";
148  int fd = mkstemp(sink_path);
149  if (!fd) return 1;
150  argv[2] = sink_path;
151  err = main(argc, argv);
152  unlink(sink_path);
153  close(fd);
154  return err;
155}
156
157int run_on_default_sink( int main(int, char**) )
158{
159  const int argc = 2;
160  int err = 0;
161  char* argv[argc];
162  char sink_path[PATH_MAX] = "tmp_aubio_XXXXXX";
163  int fd = create_temp_sink(sink_path);
164  if (!fd) return 1;
165  argv[0] = __FILE__;
166  argv[1] = sink_path;
167  err = main(argc, argv);
168  close_temp_sink(sink_path, fd);
169  return err;
170}
Note: See TracBrowser for help on using the repository browser.