shithub: aubio

Download patch

ref: 2b1b38a3ec612c7b4b0f99c98af600528e5261b8
parent: 8e76c71be6136f4e2289f8c67e422097f5e10dfe
author: Paul Brossier <piem@piem.org>
date: Fri Dec 21 07:44:22 EST 2018

[tests] improve test-fvec

--- a/tests/src/test-fvec.c
+++ b/tests/src/test-fvec.c
@@ -1,13 +1,27 @@
 #include "aubio.h"
 #include "utils_tests.h"
 
+void assert_fvec_all_equal(fvec_t *vec, smpl_t scalar)
+{
+  uint_t i;
+  for (i = 0; i < vec->length; i++) {
+    assert(vec->data[i] == scalar);
+  }
+}
+
 int main (void)
 {
-  uint_t vec_size = 10, i;
-  fvec_t * vec = new_fvec (vec_size);
+  uint_t length = 10;
+  uint_t i;
 
+  fvec_t * vec = new_fvec (length);
+  fvec_t * other_vec = new_fvec (length);
+
+  assert (vec);
+  assert (other_vec);
+
   // vec->length matches requested size
-  assert(vec->length == vec_size);
+  assert(vec->length == length);
 
   // all elements are initialized to `0.`
   for ( i = 0; i < vec->length; i++ ) {
@@ -14,20 +28,14 @@
     assert(vec->data[i] == 0.);
   }
 
-  // all elements can be set to `0.`
-  fvec_zeros(vec);
-  for ( i = 0; i < vec->length; i++ ) {
-    assert(vec->data[i] == 0.);
-  }
-  fvec_print(vec);
-
   // all elements can be set to `1.`
   fvec_ones(vec);
-  for ( i = 0; i < vec->length; i++ ) {
-    assert(vec->data[i] == 1.);
-  }
-  fvec_print(vec);
+  assert_fvec_all_equal(vec, 1.);
 
+  // all elements can be set to `0.`
+  fvec_zeros(vec);
+  assert_fvec_all_equal(vec, 0.);
+
   // each element can be accessed directly
   for ( i = 0; i < vec->length; i++ ) {
     vec->data[i] = i;
@@ -35,9 +43,31 @@
   }
   fvec_print(vec);
 
-  // now destroys the vector
-  del_fvec(vec);
+  fvec_set_sample(vec, 3, 2);
+  assert(fvec_get_sample(vec, 2) == 3);
 
+  assert(fvec_get_data(vec) == vec->data);
+
+  // wrong parameters
+  assert(new_fvec(-1) == NULL);
+
+  // copy to an identical size works
+  fvec_copy(vec, other_vec);
+  del_fvec(other_vec);
+
+  // copy to a different size fail
+  other_vec = new_fvec(length + 1);
+  fvec_copy(vec, other_vec);
+  del_fvec(other_vec);
+
+  // copy to a different size fail
+  other_vec = new_fvec(length - 1);
+  fvec_copy(vec, other_vec);
+
+  // now destroys the vector
+  if (vec)
+    del_fvec(vec);
+  if (other_vec)
+    del_fvec(other_vec);
   return 0;
 }
-