Ignore:
Timestamp:
Dec 1, 2018, 10:57:07 AM (5 years ago)
Author:
Paul Brossier <piem@piem.org>
Branches:
feature/autosink, feature/cnn, feature/cnn_org, feature/constantq, feature/crepe, feature/crepe_org, feature/pitchshift, feature/timestretch, fix/ffmpeg5, master
Children:
c1ba75b
Parents:
bbfa9a4 (diff), bde4f641 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' into feature/pytest

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tests/utils_tests.h

    rbbfa9a4 radc6e02  
    55#include <assert.h>
    66#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#if defined(HAVE_WIN_HACKS) && !defined(__GNUC__)
     24#include <io.h> // _access
     25#endif
     26
     27// This macro is used to pass a string to msvc compiler: since msvc's -D flag
     28// strips the quotes, we define the string without quotes and re-add them with
     29// this macro.
     30
     31#define REDEFINESTRING(x) #x
     32#define DEFINEDSTRING(x) REDEFINESTRING(x)
     33
     34#ifndef AUBIO_TESTS_SOURCE
     35#error "AUBIO_TESTS_SOURCE is not defined"
     36#endif
    737
    838#ifdef HAVE_C99_VARARGS_MACROS
     
    4878  time_t now = time(0);
    4979  struct tm *tm_struct = localtime(&now);
    50   int seed = tm_struct->tm_sec;
     80  size_t **tm_address = (void*)&tm_struct;
     81  int seed = tm_struct->tm_sec + (size_t)tm_address;
    5182  //PRINT_WRN("current seed: %d\n", seed);
    52   srandom (seed);
     83  srandom ((unsigned int)seed);
    5384}
     85
     86// create_temp_sink / close_temp_sink
     87#if defined(__GNUC__) // mkstemp
     88
     89int check_source(char *source_path)
     90{
     91  return access(source_path, R_OK);
     92}
     93
     94int create_temp_sink(char *sink_path)
     95{
     96  return mkstemp(sink_path);
     97}
     98
     99int close_temp_sink(char *sink_path, int sink_fildes)
     100{
     101  int err;
     102  if ((err = close(sink_fildes)) != 0) return err;
     103  if ((err = unlink(sink_path)) != 0) return err;
     104  return err;
     105}
     106
     107#elif defined(HAVE_WIN_HACKS) //&& !defined(__GNUC__)
     108// windows workaround, where mkstemp does not exist...
     109
     110int check_source(char *source_path)
     111{
     112  return _access(source_path, 04);
     113}
     114
     115int create_temp_sink(char *templ)
     116{
     117  int i = 0;
     118  static const char letters[] = "abcdefg0123456789";
     119  int letters_len = strlen(letters);
     120  int templ_len = strlen(templ);
     121  if (templ_len == 0) return 0;
     122  utils_init_random();
     123  for (i = 0; i < 6; i++)
     124  {
     125    templ[templ_len - i] = letters[rand() % letters_len];
     126  }
     127  return 1;
     128}
     129
     130int close_temp_sink(char* sink_path, int sink_fildes) {
     131  // the file should be closed when not using mkstemp, no need to open it
     132  if (sink_fildes == 0) return 1;
     133  return _unlink(sink_path);
     134}
     135
     136#else // windows workaround
     137// otherwise, we don't really know what to do yet
     138#error "mkstemp undefined, but not on windows. additional workaround required."
     139#endif
     140
     141// pass progname / default
     142int run_on_default_source( int main(int, char**) )
     143{
     144  const int argc = 2;
     145  int err = 0;
     146  char** argv = (char**)calloc(argc, sizeof(char*));
     147  argv[0] = __FILE__;
     148  argv[1] = DEFINEDSTRING(AUBIO_TESTS_SOURCE);
     149  // check if the file can be read
     150  if ( check_source(argv[1]) ) return 1;
     151  err = main(argc, argv);
     152  if (argv) free(argv);
     153  return err;
     154}
     155
     156int run_on_default_sink( int main(int, char**) )
     157{
     158  const int argc = 2;
     159  int err = 0;
     160  char** argv = (char**)calloc(argc, sizeof(char*));
     161  char sink_path[PATH_MAX] = "tmp_aubio_XXXXXX";
     162  int fd = create_temp_sink(sink_path);
     163  if (!fd) return 1;
     164  argv[0] = __FILE__;
     165  argv[1] = sink_path;
     166  err = main(argc, argv);
     167  close_temp_sink(sink_path, fd);
     168  if (argv) free(argv);
     169  return err;
     170}
     171
     172int run_on_default_source_and_sink( int main(int, char**) )
     173{
     174  const int argc = 3;
     175  int err = 0;
     176  char** argv = (char**)calloc(argc, sizeof(char*));
     177  char sink_path[PATH_MAX] = "tmp_aubio_XXXXXX";
     178  int fd = create_temp_sink(sink_path);
     179  if (!fd) return 1;
     180  argv[0] = __FILE__;
     181  argv[1] = DEFINEDSTRING(AUBIO_TESTS_SOURCE);
     182  argv[2] = sink_path;
     183  // check if the file can be read
     184  if ( check_source(argv[1]) ) return 1;
     185  err = main(argc, argv);
     186  close_temp_sink(sink_path, fd);
     187  if (argv) free(argv);
     188  return err;
     189}
Note: See TracChangeset for help on using the changeset viewer.