Changeset 5f5f843 for python/tests


Ignore:
Timestamp:
Apr 30, 2016, 4:17:31 PM (9 years ago)
Author:
Paul Brossier <piem@piem.org>
Branches:
feature/autosink, feature/cnn, feature/cnn_org, feature/constantq, feature/crepe, feature/crepe_org, feature/pitchshift, feature/pydocstrings, feature/timestretch, fix/ffmpeg5, master, pitchshift, sampler, timestretch, yinfft+
Children:
355c761
Parents:
2fe24df
Message:

python/tests/test_source.py: use nose2 to create one unit test per sound file

File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/tests/test_source.py

    r2fe24df r5f5f843  
    55from numpy import array
    66from utils import list_all_sounds
     7from nose2.tools import params
    78
    89list_of_sounds = list_all_sounds('sounds')
     10samplerates = [0, 44100, 8000, 32000]
     11hop_sizes = [512, 1024, 64]
     12
    913path = None
     14
     15all_params = []
     16for soundfile in list_of_sounds:
     17    for hop_size in hop_sizes:
     18        for samplerate in samplerates:
     19            all_params.append((hop_size, samplerate, soundfile))
     20
    1021
    1122class aubio_source_test_case_base(TestCase):
     
    4051            if read < f.hop_size: break
    4152        result_str = "read {:.2f}s ({:d} frames in {:d} blocks at {:d}Hz) from {:s}"
    42         params = total_frames / float(f.samplerate), total_frames, int(total_frames/f.hop_size), f.samplerate, f.uri
    43         print (result_str.format(*params))
     53        result_params = total_frames / float(f.samplerate), total_frames, int(total_frames/f.hop_size), f.samplerate, f.uri
     54        print (result_str.format(*result_params))
    4455        return total_frames
    4556
    46     def test_samplerate_hopsize(self):
    47         for p in list_of_sounds:
    48             for samplerate, hop_size in zip([0, 44100, 8000, 32000], [ 512, 512, 64, 256]):
    49                 f = source(p, samplerate, hop_size)
    50                 assert f.samplerate != 0
    51                 self.read_from_source(f)
     57    @params(*all_params)
     58    def test_samplerate_hopsize(self, hop_size, samplerate, soundfile):
     59        try:
     60            f = source(soundfile, samplerate, hop_size)
     61        except RuntimeError as e:
     62            self.skipTest('failed opening with hop_s = {:d}, samplerate = {:d} ({:s})'.format(hop_size, samplerate, e))
     63        assert f.samplerate != 0
     64        self.read_from_source(f)
    5265
    53     def test_samplerate_none(self):
    54         for p in list_of_sounds:
    55             f = source(p)
    56             assert f.samplerate != 0
    57             self.read_from_source(f)
     66    @params(*list_of_sounds)
     67    def test_samplerate_none(self, p):
     68        f = source(p)
     69        assert f.samplerate != 0
     70        self.read_from_source(f)
    5871
    59     def test_samplerate_0(self):
    60         for p in list_of_sounds:
    61             f = source(p, 0)
    62             assert f.samplerate != 0
    63             self.read_from_source(f)
     72    @params(*list_of_sounds)
     73    def test_samplerate_0(self, p):
     74        f = source(p, 0)
     75        assert f.samplerate != 0
     76        self.read_from_source(f)
    6477
    65     def test_wrong_samplerate(self):
    66         for p in list_of_sounds:
    67             try:
    68                 f = source(p, -1)
    69             except ValueError as e:
    70                 pass
    71             else:
    72                 self.fail('negative samplerate does not raise ValueError')
     78    @params(*list_of_sounds)
     79    def test_wrong_samplerate(self, p):
     80        try:
     81            f = source(p, -1)
     82        except ValueError as e:
     83            pass
     84        else:
     85            self.fail('negative samplerate does not raise ValueError')
    7386
    74     def test_wrong_hop_size(self):
    75         for p in list_of_sounds:
    76             try:
    77                 f = source(p, 0, -1)
    78             except ValueError as e:
    79                 pass
    80             else:
    81                 self.fail('negative hop_size does not raise ValueError')
     87    @params(*list_of_sounds)
     88    def test_wrong_hop_size(self, p):
     89        try:
     90            f = source(p, 0, -1)
     91        except ValueError as e:
     92            pass
     93        else:
     94            self.fail('negative hop_size does not raise ValueError')
    8295
    83     def test_zero_hop_size(self):
    84         for p in list_of_sounds:
    85             f = source(p, 0, 0)
    86             assert f.samplerate != 0
    87             assert f.hop_size != 0
    88             self.read_from_source(f)
     96    @params(*list_of_sounds)
     97    def test_zero_hop_size(self, p):
     98        f = source(p, 0, 0)
     99        assert f.samplerate != 0
     100        assert f.hop_size != 0
     101        self.read_from_source(f)
    89102
    90     def test_seek_to_half(self):
     103    @params(*list_of_sounds)
     104    def test_seek_to_half(self, p):
    91105        from random import randint
    92         for p in list_of_sounds:
    93             f = source(p, 0, 0)
    94             assert f.samplerate != 0
    95             assert f.hop_size != 0
    96             a = self.read_from_source(f)
    97             c = randint(0, a)
    98             f.seek(c)
    99             b = self.read_from_source(f)
    100             assert a == b + c
     106        f = source(p, 0, 0)
     107        assert f.samplerate != 0
     108        assert f.hop_size != 0
     109        a = self.read_from_source(f)
     110        c = randint(0, a)
     111        f.seek(c)
     112        b = self.read_from_source(f)
     113        assert a == b + c
    101114
    102     def test_duration(self):
    103         for p in list_of_sounds:
    104             total_frames = 0
    105             f = source(p)
    106             duration = f.duration
    107             while True:
    108                 vec, read = f()
    109                 total_frames += read
    110                 if read < f.hop_size: break
    111             self.assertEqual(duration, total_frames)
     115    @params(*list_of_sounds)
     116    def test_duration(self, p):
     117        total_frames = 0
     118        f = source(p)
     119        duration = f.duration
     120        while True:
     121            vec, read = f()
     122            total_frames += read
     123            if read < f.hop_size: break
     124        self.assertEqual(duration, total_frames)
    112125
    113126class aubio_source_readmulti_test_case(aubio_source_read_test_case):
     
    120133            if read < f.hop_size: break
    121134        result_str = "read {:.2f}s ({:d} frames in {:d} channels and {:d} blocks at {:d}Hz) from {:s}"
    122         params = total_frames / float(f.samplerate), total_frames, f.channels, int(total_frames/f.hop_size), f.samplerate, f.uri
    123         print (result_str.format(*params))
     135        result_params = total_frames / float(f.samplerate), total_frames, f.channels, int(total_frames/f.hop_size), f.samplerate, f.uri
     136        print (result_str.format(*result_params))
    124137        return total_frames
    125138
    126139if __name__ == '__main__':
    127     from unittest import main
     140    from nose2 import main
    128141    main()
Note: See TracChangeset for help on using the changeset viewer.