add unit test for sample cache

This commit is contained in:
derselbst 2018-04-06 20:21:14 +02:00
parent 4032ae0a48
commit bd5b2f9a9c
3 changed files with 67 additions and 0 deletions

10
test/CMakeLists.txt Normal file
View file

@ -0,0 +1,10 @@
project(fluidsynth-test)
ADD_EXECUTABLE(test_sfont_cache test_sfont_cache.cpp)
TARGET_LINK_LIBRARIES(test_sfont_cache libfluidsynth)
ADD_TEST(NAME test_sfont_cache COMMAND test_sfont_cache)
# make test only runs tests, but doesnt build them
add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} DEPENDS test_sfont_cache)

11
test/test.h Normal file
View file

@ -0,0 +1,11 @@
#pragma once
#include <stdio.h>
#include <stdlib.h>
#define TEST_ASSERT(COND) if (!(COND)) { fprintf(stderr, __FILE__ ":%d assertion ("#COND") failed\n", __LINE__); exit(-1); }
#define TEST_ASSERT_EQ(LHS, RHS) TEST_ASSERT((LHS) == (RHS))
#define TEST_ASSERT_NEQ(LHS, RHS) TEST_ASSERT((LHS) != (RHS))
#define TEST_SUCCESS(FLUID_FUNCT) TEST_ASSERT_NEQ((FLUID_FUNCT), FLUID_FAILED)

46
test/test_sample_cache.c Normal file
View file

@ -0,0 +1,46 @@
#include "test.h"
#include "fluidsynth.h" // use local fluidsynth header
// this test aims to make sure that sample data used by multiple synths is not freed
// once unloaded by its parent synth
int main()
{
enum { FRAMES = 1024 };
float buf[FRAMES * 2];
fluid_settings_t *settings = new_fluid_settings();
fluid_synth_t *synth1 = new_fluid_synth(settings),
*synth2 = new_fluid_synth(settings);
TEST_ASSERT_NEQ(settings, NULL);
TEST_ASSERT_NEQ(synth1, NULL);
TEST_ASSERT_NEQ(synth2, NULL);
char *s;
TEST_SUCCESS(fluid_settings_dupstr(settings, "synth.default-soundfont", &s))
TEST_ASSERT_NEQ(s[0], '\0');
// load a sfont to synth1
TEST_SUCCESS(fluid_synth_sfload(synth1, s, 1));
// load the same font to synth2 and start a note
TEST_SUCCESS(fluid_synth_sfload(synth2, s, 1));
TEST_SUCCESS(fluid_synth_noteon(synth2, 0, 60, 127));
// render some audio
TEST_SUCCESS(fluid_synth_write_float(synth2, FRAMES, buf, 0, 2, buf, 1, 2));
// delete the synth that owns the soundfont
delete_fluid_synth(synth1);
// render again with the unloaded sfont and hope no segfault happens
TEST_SUCCESS(fluid_synth_write_float(synth2, FRAMES, buf, 0, 2, buf, 1, 2));
delete_fluid_synth(synth2);
delete_fluid_settings(settings);
return EXIT_SUCCESS;
}