Skip to content

Commit

Permalink
More Awesomeness
Browse files Browse the repository at this point in the history
* Minor refactoring
* Added SubArray<T>
* Added Enuemrable Extensions
* More tests
  • Loading branch information
Nima Ara committed Sep 18, 2016
1 parent 4afb78a commit 7bbdbe8
Show file tree
Hide file tree
Showing 12 changed files with 396 additions and 43 deletions.
1 change: 1 addition & 0 deletions Easy.Common.Tests.Unit/Easy.Common.Tests.Unit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="StringBuilderCache\StringBuilderCacheTests.cs" />
<Compile Include="String\StringExtensionTests.cs" />
<Compile Include="SubArray\SubArrayTests.cs" />
<Compile Include="TaskExtensions\TaskExceptionsTests.cs" />
<Compile Include="TaskExtensions\TaskWaitAllOrFailTests.cs" />
<Compile Include="TimerAndClockTests\ClockTests.cs" />
Expand Down
1 change: 1 addition & 0 deletions Easy.Common.Tests.Unit/EnumerablesTests/Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Easy.Common.Extensions;

public class Context
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,61 @@ namespace Easy.Common.Tests.Unit.EnumerablesTests
using System;
using System.Collections.Generic;
using System.Linq;
using Easy.Common.Extensions;
using NUnit.Framework;
using Shouldly;

[TestFixture]
public sealed class EnumerableExtensionsTests
{
[Test]
public void When_getting_pages_from_a_sequence()
{
var collection = Enumerable.Range(1, 25).ToList();
collection.ShouldBe(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 });

const uint PageSize = 3;

var firstPage = collection.GetPage(0, PageSize).ToArray();

firstPage.ShouldNotBeNull();
firstPage.ShouldNotBeEmpty();
firstPage.Length.ShouldBe(3);
firstPage[0].ShouldBe(1);
firstPage[1].ShouldBe(2);
firstPage[2].ShouldBe(3);

var secondPage = collection.GetPage(1, PageSize).ToArray();

secondPage.ShouldNotBeNull();
secondPage.ShouldNotBeEmpty();
secondPage.Length.ShouldBe(3);
secondPage[0].ShouldBe(4);
secondPage[1].ShouldBe(5);
secondPage[2].ShouldBe(6);

var emptyPage = collection.GetPage(1, 0).ToArray();

emptyPage.ShouldNotBeNull();
emptyPage.ShouldBeEmpty();

var lastPage = collection.GetPage(4, 5).ToArray();

lastPage.ShouldNotBeNull();
lastPage.ShouldNotBeEmpty();
lastPage.Length.ShouldBe(5);
lastPage[0].ShouldBe(21);
lastPage[1].ShouldBe(22);
lastPage[2].ShouldBe(23);
lastPage[3].ShouldBe(24);
lastPage[4].ShouldBe(25);

var nonExistingPage = collection.GetPage(5, 5).ToArray();

nonExistingPage.ShouldNotBeNull();
nonExistingPage.ShouldBeEmpty();
}

[Test]
public void When_converting_an_enumerable_to_a_read_only_collection()
{
Expand Down Expand Up @@ -48,13 +97,39 @@ public void When_converting_an_enumerable_to_a_read_only_collection()
}

[Test]
public void When_randomizing_a_collection()
public void When_selecting_a_random_element_from_a_sequence()
{
var sequence = Enumerable.Range(1, 25).ToList();
sequence.ShouldBe(new[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25});
var collection = Enumerable.Range(1, 50).ToList();
collection.ShouldBe(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50 });
Assert.That(collection, Is.Ordered);

var firstElement = collection.First();
var secondElement = collection.First();

firstElement.ShouldBe(secondElement);

var firstRandomElement = collection.SelectRandom();
var secondRandomElement = collection.SelectRandom();

firstRandomElement.ShouldNotBe(secondRandomElement);

var sequence = collection.Skip(0);
Assert.That(sequence, Is.Ordered);

var randomized = sequence.Randomize().ToList();
firstRandomElement = sequence.SelectRandom();
secondRandomElement = sequence.SelectRandom();

firstRandomElement.ShouldNotBe(secondRandomElement);
}

[Test]
public void When_randomizing_a_collection()
{
var collection = Enumerable.Range(1, 50).ToList();
collection.ShouldBe(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50 });
Assert.That(collection, Is.Ordered);

var randomized = collection.Randomize().ToList();
Assert.That(randomized, Is.Not.Ordered);
}

Expand Down
25 changes: 10 additions & 15 deletions Easy.Common.Tests.Unit/ProducerConsumer/ProducerConsumerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ public void ConsumerWithOneWorkerShouldProcessEveryAddedItems()

10.Times(n => queue.Add(_getData(n)));

Thread.Sleep(100);

queue.ShutdownAsync(TimeSpan.Zero).Wait();
queue.Shutdown(100.Milliseconds()).ShouldBeTrue();
consumed.Count.ShouldBe(10);
exceptionThrown.ShouldBeNull();
}
Expand All @@ -120,11 +118,11 @@ public void BlockingConsumerWithOneWorkerShouldBlockEverything()

10.Times(n => queue.Add(_getData(n)));

Thread.Sleep(100);
Thread.Sleep(100.Milliseconds());

queue.PendingCount.ShouldBe<uint>(9);

