4 from numpy.testing import TestCase, assert_equal
5 from aubio import cvec, fvec, float_type
7 wrong_type = 'float32' if float_type == 'float64' else 'float64'
9 class aubio_cvec_test_case(TestCase):
11 def test_vector_created_with_zeroes(self):
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.)
18 def test_vector_assign_element(self):
21 assert_equal(a.norm[0], 1)
23 assert_equal(a.phas[0], 1)
25 def test_vector_assign_element_end(self):
28 assert_equal(a.norm[-1], 1)
29 assert_equal(a.norm[len(a.norm)-1], 1)
31 assert_equal(a.phas[-1], 1)
32 assert_equal(a.phas[len(a.phas)-1], 1)
34 def test_assign_cvec_norm_slice(self):
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)
42 def test_assign_cvec_phas_slice(self):
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)
49 def test_assign_cvec_with_other_cvec(self):
50 """ check dest cvec is still reachable after source was deleted """
52 a = np.random.rand(1024//2+1).astype(float_type)
53 b = np.random.rand(1024//2+1).astype(float_type)
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))
63 def test_pass_to_numpy(self):
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))
77 assert_equal(new_spec.norm, 0.)
78 assert_equal(new_spec.phas, 0.)
81 def test_assign_norm_too_large(self):
83 b = fvec(512//2+1 + 4)
84 with self.assertRaises(ValueError):
87 def test_assign_norm_too_small(self):
89 b = fvec(512//2+1 - 4)
90 with self.assertRaises(ValueError):
93 def test_assign_phas_too_large(self):
95 b = fvec(512//2+1 + 4)
96 with self.assertRaises(ValueError):
99 def test_assign_phas_too_small(self):
101 b = fvec(512//2+1 - 4)
102 with self.assertRaises(ValueError):
105 def test_cvec_repr(self):
108 expected_repr = "aubio cvec of {:d} elements".format(win_s//2+1)
109 self.assertEqual(repr(c), expected_repr)
111 class aubio_cvec_wrong_norm_input(TestCase):
113 def test_wrong_length(self):
114 with self.assertRaises(ValueError):
117 def test_set_norm_with_scalar(self):
119 with self.assertRaises(ValueError):
122 def test_set_norm_with_scalar_array(self):
124 with self.assertRaises(ValueError):
125 a.norm = np.ndarray(1, dtype = 'int')
127 def test_set_norm_with_int_array(self):
129 with self.assertRaises(ValueError):
130 a.norm = np.zeros(512//2+1, dtype = 'int')
132 def test_set_norm_with_wrong_float_array(self):
134 with self.assertRaises(ValueError):
135 a.norm = np.zeros(512//2+1, dtype = wrong_type)
137 def test_set_norm_with_wrong_2d_array(self):
139 with self.assertRaises(ValueError):
140 a.norm = np.zeros((512//2+1, 2), dtype = float_type)
142 if __name__ == '__main__':
143 from unittest import main