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(); }
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); }
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; }
void test_case_init (void) { reset_alloc(); N_ERROR_MESSAGES = 0; TEST_CASE_FAILED = 0; STDERR_OFF = 0; ORIG_STDERR_DESCRIPTOR = dup(STDERR_FILENO); if (ORIG_STDERR_DESCRIPTOR == -1) { fprintf(stderr, "unittest error (%s:%d)\n", __FILE__, __LINE__); exit(EXIT_FAILURE); } STDERR_BUFFER = calloc(UTEST_BUFFER_SIZE, sizeof(char)); if (STDERR_BUFFER == NULL) { fprintf(stderr, "unittest error (%s:%d)\n", __FILE__, __LINE__); exit(EXIT_FAILURE); } }
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; }
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(); }
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(); }
caddr_t resalloc(enum RESOURCES type, size_t bytes, caddr_t virthint, int align) { caddr_t vaddr; long pmap = 0; if (memlistpage == (caddr_t)0) reset_alloc(); if (bytes == 0) return ((caddr_t)0); /* extend request to fill a page */ bytes = roundup(bytes, pagesize); dprintf("resalloc: bytes = %lu\n", bytes); switch (type) { /* * even V2 PROMs never bother to indicate whether the * first MAPPEDMEM_MINTOP is taken or not. So we do it all here. * Smart PROM or no smart PROM. */ case RES_BOOTSCRATCH: case RES_BOOTSCRATCH_NOFAIL: vaddr = get_low_vpage((bytes/pagesize), type); if (resalloc_debug) { dprintf("vaddr = %p, paddr = %lx\n", (void *)vaddr, ptob(pmap)); print_memlist(vfreelistp); print_memlist(pfreelistp); } return (vaddr); /*NOTREACHED*/ case RES_CHILDVIRT: vaddr = (caddr_t)prom_alloc(virthint, bytes, align); if (vaddr == (caddr_t)virthint) return (vaddr); printf("Alloc of 0x%lx bytes at 0x%p refused.\n", bytes, (void *)virthint); return ((caddr_t)0); /*NOTREACHED*/ default: printf("Bad resurce type\n"); return ((caddr_t)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(); }
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(); }
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; }
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; }