Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
37 changes: 25 additions & 12 deletions src/Foundation/NSMutableSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,22 @@

using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;

// Disable until we get around to enable + fix any issues.
#nullable disable
#nullable enable

namespace Foundation {

public partial class NSMutableSet : IEnumerable<NSObject> {
/// <param name="objs">To be added.</param>
/// <summary>To be added.</summary>
/// <remarks>To be added.</remarks>
/// <summary>Initializes a new mutable set with the specified objects.</summary>
/// <param name="objs">The objects to add to the set.</param>
public NSMutableSet (params NSObject [] objs)
: this (NSArray.FromNSObjects (objs))
{
}

/// <param name="strings">To be added.</param>
/// <summary>To be added.</summary>
/// <remarks>To be added.</remarks>
/// <summary>Initializes a new mutable set with the specified strings.</summary>
/// <param name="strings">The strings to add to the set.</param>
public NSMutableSet (params string [] strings)
: this (NSArray.FromStrings (strings))
{
Expand All @@ -57,10 +55,18 @@ internal NSMutableSet (params INativeObject [] objs)
{
}

public static NSMutableSet operator + (NSMutableSet first, NSMutableSet second)
/// <summary>Creates a new mutable set containing all objects from both sets.</summary>
/// <param name="first">The first set.</param>
/// <param name="second">The second set.</param>
/// <returns>A new <see cref="NSMutableSet" /> containing the union of both sets, or <see langword="null" /> if both sets are <see langword="null" />.</returns>
[return: NotNullIfNotNull (nameof (first))]
[return: NotNullIfNotNull (nameof (second))]
public static NSMutableSet? operator + (NSMutableSet? first, NSMutableSet? second)
{
if (first is null && second is null)
return null;
if (first is null || first.Count == 0)
return new NSMutableSet (second);
return second is null ? new NSMutableSet () : new NSMutableSet (second);
if (second is null || second.Count == 0)
return new NSMutableSet (first);

Expand All @@ -69,10 +75,17 @@ internal NSMutableSet (params INativeObject [] objs)
return copy;
}

public static NSMutableSet operator - (NSMutableSet first, NSMutableSet second)
/// <summary>Creates a new mutable set with objects from the first set that are not in the second set.</summary>
/// <param name="first">The first set.</param>
/// <param name="second">The second set.</param>
/// <returns>A new <see cref="NSMutableSet" /> containing the difference, or <see langword="null" /> if the first set is <see langword="null" />.</returns>
[return: NotNullIfNotNull (nameof (first))]
public static NSMutableSet? operator - (NSMutableSet? first, NSMutableSet? second)
{
if (first is null || first.Count == 0)
if (first is null)
return null;
if (first.Count == 0)
return new NSMutableSet ();
if (second is null || second.Count == 0)
return new NSMutableSet (first);

Expand Down
2 changes: 0 additions & 2 deletions tests/cecil-tests/Documentation.KnownFailures.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11859,8 +11859,6 @@ M:Foundation.NSMutableOrderedSet`1.op_Addition(Foundation.NSMutableOrderedSet{`0
M:Foundation.NSMutableOrderedSet`1.op_Subtraction(Foundation.NSMutableOrderedSet{`0},Foundation.NSMutableOrderedSet{`0})
M:Foundation.NSMutableOrderedSet`1.op_Subtraction(Foundation.NSMutableOrderedSet{`0},Foundation.NSOrderedSet{`0})
M:Foundation.NSMutableOrderedSet`1.op_Subtraction(Foundation.NSMutableOrderedSet{`0},Foundation.NSSet{`0})
M:Foundation.NSMutableSet.op_Addition(Foundation.NSMutableSet,Foundation.NSMutableSet)
M:Foundation.NSMutableSet.op_Subtraction(Foundation.NSMutableSet,Foundation.NSMutableSet)
M:Foundation.NSMutableSet`1.op_Addition(Foundation.NSMutableSet{`0},Foundation.NSMutableSet{`0})
M:Foundation.NSMutableSet`1.op_Subtraction(Foundation.NSMutableSet{`0},Foundation.NSMutableSet{`0})
M:Foundation.NSNetService.add_AddressResolved(System.EventHandler)
Expand Down
222 changes: 222 additions & 0 deletions tests/monotouch-test/Foundation/NSMutableSetTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,227 @@ public void OperatorPlusReferenceTest ()
Assert.AreNotEqual (IntPtr.Zero, one.Handle, "Handle must be != IntPtr.Zero");
Assert.AreNotEqual (IntPtr.Zero, two.Handle, "Handle must be != IntPtr.Zero");
}

[Test]
public void OperatorAdd_BothNull ()
{
NSMutableSet first = null;
NSMutableSet second = null;
var result = first + second;
Assert.IsNull (result, "BothNull should return null");
}

[Test]
public void OperatorAdd_FirstNull_SecondNonEmpty ()
{
NSMutableSet first = null;
using (var second = new NSMutableSet ("1", "2"))
using (var result = first + second) {
Assert.IsNotNull (result, "FirstNull should return new set");
Assert.AreEqual ((nuint) 2, result.Count, "FirstNull Count");
Assert.IsTrue (result.Contains ("1"), "FirstNull Contains 1");
Assert.IsTrue (result.Contains ("2"), "FirstNull Contains 2");
}
}

[Test]
public void OperatorAdd_FirstNull_SecondEmpty ()
{
NSMutableSet first = null;
using (var second = new NSMutableSet ())
using (var result = first + second) {
Assert.IsNotNull (result, "FirstNull SecondEmpty should return new set");
Assert.AreEqual ((nuint) 0, result.Count, "FirstNull SecondEmpty Count");
}
}

[Test]
public void OperatorAdd_FirstNonEmpty_SecondNull ()
{
using (var first = new NSMutableSet ("1", "2"))
using (var result = first + null) {
Assert.IsNotNull (result, "SecondNull should return new set");
Assert.AreEqual ((nuint) 2, result.Count, "SecondNull Count");
Assert.IsTrue (result.Contains ("1"), "SecondNull Contains 1");
Assert.IsTrue (result.Contains ("2"), "SecondNull Contains 2");
}
}

[Test]
public void OperatorAdd_FirstEmpty_SecondNull ()
{
using (var first = new NSMutableSet ())
using (var result = first + null) {
Assert.IsNotNull (result, "FirstEmpty SecondNull should return new set");
Assert.AreEqual ((nuint) 0, result.Count, "FirstEmpty SecondNull Count");
}
}

[Test]
public void OperatorAdd_FirstEmpty_SecondNonEmpty ()
{
using (var first = new NSMutableSet ())
using (var second = new NSMutableSet ("1", "2"))
using (var result = first + second) {
Assert.IsNotNull (result, "FirstEmpty should return copy of second");
Assert.AreEqual ((nuint) 2, result.Count, "FirstEmpty Count");
Assert.IsTrue (result.Contains ("1"), "FirstEmpty Contains 1");
Assert.IsTrue (result.Contains ("2"), "FirstEmpty Contains 2");
}
}

[Test]
public void OperatorAdd_FirstNonEmpty_SecondEmpty ()
{
using (var first = new NSMutableSet ("1", "2"))
using (var second = new NSMutableSet ())
using (var result = first + second) {
Assert.IsNotNull (result, "SecondEmpty should return copy of first");
Assert.AreEqual ((nuint) 2, result.Count, "SecondEmpty Count");
Assert.IsTrue (result.Contains ("1"), "SecondEmpty Contains 1");
Assert.IsTrue (result.Contains ("2"), "SecondEmpty Contains 2");
}
}

[Test]
public void OperatorAdd_BothEmpty ()
{
using (var first = new NSMutableSet ())
using (var second = new NSMutableSet ())
using (var result = first + second) {
Assert.IsNotNull (result, "BothEmpty should return new empty set");
Assert.AreEqual ((nuint) 0, result.Count, "BothEmpty Count");
}
}

[Test]
public void OperatorAdd_WithOverlappingElements ()
{
using (var first = new NSMutableSet ("1", "2", "3"))
using (var second = new NSMutableSet ("2", "3", "4"))
using (var result = first + second) {
Assert.IsNotNull (result, "Overlapping should return new set");
Assert.AreEqual ((nuint) 4, result.Count, "Overlapping Count");
Assert.IsTrue (result.Contains ("1"), "Overlapping Contains 1");
Assert.IsTrue (result.Contains ("2"), "Overlapping Contains 2");
Assert.IsTrue (result.Contains ("3"), "Overlapping Contains 3");
Assert.IsTrue (result.Contains ("4"), "Overlapping Contains 4");
}
}

[Test]
public void OperatorSubtract_FirstNull ()
{
NSMutableSet first = null;
using (var second = new NSMutableSet ("1", "2")) {
var result = first - second;
Assert.IsNull (result, "FirstNull should return null");
}
}

[Test]
public void OperatorSubtract_SecondNull ()
{
using (var first = new NSMutableSet ("1", "2"))
using (var result = first - null) {
Assert.IsNotNull (result, "SecondNull should return copy of first");
Assert.AreEqual ((nuint) 2, result.Count, "SecondNull Count");
Assert.IsTrue (result.Contains ("1"), "SecondNull Contains 1");
Assert.IsTrue (result.Contains ("2"), "SecondNull Contains 2");
}
}

[Test]
public void OperatorSubtract_BothNull ()
{
NSMutableSet first = null;
NSMutableSet second = null;
var result = first - second;
Assert.IsNull (result, "BothNull should return null");
}

[Test]
public void OperatorSubtract_FirstEmpty ()
{
using (var first = new NSMutableSet ())
using (var second = new NSMutableSet ("1", "2"))
using (var result = first - second) {
Assert.IsNotNull (result, "FirstEmpty should return empty set");
Assert.AreEqual ((nuint) 0, result.Count, "FirstEmpty Count");
}
}

[Test]
public void OperatorSubtract_SecondEmpty ()
{
using (var first = new NSMutableSet ("1", "2"))
using (var second = new NSMutableSet ())
using (var result = first - second) {
Assert.IsNotNull (result, "SecondEmpty should return copy of first");
Assert.AreEqual ((nuint) 2, result.Count, "SecondEmpty Count");
Assert.IsTrue (result.Contains ("1"), "SecondEmpty Contains 1");
Assert.IsTrue (result.Contains ("2"), "SecondEmpty Contains 2");
}
}

[Test]
public void OperatorSubtract_BothEmpty ()
{
using (var first = new NSMutableSet ())
using (var second = new NSMutableSet ())
using (var result = first - second) {
Assert.IsNotNull (result, "BothEmpty should return empty set");
Assert.AreEqual ((nuint) 0, result.Count, "BothEmpty Count");
}
}

[Test]
public void OperatorSubtract_NoOverlap ()
{
using (var first = new NSMutableSet ("1", "2"))
using (var second = new NSMutableSet ("3", "4"))
using (var result = first - second) {
Assert.IsNotNull (result, "NoOverlap should return copy of first");
Assert.AreEqual ((nuint) 2, result.Count, "NoOverlap Count");
Assert.IsTrue (result.Contains ("1"), "NoOverlap Contains 1");
Assert.IsTrue (result.Contains ("2"), "NoOverlap Contains 2");
}
}

[Test]
public void OperatorSubtract_PartialOverlap ()
{
using (var first = new NSMutableSet ("1", "2", "3"))
using (var second = new NSMutableSet ("2", "3", "4"))
using (var result = first - second) {
Assert.IsNotNull (result, "PartialOverlap should return difference");
Assert.AreEqual ((nuint) 1, result.Count, "PartialOverlap Count");
Assert.IsTrue (result.Contains ("1"), "PartialOverlap Contains 1");
Assert.IsFalse (result.Contains ("2"), "PartialOverlap Not Contains 2");
Assert.IsFalse (result.Contains ("3"), "PartialOverlap Not Contains 3");
}
}

[Test]
public void OperatorSubtract_CompleteOverlap ()
{
using (var first = new NSMutableSet ("1", "2", "3"))
using (var second = new NSMutableSet ("1", "2", "3"))
using (var result = first - second) {
Assert.IsNotNull (result, "CompleteOverlap should return empty set");
Assert.AreEqual ((nuint) 0, result.Count, "CompleteOverlap Count");
}
}

[Test]
public void OperatorSubtract_SecondIsSupersetOfFirst ()
{
using (var first = new NSMutableSet ("1", "2"))
using (var second = new NSMutableSet ("1", "2", "3", "4"))
using (var result = first - second) {
Assert.IsNotNull (result, "Superset should return empty set");
Assert.AreEqual ((nuint) 0, result.Count, "Superset Count");
}
}
}
}
Loading