Esempio n. 1
0
Test_Filter()
{
    int count = 0;
    IFilter< int, int > f
        = new Filter< int, int >(
            null,
            (GetState,Get,Put) => {
                if( GetState() == FilterState.NotStarted ) {
                    return FilterState.Want;
                } else if( GetState() == FilterState.Want ) {
                    Put( Get() );
                    count++;
                    return FilterState.Have;
                } else if( GetState() == FilterState.Have ) {
                    if( count >= 3 ) return FilterState.Closed;
                    return FilterState.Want;
                } else {
                    return FilterState.Closed;
                } },
            () => {;} );

    Assert( f.State == FilterState.Want );
    f.Give( 1 );
    Assert( f.State == FilterState.Have );
    Assert( f.Take() == 1 );

    Assert( f.State == FilterState.Want );
    f.Give( 2 );
    Assert( f.State == FilterState.Have );
    Assert( f.Take() == 2 );

    Assert( f.State == FilterState.Want );
    f.Give( 3 );
    Assert( f.State == FilterState.Have );
    Assert( f.Take() == 3 );

    Assert( f.State == FilterState.Closed );
}