void TrafficLightTest::testCollaboratorHasColour() {
  char name1[] = "Traffic Light 1";
  char name2[] = "Traffic Light 2";
  Time delay(1,2,3);
  TrafficLight tl1(name1, delay);
  TrafficLight tl2(name2, delay);
  tl1.setCollaborator(tl2);
  
  assert(!tl1.collaboratorHasColour(TrafficLight::GREEN));
  assert(!tl1.collaboratorHasColour(TrafficLight::YELLOW));
  assert(tl1.collaboratorHasColour(TrafficLight::RED));

  //unaffected by the colour of the light itself
  tl1.colour = TrafficLight::GREEN;
  assert(!tl1.collaboratorHasColour(TrafficLight::GREEN));
  assert(!tl1.collaboratorHasColour(TrafficLight::YELLOW));
  assert(tl1.collaboratorHasColour(TrafficLight::RED));
  
  //instead it is the collaborator's colour we track
  tl2.colour = TrafficLight::YELLOW;
  assert(!tl1.collaboratorHasColour(TrafficLight::GREEN));
  assert(tl1.collaboratorHasColour(TrafficLight::YELLOW));
  assert(!tl1.collaboratorHasColour(TrafficLight::RED));


  cout << __func__ << " passed" << endl;
}
void TrafficLightTest::testSetCollaborator() {
  char name1[] = "Traffic Light 1";
  char name2[] = "Traffic Light 2";
  Time delay(1,2,3);
  TrafficLight tl1(name1, delay);
  TrafficLight tl2(name2, delay);
  tl1.setCollaborator(tl2);
  assert(strcmp(tl1.collaborating_light->name, name2) == 0);
  assert(tl1.collaborating_light == &tl2);
  assert(strcmp(tl2.collaborating_light->name, name1) == 0);
  assert(tl2.collaborating_light == &tl1);
  cout << __func__ << " passed" << endl;
}
Exemplo n.º 3
0
void testObj::test<3>(void)
{
  double     data[2]={1, 2};
  Mutex      mutex;
  TestLocker tl1(&mutex, data);
  TestLocker tl2(&mutex, data);

  // start two threads
  Base::Threads::ThreadJoiner th1(tl1);
  Base::Threads::ThreadJoiner th2(tl2);
  // and join them
  th1->join();
  th2->join();
}
void TrafficLightTest::testRequestCollaboratorTurns() {
  char name1[] = "Traffic Light 1";
  char name2[] = "Traffic Light 2";
  Time delay(1,2,3);
  TrafficLight tl1(name1, delay);
  TrafficLight tl2(name2, delay);
  tl1.setCollaborator(tl2);
  tl1.turn(TrafficLight::GREEN);
  //current is red request collaborator turns green
  
  tl1.requestCollaboratorTurn(TrafficLight::GREEN);
  assert(tl2.hasColour(TrafficLight::GREEN));
  assert(tl1.hasColour(TrafficLight::RED));

  //turn it green from yellow
  cout << __func__ << " passed" << endl;
}
void TrafficLightTest::testRequestedToTurn() {
  char name1[] = "Traffic Light 1";
  char name2[] = "Traffic Light 2";
  Time delay(1,2,3);
  TrafficLight tl1(name1, delay);
  TrafficLight tl2(name2, delay);
  tl1.setCollaborator(tl2);
  cout << __func__ << " passed" << endl;

  //request it to turn green from red
  //must first turn collaborator green
  tl2.turn(TrafficLight::GREEN);
  tl1.requestedToTurn(TrafficLight::GREEN);
  assert(tl1.hasColour(TrafficLight::GREEN));
  assert(tl2.hasColour(TrafficLight::RED));

  //request it to turn red from green
  tl1.requestedToTurn(TrafficLight::RED);
  assert(tl1.hasColour(TrafficLight::RED));
  assert(tl2.hasColour(TrafficLight::GREEN));
}