This repository has been archived by the owner on Oct 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Releasing v3_2_0-Df
- Loading branch information
Showing
38 changed files
with
1,681 additions
and
467 deletions.
There are no files selected for viewing
131 changes: 131 additions & 0 deletions
131
Dot.Net.DevFast/src/Dot.Net.DevFast.Tests/Collections/AbstractBinaryHeapTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
using System; | ||
using System.Reflection; | ||
using Dot.Net.DevFast.Collections; | ||
using Dot.Net.DevFast.Collections.Interfaces; | ||
using Dot.Net.DevFast.Etc; | ||
using Dot.Net.DevFast.Tests.TestHelpers; | ||
using NSubstitute; | ||
using NUnit.Framework; | ||
|
||
namespace Dot.Net.DevFast.Tests.Collections | ||
{ | ||
[TestFixture] | ||
public class AbstractBinaryHeapTest | ||
{ | ||
[Test] | ||
[TestCase(-1)] | ||
[TestCase(int.MinValue)] | ||
public void AbstractBinaryHeap_Ctor_Throws_Error_For_Invalid_Arguments(int capacity) | ||
{ | ||
var ctorEx = Assert.Throws<TargetInvocationException>(() => | ||
{ | ||
var _ = Substitute.For<AbstractBinaryHeap<int>>(capacity); | ||
}).InnerException as DdnDfException; | ||
Assert.IsNotNull(ctorEx); | ||
Assert.IsTrue(ctorEx.ErrorCode.Equals(DdnDfErrorCode.ValueLessThanThreshold)); | ||
} | ||
|
||
[Test] | ||
[TestCase(0)] | ||
[TestCase(1)] | ||
[TestCase(10)] | ||
public void AbstractBinaryHeap_Properties_Are_Well_Defined(int capacity) | ||
{ | ||
IHeap<int> instance = Substitute.For<AbstractBinaryHeap<int>>(capacity); | ||
Assert.True(instance.IsEmpty); | ||
Assert.AreEqual(instance.Count, 0); | ||
Assert.AreEqual(instance.Capacity, capacity); | ||
if (capacity == 0) Assert.True(instance.IsFull); | ||
else Assert.False(instance.IsFull); | ||
} | ||
|
||
[Test] | ||
public void AbstractBinaryHeap_Add_N_Try_Add_Behaves_For_Empty_Heap() | ||
{ | ||
IHeap<int> instance = new AbstractBinaryTestHeap(0, (x, y) => x < y); | ||
Assert.IsFalse(instance.TryAdd(1)); | ||
var ex = Assert.Throws<DdnDfException>(() => instance.Add(1)); | ||
Assert.NotNull(ex); | ||
Assert.IsTrue(ex.ErrorCode.Equals(DdnDfErrorCode.DemandUnfulfilled)); | ||
Assert.IsTrue(ex.Message.Equals("(DemandUnfulfilled) Unable to add element in the heap.")); | ||
} | ||
|
||
[Test] | ||
public void AbstractBinaryHeap_Add_N_Try_Add_Behaves_For_Non_Empty_Heap() | ||
{ | ||
IHeap<int> instance = new AbstractBinaryTestHeap(1, (x, y) => x < y); | ||
Assert.True(instance.IsEmpty); | ||
Assert.IsTrue(instance.TryAdd(1)); | ||
Assert.IsFalse(instance.TryAdd(1)); | ||
var ex = Assert.Throws<DdnDfException>(() => instance.Add(1)); | ||
Assert.NotNull(ex); | ||
Assert.IsTrue(ex.ErrorCode.Equals(DdnDfErrorCode.DemandUnfulfilled)); | ||
Assert.IsTrue(ex.Message.Equals("(DemandUnfulfilled) Unable to add element in the heap.")); | ||
instance = new AbstractBinaryTestHeap(3, (x, y) => x < y); | ||
instance.Add(3); | ||
Assert.False(instance.IsFull); | ||
instance.Add(2); | ||
Assert.False(instance.IsFull); | ||
Assert.IsTrue(instance.TryAdd(1)); | ||
Assert.True(instance.IsFull); | ||
} | ||
|
||
[Test] | ||
[TestCase(0)] | ||
[TestCase(1)] | ||
[TestCase(10)] | ||
public void AbstractBinaryHeap_Peek_N_TryPeek_Behaves_For_Empty_Heap(int capacity) | ||
{ | ||
IHeap<int> instance = Substitute.For<AbstractBinaryHeap<int>>(capacity); | ||
Assert.Throws<IndexOutOfRangeException>(() => instance.Peek()); | ||
Assert.False(instance.TryPeek(out _)); | ||
} | ||
|
||
[Test] | ||
public void AbstractBinaryHeap_Peek_N_TryPeek_Behaves_For_Non_Empty_Heap() | ||
{ | ||
IHeap<int> instance = new AbstractBinaryTestHeap(1, (x, y) => x < y); | ||
instance.Add(1); | ||
Assert.AreEqual(instance.Peek(), 1); | ||
Assert.True(instance.TryPeek(out var val) && val.Equals(1)); | ||
} | ||
|
||
[Test] | ||
[TestCase(0)] | ||
[TestCase(1)] | ||
[TestCase(10)] | ||
public void AbstractBinaryHeap_Pop_N_TryPop_Behaves_For_Empty_Heap(int capacity) | ||
{ | ||
IHeap<int> instance = Substitute.For<AbstractBinaryHeap<int>>(capacity); | ||
Assert.Throws<IndexOutOfRangeException>(() => instance.Pop()); | ||
Assert.False(instance.TryPop(out _)); | ||
} | ||
|
||
[Test] | ||
public void AbstractBinaryHeap_Pop_N_TryPop_Behaves_For_Non_Empty_Heap() | ||
{ | ||
IHeap<int> instance = new AbstractBinaryTestHeap(5, (x, y) => x < y); | ||
instance.Add(3); | ||
instance.Add(2); | ||
instance.Add(1); | ||
instance.Add(4); | ||
instance.Add(1); | ||
Assert.AreEqual(instance.Pop(), 1); | ||
Assert.AreEqual(instance.Pop(), 1); | ||
Assert.AreEqual(instance.Pop(), 2); | ||
Assert.AreEqual(instance.Pop(), 3); | ||
Assert.AreEqual(instance.Pop(), 4); | ||
Assert.True(instance.IsEmpty); | ||
Assert.IsFalse(instance.TryPop(out _)); | ||
} | ||
|
||
[Test] | ||
public void AbstractBinaryHeap_Compact_Behaves() | ||
{ | ||
var instance = Substitute.For<AbstractBinaryHeap<int>>(2); | ||
Assert.AreEqual(instance.Capacity, 2); | ||
instance.Compact(); | ||
Assert.AreEqual(instance.Capacity, 0); | ||
} | ||
} | ||
} |
106 changes: 106 additions & 0 deletions
106
Dot.Net.DevFast/src/Dot.Net.DevFast.Tests/Collections/AbstractSizableBinaryHeapTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
using System.Reflection; | ||
using Dot.Net.DevFast.Collections; | ||
using Dot.Net.DevFast.Collections.Interfaces; | ||
using Dot.Net.DevFast.Etc; | ||
using NSubstitute; | ||
using NUnit.Framework; | ||
|
||
namespace Dot.Net.DevFast.Tests.Collections | ||
{ | ||
[TestFixture] | ||
public class AbstractSizableBinaryHeapTest | ||
{ | ||
[Test] | ||
[TestCase(-1)] | ||
[TestCase(int.MinValue)] | ||
public void AbstractSizableBinaryHeap_Ctor_Throws_Error_For_Invalid_Capacity(int capacity) | ||
{ | ||
var ctorEx = Assert.Throws<TargetInvocationException>(() => | ||
{ | ||
var _ = Substitute.For<AbstractSizableBinaryHeap<int>>(capacity); | ||
}).InnerException as DdnDfException; | ||
Assert.IsNotNull(ctorEx); | ||
Assert.IsTrue(ctorEx.ErrorCode.Equals(DdnDfErrorCode.ValueLessThanThreshold)); | ||
} | ||
|
||
[Test] | ||
public void AbstractSizableBinaryHeap_Ctor_Throws_Error_For_Missing_Strategy() | ||
{ | ||
var ctorEx = Assert.Throws<TargetInvocationException>(() => | ||
{ | ||
var _ = Substitute.For<AbstractSizableBinaryHeap<int>>(0, null); | ||
}).InnerException as DdnDfException; | ||
Assert.IsNotNull(ctorEx); | ||
Assert.IsTrue(ctorEx.ErrorCode.Equals(DdnDfErrorCode.NullObject)); | ||
} | ||
|
||
[Test] | ||
public void AbstractSizableBinaryHeap_Ctor_Properly_Sets_Properties() | ||
{ | ||
IResizeStrategy strategy = new HeapNoResizing(); | ||
var instance = Substitute.For<AbstractSizableBinaryHeap<int>>(0); | ||
Assert.IsTrue(instance.CanResize.Equals(strategy.CanResize)); | ||
strategy = new HeapNoResizing(); | ||
instance = Substitute.For<AbstractSizableBinaryHeap<int>>(0, strategy); | ||
Assert.IsTrue(instance.CanResize.Equals(strategy.CanResize)); | ||
strategy = new StepHeapResizing(1); | ||
instance = Substitute.For<AbstractSizableBinaryHeap<int>>(0, strategy); | ||
Assert.IsTrue(instance.CanResize.Equals(strategy.CanResize)); | ||
strategy = new PercentHeapResizing(1); | ||
instance = Substitute.For<AbstractSizableBinaryHeap<int>>(0, strategy); | ||
Assert.IsTrue(instance.CanResize.Equals(strategy.CanResize)); | ||
} | ||
|
||
[Test] | ||
public void AbstractSizableBinaryHeap_FreezeCapacity_Behaves() | ||
{ | ||
foreach (var strategy in new IResizeStrategy[] | ||
{ | ||
new HeapNoResizing(), | ||
new StepHeapResizing(1), | ||
new PercentHeapResizing(1) | ||
}) | ||
{ | ||
foreach (var compact in new[] {true, false}) | ||
{ | ||
var instance = Substitute.For<AbstractSizableBinaryHeap<int>>(0, strategy); | ||
Assert.IsTrue(instance.CanResize.Equals(strategy.CanResize)); | ||
instance.FreezeCapacity(compact); | ||
Assert.False(instance.CanResize); | ||
instance.Received(compact ? 1 : 0).Compact(); | ||
} | ||
} | ||
} | ||
|
||
[Test] | ||
public void AbstractSizableBinaryHeap_UseStrategies_Properly() | ||
{ | ||
var strategy = Substitute.For<IResizeStrategy>(); | ||
strategy.TryComputeNewSize(Arg.Any<int>(), out _) | ||
.Returns(x => { | ||
x[1] = 0; | ||
return false; | ||
}); | ||
var instance = Substitute.For<AbstractSizableBinaryHeap<int>>(0, | ||
strategy); | ||
Assert.True(instance.IsFull); | ||
Assert.IsFalse(instance.TryAdd(1)); | ||
strategy.Received(1).TryComputeNewSize(0, out _); | ||
|
||
strategy = new StepHeapResizing(1); | ||
instance = Substitute.For<AbstractSizableBinaryHeap<int>>(1, strategy); | ||
Assert.AreEqual(instance.Count, 0); | ||
Assert.False(instance.IsFull); | ||
Assert.IsTrue(instance.TryAdd(1)); | ||
Assert.True(instance.IsFull); | ||
Assert.AreEqual(instance.Count, 1); | ||
Assert.IsTrue(instance.TryAdd(1)); | ||
Assert.True(instance.IsFull); | ||
Assert.AreEqual(instance.Count, 2); | ||
instance.FreezeCapacity(false); | ||
Assert.IsFalse(instance.TryAdd(1)); | ||
Assert.True(instance.IsFull); | ||
Assert.AreEqual(instance.Count, 2); | ||
} | ||
} | ||
} |
146 changes: 146 additions & 0 deletions
146
Dot.Net.DevFast/src/Dot.Net.DevFast.Tests/Collections/MinMaxHeapsTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
using System; | ||
using System.Reflection; | ||
using Dot.Net.DevFast.Collections; | ||
using Dot.Net.DevFast.Etc; | ||
using NUnit.Framework; | ||
|
||
namespace Dot.Net.DevFast.Tests.Collections | ||
{ | ||
[TestFixture] | ||
public class MinMaxHeapsTest | ||
{ | ||
[Test] | ||
[TestCase(-1)] | ||
[TestCase(int.MinValue)] | ||
public void MinHeap_Ctor_Throws_Error_For_Invalid_Arguments(int capacity) | ||
{ | ||
var ctorEx = Assert.Throws<DdnDfException>(() => | ||
{ | ||
var _ = new MinHeap<int>(capacity); | ||
}); | ||
Assert.IsNotNull(ctorEx); | ||
Assert.IsTrue(ctorEx.ErrorCode.Equals(DdnDfErrorCode.ValueLessThanThreshold)); | ||
} | ||
|
||
[Test] | ||
public void MinHeap_Ctor_Do_Not_Uses_Resizing_By_Default() | ||
{ | ||
var instance = new MinHeap<int>(0); | ||
Assert.False(instance.CanResize); | ||
} | ||
|
||
[Test] | ||
[TestCase(10)] | ||
[TestCase(10000)] | ||
public void MinHeap_Maintains_Ascending_Sorting_Order(int count) | ||
{ | ||
var instance = new MinHeap<byte>(count); | ||
Assert.False(instance.CanResize); | ||
var input = new byte[count]; | ||
new Random().NextBytes(input); | ||
foreach (var val in input) | ||
{ | ||
Assert.True(instance.TryAdd(val)); | ||
} | ||
Assert.True(instance.IsFull); | ||
Array.Sort(input); | ||
foreach (var next in input) | ||
{ | ||
Assert.IsTrue(instance.TryPop(out var val) && | ||
val.Equals(next)); | ||
} | ||
Assert.True(instance.IsEmpty); | ||
} | ||
|
||
[Test] | ||
[TestCase(10)] | ||
[TestCase(10000)] | ||
public void MinHeap_Maintains_Ascending_Sorting_Order_Input_Sorted(int count) | ||
{ | ||
var instance = new MinHeap<byte>(count); | ||
Assert.False(instance.CanResize); | ||
var input = new byte[count]; | ||
new Random().NextBytes(input); | ||
Array.Sort(input); | ||
foreach (var val in input) | ||
{ | ||
Assert.True(instance.TryAdd(val)); | ||
} | ||
Assert.True(instance.IsFull); | ||
foreach (var next in input) | ||
{ | ||
Assert.IsTrue(instance.TryPop(out var val) && | ||
val.Equals(next)); | ||
} | ||
Assert.True(instance.IsEmpty); | ||
} | ||
|
||
[Test] | ||
[TestCase(-1)] | ||
[TestCase(int.MinValue)] | ||
public void MaxHeap_Ctor_Throws_Error_For_Invalid_Arguments(int capacity) | ||
{ | ||
var ctorEx = Assert.Throws<DdnDfException>(() => | ||
{ | ||
var _ = new MaxHeap<int>(capacity); | ||
}); | ||
Assert.IsNotNull(ctorEx); | ||
Assert.IsTrue(ctorEx.ErrorCode.Equals(DdnDfErrorCode.ValueLessThanThreshold)); | ||
} | ||
|
||
[Test] | ||
public void MaxHeap_Ctor_Do_Not_Uses_Resizing_By_Default() | ||
{ | ||
var instance = new MaxHeap<int>(0); | ||
Assert.False(instance.CanResize); | ||
} | ||
|
||
[Test] | ||
[TestCase(10)] | ||
[TestCase(10000)] | ||
public void MaxHeap_Maintains_Descending_Sorting_Order(int count) | ||
{ | ||
var instance = new MaxHeap<byte>(count); | ||
Assert.False(instance.CanResize); | ||
var input = new byte[count]; | ||
new Random().NextBytes(input); | ||
foreach (var val in input) | ||
{ | ||
Assert.True(instance.TryAdd(val)); | ||
} | ||
Assert.True(instance.IsFull); | ||
Array.Sort(input); | ||
Array.Reverse(input); | ||
foreach (var next in input) | ||
{ | ||
Assert.IsTrue(instance.TryPop(out var val) && | ||
val.Equals(next)); | ||
} | ||
Assert.True(instance.IsEmpty); | ||
} | ||
|
||
[Test] | ||
[TestCase(10)] | ||
[TestCase(10000)] | ||
public void MaxHeap_Maintains_Descending_Sorting_Order_Input_Sorted(int count) | ||
{ | ||
var instance = new MaxHeap<byte>(count); | ||
Assert.False(instance.CanResize); | ||
var input = new byte[count]; | ||
new Random().NextBytes(input); | ||
Array.Sort(input); | ||
Array.Reverse(input); | ||
foreach (var val in input) | ||
{ | ||
Assert.True(instance.TryAdd(val)); | ||
} | ||
Assert.True(instance.IsFull); | ||
foreach (var next in input) | ||
{ | ||
Assert.IsTrue(instance.TryPop(out var val) && | ||
val.Equals(next)); | ||
} | ||
Assert.True(instance.IsEmpty); | ||
} | ||
} | ||
} |
Oops, something went wrong.