queue.ShutdownAsync(TimeSpan.Zero).Wait();
queue.Shutdown(600.Milliseconds()).ShouldBeFalse();
consumed.Count.ShouldBe(0);
exceptionThrown.ShouldBeFalse();
}
Expand Down Expand Up @@ -157,7 +155,7 @@ public void BlockingConsumerWithTwoWorkersShouldBlockOnlyOneItem()
Thread.Sleep(100);

queue.PendingCount.ShouldBe<uint>(0);
queue.ShutdownAsync(TimeSpan.Zero).Wait();
queue.Shutdown(500.Milliseconds()).ShouldBeFalse();
consumed.Count.ShouldBe(9);
exceptionThrown.ShouldBeFalse();
}
Expand Down Expand Up @@ -242,7 +240,7 @@ public void SlowConsumerWithFullCapacityShouldReturnFalseWhenTryingToAddNewItemW
}

[Test]
public async Task DisposedQueueShouldNotAllowAddingNewItems()
public void DisposedQueueShouldNotAllowAddingNewItems()
{
var consumed = new List<MyClass>();
Action<MyClass> consumer = x => { consumed.Add(x); };
Expand All @@ -261,7 +259,7 @@ public async Task DisposedQueueShouldNotAllowAddingNewItems()
queue.Add(_getData(i));
}

await queue.ShutdownAsync(TimeSpan.FromSeconds(5));
queue.Shutdown(TimeSpan.FromSeconds(5)).ShouldBeTrue();

Thread.Sleep(50.Milliseconds());
consumed.Count.ShouldBe(WorkItems);
Expand All @@ -274,7 +272,7 @@ public async Task DisposedQueueShouldNotAllowAddingNewItems()
}

[Test]
public async Task DisposedQueueShouldCancelConsumersCorrectly()
public void DisposedQueueShouldCancelConsumersCorrectly()
{
Exception exceptionThrown = null;
var consumed = new List<int>();
Expand All @@ -296,19 +294,16 @@ public async Task DisposedQueueShouldCancelConsumersCorrectly()
queue.Add(4);
queue.Add(5);

Thread.Sleep(350);
queue.Shutdown(500.Milliseconds()).ShouldBeTrue();

await queue.ShutdownAsync(TimeSpan.Zero);

Thread.Sleep(100);
consumed.Count.ShouldBeGreaterThanOrEqualTo(2);

Thread.Sleep(1.Seconds());
exceptionThrown.ShouldBeNull();
}

[Test]
public async Task ShutdownQueueAfterSomeAddsShouldResultInTheProcessOfAllAddedItemsBeforeDisposal()
public void ShutdownQueueAfterSomeAddsShouldResultInTheProcessOfAllAddedItemsBeforeDisposal()
{
var consumed = new List<MyClass>();

Expand All @@ -323,7 +318,7 @@ public async Task ShutdownQueueAfterSomeAddsShouldResultInTheProcessOfAllAddedIt

5.Times(n => { queue.Add(_getData(n)); });

await queue.ShutdownAsync(10.Seconds());
queue.Shutdown(10.Seconds()).ShouldBeTrue();

Action afterDisposedEnqueues = () => 3.Times(n => { queue.Add(_getData(n)); });
afterDisposedEnqueues.ShouldNotThrow();
Expand Down
28 changes: 27 additions & 1 deletion Easy.Common.Tests.Unit/String/StringExtensionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void When_checking_a_string_is_null_or_empty(string input, bool result)
[TestCase(null, false)]
public void When_checking_a_string_is_not_null_or_empty(string input, bool result)
{
input.IsNotNullOrEmpty().ShouldBe(result);
EnumerableExtensions.IsNotNullOrEmpty(input).ShouldBe(result);
}

[TestCase("abc", false)]
Expand Down Expand Up @@ -379,5 +379,31 @@ public void When_compressing_then_decompressing_string()

compressedContent.Decompress().ShouldBe(Content);
}

[Test]
public void When_checking_a_string_is_a_valid_file_name()
{
"A".IsValidFileName().ShouldBeTrue();
"MyFile".IsValidFileName().ShouldBeTrue();
"MyFile.txt".IsValidFileName().ShouldBeTrue();
"MyFile.txt ".IsValidFileName().ShouldBeTrue();
" MyFile.txt".IsValidFileName().ShouldBeTrue();
"My File.txt".IsValidFileName().ShouldBeTrue();
"My-File.txt".IsValidFileName().ShouldBeTrue();
"My-%File.txt".IsValidFileName().ShouldBeTrue();
"My-!File.txt".IsValidFileName().ShouldBeTrue();

"".IsValidFileName().ShouldBeFalse();
" ".IsValidFileName().ShouldBeFalse();
" ".IsValidFileName().ShouldBeFalse();
"/".IsValidFileName().ShouldBeFalse();
"\\".IsValidFileName().ShouldBeFalse();
"MyFile/".IsValidFileName().ShouldBeFalse();
"\\MyFile".IsValidFileName().ShouldBeFalse();
"MyFile>".IsValidFileName().ShouldBeFalse();
"<MyFile>".IsValidFileName().ShouldBeFalse();
"<MyFile".IsValidFileName().ShouldBeFalse();
"<".IsValidFileName().ShouldBeFalse();
}
}
}
Loading

0 comments on commit 7bbdbe8

Please # to comment.