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.
Creating v4.0.0
- Loading branch information
Showing
28 changed files
with
547 additions
and
240 deletions.
There are no files selected for viewing
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
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
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
125 changes: 125 additions & 0 deletions
125
...t.DevFast/src/Dot.Net.DevFast.Tests/Collections/Concurrent/LockBasedConcurrentHeapTest.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,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); | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
Dot.Net.DevFast/src/Dot.Net.DevFast.Tests/Collections/Concurrent/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,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); | ||
} | ||
} | ||
} |
1 change: 0 additions & 1 deletion
1
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
Oops, something went wrong.