/* Test run_enzyme functionality */ int test_run_enzyme(int argc, char **argv) { char* string = strdup("dcba"); workperformed = 0; please_quit = 0; pthread_t tid[3]; thread_info_t data1 = {string,0}; thread_info_t data2 = {string+1,0}; thread_info_t data3 = {string+2,0}; pthread_create(&tid[0], NULL,run_enzyme,&data1 ); pthread_create(&tid[1], NULL,run_enzyme,&data2 ); pthread_create(&tid[2], NULL,run_enzyme,&data3 ); sched_yield(); sleep(1); sched_yield(); sleep(1); sched_yield(); please_quit = 1; void* results[3]; pthread_join(tid[0], &results[0]); pthread_join(tid[1], &results[1]); pthread_join(tid[2], &results[2]); quit_if((thread_info_t*)results[0] != &data1) ; quit_if((thread_info_t*)results[1] != &data2) ; quit_if((thread_info_t*)results[2] != &data3) ; quit_if(data1.swapcount + data2.swapcount+ data3.swapcount != 6); return EXIT_SUCCESS; }
/* Test normal functionality */ int test_sort(int argc, char **argv) { char *args[] = { "./enzyme", "ddcba", NULL }; FILE *err; int rv,itmp1,itmp2,totalswap; /* Run the test */ run_test("", 2, args); /* Check output */ err = fopen("smp2.out", "r"); fscanf(err,"Creating threads...\nDone creating %d threads.\nJoining threads...\n",&itmp1); quit_if(itmp1 != 4); int found=1, lines=0; totalswap = 0; while(found<5) { lines++; quit_if(lines > 100); rv = fscanf(err, "Thread %d exited normally: %d swaps.\n",&itmp1,&itmp2); if(rv == 2) {totalswap += itmp2; found ++;} quit_if(rv<0 || feof(err)); } rv = fscanf(err, "Total: %d swaps\n" "Sorted string: abcdd\n",&itmp1); quit_if(rv != 1 || itmp1 != totalswap); quit_if(!feof(err)); fclose(err); return EXIT_SUCCESS; }
// Checks the swapcount value int test_swap_count_workperformed2(int argc, char**argv) { please_quit=0; thread_info_t data = {strdup("ba"),0}; pthread_t tid; pthread_create(&tid, NULL,run_enzyme,&data ); sched_yield(); sleep(1); please_quit=1; void * result; int r=pthread_join(tid, &result); quit_if(r!= 0 || result != &data) ; quit_if(data.swapcount != 1) ; return EXIT_SUCCESS; }
/* Test make_threads functionality */ int test_make_enzyme_threads(int argc, char **argv) { char string[] = "enzymatic"; int i; workperformed = 0; please_quit = 0; pthread_t enzymes[8]; int n = make_enzyme_threads(enzymes,string,dummy_enzyme); quit_if(n != 8); int c = 0; void *status; for(i=0;i<n;i++) { int rv = pthread_join(enzymes[i],&status); if (rv != 0) return -1; else if (status != NULL) { c += ((thread_info_t *)status)->swapcount; } } quit_if(c != 105); return EXIT_SUCCESS; }
/* Prepare input, reroute file descriptors, and run the program. */ void run_test(const char *input, int argc, char **argv) { /* Prepare input */ FILE *in = fopen("smp2.in", "w"); fprintf(in, input); fclose(in); /* Run the program */ int child_pid=fork(); if(child_pid == 0) { /* Reroute standard file descriptors */ freopen("smp2.in", "r", stdin ); freopen("smp2.out", "w", stdout); freopen("smp2.err", "w", stderr); quit_if(smp2_main(argc, argv) != EXIT_SUCCESS); exit(0); } else { int status; waitpid(child_pid,&status,0); } }
/* Test join_on_enzymes functionality */ int test_join(int argc, char **argv) { char* string = strdup("dcba"); workperformed = 0; please_quit = 0; pthread_t tid[3]; thread_info_t data1 = {string,0}; thread_info_t data2 = {string+1,0}; thread_info_t data3 = {string+2,0}; pthread_create(&tid[0], NULL,dummy_enzyme,&data1 ); pthread_create(&tid[1], NULL,dummy_enzyme,&data2 ); pthread_create(&tid[2], NULL,dummy_enzyme,&data3 ); freopen("smp2.out","w",stdout); freopen("smp2.err","w",stderr); int tot = join_on_enzymes(&tid[0],3); printf("tot=%d\n",tot); quit_if(tot != 6); return EXIT_SUCCESS; }
//#line 6 "../PROTOTYPES/ladders.ch" int main(int argc, char *argv[]) //#line 94 "../ladders.w" { /*5:*/ //#line 120 "../ladders.w" while (--argc) { if (strcmp(argv[argc], "-v") == 0) verbose = 1; else if (strcmp(argv[argc], "-a") == 0) alph = 1; else if (strcmp(argv[argc], "-f") == 0) freq = 1; else if (strcmp(argv[argc], "-h") == 0) heur = 1; else if (strcmp(argv[argc], "-e") == 0) echo = 1; else if (sscanf(argv[argc], "-n%lu", &n) == 1) randm = 0; else if (sscanf(argv[argc], "-r%lu", &n) == 1) randm = 1; else if (sscanf(argv[argc], "-s%ld", &seed) == 1); else { fprintf(stderr, "Usage: %s [-v][-a][-f][-h][-e][-nN][-rN][-sN]\n", argv[0]); return -2; } } if (alph || randm) freq = 0; if (freq) heur = 0; /*:5*/ //#line 95 "../ladders.w" ; /*6:*/ //#line 149 "../ladders.w" g = words(n, (randm ? zero_vector : NULL), 0L, seed); quit_if(g == NULL, panic_code); /*8:*/ //#line 165 "../ladders.w" if (verbose) { if (alph) printf("(alphabetic distance selected)\n"); if (freq) printf("(frequency-based distances selected)\n"); if (heur) printf("(lowerbound heuristic will be used to focus the search)\n"); if (randm) printf("(random selection of %ld words with seed %ld)\n", g->n, seed); else printf("(the graph has %ld words)\n", g->n); } /*:8*/ //#line 152 "../ladders.w" ; /*9:*/ //#line 183 "../ladders.w" if (alph) { register Vertex *u; for (u = g->vertices + g->n - 1; u >= g->vertices; u--) { register Arc *a; register char *p = u->name; for (a = u->arcs; a; a = a->next) { register char *q = a->tip->name; a->len = a_dist(a->loc); } } } else if (freq) { register Vertex *u; for (u = g->vertices + g->n - 1; u >= g->vertices; u--) { register Arc *a; for (a = u->arcs; a; a = a->next) a->len = freq_cost(a->tip); } } /*:9*/ //#line 153 "../ladders.w" ; /*10:*/ //#line 202 "../ladders.w" if (alph || freq || heur) { init_queue = init_128; del_min = del_128; enqueue = enq_128; requeue = req_128; } /*:10*/ //#line 154 "../ladders.w" ; /*:6*/ //#line 96 "../ladders.w" ; while (1) { /*26:*/ //#line 373 "../ladders.w" putchar('\n'); restart: if (prompt_for_five("Starting", start) != 0) break; if (prompt_for_five(" Goal", goal) != 0) goto restart; /*:26*/ //#line 98 "../ladders.w" ; /*13:*/ //#line 245 "../ladders.w" /*14:*/ //#line 251 "../ladders.w" gg = gb_new_graph(0L); quit_if(gg == NULL, no_room + 5); gg->vertices = g->vertices; gg->n = g->n; /*15:*/ //#line 266 "../ladders.w" (gg->vertices + gg->n)->name = start; uu = find_word(start, plant_new_edge); if (!uu) uu = gg->vertices + gg->n++; /*:15*/ //#line 256 "../ladders.w" ; /*16:*/ //#line 272 "../ladders.w" if (strncmp(start, goal, 5) == 0) vv = uu; else { (gg->vertices + gg->n)->name = goal; vv = find_word(goal, plant_new_edge); if (!vv) vv = gg->vertices + gg->n++; } /*:16*/ //#line 257 "../ladders.w" ; if (gg->n == g->n + 2) /*19: */ //#line 311 "../ladders.w" if (hamm_dist(start, goal) == 1) { gg->n--; plant_new_edge(uu); gg->n++; } /*:19*/ //#line 258 "../ladders.w" ; quit_if(gb_trouble_code, no_room + 6); /*:14*/ //#line 246 "../ladders.w" ; /*21:*/ //#line 333 "../ladders.w" if (!heur) min_dist = dijkstra(uu, vv, gg, NULL); else if (alph) min_dist = dijkstra(uu, vv, gg, alph_heur); else min_dist = dijkstra(uu, vv, gg, hamm_heur); /*:21*/ //#line 247 "../ladders.w" ; /*24:*/ //#line 350 "../ladders.w" if (min_dist < 0) printf("Sorry, there's no ladder from %s to %s.\n", start, goal); else print_dijkstra_result(vv); /*:24*/ //#line 248 "../ladders.w" ; /*25:*/ //#line 360 "../ladders.w" for (uu = g->vertices + gg->n - 1; uu >= g->vertices + g->n; uu--) { register Arc *a; for (a = uu->arcs; a; a = a->next) { vv = a->tip; vv->arcs = vv->arcs->next; } uu->arcs = NULL; } gb_recycle(gg); /*:25*/ //#line 249 "../ladders.w" ; /*:13*/ //#line 100 "../ladders.w" ; } return 0; }
int main(int argc, char *argv[]) { // Catch signals signal(SIGINT, uquad_sig_handler); signal(SIGQUIT, uquad_sig_handler); int i, retval; FILE *file; uquad_mat_t *x, *w; uquad_bool_t ctrl_outdated = false; double w_hover; if(argc < 2) { err_log_str("Invalid arguments!",USAGE) quit(); } else { file = fopen(argv[1],"r"); if(file == NULL) { err_log_stderr("Failed to open log!"); quit(); } } x = uquad_mat_alloc(STATE_COUNT,1); w = uquad_mat_alloc(LENGTH_INPUT,1); if(x == NULL || w == NULL) { quit_log_if(ERROR_MALLOC,"Failed to allocate tmp mem!"); } /** * The following should be done by calling mot_update_w_hover(), this * is just a test program. */ retval = uquad_solve_pol2(&w_hover, NULL, F_B1, F_B2, -GRAVITY*MASA_DEFAULT/4.0); quit_log_if(retval, "Failed to get w_hover!"); for(i=0; i<LENGTH_INPUT; ++i) w->m_full[i] = w_hover; retval = uquad_mat_zeros(x); quit_if(retval); ctrl = control_init(); if(ctrl == NULL) { quit_if(ERROR_FAIL); } pp = pp_init(); if(pp == NULL) { quit_if(ERROR_FAIL); } retval = pp_new_setpoint(pp, x, w); quit_if(retval); retval = control_update_K(ctrl, pp, MASA_DEFAULT); quit_log_if(retval, "Failed to update control matrix! Aborting..."); for(;;) { retval = uquad_mat_load(x,file); if(retval != ERROR_OK) { quit_log_if(retval, "End of log?"); } #warning "w_hover should use mot_control.h!!" retval = pp_update_setpoint(pp, x, w_hover, &ctrl_outdated); quit_if(retval); if(ctrl_outdated) { retval = control_update_K(ctrl, pp, MASA_DEFAULT); quit_log_if(retval, "Failed to update control matrix! Aborting..."); retval = control_dump(ctrl, NULL); quit_log_if(retval, "Failed to dump new control matrix! Aborting..."); } retval = control(ctrl, w, x, pp->sp, TS_DEFAULT_US); quit_if(retval); ctrl_outdated = false; } // Never gets here quit(); }