int handle_r_p2( clidata_t *client, char *hdrs ) { int expected = get_content_length(hdrs); queue_t *sendq = client->sendq; int chan2 = client->chan2; struct timespec ts; char *body; int sex; char *pkt; if( !expected ) { lprintf(log, WARN, "Client sent no Content-Length. Dropping."); goto cleanup1; } if( (body=getbody(chan2, hdrs, &expected)) == NULL ) { lprintf(log, WARN, "getbody() failed. Dropping client."); goto cleanup1; } if( (sex=strtol(body, NULL, 0)) == 0 ) { lprintf(log, WARN, "Client sent invalid seconds spec."); fdprintf(chan2, RESPONSE_400); goto cleanup2; } ts.tv_nsec = 0; ts.tv_sec = sex; dprintf(log, DEBUG, "waiting up to %d seconds.", sex); if( q_timedwait(sendq, &ts) ) { int sent=0, total, cnt=0; dprintf(log, DEBUG, "returned from wait, with data"); total = sendq->totsize; fdprintf(chan2, RESPONSE_200_NOBODY, sendq->totsize); while( sent < total ) { if( (pkt=q_remove(sendq, 0, NULL)) == NULL ) { lprintf(log, WARN, "q_remove failed!"); goto cleanup2; } if( write(chan2, pkt, iplen(pkt)) < 0 ) { lprintf(log, INFO, "send failed: %s", strerror(errno)); free(pkt); goto cleanup2; } cnt++; sent += iplen(pkt); free(pkt); } lprintf(log, INFO, "Sent %d bytes in %d pkts", sent, cnt); } else { dprintf(log, DEBUG, "returned from wait, with NO data"); if( client->chan2 != -1 ) fdprintf(chan2, RESPONSE_204); } return 0; cleanup2: free(body); cleanup1: return -1; }
char * test_remove_null(void) { mu_assert("init", q_init() == q_success); mu_assert("insert", q_insert(18) == q_success); mu_assert("peek", q_remove(NULL) == q_failure); return NULL; }
static void do_staff_chording(staff_p f, int do_chording) { symbol_p scan; symbol_p chord = NULL; symbol_p next; for (scan = f->unvoiced.front; scan != NULL; scan = next) { next = scan->next; if (scan->type != SYM_NOTE) { continue; } if (scan->prev != NULL && mpq_equal(scan->start, scan->prev->start)) { VPRINTF("\nInspect note start "); VPRINT_MPQ(scan->start); VPRINTF(" step %d length ", scan->symbol.note.value); VPRINT_MPQ(scan->symbol.note.duration); } for (chord = scan->prev; chord != NULL && mpq_equal(scan->start, chord->start); chord = chord->prev) { if (chord->type == scan->type) { VPRINTF(" against chord start "); VPRINT_MPQ(chord->start); VPRINTF(" step %d length ", chord->symbol.note.value); VPRINT_MPQ(chord->symbol.note.duration); } if (! (scan->symbol.note.flags & FLAG_REST) && chord->type == scan->type && chord->symbol.note.stem == scan->symbol.note.stem && mpq_equal(chord->symbol.note.duration, scan->symbol.note.duration) ) { /* OK, it is a chord continuation, as far as we can tell */ VPRINTF("\nFound a chord note"); if (do_chording) { q_remove(&f->unvoiced, scan); scan->symbol.note.chord = chord->symbol.note.chord; chord->symbol.note.chord = &scan->symbol.note; } else { // Generate a new stem, duplicate ties and slurs note_p note = &chord->symbol.note; stem_p stem = note->stem; symbol_p stem_symbol = SYMBOL_OF_SYM(stem); note->stem = &symbol_clone(stem_symbol)->symbol.stem; // We need to check both scan and chord if there // are slurs or ties to duplicate if (note->flags & FLAG_REST) { stem->slur_start = -1; stem->slur_end = -1; } else { if (stem->slur_start != -1) { int dupl_slur = slur_dupl_create(stem->slur_start); VPRINTF("Duplicate its slur %d -> %d\n", stem->slur_start, dupl_slur); stem->slur_start = dupl_slur; } if (stem->slur_end != -1) { int dupl_slur = slur_dupl_lookup(stem->slur_end); VPRINTF("Finish its duplicate slur %d -> %d\n", stem->slur_start, dupl_slur); stem->slur_end = dupl_slur; } } if (0) { if (note->tie_start != -1) { tie_p tie = &ties[note->tie_start]; if (tie_dupl_lookup(note->tie_start) == -1) { note->tie_start = tie_dupl_create(note->tie_start); tie = &ties[note->tie_start]; tie->occurred = 0; tie->occur = ties[chord->symbol.note.tie_start].occur; tie->notes = calloc(tie->occur, sizeof *tie->notes); } tie->occurred++; tie->notes[tie->occurred - 1] = note; } if (note->tie_end != -1) { note->tie_end = tie_dupl_lookup(note->tie_end); tie_p tie = &ties[note->tie_start]; tie->notes[tie->occurred - 1] = note; } } } break; } } } }
char * test_too_many_remove(void) { int v; mu_assert("init", q_init() == q_success); mu_assert("remove", q_remove(&v) == q_failure); return NULL; }
void si_remove(sorted_intlist si, int to_remove) { q_remove(si->q, (void *)to_remove); }
int main(int argc, char *argv[]) { /* test harness */ int tests_passed = 0; int tests_failed = 0; /* create a queue with a size hint */ printf("Creating queue\n"); queue qp = q_create(20); /* how do we see if queue was propery initialized? */ /* add and remove one element */ int e1 = 42; q_add(qp, e1); printf("Test 1: "); q_printf(qp); printf("\n"); /* length should have gone up by one */ if ( q_length(qp) != 1 ) { printf("Test 1 failed, length %d should be 1\n", q_length(qp)); tests_failed++; } else { printf("Test 1 passed.\n"); tests_passed++; } printf("Test 2: "); int e2 = q_remove(qp); q_printf(qp); printf("\n"); if ( q_length(qp) != 0 ) { printf("Test 2.1 failed, length %d should be 0\n", q_length(qp)); tests_failed++; } else { printf("Test 2.1 passed.\n"); tests_passed++; } if ( e1 != e2 ) { printf("Test 2.2 failed, e2 %d should equal e1 %d\n", e2, e1); tests_failed++; } else { printf("Test 2.2 passed.\n"); tests_passed++; } printf("Test 3: "); for (int i=1; i <= 10; i++) { q_add(qp, i); } q_printf(qp); printf("\n"); for (int i=1; i<= 10; i++) { e1 = q_remove(qp); if ( q_length(qp) != 10-i ) { printf("Test 3.1 failed, length %d should be %d\n", q_length(qp), 10-i); tests_failed++; } else { tests_passed++; } if ( e1 != i ) { printf("Test 3.2 failed, element %d should be %d\n", e1, i); tests_failed++; } else { tests_passed++; } } printf("Test 4: "); for (int i=1; i <= 10; i++) { q_add(qp, i); } q_printf(qp); printf("\n"); for (int i=0; i < 10; i++) { int expected = i + 1; int actual = q_get_item(qp, i); if(expected != actual) { printf("Test 4 failed, element #%d should be %d but was %d\n", i, expected, actual); tests_failed++; } else { tests_passed++; } } // Reset to empty for (int i=1; i <= 10; i++) { q_remove(qp); } q_add(qp, e1); printf("Test 5.1: "); q_printf(qp); printf("\n"); /* length should have gone up by one */ if ( q_length(qp) != 1 ) { printf("Test 5.1 failed, length %d should be 1 before deletion\n", q_length(qp)); tests_failed++; } else { int actual = q_delete_item(qp, 0); if( q_length(qp) != 0) { printf("Test 5.1 failed, length %d should be 0 after deletion\n", q_length(qp)); tests_failed++; } else if(actual != e1) { printf("Test 5.1 failed, element retrieved from deleted: Expected: %d; Actual: %d\n", e1, actual); tests_failed++; } else { printf("Test 5.1 passed.\n"); tests_passed++; } } printf("Test 5.2: "); for (int i=1; i <= 10; i++) { q_add(qp, i); } q_printf(qp); printf("\n"); int deletedElement; deletedElement = q_delete_item(qp, 4); if(deletedElement != 5) { printf("Test 5.2 failed, element retrieved from deleted: Expected: 5; Actual: %d\n", deletedElement); tests_failed++; } deletedElement = q_delete_item(qp, 4); if(deletedElement != 6) { printf("Test 5.2 failed, element retrieved from deleted: Expected: 6; Actual: %d\n", deletedElement); tests_failed++; } q_printf(qp); printf("\n"); if( q_length(qp) != 8) { printf("Test 5.2 failed, length %d should be 8 after deletion\n", q_length(qp)); tests_failed++; } else { if(q_get_item(qp, 4) != 7) { printf("Test 5.2 failed, value of element at index 4 after deletion: Expected: 7; Actual: %d\n", q_get_item(qp, 4)); tests_failed++; } else { printf("Test 5.2 passed.\n"); tests_passed++; } } /* a fatal test */ if ( 0 ) { printf("Test 4: remove on empty queue\n"); e2 = q_remove(qp); tests_failed++; } printf("Tests Passed: %d\n", tests_passed); printf("Tests Failed: %d\n", tests_failed); }
int main( /* int argc, char const *argv[] */ ) { printf("Projet Monoplan, Lenouvel Baptiste\n\n"); printf("======== TEST LIFO ========\n\n"); lifo_t stack; init_lifo(&stack, STACK_SIZE); printf("Push 42\n"); push(&stack, 42.0); printf("Push 1337\n"); push(&stack, 1337.0); printf("Push 31137\n"); push(&stack, 31337.0); printf("pop : %lf \n", pop(&stack)); printf("pop : %lf \n", pop(&stack)); printf("pop : %lf \n", pop(&stack)); printf("pop : %lf \n", pop(&stack)); printf("pop : %lf \n", pop(&stack)); free_lifo(&stack); printf("\n======== TEST QUEUE ======== \n\n"); queue_t * queue; int test = 1337; char * str = "apprenti"; char * str1 = "loick_et_ses_poules"; char * str2 = "limousin"; char * str3 = "ZZtop"; printf("Init queue : "); init_queue(&queue); printf("ok\n\n"); printf("Push %s \n", str); q_push(&queue, str); printf("Push %s \n", str1); q_push(&queue, str1); printf("Push in head : %d \n", test); q_push_head(&queue, &test); printf("Push in head : %s \n\n", str2); q_push_head(&queue, str2); printf("Removing %s ... %s \n\n", str2, q_remove(&queue, str2) ? "ok" : "ko"); // printf("Pop head : %s \n", (char *)(q_pop_head(&queue)) ); printf("Pop head : %d \n", *(int *)(q_pop_head(&queue)) ); printf("Pop head : %s \n", (char *)(q_pop_head(&queue)) ); printf("Pop head : %s \n\n", (char *)(q_pop_head(&queue)) ); printf("Push %s with q_operation \n\n", str3); q_operation(&q_push, &queue, str3); q_free(&queue); return 0; }
int main() { // SETUP init(); Serial.begin(9600); tft.initR(INITR_BLACKTAB); // initialize screen // Setting the joystick button and LEDs pinMode(JOYSTICK_BUTTON, INPUT); digitalWrite(JOYSTICK_BUTTON, HIGH); // Initialize the SD card Serial.print("Initializing SD card..."); if (!SD.begin(SD_CS)) { Serial.println("failed!"); while(true) {} // something is wrong } else {Serial.println("OK!");} // More initialization Serial.print("Initializing Raw SD card..."); if (!card.init(SPI_HALF_SPEED, SD_CS)) { Serial.println("failed!"); while(true) {} // something is wrong } else {Serial.println("OK!");} // Create states for different modes // C1 for Mode 1 - MENU screen // C2 for Mode 2 - Snake Game // C3 for Mode 3 - GAME OVER screen // C4 for Mode 4 - Choose level // C5 for Mode 5 - PAUSE screen typedef enum {C1 = 1, C2, C3, C4, C5, ERR} State; State state = C1; int select, snakelength; while (state!=ERR) { if (state == C1) { /// ====== MODE 1 - MENU ====== /// Serial.println("Currently in Mode 1"); snakelength = 1; init_vert = analogRead(JOYSTICK_VERT); init_horiz = analogRead(JOYSTICK_HORIZ); // preparations for the game - to not overlap with the pause menu q = q_create(720); i = 64; // x component j = 80; // y component q_add(q,i,j); // load into the queue random_x = food_x(); // load x coordinate of food piece random_y = food_y(); // load y coordinate of food piece pausedirection = 0; // set paused direction to 0 // reset grid to 0 for (int a = 0; a < 24; a++) { for (int b = 0; b < 30; b++) { grid[a][b] = 0; } } // display main menu snake(); tft.setTextSize(2); while(true) { // alternate highlighting of START unsigned long time = millis()%1000; int a = time%1000; if ((a<17)) { tft.setCursor(34, 83); tft.fillRoundRect(30,80,65,20,5,WHITE); tft.setTextColor(RED); tft.print("START"); } else if ((a>500) && (a<520)) { tft.setCursor(34, 83); tft.fillRoundRect(30,80,65,20,5,RED); tft.setTextColor(WHITE); tft.print("START"); } // Read the Joystick - HIGH if not pressed, LOW otherwise select = digitalRead(JOYSTICK_BUTTON); if (select == LOW) { break; } } state = C4; } else if (state == C2) { /// ====== MODE 2 - SNAKE GAME ====== /// Serial.println("Currently in Mode 2"); delay(50); soundsetup(); //setting up sound pin // print the background tft.fillScreen(DARKRED); tft.fillRect(4,5,120,150,DARKGRN); // print the snake int x,y; x = q_frontx(q); y = q_fronty(q); tft.fillRect(x,y,5,5, WHITE); //Bringing the food in, outside while loop first. tft.fillRect(random_x, random_y, 5, 5, YELLOW); // do auto calibration int px, py; int lastmove; // read beginning direction chosen by user if (pausedirection == 0) { direction = read_direction(); } else { direction = pausedirection; } lastmove = direction; while (true) { // to direct movement // (without going in reverse direction of previous movement) // up if (direction == 1) { if (lastmove == 2) { direction = 2; j = j-5; } else { j = j+5; } q_add(q,i,j); } // down else if (direction == 2) { if (lastmove == 1) { direction = 1; j = j+5; } else { j = j-5; } q_add(q,i,j); } // right else if (direction == 3) { if (lastmove == 4) { direction = 4; i = i-5; } else { i = i+5; } q_add(q,i,j); } // left else if (direction == 4) { if (lastmove == 3) { direction = 3; i = i+5; } else { i = i-5; } q_add(q,i,j); } // if the direction is changed, store the new direction & last move int new_direc = read_direction(); if ((new_direc != direction) && (new_direc != 0)) { lastmove = direction; direction = new_direc; } // if the snake hits a piece of food, the food vanishes and gets replaced if ((i == random_x) && (j == random_y)) { // snake grows by 4 squares, except for the first time // this allows for it to end up as a max of 720 in the queue if (snakelength == 1) { q_add(q,i,j); q_add(q,i,j); q_add(q,i,j); snakelength += 3; } else { q_add(q,i,j); q_add(q,i,j); q_add(q,i,j); q_add(q,i,j); snakelength += 4; } if (snakelength < 720) { random_x = food_x(); random_y = food_y(); // if the snake is already there, find a new spot for the food while (grid[random_x/5][random_y/5-1] == 1) { random_x = food_x(); random_y = food_y(); } // print the new food tft.fillRect(random_x, random_y, 5, 5, YELLOW); } } // if the snake runs over itself if ((snakelength > 1) && (grid[i/5][j/5-1] == 1)) { delay(450); // pause when snake runs into itself int m = 0; soundLoop(); while(m < 6000) { int rand_x = dissolve_x(); int rand_y = dissolve_y(); tft.fillRect(rand_x, rand_y, 5, 5, BLACK); m++; } state = C3; break; } px = q_frontx(q); py = q_fronty(q); // reprint the snake if there is movement if ((i != px) || (j != py)) { tft.fillRect(i,j,5,5, WHITE); grid[i/5][j/5-1] = 1; // snake body is in grid tft.fillRect(px,py,5,5,DARKGRN); grid[px/5][py/5-1] = 0; // snake body is no longer in grid q_remove(q); // take away from the queue delay(speed); // controls the speed of the snake } // if any of the borders are hit if ((i < 4)||(j < 5)||(i > 119)||(j > 150)) { delay(450); // pause when border is hit // dissolve the screen int m = 0; soundLoop(); while(m < 6000) { int rand_x = dissolve_x(); int rand_y = dissolve_y(); tft.fillRect(rand_x, rand_y, 5, 5, BLACK); m++; } //~ delay(250); state = C3; break; } // Read the Joystick - HIGH if not pressed, LOW otherwise select = digitalRead(JOYSTICK_BUTTON); if (select == LOW) { state = C5; break; } } } else if (state == C3) { /// ====== MODE 3 - GAME OVER ====== /// Serial.println("Currently in Mode 3"); q_destroy(q); // clear the queue tft.fillScreen(BLACK); tft.fillRoundRect(5,20,118,25,5,RED); tft.setCursor(10, 25); tft.setTextColor(BLACK); tft.setTextSize(2); tft.setTextWrap(true); tft.print("GAME OVER"); tft.print("\n"); tft.setCursor(10, 55); tft.setTextColor(RED); tft.setTextSize(1.5); if (snakelength >= 720) { snakelength = 720; tft.print("YOU WON! CONGRATZ"); } else { tft.print(" Oh no! You hit something!"); } tft.setCursor(10, 80); tft.setTextColor(WHITE); tft.setTextSize(1); tft.print("Length of Snake:"); tft.print(snakelength); tft.setCursor(10, 100); tft.print("Press the joystick to return to main menu"); // Read the Joystick - HIGH if not pressed, LOW otherwise while (true) { select = digitalRead(JOYSTICK_BUTTON); if (select == LOW) { break; } } state = C1; } else if (state == C4) { /// ====== MODE 4 - CHOOSE LEVEL ====== /// Serial.println("Currently in Mode 4"); // printing // snake display snake(); // difficulty levels tft.setTextSize(2); tft.setTextColor(WHITE); easy(RED); tft.setTextColor(RED); medium(WHITE); hard(WHITE); int selection = 1; int oldselection; while(true) { // read direction from the user for updating selection oldselection = selection; vertical = analogRead(JOYSTICK_VERT); // will be 0-1023 delay(100); // scroll down if (vertical > init_vert + 200) { selection++; if (selection > 3) { selection = 0; } } // scroll up else if (vertical < init_vert - 200) { selection--; if (selection < 0) { selection = 3; } } if (selection != oldselection) { update(selection); } // Read the Joystick - HIGH if not pressed, LOW otherwise select = digitalRead(JOYSTICK_BUTTON); if (select == LOW) { Serial.print("made selection: "); Serial.println(selection); if (selection == 1) {speed = 225;} else if (selection == 2) {speed = 150;} else if (selection == 3) {speed = 75;} break; } } state = C2; } else if (state == C5) { /// ====== MODE 5 - PAUSE MENU ====== /// Serial.println("Currently in Mode 5"); pausedirection = direction; // printing snake and pause snake(); tft.setTextSize(2); tft.setCursor(34, 73); tft.fillRoundRect(30,70,65,20,5,WHITE); tft.setTextColor(RED); tft.print("Pause"); while(true) { // Read the Joystick - HIGH if not pressed, LOW otherwise select = digitalRead(JOYSTICK_BUTTON); if (select == LOW) { break; } } // reset grid to 0 for (int a = 0; a < 24; a++) { for (int b = 0; b < 30; b++) { grid[a][b] = 0; } } state = C2; } //if not any of this: else { Serial.println("There has been an error"); state = ERR; } } Serial.end(); return 0; }