Add test for last commit.

This commit is contained in:
Erik de Castro Lopo 2012-10-11 22:20:03 +11:00
parent 835bd33ed4
commit 6040ad6a13
4 changed files with 145 additions and 1 deletions

View File

@ -11,6 +11,9 @@
Calculation of the signal max was failing because it was trying to read
a sample count that was not an integer multiple of the channel count.
* tests/channel_test.c tests/Makefile.am tests/test_wrapper.sh.in
Add test for the above.
2012-09-25 Erik de Castro Lopo <erikd AT mega-nerd DOT com>
* src/sndfile.hh

View File

@ -15,7 +15,8 @@ check_PROGRAMS = sfversion floating_point_test write_read_test \
raw_test string_test multi_file_test dither_test chunk_test \
scale_clip_test win32_test fix_this aiff_rw_test virtual_io_test \
locale_test largefile_test win32_ordinal_test ogg_test compression_size_test \
checksum_test external_libs_test rdwr_test format_check_test $(CPP_TEST)
checksum_test external_libs_test rdwr_test format_check_test $(CPP_TEST) \
channel_test
noinst_HEADERS = dft_cmp.h utils.h generate.h
@ -159,6 +160,9 @@ external_libs_test_LDADD = $(top_builddir)/src/libsndfile.la
format_check_test_SOURCES = format_check_test.c utils.c
format_check_test_LDADD = $(top_builddir)/src/libsndfile.la
channel_test_SOURCES = channel_test.c utils.c
channel_test_LDADD = $(top_builddir)/src/libsndfile.la
cpp_test_SOURCES = cpp_test.cc utils.c
cpp_test_LDADD = $(top_builddir)/src/libsndfile.la

136
tests/channel_test.c Normal file
View File

@ -0,0 +1,136 @@
/*
** Copyright (C) 2001-2012 Erik de Castro Lopo <erikd@mega-nerd.com>
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "sfconfig.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <string.h>
#include <time.h>
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <math.h>
#include <sndfile.h>
#include "utils.h"
#define BUFFER_LEN (1 << 10)
#define LOG_BUFFER_SIZE 1024
static void channel_test (void) ;
static double max_diff (const float *a, const float *b, int len) ;
int
main (void) // int argc, char *argv [])
{ channel_test () ;
return 0 ;
} /* main */
/*============================================================================================
** Here are the test functions.
*/
static void
channel_test (void)
{ static float float_data [1024] ;
static float read_float [1024] ;
static int read_int [1024] ;
static short read_short [1024] ;
unsigned int ch, k ;
gen_windowed_sine_float (float_data, ARRAY_LEN (float_data), 0.9) ;
for (ch = 1 ; ch <= 8 ; ch++)
{ SNDFILE *file ;
SF_INFO wsfinfo = { 0, }, rsfinfo = { 0, } ;
sf_count_t wframes = ARRAY_LEN (float_data) / ch ;
double maxdiff ;
char filename [256] ;
snprintf (filename, sizeof (filename), "chan_%d.wav", ch) ;
print_test_name (__func__, filename) ;
wsfinfo.samplerate = 48000 ;
wsfinfo.format = SF_FORMAT_WAV | SF_FORMAT_FLOAT ;
wsfinfo.channels = ch ;
wsfinfo.frames = wframes ;
/* Write the test file. */
file = test_open_file_or_die (filename, SFM_WRITE, &wsfinfo, SF_FALSE, __LINE__) ;
test_writef_float_or_die (file, 0, float_data, wframes, __LINE__) ;
sf_close (file) ;
/* Read it as float and test. */
file = test_open_file_or_die (filename, SFM_READ, &rsfinfo, SF_FALSE, __LINE__) ;
exit_if_true (rsfinfo.frames == 0,
"\n\nLine %d : Frames in file %ld.\n\n", __LINE__, rsfinfo.frames) ;
exit_if_true (wframes != rsfinfo.frames,
"\n\nLine %d : Wrote %ld, read %ld frames.\n\n", __LINE__, wframes, rsfinfo.frames) ;
sf_command (file, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE) ;
test_readf_float_or_die (file, 0, read_float, rsfinfo.frames, __LINE__) ;
compare_float_or_die (float_data, read_float, ch * rsfinfo.frames, __LINE__) ;
/* Read it as short and test. */
test_seek_or_die (file, 0, SEEK_SET, 0, ch, __LINE__) ;
test_readf_short_or_die (file, 0, read_short, rsfinfo.frames, __LINE__) ;
for (k = 0 ; k < ARRAY_LEN (read_float) ; k++)
read_float [k] = read_short [k] * (0.9 / 0x8000) ;
maxdiff = max_diff (float_data, read_float, ch * rsfinfo.frames) ;
exit_if_true (maxdiff > 0.5, "\n\nLine %d : Max diff is %f\n\n", __LINE__, maxdiff) ;
/* Read it as int and test. */
test_seek_or_die (file, 0, SEEK_SET, 0, ch, __LINE__) ;
test_readf_int_or_die (file, 0, read_int, rsfinfo.frames, __LINE__) ;
for (k = 0 ; k < ARRAY_LEN (read_float) ; k++)
read_float [k] = read_int [k] * (0.9 / 0x80000000) ;
maxdiff = max_diff (float_data, read_float, ch * rsfinfo.frames) ;
exit_if_true (maxdiff > 0.5, "\n\nLine %d : Max diff is %f\n\n", __LINE__, maxdiff) ;
sf_close (file) ;
unlink (filename) ;
printf ("ok\n") ;
} ;
return ;
} /* channel_test */
static double
max_diff (const float *a, const float *b, int len)
{ double mdiff = 0.0, diff ;
int k ;
for (k = 0 ; k < len ; k++)
{ diff = fabs (a [k] - b [k]) ;
mdiff = diff > mdiff ? diff : mdiff ;
// printf ("%4d: % f % f % f % f\n", k, a [k], b [k], diff, mdiff) ;
} ;
return mdiff ;
} /* max_diff */

View File

@ -102,6 +102,7 @@ if test -x test_main@EXEEXT@ ; then
./win32_ordinal_test@EXEEXT@
./external_libs_test@EXEEXT@
./format_check_test@EXEEXT@
./channel_test@EXEEXT@
# The w64 G++ compiler requires an extra runtime DLL which we don't have,
# so skip this test.