diff --git a/src/System.Collections/pkg/System.Collections.pkgproj b/src/System.Collections/pkg/System.Collections.pkgproj index 634af7c17921..bc12b580c93a 100644 --- a/src/System.Collections/pkg/System.Collections.pkgproj +++ b/src/System.Collections/pkg/System.Collections.pkgproj @@ -2,7 +2,7 @@ - + netcoreapp1.1;net463;$(AllXamarinFrameworks) diff --git a/src/System.Collections/ref/System.Collections.builds b/src/System.Collections/ref/System.Collections.builds new file mode 100644 index 000000000000..36317a999426 --- /dev/null +++ b/src/System.Collections/ref/System.Collections.builds @@ -0,0 +1,11 @@ + + + + + + + netcoreapp1.1 + + + + \ No newline at end of file diff --git a/src/System.Collections/ref/System.Collections.cs b/src/System.Collections/ref/System.Collections.cs index 770031b12c1a..e4991c18787d 100644 --- a/src/System.Collections/ref/System.Collections.cs +++ b/src/System.Collections/ref/System.Collections.cs @@ -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, System.Collections.IEnumerator, System.IDisposable { @@ -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, System.Collections.IEnumerator, System.IDisposable { diff --git a/src/System.Collections/ref/System.Collections.csproj b/src/System.Collections/ref/System.Collections.csproj index f5872159fa19..f4897f93ec4f 100644 --- a/src/System.Collections/ref/System.Collections.csproj +++ b/src/System.Collections/ref/System.Collections.csproj @@ -4,7 +4,8 @@ 4.1.0.0 Library - .NETStandard,Version=v1.7 + .NETStandard,Version=v1.7 + $(DefineConstants);netcoreapp11 diff --git a/src/System.Collections/ref/project.json b/src/System.Collections/ref/project.json index e471440524f9..9497379f2904 100644 --- a/src/System.Collections/ref/project.json +++ b/src/System.Collections/ref/project.json @@ -3,10 +3,7 @@ "System.Runtime": "4.3.0-beta-24522-03" }, "frameworks": { - "netstandard1.7": { - "imports": [ - "dotnet5.8" - ] - } + "netstandard1.7": {}, + "netcoreapp1.1": {} } } diff --git a/src/System.Collections/src/System.Collections.csproj b/src/System.Collections/src/System.Collections.csproj index e74b93964b20..d8dbc6747d0f 100644 --- a/src/System.Collections/src/System.Collections.csproj +++ b/src/System.Collections/src/System.Collections.csproj @@ -15,7 +15,9 @@ - + + + netstandard1.7 diff --git a/src/System.Collections/src/System/Collections/Generic/Queue.cs b/src/System.Collections/src/System/Collections/Generic/Queue.cs index 9afe3c1c0fc6..a207b57c03fd 100644 --- a/src/System.Collections/src/System/Collections/Generic/Queue.cs +++ b/src/System.Collections/src/System/Collections/Generic/Queue.cs @@ -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. @@ -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) diff --git a/src/System.Collections/src/System/Collections/Generic/Stack.cs b/src/System.Collections/src/System/Collections/Generic/Stack.cs index 2c9669e508db..edf3bcdc29bf 100644 --- a/src/System.Collections/src/System/Collections/Generic/Stack.cs +++ b/src/System.Collections/src/System/Collections/Generic/Stack.cs @@ -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() @@ -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) { diff --git a/src/System.Collections/tests/Generic/Queue/Queue.Generic.Tests.cs b/src/System.Collections/tests/Generic/Queue/Queue.Generic.Tests.cs index 8762f72090a6..ab31e9777fb4 100644 --- a/src/System.Collections/tests/Generic/Queue/Queue.Generic.Tests.cs +++ b/src/System.Collections/tests/Generic/Queue/Queue.Generic.Tests.cs @@ -11,7 +11,7 @@ namespace System.Collections.Tests /// /// Contains tests that ensure the correctness of the Queue class. /// - public abstract class Queue_Generic_Tests : IGenericSharedAPI_Tests + public abstract partial class Queue_Generic_Tests : IGenericSharedAPI_Tests { #region Queue Helper Methods diff --git a/src/System.Collections/tests/Generic/Queue/Queue.Generic.Tests.netcoreapp1.1.cs b/src/System.Collections/tests/Generic/Queue/Queue.Generic.Tests.netcoreapp1.1.cs new file mode 100644 index 000000000000..73dfe9454e86 --- /dev/null +++ b/src/System.Collections/tests/Generic/Queue/Queue.Generic.Tests.netcoreapp1.1.cs @@ -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 : IGenericSharedAPI_Tests + { + [Theory] + [MemberData(nameof(ValidCollectionSizes))] + public void Queue_Generic_TryDequeue_AllElements(int count) + { + Queue queue = GenericQueueFactory(count); + List 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().TryDequeue(out result)); + Assert.Equal(default(T), result); + } + + [Theory] + [MemberData(nameof(ValidCollectionSizes))] + public void Queue_Generic_TryPeek_AllElements(int count) + { + Queue queue = GenericQueueFactory(count); + List 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().TryPeek(out result)); + Assert.Equal(default(T), result); + } + } +} diff --git a/src/System.Collections/tests/Generic/Stack/Stack.Generic.Tests.cs b/src/System.Collections/tests/Generic/Stack/Stack.Generic.Tests.cs index eac0a24dd99a..26e586528315 100644 --- a/src/System.Collections/tests/Generic/Stack/Stack.Generic.Tests.cs +++ b/src/System.Collections/tests/Generic/Stack/Stack.Generic.Tests.cs @@ -11,7 +11,7 @@ namespace System.Collections.Tests /// /// Contains tests that ensure the correctness of the Stack class. /// - public abstract class Stack_Generic_Tests : IGenericSharedAPI_Tests + public abstract partial class Stack_Generic_Tests : IGenericSharedAPI_Tests { #region Stack Helper Methods diff --git a/src/System.Collections/tests/Generic/Stack/Stack.Generic.Tests.netcoreapp1.1.cs b/src/System.Collections/tests/Generic/Stack/Stack.Generic.Tests.netcoreapp1.1.cs new file mode 100644 index 000000000000..496270a168f0 --- /dev/null +++ b/src/System.Collections/tests/Generic/Stack/Stack.Generic.Tests.netcoreapp1.1.cs @@ -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 : IGenericSharedAPI_Tests + { + [Theory] + [MemberData(nameof(ValidCollectionSizes))] + public void Stack_Generic_TryPop_AllElements(int count) + { + Stack stack = GenericStackFactory(count); + List 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().TryPop(out result)); + Assert.Equal(default(T), result); + } + + [Theory] + [MemberData(nameof(ValidCollectionSizes))] + public void Stack_Generic_TryPeek_AllElements(int count) + { + Stack stack = GenericStackFactory(count); + List 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().TryPeek(out result)); + Assert.Equal(default(T), result); + } + } +} diff --git a/src/System.Collections/tests/System.Collections.Tests.builds b/src/System.Collections/tests/System.Collections.Tests.builds index 31f6711e522f..ed6e472f638a 100644 --- a/src/System.Collections/tests/System.Collections.Tests.builds +++ b/src/System.Collections/tests/System.Collections.Tests.builds @@ -7,6 +7,10 @@ netstandard1.7 netcoreapp1.1 + + netcoreapp1.1 + netcoreapp1.1 + netcore50;net46 Windows_NT diff --git a/src/System.Collections/tests/System.Collections.Tests.csproj b/src/System.Collections/tests/System.Collections.Tests.csproj index f4261cbb9b7b..e91935dc9e56 100644 --- a/src/System.Collections/tests/System.Collections.Tests.csproj +++ b/src/System.Collections/tests/System.Collections.Tests.csproj @@ -127,6 +127,7 @@ + @@ -144,11 +145,12 @@ + Common\System\Diagnostics\DebuggerAttributes.cs - + Common\System\SerializableAttribute.cs diff --git a/src/System.Collections/tests/project.json b/src/System.Collections/tests/project.json index 06ab44ef707d..e9f39268d9cf 100644 --- a/src/System.Collections/tests/project.json +++ b/src/System.Collections/tests/project.json @@ -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": {