Ejemplo n.º 1
0
void test_store_given_5_6_7_8_9_10_should_return_5_6_7_8_9_10(void)
{
	CEXCEPTION_T err;
	
	List *list = listNew(6);
	
	int integers[] = {5,6,7,8,9,10};

	Try
	{
		store(list,integers,6);
		
	}
	Catch(err)
	{
		TEST_FAIL_MESSAGE("Do not expect exception to be generated.");
	}
	
	TEST_ASSERT_EQUAL(5,*(list->buffer));
	TEST_ASSERT_EQUAL(6,*(list->buffer)+1);
	TEST_ASSERT_EQUAL(7,*(list->buffer)+2);

	listDel(list);
}
Ejemplo n.º 2
0
/**
 *  Case 4
 *
 *  Add Zorro into a map. Zorro is first hashed and hash value 3 is obtained.
 *  Ali is already placed in the bucket 3.
 *  Zorro will then be added to the head of the bucket 3.
 */
void test_mapStore_given_Zorro_should_add_into_the_head_when_there_is_already_an_Ali_stored(){
  CEXCEPTION_T e;
  Person *person1 = personNew("Ali", 25, 70.3);
  Person *person2 = personNew("Zorro", 60, 70.3);
  List *list = listNew(person1, NULL);
  Map *map = mapNew(5);
  
  // hash_ExpectAndReturn(person,3);
  // mapStore(map, person, comparePerson, hash);
  map->bucket[3] = list;
  
  hash_ExpectAndReturn(person2,3);
	comparePerson_ExpectAndReturn(person1, person2, 0);
  
	Try{
		mapStore(map, person2, comparePerson, hash);
    TEST_ASSERT_NOT_NULL(map->bucket[3]);
    TEST_ASSERT_EQUAL_Person(person2, getPersonFromBucket(map->bucket[3]));
    TEST_ASSERT_EQUAL_Person(person1, getPersonFromBucket(( (List *) map->bucket[3] )->next) );
	}
	Catch(e){
    TEST_FAIL_MESSAGE("Expect not to throw exception but thrown.");
	}
}
Ejemplo n.º 3
0
List * intRangeList(int f, int t) {
 List * l = listNew(sizeof f, &f), * e = l;
 for (int i = f + 1; i <= t; i ++) { e = e->nx = listNew(sizeof i, &i); }
 return l;
}
Ejemplo n.º 4
0
List * listAppend(List * l, int sz, void * val) {
 while (l->nx) { l = l->nx; } l->nx = listNew(sz, val); return l;
}
Ejemplo n.º 5
0
// pass number of terminals on commandline
int main(int argc, char *argv[]) {
	struct virtualTerminal *vtPointer;

	// nCurses related variables
	WINDOW *ncursesScreen;
	int screenWidth, screenHeight, vtColumns, vtRows; // to hold screen's height and width

	// Miscellaneous variables
	unsigned int input;
	char ch;
	int in;
	char inputString[33];
	int i, j, k, done, fdMax, fdCurrent, inputSize;
	int *vtCoordinates, *vtCoordPointer;
	fd_set fileDescriptors;
	struct timespec timeout;
	struct sigaction sa;
	linkedList *llTerminals;
	linkedListNode *llNode, *llCurrent;

	//mtrace();
	// Set up signal handling
	memset(&sa, 0, sizeof(sa));
	sa.sa_handler = &signalHandler;
	sigaction(SIGTERM, &sa, NULL);

	// Initializations
	llTerminals = listNew();
	done = fdMax = 0;
	timeout.tv_sec = 0;
	timeout.tv_nsec = 500000000; // 10E-9

	// Initialize terminal
	ncursesScreen = initscr();
	if(ncursesScreen == NULL) {
		perror("Error initializing nCurses\n");
		exit(1);
	}

	if(has_colors()) {
		start_color();
		init_pair(1, COLOR_WHITE, COLOR_BLACK);
		init_pair(2, COLOR_BLACK, COLOR_WHITE);
		init_pair(3, COLOR_RED, COLOR_BLACK);
		init_pair(4, COLOR_BLUE, COLOR_BLACK);
		init_pair(5, COLOR_WHITE, COLOR_BLACK);
	}

	//keypad(ncursesScreen, true); // cause nCurses to package key combos into special, single values
	//raw();
	noecho(); // don't echo input
	refresh(); // clear the main window

	// Get main window dimensions. This will be used when creating additional virtual terminals
	getmaxyx(ncursesScreen, screenHeight, screenWidth);
#ifdef DEBUG
	fprintf(stderr, "Height %d Width %d\n", screenHeight, screenWidth);
#endif
	//vtRows = (vtRows / 2); // - (vtRows % 4);
	//vtCols = (vtCols / 2); // - (vtCols % 4);

	//fprintf(stderr, "Rows: %d Cols: %d\n", vtRows, vtCols);

	// set j equal to number of terminals
	if(argc > 1)
		j = atoi(argv[1]);
	else
		j = 1;

	vtPointer = vtCreate(screenWidth, 2, 0, screenHeight - 2, 0); // pass width, height, x, y, number
	listAppendCargo(llTerminals, vtPointer);

	screenHeight -= 2;

	vtCoordPointer = vtCoordinates = divideRectangle_favorHeight(screenWidth, screenHeight, j, 0);
	for(i = 0; i < j; i++) {
		vtPointer = vtCreate(*(vtCoordPointer + 2), *(vtCoordPointer + 3), *vtCoordPointer, *(vtCoordPointer + 1), i + 1); // pass width, height, x, y, number
		if(vtPointer == NULL)
			break;
		listAppendCargo(llTerminals, vtPointer);
		vtCoordPointer += 4;
	}

	llCurrent = llNode = listFirst(llTerminals);
	vtPointer = (struct virtualTerminal*) nodeCargo(llNode);
	fdCurrent = vtPointer->fd;
	fprintf(stderr, "fd %d\n",fdCurrent);
	k = 0;

	while(!done) {
		FD_ZERO(&fileDescriptors);
		FD_SET(0, &fileDescriptors);
		fdMax = 0;
		llNode = listFirst(llTerminals);
		while(llNode != NULL) {
			vtPointer = (struct virtualTerminal*) nodeCargo(llNode);
			if(vtPointer->fd > 0 && vtPointer->state != VT_PAUSED) { // fd will be -1 if shell program has exited
				j = vtPointer->fd;
				if(j > fdMax)
					fdMax = j;
				FD_SET(j, &fileDescriptors);
			}
			llNode = listNext(llNode);
		}

		pselect(fdMax + 1, &fileDescriptors, NULL, NULL, &timeout, NULL);

		// process keyboard input, if any
		if(FD_ISSET(0, &fileDescriptors)) {
			//inputSize = read(0, &ch, 1);
			in = getch();
			switch(in) { //inputString[i]) {
				case '`': // tab
					vtPointer = (struct virtualTerminal*) nodeCargo(llCurrent);
					if(vtPointer->wBorder) {
						wattron(vtPointer->wBorder, COLOR_PAIR(1));
						box(vtPointer->wBorder, ACS_VLINE, ACS_HLINE);
						mvwprintw(vtPointer->wBorder, 0, 0, "%i ", vtPointer->id);
						wnoutrefresh(vtPointer->wBorder);
					}

					llCurrent = listNext(llCurrent);
					if(llCurrent == NULL)
						llCurrent = listFirst(llTerminals);
					vtPointer = (struct virtualTerminal*) nodeCargo(llCurrent);
					if(vtPointer->wBorder) {
						wattron(vtPointer->wBorder, COLOR_PAIR(3));
						box(vtPointer->wBorder, ACS_VLINE, ACS_HLINE);
						mvwprintw(vtPointer->wBorder, 0, 0, "%i ", vtPointer->id);
						wattroff(vtPointer->wBorder, COLOR_PAIR(3));
						wnoutrefresh(vtPointer->wBorder);
					}
					doupdate();

					//vtPointer = (struct virtualTerminal*) to->pointer;
					//wcolor_set(vtPointer->window, 1, NULL);
					k++;
					if(k == llTerminals->length)
						k = 0;
					fdCurrent = vtPointer->fd;
					break;

				case '~':
					done = 1;
					break;
				/*
				case KEY_ENTER:
					ch = '\n';
					write(fdCurrent, &ch, 1);
					break;
				*/

				default:
					ch = (char)in;

					write(fdCurrent, &ch, 1);
fprintf(stderr, "wrote: %c\n", ch);
					break;
			}
		}

		j = 0; // temporarily keep track of whether any windows were updated
		llNode = listFirst(llTerminals);
		while(llNode != NULL) {
			vtPointer = (struct virtualTerminal*) nodeCargo(llNode);
			if(vtPointer->fd > 0 && FD_ISSET(vtPointer->fd, &fileDescriptors) && vtPointer->state != VT_PAUSED) { // fd will be -1 if shell has closed
				//tesi_handleInput(to); // handle escape sequences
				VTCore_dispatch(vtPointer->core);

				//if(vtPointer->wBorder)
					//wnoutrefresh(vtPointer->wBorder); // updates backbuffered nCurses window
				wnoutrefresh(vtPointer->window); // updates backbuffered nCurses window
				j = 1; // specifies whether we should flush nCurses buffered windows
			}
			llNode = listNext(llNode);
		}
		if(j)
			doupdate(); // updates were made, wnoutrefresh() was called, now flush updates

		switch(previousSignal) {
			case SIGTERM:
				fprintf(stderr, "Caught SIGTERM\n");
				done = 1;
				break;
			default:
				break;
		}
	}

	//strcpy(inputString, "\nexit\n");
	/*
	llNode = listFirst(llTerminals);
	while(llNode != NULL) {
		vtPointer = (struct virtualTerminal*) nodeCargo(llNode);

		kill(vtPointer->pid, 9);
		//write(to->pipeToChild[1], &inputString, strlen(inputString)); // send exit to bash
		waitpid(vtPointer->pid, NULL, 0); // bash should have quit by the time it gets here

		vtDestroy(vtPointer);

		llNode = listNext(llNode);
	}
	* */

	free(vtCoordinates);
	listDelete(llTerminals, vtDestroy);

	endwin();

	return 0;
}
Ejemplo n.º 6
0
// pass number of terminals on commandline
int main(int argc, char *argv[]) {
	// Virtual Terminal related
	struct virtualTerminal *vt;

	// nCurses related variables
	WINDOW *ncursesScreen;
	int screenWidth, screenHeight, vtColumns, vtRows; // to hold screen's height and width

	// Miscellaneous variables
	unsigned int input;
	char ch;
	char inputString[33];
	int i, j, k, done, fdMax, fdCurrent, inputSize;
	int *vtCoordinates, *vtPointer;
	fd_set fileDescriptors;
	struct timespec timeout;
	struct sigaction sa;
	linkedList *llTerminals;
	linkedListNode *llNode;

	//mtrace();
	// Set up signal handling
	memset(&sa, 0, sizeof(sa));
	sa.sa_handler = &signalHandler;
	sigaction(SIGTERM, &sa, NULL);

	// Initializations
	llTerminals = listNew();
	done = fdMax = 0;
	timeout.tv_sec = 0;
	timeout.tv_nsec = 500000000; // 10E-9

	// Initialize terminal
	ncursesScreen = initscr();
	if(ncursesScreen == NULL) {
		perror("Error initializing nCurses\n");
		exit(1);
	}

	if(has_colors()) {
		start_color();
	}

	keypad(ncursesScreen, true); // cause nCurses to package key combos into special, single values
	noecho(); // don't echo input
	refresh(); // clear the main window

	// Get main window dimensions. This will be used when creating additional virtual terminals
	getmaxyx(ncursesScreen, screenHeight, screenWidth);
	//vtRows = (vtRows / 2); // - (vtRows % 4);
	//vtCols = (vtCols / 2); // - (vtCols % 4);

	//fprintf(stderr, "Rows: %d Cols: %d\n", vtRows, vtCols);

	// set j equal to number of terminals
	if(argc > 1)
		j = atoi(argv[1]);
	else
		j = 1;


	//vtPointer = vtCoordinates = rectangleCoordinates_favorWidth(screenWidth, screenHeight, j, 1);
	vtPointer = vtCoordinates = rectangleCoordinates_favorHeight(screenWidth, screenHeight, j, 0);
	for(i = 0; i < j; i++) {
		vt = vtCreate( *(vtPointer + 2), *(vtPointer + 3), *vtPointer, *(vtPointer + 1), i);
		listAppendCargo(llTerminals, vt);
		vtPointer += 4;
	}

	llNode = listFirst(llTerminals);
	vt = (struct virtualTerminal*) nodeCargo(llNode);
	fdCurrent = vt->fd;
	//fprintf(stderr, "fd %d\n",fdCurrent);
	k = 0;

	while(!done) {
		FD_ZERO(&fileDescriptors);
		FD_SET(0, &fileDescriptors);
		llNode = listFirst(llTerminals);
		fdMax = 0;
		while(llNode != NULL) {
			vt = (struct virtualTerminal*) nodeCargo(llNode);
			if(vt->fd > 0 && vt->state != VT_PAUSED) { // fd will be -1 if shell program has exited
				j = vt->fd;
				if(j > fdMax)
					fdMax = j;
				FD_SET(j, &fileDescriptors);
			}
			llNode = listNext(llNode);
		}

		pselect(fdMax + 1, &fileDescriptors, NULL, NULL, &timeout, NULL); 

		if(FD_ISSET(0, &fileDescriptors)) {
			inputSize = read(0, &ch, 1);
			//for(i = 0; i < inputSize; i++) {
				switch(ch) { //inputString[i]) {
			
					case '`': // tab
						wcolor_set(((struct virtualTerminal*)listNodeIndex(llTerminals ,k))->window, 1, NULL);
						k++;
						if(k == llTerminals->length)
							k = 0;
						fdCurrent = TtyTerminalIO_get_associated_fd( ((struct virtualTerminal*)listNodeIndex(llTerminals ,k))->terminalIO );
						break;

					case '~':
						done = 1;
						break;

					default:
						//ch = inputString[i];

						write(fdCurrent, &ch, 1);
						break;
				}
			//}
		}

		j = 0; // temporarily keep track of whether any windows were updated
		llNode = listFirst(llTerminals);
		while(llNode != NULL) {
			vt = (struct virtualTerminal*) nodeCargo(llNode);
			if(vt->fd > 0 && FD_ISSET(vt->fd, &fileDescriptors) && vt->state != VT_PAUSED) { // fd will be -1 if shell has closed
				VTCore_dispatch(vt->core);
			
				wnoutrefresh(vt->wBorder); // updates backbuffered nCurses window
				wnoutrefresh(vt->window); // updates backbuffered nCurses window
				j = 1; // specifies whether we should flush nCurses buffered windows
			}
			llNode = listNext(llNode);
		}
		if(j)
			doupdate(); // updates were made, wnoutrefresh() was called, now flush updates

		switch(previousSignal) {
			case SIGTERM:
				fprintf(stderr, "Caught SIGTERM\n");
				done = 1;
				break;
			default:
				break;
		}
	}

	llNode = listFirst(llTerminals);
	while(llNode != NULL) {
		vt = (struct virtualTerminal*) nodeCargo(llNode);
		//fprintf(stderr,"Terminal %d\n", vt->id);
		llNode = listNext(llNode);
	}

	free(vtCoordinates);
	listDelete(llTerminals, vtDestroy);
	endwin();

	return 0;
}
Ejemplo n.º 7
0
Route* routeCalculate(int sid1, int sid2, List* stations, List* links)
{
    Station** stationsArray = (Station**)stations->data;
    double* map = (double*)calloc(stations->count, sizeof(double));

    for (int i = 0; i < stations->count; i++) 
    {
        map[i] = -1;
    }

    int found = false;
    int step = false;
    List* currentStations = listNew();
    List* nextStations = listNew();
    listAdd(currentStations, stationsArray[sid2]);
    map[sid2] = 0;


    while (true)
    {
        listClear(nextStations);
        step = false;
        for (int j = 0; j < currentStations->count; j++)
        {
            Station* current = (Station*)currentStations->data[j];

            for (int l = 0; l < current->srtLids->count; l++)
            {
                Link* link = (Link*)current->srtLids->data[l];
                _stepBack(current, link, link->sid2, sid1, map, stationsArray, nextStations, &step, &found);
            }

            for (int l = 0; l < current->endLids->count; l++)
            {
                Link* link = (Link*)current->endLids->data[l];
                _stepBack(current, link, link->sid1, sid1, map, stationsArray, nextStations, &step, &found);
            }
        }
        if (!found && !step) break;

        if (found && nextStations->count == 0) break;
        listClear(currentStations);
        for (int l = 0; l < nextStations->count; l++)
        {
            listAdd(currentStations, nextStations->data[l]);
        }
    }

    listFree(currentStations);
    listFree(nextStations);

    Route* result = NULL;

    if (found)
    {
        found = false;
        Link* nextLink = NULL;
        Station* nextStation = stationsArray[sid1];
        double minval = map[sid1];

        while (!found)
        {
            Station* cursid = nextStation;
            step = false;

            for (int l = 0; l < cursid->srtLids->count; l++)
            {
                Link* link = (Link*)cursid->srtLids->data[l];
                _stepForward(cursid, link, link->sid2, sid2, map, stationsArray, &nextStation, &nextLink, &minval, &step, &found);
            }

            for (int l = 0; l < cursid->endLids->count; l++)
            {
                Link* link = (Link*)cursid->endLids->data[l];
                _stepForward(cursid, link, link->sid1, sid2, map, stationsArray, &nextStation, &nextLink, &minval, &step, &found);
            }

            if (!found && !step) return NULL;

            if (result == NULL)
            { 
                result = _routeNew(stationsArray[sid1]);
            }
            
            result->length += nextLink->len;
            listAdd(result->stations, nextStation);
            listAdd(result->links, nextLink);

            if (found) return result;
        }
    
    }

    routeFree(&result);
    free(map);

    return NULL;
}
Ejemplo n.º 8
0
Graph *graphNew()
{
	return listNew();
}