Skip to content

Commit

Permalink
implement ISet and update namespace.
Browse files Browse the repository at this point in the history
  • Loading branch information
Corniel committed Feb 7, 2024
1 parent b2be5cf commit 63ce9fd
Show file tree
Hide file tree
Showing 11 changed files with 336 additions and 246 deletions.
111 changes: 111 additions & 0 deletions specs/Qowaiv.Specs/Collections/Email_address_set_specs.cs
@@ -0,0 +1,111 @@
using Qowaiv.Collections;

namespace Collections.Email_address_set_specs;

public class Appends
{
[Test]
public void new_email_address_in_ascending_order([Range(0, 25)] int index)
{
var emails = Enumerable.Range(0, 26).Select(i => EmailAddress.Parse($"{(char)('A' + i)}@qowaiv.org")).ToArray();
var collection = EmailAddressCollection.New(emails);

IReadOnlyCollection<EmailAddress> appended = collection.Append(EmailAddress.Parse($"{emails[index]}.com"));

appended.Should().HaveCount(collection.Count + 1);
appended.Should().BeInAscendingOrder();
}

[Test]
public void existing_email_address_changes_nothing([Range(0, 25)] int index)
{
var emails = Enumerable.Range(0, 26).Select(i => EmailAddress.Parse($"{(char)('A' + i)}@qowaiv.org")).ToArray();
var collection = EmailAddressCollection.New(emails);

object appended = collection.Append(emails[index]);
appended.Should().Be(collection);
}

[TestCase("")]
[TestCase("?")]
public void changes_nothing_for_empty_or_unknown(EmailAddress notKnown)
{
object appended = Svo.EmailAddressCollection.Append(notKnown);
appended.Should().Be(Svo.EmailAddressCollection);
}
}

public class Implements_ISet
{
private readonly EmailAddressCollection empty = EmailAddressCollection.Empty;
private readonly EmailAddressCollection set = Svo.EmailAddressCollection;
private readonly EmailAddressCollection more = Svo.EmailAddressCollection.Append(EmailAddress.Parse("jimmy@hendrix.com"));
private readonly EmailAddressCollection less = EmailAddressCollection.New(Svo.EmailAddressCollection.Skip(1));
private readonly EmailAddressCollection other = EmailAddressCollection.New(Svo.EmailAddressCollection.Skip(1).Concat([EmailAddress.Parse("jimmy@hendrix.com")]));
private readonly EmailAddressCollection different = EmailAddressCollection.New([EmailAddress.Parse("jimmy@hendrix.com"), EmailAddress.Parse("nick@drake.co.uk")]);

[Test]
public void Is_subset()
{
empty.IsSubsetOf(empty).Should().Be(empty.ToHashSet().IsSubsetOf(empty));
empty.IsSubsetOf(set).Should().Be(empty.ToHashSet().IsSubsetOf(set));

set.IsSubsetOf(set).Should().Be(set.ToHashSet().IsSubsetOf(set));
set.IsSubsetOf(more).Should().Be(set.ToHashSet().IsSubsetOf(more));
set.IsSubsetOf(less).Should().Be(set.ToHashSet().IsSubsetOf(less));
set.IsSubsetOf(other).Should().Be(set.ToHashSet().IsSubsetOf(other));
set.IsSubsetOf(different).Should().Be(set.ToHashSet().IsSubsetOf(different));
}

[Test]
public void Is_proper_subset()
{
empty.IsProperSubsetOf(empty).Should().Be(empty.ToHashSet().IsProperSubsetOf(empty));
empty.IsProperSubsetOf(set).Should().Be(empty.ToHashSet().IsProperSubsetOf(set));

set.IsProperSubsetOf(set).Should().Be(set.ToHashSet().IsProperSubsetOf(set));
set.IsProperSubsetOf(more).Should().Be(set.ToHashSet().IsProperSubsetOf(more));
set.IsProperSubsetOf(less).Should().Be(set.ToHashSet().IsProperSubsetOf(less));
set.IsProperSubsetOf(other).Should().Be(set.ToHashSet().IsProperSubsetOf(other));
set.IsProperSubsetOf(different).Should().Be(set.ToHashSet().IsProperSubsetOf(different));
}

[Test]
public void Is_superset()
{
empty.IsSupersetOf(empty).Should().Be(empty.ToHashSet().IsSupersetOf(empty));
empty.IsSupersetOf(set).Should().Be(empty.ToHashSet().IsSupersetOf(set));

set.IsSupersetOf(set).Should().Be(set.ToHashSet().IsSupersetOf(set));
set.IsSupersetOf(more).Should().Be(set.ToHashSet().IsSupersetOf(more));
set.IsSupersetOf(less).Should().Be(set.ToHashSet().IsSupersetOf(less));
set.IsSupersetOf(other).Should().Be(set.ToHashSet().IsSupersetOf(other));
set.IsSupersetOf(different).Should().Be(set.ToHashSet().IsSupersetOf(different));
}

[Test]
public void Is_proper_superset()
{
empty.IsProperSupersetOf(empty).Should().Be(empty.ToHashSet().IsProperSupersetOf(empty));
empty.IsProperSupersetOf(set).Should().Be(empty.ToHashSet().IsProperSupersetOf(set));

set.IsProperSupersetOf(set).Should().Be(set.ToHashSet().IsProperSupersetOf(set));
set.IsProperSupersetOf(more).Should().Be(set.ToHashSet().IsProperSupersetOf(more));
set.IsProperSupersetOf(less).Should().Be(set.ToHashSet().IsProperSupersetOf(less));
set.IsProperSupersetOf(other).Should().Be(set.ToHashSet().IsProperSupersetOf(other));
set.IsProperSupersetOf(different).Should().Be(set.ToHashSet().IsProperSupersetOf(different));
}

[Test]
public void Overlaps()
{
empty.Overlaps(empty).Should().Be(empty.ToHashSet().Overlaps(empty));
empty.Overlaps(set).Should().Be(empty.ToHashSet().Overlaps(set));

set.Overlaps(set).Should().Be(set.ToHashSet().Overlaps(set));
set.Overlaps(more).Should().Be(set.ToHashSet().Overlaps(more));
set.Overlaps(less).Should().Be(set.ToHashSet().Overlaps(less));
set.Overlaps(other).Should().Be(set.ToHashSet().Overlaps(other));
set.Overlaps(different).Should().Be(set.ToHashSet().Overlaps(different));
}
}
34 changes: 0 additions & 34 deletions specs/Qowaiv.Specs/Email_address_set_specs.cs

