source: tests/utils_tests.h @ d22b568

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

[tests] use constant for array size, include string.h

  • 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_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  int seed = tm_struct->tm_sec;
66  //PRINT_WRN("current seed: %d\n", seed);
67  srandom (seed);
68}
69
70// create_temp_sink / close_temp_sink
71#if defined(__GNUC__) // mkstemp
72
73int create_temp_sink(char *sink_path)
74{
75  return mkstemp(sink_path);
76}
77
78int close_temp_sink(char *sink_path, int sink_fildes)
79{
80  int err;
81  if ((err = close(sink_fildes)) != 0) return err;
82  if ((err = unlink(sink_path)) != 0) return err;
83  return err;
84}
85
86#elif (defined(HAVE_WIN_HACKS) //&& !defined(__GNUC__))
87// windows workaround, where mkstemp does not exist...
88int create_temp_sink(char *templ)
89{
90  int i = 0;
91  static const char letters[] = "abcdefg0123456789";
92  int letters_len = strlen(letters);
93  int templ_len = strlen(templ);
94  if (templ_len == 0) return 0;
95  utils_init_random();
96  for (i = 0; i < 6; i++)
97  {
98    templ[templ_len - i] = letters[rand() % letters_len];
99  }
100  return 1;
101}
102
103int close_temp_sink(char* sink_path, int sink_fildes) {
104  // the file should be closed when not using mkstemp, no need to open it
105  if (sink_fildes == 0) return 1;
106  return _unlink(sink_path);
107}
108
109#else // windows workaround
110// otherwise, we don't really know what to do yet
111#error "mkstemp undefined, but not on windows. additional workaround required."
112#endif
113
114
115int run_on_default_sink( int main(int, char**) )
116{
117  const int argc = 2;
118  int err = 0;
119  char* argv[argc];
120  char sink_path[PATH_MAX] = "tmp_aubio_XXXXXX";
121  int fd = create_temp_sink(sink_path);
122  if (!fd) return 1;
123  argv[0] = __FILE__;
124  argv[1] = sink_path;
125  err = main(argc, argv);
126  close_temp_sink(sink_path, fd);
127  return err;
128}
Note: See TracBrowser for help on using the repository browser.