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)
17 assert_equal(a.norm, 0.)
18 assert_equal(a.phas, 0.)
20 def test_vector_assign_element(self):
23 assert_equal(a.norm[0], 1)
25 assert_equal(a.phas[0], 1)
27 def test_vector_assign_element_end(self):
30 assert_equal(a.norm[-1], 1)
31 assert_equal(a.norm[len(a.norm)-1], 1)
33 assert_equal(a.phas[-1], 1)
34 assert_equal(a.phas[len(a.phas)-1], 1)
36 def test_assign_cvec_norm_slice(self):
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)
44 def test_assign_cvec_phas_slice(self):
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)
51 def test_assign_cvec_with_other_cvec(self):
52 """ check dest cvec is still reachable after source was deleted """
54 a = np.random.rand(1024//2+1).astype(float_type)
55 b = np.random.rand(1024//2+1).astype(float_type)
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))
65 def test_pass_to_numpy(self):
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))
79 assert_equal(new_spec.norm, 0.)
80 assert_equal(new_spec.phas, 0.)
83 def test_assign_norm_too_large(self):
85 b = fvec(512//2+1 + 4)
86 with self.assertRaises(ValueError):
89 def test_assign_norm_too_small(self):
91 b = fvec(512//2+1 - 4)
92 with self.assertRaises(ValueError):
95 def test_assign_phas_too_large(self):
97 b = fvec(512//2+1 + 4)
98 with self.assertRaises(ValueError):
101 def test_assign_phas_too_small(self):
103 b = fvec(512//2+1 - 4)
104 with self.assertRaises(ValueError):
107 def test_cvec_repr(self):
110 expected_repr = "aubio cvec of {:d} elements".format(win_s//2+1)
111 self.assertEqual(repr(c), expected_repr)
113 class aubio_cvec_wrong_norm_input(TestCase):
115 def test_wrong_length(self):
116 with self.assertRaises(ValueError):
119 def test_set_norm_with_scalar(self):
121 with self.assertRaises(ValueError):
124 def test_set_norm_with_scalar_array(self):
126 with self.assertRaises(ValueError):
127 a.norm = np.ndarray(1, dtype = 'int')
129 def test_set_norm_with_int_array(self):
131 with self.assertRaises(ValueError):
132 a.norm = np.zeros(512//2+1, dtype = 'int')
134 def test_set_norm_with_wrong_float_array(self):
136 with self.assertRaises(ValueError):
137 a.norm = np.zeros(512//2+1, dtype = wrong_type)
139 def test_set_norm_with_wrong_2d_array(self):
141 with self.assertRaises(ValueError):
142 a.norm = np.zeros((512//2+1, 2), dtype = float_type)
144 if __name__ == '__main__':