Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/System.Collections/pkg/System.Collections.pkgproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
<ItemGroup>
<ProjectReference Include="..\ref\System.Collections.csproj">
<ProjectReference Include="..\ref\System.Collections.builds">
<SupportedFramework>netcoreapp1.1;net463;$(AllXamarinFrameworks)</SupportedFramework>
</ProjectReference>
<ProjectReference Include="..\src\System.Collections.csproj">
Expand Down
11 changes: 11 additions & 0 deletions src/System.Collections/ref/System.Collections.builds
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
<ItemGroup>
<Project Include="System.Collections.csproj" />
<Project Include="System.Collections.csproj">
<TargetGroup>netcoreapp1.1</TargetGroup>
</Project>
</ItemGroup>
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.traversal.targets))\dir.traversal.targets" />
</Project>
8 changes: 8 additions & 0 deletions src/System.Collections/ref/System.Collections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,10 @@ void System.Collections.ICollection.CopyTo(System.Array array, int index) { }
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return default(System.Collections.IEnumerator); }
public T[] ToArray() { return default(T[]); }
public void TrimExcess() { }
#if netcoreapp11
public bool TryDequeue(out T result) { result = default(T); return default(bool); }
public bool TryPeek(out T result) { result = default(T); return default(bool); }
#endif
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public partial struct Enumerator : System.Collections.Generic.IEnumerator<T>, System.Collections.IEnumerator, System.IDisposable
{
Expand Down Expand Up @@ -609,6 +613,10 @@ void System.Collections.ICollection.CopyTo(System.Array array, int arrayIndex) {
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return default(System.Collections.IEnumerator); }
public T[] ToArray() { return default(T[]); }
public void TrimExcess() { }
#if netcoreapp11
public bool TryPeek(out T result) { result = default(T); return default(bool); }
public bool TryPop(out T result) { result = default(T); return default(bool); }
#endif
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public partial struct Enumerator : System.Collections.Generic.IEnumerator<T>, System.Collections.IEnumerator, System.IDisposable
{
Expand Down
3 changes: 2 additions & 1 deletion src/System.Collections/ref/System.Collections.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
<PropertyGroup>
<AssemblyVersion>4.1.0.0</AssemblyVersion>
<OutputType>Library</OutputType>
<NuGetTargetMoniker>.NETStandard,Version=v1.7</NuGetTargetMoniker>
<NuGetTargetMoniker Condition="'$(TargetGroup)' == ''">.NETStandard,Version=v1.7</NuGetTargetMoniker>
<DefineConstants Condition="'$(TargetGroup)' == 'netcoreapp1.1'">$(DefineConstants);netcoreapp11</DefineConstants>
</PropertyGroup>
<ItemGroup>
<Compile Include="System.Collections.cs" />
Expand Down
7 changes: 2 additions & 5 deletions src/System.Collections/ref/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
"System.Runtime": "4.3.0-beta-24522-03"
},
"frameworks": {
"netstandard1.7": {
"imports": [
"dotnet5.8"
]
}
"netstandard1.7": {},
"netcoreapp1.1": {}
}
}
4 changes: 3 additions & 1 deletion src/System.Collections/src/System.Collections.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'net463_Release|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netstandard1.7_Debug|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netstandard1.7_Release|AnyCPU'" />
<ItemGroup Condition="'$(TargetGroup)' == '' OR '$(TargetGroup)'=='netstandard1.7'">
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netcoreapp1.1_Debug|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netcoreapp1.1_Release|AnyCPU'" />
<ItemGroup Condition="'$(TargetGroup)' == '' OR '$(TargetGroup)'=='netstandard1.7' OR '$(TargetGroup)'=='netcoreapp1.1'">
<ProjectReference Include="..\..\System.Runtime\src\System.Runtime.csproj">
<TargetGroup>netstandard1.7</TargetGroup>
</ProjectReference>
Expand Down
28 changes: 28 additions & 0 deletions src/System.Collections/src/System/Collections/Generic/Queue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,22 @@ public T Dequeue()
return removed;
}

public bool TryDequeue(out T result)
{
if (_size == 0)
{
result = default(T);
return false;
}

result = _array[_head];
_array[_head] = default(T);
MoveNext(ref _head);
_size--;
_version++;
return true;
}

// Returns the object at the head of the queue. The object remains in the
// queue. If the queue is empty, this method throws an
// InvalidOperationException.
Expand All @@ -249,6 +265,18 @@ public T Peek()
return _array[_head];
}

public bool TryPeek(out T result)
{
if (_size == 0)
{
result = default(T);
return false;
}

result = _array[_head];
return true;
}

// Returns true if the queue contains at least one object equal to item.
// Equality is determined using item.Equals().
public bool Contains(T item)
Expand Down
25 changes: 25 additions & 0 deletions src/System.Collections/src/System/Collections/Generic/Stack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,17 @@ public T Peek()
return _array[_size - 1];
}

public bool TryPeek(out T result)
{
if (_size == 0)
{
result = default(T);
return false;
}
result = _array[_size - 1];
return true;
}

// Pops an item from the top of the stack. If the stack is empty, Pop
// throws an InvalidOperationException.
public T Pop()
Expand All @@ -212,6 +223,20 @@ public T Pop()
return item;
}

public bool TryPop(out T result)
{
if (_size == 0)
{
result = default(T);
return false;
}

_version++;
result = _array[--_size];
_array[_size] = default(T); // Free memory quicker.
return true;
}

