source: python/tests/test_cvec.py @ 288c193

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

python/tests: fix most prospect warnings

  • Property mode set to 100755
File size: 4.3 KB
Line 
1#! /usr/bin/env python
2
3from unittest import main
4import numpy as np
5from numpy.testing import TestCase, assert_equal
6from aubio import cvec, fvec, float_type
7
8wrong_type = 'float32' if float_type == 'float64' else 'float64'
9
10class aubio_cvec_test_case(TestCase):
11
12    def test_vector_created_with_zeroes(self):
13        a = cvec(10)
14        assert_equal(a.norm.shape[0], 10 / 2 + 1)
15        assert_equal(a.phas.shape[0], 10 / 2 + 1)
16        _ = a.norm[0]
17        assert_equal(a.norm, 0.)
18        assert_equal(a.phas, 0.)
19
20    def test_vector_assign_element(self):
21        a = cvec()
22        a.norm[0] = 1
23        assert_equal(a.norm[0], 1)
24        a.phas[0] = 1
25        assert_equal(a.phas[0], 1)
26
27    def test_vector_assign_element_end(self):
28        a = cvec()
29        a.norm[-1] = 1
30        assert_equal(a.norm[-1], 1)
31        assert_equal(a.norm[len(a.norm)-1], 1)
32        a.phas[-1] = 1
33        assert_equal(a.phas[-1], 1)
34        assert_equal(a.phas[len(a.phas)-1], 1)
35
36    def test_assign_cvec_norm_slice(self):
37        spec = cvec(1024)
38        spec.norm[40:100] = 100
39        assert_equal(spec.norm[0:40], 0)
40        assert_equal(spec.norm[40:100], 100)
41        assert_equal(spec.norm[100:-1], 0)
42        assert_equal(spec.phas, 0)
43
44    def test_assign_cvec_phas_slice(self):
45        spec = cvec(1024)
46        spec.phas[39:-1] = -np.pi
47        assert_equal(spec.phas[0:39], 0)
48        assert_equal(spec.phas[39:-1], -np.pi)
49        assert_equal(spec.norm, 0)
50
51    def test_assign_cvec_with_other_cvec(self):
52        """ check dest cvec is still reachable after source was deleted """
53        spec = cvec(1024)
54        a = np.random.rand(1024//2+1).astype(float_type)
55        b = np.random.rand(1024//2+1).astype(float_type)
56        spec.norm = a
57        spec.phas = b
58        new_spec = spec
59        del spec
60        assert_equal(a, new_spec.norm)
61        assert_equal(b, new_spec.phas)
62        assert_equal(id(a), id(new_spec.norm))
63        assert_equal(id(b), id(new_spec.phas))
64
65    def test_pass_to_numpy(self):
66        spec = cvec(1024)
67        norm = spec.norm
68        phas = spec.phas
69        del spec
70        new_spec = cvec(1024)
71        new_spec.norm = norm
72        new_spec.phas = phas
73        assert_equal(norm, new_spec.norm)
74        assert_equal(phas, new_spec.phas)
75        assert_equal(id(norm), id(new_spec.norm))
76        assert_equal(id(phas), id(new_spec.phas))
77        del norm
78        del phas
79        assert_equal(new_spec.norm, 0.)
80        assert_equal(new_spec.phas, 0.)
81        del new_spec
82
83    def test_assign_norm_too_large(self):
84        a = cvec(512)
85        b = fvec(512//2+1 + 4)
86        with self.assertRaises(ValueError):
87            a.norm = b
88
89    def test_assign_norm_too_small(self):
90        a = cvec(512)
91        b = fvec(512//2+1 - 4)
92        with self.assertRaises(ValueError):
93            a.norm = b
94
95    def test_assign_phas_too_large(self):
96        a = cvec(512)
97        b = fvec(512//2+1 + 4)
98        with self.assertRaises(ValueError):
99            a.phas = b
100
101    def test_assign_phas_too_small(self):
102        a = cvec(512)
103        b = fvec(512//2+1 - 4)
104        with self.assertRaises(ValueError):
105            a.phas = b
106
107    def test_cvec_repr(self):
108        win_s = 512
109        c = cvec(win_s)
110        expected_repr = "aubio cvec of {:d} elements".format(win_s//2+1)
111        self.assertEqual(repr(c), expected_repr)
112
113class aubio_cvec_wrong_norm_input(TestCase):
114
115    def test_wrong_length(self):
116        with self.assertRaises(ValueError):
117            cvec(-1)
118
119    def test_set_norm_with_scalar(self):
120        a = cvec(512)
121        with self.assertRaises(ValueError):
122            a.norm = 1
123
124    def test_set_norm_with_scalar_array(self):
125        a = cvec(512)
126        with self.assertRaises(ValueError):
127            a.norm = np.ndarray(1, dtype = 'int')
128
129    def test_set_norm_with_int_array(self):
130        a = cvec(512)
131        with self.assertRaises(ValueError):
132            a.norm = np.zeros(512//2+1, dtype = 'int')
133
134    def test_set_norm_with_wrong_float_array(self):
135        a = cvec(512)
136        with self.assertRaises(ValueError):
137            a.norm = np.zeros(512//2+1, dtype = wrong_type)
138
139    def test_set_norm_with_wrong_2d_array(self):
140        a = cvec(512)
141        with self.assertRaises(ValueError):
142            a.norm = np.zeros((512//2+1, 2), dtype = float_type)
143
144if __name__ == '__main__':
145    main()
Note: See TracBrowser for help on using the repository browser.