]> git.parisson.com Git - timeside.git/commitdiff
Tests : add some enhancements
authorThomas Fillon <thomas@parisson.com>
Wed, 20 Nov 2013 16:40:40 +0000 (17:40 +0100)
committerThomas Fillon <thomas@parisson.com>
Wed, 20 Nov 2013 16:40:40 +0000 (17:40 +0100)
tests/test_analyzer_preprocessors.py
tests/test_array_decoding.py
tests/test_decoding.py
tests/unit_timeside.py
timeside/analyzer/core.py
timeside/analyzer/preprocessors.py
timeside/decoder/core.py

index b4b703bc865e141d83ff24e6ccfb9dc06ca2be5b..3a3762571e3f3034fbaf3cab6e756344b077cb65 100644 (file)
@@ -1,8 +1,7 @@
+#! /usr/bin/env python
 # -*- coding: utf-8 -*-
 # Author : Thomas fillon <thomas@parisson.com>
 
-#! /usr/bin/env python
-
 from unit_timeside import *
 from timeside.decoder import *
 from timeside.analyzer.preprocessors import downmix_to_mono, frames_adapter
index 64d21b10314490c4f09f10fea87710a65cc70e43..b29a14b77c8eacfc28f030bc53bdb31688073dce 100644 (file)
@@ -106,6 +106,7 @@ class TestDecoding(TestCase):
 
 
         self.assertEqual(totalframes, self.expected_duration * decoder.output_samplerate)
+        self.assertEquals(totalframes, decoder.totalframes())
 
 
 class TestDecodingSegment(TestDecoding):
index f400314b912d89d73c5bebdf1ed18a3b0afb3cf2..a8b8fc76d2c19f39e36e6cf81e9fa0778cc540b1 100755 (executable)
@@ -1,5 +1,7 @@
 #! /usr/bin/env python
 
+from __future__ import division
+
 from timeside.decoder.core import FileDecoder
 from unit_timeside import *
 
@@ -19,6 +21,7 @@ class TestDecoding(TestCase):
         self.expected_samplerate = 44100
         self.expected_channels = 2
         self.expected_totalframes = 352800
+        self.test_exact_duration = True
 
     def testWav(self):
         "Test wav decoding"
@@ -39,7 +42,7 @@ class TestDecoding(TestCase):
                                    "samples/sweep_32000.wav")
 
         expected_samplerate = 32000
-        ratio = expected_samplerate/float(self.expected_samplerate)
+        ratio = expected_samplerate/self.expected_samplerate
 
         self.expected_totalframes = int(self.expected_totalframes * ratio)
         self.expected_samplerate = expected_samplerate
@@ -55,6 +58,8 @@ class TestDecoding(TestCase):
                                    "samples/sweep.ogg")
 
         self.expected_totalframes = 352832
+        self.test_exact_duration = False
+
 
     def testMp3(self):
         "Test mp3 decoding"
@@ -62,6 +67,8 @@ class TestDecoding(TestCase):
                                    "samples/sweep.mp3")
 
         self.expected_totalframes = 353664
