Changeset ad45776


Ignore:
Timestamp:
Nov 2, 2018, 12:10:29 AM (13 months ago)
Author:
Paul Brossier <piem@piem.org>
Branches:
feature/autosink, feature/constantq, feature/pitchshift, feature/timestretch, master
Children:
235a891
Parents:
ecd370b
Message:

[tests] update source tests

File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/tests/test_source.py

    recd370b rad45776  
    11#! /usr/bin/env python
    22
    3 from nose2 import main
    4 from nose2.tools import params
     3
    54from numpy.testing import TestCase, assert_equal
    65from aubio import source
    7 from .utils import list_all_sounds
    8 
    9 import warnings
    10 warnings.filterwarnings('ignore', category=UserWarning, append=True)
     6from utils import list_all_sounds
     7import unittest
     8from _tools import parametrize, assert_raises, assert_equal, skipTest
    119
    1210list_of_sounds = list_all_sounds('sounds')
     
    1412hop_sizes = [512, 1024, 64]
    1513
    16 path = None
     14default_test_sound = len(list_of_sounds) and list_of_sounds[0] or None
    1715
    1816all_params = []
     
    2220            all_params.append((hop_size, samplerate, soundfile))
    2321
     22no_sounds_msg = "no test sounds, add some in 'python/tests/sounds/'!"
    2423
    25 class aubio_source_test_case_base(TestCase):
     24_debug = False
    2625
    27     def setUp(self):
    28         if not len(list_of_sounds):
    29             self.skipTest('add some sound files in \'python/tests/sounds\'')
    30         self.default_test_sound = list_of_sounds[0]
     26class Test_aubio_source_test_case:
    3127
    32 class aubio_source_test_case(aubio_source_test_case_base):
    33 
    34     @params(*list_of_sounds)
     28    @parametrize('filename', list_of_sounds)
    3529    def test_close_file(self, filename):
    3630        samplerate = 0 # use native samplerate
     
    3933        f.close()
    4034
    41     @params(*list_of_sounds)
     35    @parametrize('filename', list_of_sounds)
    4236    def test_close_file_twice(self, filename):
    4337        samplerate = 0 # use native samplerate
     
    4741        f.close()
    4842
    49 class aubio_source_read_test_case(aubio_source_test_case_base):
     43class Test_aubio_source_read:
    5044
    5145    def read_from_source(self, f):
     
    5751                assert_equal(samples[read:], 0)
    5852                break
    59         #result_str = "read {:.2f}s ({:d} frames in {:d} blocks at {:d}Hz) from {:s}"
    60         #result_params = total_frames / float(f.samplerate), total_frames, total_frames//f.hop_size, f.samplerate, f.uri
    61         #print (result_str.format(*result_params))
     53        if _debug:
     54            result_str = "read {:.2f}s ({:d} frames"
     55            result_str += " in {:d} blocks at {:d}Hz) from {:s}"
     56            result_params = total_frames / float(f.samplerate), total_frames, \
     57                    total_frames//f.hop_size, f.samplerate, f.uri
     58            print (result_str.format(*result_params))
    6259        return total_frames
    6360
    64     @params(*all_params)
     61    @parametrize('hop_size, samplerate, soundfile', all_params)
    6562    def test_samplerate_hopsize(self, hop_size, samplerate, soundfile):
    6663        try:
    6764            f = source(soundfile, samplerate, hop_size)
    6865        except RuntimeError as e:
    69             self.skipTest('failed opening with hop_s = {:d}, samplerate = {:d} ({:s})'.format(hop_size, samplerate, str(e)))
     66            err_msg = 'failed opening with hop_s={:d}, samplerate={:d} ({:s})'
     67            skipTest(err_msg.format(hop_size, samplerate, str(e)))
    7068        assert f.samplerate != 0
    7169        read_frames = self.read_from_source(f)
     
    7674            if len(match_f) == 1:
    7775                expected_frames = int(match_f[0])
    78                 self.assertEqual(expected_frames, read_frames)
     76                assert_equal(expected_frames, read_frames)
    7977
    80     @params(*list_of_sounds)
     78    @parametrize('p', list_of_sounds)
    8179    def test_samplerate_none(self, p):
    8280        f = source(p)
     
    8482        self.read_from_source(f)
    8583
    86     @params(*list_of_sounds)
     84    @parametrize('p', list_of_sounds)
    8785    def test_samplerate_0(self, p):
    8886        f = source(p, 0)
     
    9088        self.read_from_source(f)
    9189
    92     @params(*list_of_sounds)
     90    @parametrize('p', list_of_sounds)
    9391    def test_zero_hop_size(self, p):
    9492        f = source(p, 0, 0)
     
    9795        self.read_from_source(f)
    9896
    99     @params(*list_of_sounds)
     97    @parametrize('p', list_of_sounds)
    10098    def test_seek_to_half(self, p):
    10199        from random import randint
     
    109107        assert a == b + c
    110108
    111     @params(*list_of_sounds)
     109    @parametrize('p', list_of_sounds)
    112110    def test_duration(self, p):
    113111        total_frames = 0
     
    118116            total_frames += read
    119117            if read < f.hop_size: break
    120         self.assertEqual(duration, total_frames)
     118        assert_equal (duration, total_frames)
    121119
    122120
    123 class aubio_source_test_wrong_params(TestCase):
     121class Test_aubio_source_wrong_params:
    124122
    125123    def test_wrong_file(self):
    126         with self.assertRaises(RuntimeError):
     124        with assert_raises(RuntimeError):
    127125            source('path_to/unexisting file.mp3')
    128126
    129 class aubio_source_test_wrong_params_with_file(aubio_source_test_case_base):
     127@unittest.skipIf(default_test_sound is None, no_sounds_msg)
     128class Test_aubio_source_wrong_params_with_file(TestCase):
    130129
    131130    def test_wrong_samplerate(self):
    132         with self.assertRaises(ValueError):
    133             source(self.default_test_sound, -1)
     131        with assert_raises(ValueError):
     132            source(default_test_sound, -1)
    134133
    135134    def test_wrong_hop_size(self):
    136         with self.assertRaises(ValueError):
    137             source(self.default_test_sound, 0, -1)
     135        with assert_raises(ValueError):
     136            source(default_test_sound, 0, -1)
    138137
    139138    def test_wrong_channels(self):
    140         with self.assertRaises(ValueError):
    141             source(self.default_test_sound, 0, 0, -1)
     139        with assert_raises(ValueError):
     140            source(default_test_sound, 0, 0, -1)
    142141
    143142    def test_wrong_seek(self):
    144         f = source(self.default_test_sound)
    145         with self.assertRaises(ValueError):
     143        f = source(default_test_sound)
     144        with assert_raises(ValueError):
    146145            f.seek(-1)
    147146
    148147    def test_wrong_seek_too_large(self):
    149         f = source(self.default_test_sound)
     148        f = source(default_test_sound)
    150149        try:
    151             with self.assertRaises(ValueError):
     150            with assert_raises(ValueError):
    152151                f.seek(f.duration + f.samplerate * 10)
    153         except AssertionError:
    154             self.skipTest('seeking after end of stream failed raising ValueError')
     152        except:
     153            skipTest('seeking after end of stream failed raising ValueError')
    155154
    156 class aubio_source_readmulti_test_case(aubio_source_read_test_case):
     155class Test_aubio_source_readmulti(Test_aubio_source_read):
    157156
    158157    def read_from_source(self, f):
     
    164163                assert_equal(samples[:,read:], 0)
    165164                break
    166         #result_str = "read {:.2f}s ({:d} frames in {:d} channels and {:d} blocks at {:d}Hz) from {:s}"
    167         #result_params = total_frames / float(f.samplerate), total_frames, f.channels, int(total_frames/f.hop_size), f.samplerate, f.uri
    168         #print (result_str.format(*result_params))
     165        if _debug:
     166            result_str = "read {:.2f}s ({:d} frames in {:d} channels"
     167            result_str += " and {:d} blocks at {:d}Hz) from {:s}"
     168            result_params = total_frames / float(f.samplerate), total_frames, \
     169                    f.channels, int(total_frames/f.hop_size), \
     170                    f.samplerate, f.uri
     171            print (result_str.format(*result_params))
    169172        return total_frames
    170173
    171 class aubio_source_with(aubio_source_test_case_base):
     174class Test_aubio_source_with:
    172175
    173     #@params(*list_of_sounds)
    174     @params(*list_of_sounds)
     176    @parametrize('filename', list_of_sounds)
    175177    def test_read_from_mono(self, filename):
    176178        total_frames = 0
     
    186188
    187189if __name__ == '__main__':
    188     main()
     190    import sys, pytest
     191    pytest.main(sys.argv)
Note: See TracChangeset for help on using the changeset viewer.