Skip to content

Commit f712a03

Browse files
committed
implement IComparisonOperators and IEqualityOperators
1 parent cdeb21b commit f712a03

File tree

6 files changed

+52
-0
lines changed

6 files changed

+52
-0
lines changed

src/Smdn.Fundamental.Uuid/Smdn.Formats.UniversallyUniqueIdentifiers/Node.IComparable.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@ namespace Smdn.Formats.UniversallyUniqueIdentifiers;
77
#pragma warning disable IDE0040
88
partial struct Node :
99
#pragma warning restore IDE0040
10+
#if FEATURE_GENERIC_MATH
11+
IComparisonOperators<Node, Node>
12+
#else
1013
IComparable<Node>,
1114
IComparable
15+
#endif
1216
{
1317
public static bool operator <(Node x, Node y) => x.CompareTo(y) < 0;
1418
public static bool operator <=(Node x, Node y) => x.CompareTo(y) <= 0;

src/Smdn.Fundamental.Uuid/Smdn.Formats.UniversallyUniqueIdentifiers/Node.IEquatable.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ namespace Smdn.Formats.UniversallyUniqueIdentifiers;
77
#pragma warning disable IDE0040
88
partial struct Node :
99
#pragma warning restore IDE0040
10+
#if FEATURE_GENERIC_MATH
11+
IEqualityOperators<Node, Node>
12+
#else
1013
IEquatable<Node>
14+
#endif
1115
{
1216
public static bool operator ==(Node x, Node y) => x.Equals(y);
1317
public static bool operator !=(Node x, Node y) => !x.Equals(y);

src/Smdn.Fundamental.Uuid/Smdn.Fundamental.Uuid.csproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ SPDX-License-Identifier: MIT
2626
<PackageReference Include="Microsoft.Bcl.HashCode" Version="1.1.1" Condition="($(TargetFramework.StartsWith('net46')) and '$(TargetFramework)' != 'net46') or $(TargetFramework.StartsWith('netstandard2.0'))" />
2727
</ItemGroup>
2828

29+
<PropertyGroup Condition="$([MSBuild]::VersionGreaterThanOrEquals('$(NETCoreSdkVersion)', '7.0.0'))">
30+
<TargetFrameworks>net7.0;$(TargetFrameworks)</TargetFrameworks>
31+
<DefineConstants Condition="$(TargetFramework.StartsWith('net7'))">FEATURE_GENERIC_MATH;$(DefineConstants)</DefineConstants>
32+
<EnablePreviewFeatures Condition="$(TargetFramework.StartsWith('net7'))">true</EnablePreviewFeatures>
33+
</PropertyGroup>
34+
35+
<ItemGroup Condition="$([MSBuild]::VersionGreaterThanOrEquals('$(NETCoreSdkVersion)', '7.0.0'))">
36+
<PackageReference Include="System.Runtime.Experimental" Version="7.0.0-preview.2.22152.2" Condition="$(TargetFramework.StartsWith('net7'))" />
37+
</ItemGroup>
38+
2939
<Target Name="AddExtraDefineConstants" AfterTargets="AddNETSdkApiDefineConstants">
3040
<PropertyGroup>
3141
<DefineConstants Condition="

tests/Smdn.Fundamental.Uuid/Smdn.Formats.UniversallyUniqueIdentifiers/Node.IComparable.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ public void TestOpLessThan_OpLessThanOrEqual(string x, string y, bool lessThan,
2424
{
2525
Assert.AreEqual(lessThan, Node.Parse(x) < Node.Parse(y), "op <");
2626
Assert.AreEqual(lessThanOrEqual, Node.Parse(x) <= Node.Parse(y), "op <=");
27+
28+
#if FEATURE_GENERIC_MATH
29+
Assert.AreEqual(lessThan, OpLessThan(Node.Parse(x), Node.Parse(y)), "IComparisonOperators <");
30+
Assert.AreEqual(lessThanOrEqual, OpLessThanOrEqual(Node.Parse(x), Node.Parse(y)), "IComparisonOperators <=");
31+
32+
static bool OpLessThan<T>(T x, T y) where T : IComparisonOperators<T, T> => x < y;
33+
static bool OpLessThanOrEqual<T>(T x, T y) where T : IComparisonOperators<T, T> => x <= y;
34+
#endif
2735
}
2836

2937
[TestCase("00:00:00:00:00:00", "00:00:00:00:00:00", false, true)]
@@ -44,6 +52,14 @@ public void TestOpGreaterThan_OpGreaterThanOrEqual(string x, string y, bool grea
4452
{
4553
Assert.AreEqual(greaterThan, Node.Parse(x) > Node.Parse(y), "op >");
4654
Assert.AreEqual(greaterThanOrEqual, Node.Parse(x) >= Node.Parse(y), "op >=");
55+
56+
#if FEATURE_GENERIC_MATH
57+
Assert.AreEqual(greaterThan, OpGreaterThan(Node.Parse(x), Node.Parse(y)), "IComparisonOperators >");
58+
Assert.AreEqual(greaterThanOrEqual, OpGreaterThanOrEqual(Node.Parse(x), Node.Parse(y)), "IComparisonOperators >=");
59+
60+
static bool OpGreaterThan<T>(T x, T y) where T : IComparisonOperators<T, T> => x > y;
61+
static bool OpGreaterThanOrEqual<T>(T x, T y) where T : IComparisonOperators<T, T> => x >= y;
62+
#endif
4763
}
4864

4965
[TestCase("00:00:00:00:00:00", "00:00:00:00:00:00", 0)]

tests/Smdn.Fundamental.Uuid/Smdn.Formats.UniversallyUniqueIdentifiers/Node.IEquatable.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ public void TestOpEquality_OpInequality(string x, string y, bool isEqual)
2424
{
2525
Assert.AreEqual(isEqual, Node.Parse(x) == Node.Parse(y), "op ==");
2626
Assert.AreEqual(!isEqual, Node.Parse(x) != Node.Parse(y), "op !=");
27+
28+
#if FEATURE_GENERIC_MATH
29+
Assert.AreEqual(isEqual, OpEquality(Node.Parse(x), Node.Parse(y)), "IEqualityOperators ==");
30+
Assert.AreEqual(!isEqual, OpInequality(Node.Parse(x), Node.Parse(y)), "IEqualityOperators !=");
31+
32+
static bool OpEquality<T>(T x, T y) where T : IEqualityOperators<T, T> => x == y;
33+
static bool OpInequality<T>(T x, T y) where T : IEqualityOperators<T, T> => x != y;
34+
#endif
2735
}
2836

2937
[TestCase("00:00:00:00:00:00", "00:00:00:00:00:00", true)]

tests/Smdn.Fundamental.Uuid/Smdn.Fundamental.Uuid.Tests.csproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,14 @@ SPDX-License-Identifier: MIT
77
<TargetFrameworks>net6.0;net5.0;net48;net461;net45;netcoreapp3.1</TargetFrameworks>
88
<RootNamespace>Smdn</RootNamespace>
99
</PropertyGroup>
10+
11+
<PropertyGroup Condition="$([MSBuild]::VersionGreaterThanOrEquals('$(NETCoreSdkVersion)', '7.0.0'))">
12+
<TargetFrameworks>net7.0;$(TargetFrameworks)</TargetFrameworks>
13+
<DefineConstants Condition="$(TargetFramework.StartsWith('net7'))">FEATURE_GENERIC_MATH;$(DefineConstants)</DefineConstants>
14+
<EnablePreviewFeatures Condition="$(TargetFramework.StartsWith('net7'))">true</EnablePreviewFeatures>
15+
</PropertyGroup>
16+
17+
<ItemGroup Condition="$([MSBuild]::VersionGreaterThanOrEquals('$(NETCoreSdkVersion)', '7.0.0'))">
18+
<PackageReference Include="System.Runtime.Experimental" Version="7.0.0-preview.2.22152.2" Condition="$(TargetFramework.StartsWith('net7'))" />
19+
</ItemGroup>
1020
</Project>

0 commit comments

Comments
 (0)