shithub: aubio

Download patch

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

[tests] improve test-cvec

--- a/tests/src/test-cvec.c
+++ b/tests/src/test-cvec.c
@@ -3,46 +3,75 @@
 
 int main (void)
 {
-  uint_t i, window_size = 16; // window size
-  cvec_t * complex_vector = new_cvec (window_size); // input buffer
-  uint_t rand_times = 4;
+  uint_t i, window_size = 16;
+  cvec_t * complex_vector = new_cvec(window_size);
+  cvec_t * other_cvector = new_cvec(window_size);
 
-  utils_init_random();
+  assert(cvec_norm_get_data(complex_vector) == complex_vector->norm);
+  assert(cvec_phas_get_data(complex_vector) == complex_vector->phas);
+  assert(complex_vector->length == window_size / 2 + 1);
 
-  while (rand_times -- ) {
-    // fill with random phas and norm
-    for ( i = 0; i < complex_vector->length; i++ ) {
-      complex_vector->norm[i] = ( 2. / RAND_MAX * random() - 1. );
-      complex_vector->phas[i] = ( 2. / RAND_MAX * random() - 1. ) * M_PI;
-    }
-    // print the vector
-    cvec_print(complex_vector);
+  // all elements are initialized to 0
+  for ( i = 0; i < complex_vector->length; i++ ) {
+    assert( complex_vector->norm[i] == 0. );
+    assert( complex_vector->phas[i] == 0. );
   }
 
-  // set all vector elements to `0`
-  cvec_norm_zeros(complex_vector);
+  cvec_norm_set_sample(complex_vector, 2., 1);
+  assert(cvec_norm_get_sample(complex_vector, 1));
+
+  cvec_phas_set_sample(complex_vector, 2., 1);
+  assert(cvec_phas_get_sample(complex_vector, 1));
+
+  cvec_print(complex_vector);
+
+  // set all norm and phas elements to 0
+  cvec_zeros(complex_vector);
   for ( i = 0; i < complex_vector->length; i++ ) {
     assert( complex_vector->norm[i] == 0. );
-    // assert( complex_vector->phas[i] == 0 );
+    assert( complex_vector->phas[i] == 0. );
   }
-  cvec_print(complex_vector);
 
-  // set all vector elements to `1`
+  // set all norm elements to 1
   cvec_norm_ones(complex_vector);
   for ( i = 0; i < complex_vector->length; i++ ) {
     assert( complex_vector->norm[i] == 1. );
-    // assert( complex_vector->phas[i] == 0 );
   }
-  cvec_print(complex_vector);
 
-  cvec_zeros(complex_vector);
-  cvec_phas_zeros(complex_vector);
+  // set all norm elements to 0
   cvec_norm_zeros(complex_vector);
-  cvec_norm_ones(complex_vector);
+  for ( i = 0; i < complex_vector->length; i++ ) {
+    assert( complex_vector->norm[i] == 0. );
+  }
+
+  // set all phas elements to 1
   cvec_phas_ones(complex_vector);
+  for ( i = 0; i < complex_vector->length; i++ ) {
+    assert( complex_vector->phas[i] == 1. );
+  }
+
+  // set all phas elements to 0
+  cvec_phas_zeros(complex_vector);
+  for ( i = 0; i < complex_vector->length; i++ ) {
+    assert( complex_vector->phas[i] == 0. );
+  }
+
+  cvec_copy(complex_vector, other_cvector);
+  // copy to self
   cvec_copy(complex_vector, complex_vector);
+  // copy to a different size fails
+  del_cvec(other_cvector);
+  other_cvector = new_cvec(window_size + 2);
+  cvec_copy(complex_vector, other_cvector);
 
-  // destroy it
-  del_cvec(complex_vector);
+  if (complex_vector)
+    del_cvec(complex_vector);
+  if (other_cvector)
+    del_cvec(other_cvector);
+
+  // wrong parameters
+  assert(new_cvec(-1) == NULL);
+  assert(new_cvec(0) == NULL);
+
   return 0;
 }