1 | #! /usr/bin/env python |
---|
2 | |
---|
3 | from numpy.testing import TestCase |
---|
4 | from numpy.testing import assert_equal, assert_almost_equal |
---|
5 | from aubio import cvec, fvec, float_type |
---|
6 | import numpy as np |
---|
7 | |
---|
8 | class aubio_cvec_test_case(TestCase): |
---|
9 | |
---|
10 | def test_vector_created_with_zeroes(self): |
---|
11 | a = cvec(10) |
---|
12 | assert_equal(a.norm.shape[0], 10 / 2 + 1) |
---|
13 | assert_equal(a.phas.shape[0], 10 / 2 + 1) |
---|
14 | a.norm[0] |
---|
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 | if __name__ == '__main__': |
---|
106 | from nose2 import main |
---|
107 | main() |
---|