Skip to content

Commit

Permalink
Lifetime: new equality model for Eternal and default values
Browse files Browse the repository at this point in the history
  • Loading branch information
ForNeVeR committed Jan 28, 2025
1 parent f55dda7 commit 51f7fd5
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 17 deletions.
9 changes: 7 additions & 2 deletions rd-net/Lifetimes/Lifetimes/Lifetime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1165,7 +1165,7 @@ [PublicAPI] public Lifetime CreateTerminatedAfter(TimeSpan timeSpan, TaskSchedul

public static bool operator ==(Lifetime left, Lifetime right)
{
return ReferenceEquals(left.Definition, right.Definition);
return left.Equals(right);
}

public static bool operator !=(Lifetime left, Lifetime right)
Expand All @@ -1175,6 +1175,11 @@ [PublicAPI] public Lifetime CreateTerminatedAfter(TimeSpan timeSpan, TaskSchedul

public bool Equals(Lifetime other)
{
if (LogErrorIfLifetimeIsNotInitialized)
{
return ReferenceEquals(myDefinition, other.myDefinition);
}

return ReferenceEquals(Definition, other.Definition);
}

Expand All @@ -1186,7 +1191,7 @@ public override bool Equals(object? obj)

public override int GetHashCode()
{
return Definition.GetHashCode();
return myDefinition?.GetHashCode() ?? 0;
}

#endregion
Expand Down
72 changes: 57 additions & 15 deletions rd-net/Test.Lifetimes/Lifetimes/LifetimeTest.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Core;
Expand Down Expand Up @@ -152,24 +151,59 @@ public void TestEternal()
[Test]
public void TestEquals()
{
var old = Lifetime.LogErrorIfLifetimeIsNotInitialized;
Lifetime.LogErrorIfLifetimeIsNotInitialized = false;
var oldValue = Lifetime.LogErrorIfLifetimeIsNotInitialized;
try
{
Lifetime eternal = default;
Assert.AreEqual(Lifetime.Eternal, eternal);
Assert.AreEqual(Lifetime.Eternal, Lifetime.Eternal);
Assert.AreEqual(eternal, eternal);

Assert.True(Lifetime.Eternal == eternal);
Lifetime.LogErrorIfLifetimeIsNotInitialized = false;
DoChecks(false);

Assert.AreNotEqual(Lifetime.Eternal, Lifetime.Terminated);
Assert.False(Lifetime.Eternal == Lifetime.Terminated);
Assert.False(eternal == Lifetime.Terminated);
Lifetime.LogErrorIfLifetimeIsNotInitialized = true;
DoChecks(true);
}
finally
{
Lifetime.LogErrorIfLifetimeIsNotInitialized = old;
Lifetime.LogErrorIfLifetimeIsNotInitialized = oldValue;
}

[SuppressMessage("ReSharper", "EqualExpressionComparison")]
void DoChecks(bool newDefaultBehaviorFlag)
{
Assert.AreEqual(Lifetime.LogErrorIfLifetimeIsNotInitialized, newDefaultBehaviorFlag);

Lifetime defaultLifetime = default;

// Checks that are always true:
Assert.AreEqual(defaultLifetime, defaultLifetime);
Assert.AreEqual(Lifetime.Eternal, Lifetime.Eternal);
Assert.AreEqual(Lifetime.Terminated, Lifetime.Terminated);
Assert.AreNotEqual(Lifetime.Eternal, Lifetime.Terminated);
Assert.AreNotEqual(defaultLifetime, Lifetime.Terminated);

Assert.True(defaultLifetime == defaultLifetime);

Check warning on line 182 in rd-net/Test.Lifetimes/Lifetimes/LifetimeTest.cs

View workflow job for this annotation

GitHub Actions / build (macos-13)

Comparison made to same variable; did you mean to compare something else?

Check warning on line 182 in rd-net/Test.Lifetimes/Lifetimes/LifetimeTest.cs

View workflow job for this annotation

GitHub Actions / build (macos-13)

Comparison made to same variable; did you mean to compare something else?

Check warning on line 182 in rd-net/Test.Lifetimes/Lifetimes/LifetimeTest.cs

View workflow job for this annotation

GitHub Actions / build (macos-13)

Comparison made to same variable; did you mean to compare something else?

Check warning on line 182 in rd-net/Test.Lifetimes/Lifetimes/LifetimeTest.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04)

Comparison made to same variable; did you mean to compare something else?

Check warning on line 182 in rd-net/Test.Lifetimes/Lifetimes/LifetimeTest.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04)

Comparison made to same variable; did you mean to compare something else?

Check warning on line 182 in rd-net/Test.Lifetimes/Lifetimes/LifetimeTest.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04)

Comparison made to same variable; did you mean to compare something else?

Check warning on line 182 in rd-net/Test.Lifetimes/Lifetimes/LifetimeTest.cs

View workflow job for this annotation

GitHub Actions / build (windows-2022)

Comparison made to same variable; did you mean to compare something else?

Check warning on line 182 in rd-net/Test.Lifetimes/Lifetimes/LifetimeTest.cs

View workflow job for this annotation

GitHub Actions / build (windows-2022)

Comparison made to same variable; did you mean to compare something else?

Check warning on line 182 in rd-net/Test.Lifetimes/Lifetimes/LifetimeTest.cs

View workflow job for this annotation

GitHub Actions / build (windows-2022)

Comparison made to same variable; did you mean to compare something else?
Assert.True(Lifetime.Eternal == Lifetime.Eternal);
Assert.True(Lifetime.Terminated == Lifetime.Terminated);
Assert.False(Lifetime.Eternal == Lifetime.Terminated);
Assert.False(defaultLifetime == Lifetime.Terminated);

Assert.False(defaultLifetime != defaultLifetime);

Check warning on line 188 in rd-net/Test.Lifetimes/Lifetimes/LifetimeTest.cs

View workflow job for this annotation

GitHub Actions / build (macos-13)

Comparison made to same variable; did you mean to compare something else?

Check warning on line 188 in rd-net/Test.Lifetimes/Lifetimes/LifetimeTest.cs

View workflow job for this annotation

GitHub Actions / build (macos-13)

Comparison made to same variable; did you mean to compare something else?

Check warning on line 188 in rd-net/Test.Lifetimes/Lifetimes/LifetimeTest.cs

View workflow job for this annotation

GitHub Actions / build (macos-13)

Comparison made to same variable; did you mean to compare something else?

Check warning on line 188 in rd-net/Test.Lifetimes/Lifetimes/LifetimeTest.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04)

