[tests] improve test-fmat
[aubio.git] / tests / src / test-fmat.c
1 #include "aubio.h"
2 #include "utils_tests.h"
3
4 // create a new matrix and fill it with i * 1. + j * .1, where i is the row,
5 // and j the column.
6
7 void assert_fmat_all_equal(fmat_t *mat, smpl_t scalar)
8 {
9   uint_t i, j;
10   for ( i = 0; i < mat->height; i++ ) {
11     for ( j = 0; j < mat->length; j++ ) {
12       assert(mat->data[i][j] == scalar);
13     }
14   }
15 }
16
17 int main (void)
18 {
19   uint_t i, j;
20   uint_t height = 3, length = 9;
21
22   // create fmat_t object
23   fmat_t * mat = new_fmat(height, length);
24   fmat_t * other_mat = new_fmat(height, length);
25
26   assert(mat);
27   assert(other_mat);
28
29   assert(mat->length == length);
30   assert(mat->height == height);
31
32   for (i = 0; i < mat->height; i++) {
33     for (j = 0; j < mat->length; j++) {
34       // all elements are already initialized to 0.
35       assert(mat->data[i][j] == 0);
36       // setting element of row i, column j
37       mat->data[i][j] = i * 10. + j;
38     }
39   }
40
41   // print out matrix
42   fmat_print(mat);
43
44   // helpers
45   fmat_rev(mat);
46   fmat_print(mat);
47   for (i = 0; i < mat->height; i++) {
48     for (j = 0; j < mat->length; j++) {
49       assert(mat->data[i][j] == i * 10. + mat->length - 1. - j);
50     }
51   }
52
53   fmat_set_sample(mat, 3, 1, 1);
54   assert(fmat_get_sample(mat, 1, 1) == 3.);
55
56   fmat_ones(mat);
57   assert_fmat_all_equal(mat, 1.);
58
59   fmat_set(other_mat, .5);
60   assert_fmat_all_equal(other_mat, .5);
61
62   fmat_weight(mat, other_mat);
63   assert_fmat_all_equal(mat, .5);
64
65   fvec_t channel_onstack;
66   fvec_t *channel = &channel_onstack;
67   fmat_get_channel(mat, 1, channel);
68   assert(channel->data == mat->data[1]);
69
70   // copy of the same size
71   fmat_copy(mat, other_mat);
72   del_fmat(other_mat);
73
74   // copy to undersized
75   other_mat = new_fmat(height - 1, length);
76   fmat_copy(mat, other_mat);
77   del_fmat(other_mat);
78
79   // copy from undersized
80   other_mat = new_fmat(height, length + 1);
81   fmat_copy(mat, other_mat);
82
83   // wrong parameters
84   assert(new_fmat(-1, length) == NULL);
85   assert(new_fmat(height, -1) == NULL);
86
87   // methods for wrappers with opaque structure
88   assert (fmat_get_channel_data(mat, 0) == mat->data[0]);
89   assert (fmat_get_data(mat) == mat->data);
90
91   if (mat)
92     del_fmat(mat);
93   if (other_mat)
94     del_fmat(other_mat);
95   return 0;
96 }