コード例 #1
0
ファイル: Tests.cs プロジェクト: macro187/halfdecentsharp
Test_ICollectionG_AsSink()
{
    ICollectionRG< int > c =
        new SCG.List< int >()
        .AsHalfdecentCollection();
    new int[] { 1, 2, 3 }
        .AsStream()
        .EmptyTo( c.AsSink() );
    Assert(
        c.Stream().AsEnumerable().SequenceEqual(
            new int[] { 1, 2, 3 } ) );
}
コード例 #2
0
ファイル: Tests.cs プロジェクト: macro187/halfdecentsharp
Test_CollectionFromSystemListAdapter()
{
    Print( "Create" );
    var c =
        SystemEnumerable.Create( 1, 2, 3, 4, 5, 6 )
        .ToList()
        .AsHalfdecentCollection();

    Print( ".Stream()" );
    Assert(
        c.Stream()
        .SequenceEqual(
            Stream.Create( 1, 2, 3, 4, 5, 6 ) ) );

    Print( ".GetAndReplaceWhere( Predicate< T > )" );
    var to = new SCG.List< int >();
    Stream.Create( 20, 40, 60, 80, 100 )
        .To( c.GetAndReplaceWhere( i => i % 2 == 0 ) )
        .EmptyTo(
            to.AsSink() );
    Assert(
        c.Stream()
        .SequenceEqual(
            Stream.Create( 1, 20, 3, 40, 5, 60 ) ) );
    to.Sort();
    Assert(
        to.SequenceEqual(
            SystemEnumerable.Create( 2, 4, 6 ) ) );

    Print( ".GetAndRemoveWhere( Predicate< T > )" );
    to.Clear();
    c.GetAndRemoveWhere( i => i >= 10 )
        .EmptyTo( to.AsSink() );
    Assert(
        c.Stream()
        .SequenceEqual(
            Stream.Create( 1, 3, 5 ) ) );
    to.Sort();
    Assert(
        to.SequenceEqual(
            SystemEnumerable.Create( 20, 40, 60 ) ) );

    Print( ".Get( TKey )" );
    Assert( c.Get( 0 ) == 1 );
    Assert( c.Get( 1 ) == 3 );
    Assert( c.Get( 2 ) == 5 );

    Print( ".Replace( TKey, T )" );
    c.Replace( 1, 2 );
    Assert(
        c.Stream()
        .SequenceEqual(
            Stream.Create( 1, 2, 5 ) ) );

    Print( ".Remove( TKey )" );
    c.Remove( 1 );
    Assert(
        c.Stream()
        .SequenceEqual(
            Stream.Create( 1, 5 ) ) );

    Print( ".Contains( TKey )" );
    Assert( c.Contains( 0 ) );
    Assert( c.Contains( 1 ) );
    Assert( !c.Contains( 2 ) );

    Print( ".Stream( TKey )" );
    Assert(
        c.Stream( 1 )
        .SequenceEqual(
            Stream.Create( 5 ) ) );

    Print( ".GetAndReplaceAll( TKey )" );
    to.Clear();
    Stream.Create( 6 )
        .To( c.GetAndReplaceAll( 1 ) )
        .EmptyTo( to.AsSink() );
    Assert(
        c.Stream()
        .SequenceEqual(
            Stream.Create( 1, 6 ) ) );
    Assert(
        to.SequenceEqual(
            SystemEnumerable.Create( 5 ) ) );

    Print( ".GetAndRemoveAll( TKey )" );
    to.Clear();
    c.GetAndRemoveAll( 1 )
        .EmptyTo(
            to.AsSink() );
    Assert(
        c.Stream().SequenceEqual(
            Stream.Create( 1 ) ) );
    Assert(
        to.SequenceEqual(
            SystemEnumerable.Create( 6 ) ) );

    Print( ".Add( TKey, T )" );
    c.Add( 0, 0 );
    c.Add( 2, 2 );
    c.Add( 1, 1 );
    Assert(
        c.Stream()
        .SequenceEqual(
            Stream.Create( 0, 1, 1, 2 ) ) );

    Print( ".Count" );
    Assert( c.Count == 4 );

    Print( ".StreamPairs()" );
    var ts = c.StreamPairs();
    Assert( ts.Pull().BothEqual( 0L, 0 ) );
    Assert( ts.Pull().BothEqual( 1L, 1 ) );
    Assert( ts.Pull().BothEqual( 2L, 1 ) );
    Assert( ts.Pull().BothEqual( 3L, 2 ) );
    Assert( !ts.TryPull().A );
}
コード例 #3
0
ファイル: Tests.cs プロジェクト: macro187/halfdecentsharp
Test_CollectionFromSystemStringBuilderAdapter()
{
    Print( "Create" );
    var c =
        new StringBuilder( "abcde" )
        .AsHalfdecentCollection();

    Print( ".Stream()" );
    Assert(
        c.Stream()
        .SequenceEqual(
            Stream.Create( 'a', 'b', 'c', 'd', 'e' ) ) );

    Print( ".GetAndReplaceWhere( Predicate< char > )" );
    var to = new SCG.List< char >();
    Stream.Create( 'B' )
        .To( c.GetAndReplaceWhere( ch => ch == 'b' ) )
        .EmptyTo( to.AsSink() );
    Assert(
        c.Stream()
        .SequenceEqual(
            Stream.Create( 'a', 'B', 'c', 'd', 'e' ) ) );
    to.Sort();
    Assert(
        to.SequenceEqual(
            SystemEnumerable.Create( 'b' ) ) );

    Print( ".GetAndRemoveWhere( Predicate< char > )" );
    to.Clear();
    c.GetAndRemoveWhere( ch => ch == 'B' )
        .EmptyTo( to.AsSink() );
    Assert(
        c.Stream()
        .SequenceEqual(
            Stream.Create( 'a', 'c', 'd', 'e' ) ) );
    to.Sort();
    Assert(
        to.SequenceEqual(
            SystemEnumerable.Create( 'B' ) ) );

    Print( ".Get( IInteger )" );
    Assert( c.Get( 0 ) == 'a' );
    Assert( c.Get( 1 ) == 'c' );
    Assert( c.Get( 2 ) == 'd' );
    Assert( c.Get( 3 ) == 'e' );

    Print( ".Replace( IInteger, char )" );
    c.Replace( 1, 'C' );
    Assert(
        c.Stream()
        .SequenceEqual(
            Stream.Create( 'a', 'C', 'd', 'e' ) ) );

    Print( ".Remove( IInteger )" );
    c.Remove( 1 );
    Assert(
        c.Stream()
        .SequenceEqual(
            Stream.Create( 'a', 'd', 'e' ) ) );

    Print( ".Contains( IInteger )" );
    Assert( !c.Contains( -1 ) );
    Assert( c.Contains( 0 ) );
    Assert( c.Contains( 1 ) );
    Assert( c.Contains( 2 ) );
    Assert( !c.Contains( 3 ) );

    Print( ".Stream( IInteger )" );
    Assert(
        c.Stream( 1 )
        .SequenceEqual(
            Stream.Create( 'd' ) ) );

    Print( ".GetAndReplaceAll( IInteger )" );
    to.Clear();
    Stream.Create( 'D' )
        .To( c.GetAndReplaceAll( 1 ) )
        .EmptyTo( to.AsSink() );
    Assert(
        c.Stream()
        .SequenceEqual(
            Stream.Create( 'a', 'D', 'e' ) ) );
    Assert(
        to.SequenceEqual(
            SystemEnumerable.Create( 'd' ) ) );

    Print( ".GetAndRemoveAll( IInteger )" );
    to.Clear();
    c.GetAndRemoveAll( 1 )
        .EmptyTo( to.AsSink() );
    Assert(
        c.Stream()
        .SequenceEqual(
            Stream.Create( 'a', 'e' ) ) );
    Assert(
        to.SequenceEqual(
            SystemEnumerable.Create( 'D' ) ) );

    Print( ".Add( IInteger, char )" );
    c.Add( 1, 'b' );
    c.Add( 2, 'c' );
    c.Add( 3, 'd' );
    c.Add( 5, 'f' );
    Assert(
        c.Stream()
        .SequenceEqual(
            Stream.Create( 'a', 'b', 'c', 'd', 'e', 'f' ) ) );

    Print( ".Count" );
    Assert( c.Count == 6 );

    Print( ".StreamPairs()" );
    var ts = c.StreamPairs();
    Assert( ts.Pull().BothEqual( 0L, 'a' ) );
    Assert( ts.Pull().BothEqual( 1L, 'b' ) );
    Assert( ts.Pull().BothEqual( 2L, 'c' ) );
    Assert( ts.Pull().BothEqual( 3L, 'd' ) );
    Assert( ts.Pull().BothEqual( 4L, 'e' ) );
    Assert( ts.Pull().BothEqual( 5L, 'f' ) );
    Assert( !ts.TryPull().A );
}