// Pushes an item to the top of the stack.
public void Push(T item)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace System.Collections.Tests
/// <summary>
/// Contains tests that ensure the correctness of the Queue class.
/// </summary>
public abstract class Queue_Generic_Tests<T> : IGenericSharedAPI_Tests<T>
public abstract partial class Queue_Generic_Tests<T> : IGenericSharedAPI_Tests<T>
{
#region Queue<T> Helper Methods

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using System.Linq;
using Xunit;

namespace System.Collections.Tests
{
public abstract partial class Queue_Generic_Tests<T> : IGenericSharedAPI_Tests<T>
{
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void Queue_Generic_TryDequeue_AllElements(int count)
{
Queue<T> queue = GenericQueueFactory(count);
List<T> elements = queue.ToList();
foreach (T element in elements)
{
T result;
Assert.True(queue.TryDequeue(out result));
Assert.Equal(element, result);
}
}

[Fact]
public void Queue_Generic_TryDequeue_EmptyQueue_ReturnsFalse()
{
T result;
Assert.False(new Queue<T>().TryDequeue(out result));
Assert.Equal(default(T), result);
}

[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void Queue_Generic_TryPeek_AllElements(int count)
{
Queue<T> queue = GenericQueueFactory(count);
List<T> elements = queue.ToList();
foreach (T element in elements)
{
T result;
Assert.True(queue.TryPeek(out result));
Assert.Equal(element, result);

queue.Dequeue();
}
}

[Fact]
public void Queue_Generic_TryPeek_EmptyQueue_ReturnsFalse()
{
T result;
Assert.False(new Queue<T>().TryPeek(out result));
Assert.Equal(default(T), result);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace System.Collections.Tests
/// <summary>
/// Contains tests that ensure the correctness of the Stack class.
/// </summary>
public abstract class Stack_Generic_Tests<T> : IGenericSharedAPI_Tests<T>
public abstract partial class Stack_Generic_Tests<T> : IGenericSharedAPI_Tests<T>
{
#region Stack<T> Helper Methods

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using System.Linq;
using Xunit;

namespace System.Collections.Tests
{
public abstract partial class Stack_Generic_Tests<T> : IGenericSharedAPI_Tests<T>
{
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void Stack_Generic_TryPop_AllElements(int count)
{
Stack<T> stack = GenericStackFactory(count);
List<T> elements = stack.ToList();
foreach (T element in elements)
{
T result;
Assert.True(stack.TryPop(out result));
Assert.Equal(element, result);
}
}

[Fact]
public void Stack_Generic_TryPop_EmptyStack_ReturnsFalse()
{
T result;
Assert.False(new Stack<T>().TryPop(out result));
Assert.Equal(default(T), result);
}

[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void Stack_Generic_TryPeek_AllElements(int count)
{
Stack<T> stack = GenericStackFactory(count);
List<T> elements = stack.ToList();
foreach (T element in elements)
{
T result;
Assert.True(stack.TryPeek(out result));
Assert.Equal(element, result);

stack.Pop();
}
}

[Fact]
public void Stack_Generic_TryPeek_EmptyStack_ReturnsFalse()
{
T result;
Assert.False(new Stack<T>().TryPeek(out result));
Assert.Equal(default(T), result);
}
}
}
4 changes: 4 additions & 0 deletions src/System.Collections/tests/System.Collections.Tests.builds
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
<TargetGroup>netstandard1.7</TargetGroup>
<TestTFMs>netcoreapp1.1</TestTFMs>
</Project>
<Project Include="System.Collections.Tests.csproj">
<TargetGroup>netcoreapp1.1</TargetGroup>
<TestTFMs>netcoreapp1.1</TestTFMs>
</Project>
<Project Include="System.Collections.Tests.csproj">
<TestTFMs>netcore50;net46</TestTFMs>
<OSGroup>Windows_NT</OSGroup>
Expand Down
4 changes: 3 additions & 1 deletion src/System.Collections/tests/System.Collections.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
<Compile Include="Generic\List\List.Generic.Tests.Sort.cs" />
<Compile Include="Generic\Queue\Queue.Generic.cs" />
<Compile Include="Generic\Queue\Queue.Generic.Tests.cs" />
<Compile Include="Generic\Queue\Queue.Generic.Tests.netcoreapp1.1.cs" Condition="'$(TargetGroup)'=='netcoreapp1.1'" />
<Compile Include="Generic\Queue\Queue.Tests.cs" />
<Compile Include="Generic\SortedDictionary\SortedDictionary.Generic.cs" />
<Compile Include="Generic\SortedDictionary\SortedDictionary.Generic.Tests.cs" />
Expand All @@ -144,11 +145,12 @@
<Compile Include="Generic\Stack\Stack.Tests.cs" />
<Compile Include="Generic\Stack\Stack.Generic.cs" />
<Compile Include="Generic\Stack\Stack.Generic.Tests.cs" />
<Compile Include="Generic\Stack\Stack.Generic.Tests.netcoreapp1.1.cs" Condition="'$(TargetGroup)'=='netcoreapp1.1'" />
<Compile Include="$(CommonTestPath)\System\Diagnostics\DebuggerAttributes.cs">
<Link>Common\System\Diagnostics\DebuggerAttributes.cs</Link>
</Compile>
</ItemGroup>
<ItemGroup Condition="'$(TargetGroup)'!='netstandard1.7'">
<ItemGroup Condition="'$(TargetGroup)'!='netstandard1.7' and '$(TargetGroup)'!='netcoreapp1.1'">
<Compile Include="$(CommonPath)\System\SerializableAttribute.cs">
<Link>Common\System\SerializableAttribute.cs</Link>
</Compile>
Expand Down
5 changes: 5 additions & 0 deletions src/System.Collections/tests/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
"dependencies": {
"System.Runtime.Serialization.Formatters": "4.3.0-beta-24522-03"
}
},
"netcoreapp1.1": {
"dependencies": {
"System.Runtime.Serialization.Formatters": "4.3.0-beta-24522-03"
}
}
},
"supports": {
Expand Down