Skip to content

Commit

Permalink
Added boolean component for functions, added infinite and extend meth…
Browse files Browse the repository at this point in the history
…ods for lists and collections, added tests for all of the newly added methods
  • Loading branch information
konkked committed Feb 1, 2016
1 parent 8f4b7d1 commit d4373f1
Show file tree
Hide file tree
Showing 21 changed files with 1,478 additions and 13 deletions.
Binary file modified Underscore.Test/Collection/CreationTest.cs
Binary file not shown.
109 changes: 109 additions & 0 deletions Underscore.Test/Function/BooleanTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Underscore.Function;

namespace Underscore.Test.Function
{
[TestClass]
public class BooleanTest
{
[TestMethod]
public void FunctionNegate()
{
var testing = new BooleanComponent();
var function = testing.Negate( () => true);

Assert.IsFalse(function());

var fn2 = testing.Negate(new Func<string, bool>(s => s == "s"));
Assert.IsTrue(fn2("l"));
Assert.IsFalse(fn2("s"));

}

[TestMethod]
public void FunctionOrPositive()
{
bool[] wasCalled = {false, false, false, false};
Func<string,string, bool>[] toCall =
{
(s,s2) => wasCalled[0] = s == "s1" && s2 == "s2",
(s,s2) => wasCalled[1] = s == "s1" && s2 == "s2",
(s,s2) => wasCalled[2] = s == "s1" && s2 == "s2",
(s,s2) => wasCalled[3] = s == "s1" && s2 == "s2"
};
var testing = new BooleanComponent();
var result = testing.Or(toCall);
Assert.IsTrue(result("s1","s2"));
Assert.IsTrue(wasCalled[0]);
Assert.IsFalse(wasCalled[1]);
Assert.IsFalse(wasCalled[2]);
Assert.IsFalse(wasCalled[3]);

}


[TestMethod]
public void FunctionOrNegative()
{
bool[] wasCalled = { true, true, true, true};
Func<string, string, bool>[] toCall =
{
(s,s2) => wasCalled[0] = s != "s1" && s2 != "s2",
(s,s2) => wasCalled[1] = s != "s1" && s2 != "s2",
(s,s2) => wasCalled[2] = s != "s1" && s2 != "s2",
(s,s2) => wasCalled[3] = s != "s1" && s2 != "s2"
};
var testing = new BooleanComponent();
var result = testing.Or(toCall);
Assert.IsFalse(result("s1", "s2"));
foreach (var b in wasCalled)
{
Assert.IsFalse(b);
}
}



[TestMethod]
public void FunctionAndPositive()
{
bool[] wasCalled = { false, false, false, false };
Func<string, string, bool>[] toCall =
{
(s,s2) => wasCalled[0] = s == "s1" && s2 == "s2",
(s,s2) => wasCalled[1] = s == "s1" && s2 == "s2",
(s,s2) => wasCalled[2] = s == "s1" && s2 == "s2",
(s,s2) => wasCalled[3] = s == "s1" && s2 == "s2"
};
var testing = new BooleanComponent();
var result = testing.And(toCall);
Assert.IsTrue(result("s1", "s2"));
foreach (var b in wasCalled)
{
Assert.IsTrue(b);
}
}


[TestMethod]
public void FunctionAndNegative()
{
bool[] wasCalled = { true, true, true, true };
Func<string, string, bool>[] toCall =
{
(s,s2) => wasCalled[0] = s != "s1" && s2 != "s2",
(s,s2) => wasCalled[1] = s == "s1" && s2 == "s2",
(s,s2) => wasCalled[2] = s == "s1" && s2 == "s2",
(s,s2) => wasCalled[3] = s == "s1" && s2 == "s2"
};
var testing = new BooleanComponent();
var result = testing.And(toCall);
Assert.IsFalse(result("s1", "s2"));
Assert.IsFalse(wasCalled[0]);

for(int i=1;i<wasCalled.Length;i++)
Assert.IsTrue(wasCalled[i]);
}
}
}
Binary file modified Underscore.Test/List/ManipulateTest.cs
Binary file not shown.
3 changes: 2 additions & 1 deletion Underscore.Test/Module/FunctionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public void FunctionModuleCreateTest( )
new ComposeComponent( ),
new ConvertComponent(),
new SynchComponent( new CompactComponent(), new Underscore.Utility.CompactComponent(), new Underscore.Utility.MathComponent()),
new CacheComponent( new Underscore.Function.CompactComponent(), new Underscore.Utility.CompactComponent())
new CacheComponent( new Underscore.Function.CompactComponent(), new Underscore.Utility.CompactComponent()),
new BooleanComponent()
);
}
}
Expand Down
5 changes: 4 additions & 1 deletion Underscore.Test/Underscore.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@
</When>
<Otherwise>
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework" />
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework">
<Private>False</Private>
</Reference>
</ItemGroup>
</Otherwise>
</Choose>
Expand All @@ -69,6 +71,7 @@
<Compile Include="Function\SynchTest.cs" />
<Compile Include="Function\PartialTest.cs" />
<Compile Include="Function\SplitTest.cs" />
<Compile Include="Function\BooleanTest.cs" />
<Compile Include="List\AggregateTest.cs" />
<Compile Include="List\DelegateTest.cs" />
<Compile Include="List\ManipulateTest.cs" />
Expand Down
10 changes: 8 additions & 2 deletions Underscore.cs.sln
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.30723.0
# Visual Studio 14
VisualStudioVersion = 14.0.23107.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Underscore.cs", "Underscore.cs\Underscore.cs.csproj", "{946C4F0D-101A-46DB-8C41-C102D0947E99}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Underscore.Test", "Underscore.Test\Underscore.Test.csproj", "{D8C013CF-12B1-4743-9044-D92903510970}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PreformanceTest", "PreformanceTest\PreformanceTest.csproj", "{B34E3068-1596-4F2D-B1BA-39074E0B2661}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -21,6 +23,10 @@ Global
{D8C013CF-12B1-4743-9044-D92903510970}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D8C013CF-12B1-4743-9044-D92903510970}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D8C013CF-12B1-4743-9044-D92903510970}.Release|Any CPU.Build.0 = Release|Any CPU
{B34E3068-1596-4F2D-B1BA-39074E0B2661}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B34E3068-1596-4F2D-B1BA-39074E0B2661}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B34E3068-1596-4F2D-B1BA-39074E0B2661}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B34E3068-1596-4F2D-B1BA-39074E0B2661}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
3 changes: 3 additions & 0 deletions Underscore.cs/Collection/Contract/Creation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ public interface ICreationComponent
/// </summary>
Func<IEnumerable<T>> Snapshot<T>( IEnumerable<T> collection );

