source: tests/utils_tests.h @ aa15080

feature/autosinkfeature/constantqfeature/pitchshiftfeature/pydocstringsfeature/timestretch
Last change on this file since aa15080 was aa15080, checked in by Paul Brossier <piem@piem.org>, 14 months ago

[tests] workaround for windows

  • Property mode set to 100644
File size: 3.1 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_UNISTD_H
9#include <unistd.h> // unlink, close
10#endif
11
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
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
25#define PRINT_ERR(format, args...)   fprintf(stderr, "AUBIO-TESTS ERROR: " format , ##args)
26#define PRINT_MSG(format, args...)   fprintf(stdout, format , ##args)
27#define PRINT_DBG(format, args...)   fprintf(stderr, format , ##args)
28#define PRINT_WRN(format, args...)   fprintf(stderr, "AUBIO-TESTS WARNING: " format, ##args)
29#endif
30
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
39// are we on windows ? or are we using -std=c99 ?
40#if defined(HAVE_WIN_HACKS) || defined(__STRICT_ANSI__)
41// http://en.wikipedia.org/wiki/Linear_congruential_generator
42// no srandom/random on win32
43
44uint_t srandom_seed = 1029;
45
46void srandom(uint_t new_seed) {
47    srandom_seed = new_seed;
48}
49
50uint_t random(void) {
51    srandom_seed = 1664525 * srandom_seed + 1013904223;
52    return srandom_seed;
53}
54#endif
55
56void utils_init_random (void);
57
58void utils_init_random (void) {
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}
65
66// create_temp_sink / close_temp_sink
67#if defined(__GNUC__) // mkstemp
68
69int create_temp_sink(char *sink_path)
70{
71  return mkstemp(sink_path);
72}
73
74int 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...
84int 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
99int 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
111int 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";
116  int fd = create_temp_sink(sink_path);
117  if (!fd) return 1;
118  argv[0] = __FILE__;
119  argv[1] = sink_path;
120  err = main(argc, argv);
121  close_temp_sink(sink_path, fd);
122  return err;
123}
Note: See TracBrowser for help on using the repository browser.