-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
632 additions
and
137 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
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
107 changes: 107 additions & 0 deletions
107
src/DotNext.Tests/Collections/Concurrent/IndexPoolTests.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,107 @@ | ||
namespace DotNext.Collections.Concurrent; | ||
|
||
public sealed class IndexPoolTests : Test | ||
{ | ||
[Fact] | ||
public static void EmptyPool() | ||
{ | ||
var pool = default(IndexPool); | ||
False(pool.TryPeek(out _)); | ||
False(pool.TryTake(out _)); | ||
DoesNotContain(10, pool); | ||
Empty(pool); | ||
} | ||
|
||
[Fact] | ||
public static void TakeAll() | ||
{ | ||
var pool = new IndexPool(); | ||
NotEmpty(pool); | ||
|
||
for (var i = 0; i <= IndexPool.MaxValue; i++) | ||
{ | ||
Equal(i, pool.Take()); | ||
} | ||
|
||
Throws<OverflowException>(() => pool.Take()); | ||
} | ||
|
||
[Fact] | ||
public static void ContainsAll() | ||
{ | ||
var pool = new IndexPool(); | ||
for (var i = 0; i <= IndexPool.MaxValue; i++) | ||
{ | ||
True(pool.Contains(i)); | ||
} | ||
|
||
for (var i = 0; i <= IndexPool.MaxValue; i++) | ||
{ | ||
Equal(i, pool.Take()); | ||
} | ||
|
||
for (var i = 0; i <= IndexPool.MaxValue; i++) | ||
{ | ||
False(pool.Contains(i)); | ||
} | ||
} | ||
|
||
[Fact] | ||
public static void Enumerator() | ||
{ | ||
var pool = new IndexPool(); | ||
var expected = new int[pool.Count]; | ||
Span.ForEach(expected, static (ref int value, int index) => value = index); | ||
|
||
Equal(expected, pool.ToArray()); | ||
|
||
while (pool.TryTake(out _)) | ||
{ | ||
// take all indicies | ||
} | ||
|
||
Equal(Array.Empty<int>(), pool.ToArray()); | ||
} | ||
|
||
[Fact] | ||
public static void CustomMaxValue() | ||
{ | ||
var pool = new IndexPool(maxValue: 2); | ||
Equal(3, pool.Count); | ||
|
||
Equal(0, pool.Take()); | ||
Equal(1, pool.Take()); | ||
Equal(2, pool.Take()); | ||
|
||
False(pool.TryTake(out _)); | ||
Empty(pool); | ||
} | ||
|
||
[Fact] | ||
public static void Consistency() | ||
{ | ||
var pool = new IndexPool(); | ||
Equal(0, pool.Take()); | ||
|
||
Equal(1, pool.Take()); | ||
pool.Return(1); | ||
|
||
Equal(1, pool.Take()); | ||
pool.Return(1); | ||
|
||
pool.Return(0); | ||
} | ||
|
||
[Fact] | ||
public static void TakeReturnMany() | ||
{ | ||
var pool = new IndexPool(); | ||
Span<int> indicies = stackalloc int[IndexPool.Capacity]; | ||
|
||
Equal(IndexPool.Capacity, pool.Take(indicies)); | ||
Empty(pool); | ||
|
||
pool.Return(indicies); | ||
NotEmpty(pool); | ||
} | ||
} |
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
Oops, something went wrong.