Merge pull request #409 from Alkarex/patch-1
[mono.git] / mcs / class / System.Threading.Tasks.Dataflow / Test / Blocks.cs
1 // Blocks.cs
2 //  
3 // Copyright (c) 2012 Petr Onderka
4 // 
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 // 
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 // 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 // THE SOFTWARE.
22
23 using System.Collections.Generic;
24 using System.Threading;
25 using System.Threading.Tasks.Dataflow;
26
27 namespace MonoTests {
28         public static class Blocks {
29                 public static IEnumerable<IDataflowBlock> CreateBlocksWithOptions (
30                         DataflowBlockOptions dataflowBlockOptions,
31                         ExecutionDataflowBlockOptions executionDataflowBlockOptions,
32                         GroupingDataflowBlockOptions groupingDataflowBlockOptions)
33                 {
34                         yield return new ActionBlock<int> (i => { }, executionDataflowBlockOptions);
35                         yield return new BatchBlock<double> (10, groupingDataflowBlockOptions);
36                         yield return new BatchedJoinBlock<dynamic, object> (
37                                 10, groupingDataflowBlockOptions);
38                         yield return new BatchedJoinBlock<dynamic, object, char> (
39                                 10, groupingDataflowBlockOptions);
40                         yield return new BroadcastBlock<byte> (x => x, dataflowBlockOptions);
41                         yield return new BufferBlock<int> (dataflowBlockOptions);
42                         yield return new JoinBlock<double, dynamic> (groupingDataflowBlockOptions);
43                         yield return new JoinBlock<object, char, byte> (
44                                 groupingDataflowBlockOptions);
45                         yield return new TransformBlock<int, int> (
46                                 i => i, executionDataflowBlockOptions);
47                         yield return new TransformManyBlock<double, dynamic>(
48                                 x => new dynamic[0], executionDataflowBlockOptions);
49                         yield return new WriteOnceBlock<object> (x => x, dataflowBlockOptions);
50                 }
51
52                 public static IEnumerable<IDataflowBlock> CreateBlocks()
53                 {
54                         return CreateBlocksWithOptions (new DataflowBlockOptions (),
55                                 new ExecutionDataflowBlockOptions (), new GroupingDataflowBlockOptions ());
56                 }
57
58                 public static IEnumerable<IDataflowBlock> CreateBlocksWithNameFormat(string nameFormat)
59                 {
60                         var dataflowBlockOptions = new DataflowBlockOptions { NameFormat = nameFormat };
61                         var executionDataflowBlockOptions = new ExecutionDataflowBlockOptions { NameFormat = nameFormat };
62                         var groupingDataflowBlockOptions = new GroupingDataflowBlockOptions { NameFormat = nameFormat };
63
64                         return CreateBlocksWithOptions (dataflowBlockOptions,
65                                 executionDataflowBlockOptions, groupingDataflowBlockOptions);
66                 }
67
68                 public static IEnumerable<IDataflowBlock> CreateBlocksWithCancellationToken(CancellationToken cancellationToken)
69                 {
70                         var dataflowBlockOptions = new DataflowBlockOptions { CancellationToken = cancellationToken};
71                         var executionDataflowBlockOptions = new ExecutionDataflowBlockOptions { CancellationToken = cancellationToken};
72                         var groupingDataflowBlockOptions = new GroupingDataflowBlockOptions { CancellationToken = cancellationToken};
73
74                         return CreateBlocksWithOptions (dataflowBlockOptions,
75                                 executionDataflowBlockOptions, groupingDataflowBlockOptions);
76                 }
77
78                 public static IEnumerable<IReceivableSourceBlock<T>> CreateSimpleSourceBlocks<T>()
79                 {
80                         yield return new BroadcastBlock<T> (x => x);
81                         yield return new BufferBlock<T> ();
82                         yield return new TransformBlock<T, T> (
83                                 x => x);
84                         yield return new TransformManyBlock<T, T>(
85                                 x => new T[0]);
86                         yield return new WriteOnceBlock<T> (x => x);
87                 }
88
89                 public static IEnumerable<ITargetBlock<T>> CreateTargetBlocks<T> ()
90                 {
91                         yield return new ActionBlock<T> (i => { });
92                         yield return new BatchBlock<T> (10);
93                         yield return new BatchedJoinBlock<T, T> (10).Target1;
94                         yield return new BatchedJoinBlock<T, T> (10).Target2;
95                         yield return new BatchedJoinBlock<T, T, T> (10).Target1;
96                         yield return new BatchedJoinBlock<T, T, T> (10).Target2;
97                         yield return new BatchedJoinBlock<T, T, T> (10).Target3;
98                         yield return new BroadcastBlock<T> (x => x);
99                         yield return new BufferBlock<T> ();
100                         yield return new JoinBlock<T, T> ().Target1;
101                         yield return new JoinBlock<T, T> ().Target2;
102                         yield return new JoinBlock<T, T, T> ().Target1;
103                         yield return new JoinBlock<T, T, T> ().Target2;
104                         yield return new JoinBlock<T, T, T> ().Target3;
105                         yield return new TransformBlock<T, T> (i => i);
106                         yield return new TransformManyBlock<T, T>(x => new T[0]);
107                         yield return new WriteOnceBlock<T> (x => x);
108                         yield return DataflowBlock.NullTarget<T> ();
109                 }
110         }
111 }