Skip to content

Commit

Permalink
Add lab
Browse files Browse the repository at this point in the history
Add lab
  • Loading branch information
JonathanMagnan committed Oct 6, 2018
1 parent d262f64 commit c7b4f30
Show file tree
Hide file tree
Showing 9 changed files with 348 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/Z.Linq.Async.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{85D28F09-D
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Z.Test.Linq.Async", "test\Z.Test.Linq.Async\Z.Test.Linq.Async.csproj", "{51938F78-E4FE-4CE0-9343-AC62DB552025}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Z.Linq.Async.NetStandard20", "Z.Linq.Async.NetStandard20\Z.Linq.Async.NetStandard20.csproj", "{CFCA713B-34D9-4D00-B478-2A5B25C6403E}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Z.Linq.Async.NetStandard20", "Z.Linq.Async.NetStandard20\Z.Linq.Async.NetStandard20.csproj", "{CFCA713B-34D9-4D00-B478-2A5B25C6403E}"
EndProject
Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "Z.Linq.Async.Shared", "Z.Linq.Async.Shared\Z.Linq.Async.Shared.shproj", "{9E4CC057-6A64-4A8B-8E65-65EAE53488BF}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "lab", "lab", "{91652E81-135B-47CF-B92C-98BED3A67EC7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Z.Lab.LinqAsync.NetCore", "lab\Z.Lab.LinqAsync.NetCore\Z.Lab.LinqAsync.NetCore.csproj", "{440A34AA-9335-4E4F-926C-EE1C99880573}"
EndProject
Global
GlobalSection(SharedMSBuildProjectFiles) = preSolution
Z.Linq.Async.Shared\Z.Linq.Async.Shared.projitems*{8893c88a-9a81-4269-a479-e5a5af33b94d}*SharedItemsImports = 4
Expand All @@ -35,12 +39,17 @@ Global
{CFCA713B-34D9-4D00-B478-2A5B25C6403E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CFCA713B-34D9-4D00-B478-2A5B25C6403E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CFCA713B-34D9-4D00-B478-2A5B25C6403E}.Release|Any CPU.Build.0 = Release|Any CPU
{440A34AA-9335-4E4F-926C-EE1C99880573}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{440A34AA-9335-4E4F-926C-EE1C99880573}.Debug|Any CPU.Build.0 = Debug|Any CPU
{440A34AA-9335-4E4F-926C-EE1C99880573}.Release|Any CPU.ActiveCfg = Release|Any CPU
{440A34AA-9335-4E4F-926C-EE1C99880573}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{51938F78-E4FE-4CE0-9343-AC62DB552025} = {85D28F09-DFB3-409C-877B-8E6546D87C1F}
{440A34AA-9335-4E4F-926C-EE1C99880573} = {91652E81-135B-47CF-B92C-98BED3A67EC7}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {8F1FF45B-4B61-4F59-A668-C5B7184F74A5}
Expand Down
60 changes: 60 additions & 0 deletions src/lab/Z.Lab.LinqAsync.NetCore/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Z.Linq;
using Z.Linq.Async;
using Z.Test.Linq.Async;
using Z.Test.Linq.Async.Model;

namespace Z.Lab.LinqAsync.NetCore
{
class Program
{
static void Main(string[] args)
{
var list = new List<int> { 1, 2, 3, 4, 5 };
var enumerable = new TestEnumerable<int>(list);
var predicateAsync = new TestPredicateAsync<int>(i =>
{
switch (i)
{
case 1:
return Task.Delay(600).ContinueWith(task => true);
case 2:
return Task.Delay(200).ContinueWith(task => true);
case 3:
return Task.Delay(800).ContinueWith(task => true);
case 4:
return Task.Delay(500).ContinueWith(task => true);
case 5:
return Task.Delay(300).ContinueWith(task => true);
default:
throw new Exception("Oops!");
}
});

var defaultValue = LinqAsyncManager.DefaultValue.OrderByPredicateCompletion;

try
{
LinqAsyncManager.DefaultValue.OrderByPredicateCompletion = false;

var result = enumerable.WhereAsync(x => predicateAsync.Predicate(x)).ToList().Result;

//// MUST have 5 iterations for enumerable
//Assert.AreEqual(4, enumerable.CurrentIndex);

//// MUST be ordered with original order
//Assert.AreEqual(1, result[0]);
//Assert.AreEqual(2, result[1]);
//Assert.AreEqual(3, result[2]);
//Assert.AreEqual(4, result[3]);
//Assert.AreEqual(5, result[4]);
}
finally
{
LinqAsyncManager.DefaultValue.OrderByPredicateCompletion = defaultValue;
}
}
}
}
12 changes: 12 additions & 0 deletions src/lab/Z.Lab.LinqAsync.NetCore/Z.Lab.LinqAsync.NetCore.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\Z.Linq.Async.NetStandard20\Z.Linq.Async.NetStandard20.csproj" />
</ItemGroup>

</Project>
39 changes: 39 additions & 0 deletions src/lab/Z.Lab.LinqAsync.NetCore/_Model/TestEnumerable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Collections;
using System.Collections.Generic;
using Z.Test.Linq.Async.Model;

namespace Z.Test.Linq.Async
{
public class TestEnumerable<T> : IEnumerable<T>
{
public TestEnumerable(List<T> originalValues)
{
CurrentIndex = -1;
OriginalValues = originalValues;
}

public TestEnumerable(List<T> originalValues, Func<T, bool> errorPredicate)
{
CurrentIndex = -1;
ErrorPredicate = errorPredicate;
OriginalValues = originalValues;
}

public List<T> OriginalValues { get; set; }

public int CurrentIndex { get; set; }

public Func<T, bool> ErrorPredicate { get; set; }

public IEnumerator<T> GetEnumerator()
{
return new TestEnumerator<T>(this);
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}
40 changes: 40 additions & 0 deletions src/lab/Z.Lab.LinqAsync.NetCore/_Model/TestEnumerableAsync.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using Z.Test.Linq.Async.Model;

namespace Z.Test.Linq.Async
{
public class TestEnumerableAsync<T, TItem> : IEnumerable<T> where T : Task<TItem>
{
public TestEnumerableAsync(List<TItem> originalValues)
{
CurrentIndex = -1;
OriginalValues = originalValues;
}

public TestEnumerableAsync(List<TItem> originalValues, Func<TItem, bool> errorPredicate)
{
CurrentIndex = -1;
ErrorPredicate = errorPredicate;
OriginalValues = originalValues;
}

public List<TItem> OriginalValues { get; set; }

public int CurrentIndex { get; set; }

public Func<TItem, bool> ErrorPredicate { get; set; }

public IEnumerator<T> GetEnumerator()
{
return new TestEnumeratorAsync<T, TItem>(this);
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}
57 changes: 57 additions & 0 deletions src/lab/Z.Lab.LinqAsync.NetCore/_Model/TestEnumerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.Collections;
using System.Collections.Generic;

namespace Z.Test.Linq.Async.Model
{
public class TestEnumerator<T> : IEnumerator<T>
{
public TestEnumerator(TestEnumerable<T> testTestEnumerable)
{
TestEnumerable = testTestEnumerable;
}

private TestEnumerable<T> TestEnumerable { get; }

public T Current
{
get
{
var currentValue = TestEnumerable.OriginalValues[TestEnumerable.CurrentIndex];
if (TestEnumerable.ErrorPredicate != null)
{
if (TestEnumerable.ErrorPredicate(currentValue))
{
throw new Exception("TestEnumerable;ErrorPredicate;Value=" + currentValue);
}
}
return currentValue;
}
}

object IEnumerator.Current
{
get { return Current; }
}

public void Dispose()
{
}

public bool MoveNext()
{
if (TestEnumerable.CurrentIndex + 1 < TestEnumerable.OriginalValues.Count)
{
TestEnumerable.CurrentIndex++;
return true;
}

return false;
}

public void Reset()
{
TestEnumerable.CurrentIndex = -1;
}
}
}
58 changes: 58 additions & 0 deletions src/lab/Z.Lab.LinqAsync.NetCore/_Model/TestEnumeratorAsync.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Z.Test.Linq.Async.Model
{
public class TestEnumeratorAsync<T, TItem> : IEnumerator<T> where T : Task<TItem>
{
public TestEnumeratorAsync(TestEnumerableAsync<T, TItem> testTestEnumerable)
{
TestEnumerable = testTestEnumerable;
}

private TestEnumerableAsync<T, TItem> TestEnumerable { get; }

public T Current
{
get
{
var currentValue = TestEnumerable.OriginalValues[TestEnumerable.CurrentIndex];
if (TestEnumerable.ErrorPredicate != null)
{
if (TestEnumerable.ErrorPredicate(currentValue))
{
throw new Exception("TestEnumerable;ErrorPredicate;Value=" + currentValue);
}
}
return (T) Task.Delay(100*(int) (object) currentValue);
}
}

object IEnumerator.Current
{
get { return Current; }
}

public void Dispose()
{
}

public bool MoveNext()
{
if (TestEnumerable.CurrentIndex + 1 < TestEnumerable.OriginalValues.Count)
{
TestEnumerable.CurrentIndex++;
return true;
}

return false;
}

public void Reset()
{
TestEnumerable.CurrentIndex = -1;
}
}
}
35 changes: 35 additions & 0 deletions src/lab/Z.Lab.LinqAsync.NetCore/_Model/TestPredicate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;

namespace Z.Test.Linq.Async.Model
{
public class TestPredicate<T>
{
public Func<T, bool> ErrorPredicate;
public Func<T, bool> OriginalPredicate;

public TestPredicate(Func<T, bool> originalPredicate)
{
OriginalPredicate = originalPredicate;
}

public TestPredicate(Func<T, bool> originalPredicate, Func<T, bool> errorPredicate)
{
ErrorPredicate = errorPredicate;
OriginalPredicate = originalPredicate;
}

public int Count { get; set; }

public bool Predicate(T item)
{
Count++;

if (ErrorPredicate != null && ErrorPredicate(item))
{
throw new Exception("TestPredicate;ErrorPredicate;Value=" + item);
}

return OriginalPredicate(item);
}
}
}
37 changes: 37 additions & 0 deletions src/lab/Z.Lab.LinqAsync.NetCore/_Model/TestPredicateAsync.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Threading.Tasks;

namespace Z.Test.Linq.Async.Model
{
public class TestPredicateAsync<T>
{
public Func<T, Task<bool>> ErrorPredicate;
public Func<T, Task<bool>> OriginalPredicate;

public TestPredicateAsync(Func<T, Task<bool>> originalPredicate)
{
OriginalPredicate = originalPredicate;
}

public TestPredicateAsync(Func<T, Task<bool>> originalPredicate, Func<T, Task<bool>> errorPredicate)
{
ErrorPredicate = errorPredicate;
OriginalPredicate = originalPredicate;
}

public int Count { get; set; }


public async Task<bool> Predicate(T item)
{
Count++;

if (ErrorPredicate != null && await ErrorPredicate(item).ConfigureAwait(false))
{
throw new Exception("TestPredicateAsync;ErrorPredicateAsync;Value=" + item);
}

return await OriginalPredicate(item).ConfigureAwait(false);
}
}
}

0 comments on commit c7b4f30

Please sign in to comment.