Skip to content

Commit

Permalink
Add value equality to ConditionalEffect.
Browse files Browse the repository at this point in the history
  • Loading branch information
homothetyhk committed Mar 2, 2024
1 parent 0018cfb commit 6cbd34d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
10 changes: 10 additions & 0 deletions RandomizerCore/StringItems/StringItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,16 @@ public override IExpression<ItemExpressionType> ToExpression()
if (Negated) logicExpr = ExpressionBuilder.ApplyPrefixOperator(ExpressionBuilder.Op(ItemOperatorProvider.Negation), logicExpr);
return ExpressionBuilder.ApplyInfixOperator(logicExpr, ExpressionBuilder.Op(ItemOperatorProvider.Conditional), Effect.ToExpression());
}

public virtual bool Equals(ConditionalEffect? other)
{
return other is not null && other.Negated == Negated && other.Logic.GetType() == Logic.GetType() && other.Logic.InfixSource == Logic.InfixSource;
}

public override int GetHashCode()
{
return Logic.InfixSource.GetHashCode() * (Negated ? 2 : 1);
}
}

public record ReferenceEffect(LogicItem Item) : StringItemEffect
Expand Down
29 changes: 29 additions & 0 deletions RandomizerCoreTests/StringItemEqualityTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using FluentAssertions;
using RandomizerCore;
using RandomizerCore.Logic;
using RandomizerCore.StringItems;
namespace RandomizerCoreTests
{
public class StringItemEqualityTests
{
[Theory]
[InlineData("_ >|> _")]
[InlineData("`A>1` => A++")]
[InlineData("`A<1 | B<1 | A<2 + B<2` => (`A + B` => (A++ >> B++) >|> `A<1 + B>1` => A += 2 >|> B++)")]
public void ItemEqualsTest(string effect)
{
LogicManagerBuilder lmb = new();
lmb.GetOrAddTerm("A");
lmb.GetOrAddTerm("B");

LogicManager lm = new(lmb);

LogicItem i1 = lm.FromItemString("I", effect);
LogicItem i2 = lm.FromItemString("I", effect);

i1.Should().Be(i2);
i1.GetHashCode().Should().Be(i2.GetHashCode());
}

}
}

0 comments on commit 6cbd34d

Please sign in to comment.