int delete_client(t_conf *conf, t_simple_list *client) { t_simple_list *tmp; if (conf->client == client) { tmp = client->next; LOG("delete_client 0x%x %s\n", client->session_id, client->control.authenticated ? "" : "(not authenticated)"); delete_queue(client->saved_queue); list_destroy_simple_cell(conf->client); conf->client = tmp; return (0); } for (tmp = conf->client; tmp; tmp = tmp->next) { if (tmp->next == client) { tmp->next = client->next; LOG("delete_client 0x%x\n", client->session_id); delete_queue(client->saved_queue); return (list_destroy_simple_cell(client)); } } return (-1); }
int server() { int msg_id = creat_queue(); if(msg_id < 0){ printf("%s\n",strerror(errno)); return -1; } char buf[_SIZE_]; while(1) { sleep(5); memset(buf,'\0',sizeof(buf)); int ret = recv_msg(msg_id,CLIENT_TYPE,buf,sizeof(buf)); if(ret == 0){ if(strncasecmp(buf,"quit",4) == 0){ printf("client leave!\n"); break; } printf("client say : %s\n",buf); } printf("Please Enter :"); fflush(stdout); memset(buf,'\0',sizeof(buf)); scanf("%s",buf); send_msg(msg_id,SERVER_TYPE,buf); } return delete_queue(msg_id); }
TEST(PRIORITY_QUEUE,FULL){ //try to get the full status of a non existent queue RESULT result = is_full(424234); EXPECT_EQ(result.code,TICKET_INVALID); //fill up the queue WELCOME_PACKET packet = create_queue(); //if(packet.result.code == SUCCESS){ for(int i = 0; i < MAXIMUM_NUMBER_OF_ELEMENTS_IN_A_QUEUE-1; i++){ ELEMENT e = {i,i%10}; EXPECT_EQ(enqueue(e,packet.ticket).code,SUCCESS); EXPECT_EQ(is_full(packet.ticket).code,QUEUE_IS_NOT_FULL); } ELEMENT e = {3,3}; enqueue(e,packet.ticket); EXPECT_EQ(is_full(packet.ticket).code,QUEUE_IS_FULL); enqueue(e,packet.ticket); EXPECT_EQ(is_full(packet.ticket).code,QUEUE_IS_FULL); enqueue(e,packet.ticket); EXPECT_EQ(is_full(packet.ticket).code,QUEUE_IS_FULL); //delete the queue then try to get the full status delete_queue(packet.ticket); EXPECT_EQ(is_full(packet.ticket).code,TICKET_INVALID); }
int main() { struct priority_queue *queue = create_queue(10); if (queue == NULL) printf("create_queue error\n"); insert_queue(queue, 10, (struct huff_table*)10); insert_queue(queue, 11, (struct huff_table*)11); insert_queue(queue, 8, (struct huff_table*)8); insert_queue(queue, 3, (struct huff_table*)3); insert_queue(queue, 7, (struct huff_table*)7); insert_queue(queue, 15, (struct huff_table*)15); uint32_t priority = 0; struct huff_table *table = NULL; while (best_queue(queue, &priority, &table)) { printf("Table = %ld\n", (uint64_t)table); delete_queue(queue); } free_queue(queue); return EXIT_SUCCESS; }
void BFS(int v) { int i; insert_queue(v); state[v] = waiting; while(!isEmpty_queue()) { v = delete_queue(); printf("Vertex %d visited\n",v); state[v] = visited; for(i=0; i<n; i++) { /* Check for adjacent unvisited vertices */ if(adj[v][i] == 1 && state[i] == initial) { insert_queue(i); state[i] = waiting; printf("-----Tree edge - (%d,%d)\n", v, i); } } } printf("\n"); }/*End of BFS()*/
static void * capture_main( void *args ) { UNUSED( args ); info( "Starting packet capture ( interface_name = %s ).", interface_name ); packet_queue = create_queue(); pcap_t *cd = NULL; if ( packet_queue == NULL ) { error( "Failed to create packet queue." ); goto error; } char errbuf[ PCAP_ERRBUF_SIZE ]; bpf_u_int32 mask = 0; bpf_u_int32 net = 0; int ret = pcap_lookupnet( interface_name, &net, &mask, errbuf ); if ( ret < 0 ) { error( "Failed to get netmask for device %s ( error = %s ).", interface_name, errbuf ); net = 0; mask = 0; } cd = pcap_open_live( interface_name, UINT16_MAX, 1, 100, errbuf ); if ( cd == NULL ) { error( "Failed to open network interface ( interface_name = %s, error = %s ).", interface_name, errbuf ); goto error; } if ( filter_expression != NULL ) { struct bpf_program fp; ret = pcap_compile( cd, &fp, filter_expression, 0, net ); if ( ret < 0 ) { error( "Failed to parse filter `%s' ( error = %s ).", filter_expression, pcap_geterr( cd ) ); goto error; } ret = pcap_setfilter( cd, &fp ); if ( ret < 0 ) { error( "Failed to set filter `%s' ( error = %s ).", filter_expression, pcap_geterr( cd ) ); goto error; } } int dlt = pcap_datalink( cd ); pcap_loop( cd, -1, handle_packet, ( u_char * ) &dlt ); error: if ( cd != NULL ) { pcap_close( cd ); } if ( packet_queue != NULL ) { delete_queue( packet_queue ); } return NULL; }
TEST(PRIORITY_QUEUE,DELETE_QUEUE){ //try to delete a queue with a forged ticket RESULT r = delete_queue(23131323); EXPECT_EQ(r.code, TICKET_INVALID); WELCOME_PACKET w = create_queue(); EXPECT_EQ(w.result.code, SUCCESS); //delete a queue with a valid ticket r = delete_queue(w.ticket); EXPECT_EQ(r.code, SUCCESS); //try to delete an already deleted queue r = delete_queue(w.ticket); EXPECT_EQ(r.code, TICKET_INVALID); }
int main() { Dnode *ptr = Create_list(); Dnode *rear = ptr->next; printf("Please input 1 to enqueue or 2 to dequeue or 0 to end program : "); int in; while(scanf("%d",&in)!=EOF) { int input; if(in==1) { printf("Please input a number to enqueue : "); scanf("%d",&input); enqueue(ptr,input); } else if(in==2) { dequeue(rear); } else if(in==0) break; Dnode *zz; zz = ptr->next; int con = 0; while(zz!=rear) { con = 1; printf("%d ",zz->number); zz = zz->next; } if(con==0) printf("NULL"); printf("\nPlease input 1 to enqueue or 2 to dequeue or 0 to end program : "); } delete_queue(ptr); //Delete queue before the end of program. int det = determine_free(); if(det) printf("All the memory are under control and eventually free back to the system\n"); else printf("The memory leak has happened!\n"); return 0; }
TEST(PRIORITY_QUEUE,CREATE_QUEUE){ WELCOME_PACKET w = create_queue(); delete_queue(w.ticket); WELCOME_PACKET wp[1024]; //this is used to stress test the creation and deletion of queues. for(int m = 0; m < 1000; m++){ for(int j = 0; j < 100; j++){ for(int i = 0; i < MAXIMUM_NUMBER_OF_QUEUES ; i++){ wp[i] = create_queue(); EXPECT_EQ(wp[i].result.code, SUCCESS); } for(int i = 0; i < MAXIMUM_NUMBER_OF_QUEUES ; i++) EXPECT_EQ(delete_queue(wp[i].ticket).code, SUCCESS); } for(int i = 0; i < MAXIMUM_NUMBER_OF_QUEUES ; i++) EXPECT_EQ(delete_queue(wp[i].ticket).code,TICKET_INVALID); }//end creation stress test //create a full set of queues for(int i = 0; i < MAXIMUM_NUMBER_OF_QUEUES ; i++){ wp[i] = create_queue(); EXPECT_EQ(wp[i].result.code, SUCCESS); } //delete some random queues and try to recreate them delete_queue(wp[0].ticket); delete_queue(wp[1023].ticket); delete_queue(wp[64].ticket); delete_queue(wp[800].ticket); wp[0] = create_queue(); EXPECT_EQ(wp[0].result.code,SUCCESS); wp[1023] = create_queue(); EXPECT_EQ(wp[1023].result.code,SUCCESS); wp[64] = create_queue(); EXPECT_EQ(wp[64].result.code,SUCCESS); wp[800] = create_queue(); EXPECT_EQ(wp[800].result.code,SUCCESS); //delete all created queues for(int i = 0; i < MAXIMUM_NUMBER_OF_QUEUES ; i++) delete_queue(wp[i].ticket); }
JNIEXPORT void JNICALL Java_com_yichou_test_jni_MainActivity_exit(JNIEnv * env, jobject self) { char *msg; timerStop(); pthread_join(pid, (void **)&msg); LOGD("pthread_join finish, msg=%s", msg); delete_queue(mQueue); mHandler->exit(); }
bool delete_pcap_queue( void ) { assert( packet_queue != NULL ); bool ret = delete_queue( packet_queue ); if ( !ret ) { return false; } return true; }
void email_asciifile_tail( FILE* output, const char* file, int lines ) { FILE *input; int ch, last_ch; long loc; int first_line = TRUE; TAIL_QUEUE queue, *q = &queue; if( !file ) { return; } if( (input=safe_fopen_wrapper_follow(file,"r",0644)) == NULL ) { // try the .old file in the off shoot case we hit this during the transition. std::string szTmp = file; szTmp += ".old"; if( (input=safe_fopen_wrapper_follow(szTmp.c_str(),"r",0644)) == NULL ) { dprintf( D_FULLDEBUG, "Failed to email %s: cannot open file\n", file ); return; } } init_queue( q, lines ); last_ch = '\n'; while( (ch=getc(input)) != EOF ) { if( last_ch == '\n' && ch != '\n' ) { insert_queue( q, ftell(input) - 1 ); } last_ch = ch; } while( !empty_queue( q ) ) { loc = delete_queue( q ); /* If this is the first line, print header */ if ( first_line ) { first_line = FALSE; fprintf(output,"\n*** Last %d line(s) of file %s:\n", lines, file); } /* Now print the line */ display_line( loc, input, output ); } (void)fclose( input ); /* if we printed any of the file, print a footer */ if ( first_line == FALSE ) { fprintf(output,"*** End of file %s\n\n", condor_basename(file)); } }
bool finalize_http_client() { debug( "Finalizaing HTTP client ( transactions = %p ).", transactions ); assert( transactions != NULL ); if ( http_client_thread != NULL ) { pthread_cancel( *http_client_thread ); xfree( http_client_thread ); http_client_thread = NULL; } if ( http_client_efd >= 0 ) { set_writable( http_client_efd, false ); delete_fd_handler( http_client_efd ); close( http_client_efd ); http_client_efd = -1; } http_client_notify_count = 0; if ( main_efd >= 0 ) { set_readable( main_efd, false ); delete_fd_handler( main_efd ); close( main_efd ); main_efd = -1; } delete_queue( main_to_http_client_queue ); main_to_http_client_queue = NULL; delete_queue( http_client_to_main_queue ); http_client_to_main_queue = NULL; delete_http_transaction_db(); debug( "Finalization completed." ); return true; }
int myrg_close(MYRG_INFO *info) { int error=0,new_error; MYRG_TABLE *file; DBUG_ENTER("myrg_close"); /* Assume that info->children_attached means that this is called from direct use of MERGE, not from a MySQL server. In this case the children must be closed and info->rec_per_key_part is part of the 'info' multi_alloc. If info->children_attached is false, this is called from a MySQL server. Children are closed independently but info->rec_per_key_part must be freed. Just in case of a server panic (myrg_panic()) info->children_attached might be true. We would close the children though they should be closed independently and info->rec_per_key_part is not freed. This should be acceptable for a panic. In case of a MySQL server and no children, children_attached is always true. In this case no rec_per_key_part has been allocated. So it is correct to use the branch where an empty list of tables is (not) closed. */ if (info->children_attached) { for (file= info->open_tables; file != info->end_table; file++) { /* purecov: begin inspected */ if ((new_error= mi_close(file->table))) error= new_error; else file->table= NULL; /* purecov: end */ } } else my_free((uchar*) info->rec_per_key_part, MYF(MY_ALLOW_ZERO_PTR)); delete_queue(&info->by_key); pthread_mutex_lock(&THR_LOCK_open); myrg_open_list=list_delete(myrg_open_list,&info->open_list); pthread_mutex_unlock(&THR_LOCK_open); VOID(pthread_mutex_destroy(&info->mutex)); my_free((uchar*) info,MYF(0)); if (error) { DBUG_RETURN(my_errno=error); } DBUG_RETURN(0); }
main() { int i, j = 0, k; int topsort[ MAX ], indeg[ MAX ]; create_graph(); printf( "The adjacency matrix is :\n" ); display(); /*Find the indegree of each node*/ for ( i = 1;i <= n;i++ ) { indeg[ i ] = indegree( i ); if ( indeg[ i ] == 0 ) insert_queue( i ); } while ( front <= rear ) /*Loop till queue is not empty */ { k = delete_queue(); topsort[ j++ ] = k; /*Add node k to topsort array*/ /*Delete all edges going fron node k */ for ( i = 1;i <= n;i++ ) { if ( adj[ k ][ i ] == 1 ) { adj[ k ][ i ] = 0; indeg[ i ] = indeg[ i ] – 1; if ( indeg[ i ] == 0 ) insert_queue( i ); } } /*End of for*/ } /*End of while*/ printf( "Nodes after topological sorting are :\n" ); for ( i = 0;i < j;i++ ) printf( "%d ", topsort[ i ] ); printf( "\n" ); } /*End of main()*/
main() { int i,v,count,topo_order[MAX],indeg[MAX]; create_graph(); /*Find the indegree of each vertex*/ for(i=0;i<n;i++) { indeg[i] = indegree(i); if( indeg[i] == 0 ) insert_queue(i); } count = 0; while( !isEmpty_queue( ) && count < n ) { v = delete_queue(); topo_order[++count] = v; /*Add vertex v to topo_order array*/ /*Delete all edges going fron vertex v */ for(i=0; i<n; i++) { if(adj[v][i] == 1) { adj[v][i] = 0; indeg[i] = indeg[i]-1; if(indeg[i] == 0) insert_queue(i); } } } if( count < n ) { printf("No topological ordering possible, graph contains cycle\n"); exit(1); } printf("Vertices in topological order are :\n"); for(i=1; i<=count; i++) printf( "%d ",topo_order[i] ); printf("\n"); }/*End of main()*/
int main(int argc, char *argv[]){ TABLE *table = create_table(); read_table(stdin, table); // print_table(table); PIECE_LIST *list = create_piece_list(table); QUEUE *queue = create_queue(); list_moves(table, queue, list); print_queue(queue); //printf("-----------------------------------------------------\n"); // print_table(table); delete_queue(&queue); delete_list(&list); //printf("-----------------------------------------------------\n"); // print_table(table); delete_table(&table); return 0; }
void BFS(int v) { int i; insert_queue(v); state[v]=waiting; while(!isempty_queue()) { v=delete_queue(); printf("%d ",v); state[v]=visited; for(i=0;i<n;i++) { if(adj[v][i]==1&&state[i]==initial) { insert_queue(i); state[i]=waiting; } } } printf("\n"); }
TEST(PRIORITY_QUEUE,DEQUEUE){ ELEMENT_RESULT ele_result = dequeue(424234); EXPECT_EQ(ele_result.result.code,TICKET_INVALID);//or does not exist EXPECT_EQ(strcmp(ele_result.element.item,""),0); EXPECT_EQ(ele_result.element.priority,0); WELCOME_PACKET packet = create_queue(); ele_result = dequeue(packet.ticket); EXPECT_EQ(ele_result.result.code,QUEUE_IS_EMPTY); ELEMENT e; char temp[MAX_STRING_LENGTH]; for(int i = 0; i < MAXIMUM_NUMBER_OF_ELEMENTS_IN_A_QUEUE; i++){ snprintf(temp,MAX_STRING_LENGTH,"%d",i); e = fill_element(temp,4); enqueue(e,packet.ticket); } for(int i = 0; i < MAXIMUM_NUMBER_OF_ELEMENTS_IN_A_QUEUE; i++){ ELEMENT_RESULT item = dequeue(packet.ticket); e = item.element; snprintf(temp,MAX_STRING_LENGTH,"%d",i); EXPECT_EQ(strcmp(e.item,temp),0); } ELEMENT_RESULT r = dequeue(packet.ticket); EXPECT_EQ(r.result.code,QUEUE_IS_EMPTY); RESULT res = delete_queue(packet.ticket); EXPECT_EQ(res.code,SUCCESS); ELEMENT_RESULT result = dequeue(packet.ticket); EXPECT_EQ(r.result.code,QUEUE_IS_EMPTY); }
void carmen_conventional_dynamic_program(int goal_x, int goal_y) { double *utility_ptr; int index; double max_val, min_val; int num_expanded; int done; struct timeval start_time, end_time; int delta_sec, delta_usec; static double last_time, cur_time, last_print; queue state_queue; state_ptr current_state; if (costs == NULL) return; gettimeofday(&start_time, NULL); cur_time = carmen_get_time(); if ((int)(cur_time - last_print) > 10) { carmen_verbose("Time since last DP: %d secs, %d usecs\n", (int)(cur_time - last_time), ((int)((cur_time-last_time)*1e6)) % 1000000); last_print = cur_time; } last_time = cur_time; if (utility == NULL) { utility = (double *)calloc(x_size*y_size, sizeof(double)); carmen_test_alloc(utility); } utility_ptr = utility; for (index = 0; index < x_size * y_size; index++) *(utility_ptr++) = -1; if (is_out_of_map(goal_x, goal_y)) return; max_val = -MAXDOUBLE; min_val = MAXDOUBLE; done = 0; state_queue = make_queue(); current_state = carmen_conventional_create_state(goal_x, goal_y, 0); max_val = MAX_UTILITY; *(utility_value(goal_x, goal_y)) = max_val; add_neighbours_to_queue(goal_x, goal_y, state_queue); num_expanded = 1; while ((current_state = pop_queue(state_queue)) != NULL) { num_expanded++; if (current_state->cost <= *(utility_value(current_state->x, current_state->y))) add_neighbours_to_queue(current_state->x, current_state->y, state_queue); if (current_state->cost < min_val) min_val = current_state->cost; free(current_state); } delete_queue(&state_queue); gettimeofday(&end_time, NULL); delta_usec = end_time.tv_usec - start_time.tv_usec; delta_sec = end_time.tv_sec - start_time.tv_sec; if (delta_usec < 0) { delta_sec--; delta_usec += 1000000; } // carmen_warn("Elasped time for dp: %d secs, %d usecs\n", delta_sec, delta_usec); }
main() { int i,v; int topo_order[MAX],indeg[MAX]; int count; int j; create_graph(); /*Find the indegree of each vertex*/ for(i=0; i<n; i++) { indeg[i] = indegree(i); if( indeg[i] == 0 ) insert_queue(i); } for(j=0; j<n; j++) printf("In(%d) = %d\t",j, indeg[j]); printf("\n"); display_queue(); count = 0; while( !isEmpty_queue() && count<n ) { v = delete_queue(); printf("Delete %d and edges going from it\n",v); topo_order[++count] = v; /*Add vertex v to topo_order array*/ /*Delete all edges going fron vertex v */ for(i=0; i<n; i++) { if(adj[v][i]==1) { adj[v][i] = 0; indeg[i] = indeg[i]-1; if(indeg[i] == 0) { printf("Now vertex %d has zero indegree so insert it in the queue\n",i); insert_queue(i); } } } for(j=0; j<n; j++) { if(indeg[j]!=0) printf("In(%d) = %d\t",j, indeg[j]); } printf("\n"); display_queue(); STOP; } printf("count = %d\n",count); if( count < n ) { printf("No topological ordering possible, graph contains cycle\n"); exit(1); } printf("Vertices in topological order are :\n"); for(i=1; i<=count; i++) printf( "%d ",topo_order[i] ); printf("\n"); }/*End of main()*/
static void search(carmen_roadmap_vertex_t *start_node, carmen_roadmap_t *roadmap) { int num_expanded; state_ptr current_state; int i, j; double *fwd_utilities, *open_list; int *parent_ptrs; carmen_roadmap_vertex_t *node_list; carmen_roadmap_edge_t *edges; double min_neighbour_utility; int min_neighbour, neighbour_id; int min_edge; double best_utility, utility; int best_neighbour; queue the_state_queue = NULL; the_state_queue = make_queue(); node_list = (carmen_roadmap_vertex_t *)(roadmap->nodes->list); fwd_utilities = (double *)calloc(roadmap->nodes->length, sizeof(double)); carmen_test_alloc(fwd_utilities); for (i = 0; i < roadmap->nodes->length; i++) fwd_utilities[i] = FLT_MAX; open_list = (double *)calloc(roadmap->nodes->length, sizeof(double)); carmen_test_alloc(open_list); for (i = 0; i < roadmap->nodes->length; i++) open_list[i] = -1; parent_ptrs = (int *)calloc(roadmap->nodes->length, sizeof(int)); carmen_test_alloc(parent_ptrs); for (i = 0; i < roadmap->nodes->length; i++) parent_ptrs[i] = -1; push_state(start_node->id, start_node->id, 0, 0, the_state_queue); fwd_utilities[start_node->id] = 0; node_list[roadmap->goal_id].utility = 0; num_expanded = 0; while ((current_state = pop_queue(the_state_queue)) != NULL) { num_expanded++; if (0) carmen_warn("Popped %d\n", current_state->id); fwd_utilities[current_state->id] = fwd_utilities[current_state->parent_id] + current_state->cost; parent_ptrs[current_state->id] = current_state->parent_id; open_list[current_state->id] = -2; assert (fwd_utilities[current_state->id] < 1e5); if (current_state->id == roadmap->goal_id) { if (node_list[roadmap->goal_id].edges->length == 0) construct_edges(roadmap, roadmap->goal_id); empty_queue(the_state_queue); } else { add_neighbours_to_queue(roadmap, current_state->id, roadmap->goal_id, fwd_utilities, open_list, the_state_queue); } free(current_state); } carmen_warn("Num Expanded %d\n", num_expanded); if (fwd_utilities[roadmap->goal_id] > FLT_MAX/2) carmen_warn("PROBLEM\n"); else if (0) { node_list[roadmap->goal_id].utility = 0; i = roadmap->goal_id; while (i != start_node->id) { edges = (carmen_roadmap_edge_t *)(node_list[i].edges->list); assert (node_list[i].edges->length > 0); min_neighbour_utility = FLT_MAX; min_neighbour = -1; min_edge = -1; for (j = 0; j < node_list[i].edges->length; j++) { if (edges[j].cost > 1e5 || edges[j].blocked) continue; neighbour_id = edges[j].id; assert (fwd_utilities[neighbour_id] >= 0); if (fwd_utilities[neighbour_id] < min_neighbour_utility) { min_neighbour_utility = fwd_utilities[neighbour_id]; min_neighbour = neighbour_id; min_edge = j; } } assert (min_neighbour >= 0); assert (min_neighbour_utility < fwd_utilities[i]); assert(node_list[min_neighbour].utility > FLT_MAX/2 || node_list[min_neighbour].utility == node_list[i].utility + edges[min_edge].cost); if (node_list[min_neighbour].utility > FLT_MAX/2) { node_list[min_neighbour].utility = node_list[i].utility + edges[min_edge].cost; assert(node_list[i].utility < FLT_MAX/2); assert(fwd_utilities[i] < FLT_MAX/2); assert(fwd_utilities[min_neighbour] < FLT_MAX/2); assert(node_list[min_neighbour].utility < 1e5); } assert (node_list[i].utility < node_list[min_neighbour].utility); // carmen_warn("%d %d %f %f\n", node_list[i].x, node_list[i].y, node_list[i].utility, FLT_MAX/2); i = min_neighbour; assert(node_list[i].utility < FLT_MAX/2); } } else { node_list[roadmap->goal_id].utility = 0; i = roadmap->goal_id; while (i != start_node->id) { min_neighbour = parent_ptrs[i]; assert (min_neighbour >= 0); min_neighbour_utility = fwd_utilities[min_neighbour]; assert (min_neighbour_utility < fwd_utilities[i]); edges = (carmen_roadmap_edge_t *)(node_list[i].edges->list); assert (node_list[i].edges->length > 0); for (min_edge = 0; min_edge < node_list[i].edges->length; min_edge++) { if (edges[min_edge].id == min_neighbour) break; } assert (min_edge < node_list[i].edges->length); assert(node_list[min_neighbour].utility > FLT_MAX/2 || fabs(node_list[min_neighbour].utility - (node_list[i].utility + edges[min_edge].cost)) < 1e6); if (node_list[min_neighbour].utility > FLT_MAX/2) { carmen_roadmap_edge_t *edges2; edges2 = (carmen_roadmap_edge_t *)node_list[min_neighbour].edges->list; node_list[min_neighbour].utility = node_list[i].utility + edges[min_edge].cost; best_neighbour = -1; best_utility = FLT_MAX; for (j = 0; j < node_list[min_neighbour].edges->length; j++) { if (edges2[j].cost > 1e5 || edges2[j].blocked) continue; if (node_list[edges2[j].id].utility > FLT_MAX/2) continue; utility = node_list[edges2[j].id].utility; if (utility < best_utility) { best_utility = utility; best_neighbour = edges2[j].id; } } if (best_neighbour < 0) continue; assert (best_utility < FLT_MAX/2); assert (node_list[best_neighbour].utility < node_list[min_neighbour].utility); assert(node_list[i].utility < FLT_MAX/2); assert(fwd_utilities[i] < FLT_MAX/2); assert(fwd_utilities[min_neighbour] < FLT_MAX/2); assert(node_list[min_neighbour].utility < 1e5); } assert (node_list[i].utility < node_list[min_neighbour].utility); // carmen_warn("%d %d %f %f\n", node_list[i].x, node_list[i].y, node_list[i].utility, FLT_MAX/2); i = min_neighbour; assert(node_list[i].utility < FLT_MAX/2); } } /* Check to make sure that no node is a local minimum. The only node that's allowed to be a local minimum is the goal. */ for (i = 0; i < roadmap->nodes->length; i++) { if (i == roadmap->goal_id) continue; edges = (carmen_roadmap_edge_t *)(node_list[i].edges->list); if (node_list[i].utility > FLT_MAX/2) continue; best_neighbour = -1; best_utility = FLT_MAX; for (j = 0; j < node_list[i].edges->length; j++) { if (edges[j].cost > 1e5 || edges[j].blocked) continue; if (node_list[edges[j].id].utility > FLT_MAX/2) continue; utility = node_list[edges[j].id].utility; if (utility < best_utility) { best_utility = utility; best_neighbour = edges[j].id; } } if (best_neighbour < 0) continue; assert (best_utility < FLT_MAX/2); assert (node_list[best_neighbour].utility < node_list[i].utility); } free(fwd_utilities); delete_queue(&the_state_queue); }
void destroy_node(struct node *node){ delete_queue(node->queue); free(node); }
TEST(PRIORITY_QUEUE,ENQUEUE){ //try a bad ticket numbers ELEMENT e = {"test",9}; RESULT r = enqueue(e,-313213); EXPECT_EQ(r.code, TICKET_INVALID); r = enqueue(e,0); EXPECT_EQ(r.code, TICKET_INVALID); //create a valid queue WELCOME_PACKET packet = create_queue(); //try bad priority e = fill_element("test",-3); r = enqueue(e,packet.ticket); EXPECT_EQ(r.code, ITEM_INVALID); //try bad priority e = fill_element("test",11); r = enqueue(e,packet.ticket); EXPECT_EQ(r.code, ITEM_INVALID); //fill queue e = fill_element("test",4); for(int i = 0; i < MAXIMUM_NUMBER_OF_ELEMENTS_IN_A_QUEUE; i++){ r = enqueue(e,packet.ticket); EXPECT_EQ(get_size(packet.ticket).size,i+1); EXPECT_EQ(r.code, SUCCESS); } //add one to many to the queue r = enqueue(e,packet.ticket); EXPECT_EQ(r.code, QUEUE_IS_FULL); //delete the queue r = delete_queue(packet.ticket); EXPECT_EQ(r.code,SUCCESS); //try to enque on a deleted queue r = enqueue(e,packet.ticket); EXPECT_EQ(r.code, TICKET_INVALID); /////////////////////////////////////////////////////////// // CHECK FOR THE CORRECT ENQUEUEUING BASED ON PRIORITIES // srand(time(NULL)); packet = create_queue(); // fill queue with max elements with random priorities from // [0-10], then compare the insertion number as the base of the // comparision char temp[1024]; for(int i = 0; i < MAXIMUM_NUMBER_OF_ELEMENTS_IN_A_QUEUE; i++){ snprintf(temp,1024,"%d",i); e = fill_element(temp,rand()%10); r = enqueue(e,packet.ticket); EXPECT_EQ(get_size(packet.ticket).size,i+1); EXPECT_EQ(r.code, SUCCESS); } ELEMENT_RESULT er; ELEMENT_RESULT last; last = dequeue(packet.ticket); for(int i = 1; i < MAXIMUM_NUMBER_OF_ELEMENTS_IN_A_QUEUE; i++){ er = dequeue(packet.ticket); EXPECT_TRUE(last.element.priority >= er.element.priority); if(last.element.priority == er.element.priority) EXPECT_TRUE(atoi(last.element.item) < atoi(er.element.item)); last = er; } }
void mv_stop(void) { free(memory); free(frame_of); delete_queue(fifo); }
int main() { const int max_entries = 5; /* size of the queue */ /* define some variables to hold Foo structs*/ Foo* new_foo1; Foo* new_foo2; Foo* new_foo3; Foo* new_foo4; Foo* new_foo5; Foo* new_foo6; Foo* returned_foo; /* create a Queue struct with max_entries size */ Queue *new_queue = create_queue(max_entries); /* show pointer addresses to demonstrate wrap around */ printf("\nWrap around demonstration before Queue is modified:"); printf("\nBuffer: %p\n", new_queue); printf("Head: %p\n", new_queue->queue_head); printf("Tail: %p\n\n", new_queue->queue_tail); /* allocate a Foo and add it onto the queue */ new_foo1 = (Foo *) malloc(sizeof(Foo)); new_foo1->x = 6; new_foo1->y = 14.79; printf("Adding: x = %5d, y = %10.3f\n", new_foo1->x, new_foo1->y); enqueue(new_queue, (void *) new_foo1); /* allocate a Foo and add it onto the queue */ new_foo2 = (Foo *) malloc(sizeof(Foo)); new_foo2->x = 217; new_foo2->y = 3.14159; printf("Adding: x = %5d, y = %10.3f\n", new_foo2->x, new_foo2->y); enqueue(new_queue, (void *) new_foo2);; /* allocate a Foo and add it onto the queue */ new_foo3 = (Foo *) malloc(sizeof(Foo)); new_foo3->x = 500; new_foo3->y = 4.50483; printf("Adding: x = %5d, y = %10.3f\n", new_foo3->x, new_foo3->y); enqueue(new_queue, (void *) new_foo3); /* allocate a Foo and add it onto the queue */ new_foo4 = (Foo *) malloc(sizeof(Foo)); new_foo4->x = 4059; new_foo4->y = 22.48300; printf("Adding: x = %5d, y = %10.3f\n", new_foo4->x, new_foo4->y); enqueue(new_queue, (void *) new_foo4); /* allocate a Foo and add it onto the queue */ new_foo5 = (Foo *) malloc(sizeof(Foo)); new_foo5->x = 1010; new_foo5->y = 1056.3827; printf("Adding: x = %5d, y = %10.3f\n", new_foo5->x, new_foo5->y); enqueue(new_queue, (void *) new_foo5); /* show pointer addresses to demonstrate wrap around */ printf("\nWrap around demonstration after max entries are added:"); printf("\nBuffer: %p\n", new_queue); printf("Head: %p\n", new_queue->queue_head); printf("Tail: %p\n\n", new_queue->queue_tail); /* attempt to add more than max number of entries onto the queue */ new_foo6 = (Foo *) malloc(sizeof(Foo)); new_foo6->x = 666; new_foo6->y = 66.666666; printf("Attempting to add more than max # of entries to the queue.\n"); printf("Last entry is not added to queue!\n"); printf("Adding: x = %5d, y = %10.3f\n", new_foo6->x, new_foo6->y); enqueue(new_queue, (void *) new_foo6); /* remove entries from the queue and print them */ returned_foo = (Foo *) dequeue(new_queue); printf("Removed: x = %5d, y = %10.3f\n", returned_foo->x, returned_foo->y); returned_foo = (Foo *) dequeue(new_queue); printf("Removed: x = %5d, y = %10.3f\n", returned_foo->x, returned_foo->y); returned_foo = (Foo *) dequeue(new_queue); printf("Removed: x = %5d, y = %10.3f\n", returned_foo->x, returned_foo->y); returned_foo = (Foo *) dequeue(new_queue); printf("Removed: x = %5d, y = %10.3f\n", returned_foo->x, returned_foo->y); returned_foo = (Foo *) dequeue(new_queue); printf("Removed: x = %5d, y = %10.3f\n", returned_foo->x, returned_foo->y); /* show pointer addresses to demonstrate wrap around */ printf("\nWrap around demonstration after entries are removed:"); printf("\nBuffer: %p\n", new_queue); printf("Head: %p\n", new_queue->queue_head); printf("Tail: %p\n\n", new_queue->queue_tail); /* attempt to remove non-existant element from queue */ returned_foo = (Foo *) dequeue(new_queue); /* check for removing more elements than exist in the queue */ if(returned_foo != NULL) { printf("Removed: x = %5d, y = %10.3f\n", returned_foo->x, returned_foo->y); } else { printf("\nError. Trying to remove nonexistant entry from queue!\n"); } /* end else */ /* free any allocated memory and clean up */ delete_queue(new_queue); free(new_foo1); free(new_foo2); free(new_foo3); free(new_foo4); free(new_foo5); free(new_foo6); return 0; /* success */ } /* end function main */