예제 #1
0
GEP_UNITTEST_TEST(StateMachine, INSERT_TEST_NAME_HERE)
{
    GEP_UNITTEST_SETUP_EVENT_GLOBALS;
    auto& logging =
        TestLogging::instance();
    //DummyLogging::instance();

    // set up update framework
    _updateFramework.setElapsedTime(10.0f);

    // Add at least one callback so we know that we are still updating
    size_t frameCount = 0;
    _updateFramework.registerUpdateCallback([&](float){ logging.logWarning("frame %u:", frameCount); });

    StateMachineFactory factory(&g_stdAllocator);

    StateMachine* pMainFsm = nullptr;

    // Test data.

    /// TODO Insert test data here ///////////////////////////////////////////

    // Actual tests. Don't put test data in here that has to be referenced
    // by the state machines, as they will get out of scope when pMainFsm
    // is actually run.
    {
        pMainFsm = factory.create("fsm");
        /// TODO Insert test code here ///////////////////////////////////////////
    }

    pMainFsm->setLogging(&logging);

    // Run the machine
    pMainFsm->run(_updateFramework);

    for(frameCount = 0; frameCount < 20; ++frameCount)
    {
        _updateFramework.run();
    }
}
예제 #2
0
GEP_UNITTEST_TEST(StateMachine, UpdateStepBehavior)
{
    GEP_UNITTEST_SETUP_EVENT_GLOBALS;
    auto& logging =
        TestLogging::instance();
    //DummyLogging::instance();

    // set up update framework
    _updateFramework.setElapsedTime(10.0f);

    // Add at least one callback so we know that we are still updating
    size_t frameCount = 0;
    _updateFramework.registerUpdateCallback([&](float){ logging.logWarning("frame %u:", frameCount); });

    StateMachineFactory factory(&g_stdAllocator);

    StateMachine* pMainFsm = nullptr;

    // Test data.

    size_t conditionCheckCount = 0;
    size_t updateCount = 0;

    // Actual tests. Don't put test data in here that has to be referenced
    // by the state machines, as they will get out of scope when pMainFsm
    // is actually run.
    {
        pMainFsm = factory.create("fsm");
        pMainFsm->setLogging(&logging);

        auto pState_A = pMainFsm->create<State>("A");
        auto pState_B = pMainFsm->create<State>("B");
        auto pState_C = pMainFsm->create<State>("C");

        pState_C->setLeaveCondition([](){ return true; });

        // Adding state transitions
        pMainFsm->addTransition("__enter", "A");
        pMainFsm->addTransition("A", "B", [&](){
            // This should be hit
            conditionCheckCount++;
            return true;
        });
        pMainFsm->addTransition("B", "C", [&](){
            // This should not be hit
            conditionCheckCount++;
            return true;
        });
        pMainFsm->addTransition("C", "__leave");

        pState_B->getLeaveEvent()->registerListener([&](State::LeaveEventData* pData){
            GEP_ASSERT(conditionCheckCount == 1, "Condition check 'A->B' not performed!");
            conditionCheckCount = 0;
            pData->setNextState(pState_C);
            return gep::EventResult::Handled;
        });

        pState_C->getLeaveEvent()->registerListener([&](State::LeaveEventData*){
            GEP_ASSERT(conditionCheckCount == 0, "Condition check 'B->C' should not have been performed!");
            conditionCheckCount = 0;
            return gep::EventResult::Handled;
        });

        // Set update event listeners. They should use the UpdateStepBehavior options.
        //////////////////////////////////////////////////////////////////////////

        pState_A->getUpdateEvent()->registerListener([&](State::UpdateEventData* pData){
            pData->setUpdateStepBehavior(State::UpdateStepBehavior::Leave);
            updateCount++;
            return gep::EventResult::Handled;
        });

        pState_B->getUpdateEvent()->registerListener([&](State::UpdateEventData* pData){
            pData->setUpdateStepBehavior(State::UpdateStepBehavior::LeaveWithNoConditionChecks);
            return gep::EventResult::Handled;
        });
    }

    pMainFsm->setLogging(&logging);

    // Run the machine
    pMainFsm->run(_updateFramework);

    for(frameCount = 0; frameCount < 5; ++frameCount)
    {
        _updateFramework.run();
    }

    GEP_ASSERT(updateCount > 0, "State A should have at least be updated once!");
}
예제 #3
0
GEP_UNITTEST_TEST(StateMachine, Basics)
{
    GEP_UNITTEST_SETUP_EVENT_GLOBALS;
    auto& logging =
        TestLogging::instance();
    //DummyLogging::instance();

    // set up update framework
    _updateFramework.setElapsedTime(10.0f);

    // Add at least one callback so we know that we are still updating
    size_t frameCount = 0;
    _updateFramework.registerUpdateCallback([&](float){ logging.logWarning("frame %u:", frameCount); });

    StateMachineFactory factory(&g_stdAllocator);

    StateMachine* pMainFsm = nullptr;

    // test data
    size_t testData_A = 0;
    size_t testData_B = 0;

    const size_t numTestStages = 9;
    size_t testStages[numTestStages] = { 0 };

    // actual test
    {
        auto pFsm = pMainFsm = factory.create("fsm");
        pFsm->setLogging(&logging);

        // Adding states
        auto pState_A = pFsm->create<State>("A");
        auto state_B =  pFsm->create<State>("B");

        // Adding state transitions
        pFsm->addTransition("__enter", "A");
        pFsm->addTransition("A", "B");
        pFsm->addTransition("B", "__leave");

        // Set state leave conditions
        pState_A->setLeaveCondition([&](){
            return frameCount == 3;
        });
        state_B->setLeaveCondition([&](){
            return frameCount == 6;
        });

        // Set some listeners
        pFsm->getEnterEvent()->registerListener([&](State::EnterEventData*){
            testStages[0]++;
            return gep::EventResult::Handled;
        });
        pFsm->getLeaveEvent()->registerListener([&](State::LeaveEventData*){
            testStages[1]++;
            return gep::EventResult::Handled;
        });
        pFsm->getUpdateEvent()->registerListener([&](State::UpdateEventData*){
            testStages[2]++;
            return gep::EventResult::Handled;
        });

        pState_A->getEnterEvent()->registerListener([&](State::EnterEventData*){
            testStages[3]++;
            return gep::EventResult::Handled;
        });
        pState_A->getLeaveEvent()->registerListener([&](State::LeaveEventData*){
            testStages[4]++;
            return gep::EventResult::Handled;
        });
        pState_A->getUpdateEvent()->registerListener([&](State::UpdateEventData*){
            testStages[5]++;
            testData_A++;
            return gep::EventResult::Handled;
        });

        state_B->getEnterEvent()->registerListener([&](State::EnterEventData*){
            testStages[6]++;
            return gep::EventResult::Handled;
        });
        state_B->getLeaveEvent()->registerListener([&](State::LeaveEventData*){
            testStages[7]++;
            return gep::EventResult::Handled;
        });
        state_B->getUpdateEvent()->registerListener([&](State::UpdateEventData*){
            testStages[8]++;
            testData_B++;
            return gep::EventResult::Handled;
        });
    }

    pMainFsm->setLogging(&logging);

    // Run the machine
    pMainFsm->run(_updateFramework);

    for(frameCount = 0; frameCount < 10; ++frameCount)
    {
        _updateFramework.run();
    }
}