source: python/tests/test_cvec.py @ 11526e6

feature/crepe_org
Last change on this file since 11526e6 was fc144a5, checked in by Paul Brossier <piem@piem.org>, 6 years ago

[tests] avoid some imports, add missing main

  • Property mode set to 100755
File size: 4.3 KB
Line 
1#! /usr/bin/env python
2
3import numpy as np
4from numpy.testing import TestCase, assert_equal
5from aubio import cvec, fvec, float_type
6
7wrong_type = 'float32' if float_type == 'float64' else 'float64'
8
9class aubio_cvec_test_case(TestCase):
10
11    def test_vector_created_with_zeroes(self):
12        a = cvec(10)
13        assert_equal(a.norm.shape[0], 10 // 2 + 1)
14        assert_equal(a.phas.shape[0], 10 // 2 + 1)
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
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)
41
42    def test_assign_cvec_phas_slice(self):
43        spec = cvec(1024)
44        spec.phas[39:-1] = -np.pi
45        assert_equal(spec.phas[0:39], 0)
46        assert_equal(spec.phas[39:-1], -np.pi)
47        assert_equal(spec.norm, 0)
48
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
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
105    def test_cvec_repr(self):
106        win_s = 512
107        c = cvec(win_s)
108        expected_repr = "aubio cvec of {:d} elements".format(win_s//2+1)
109        self.assertEqual(repr(c), expected_repr)
110
111class aubio_cvec_wrong_norm_input(TestCase):
112
113    def test_wrong_length(self):
114        with self.assertRaises(ValueError):
115            cvec(-1)
116
117    def test_set_norm_with_scalar(self):
118        a = cvec(512)
119        with self.assertRaises(ValueError):
120            a.norm = 1
121
122    def test_set_norm_with_scalar_array(self):
123        a = cvec(512)
124        with self.assertRaises(ValueError):
125            a.norm = np.ndarray(1, dtype = 'int')
126
127    def test_set_norm_with_int_array(self):
128        a = cvec(512)
129        with self.assertRaises(ValueError):
130            a.norm = np.zeros(512//2+1, dtype = 'int')
131
132    def test_set_norm_with_wrong_float_array(self):
133        a = cvec(512)
134        with self.assertRaises(ValueError):
135            a.norm = np.zeros(512//2+1, dtype = wrong_type)
136
137    def test_set_norm_with_wrong_2d_array(self):
138        a = cvec(512)
139        with self.assertRaises(ValueError):
140            a.norm = np.zeros((512//2+1, 2), dtype = float_type)
141
142if __name__ == '__main__':
143    from unittest import main
144    main()
Note: See TracBrowser for help on using the repository browser.