source: python/tests/test_cvec.py @ 1e4d90f

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 1e4d90f was cd53888, checked in by Paul Brossier <piem@piem.org>, 8 years ago

python/tests/test_cvec.py: check input sizes

  • Property mode set to 100755
File size: 3.1 KB
RevLine 
[5810ed4]1#! /usr/bin/env python
2
[9f8cd9f]3from numpy.testing import TestCase
[1a6ef2c]4from numpy.testing import assert_equal, assert_almost_equal
[cd53888]5from aubio import cvec, fvec, float_type
[9f8cd9f]6import numpy as np
[1a6ef2c]7
8class aubio_cvec_test_case(TestCase):
9
10    def test_vector_created_with_zeroes(self):
11        a = cvec(10)
[9f8cd9f]12        assert_equal(a.norm.shape[0], 10 / 2 + 1)
13        assert_equal(a.phas.shape[0], 10 / 2 + 1)
[1a6ef2c]14        a.norm[0]
15        assert_equal(a.norm, 0.)
16        assert_equal(a.phas, 0.)
17
18    def test_vector_assign_element(self):
19        a = cvec()
20        a.norm[0] = 1
21        assert_equal(a.norm[0], 1)
22        a.phas[0] = 1
23        assert_equal(a.phas[0], 1)
24
25    def test_vector_assign_element_end(self):
26        a = cvec()
27        a.norm[-1] = 1
28        assert_equal(a.norm[-1], 1)
29        assert_equal(a.norm[len(a.norm)-1], 1)
30        a.phas[-1] = 1
31        assert_equal(a.phas[-1], 1)
32        assert_equal(a.phas[len(a.phas)-1], 1)
33
34    def test_assign_cvec_norm_slice(self):
35        spec = cvec(1024)
36        spec.norm[40:100] = 100
[0e362b5]37        assert_equal(spec.norm[0:40], 0)
38        assert_equal(spec.norm[40:100], 100)
39        assert_equal(spec.norm[100:-1], 0)
40        assert_equal(spec.phas, 0)
[1a6ef2c]41
42    def test_assign_cvec_phas_slice(self):
43        spec = cvec(1024)
[9f8cd9f]44        spec.phas[39:-1] = -np.pi
[0e362b5]45        assert_equal(spec.phas[0:39], 0)
[9f8cd9f]46        assert_equal(spec.phas[39:-1], -np.pi)
[0e362b5]47        assert_equal(spec.norm, 0)
[1a6ef2c]48
[5dc1abc]49    def test_assign_cvec_with_other_cvec(self):
50        """ check dest cvec is still reachable after source was deleted """
51        spec = cvec(1024)
52        a = np.random.rand(1024/2+1).astype(float_type)
53        b = np.random.rand(1024/2+1).astype(float_type)
54        spec.norm = a
55        spec.phas = b
56        new_spec = spec
57        del spec
58        assert_equal(a, new_spec.norm)
59        assert_equal(b, new_spec.phas)
60        assert_equal(id(a), id(new_spec.norm))
61        assert_equal(id(b), id(new_spec.phas))
62
63    def test_pass_to_numpy(self):
64        spec = cvec(1024)
65        norm = spec.norm
66        phas = spec.phas
67        del spec
68        new_spec = cvec(1024)
69        new_spec.norm = norm
70        new_spec.phas = phas
71        assert_equal(norm, new_spec.norm)
72        assert_equal(phas, new_spec.phas)
73        assert_equal(id(norm), id(new_spec.norm))
74        assert_equal(id(phas), id(new_spec.phas))
75        del norm
76        del phas
77        assert_equal(new_spec.norm, 0.)
78        assert_equal(new_spec.phas, 0.)
79        del new_spec
80
[cd53888]81    def test_assign_norm_too_large(self):
82        a = cvec(512)
83        b = fvec(512//2+1 + 4)
84        with self.assertRaises(ValueError):
85            a.norm = b
86
87    def test_assign_norm_too_small(self):
88        a = cvec(512)
89        b = fvec(512//2+1 - 4)
90        with self.assertRaises(ValueError):
91            a.norm = b
92
93    def test_assign_phas_too_large(self):
94        a = cvec(512)
95        b = fvec(512//2+1 + 4)
96        with self.assertRaises(ValueError):
97            a.phas = b
98
99    def test_assign_phas_too_small(self):
100        a = cvec(512)
101        b = fvec(512//2+1 - 4)
102        with self.assertRaises(ValueError):
103            a.phas = b
104
[1a6ef2c]105if __name__ == '__main__':
[9f8cd9f]106    from nose2 import main
[1a6ef2c]107    main()
Note: See TracBrowser for help on using the repository browser.