xtc/tests/unit/test_sample.c

22 lines
1.3 KiB
C
Raw Normal View History

#define _POSIX_C_SOURCE 200809L
#include "xtc/xtc_sample.h"
#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(void)
{
char root[]="/tmp/xtc-sample-test-XXXXXX",path[256];float values[]={0,.25f,-.5f,.75f,0,0,.5f,-.25f};
xtc_recorder *rec=NULL;xtc_sample *s=NULL,*sentinel=NULL;xtc_sample_stats stats;size_t frames;unsigned int rate;float v=123;
assert(mkdtemp(root));snprintf(path,sizeof(path),"%s/source.wav",root);
assert(xtc_recorder_create(path,8000,8,&rec)==XTC_OK);xtc_recorder_push(rec,values,8);assert(xtc_recorder_finish(rec)==XTC_OK);xtc_recorder_destroy(rec);
assert(xtc_sample_load(path,&s)==XTC_OK && xtc_sample_info(s,&rate,&frames)==XTC_OK && rate==8000 && frames==8);
assert(xtc_sample_analyze(s,0,8,&stats)==XTC_OK && stats.minimum==-.5f && stats.maximum==.75f && stats.mean==.09375);
assert(fabs(stats.rms-sqrt(1.1875/8))<.000001);
assert(xtc_sample_read(s,8,&v)==XTC_ERR_INVALID_ARGUMENT && v==123);
assert(xtc_sample_read(s,2,&v)==XTC_OK && v==-.5f);
assert(xtc_sample_analyze(s,2,2,&stats)==XTC_ERR_INVALID_ARGUMENT);
assert(xtc_sample_load("/missing/xtc.wav",&sentinel)==XTC_ERR_IO && sentinel==NULL);
xtc_sample_destroy(s);assert(unlink(path)==0 && rmdir(root)==0);puts("Sample editor core tests passed.");return 0;
}