qioerr qbuffer_destroy(qbuffer_t* buf) { qioerr err = 0; deque_iterator_t cur = deque_begin(& buf->deque); deque_iterator_t end = deque_end(& buf->deque); while( ! deque_it_equals(cur, end) ) { qbuffer_part_t* qbp = (qbuffer_part_t*) deque_it_get_cur_ptr(sizeof(qbuffer_part_t), cur); // release the qbuffer. qbytes_release(qbp->bytes); deque_it_forward_one(sizeof(qbuffer_part_t), &cur); } // remove any cached data qbuffer_clear_cached(buf); // destroy the deque deque_destroy(& buf->deque); DO_DESTROY_REFCNT(buf); return err; }
TEST_C(DequeTests, DequeCopyShallow) { int a = 1; int b = 2; int c = 3; deque_add_last(deque, &a); deque_add_last(deque, &b); deque_add_last(deque, &c); Deque *copy; deque_copy_shallow(deque, ©); int size = deque_size(copy); CHECK_EQUAL_C_INT(3, size); int *ca; deque_get_at(copy, 0, (void*)&ca); int *cb; deque_get_at(copy, 1, (void*)&cb); int *cc; deque_get_at(copy, 2, (void*)&cc); CHECK_EQUAL_C_INT(a, *ca); CHECK_EQUAL_C_INT(b, *cb); CHECK_EQUAL_C_INT(c, *cc); deque_destroy(copy); };
int main(int argc, char** argv) { int i; test_reallocate(); { deque_t d; int lots = 2; deque_init(4, &d, 0); push_front_counting(4, &d, lots); //print_deque(4, &d); pop_back_check_counting(4, &d, lots); //print_deque(4, &d); check_empty(4, &d); deque_destroy(&d); } for( i = 0; i < 32; i++ ) { test_deque(i); } for( i = 127; i < 512; i++ ) { test_deque(i); } test_deque(1 << 15); printf("deque_test PASS\n"); return 0; }
TEST_C(DequeTests, DequeZipIterRemove) { deque_add(deque, "a"); deque_add(deque, "b"); deque_add(deque, "c"); deque_add(deque, "d"); Deque *d2; deque_new(&d2); deque_add(d2, "e"); deque_add(d2, "f"); deque_add(d2, "g"); DequeZipIter zip; deque_zip_iter_init(&zip, deque, d2); void *e1, *e2; void *r1, *r2; while (deque_zip_iter_next(&zip, &e1, &e2) != CC_ITER_END) { if (strcmp((char*) e1, "b") == 0) deque_zip_iter_remove(&zip, &r1, &r2); } CHECK_EQUAL_C_STRING("b", (char*)r1); CHECK_EQUAL_C_STRING("f", (char*)r2); CHECK_EQUAL_C_INT(0, deque_contains(deque, "b")); CHECK_EQUAL_C_INT(0, deque_contains(deque, "f")); CHECK_EQUAL_C_INT(3, deque_size(deque)); CHECK_EQUAL_C_INT(2, deque_size(d2)); deque_destroy(d2); };
TEST_C(DequeTests, DequeZipIterNext) { deque_add(deque, "a"); deque_add(deque, "b"); deque_add(deque, "c"); deque_add(deque, "d"); Deque *d2; deque_new(&d2); deque_add(d2, "e"); deque_add(d2, "f"); deque_add(d2, "g"); DequeZipIter zip; deque_zip_iter_init(&zip, deque, d2); size_t i = 0; void *e1, *e2; while (deque_zip_iter_next(&zip, &e1, &e2) != CC_ITER_END) { if (i == 0) { CHECK_EQUAL_C_STRING("a", (char*)e1); CHECK_EQUAL_C_STRING("e", (char*)e2); } if (i == 2) { CHECK_EQUAL_C_STRING("c", (char*)e1); CHECK_EQUAL_C_STRING("g", (char*)e2); } i++; } CHECK_EQUAL_C_INT(3, i); deque_destroy(d2); };
void test__create_deque__libcstl_builtin_type(void** state) { deque_t* pdeq = _create_deque("vector_t < int> "); assert_true(pdeq != NULL); assert_true(strcmp(_GET_DEQUE_TYPE_NAME(pdeq), "vector_t<int>") == 0); assert_true(_deque_is_created(pdeq)); deque_destroy(pdeq); }
void test__create_deque__c_builtin_type(void** state) { deque_t* pdeq = _create_deque("unsigned int"); assert_true(pdeq != NULL); assert_true(strcmp(_GET_DEQUE_TYPE_NAME(pdeq), "unsigned int") == 0); assert_true(_deque_is_created(pdeq)); deque_destroy(pdeq); }
void test__create_deque_auxiliary__libcstl_builtin(void** state) { deque_t* pdeq = malloc(sizeof(deque_t)); assert_true(_create_deque_auxiliary(pdeq, "deque_t< vector_t < string_t>> ")); assert_true(strcmp(_GET_DEQUE_TYPE_NAME(pdeq), "deque_t<vector_t<string_t>>") == 0); assert_true(_alloc_is_inited(&pdeq->_t_allocator)); assert_true(_deque_is_created(pdeq)); deque_destroy(pdeq); }
void test__create_deque__user_defined_type_dup(void** state) { deque_t* pdeq = NULL; type_duplicate(struct _tag_test__create_deque__user_defined_type, _test__create_deque__user_defined_type_t); pdeq = _create_deque("_test__create_deque__user_defined_type_t"); assert_true(pdeq != NULL); assert_true(strcmp(_GET_DEQUE_TYPE_NAME(pdeq), "_test__create_deque__user_defined_type_t") == 0); assert_true(_deque_is_created(pdeq)); deque_destroy(pdeq); }
void test__create_deque__user_defined_type(void** state) { deque_t* pdeq = NULL; type_register(struct _tag_test__create_deque__user_defined_type, NULL, NULL, NULL, NULL); pdeq = _create_deque("struct _tag_test__create_deque__user_defined_type"); assert_true(pdeq != NULL); assert_true(strcmp(_GET_DEQUE_TYPE_NAME(pdeq), "struct _tag_test__create_deque__user_defined_type") == 0); assert_true(_deque_is_created(pdeq)); deque_destroy(pdeq); }
void test__create_deque_auxiliary__user_defined_dup(void** state) { deque_t* pdeq = NULL; type_duplicate(struct _tag_test__create_deque_auxiliary__user_defined, _test__create_deque_auxiliary__user_defined_t); pdeq = malloc(sizeof(deque_t)); assert_true(_create_deque_auxiliary(pdeq, "_test__create_deque_auxiliary__user_defined_t")); assert_true(strcmp(_GET_DEQUE_TYPE_NAME(pdeq), "_test__create_deque_auxiliary__user_defined_t") == 0); assert_true(_alloc_is_inited(&pdeq->_t_allocator)); assert_true(_deque_is_created(pdeq)); deque_destroy(pdeq); }
int main(int argc, char* argv[]) { deque_t* pdeq_coll = NULL; person_t t_person; type_register(person_t, NULL, NULL, NULL, NULL); pdeq_coll = create_deque(person_t); if(pdeq_coll == NULL) { return -1; } deque_init(pdeq_coll); memset(t_person.s_firstname, '\0', 21); memset(t_person.s_lastname, '\0', 21); strcpy(t_person.s_firstname, "Jonh"); strcpy(t_person.s_lastname, "right"); deque_push_back(pdeq_coll, &t_person); memset(t_person.s_firstname, '\0', 21); memset(t_person.s_lastname, '\0', 21); strcpy(t_person.s_firstname, "Bill"); strcpy(t_person.s_lastname, "killer"); deque_push_back(pdeq_coll, &t_person); memset(t_person.s_firstname, '\0', 21); memset(t_person.s_lastname, '\0', 21); strcpy(t_person.s_firstname, "Jonh"); strcpy(t_person.s_lastname, "sound"); deque_push_back(pdeq_coll, &t_person); memset(t_person.s_firstname, '\0', 21); memset(t_person.s_lastname, '\0', 21); strcpy(t_person.s_firstname, "Bin"); strcpy(t_person.s_lastname, "lee"); deque_push_back(pdeq_coll, &t_person); memset(t_person.s_firstname, '\0', 21); memset(t_person.s_lastname, '\0', 21); strcpy(t_person.s_firstname, "Lee"); strcpy(t_person.s_lastname, "bird"); deque_push_back(pdeq_coll, &t_person); algo_for_each(deque_begin(pdeq_coll), deque_end(pdeq_coll), _print); printf("\n"); algo_sort_if(deque_begin(pdeq_coll), deque_end(pdeq_coll), _person_sort_criterion); algo_for_each(deque_begin(pdeq_coll), deque_end(pdeq_coll), _print); printf("\n"); deque_destroy(pdeq_coll); return 0; }
TEST_C(DequeTests, DequeZipIterAdd) { deque_add(deque, "a"); deque_add(deque, "b"); deque_add(deque, "c"); deque_add(deque, "d"); Deque *d2; deque_new(&d2); deque_add(d2, "e"); deque_add(d2, "f"); deque_add(d2, "g"); char *h = "h"; char *i = "i"; DequeZipIter zip; deque_zip_iter_init(&zip, deque, d2); void *e1, *e2; while (deque_zip_iter_next(&zip, &e1, &e2) != CC_ITER_END) { if (strcmp((char*) e1, "b") == 0) deque_zip_iter_add(&zip, h, i); } size_t index; deque_index_of(deque, "h", &index); CHECK_EQUAL_C_INT(2, index); deque_index_of(deque, "i", &index); CHECK_EQUAL_C_INT(2, index); deque_index_of(deque, "c", &index); CHECK_EQUAL_C_INT(3, index); CHECK_EQUAL_C_INT(1, deque_contains(deque, "h")); CHECK_EQUAL_C_INT(1, deque_contains(d2, "i")); CHECK_EQUAL_C_INT(5, deque_size(deque)); CHECK_EQUAL_C_INT(4, deque_size(d2)); deque_destroy(d2); };
/** * Destroys the Deque structure along with all the data it holds. * * @note * This function should not be called on a Deque that has some of its elements * allocated on the stack. * * @param[in] deque Deque that is to be destroyed */ void deque_destroy_free(Deque *deque) { deque_remove_all_free(deque); deque_destroy(deque); }
void test_reallocate(void) { // hit the different reallocate cases. // map_size > 2* num_new_nodes // add to front, add to back { // start with copy forward/copy backward. deque_node_t nodes[4]; nodes[0].data = (void*) 0x100; nodes[1].data = (void*) 0x101; nodes[2].data = (void*) 0x102; nodes[3].data = (void*) 0x103; // test overlapping copy forward. // dstBeg should be before srcBegin _deque_map_copy_forward( &nodes[2], &nodes[4], &nodes[1]); assert(nodes[0].data == (void*) 0x100); assert(nodes[1].data == (void*) 0x102); assert(nodes[2].data == (void*) 0x103); assert(nodes[3].data == (void*) 0x103); nodes[0].data = (void*) 0x100; nodes[1].data = (void*) 0x101; nodes[2].data = (void*) 0x102; nodes[3].data = (void*) 0x103; // test overlapping copy backward. // dstEnd should be after srcEnd _deque_map_copy_backward( &nodes[0], &nodes[2], &nodes[3]); assert(nodes[0].data == (void*) 0x100); assert(nodes[1].data == (void*) 0x100); assert(nodes[2].data == (void*) 0x101); assert(nodes[3].data == (void*) 0x103); } // Now, suppose we've allocated our stuff... // let's reallocate. { deque_t d; qioerr err; ssize_t saved_size; deque_node_t saved_nodes[2*_DEQUE_INITIAL_MAP_SIZE]; deque_node_t* cur; int i; // this time, try with map_size < 2*num_new_nodes err = deque_init(16, &d, 16*_DEQUE_INITIAL_MAP_SIZE); assert(!err); // save the pointers. assert(d.map_size < 2*_DEQUE_INITIAL_MAP_SIZE);// fits in test buffer saved_size = 0; for( cur = d.start.node; cur < d.finish.node; ++cur ) { saved_nodes[saved_size++].data = cur->data; } _deque_reallocate_map(16, 512, /* first two args dont matter in this test*/ &d, 1, 0 /* not at front */); assert(d.map_size > saved_size); // make sure it grew i = 0; for( cur = d.start.node; cur < d.finish.node && i < saved_size; ++cur ) { assert(saved_nodes[i++].data == cur->data); } // don't care value of nodes[saved_size], the new slot. deque_destroy(&d); err = deque_init(16, &d, 16*_DEQUE_INITIAL_MAP_SIZE); assert(!err); assert(d.map_size < 2*_DEQUE_INITIAL_MAP_SIZE);// fits in test buffer saved_size = 0; for( cur = d.start.node; cur < d.finish.node; ++cur ) { saved_nodes[saved_size++].data = cur->data; } _deque_reallocate_map(16, 512, /* first two args dont matter in this test*/ &d, 1, 1 /* at front */); assert(d.map_size > saved_size); // make sure it grew // don't care value of nodes[0], the new slot. i = 0; for( cur = d.start.node; cur < d.finish.node && i < saved_size ; ++cur ) { assert(saved_nodes[i++].data == cur->data); } // don't care of value of nodes[saved_size+1], the previous new slot. deque_destroy(&d); // this time, try with map_size > 2*num_new_nodes err = deque_init(16, &d, 0); assert(!err); // save the pointers. assert(d.map_size < 2*_DEQUE_INITIAL_MAP_SIZE);// fits in test buffer saved_size = 0; for( cur = d.start.node; cur < d.finish.node; ++cur ) { saved_nodes[saved_size++].data = cur->data; } _deque_reallocate_map(16, 512, /* first two args dont matter in this test*/ &d, 1, 0 /* not at front */); i = 0; for( cur = d.start.node; cur < d.finish.node; ++cur ) { assert(saved_nodes[i++].data == cur->data); } assert( i == saved_size ); deque_destroy(&d); err = deque_init(16, &d, 0); assert(!err); // save the pointers. assert(d.map_size < 2*_DEQUE_INITIAL_MAP_SIZE);// fits in test buffer saved_size = 0; for( cur = d.start.node; cur < d.finish.node; ++cur ) { saved_nodes[saved_size++].data = cur->data; } _deque_reallocate_map(16, 512, /* first two args dont matter in this test*/ &d, 1, 1 /* at front */); i = 0; for( cur = d.start.node; cur < d.finish.node; ++cur ) { assert(saved_nodes[i++].data == cur->data); } assert( i == saved_size ); deque_destroy(&d); } }
void test_deque(int lots) { deque_t d; // test: space for zeros. deque_init(4, &d, 0); check_empty(4, &d); deque_destroy(&d); // test: space for zeros. deque_init(4, &d, 10); pop_back_check_constant(4, &d, 10, 0); check_empty(4, &d); deque_destroy(&d); // test: space for zeros. deque_init(4, &d, 10); pop_front_check_constant(4, &d, 10, 0); check_empty(4, &d); deque_destroy(&d); // test: push back lots, destroy deque_init(4, &d, 0); push_back_counting(4, &d, lots); iterate_counting(4, &d, lots); deque_destroy(&d); // test: push back lots, pop front lots. deque_init(4, &d, 0); push_back_counting(4, &d, lots); iterate_counting(4, &d, lots); pop_front_check_counting(4, &d, lots); check_empty(4, &d); deque_destroy(&d); // test: push back lots, pop back lots. deque_init(4, &d, 0); push_back_counting(4, &d, lots); pop_back_check_counting_reverse(4, &d, lots); check_empty(4, &d); deque_destroy(&d); // test: push front lots, pop front lots. deque_init(4, &d, 0); push_front_counting(4, &d, lots); iterate_counting_reverse(4, &d, lots); //print_deque(4, &d); pop_front_check_counting_reverse(4, &d, lots); //print_deque(4, &d); check_empty(4, &d); deque_destroy(&d); // test: push front lots, pop back lots. deque_init(4, &d, 0); push_front_counting(4, &d, lots); //print_deque(4, &d); pop_back_check_counting(4, &d, lots); //print_deque(4, &d); check_empty(4, &d); deque_destroy(&d); // test: push both lots, pop back lots, pop back lots. deque_init(4, &d, 0); push_both_counting(4, &d, lots); //print_deque(4, &d); pop_back_check_counting_reverse(4, &d, lots); pop_back_check_counting(4, &d, lots); check_empty(4, &d); deque_destroy(&d); // test: push both lots, pop front lots, pop front lots. deque_init(4, &d, 0); push_both_counting(4, &d, lots); //print_deque(4, &d); pop_front_check_counting_reverse(4, &d, lots); pop_front_check_counting(4, &d, lots); check_empty(4, &d); deque_destroy(&d); // test: push both lots, pop back lots, pop front lots deque_init(4, &d, 0); push_both_counting(4, &d, lots); //print_deque(4, &d); pop_back_check_counting_reverse(4, &d, lots); pop_front_check_counting_reverse(4, &d, lots); check_empty(4, &d); deque_destroy(&d); // test: push both lots, pop front lots, pop back lots deque_init(4, &d, 0); push_both_counting(4, &d, lots); //print_deque(4, &d); pop_front_check_counting_reverse(4, &d, lots); pop_back_check_counting_reverse(4, &d, lots); check_empty(4, &d); deque_destroy(&d); }
/** * Destroys the queue structure, but leaves the data it used to hold intact. * * @param[in] queue the queue that is to be destroyed */ void queue_destroy(Queue *queue) { deque_destroy(queue->d); queue->mem_free(queue); }