[5810ed4] | 1 | #! /usr/bin/env python |
---|
| 2 | |
---|
[0b6d23d] | 3 | from unittest import main |
---|
[9f8cd9f] | 4 | import numpy as np |
---|
[0b6d23d] | 5 | from numpy.testing import TestCase, assert_equal |
---|
| 6 | from aubio import cvec, fvec, float_type |
---|
[1a6ef2c] | 7 | |
---|
[c09efca] | 8 | wrong_type = 'float32' if float_type == 'float64' else 'float64' |
---|
| 9 | |
---|
[1a6ef2c] | 10 | class aubio_cvec_test_case(TestCase): |
---|
| 11 | |
---|
| 12 | def test_vector_created_with_zeroes(self): |
---|
| 13 | a = cvec(10) |
---|
[67f0092] | 14 | assert_equal(a.norm.shape[0], 10 // 2 + 1) |
---|
| 15 | assert_equal(a.phas.shape[0], 10 // 2 + 1) |
---|
[1a6ef2c] | 16 | assert_equal(a.norm, 0.) |
---|
| 17 | assert_equal(a.phas, 0.) |
---|
| 18 | |
---|
| 19 | def test_vector_assign_element(self): |
---|
| 20 | a = cvec() |
---|
| 21 | a.norm[0] = 1 |
---|
| 22 | assert_equal(a.norm[0], 1) |
---|
| 23 | a.phas[0] = 1 |
---|
| 24 | assert_equal(a.phas[0], 1) |
---|
| 25 | |
---|
| 26 | def test_vector_assign_element_end(self): |
---|
| 27 | a = cvec() |
---|
| 28 | a.norm[-1] = 1 |
---|
| 29 | assert_equal(a.norm[-1], 1) |
---|
| 30 | assert_equal(a.norm[len(a.norm)-1], 1) |
---|
| 31 | a.phas[-1] = 1 |
---|
| 32 | assert_equal(a.phas[-1], 1) |
---|
| 33 | assert_equal(a.phas[len(a.phas)-1], 1) |
---|
| 34 | |
---|
| 35 | def test_assign_cvec_norm_slice(self): |
---|
| 36 | spec = cvec(1024) |
---|
| 37 | spec.norm[40:100] = 100 |
---|
[0e362b5] | 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) |
---|
[1a6ef2c] | 42 | |
---|
| 43 | def test_assign_cvec_phas_slice(self): |
---|
| 44 | spec = cvec(1024) |
---|
[9f8cd9f] | 45 | spec.phas[39:-1] = -np.pi |
---|
[0e362b5] | 46 | assert_equal(spec.phas[0:39], 0) |
---|
[9f8cd9f] | 47 | assert_equal(spec.phas[39:-1], -np.pi) |
---|
[0e362b5] | 48 | assert_equal(spec.norm, 0) |
---|
[1a6ef2c] | 49 | |
---|
[5dc1abc] | 50 | def test_assign_cvec_with_other_cvec(self): |
---|
| 51 | """ check dest cvec is still reachable after source was deleted """ |
---|
| 52 | spec = cvec(1024) |
---|
[c09efca] | 53 | a = np.random.rand(1024//2+1).astype(float_type) |
---|
| 54 | b = np.random.rand(1024//2+1).astype(float_type) |
---|
[5dc1abc] | 55 | spec.norm = a |
---|
| 56 | spec.phas = b |
---|
| 57 | new_spec = spec |
---|
| 58 | del spec |
---|
| 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)) |
---|
| 63 | |
---|
| 64 | def test_pass_to_numpy(self): |
---|
| 65 | spec = cvec(1024) |
---|
| 66 | norm = spec.norm |
---|
| 67 | phas = spec.phas |
---|
| 68 | del spec |
---|
| 69 | new_spec = cvec(1024) |
---|
| 70 | new_spec.norm = norm |
---|
| 71 | new_spec.phas = phas |
---|
| 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)) |
---|
| 76 | del norm |
---|
| 77 | del phas |
---|
| 78 | assert_equal(new_spec.norm, 0.) |
---|
| 79 | assert_equal(new_spec.phas, 0.) |
---|
| 80 | del new_spec |
---|
| 81 | |
---|
[cd53888] | 82 | def test_assign_norm_too_large(self): |
---|
| 83 | a = cvec(512) |
---|
| 84 | b = fvec(512//2+1 + 4) |
---|
| 85 | with self.assertRaises(ValueError): |
---|
| 86 | a.norm = b |
---|
| 87 | |
---|
| 88 | def test_assign_norm_too_small(self): |
---|
| 89 | a = cvec(512) |
---|
| 90 | b = fvec(512//2+1 - 4) |
---|
| 91 | with self.assertRaises(ValueError): |
---|
| 92 | a.norm = b |
---|
| 93 | |
---|
| 94 | def test_assign_phas_too_large(self): |
---|
| 95 | a = cvec(512) |
---|
| 96 | b = fvec(512//2+1 + 4) |
---|
| 97 | with self.assertRaises(ValueError): |
---|
| 98 | a.phas = b |
---|
| 99 | |
---|
| 100 | def test_assign_phas_too_small(self): |
---|
| 101 | a = cvec(512) |
---|
| 102 | b = fvec(512//2+1 - 4) |
---|
| 103 | with self.assertRaises(ValueError): |
---|
| 104 | a.phas = b |
---|
| 105 | |
---|
[c09efca] | 106 | def test_cvec_repr(self): |
---|
| 107 | win_s = 512 |
---|
| 108 | c = cvec(win_s) |
---|
| 109 | expected_repr = "aubio cvec of {:d} elements".format(win_s//2+1) |
---|
| 110 | self.assertEqual(repr(c), expected_repr) |
---|
| 111 | |
---|
| 112 | class aubio_cvec_wrong_norm_input(TestCase): |
---|
| 113 | |
---|
| 114 | def test_wrong_length(self): |
---|
| 115 | with self.assertRaises(ValueError): |
---|
| 116 | cvec(-1) |
---|
| 117 | |
---|
| 118 | def test_set_norm_with_scalar(self): |
---|
| 119 | a = cvec(512) |
---|
| 120 | with self.assertRaises(ValueError): |
---|
| 121 | a.norm = 1 |
---|
| 122 | |
---|
| 123 | def test_set_norm_with_scalar_array(self): |
---|
| 124 | a = cvec(512) |
---|
| 125 | with self.assertRaises(ValueError): |
---|
| 126 | a.norm = np.ndarray(1, dtype = 'int') |
---|
| 127 | |
---|
| 128 | def test_set_norm_with_int_array(self): |
---|
| 129 | a = cvec(512) |
---|
| 130 | with self.assertRaises(ValueError): |
---|
| 131 | a.norm = np.zeros(512//2+1, dtype = 'int') |
---|
| 132 | |
---|
| 133 | def test_set_norm_with_wrong_float_array(self): |
---|
| 134 | a = cvec(512) |
---|
| 135 | with self.assertRaises(ValueError): |
---|
| 136 | a.norm = np.zeros(512//2+1, dtype = wrong_type) |
---|
| 137 | |
---|
| 138 | def test_set_norm_with_wrong_2d_array(self): |
---|
| 139 | a = cvec(512) |
---|
| 140 | with self.assertRaises(ValueError): |
---|
| 141 | a.norm = np.zeros((512//2+1, 2), dtype = float_type) |
---|
| 142 | |
---|
[1a6ef2c] | 143 | if __name__ == '__main__': |
---|
| 144 | main() |
---|