Пример #1
0
void advance_trains(struct station *kipling, struct train **first)
{
	// Advance every train on the track (if it can be advanced)
	// PRE: *first != NULL and there is more than one train on the track
	// POST: the trains' positions will be incremented
	assert(*first);
	assert((*first)->next);

	struct train *curr = *first;
	struct train *prev = NULL;
	while(curr)
	{
		if(curr->pos >= MAX_POS + 1)
		{
			// delete the train when it gets to position 83
			prev->next = NULL;
			free(curr);
			return;
		}
		else
		{
			// otherwise, the train is on the track and we should advance it
			int num_pass = 0;
			struct station *curr_station = get_station_at_pos(curr->pos, kipling);	
			if(curr_station)
			{
				num_pass = num_passengers(curr_station->passengers);
			}

			advance_train(curr, num_pass);

			prev = curr;
			curr = curr->next;
		}
	}
}
Пример #2
0
static void advance_train_test() {
    // absent
    check_expect_train(__LINE__, advance_train(make_train_none(), 3.0),
                          make_train_none());
 
    // borders
    check_expect_train(__LINE__, advance_train(make_train_at(0.0), 0.0),
                          make_train_at(0.0));
    check_expect_train(__LINE__, advance_train(make_train_at(10.0), 0.0),
                          make_train_at(10.0));
    check_expect_train(__LINE__, advance_train(make_train_at(1.0), -1.0),
                          make_train_at(0.0));
    check_expect_train(__LINE__, advance_train(make_train_at(9.0), 1.0),
                          make_train_at(10.0));
 
    // interior (both before and after advance)
    check_expect_train(__LINE__, advance_train(make_train_at(1.0), 0.0),
                          make_train_at(1.0));
    check_expect_train(__LINE__, advance_train(make_train_at(5.5), 1.5),
                          make_train_at(7.0));
    check_expect_train(__LINE__, advance_train(make_train_at(4.5), -1.0),
                          make_train_at(3.5));
 
    // leaving the section
    check_expect_train(__LINE__, advance_train(make_train_at(9.0), 1.1),
                          make_train_none());
    check_expect_train(__LINE__, advance_train(make_train_at(1.0), -1.1),
                          make_train_none());
 
    // entering the section
    check_expect_train(__LINE__, advance_train(make_train_at(-0.1), 0.1),
                          make_train_at(0.0));
    check_expect_train(__LINE__, advance_train(make_train_at(10.1), -0.1),
                          make_train_at(10.0));
}