[r236]: / tags / v1.0.0.0 / CfUnitTimedTestExample.project.md  Maximize  Restore  History

Download this file

172 lines (153 with data), 3.7 kB


FUNCTION_BLOCK FB_Timer
VAR_INPUT
    xSwitch: BOOL;
    xOff: BOOL;
    tDuration: TIME;
END_VAR
VAR_OUTPUT
    xOut: BOOL;
END_VAR
VAR
    tonDuration: TON;
    rtrigSwitch: R_TRIG;
    rtrigOff: R_TRIG;
    bStart: BOOL;
END_VAR
rtrigSwitch(CLK := xSwitch);
IF (rtrigSwitch.Q) THEN
    bStart := TRUE;
    xOut := TRUE;
    tonDuration(IN := FALSE);
END_IF

rtrigOff(CLK := xOff);
IF (rtrigOff.Q) THEN
    bStart := FALSE;
    xOut := FALSE;
    tonDuration(IN := FALSE);
END_IF

IF (bStart) THEN
    tonDuration(IN := TRUE, PT := tDuration);
    IF (tonDuration.Q) THEN
        tonDuration(IN := FALSE);
        xOut := FALSE;
        bStart := FALSE;
    END_IF
END_IF

PROGRAM MAIN
VAR
    fbFoo_Tests: FB_Timer_Tests;
END_VAR
CfUnit.RUN();

FUNCTION_BLOCK FB_Timer_Tests EXTENDS CfUnit.FB_TestSuite
VAR
    State: WORD;
    tonDelay: TON;
    fbTimer_0100: FB_Timer;
    fbTimer_0200: FB_Timer;
    fbTimer_0300: FB_Timer;
    fbTimer_0400: FB_Timer;
    xError: BOOL;
    xDone: BOOL;
END_VAR
SomeTimerTest( xError => xError,
               xDone  => xDone );

METHOD SomeTimerTest: 
VAR_OUTPUT
    xDone: BOOL;
    xError: BOOL;
END_VAR
CASE State OF
{region "start"}
    0:
        TEST('SomeFooTest');
        LOGSTR( 1, 'Test start: %s' , CfUnit.GVL_CfUnit.CurrentTestNameBeingCalled ); // send a message to the log as information(1), warning(2), or error(4)
        xError := FALSE;
        xDone := FALSE; 
        State := 100;
{endregion}

{region "Switch_RisingEdgeAndDuration1s_OutIsTrueFor1s"}
    100:
        fbTimer_0100(xSwitch := TRUE, tDuration := T#1S);
        AssertEquals_BOOL(TRUE, fbTimer_0100.xOut, 'Switch_RisingEdgeAndDuration1s_OutIsTrueFor1s');         
        tonDelay(IN := TRUE, PT := T#900MS);
        IF (tonDelay.Q) THEN
            tonDelay(IN := FALSE);
            State := 200;
        END_IF  
{endregion}

{region "Switch_RisingEdgeAndDuration1s_OutIsFalseAfter1100ms"}
    200:
        fbTimer_0200(xSwitch := TRUE, tDuration := T#1S);
        tonDelay(IN := TRUE, PT := T#1100MS);
        IF (tonDelay.Q) THEN
            tonDelay(IN := FALSE);
            AssertEquals_BOOL(FALSE, fbTimer_0200.xOut, 'Switch_RisingEdgeAndDuration1s_OutIsFalseAfter1100ms');
            State := 300;
        END_IF  
{endregion}

{region "Switch_RetriggerSwitch_OutKeepsTrue"}
    300:
        fbTimer_0300(xSwitch := TRUE, tDuration := T#500MS);
        AssertEquals_BOOL(TRUE, fbTimer_0300.xOut, 'Switch_RetriggerSwitch_OutKeepsTrue');
        tonDelay(IN := TRUE, PT := T#400MS);
        IF (tonDelay.Q) THEN
            tonDelay(IN := FALSE);
            fbTimer_0300(xSwitch := FALSE);
            State := 310;           
        END_IF      

    310:
        fbTimer_0300(xSwitch := TRUE, tDuration := T#500MS);
        AssertEquals_BOOL(TRUE, fbTimer_0300.xOut, 'Switch_RetriggerSwitch_OutKeepsTrue');
        tonDelay(IN := TRUE, PT := T#400MS);
        IF (tonDelay.Q) THEN
            tonDelay(IN := FALSE);
            State := 400;
        END_IF
{endregion}

{region "Off_RisingEdgeAndOutIsTrue_OutIsFalse"}
    400:
        fbTimer_0400(xSwitch := TRUE, tDuration := T#200MS);
        AssertEquals_BOOL(TRUE, fbTimer_0400.xOut, 'Off_RisingEdgeAndOutIsTrue_OutIsFalse');
        tonDelay(IN := TRUE, PT := T#100MS);
        IF (tonDelay.Q) THEN
            tonDelay(IN := FALSE);
            fbTimer_0400(xOff := TRUE);
            AssertEquals_BOOL(FALSE, fbTimer_0400.xOut, 'Off_RisingEdgeAndOutIsTrue_OutIsFalse');
            State := 1000;
        END_IF
{endregion}

{region "done"}
    1000:
        State := 1010;
        LOGSTR( 1, 'Test finished: %s' , CfUnit.GVL_CfUnit.CurrentTestNameBeingCalled );             

    1010:
        TEST_FINISHED();
        xDone := TRUE;
{endregion}

{region "error"}
    1200:
        xError := TRUE;

{endregion}
    ELSE
        state := 1200;
END_CASE