This file was deleted.

1 change: 1 addition & 0 deletions specs/Qowaiv.Specs/TestTools/Svo.cs
@@ -1,4 +1,5 @@
using Qowaiv.Chemistry;
using Qowaiv.Collections;
using Qowaiv.Customization;
using Qowaiv.Sustainability;

Expand Down
@@ -1,6 +1,6 @@
namespace Qowaiv;
namespace Qowaiv.Collections;

public partial struct EmailAddressCollection
public readonly partial struct EmailAddressCollection

Check warning on line 3 in src/Qowaiv/Collections/EmailAddressCollection.IReadOnlySet.cs

View workflow job for this annotation

GitHub Actions / Build

When implementing IComparable or IComparable<T>, you should also override <, <=, >, and >=. (https://rules.sonarsource.com/csharp/RSPEC-1210)
#if NET6_0_OR_GREATER
: IReadOnlySet<EmailAddress>
#else
Expand All @@ -20,8 +20,7 @@ public partial struct EmailAddressCollection
[Pure]
public EmailAddressCollection Append(EmailAddress item)
{
if (item.IsEmptyOrUnknown()) return this;
else
if (item.IsKnown)
{
var index = Array.BinarySearch(val, item);

Expand All @@ -41,6 +40,7 @@ public EmailAddressCollection Append(EmailAddress item)
return new(appended);
}
}
else return this;
}

/// <inheritdoc />
Expand All @@ -51,30 +51,36 @@ public bool Contains(EmailAddress item)

/// <inheritdoc />
[Pure]
public bool IsProperSubsetOf(IEnumerable<EmailAddress> other)
=> throw new NotImplementedException();
public bool IsSubsetOf(IEnumerable<EmailAddress> other)
=> Count == 0 || m_Value!.ToHashSet().IsSubsetOf(other);

/// <inheritdoc />
[Pure]
public bool IsProperSupersetOf(IEnumerable<EmailAddress> other)
=> throw new NotImplementedException();
public bool IsProperSubsetOf(IEnumerable<EmailAddress> other)
=> val.ToHashSet().IsProperSubsetOf(other);

/// <inheritdoc />
[Pure]
public bool IsSubsetOf(IEnumerable<EmailAddress> other)
=> throw new NotImplementedException();
public bool IsSupersetOf(IEnumerable<EmailAddress> other)
{
foreach (var element in Guard.NotNull(other))
{
if (!Contains(element)) return false;
}
return true;
}

/// <inheritdoc />
[Pure]
public bool IsSupersetOf(IEnumerable<EmailAddress> other)
=> throw new NotImplementedException();
public bool IsProperSupersetOf(IEnumerable<EmailAddress> other)
=> Count != 0 && m_Value!.ToHashSet().IsProperSupersetOf(other);

/// <inheritdoc />
[Pure]
public bool Overlaps(IEnumerable<EmailAddress> other)
{
var self = this;
return Guard.NotNull(other).Any(self.Contains);
if (Count == 0) return false;
return Guard.NotNull(other).Any(Contains);
}

/// <inheritdoc />
Expand Down

0 comments on commit 63ce9fd

Please sign in to comment.