+        self.test_exact_duration = False
+
 
     def tearDown(self):
         decoder = FileDecoder(uri=self.source,
@@ -78,10 +85,10 @@ class TestDecoding(TestCase):
             totalframes += frames.shape[0]
             if eod or decoder.eod:
                 break
-            self.assertEquals(frames.shape[0], decoder.blocksize())
-            self.assertEquals(frames.shape[1], decoder.channels())
+            self.assertEqual(frames.shape[0], decoder.blocksize())
+            self.assertEqual(frames.shape[1], decoder.channels())
 
-        ratio = decoder.output_samplerate / float(decoder.input_samplerate)
+        ratio = decoder.output_samplerate / decoder.input_samplerate
         if 0:
             print "input / output_samplerate:", decoder.input_samplerate, '/', decoder.output_samplerate,
             print "ratio:", ratio
@@ -91,22 +98,29 @@ class TestDecoding(TestCase):
 
         if self.channels:
             # when specified, check that the channels are the ones requested
-            self.assertEquals(self.channels, decoder.output_channels)
+            self.assertEqual(self.channels, decoder.output_channels)
         else:
             # otherwise check that the channels are preserved, if not specified
-            self.assertEquals(decoder.input_channels, decoder.output_channels)
+            self.assertEqual(decoder.input_channels, decoder.output_channels)
             # and if we know the expected channels, check the output match
             if self.expected_channels:
-                self.assertEquals(self.expected_channels, decoder.output_channels)
+                self.assertEqual(self.expected_channels, decoder.output_channels)
         # do the same with the sampling rate
         if self.samplerate:
-            self.assertEquals(self.samplerate, decoder.output_samplerate)
+            self.assertEqual(self.samplerate, decoder.output_samplerate)
         else:
-            self.assertEquals(decoder.input_samplerate, decoder.output_samplerate)
+            self.assertEqual(decoder.input_samplerate, decoder.output_samplerate)
             if self.expected_samplerate:
-                self.assertEquals(self.expected_samplerate, decoder.output_samplerate)
+                self.assertEqual(self.expected_samplerate, decoder.output_samplerate)
 
-        self.assertEquals(totalframes, self.expected_totalframes)
+        self.assertEqual(totalframes, self.expected_totalframes)
+        if self.test_exact_duration:
+            self.assertEqual(totalframes/decoder.output_samplerate,
+                             decoder.totalframes()/decoder.output_samplerate)
+        else:
+            self.assertAlmostEqual(totalframes/decoder.output_samplerate,
+                               decoder.totalframes()/decoder.output_samplerate,
+                               places=1)
 
 
 class TestDecodingSegment(TestDecoding):
index 01a1b7681a20ffe7ad9a0ed7a6928b7d42d7a6d2..96d0e56b514d46a80d8ab5f963497d0091250400 100644 (file)
@@ -1,6 +1,7 @@
 # -*- coding: utf-8 -*-
 
 import unittest
+import doctest
 import os, sys
 import time
 from tools import *
@@ -142,6 +143,15 @@ class TestRunner:
             self.stream.writeln("OK")
         return result
 
-def runTestModule(module):
-    suite = unittest.loader.TestLoader().loadTestsFromModule(module)
+
+def runTestModule(*modules):
+
+    suite = unittest.TestSuite()
+    finder = doctest.DocTestFinder(exclude_empty=False)  # finder for doctest
+
+    for module in modules:
+        # Doctest
+        suite.addTest(doctest.DocTestSuite(module, test_finder=finder))
+        # unittest
+        suite.addTest(unittest.loader.TestLoader().loadTestsFromModule(module))
     TestRunner().run(suite)
index 3abadb3bef3be2f7fb96c1efb3aa47475b8505d2..81f57e73aaa3cf78505f6ffd92a6b67eb0ffa6fe 100644 (file)
@@ -27,7 +27,7 @@ from timeside.core import Processor
 from timeside.__init__ import __version__
 import numpy
 from collections import OrderedDict
-from copy import deepcopy
+
 
 numpy_data_types = [
     #'float128',
@@ -433,7 +433,6 @@ class AnalyzerResult(MetadataObject):
             - 'segment'
             - 'event'
 
-
     Returns
     -------
     A new MetadataObject with the following attributes :
index bf9fda24db2dd31bfdaf874db0e47578d0a5f086..3afd6adb0e542282713c5ce71d4b4ea5663d343b 100644 (file)
@@ -128,11 +128,11 @@ def frames_adapter(process_func):
         def __init__(self, blocksize, stepsize):
             self.blocksize = blocksize
             self.stepsize = stepsize
-            self.stack = None
+            self.buffer = None
 
         def frames(self, frames, eod):
-            if self.stack is not None:
-                stack = np.concatenate([self.stack, frames])
+            if self.buffer is not None:
+                stack = np.concatenate([self.buffer, frames])
             else:
                 stack = frames.copy()
 
@@ -154,17 +154,14 @@ def frames_adapter(process_func):
                                                         dtype=frames.dtype)])
                 nb_frames += 1
 
-            self.stack = stack[nb_frames * self.stepsize:]
+            self.buffer = stack[nb_frames * self.stepsize:]
 
             eod_list = np.repeat(False, nb_frames)
             if eod and len(eod_list):
                 eod_list[-1] = eod
 
-            for n in xrange(nb_frames):
-                yield (
-                    stack[
-                        n * self.stepsize:n * self.stepsize + self.blocksize],
-                    eod_list[n])
+            for index, eod in zip(xrange(0, nb_frames*self.stepsize, self.stepsize), eod_list):
+                yield (stack[index:index + self.blocksize],eod)
 
     @functools.wraps(process_func)
     def wrapper(analyzer, frames, eod):
@@ -182,11 +179,7 @@ def frames_adapter(process_func):
 
 
 if __name__ == "__main__":
-    # Run doctest
-    import doctest
-    doctest.testmod()
-
-    # Run unittest from test_analyzer_preprocessors
+    # Run doctest from __main__ and unittest from test_analyzer_preprocessors
     from tests import test_analyzer_preprocessors
     from tests.unit_timeside import runTestModule
-    runTestModule(test_analyzer_preprocessors)
+    runTestModule('__main__', test_analyzer_preprocessors)
index 616da054c087cee973dc0909817a5202a95956bc..ef61fc3783fb16ec5edb44cdebb40c3d1af2459f 100644 (file)
@@ -62,17 +62,20 @@ class FileDecoder(Processor):
         return "gst_dec"
 
     def __init__(self, uri, start = 0, duration = None):
-        '''
-            Construct a new FileDecoder
 
-            Parameters
-            ----------
-            uri: uri of the media
-            start : float
-                start time of the segment in seconds
-            duration : float
-                duration of the segment in seconds
-        '''
+        """
+        Construct a new FileDecoder
+
+        Parameters
+        ----------
+        uri : str
+            uri of the media
+        start : float
+            start time of the segment in seconds
+        duration : float
+            duration of the segment in seconds
+        """
+
         super(FileDecoder, self).__init__()
 
         # is this a file?
@@ -526,3 +529,13 @@ class ArrayDecoder(Processor):
     @interfacedoc
     def metadata(self):
         return None
+
+
+if __name__ == "__main__":
+    # Run doctest from __main__ and unittest from tests
+    from tests.unit_timeside import runTestModule
+    # load corresponding tests
+    from tests import test_decoding, test_array_decoding
+
+    runTestModule('__main__', test_decoding, test_array_decoding)
+