-
-
Notifications
You must be signed in to change notification settings - Fork 4
Add polyfills for Queue<T>.TryDequeue(...) and Queue<T>.TryPeek(...)
#48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,101 @@ | ||
| using System.Collections.Generic; | ||
| using FluentAssertions; | ||
| using Xunit; | ||
|
|
||
| namespace PolyShim.Tests.NetCore20; | ||
|
|
||
| public class QueueTests | ||
| { | ||
| [Fact] | ||
| public void TryDequeue_WithElements_Test() | ||
| { | ||
| // Arrange | ||
| var queue = new Queue<int>(); | ||
| queue.Enqueue(1); | ||
| queue.Enqueue(2); | ||
| queue.Enqueue(3); | ||
|
|
||
| // Act | ||
| var result1 = queue.TryDequeue(out var value1); | ||
| var result2 = queue.TryDequeue(out var value2); | ||
| var result3 = queue.TryDequeue(out var value3); | ||
|
|
||
| // Assert | ||
| result1.Should().BeTrue(); | ||
| value1.Should().Be(1); | ||
| result2.Should().BeTrue(); | ||
| value2.Should().Be(2); | ||
| result3.Should().BeTrue(); | ||
| value3.Should().Be(3); | ||
| queue.Should().BeEmpty(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TryDequeue_EmptyQueue_Test() | ||
| { | ||
| // Arrange | ||
| var queue = new Queue<int>(); | ||
|
|
||
| // Act | ||
| var result = queue.TryDequeue(out var value); | ||
|
|
||
| // Assert | ||
| result.Should().BeFalse(); | ||
| value.Should().Be(0); | ||
| queue.Should().BeEmpty(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TryPeek_WithElements_Test() | ||
| { | ||
| // Arrange | ||
| var queue = new Queue<int>(); | ||
| queue.Enqueue(1); | ||
| queue.Enqueue(2); | ||
| queue.Enqueue(3); | ||
|
|
||
| // Act | ||
| var result1 = queue.TryPeek(out var value1); | ||
| var result2 = queue.TryPeek(out var value2); | ||
|
|
||
| // Assert | ||
| result1.Should().BeTrue(); | ||
| value1.Should().Be(1); | ||
| result2.Should().BeTrue(); | ||
| value2.Should().Be(1); | ||
| queue.Should().HaveCount(3); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TryPeek_EmptyQueue_Test() | ||
| { | ||
| // Arrange | ||
| var queue = new Queue<int>(); | ||
|
|
||
| // Act | ||
| var result = queue.TryPeek(out var value); | ||
|
|
||
| // Assert | ||
| result.Should().BeFalse(); | ||
| value.Should().Be(0); | ||
| queue.Should().BeEmpty(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TryPeek_AfterDequeue_Test() | ||
| { | ||
| // Arrange | ||
| var queue = new Queue<int>(); | ||
| queue.Enqueue(1); | ||
| queue.Enqueue(2); | ||
|
|
||
| // Act | ||
| queue.TryDequeue(out _); | ||
| var result = queue.TryPeek(out var value); | ||
|
|
||
| // Assert | ||
| result.Should().BeTrue(); | ||
| value.Should().Be(2); | ||
| queue.Should().HaveCount(1); | ||
| } | ||
| } |
This file contains hidden or 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,41 @@ | ||
| #if (NETCOREAPP && !NETCOREAPP2_0_OR_GREATER) || (NETFRAMEWORK) || (NETSTANDARD && !NETSTANDARD2_1_OR_GREATER) | ||
| #nullable enable | ||
| // ReSharper disable RedundantUsingDirective | ||
| // ReSharper disable CheckNamespace | ||
| // ReSharper disable InconsistentNaming | ||
| // ReSharper disable PartialTypeWithSinglePart | ||
|
|
||
| using System.Collections.Generic; | ||
|
|
||
| internal static partial class PolyfillExtensions | ||
| { | ||
| extension<T>(Queue<T> queue) | ||
| { | ||
| // https://learn.microsoft.com/dotnet/api/system.collections.generic.queue-1.trydequeue | ||
| public bool TryDequeue(out T? result) | ||
| { | ||
| if (queue.Count > 0) | ||
| { | ||
| result = queue.Dequeue(); | ||
| return true; | ||
| } | ||
|
|
||
| result = default; | ||
| return false; | ||
| } | ||
|
|
||
| // https://learn.microsoft.com/dotnet/api/system.collections.generic.queue-1.trypeek | ||
| public bool TryPeek(out T? result) | ||
| { | ||
| if (queue.Count > 0) | ||
| { | ||
| result = queue.Peek(); | ||
| return true; | ||
| } | ||
|
|
||
| result = default; | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
| #endif | ||
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.