void
AdaptiveStateManagerBaseTest::testFossilCollect() {
    // Create the same state queue as above.
    ASMBTestClass* toCheck = dynamic_cast<ASMBTestClass*>(toTest);
    toCheck->setPeriod(getSimObj1(), 3);
    defaultSaveState();

    // Call fossilCollect.
    CPPUNIT_ASSERT(toTest->fossilCollect(getSimObj1(), IntVTime(7)) == IntVTime(5));

    // Check the size of the state queue. May also want to check the entire queue.
    multiset< SetObject<State> >* stateQueue = toCheck->getStateQueue();

    CPPUNIT_ASSERT(stateQueue[ getSimObj1()->getObjectID()->getSimulationObjectID() ].size() == 2);

    multiset< SetObject<State> >::iterator iter_begin =
        stateQueue[ getSimObj1()->getObjectID()->getSimulationObjectID() ].begin();

    const UnitTestState* checkState = dynamic_cast<const UnitTestState*>((*iter_begin).getElement());
    IntVTime time = atoi((*iter_begin).getMainTime().toString().c_str());
    CPPUNIT_ASSERT(time == 5);
    CPPUNIT_ASSERT(checkState->intMember == 50);

    iter_begin++;

    checkState = dynamic_cast<const UnitTestState*>((*iter_begin).getElement());
    time = atoi((*iter_begin).getMainTime().toString().c_str());
    CPPUNIT_ASSERT(time == 9);
    CPPUNIT_ASSERT(checkState->intMember == 90);
}
void
AdaptiveStateManagerBaseTest::testRestoreState() {
    // Create the same state queue as above.
    ASMBTestClass* toCheck = dynamic_cast<ASMBTestClass*>(toTest);
    toCheck->setPeriod(getSimObj1(), 3);
    defaultSaveState();

    // Call restoreState.
    toTest->restoreState(IntVTime(7), getSimObj1());

    // Check the current state of the object
    UnitTestState* testState = dynamic_cast<UnitTestState*>(getSimObj1()->getState());
    CPPUNIT_ASSERT(testState->intMember == 50);

    // May also want to check the state queue and see if its last element matches the current state.
    // This includes checking the size.
    multiset< SetObject<State> >* stateQueue = toCheck->getStateQueue();

    multiset< SetObject<State> >::iterator iter_end =
        stateQueue[ getSimObj1()->getObjectID()->getSimulationObjectID() ].end();

    iter_end--;

    const UnitTestState* checkState = dynamic_cast<const UnitTestState*>((*iter_end).getElement());
    IntVTime time = atoi((*iter_end).getMainTime().toString().c_str());
    CPPUNIT_ASSERT(time == 5);
    CPPUNIT_ASSERT(checkState->intMember == 50);
}
void
AdaptiveStateManagerBaseTest::testSaveState() {
    // There should be saved states every 4th state.
    ASMBTestClass* toCheck = dynamic_cast<ASMBTestClass*>(toTest);
    toCheck->setPeriod(getSimObj1(), 3);
    defaultSaveState();

    // Go through the state queue. The key is the object. There should then be a queue of
    // pairs SetObject. The pair is (time, state). Examine the time and state. In this case
    // the state should be 10*time.

    multiset< SetObject<State> >* stateQueue = toCheck->getStateQueue();

    multiset< SetObject<State> >::iterator iter_begin =
        stateQueue[ getSimObj1()->getObjectID()->getSimulationObjectID() ].begin();

    multiset< SetObject<State> >::iterator iter_end =
        stateQueue[ getSimObj1()->getObjectID()->getSimulationObjectID() ].end();

    int check = 1;
    while (iter_begin != iter_end) {
        const UnitTestState* checkState = dynamic_cast<const UnitTestState*>((*iter_begin).getElement());
        CPPUNIT_ASSERT(checkState->intMember == check * 10);
        IntVTime time = atoi((*iter_begin).getMainTime().toString().c_str());
        CPPUNIT_ASSERT(time == check);
        check += 4;
        iter_begin++;
    }
}
void
AdaptiveStateManagerBaseTest::defaultSaveState() {
    for (int a = 1; a <= 10; a++) {
        UnitTestState* testState = dynamic_cast<UnitTestState*>(getSimObj1()->getState());
        testState->intMember = a * 10;
        toTest->saveState(IntVTime(a), getSimObj1());
    }
}
void
CostAdaptiveStateManagerTest::testSaveState() {
    // The actual saving of the state is tested in AdaptiveStateManagerBaseTest.
    // Just check that the forward execution length is being incremented and reset
    // when the period is recalculated.
    CASMTestClass* toCheck = dynamic_cast<CASMTestClass*>(toTest);
    toCheck->setAdaptiveParameters(0, 30);

    for (int a = 1; a <= 20; a++) {
        toTest->saveState(IntVTime(a), getSimObj1());
    }

    CPPUNIT_ASSERT(toCheck->getForwardExecutionLength()[0] == 20);

    for (int b = 21; b <= 40; b++) {
        toTest->saveState(IntVTime(b), getSimObj1());
    }

    CPPUNIT_ASSERT(toCheck->getForwardExecutionLength()[0] == 10);
}
void
SchedulingManagerTest::testGetNextEvent( SchedulingManager *toTest ) {
    CPPUNIT_ASSERT( toTest != 0 );
    doDefaultInsert( getSimManager()->getEventSetManager() );
    const Event *event1 = toTest->peekNextEvent();
    CPPUNIT_ASSERT( event1 != 0 );
    CPPUNIT_ASSERT( toTest->getLastEventScheduledTime() == IntVTime(0) );
    CPPUNIT_ASSERT( event1 == getSimManager()->getEventSetManager()->getEvent( getSimObj1() ) );
    const Event *event2 = toTest->peekNextEvent();
    CPPUNIT_ASSERT( event2 != 0 );
    CPPUNIT_ASSERT( toTest->getLastEventScheduledTime() == IntVTime(0) );
    CPPUNIT_ASSERT( event2 == getSimManager()->getEventSetManager()->getEvent( getSimObj1() ) );
    const Event *event3 = toTest->peekNextEvent();
    CPPUNIT_ASSERT( event3 != 0 );
    CPPUNIT_ASSERT( toTest->getLastEventScheduledTime() == IntVTime(0) );
    CPPUNIT_ASSERT( event3 == getSimManager()->getEventSetManager()->getEvent( getSimObj1() ) );
    const Event *event4 = toTest->peekNextEvent();
    CPPUNIT_ASSERT( event4 != 0 );
    CPPUNIT_ASSERT( toTest->getLastEventScheduledTime() == IntVTime(1) );
    CPPUNIT_ASSERT( event4 == getSimManager()->getEventSetManager()->getEvent( getSimObj1() ) );

    checkDefaultEvents( event1, event2, event3, event4 );
}
void
LazyOutputManagerTest::testRollback(){
  CPPUNIT_ASSERT( toTest != 0 );
  LOMSimMgr *simMgr = dynamic_cast<LOMSimMgr *>(getSimManager());
  CPPUNIT_ASSERT( simMgr != 0 );
  CPPUNIT_ASSERT( simMgr->negativeCount() == 0 );

  doDefaultInsert( toTest );
  toTest->rollback( getSimObj1(), IntVTime(1) );
  CPPUNIT_ASSERT( simMgr->negativeCount() == 0 );
  CPPUNIT_ASSERT( getNumElements( toTest, *(getSimObj1()->getObjectID()) ) == 4 );
  CPPUNIT_ASSERT( getNumLazyCancelEvents( toTest, *(getSimObj1()->getObjectID()) ) == 0 );
  
  toTest->rollback( getSimObj1(), IntVTime(0) );
  CPPUNIT_ASSERT( simMgr->negativeCount() == 0 );
  CPPUNIT_ASSERT( getNumElements( toTest, *(getSimObj1()->getObjectID()) ) == 0 );
  CPPUNIT_ASSERT( getNumLazyCancelEvents( toTest, *(getSimObj1()->getObjectID()) ) == 4 );
}
void
CostAdaptiveStateManagerTest::testCalculatePeriod() {

    // Case 1: The first time the period is calculated, it is increased by
    //         1 because the oldCostIndex is initially 0.
    //         filterCostIndex becomes 4.80
    CASMTestClass* toCheck = dynamic_cast<CASMTestClass*>(toTest);
    toCheck->StateSaveTimeWeighted[0].update(10);
    toCheck->CoastForwardTimeWeighted[0].update(10);
    toCheck->calculatePeriod(getSimObj1());
    CPPUNIT_ASSERT(toCheck->getObjectStatePeriod()[0] == 1);

    // Case 2: Period remains the same.
    //         filterCostIndex becomes 4.32
    toCheck->StateSaveTimeWeighted[0].update(5);
    toCheck->CoastForwardTimeWeighted[0].update(5);
    toCheck->calculatePeriod(getSimObj1());
    CPPUNIT_ASSERT(toCheck->getObjectStatePeriod()[0] == 1);

    // Case 3: Period increases by 1.
    //         filteredCostIndex becomes 1.78
    toCheck->StateSaveTimeWeighted[0].update(0.1);
    toCheck->CoastForwardTimeWeighted[0].update(0.1);
    toCheck->calculatePeriod(getSimObj1());
    CPPUNIT_ASSERT(toCheck->getObjectStatePeriod()[0] == 2);

    // Case 4: Period decreases by 1.
    //         filteredCostIndex becomes 5.51
    toCheck->StateSaveTimeWeighted[0].update(10);
    toCheck->CoastForwardTimeWeighted[0].update(10);
    toCheck->calculatePeriod(getSimObj1());
    CPPUNIT_ASSERT(toCheck->getObjectStatePeriod()[0] == 1);

    // Case 5: Period is at the maximum. An attempt to increase it should keep it at maximum
    toCheck->setPeriod(getSimObj1(), maxDefaultInterval);
    toCheck->StateSaveTimeWeighted[0].update(0.1);
    toCheck->CoastForwardTimeWeighted[0].update(0.1);
    toCheck->calculatePeriod(getSimObj1());
    CPPUNIT_ASSERT(toCheck->getObjectStatePeriod()[0] == maxDefaultInterval - 1);
}
void
LazyOutputManagerTest::testLazyCancel(){
  LazyOutputManager *toCheck = dynamic_cast<LazyOutputManager *>( toTest );
  CPPUNIT_ASSERT( toTest != 0 );
  LOMSimMgr *simMgr = dynamic_cast<LOMSimMgr *>(getSimManager());
  CPPUNIT_ASSERT( simMgr != 0 );
  CPPUNIT_ASSERT( simMgr->negativeCount() == 0 );
  CPPUNIT_ASSERT( getNumElements( toTest, *(getSimObj1()->getObjectID()) ) == 0);

  //Insert events into the output manager.
  toTest->insert(new UnitTestEvent( *new IntVTime( 1 ), getSimObj1ID(), *new IntVTime( 0 ) ));
  toTest->insert(new UnitTestEvent( *new IntVTime( 1 ), getSimObj1ID(), *new IntVTime( 0 ) ));
  toTest->insert(new UnitTestEvent( *new IntVTime( 1 ), getSimObj1ID(), *new IntVTime( 0 ) ));
  toTest->insert(new UnitTestEvent( *new IntVTime( 2 ), getSimObj1ID(), *new IntVTime( 1 ) ));
  toTest->insert(new UnitTestEvent( *new IntVTime( 3 ), getSimObj1ID(), *new IntVTime( 2 ) ));
  toTest->insert(new UnitTestEvent( *new IntVTime( 3 ), getSimObj1ID(), *new IntVTime( 2 ) ));
  toTest->insert(new UnitTestEvent( *new IntVTime( 4 ), getSimObj1ID(), *new IntVTime( 3 ) ));
  toTest->insert(new UnitTestEvent( *new IntVTime( 4 ), getSimObj1ID(), *new IntVTime( 3 ) ));
  toTest->insert(new UnitTestEvent( *new IntVTime( 4 ), getSimObj1ID(), *new IntVTime( 3 ) ));
  toTest->insert(new UnitTestEvent( *new IntVTime( 5 ), getSimObj1ID(), *new IntVTime( 4 ) ));
  toTest->insert(new UnitTestEvent( *new IntVTime( 6 ), getSimObj1ID(), *new IntVTime( 5 ) ));

  //Rollback the output manager and check the lazy queue.
  toTest->rollback( getSimObj1(), IntVTime(2) );
  CPPUNIT_ASSERT( simMgr->negativeCount() == 0 );
  CPPUNIT_ASSERT( getNumElements( toTest, *(getSimObj1()->getObjectID()) ) == 4 );
  CPPUNIT_ASSERT( getNumLazyCancelEvents( toTest, *(getSimObj1()->getObjectID()) ) == 7 );

  //Test the case where all events are regenerated.
  CPPUNIT_ASSERT( toCheck->lazyCancel(new UnitTestEvent( *new IntVTime( 3 ), getSimObj1ID(), *new IntVTime( 2 ) )));
  CPPUNIT_ASSERT( toCheck->lazyCancel(new UnitTestEvent( *new IntVTime( 3 ), getSimObj1ID(), *new IntVTime( 2 ) )));
  CPPUNIT_ASSERT( toCheck->lazyCancel(new UnitTestEvent( *new IntVTime( 4 ), getSimObj1ID(), *new IntVTime( 3 ) )));
  CPPUNIT_ASSERT( toCheck->lazyCancel(new UnitTestEvent( *new IntVTime( 4 ), getSimObj1ID(), *new IntVTime( 3 ) )));
  CPPUNIT_ASSERT( toCheck->lazyCancel(new UnitTestEvent( *new IntVTime( 4 ), getSimObj1ID(), *new IntVTime( 3 ) )));
  CPPUNIT_ASSERT( toCheck->lazyCancel(new UnitTestEvent( *new IntVTime( 5 ), getSimObj1ID(), *new IntVTime( 4 ) )));
  CPPUNIT_ASSERT( toCheck->lazyCancel(new UnitTestEvent( *new IntVTime( 6 ), getSimObj1ID(), *new IntVTime( 5 ) )));
  CPPUNIT_ASSERT( getNumElements( toTest, *(getSimObj1()->getObjectID()) ) == 11 );
  CPPUNIT_ASSERT( getNumLazyCancelEvents( toTest, *(getSimObj1()->getObjectID()) ) == 0 );
  CPPUNIT_ASSERT( simMgr->negativeCount() == 0 );
  toCheck->lazyCancel(new UnitTestEvent( *new IntVTime( 7 ), getSimObj1ID(), *new IntVTime( 6 ) ));

  //Test the case where some events are not regenerated.
  toTest->rollback( getSimObj1(), IntVTime(2) );
  CPPUNIT_ASSERT( toCheck->lazyCancel(new UnitTestEvent( *new IntVTime( 4 ), getSimObj1ID(), *new IntVTime( 3 ) )));
  CPPUNIT_ASSERT( getNumElements( toTest, *(getSimObj1()->getObjectID()) ) == 5 );
  CPPUNIT_ASSERT( getNumLazyCancelEvents( toTest, *(getSimObj1()->getObjectID()) ) == 5 );
  CPPUNIT_ASSERT( simMgr->negativeCount() == 2 );

  CPPUNIT_ASSERT( !toCheck->lazyCancel(new UnitTestEvent( *new IntVTime( 8 ), getSimObj1ID(), *new IntVTime( 7 ) )));
  CPPUNIT_ASSERT( getNumElements( toTest, *(getSimObj1()->getObjectID()) ) == 6 );
  CPPUNIT_ASSERT( getNumLazyCancelEvents( toTest, *(getSimObj1()->getObjectID()) ) == 0 );
  CPPUNIT_ASSERT( simMgr->negativeCount() == 7 );
}