[124166d] | 1 | #include "aubio.h" |
---|
| 2 | #include "utils_tests.h" |
---|
| 3 | |
---|
| 4 | #include "ai/tensor.h" |
---|
| 5 | |
---|
| 6 | int test_1d(void) |
---|
| 7 | { |
---|
| 8 | uint_t dims[1] = {11}; |
---|
| 9 | aubio_tensor_t *c = new_aubio_tensor(1, dims); |
---|
| 10 | fvec_t a; |
---|
| 11 | aubio_tensor_t cp; |
---|
| 12 | |
---|
| 13 | assert(c); |
---|
| 14 | |
---|
| 15 | c->data[1][0] = 1.; |
---|
| 16 | c->data[0][9] = 1.; |
---|
| 17 | c->data[1][1] = 1.; |
---|
| 18 | |
---|
| 19 | aubio_tensor_print(c); |
---|
| 20 | |
---|
| 21 | PRINT_MSG(" created from fvec_t \n"); |
---|
| 22 | // view tensor as fvec |
---|
| 23 | assert (aubio_tensor_as_fvec(c, &a) == 0); |
---|
| 24 | fvec_print(&a); |
---|
| 25 | // view fvec as tensor |
---|
| 26 | assert (aubio_fvec_as_tensor(&a, &cp) == 0); |
---|
| 27 | aubio_tensor_print(&cp); |
---|
| 28 | |
---|
| 29 | // wrong input |
---|
| 30 | assert (aubio_tensor_as_fvec(NULL, &a) != 0); |
---|
| 31 | assert (aubio_tensor_as_fvec(c, NULL) != 0); |
---|
| 32 | |
---|
| 33 | assert (aubio_fvec_as_tensor(NULL, &cp) != 0); |
---|
| 34 | assert (aubio_fvec_as_tensor(&a, NULL) != 0); |
---|
| 35 | |
---|
| 36 | del_aubio_tensor(c); |
---|
| 37 | return 0; |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | int test_2d(void) |
---|
| 41 | { |
---|
| 42 | uint_t dims[2] = {3, 2}; |
---|
| 43 | aubio_tensor_t *c = new_aubio_tensor(2, dims); |
---|
| 44 | fmat_t b; |
---|
| 45 | |
---|
| 46 | assert (c); |
---|
| 47 | c->data[0][1] = 2.; |
---|
| 48 | c->data[1][0] = 1.; |
---|
| 49 | |
---|
| 50 | aubio_tensor_print(c); |
---|
| 51 | assert (aubio_tensor_as_fmat(c, &b) == 0); |
---|
| 52 | fmat_print(&b); |
---|
| 53 | |
---|
| 54 | PRINT_MSG(" created from fmat_t\n"); |
---|
| 55 | fmat_t *m = new_fmat(dims[0], dims[1]); |
---|
| 56 | aubio_tensor_t cp; |
---|
| 57 | |
---|
| 58 | fmat_ones(m); |
---|
| 59 | m->data[0][1] = 0; |
---|
| 60 | fmat_print(m); |
---|
| 61 | assert (aubio_fmat_as_tensor(m, &cp) == 0); |
---|
| 62 | aubio_tensor_print(&cp); |
---|
| 63 | |
---|
| 64 | // view tensor as fvec |
---|
| 65 | fvec_t vp; |
---|
| 66 | assert (aubio_tensor_as_fvec(&cp, &vp) == 0); |
---|
| 67 | fvec_print (&vp); |
---|
| 68 | |
---|
| 69 | assert (aubio_tensor_as_fmat(NULL, &b) != 0); |
---|
| 70 | assert (aubio_tensor_as_fmat(c, NULL) != 0); |
---|
| 71 | |
---|
| 72 | assert (aubio_fmat_as_tensor (NULL, &cp) != 0); |
---|
| 73 | assert (aubio_fmat_as_tensor (m, NULL) != 0); |
---|
| 74 | |
---|
| 75 | del_fmat(m); |
---|
| 76 | del_aubio_tensor(c); |
---|
| 77 | |
---|
| 78 | return 0; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | int test_3d(void) |
---|
| 82 | { |
---|
| 83 | uint_t dims[3] = {3, 2, 3}; |
---|
| 84 | aubio_tensor_t *c = new_aubio_tensor(3, dims); |
---|
| 85 | assert (c); |
---|
| 86 | |
---|
| 87 | c->data[0][0 * 3 + 0] = 1; |
---|
| 88 | c->data[1][0 * 3 + 1] = 2; |
---|
| 89 | c->data[1][1 * 3 + 1] = 2; |
---|
| 90 | c->data[2][1 * 3 + 2] = 3; |
---|
| 91 | |
---|
| 92 | aubio_tensor_print(c); |
---|
| 93 | |
---|
| 94 | // view tensor as fmat |
---|
| 95 | fmat_t vm; |
---|
| 96 | assert (aubio_tensor_as_fmat(c, &vm) == 0); |
---|
| 97 | fmat_print (&vm); |
---|
| 98 | |
---|
| 99 | del_aubio_tensor(c); |
---|
| 100 | return 0; |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | int test_4d(void) |
---|
| 104 | { |
---|
| 105 | uint_t d1 = 3, d2 = 2, d3 = 2, d4 = 4; |
---|
| 106 | uint_t dims[4] = {d1, d2, d3, d4}; |
---|
| 107 | aubio_tensor_t *c = new_aubio_tensor(4, dims); |
---|
| 108 | |
---|
| 109 | c->data[0][0] = 1; |
---|
| 110 | c->data[1][1 * d3 * d4 + 1 * d4 + 2] = 2; |
---|
| 111 | c->data[0][2 * d2 * d3 * d4 + 1 * d3 * d4 + 1 * d4 + 3] = c->size; |
---|
| 112 | |
---|
| 113 | aubio_tensor_print(c); |
---|
| 114 | |
---|
| 115 | del_aubio_tensor(c); |
---|
| 116 | return 0; |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | int test_sizes(void) |
---|
| 120 | { |
---|
| 121 | uint_t d1 = 3, d2 = 2, d3 = 2, d4 = 4; |
---|
| 122 | uint_t dims[4] = {d1, d2, d3, d4}; |
---|
| 123 | aubio_tensor_t *a = new_aubio_tensor(4, dims); |
---|
| 124 | aubio_tensor_t *b = new_aubio_tensor(3, dims); |
---|
| 125 | |
---|
| 126 | assert (!aubio_tensor_have_same_size(a, b)); |
---|
| 127 | |
---|
| 128 | del_aubio_tensor(b); |
---|
| 129 | dims[2] += 1; |
---|
| 130 | b = new_aubio_tensor(4, dims); |
---|
| 131 | assert (!aubio_tensor_have_same_size(a, b)); |
---|
| 132 | del_aubio_tensor(b); |
---|
| 133 | dims[2] -= 1; |
---|
| 134 | |
---|
| 135 | dims[0] -= 1; |
---|
| 136 | dims[1] += 1; |
---|
| 137 | b = new_aubio_tensor(4, dims); |
---|
| 138 | assert (!aubio_tensor_have_same_size(a, b)); |
---|
| 139 | del_aubio_tensor(b); |
---|
| 140 | |
---|
| 141 | dims[0] += 1; |
---|
| 142 | dims[1] -= 1; |
---|
| 143 | b = new_aubio_tensor(4, dims); |
---|
| 144 | assert (aubio_tensor_have_same_size(a, b)); |
---|
| 145 | |
---|
| 146 | assert (!aubio_tensor_have_same_size(NULL, b)); |
---|
| 147 | assert (!aubio_tensor_have_same_size(a, NULL)); |
---|
| 148 | |
---|
| 149 | del_aubio_tensor(a); |
---|
| 150 | del_aubio_tensor(b); |
---|
| 151 | return 0; |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | int test_wrong_args(void) |
---|
| 155 | { |
---|
| 156 | uint_t dims[3] = {3, 2, 1}; |
---|
| 157 | assert (new_aubio_tensor(0, dims) == NULL); |
---|
| 158 | dims[1] = -1; |
---|
| 159 | assert (new_aubio_tensor(2, dims) == NULL); |
---|
| 160 | return 0; |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | int test_subtensor(void) { |
---|
| 164 | uint_t i; |
---|
| 165 | uint_t d1 = 4, d2 = 2, d3 = 2, d4 = 3; |
---|
| 166 | uint_t dims[4] = {d1, d2, d3, d4}; |
---|
| 167 | uint_t ndim = 3; |
---|
| 168 | aubio_tensor_t *t = new_aubio_tensor(ndim, dims); |
---|
| 169 | |
---|
| 170 | assert (t != NULL); |
---|
| 171 | assert (t->data != NULL); |
---|
| 172 | |
---|
| 173 | for (i = 0; i < t->size; i++) { |
---|
| 174 | t->data[0][i] = (smpl_t)i; |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | aubio_tensor_print(t); |
---|
| 178 | |
---|
| 179 | aubio_tensor_t st; |
---|
| 180 | |
---|
| 181 | PRINT_MSG(" getting subtensor 1\n"); |
---|
| 182 | assert (aubio_tensor_get_subtensor(t, 1, &st) == 0); |
---|
| 183 | assert (st.ndim = ndim - 1); |
---|
| 184 | assert (st.shape[0] == d2); |
---|
| 185 | assert (st.shape[1] == d3); |
---|
| 186 | assert (st.shape[2] == 0); |
---|
| 187 | assert (st.buffer[0] == 4); |
---|
| 188 | assert (st.buffer[3] == 7); |
---|
| 189 | assert (st.data == NULL); |
---|
| 190 | aubio_tensor_print(&st); |
---|
| 191 | |
---|
| 192 | PRINT_MSG(" check subtensor 3\n"); |
---|
| 193 | assert (aubio_tensor_get_subtensor(t, 3, &st) == 0); |
---|
| 194 | aubio_tensor_print(&st); |
---|
| 195 | |
---|
| 196 | aubio_tensor_t sst; |
---|
| 197 | |
---|
| 198 | PRINT_MSG(" check subtensor 1 of subtensor 3\n"); |
---|
| 199 | assert (aubio_tensor_get_subtensor(&st, 1, &sst) == 0); |
---|
| 200 | assert (sst.ndim = ndim - 2); |
---|
| 201 | assert (sst.shape[0] == d3); |
---|
| 202 | assert (sst.shape[1] == 0); |
---|
| 203 | assert (sst.buffer[0] == 14); |
---|
| 204 | assert (sst.buffer[1] == 15); |
---|
| 205 | aubio_tensor_print(&sst); |
---|
| 206 | |
---|
| 207 | // can get a single element as a tensor |
---|
| 208 | assert (aubio_tensor_get_subtensor(&sst, 1, &st) == 0); |
---|
| 209 | assert (st.buffer[0] == sst.buffer[1]); |
---|
| 210 | assert (&st.buffer[0] == &sst.buffer[1]); |
---|
| 211 | aubio_tensor_print(&st); |
---|
| 212 | |
---|
| 213 | // getting wrong tensors |
---|
| 214 | assert (aubio_tensor_get_subtensor(NULL, 0, &sst) != 0); |
---|
| 215 | assert (aubio_tensor_get_subtensor(&st, -2, &sst) != 0); |
---|
| 216 | assert (aubio_tensor_get_subtensor(&st, 2, &sst) != 0); |
---|
| 217 | assert (aubio_tensor_get_subtensor(&st, 0, NULL) != 0); |
---|
| 218 | |
---|
| 219 | del_aubio_tensor(t); |
---|
| 220 | return 0; |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | int test_subtensor_tricks (void) |
---|
| 224 | { |
---|
| 225 | uint_t d1 = 4, d2 = 2, d3 = 2, d4 = 3; |
---|
| 226 | uint_t dims[4] = {d1, d2, d3, d4}; |
---|
| 227 | uint_t ndim = 3; |
---|
| 228 | aubio_tensor_t *t = new_aubio_tensor(ndim, dims); |
---|
| 229 | // manually delete content |
---|
| 230 | free(t->data[0]); |
---|
| 231 | t->data[0] = NULL; |
---|
| 232 | |
---|
| 233 | del_aubio_tensor(t); |
---|
| 234 | return 0; |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | int test_maxtensor(void) |
---|
| 238 | { |
---|
| 239 | uint_t d1 = 3, d2 = 2, d3 = 2, d4 = 4; |
---|
| 240 | uint_t dims[4] = {d1, d2, d3, d4}; |
---|
| 241 | aubio_tensor_t *c = new_aubio_tensor(4, dims); |
---|
| 242 | |
---|
| 243 | c->data[0][0] = -200; |
---|
| 244 | c->data[1][1 * d3 * d4 + 1 * d4 + 2] = 2; |
---|
| 245 | c->data[0][2 * d2 * d3 * d4 + 1 * d3 * d4 + 1 * d4 + 3] = c->size; |
---|
| 246 | |
---|
| 247 | assert (aubio_tensor_max(c) == c->size); |
---|
| 248 | |
---|
| 249 | del_aubio_tensor(c); |
---|
| 250 | return 0; |
---|
| 251 | } |
---|
| 252 | |
---|
| 253 | int test_get_shape_string(void) |
---|
| 254 | { |
---|
| 255 | uint_t d1 = 3, d2 = 2, d3 = 2, d4 = 4; |
---|
| 256 | uint_t dims[4] = {d1, d2, d3, d4}; |
---|
| 257 | aubio_tensor_t *c = new_aubio_tensor(4, dims); |
---|
| 258 | |
---|
| 259 | c->data[0][0] = -200; |
---|
| 260 | c->data[1][1 * d3 * d4 + 1 * d4 + 2] = 2; |
---|
| 261 | c->data[0][2 * d2 * d3 * d4 + 1 * d3 * d4 + 1 * d4 + 3] = c->size; |
---|
| 262 | |
---|
| 263 | const char_t *shape_string = aubio_tensor_get_shape_string(c); |
---|
| 264 | PRINT_MSG("found shape string %s\n", shape_string); |
---|
| 265 | |
---|
| 266 | del_aubio_tensor(c); |
---|
| 267 | |
---|
| 268 | assert (aubio_tensor_get_shape_string(NULL) == NULL); |
---|
| 269 | |
---|
| 270 | return 0; |
---|
| 271 | } |
---|
| 272 | |
---|
| 273 | int main(void) { |
---|
| 274 | PRINT_MSG("testing 1d tensors\n"); |
---|
| 275 | assert (test_1d() == 0); |
---|
| 276 | PRINT_MSG("testing 2d tensors\n"); |
---|
| 277 | assert (test_2d() == 0); |
---|
| 278 | PRINT_MSG("testing 3d tensors\n"); |
---|
| 279 | assert (test_3d() == 0); |
---|
| 280 | PRINT_MSG("testing 4d tensors\n"); |
---|
| 281 | assert (test_4d() == 0); |
---|
| 282 | PRINT_MSG("testing aubio_tensor_have_same_size\n"); |
---|
| 283 | assert (test_sizes() == 0); |
---|
| 284 | PRINT_MSG("testing new_aubio_tensor with wrong arguments\n"); |
---|
| 285 | assert (test_wrong_args() == 0); |
---|
| 286 | PRINT_MSG("testing subtensors\n"); |
---|
| 287 | assert (test_subtensor() == 0); |
---|
| 288 | PRINT_MSG("testing subtensors\n"); |
---|
| 289 | assert (test_subtensor_tricks() == 0); |
---|
| 290 | PRINT_MSG("testing max\n"); |
---|
| 291 | assert (test_maxtensor() == 0); |
---|
| 292 | PRINT_MSG("testing get_shape_string\n"); |
---|
| 293 | assert (test_get_shape_string() == 0); |
---|
| 294 | return 0; |
---|
| 295 | } |
---|