3 from unittest import main
5 from numpy.testing import TestCase, assert_equal
6 from aubio import cvec, fvec, float_type
8 wrong_type = 'float32' if float_type == 'float64' else 'float64'
10 class aubio_cvec_test_case(TestCase):
12 def test_vector_created_with_zeroes(self):
14 assert_equal(a.norm.shape[0], 10 // 2 + 1)
15 assert_equal(a.phas.shape[0], 10 // 2 + 1)
16 assert_equal(a.norm, 0.)
17 assert_equal(a.phas, 0.)
19 def test_vector_assign_element(self):
22 assert_equal(a.norm[0], 1)
24 assert_equal(a.phas[0], 1)
26 def test_vector_assign_element_end(self):
29 assert_equal(a.norm[-1], 1)
30 assert_equal(a.norm[len(a.norm)-1], 1)
32 assert_equal(a.phas[-1], 1)
33 assert_equal(a.phas[len(a.phas)-1], 1)
35 def test_assign_cvec_norm_slice(self):
37 spec.norm[40:100] = 100
38 assert_equal(spec.norm[0:40], 0)
39 assert_equal(spec.norm[40:100], 100)
40 assert_equal(spec.norm[100:-1], 0)
41 assert_equal(spec.phas, 0)
43 def test_assign_cvec_phas_slice(self):
45 spec.phas[39:-1] = -np.pi
46 assert_equal(spec.phas[0:39], 0)
47 assert_equal(spec.phas[39:-1], -np.pi)
48 assert_equal(spec.norm, 0)
50 def test_assign_cvec_with_other_cvec(self):
51 """ check dest cvec is still reachable after source was deleted """
53 a = np.random.rand(1024//2+1).astype(float_type)
54 b = np.random.rand(1024//2+1).astype(float_type)
59 assert_equal(a, new_spec.norm)
60 assert_equal(b, new_spec.phas)
61 assert_equal(id(a), id(new_spec.norm))
62 assert_equal(id(b), id(new_spec.phas))
64 def test_pass_to_numpy(self):
72 assert_equal(norm, new_spec.norm)
73 assert_equal(phas, new_spec.phas)
74 assert_equal(id(norm), id(new_spec.norm))
75 assert_equal(id(phas), id(new_spec.phas))
78 assert_equal(new_spec.norm, 0.)
79 assert_equal(new_spec.phas, 0.)
82 def test_assign_norm_too_large(self):
84 b = fvec(512//2+1 + 4)
85 with self.assertRaises(ValueError):
88 def test_assign_norm_too_small(self):
90 b = fvec(512//2+1 - 4)
91 with self.assertRaises(ValueError):
94 def test_assign_phas_too_large(self):
96 b = fvec(512//2+1 + 4)
97 with self.assertRaises(ValueError):
100 def test_assign_phas_too_small(self):
102 b = fvec(512//2+1 - 4)
103 with self.assertRaises(ValueError):
106 def test_cvec_repr(self):
109 expected_repr = "aubio cvec of {:d} elements".format(win_s//2+1)
110 self.assertEqual(repr(c), expected_repr)
112 class aubio_cvec_wrong_norm_input(TestCase):
114 def test_wrong_length(self):
115 with self.assertRaises(ValueError):
118 def test_set_norm_with_scalar(self):
120 with self.assertRaises(ValueError):
123 def test_set_norm_with_scalar_array(self):
125 with self.assertRaises(ValueError):
126 a.norm = np.ndarray(1, dtype = 'int')
128 def test_set_norm_with_int_array(self):
130 with self.assertRaises(ValueError):
131 a.norm = np.zeros(512//2+1, dtype = 'int')
133 def test_set_norm_with_wrong_float_array(self):
135 with self.assertRaises(ValueError):
136 a.norm = np.zeros(512//2+1, dtype = wrong_type)
138 def test_set_norm_with_wrong_2d_array(self):
140 with self.assertRaises(ValueError):
141 a.norm = np.zeros((512//2+1, 2), dtype = float_type)
143 if __name__ == '__main__':