IEnumerable<T> Extend<T>(IEnumerable<T> collection, int length);


IEnumerable<T> Infinite<T>(IEnumerable<T> collection);
}
}
41 changes: 41 additions & 0 deletions Underscore.cs/Collection/Implementation/Creation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,46 @@ public Func<IEnumerable<T>> Snapshot<T>( IEnumerable<T> collection )
return ( ) => new List<T>( tsnap );
}

public IEnumerable<T> Extend<T>(IEnumerable<T> collection, int length)
{
using (var enmr = collection.GetEnumerator())
{
if(!enmr.MoveNext()) throw new ApplicationException("Cannot extend an empty collection");
yield return enmr.Current;

//make i = 1 instead of zero to account for the first return.
for (int i = 1; i < length; i++)
{
if (!enmr.MoveNext()) { enmr.Reset();
enmr.MoveNext();
}

yield return enmr.Current;

}
}
}

public IEnumerable<T> Infinite<T>(IEnumerable<T> collection)
{
using (var enmr = collection.GetEnumerator())
{
if (!enmr.MoveNext()) throw new ApplicationException("Cannot extend an empty collection");
yield return enmr.Current;

//make i = 1 instead of zero to account for the first return.
while(true)
{

if (!enmr.MoveNext()) { enmr.Reset();
enmr.MoveNext();
}

yield return enmr.Current;

}
}
}

}
}
Loading

0 comments on commit d4373f1

Please sign in to comment.