summaryrefslogtreecommitdiff
path: root/python/tests/test_cvec.py
blob: 53bf8dfdd39e21853ee667713f851dadde22d808 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#! /usr/bin/env python

from unittest import main
import numpy as np
from numpy.testing import TestCase, assert_equal
from aubio import cvec, fvec, float_type

wrong_type = 'float32' if float_type == 'float64' else 'float64'

class aubio_cvec_test_case(TestCase):

    def test_vector_created_with_zeroes(self):
        a = cvec(10)
        assert_equal(a.norm.shape[0], 10 / 2 + 1)
        assert_equal(a.phas.shape[0], 10 / 2 + 1)
        _ = a.norm[0]
        assert_equal(a.norm, 0.)
        assert_equal(a.phas, 0.)

    def test_vector_assign_element(self):
        a = cvec()
        a.norm[0] = 1
        assert_equal(a.norm[0], 1)
        a.phas[0] = 1
        assert_equal(a.phas[0], 1)

    def test_vector_assign_element_end(self):
        a = cvec()
        a.norm[-1] = 1
        assert_equal(a.norm[-1], 1)
        assert_equal(a.norm[len(a.norm)-1], 1)
        a.phas[-1] = 1
        assert_equal(a.phas[-1], 1)
        assert_equal(a.phas[len(a.phas)-1], 1)

    def test_assign_cvec_norm_slice(self):
        spec = cvec(1024)
        spec.norm[40:100] = 100
        assert_equal(spec.norm[0:40], 0)
        assert_equal(spec.norm[40:100], 100)
        assert_equal(spec.norm[100:-1], 0)
        assert_equal(spec.phas, 0)

    def test_assign_cvec_phas_slice(self):
        spec = cvec(1024)
        spec.phas[39:-1] = -np.pi
        assert_equal(spec.phas[0:39], 0)
        assert_equal(spec.phas[39:-1], -np.pi)
        assert_equal(spec.norm, 0)

    def test_assign_cvec_with_other_cvec(self):
        """ check dest cvec is still reachable after source was deleted """
        spec = cvec(1024)
        a = np.random.rand(1024//2+1).astype(float_type)
        b = np.random.rand(1024//2+1).astype(float_type)
        spec.norm = a
        spec.phas = b
        new_spec = spec
        del spec
        assert_equal(a, new_spec.norm)
        assert_equal(b, new_spec.phas)
        assert_equal(id(a), id(new_spec.norm))
        assert_equal(id(b), id(new_spec.phas))

    def test_pass_to_numpy(self):
        spec = cvec(1024)
        norm = spec.norm
        phas = spec.phas
        del spec
        new_spec = cvec(1024)
        new_spec.norm = norm
        new_spec.phas = phas
        assert_equal(norm, new_spec.norm)
        assert_equal(phas, new_spec.phas)
        assert_equal(id(norm), id(new_spec.norm))
        assert_equal(id(phas), id(new_spec.phas))
        del norm
        del phas
        assert_equal(new_spec.norm, 0.)
        assert_equal(new_spec.phas, 0.)
        del new_spec

    def test_assign_norm_too_large(self):
        a = cvec(512)
        b = fvec(512//2+1 + 4)
        with self.assertRaises(ValueError):
            a.norm = b

    def test_assign_norm_too_small(self):
        a = cvec(512)
        b = fvec(512//2+1 - 4)
        with self.assertRaises(ValueError):
            a.norm = b

    def test_assign_phas_too_large(self):
        a = cvec(512)
        b = fvec(512//2+1 + 4)
        with self.assertRaises(ValueError):
            a.phas = b

    def test_assign_phas_too_small(self):
        a = cvec(512)
        b = fvec(512//2+1 - 4)
        with self.assertRaises(ValueError):
            a.phas = b

    def test_cvec_repr(self):
        win_s = 512
        c = cvec(win_s)
        expected_repr = "aubio cvec of {:d} elements".format(win_s//2+1)
        self.assertEqual(repr(c), expected_repr)

class aubio_cvec_wrong_norm_input(TestCase):

    def test_wrong_length(self):
        with self.assertRaises(ValueError):
            cvec(-1)

    def test_set_norm_with_scalar(self):
        a = cvec(512)
        with self.assertRaises(ValueError):
            a.norm = 1

    def test_set_norm_with_scalar_array(self):
        a = cvec(512)
        with self.assertRaises(ValueError):
            a.norm = np.ndarray(1, dtype = 'int')

    def test_set_norm_with_int_array(self):
        a = cvec(512)
        with self.assertRaises(ValueError):
            a.norm = np.zeros(512//2+1, dtype = 'int')

    def test_set_norm_with_wrong_float_array(self):
        a = cvec(512)
        with self.assertRaises(ValueError):
            a.norm = np.zeros(512//2+1, dtype = wrong_type)

    def test_set_norm_with_wrong_2d_array(self):
        a = cvec(512)
        with self.assertRaises(ValueError):
            a.norm = np.zeros((512//2+1, 2), dtype = float_type)

if __name__ == '__main__':
    main()