source: tests/utils_tests.h @ a334368

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

Revert "[tests] fix unmatched parenthesis on windows"

This reverts commit bab46116728862e3c4b8d5480c39b75647abb73c.

  • Property mode set to 100644
File size: 3.2 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#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
29#define PRINT_ERR(format, args...)   fprintf(stderr, "AUBIO-TESTS ERROR: " format , ##args)
30#define PRINT_MSG(format, args...)   fprintf(stdout, format , ##args)
31#define PRINT_DBG(format, args...)   fprintf(stderr, format , ##args)
32#define PRINT_WRN(format, args...)   fprintf(stderr, "AUBIO-TESTS WARNING: " format, ##args)
33#endif
34
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
43// are we on windows ? or are we using -std=c99 ?
44#if defined(HAVE_WIN_HACKS) || defined(__STRICT_ANSI__)
45// http://en.wikipedia.org/wiki/Linear_congruential_generator
46// no srandom/random on win32
47
48uint_t srandom_seed = 1029;
49
50void srandom(uint_t new_seed) {
51    srandom_seed = new_seed;
52}
53
54uint_t random(void) {
55    srandom_seed = 1664525 * srandom_seed + 1013904223;
56    return srandom_seed;
57}
58#endif
59
60void utils_init_random (void);
61
62void utils_init_random (void) {
63  time_t now = time(0);
64  struct tm *tm_struct = localtime(&now);
65  size_t **tm_address = (void*)&tm_struct;
66  int seed = tm_struct->tm_sec + (size_t)tm_address;
67  //PRINT_WRN("current seed: %d\n", seed);
68  srandom (seed);
69}
70
71// create_temp_sink / close_temp_sink
72#if defined(__GNUC__) // mkstemp
73
74int create_temp_sink(char *sink_path)
75{
76  return mkstemp(sink_path);
77}
78
79int 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
87#elif (defined(HAVE_WIN_HACKS) //&& !defined(__GNUC__))
88// windows workaround, where mkstemp does not exist...
89int 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
104int 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
116int run_on_default_sink( int main(int, char**) )
117{
118  const int argc = 2;
119  int err = 0;
120  char* argv[argc];
121  char sink_path[PATH_MAX] = "tmp_aubio_XXXXXX";
122  int fd = create_temp_sink(sink_path);
123  if (!fd) return 1;
124  argv[0] = __FILE__;
125  argv[1] = sink_path;
126  err = main(argc, argv);
127  close_temp_sink(sink_path, fd);
128  return err;
129}
Note: See TracBrowser for help on using the repository browser.