From: Thomas Fillon Date: Wed, 20 Nov 2013 16:40:40 +0000 (+0100) Subject: Tests : add some enhancements X-Git-Tag: 0.5.2~47 X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=3662f158248c618a04994a2304e30c973ed085b0;p=timeside.git Tests : add some enhancements --- diff --git a/tests/test_analyzer_preprocessors.py b/tests/test_analyzer_preprocessors.py index b4b703b..3a37625 100644 --- a/tests/test_analyzer_preprocessors.py +++ b/tests/test_analyzer_preprocessors.py @@ -1,8 +1,7 @@ +#! /usr/bin/env python # -*- coding: utf-8 -*- # Author : Thomas fillon -#! /usr/bin/env python - from unit_timeside import * from timeside.decoder import * from timeside.analyzer.preprocessors import downmix_to_mono, frames_adapter diff --git a/tests/test_array_decoding.py b/tests/test_array_decoding.py index 64d21b1..b29a14b 100644 --- a/tests/test_array_decoding.py +++ b/tests/test_array_decoding.py @@ -106,6 +106,7 @@ class TestDecoding(TestCase): self.assertEqual(totalframes, self.expected_duration * decoder.output_samplerate) + self.assertEquals(totalframes, decoder.totalframes()) class TestDecodingSegment(TestDecoding): diff --git a/tests/test_decoding.py b/tests/test_decoding.py index f400314..a8b8fc7 100755 --- a/tests/test_decoding.py +++ b/tests/test_decoding.py @@ -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): diff --git a/tests/unit_timeside.py b/tests/unit_timeside.py index 01a1b76..96d0e56 100644 --- a/tests/unit_timeside.py +++ b/tests/unit_timeside.py @@ -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) diff --git a/timeside/analyzer/core.py b/timeside/analyzer/core.py index 3abadb3..81f57e7 100644 --- a/timeside/analyzer/core.py +++ b/timeside/analyzer/core.py @@ -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 : diff --git a/timeside/analyzer/preprocessors.py b/timeside/analyzer/preprocessors.py index bf9fda2..3afd6ad 100644 --- a/timeside/analyzer/preprocessors.py +++ b/timeside/analyzer/preprocessors.py @@ -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) diff --git a/timeside/decoder/core.py b/timeside/decoder/core.py index 616da05..ef61fc3 100644 --- a/timeside/decoder/core.py +++ b/timeside/decoder/core.py @@ -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) +