Skip to content
This repository has been archived by the owner on Oct 4, 2023. It is now read-only.

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
Creating v4.0.0
  • Loading branch information
samaysar committed Sep 15, 2020
2 parents 040c1b9 + 55cc4ef commit c75304c
Show file tree
Hide file tree
Showing 28 changed files with 547 additions and 240 deletions.
4 changes: 2 additions & 2 deletions Dot.Net.DevFast/src/Dot.Net.DevFast.Tests/App.config
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Threading.Tasks.Extensions" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.2.0.0" />
<bindingRedirect oldVersion="0.0.0.0-4.2.0.1" newVersion="4.2.0.1" />
</dependentAssembly>
</assemblyBinding>
</runtime>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ public class AbstractBinaryHeapTest
[Test]
[TestCase(-1)]
[TestCase(int.MinValue)]
public void AbstractBinaryHeap_Ctor_Throws_Error_For_Invalid_Arguments(int capacity)
public void Ctor_Throws_Error_For_Invalid_Arguments(int capacity)
{
var ctorEx = Assert.Throws<TargetInvocationException>(() =>
{
var _ = Substitute.For<AbstractBinaryHeap<int>>(capacity);
}).InnerException as DdnDfException;
var ctorEx =
Assert.Throws<TargetInvocationException>(() => Substitute.For<AbstractBinaryHeap<int>>(capacity))
.InnerException as DdnDfException;
Assert.IsNotNull(ctorEx);
Assert.IsTrue(ctorEx.ErrorCode.Equals(DdnDfErrorCode.ValueLessThanThreshold));
}
Expand All @@ -29,7 +28,7 @@ public void AbstractBinaryHeap_Ctor_Throws_Error_For_Invalid_Arguments(int capac
[TestCase(0)]
[TestCase(1)]
[TestCase(10)]
public void AbstractBinaryHeap_Properties_Are_Well_Defined(int capacity)
public void Properties_Are_Well_Defined(int capacity)
{
IHeap<int> instance = Substitute.For<AbstractBinaryHeap<int>>(capacity);
Assert.True(instance.IsEmpty);
Expand All @@ -40,7 +39,7 @@ public void AbstractBinaryHeap_Properties_Are_Well_Defined(int capacity)
}

[Test]
public void AbstractBinaryHeap_Add_N_Try_Add_Behaves_For_Empty_Heap()
public void Add_N_Try_Add_Behaves_For_Empty_Heap()
{
IHeap<int> instance = new AbstractBinaryTestHeap(0, (x, y) => x < y);
Assert.IsFalse(instance.TryAdd(1));
Expand All @@ -51,7 +50,7 @@ public void AbstractBinaryHeap_Add_N_Try_Add_Behaves_For_Empty_Heap()
}

[Test]
public void AbstractBinaryHeap_Add_N_Try_Add_Behaves_For_Non_Empty_Heap()
public void Add_N_Try_Add_Behaves_For_Non_Empty_Heap()
{
IHeap<int> instance = new AbstractBinaryTestHeap(1, (x, y) => x < y);
Assert.True(instance.IsEmpty);
Expand All @@ -74,15 +73,15 @@ public void AbstractBinaryHeap_Add_N_Try_Add_Behaves_For_Non_Empty_Heap()
[TestCase(0)]
[TestCase(1)]
[TestCase(10)]
public void AbstractBinaryHeap_Peek_N_TryPeek_Behaves_For_Empty_Heap(int capacity)
public void 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()
public void Peek_N_TryPeek_Behaves_For_Non_Empty_Heap()
{
IHeap<int> instance = new AbstractBinaryTestHeap(1, (x, y) => x < y);
instance.Add(1);
Expand All @@ -94,15 +93,15 @@ public void AbstractBinaryHeap_Peek_N_TryPeek_Behaves_For_Non_Empty_Heap()
[TestCase(0)]
[TestCase(1)]
[TestCase(10)]
public void AbstractBinaryHeap_Pop_N_TryPop_Behaves_For_Empty_Heap(int capacity)
public void 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()
public void Pop_N_TryPop_Behaves_For_Non_Empty_Heap()
{
IHeap<int> instance = new AbstractBinaryTestHeap(5, (x, y) => x < y);
instance.Add(3);
Expand All @@ -120,7 +119,7 @@ public void AbstractBinaryHeap_Pop_N_TryPop_Behaves_For_Non_Empty_Heap()
}

[Test]
public void AbstractBinaryHeap_Compact_Behaves()
public void Compact_Behaves()
{
var instance = Substitute.For<AbstractBinaryHeap<int>>(2);
Assert.AreEqual(instance.Capacity, 2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class AbstractSizableBinaryHeapTest
[Test]
[TestCase(-1)]
[TestCase(int.MinValue)]
public void AbstractSizableBinaryHeap_Ctor_Throws_Error_For_Invalid_Capacity(int capacity)
public void Ctor_Throws_Error_For_Invalid_Capacity(int capacity)
{
var ctorEx = Assert.Throws<TargetInvocationException>(() =>
{
Expand All @@ -24,7 +24,7 @@ public void AbstractSizableBinaryHeap_Ctor_Throws_Error_For_Invalid_Capacity(int
}

[Test]
public void AbstractSizableBinaryHeap_Ctor_Throws_Error_For_Missing_Strategy()
public void Ctor_Throws_Error_For_Missing_Strategy()
{
var ctorEx = Assert.Throws<TargetInvocationException>(() =>
{
Expand All @@ -35,7 +35,7 @@ public void AbstractSizableBinaryHeap_Ctor_Throws_Error_For_Missing_Strategy()
}

[Test]
public void AbstractSizableBinaryHeap_Ctor_Properly_Sets_Properties()
public void Ctor_Properly_Sets_Properties()
{
IResizeStrategy strategy = new HeapNoResizing();
var instance = Substitute.For<AbstractSizableBinaryHeap<int>>(0);
Expand All @@ -52,7 +52,7 @@ public void AbstractSizableBinaryHeap_Ctor_Properly_Sets_Properties()
}

[Test]
public void AbstractSizableBinaryHeap_FreezeCapacity_Behaves()
public void FreezeCapacity_Behaves()
{
foreach (var strategy in new IResizeStrategy[]
{
Expand All @@ -73,7 +73,7 @@ public void AbstractSizableBinaryHeap_FreezeCapacity_Behaves()
}

[Test]
public void AbstractSizableBinaryHeap_UseStrategies_Properly()
public void UseStrategies_Properly()
{
var strategy = Substitute.For<IResizeStrategy>();
strategy.TryComputeNewSize(Arg.Any<int>(), out _)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
using System.Threading;
using Dot.Net.DevFast.Collections;
using Dot.Net.DevFast.Collections.Concurrent;
using Dot.Net.DevFast.Collections.Interfaces;
using Dot.Net.DevFast.Etc;
using NSubstitute;
using NUnit.Framework;

namespace Dot.Net.DevFast.Tests.Collections.Concurrent
{
[TestFixture]
public class LockBasedConcurrentHeapTest
{
[Test]
[TestCase(null)]
public void Ctor_Throws_Error_When_Heap_Instance_Is_Null(AbstractSizableBinaryHeap<int> heap)
{
var ex = Assert.Throws<DdnDfException>(() =>
{
var _ = new LockBasedConcurrentHeap<int>(heap);
});
Assert.NotNull(ex);
Assert.IsTrue(ex.ErrorCode.Equals(DdnDfErrorCode.NullObject));
Assert.True(new LockBasedConcurrentHeap<int>(new ConcurrentMinHeap<int>(0)).IsEmpty);
}

[Test]
public void Properties_Are_Accessed_Inside_Lock()
{
var syncRoot = new object();
var heap = Substitute.For<IResizableHeap<int>>();
heap.IsEmpty.Returns(x =>
{
Assert.IsTrue(Monitor.IsEntered(syncRoot));
return true;
});
heap.IsFull.Returns(x =>
{
Assert.IsTrue(Monitor.IsEntered(syncRoot));
return true;
});
heap.Count.Returns(x =>
{
Assert.IsTrue(Monitor.IsEntered(syncRoot));
return 1;
});
heap.CanResize.Returns(x =>
{
Assert.IsTrue(Monitor.IsEntered(syncRoot));
return true;
});
heap.Capacity.Returns(x =>
{
Assert.IsTrue(Monitor.IsEntered(syncRoot));
return 1;
});
var instance = new LockBasedConcurrentHeap<int>(heap, syncRoot);
Assert.True(instance.IsEmpty);
Assert.True(instance.CanResize);
Assert.True(instance.IsFull);
Assert.IsTrue(instance.Count == 1);
Assert.IsTrue(instance.Capacity == 1);
var _ = heap.Received(1).IsFull;
_ = heap.Received(1).IsEmpty;
_ = heap.Received(1).CanResize;
var __ = heap.Received(1).Count;
__ = heap.Received(1).Capacity;
}

[Test]
public void Methods_Are_Accessed_Inside_Lock()
{
var syncRoot = new object();
var heap = Substitute.For<IResizableHeap<int>>();
heap.Peek().Returns(x =>
{
Assert.IsTrue(Monitor.IsEntered(syncRoot));
return 1;
});
heap.TryPeek(out _).Returns(x =>
{
Assert.IsTrue(Monitor.IsEntered(syncRoot));
x[0] = 1;
return true;
});
heap.Pop().Returns(x =>
{
Assert.IsTrue(Monitor.IsEntered(syncRoot));
return 1;
});
heap.TryPop(out _).Returns(x =>
{
Assert.IsTrue(Monitor.IsEntered(syncRoot));
x[0] = 2;
return true;
});
heap.When(x => x.Add(1)).Do(x => Assert.IsTrue(Monitor.IsEntered(syncRoot)));
heap.TryAdd(1).Returns(x =>
{
Assert.IsTrue(Monitor.IsEntered(syncRoot));
return true;
});
heap.When(x => x.Compact()).Do(x => Assert.IsTrue(Monitor.IsEntered(syncRoot)));
heap.When(x => x.FreezeCapacity(true)).Do(x => Assert.IsTrue(Monitor.IsEntered(syncRoot)));

var instance = new LockBasedConcurrentHeap<int>(heap, syncRoot);
Assert.AreEqual(instance.Peek(), 1);
Assert.IsTrue(instance.TryPeek(out var val) && val.Equals(1));
Assert.AreEqual(instance.Pop(), 1);
Assert.IsTrue(instance.TryPop(out var val2) && val2.Equals(2));
instance.Add(1);
Assert.IsTrue(instance.TryAdd(1));
instance.Compact();
instance.FreezeCapacity(true);
heap.Received(1).Pop();
heap.Received(1).TryPop(out _);
heap.Received(1).Peek();
heap.Received(1).TryPeek(out _);
heap.Received(1).Add(1);
heap.Received(1).TryAdd(1);
heap.Received(1).Compact();
heap.Received(1).FreezeCapacity(true);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using Dot.Net.DevFast.Collections.Concurrent;
using Dot.Net.DevFast.Collections.Interfaces;
using Dot.Net.DevFast.Etc;
using NUnit.Framework;

namespace Dot.Net.DevFast.Tests.Collections.Concurrent
{
[TestFixture]
public class MinMaxHeapsTest
{
[Test]
[TestCase(-1)]
[TestCase(-10)]
[TestCase(int.MinValue)]
public void ConcurrentMinHeap_Ctor_Throws_Error_For_Invalid_Capacity(int capacity)
{
var ex = Assert.Throws<DdnDfException>(() =>
{
var _ = new ConcurrentMinHeap<int>(capacity);
});
Assert.NotNull(ex);
Assert.IsTrue(ex.ErrorCode.Equals(DdnDfErrorCode.ValueLessThanThreshold));
}

[Test]
[TestCase(-1)]
[TestCase(-10)]
[TestCase(int.MinValue)]
public void ConcurrentMaxHeap_Ctor_Throws_Error_For_Invalid_Capacity(int capacity)
{
var ex = Assert.Throws<DdnDfException>(() =>
{
var _ = new ConcurrentMaxHeap<int>(capacity);
});
Assert.NotNull(ex);
Assert.IsTrue(ex.ErrorCode.Equals(DdnDfErrorCode.ValueLessThanThreshold));
}

[Test]
public void Ctors_Passes_For_Valid_Params()
{
IHeap<int> instance = new ConcurrentMinHeap<int>(1);
Assert.True(instance.IsEmpty);
Assert.False(instance.IsFull);
instance = new ConcurrentMaxHeap<int>(1);
Assert.True(instance.IsEmpty);
Assert.False(instance.IsFull);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Reflection;
using Dot.Net.DevFast.Collections;
using Dot.Net.DevFast.Etc;
using NUnit.Framework;
Expand Down
Loading

0 comments on commit c75304c

Please sign in to comment.