From 142eb92a7669debb3748d85e56daa0e83d39464d Mon Sep 17 00:00:00 2001
From: John Glover <j@johnglover.net>
Date: Sat, 22 Jun 2013 18:39:17 +0200
Subject: [residual] Add basic test for SMSResidual

---
 tests/test_residual.cpp | 51 +++++++++++++++++++++++++++++++++++++++++++++++++
 tests/test_residual.h   | 37 +++++++++++++++++++++++++++++++++++
 tests/tests.cpp         |  2 ++
 3 files changed, 90 insertions(+)
 create mode 100644 tests/test_residual.cpp
 create mode 100644 tests/test_residual.h

(limited to 'tests')

diff --git a/tests/test_residual.cpp b/tests/test_residual.cpp
new file mode 100644
index 0000000..a597317
--- /dev/null
+++ b/tests/test_residual.cpp
@@ -0,0 +1,51 @@
+#include "test_residual.h"
+
+using namespace simpl;
+
+// ---------------------------------------------------------------------------
+//	test_basic
+// ---------------------------------------------------------------------------
+static void test_basic(Residual* residual, SndfileHandle *sf) {
+    int num_samples = 4096;
+    int hop_size = 256;
+    int frame_size = 512;
+
+    std::vector<sample> audio(sf->frames(), 0.0);
+    sf->read(&audio[0], (int)sf->frames());
+
+    residual->reset();
+    residual->frame_size(frame_size);
+    residual->hop_size(hop_size);
+
+    Frames frames = residual->synth(num_samples,
+                                    &(audio[(int)sf->frames() / 2]));
+
+    for(int i = 0; i < frames.size(); i++) {
+        CPPUNIT_ASSERT(frames[i]->num_peaks() > 0);
+        CPPUNIT_ASSERT(frames[i]->num_partials() > 0);
+
+        double energy = 0.f;
+        for(int j = 0; j < residual->hop_size(); j++) {
+            energy += frames[i]->synth_residual()[j] *
+                      frames[i]->synth_residual()[j];
+        }
+        CPPUNIT_ASSERT(energy > 0.f);
+    }
+}
+
+
+// ---------------------------------------------------------------------------
+//	TestSMSResidual
+// ---------------------------------------------------------------------------
+void TestSMSResidual::setUp() {
+    _sf = SndfileHandle(TEST_AUDIO_FILE);
+
+    if(_sf.error() > 0) {
+        throw Exception(std::string("Could not open audio file: ") +
+                        std::string(TEST_AUDIO_FILE));
+    }
+}
+
+void TestSMSResidual::test_basic() {
+    ::test_basic(&_res, &_sf);
+}
diff --git a/tests/test_residual.h b/tests/test_residual.h
new file mode 100644
index 0000000..60f4eb9
--- /dev/null
+++ b/tests/test_residual.h
@@ -0,0 +1,37 @@
+#ifndef TEST_RESIDUAL_H
+#define TEST_RESIDUAL_H
+
+#include <cppunit/extensions/HelperMacros.h>
+
+#include "../src/simpl/base.h"
+#include "../src/simpl/peak_detection.h"
+#include "../src/simpl/partial_tracking.h"
+#include "../src/simpl/synthesis.h"
+#include "../src/simpl/residual.h"
+#include "test_common.h"
+
+namespace simpl
+{
+
+// ---------------------------------------------------------------------------
+//	TestSMSResidual
+// ---------------------------------------------------------------------------
+class TestSMSResidual : public CPPUNIT_NS::TestCase {
+    CPPUNIT_TEST_SUITE(TestSMSResidual);
+    CPPUNIT_TEST(test_basic);
+    CPPUNIT_TEST_SUITE_END();
+
+public:
+    void setUp();
+
+protected:
+    SMSResidual _res;
+    SndfileHandle _sf;
+    Frames _frames;
+
+    void test_basic();
+};
+
+} // end of namespace simpl
+
+#endif
diff --git a/tests/tests.cpp b/tests/tests.cpp
index dc11c98..517b7f1 100644
--- a/tests/tests.cpp
+++ b/tests/tests.cpp
@@ -6,6 +6,7 @@
 #include "test_peak_detection.h"
 #include "test_partial_tracking.h"
 #include "test_synthesis.h"
+#include "test_residual.h"
 
 CPPUNIT_TEST_SUITE_REGISTRATION(simpl::TestPeak);
 CPPUNIT_TEST_SUITE_REGISTRATION(simpl::TestFrame);
@@ -21,6 +22,7 @@ CPPUNIT_TEST_SUITE_REGISTRATION(simpl::TestMQSynthesis);
 CPPUNIT_TEST_SUITE_REGISTRATION(simpl::TestLorisSynthesis);
 CPPUNIT_TEST_SUITE_REGISTRATION(simpl::TestSMSSynthesis);
 CPPUNIT_TEST_SUITE_REGISTRATION(simpl::TestSndObjSynthesis);
+CPPUNIT_TEST_SUITE_REGISTRATION(simpl::TestSMSResidual);
 
 int main(int arg, char **argv) {
     CppUnit::TextTestRunner runner;
-- 
cgit v1.2.3