Beispiel #1
0
void *
run_test
(
   void * data
)
{

   // Unpack argument. //
   const test_case_t test_case = *(test_case_t *) data;

   // -- Initialize -- //

   // Register signal handlers. This will allow execution
   // to return to main thread in case of crash.
   signal(SIGSEGV, terminate_thread);
   
   // Get test name and fixture (test function). //
   fixture_t fixture = test_case.fixture;

   // Initialize variable, empty buffers... //
   test_case_init();
   unredirect_stderr();
   unredirect_stdout();

   //  -- Run the test -- //
   (*fixture)();
   
   // -- Clean -- //
   unredirect_stderr();
   unredirect_stdout();
   test_case_clean();

   return NULL;

}
Beispiel #2
0
void
test_err_handler
(void)
{

   int obs[] = {6,2,2,3,2,0,1,2,5,2,0,0,0};
   zinb_par_t *par = NULL;

   set_zinb_err_handler(dummy_handler);

   redirect_stderr();
   set_alloc_failure_countdown_to(0);
   par = mle_zinb(obs, 13);
   reset_alloc();
   unredirect_stderr();
   test_assert(par == NULL);
   test_assert(strcmp(caught_in_stderr(), "dummy") == 0);

   redirect_stderr();
   set_alloc_failure_countdown_to(0);
   par = mle_zinb(obs, 13);
   reset_alloc();
   unredirect_stderr();
   test_assert(par == NULL);
   test_assert(strcmp(caught_in_stderr(), "dummy") == 0);

   // Reset error handler.
   set_zinb_err_handler(NULL);

   redirect_stderr();
   set_alloc_failure_countdown_to(0);
   par = mle_zinb(obs, 13);
   reset_alloc();
   unredirect_stderr();
   test_assert(par == NULL);
   test_assert(strncmp(caught_in_stderr(), "memory error", 12) == 0);

   redirect_stderr();
   set_alloc_failure_countdown_to(0);
   par = mle_nb(obs, 13);
   reset_alloc();
   unredirect_stderr();
   test_assert(par == NULL);
   test_assert(strncmp(caught_in_stderr(), "memory error", 12) == 0);

   free(par);

}
Beispiel #3
0
void
test_compress_histo
(void)
{

   histo_t *histo = new_histo();
   test_assert_critical(histo != NULL);
   for (size_t i = 0 ; i < 4096 ; i += 2) {
      test_assert(histo_push(&histo, i) == 0);
   }

   tab_t *tab;
   tab = compress_histo(histo);
   test_assert_critical(tab != NULL);
   test_assert(tab->size == 2048);
   for (size_t i = 0 ; i < tab->size ; i++) {
      test_assert(tab->val[i] == 2*i);
      test_assert(tab->num[i] == 1);
   }

   free(tab);

   redirect_stderr();
   set_alloc_failure_rate_to(1.0);
   tab = compress_histo(histo);
   reset_alloc();
   unredirect_stderr();
   test_assert(tab == NULL);
   test_assert(strcmp(caught_in_stderr(), "") != 0);

   free(histo);
   return;

}
Beispiel #4
0
void
test_io_stream_open
(void)
{
   iostream_t * stream;

   // Test invalid arguments.
   redirect_stderr();
   stream = io_stream_open("fake-file.txt");
   test_assert(stream == NULL);
   unredirect_stderr();

   // Load files.
   stream = io_stream_open("examples/io_input.raw");
   test_assert(stream != NULL);
   io_stream_free(stream);
 
   stream = io_stream_open("examples/io_input.fasta");
   test_assert(stream != NULL);
   io_stream_free(stream);

   stream = io_stream_open("examples/io_input.fastq");
   test_assert(stream != NULL);
   io_stream_free(stream);
}
Beispiel #5
0
void
test_mem_index_read
(void)
{
   redirect_stderr();
   
   index_t * index = index_build("examples/repeats.fa", "test_base30");
   test_assert_critical(index != NULL);
   test_assert(index_ann_new(25, 1, 1, index) == 0);

   // Set alloc failure rate to 0.1.
   set_alloc_failure_rate_to(0.1);
   for (int i = 0; i < 1000; i++) {
      index_t * index_i = index_read("test_base30");
      index_free(index_i);
   }
   reset_alloc();

   // Set alloc countdown 0->10.
   for (int i = 0; i <= 200; i++) {
      set_alloc_failure_countdown_to(i);
      index_t * index_i = index_read("test_base30");
      index_free(index_i);
   }
   reset_alloc();

   index_free(index);
   unredirect_stderr();
}
Beispiel #6
0
void
terminate_thread
(
   int sig
)
{
   unredirect_stderr();

   // If test was not failed so far, update display.
   if (!TEST_CASE_FAILED) {
      update_display_failed();
   }

   fprintf(stderr, "caught SIGTERM (interrupting)\n");

   // Label the test case as failed. //
   TEST_CASE_FAILED = 1;

   // Clean it all. //
   test_case_clean();

   // Return to main thread.
   pthread_exit(NULL);

}
Beispiel #7
0
void
fail_critical
(
   const char * assertion,
   const char * file,
         int    lineno,
   const char * function
)
// Handle failure of critical (fatal) assert statements.
// Set the test status to failed and update the display
// and print error message.
{
   // If test was not failed so far, update display.
   if (!TEST_CASE_FAILED) {
      update_display_failed();
   }

   TEST_CASE_FAILED = 1;

   // That's the end of the test case. We can
   // take back stderr without worries.
   unredirect_stderr();
   fprintf(stderr, "assertion failed in %s, %s:%d: `%s' (CRITICAL)\n",
         function, file, lineno, assertion);
   fflush(stderr);

}
Beispiel #8
0
void
test_fail_mle_nb
(void)
{

   int obs[] = {6,2,2,3,2,0,1,2,5,2};
   zinb_par_t *par = NULL;

   redirect_stderr();
   set_alloc_failure_countdown_to(0);
   par = mle_nb(obs, 10);
   reset_alloc();
   unredirect_stderr();
   test_assert(par == NULL);
   test_assert(strncmp(caught_in_stderr(), "memory error", 12) == 0);

   redirect_stderr();
   set_alloc_failure_countdown_to(1);
   par = mle_nb(obs, 10);
   reset_alloc();
   unredirect_stderr();
   test_assert(par == NULL);
   test_assert(strncmp(caught_in_stderr(), "memory error", 12) == 0);

   redirect_stderr();
   set_alloc_failure_countdown_to(2);
   par = mle_nb(obs, 10);
   reset_alloc();
   unredirect_stderr();
   test_assert(par == NULL);
   test_assert(strncmp(caught_in_stderr(), "memory error", 12) == 0);

   redirect_stderr();
   set_alloc_failure_countdown_to(3);
   par = mle_nb(obs, 10);
   reset_alloc();
   unredirect_stderr();
   test_assert_critical(par != NULL);
   test_assert(strncmp(caught_in_stderr(), "$", 1) == 0);

   free(par);

   return;

}
Beispiel #9
0
void
assert_fail_critical
(
   const char         * assertion,
   const char         * file,
   const unsigned int   lineno,
   const char         * function
)
{
   TEST_CASE_FAILED = 1;
   unredirect_stderr();
   fprintf(stderr, "assertion failed in %s(), %s:%d: `%s' (CRITICAL)\n",
         function, file, lineno, assertion);
   fflush(stderr);
}
Beispiel #10
0
void
test_mem_sar_get_range
(void)
{
   sym_t * sym = sym_new_dna();
   test_assert_critical(sym != NULL);

   txt_t * txt = txt_new(sym);
   test_assert_critical(txt != NULL);
   test_assert(txt_append("TAGCNTCGACA", txt) == 0);
   test_assert(txt_commit_seq("seq0",txt) == 0);
   test_assert(txt_commit_rc(txt) == 0);

   sar_t * sar = sar_build(txt);
   test_assert_critical(sar != NULL);

   int64_t * sa_buf = malloc(30*sizeof(int64_t));
   test_assert_critical(sa_buf != NULL);

   redirect_stderr();
   // Set alloc failure rate to 0.1.
   set_alloc_failure_rate_to(0.1);
   for (int i = 0; i < 100; i++) {
      sar_get_range(0,10,sa_buf,sar);
      sar_get_range(10,5,sa_buf,sar);
      sar_get_range(19,30,sa_buf,sar);
   }
   reset_alloc();

   // Set alloc countdown 0->10.
   for (int i = 0; i <= 10; i++) {
      set_alloc_failure_countdown_to(i);
      sar_get_range(0,10,sa_buf,sar);
      sar_get_range(10,5,sa_buf,sar);
      sar_get_range(19,30,sa_buf,sar);
   }
   reset_alloc();

   free(sa_buf);
   sar_free(sar);
   txt_free(txt);
   sym_free(sym);
   unredirect_stderr();
}
Beispiel #11
0
void
test_mem_sar_file
(void)
{
   sym_t * sym = sym_new_dna();
   test_assert_critical(sym != NULL);

   txt_t * txt = txt_new(sym);
   test_assert_critical(txt != NULL);
   test_assert(txt_append("TAGCNTCGACA", txt) == 0);
   test_assert(txt_commit_seq("seq0",txt) == 0);
   test_assert(txt_commit_rc(txt) == 0);

   sar_t * sar = sar_build(txt);
   test_assert_critical(sar != NULL);

   sar_t * sar_i;

   redirect_stderr();
   // Set alloc failure rate to 0.1.
   set_alloc_failure_rate_to(0.1);
   for (int i = 0; i < 100; i++) {
      sar_file_write("test30.sar", sar);
      sar_i = sar_file_read("test30.sar");
      sar_free(sar_i);
   }
   reset_alloc();

   // Set alloc countdown 0->10.
   for (int i = 0; i <= 10; i++) {
      set_alloc_failure_countdown_to(i);
      sar_file_write("test30.sar", sar);
      sar_i = sar_file_read("test30.sar");
      sar_free(sar_i);
   }
   reset_alloc();

   sar_free(sar);
   txt_free(txt);
   sym_free(sym);
   unredirect_stderr();
}
Beispiel #12
0
void
fail_non_critical
(
   const char * assertion,
   const char * file,
         int    lineno,
   const char * function
)
// Handle failure of non critical assert statements.
// Set the test status to failed and update the display
// and print error message.
{

   // If first failed assertion of the test
   // case, update display for the user.
   if (!TEST_CASE_FAILED) {
      update_display_failed();
   }

   TEST_CASE_FAILED = 1;

   // If stderr is redirected, we will need to
   // take it back to display the error message.
   int toggle_stderr = STDERR_OFF;
   if (toggle_stderr) unredirect_stderr();

   // Don't show more than 'MAX_N_ERROR_MSG', unless
   // user passed the --showall or -a option.
   if (N_ERROR_MSG++ < MAX_N_ERROR_MSG || SHOWALL) {
      fprintf(stderr, "%s:%d: `%s'\n", file, lineno, assertion);
   }
   else if (N_ERROR_MSG == MAX_N_ERROR_MSG + 1) {
      fprintf(stderr, "more than %d failed assertions...\n",
            MAX_N_ERROR_MSG);
   }

   // Flush stderr and put it back as it was (ie redirect
   // it if it was redirected, or leave it as is otherwise).
   fflush(stderr);
   if (toggle_stderr) redirect_stderr();

}
Beispiel #13
0
void
terminate_thread
(
   int sig
)
{

   fprintf(stderr, "caught SIGTERM (interrupting)\n");

   // Label the test case as failed. //
   TEST_CASE_FAILED = 1;

   // Clean it all. //
   unredirect_stderr();
   test_case_clean();

   // Return to main thread.
   pthread_exit(NULL);

}
Beispiel #14
0
void
test_histo_push
(void)
{

   histo_t *histo;
   histo = new_histo();
   test_assert_critical(histo != NULL);
   for (size_t i = 0 ; i < (1 << 16) ; i++) {
      test_assert(histo_push(&histo, i) == 0);
   }
   test_assert(histo->size == (1 << 16));
   for (size_t i = 0 ; i < (1 << 16) ; i++) {
      test_assert(histo->num[i] == 1);
   }
   free(histo);

   histo = new_histo();
   test_assert_critical(histo != NULL);
   for (size_t i = (1 << 16) ; i > 0 ; i--) {
      test_assert(histo_push(&histo, i-1) == 0);
   }
   test_assert(histo->size == 2*((1 << 16)-1));
   for (size_t i = 0 ; i < (1 << 16) ; i++) {
      test_assert(histo->num[i] == 1);
   }
   free(histo);

   histo = new_histo();
   test_assert_critical(histo != NULL);
   redirect_stderr();
   set_alloc_failure_rate_to(1.0);
   test_assert(histo_push(&histo, HISTO_INIT_SIZE) == 1);
   reset_alloc();
   unredirect_stderr();
   test_assert(strcmp(caught_in_stderr(), "") != 0);
   free(histo);

   return;

}
Beispiel #15
0
void
test_mem_index_build
(void)
{
   redirect_stderr();
   // Set alloc failure rate to 0.1.
   set_alloc_failure_rate_to(0.1);
   for (int i = 0; i < 1000; i++) {
      index_t * index = index_build("examples/repeats.fa", "test_base20");
      index_free(index);
   }
   reset_alloc();

   // Set alloc countdown 0->10.
   for (int i = 0; i <= 200; i++) {
      set_alloc_failure_countdown_to(i);
      index_t * index = index_build("examples/repeats.fa", "test_base20");
      index_free(index);
   }
   reset_alloc();
   unredirect_stderr();
}
Beispiel #16
0
void
test_mem_index_ann_new
(void)
{
   redirect_stderr();
   
   index_t * index = index_build("examples/repeats.fa", "test_base20");
   test_assert_critical(index != NULL);

   // Set alloc failure rate to 0.1.
   set_alloc_failure_rate_to(0.1);
   for (int i = 0; i < 1000; i++) {
      if (index_ann_new(25, 1, 1, index) == 0) {
         unlink("test_base20.ann.25.1");
         ann_free(index->ann[0]);
         free(index->ann);
         index->ann = NULL;
         index->ann_cnt = 0;
      }
   }
   reset_alloc();

   // Set alloc countdown 0->10.
   for (int i = 0; i <= 1000; i++) {
      set_alloc_failure_countdown_to(i);
      if (index_ann_new(25, 1, 1, index) == 0) {
         unlink("test_base20.ann.25.1");
         ann_free(index->ann[0]);
         free(index->ann);
         index->ann = NULL;
         index->ann_cnt = 0;
      }
   }
   reset_alloc();

   index_free(index);
   unredirect_stderr();
}
Beispiel #17
0
void
assert_fail_non_critical
(
   const char         * assertion,
   const char         * file,
   const unsigned int   lineno,
   const char         * function
)
{
   TEST_CASE_FAILED = 1;
   if (++N_ERROR_MESSAGES > MAX_N_ERROR_MESSAGES + 1) return;
   int switch_stderr = STDERR_OFF;
   if (switch_stderr) unredirect_stderr();
   if (N_ERROR_MESSAGES == MAX_N_ERROR_MESSAGES + 1) {
      fprintf(stderr, "more than %d failed assertions...\n",
            MAX_N_ERROR_MESSAGES);
   }
   else {
      fprintf(stderr, "assertion failed in %s(), %s:%d: `%s'\n",
            function, file, lineno, assertion);
   }
   fflush(stderr);
   if (switch_stderr) redirect_stderr();
}
Beispiel #18
0
void
test_new_histo
(void)
{

   histo_t *histo;
   histo = new_histo();
   test_assert_critical(histo != NULL);
   for (size_t i = 0 ; i < HISTO_INIT_SIZE ; i++) {
      test_assert(histo->num[i] == 0);
   }
   free(histo);

   redirect_stderr();
   set_alloc_failure_rate_to(1.0);
   histo = new_histo();
   reset_alloc();
   unredirect_stderr();
   test_assert(histo == NULL);
   test_assert(strcmp(caught_in_stderr(), "") != 0);

   return;

}
Beispiel #19
0
void
test_io_stream_read_seq
(void)
{
   iostream_t * stream;
   gstack_t   * stack;
   seqread_t  * read;

   // Invalid arguments.
   redirect_stderr();
   test_assert(io_stream_read_seq(NULL) == NULL);
   unredirect_stderr();

   // Load files.
   // Read RAW file.
   stream = io_stream_open("examples/io_input.raw");
   test_assert_critical(stream != NULL);
   
   stack = io_stream_read_seq(stream);
   test_assert_critical(stack != NULL);
   test_assert(gstack_num_elm(stack) == 6);
   
   read = seqread_pop(stack);
   test_assert(read != NULL);
   test_assert(strcmp(seqread_seq(read), "NNNNNNLLYYYYJDFLS") == 0);
   test_assert(strcmp(seqread_tag(read), "6") == 0);
   seqread_free(read);
   read = seqread_pop(stack);
   test_assert(read != NULL);
   test_assert(strcmp(seqread_seq(read), "NNNNNNNNACGTACGCC") == 0);
   test_assert(strcmp(seqread_tag(read), "5") == 0);
   seqread_free(read);
   read = seqread_pop(stack);
   test_assert(read != NULL);
   test_assert(strcmp(seqread_seq(read), "ACGTACATGTATGACAC") == 0);
   test_assert(strcmp(seqread_tag(read), "4") == 0);
   seqread_free(read);

   gstack_free(stack);
   io_stream_free(stream);

   // Read FASTA file.
   stream = io_stream_open_buf("examples/io_input.fasta", 80);
   test_assert_critical(stream != NULL);

   stack = io_stream_read_seq(stream);
   test_assert_critical(stack != NULL);
   test_assert(gstack_num_elm(stack) == 4);

   read = seqread_pop(stack);
   test_assert(read != NULL);
   test_assert(strcmp(seqread_seq(read), "ACGTACATGTATGACAC") == 0);
   test_assert(strcmp(seqread_tag(read), "seq4") == 0);
   seqread_free(read);

   read = seqread_pop(stack);
   test_assert(read != NULL);
   test_assert(strcmp(seqread_seq(read), "AGTCGANTATACNTACG") == 0);
   test_assert(strcmp(seqread_tag(read), "seq3") == 0);
   seqread_free(read);

   read = seqread_pop(stack);
   test_assert(read != NULL);
   test_assert(strcmp(seqread_seq(read), "GTATCGACTACGAGCTA") == 0);
   test_assert(strcmp(seqread_tag(read), "seq2") == 0);
   seqread_free(read);

   read = seqread_pop(stack);
   test_assert(read != NULL);
   test_assert(strcmp(seqread_seq(read), "ATGCGTACGTCGTATCA") == 0);
   test_assert(strcmp(seqread_tag(read), "seq1") == 0);
   seqread_free(read);
   gstack_free(stack);
   
   stack = io_stream_read_seq(stream);
   test_assert_critical(stack != NULL);
   test_assert(gstack_num_elm(stack) == 2);

   read = seqread_pop(stack);
   test_assert(read != NULL);
   test_assert(strcmp(seqread_seq(read), "NNNNNNLLYYYYJDFLS") == 0);
   test_assert(strcmp(seqread_tag(read), "seq6") == 0);
   seqread_free(read);
   read = seqread_pop(stack);
   test_assert(read != NULL);
   test_assert(strcmp(seqread_seq(read), "NNNNNNNNACGTACGCC") == 0);
   test_assert(strcmp(seqread_tag(read), "seq5") == 0);
   seqread_free(read);


   gstack_free(stack);
   io_stream_free(stream);

   // Read FASTQ file.
   stream = io_stream_open_buf("examples/io_input.fastq", 50);
   test_assert_critical(stream != NULL);
   
   stack = io_stream_read_seq(stream);
   test_assert_critical(stack != NULL);
   test_assert(gstack_num_elm(stack) == 2);

   read = seqread_pop(stack);
   test_assert(read != NULL);
   test_assert(strcmp(seqread_seq(read), "GTATCGACTACGAGCTA") == 0);
   test_assert(strcmp(seqread_tag(read), "seq2") == 0);
   test_assert(strcmp(seqread_qscore(read), "BACBABCA0ACBB00AC") == 0);   
   seqread_free(read);

   read = seqread_pop(stack);
   test_assert(read != NULL);
   test_assert(strcmp(seqread_seq(read), "ATGCGTACGTCGTATCA") == 0);
   test_assert(strcmp(seqread_tag(read), "seq1") == 0);
   test_assert(strcmp(seqread_qscore(read), "12391284194819241") == 0);   
   seqread_free(read);
   gstack_free(stack);

   stack = io_stream_read_seq(stream);
   test_assert_critical(stack != NULL);
   test_assert(gstack_num_elm(stack) == 2);
   gstack_free(stack);

   stack = io_stream_read_seq(stream);
   test_assert_critical(stack != NULL);
   test_assert(gstack_num_elm(stack) == 2);
   gstack_free(stack);

   stack = io_stream_read_seq(stream);
   test_assert_critical(stack != NULL);
   test_assert(gstack_num_elm(stack) == 0);
   gstack_free(stack);

   stack = io_stream_read_seq(stream);
   test_assert(stack == NULL);
   
   io_stream_free(stream);   
}