Comparison made to same variable; did you mean to compare something else?

Check warning on line 188 in rd-net/Test.Lifetimes/Lifetimes/LifetimeTest.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04)

Comparison made to same variable; did you mean to compare something else?

Check warning on line 188 in rd-net/Test.Lifetimes/Lifetimes/LifetimeTest.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04)

Comparison made to same variable; did you mean to compare something else?

Check warning on line 188 in rd-net/Test.Lifetimes/Lifetimes/LifetimeTest.cs

View workflow job for this annotation

GitHub Actions / build (windows-2022)

Comparison made to same variable; did you mean to compare something else?

Check warning on line 188 in rd-net/Test.Lifetimes/Lifetimes/LifetimeTest.cs

View workflow job for this annotation

GitHub Actions / build (windows-2022)

Comparison made to same variable; did you mean to compare something else?

Check warning on line 188 in rd-net/Test.Lifetimes/Lifetimes/LifetimeTest.cs

View workflow job for this annotation

GitHub Actions / build (windows-2022)

Comparison made to same variable; did you mean to compare something else?
Assert.False(Lifetime.Eternal != Lifetime.Eternal);
Assert.False(Lifetime.Terminated != Lifetime.Terminated);
Assert.True(Lifetime.Eternal != Lifetime.Terminated);
Assert.True(defaultLifetime != Lifetime.Terminated);

// Checks depending on the state of the flag:
if (newDefaultBehaviorFlag)
{
Assert.AreNotEqual(defaultLifetime, Lifetime.Eternal);
Assert.False(defaultLifetime == Lifetime.Eternal);
Assert.True(defaultLifetime != Lifetime.Eternal);
}
else
{
Assert.AreEqual(defaultLifetime, Lifetime.Eternal);
Assert.True(defaultLifetime == Lifetime.Eternal);
Assert.False(defaultLifetime != Lifetime.Eternal);
}
}
}

Expand All @@ -179,6 +213,14 @@ public void TestTerminated()
Assert.True(Lifetime.Terminated.Status == LifetimeStatus.Terminated);
}

[Test]
public void TestGetHashCode()
{
_ = Lifetime.Eternal.GetHashCode();
_ = Lifetime.Terminated.GetHashCode();
_ = ((Lifetime)default).GetHashCode();
}


[Test]
public void TestRecursiveTermination()
Expand Down Expand Up @@ -1147,7 +1189,7 @@ public void T020_DefineNestedOrder()

Assert.IsFalse(flag, "Nested closed twice.");

CollectionAssert.AreEqual(System.Linq.Enumerable.Range(0, entries.Count).Reverse().ToArray(), entries, "Order FAIL.");
CollectionAssert.AreEqual(Enumerable.Range(0, entries.Count).Reverse().ToArray(), entries, "Order FAIL.");

}

Expand Down

0 comments on commit 51f7fd5

Please sign in to comment.