// Copyright (C) Stichting Deltares 2025. All rights reserved.
//
// This file is part of the Dam Engine.
//
// The Dam Engine is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see .
//
// All names, logos, and references to "Deltares" are registered trademarks of
// Stichting Deltares and remain full property of Stichting Deltares at all times.
// All rights reserved.
using Deltares.DamEngine.Data.Standard;
using NUnit.Framework;
namespace Deltares.DamEngine.Data.Tests.Standard;
///
/// Tests to check the reference equality comparer
///
[TestFixture]
public class ReferenceEqualityComparerTest
{
///
/// Test for object comparer.
///
[Test]
public void EqualsTest()
{
var referenceComparer = new ReferenceEqualityComparer();
const double doubleValue1 = 1;
const double doubleValue2 = 5;
const int intValue1 = 1;
bool result = referenceComparer.Equals(doubleValue1.GetType(), doubleValue2.GetType());
Assert.That(result, Is.True);
result = referenceComparer.Equals(doubleValue2.GetType(), intValue1.GetType());
Assert.That(result, Is.False);
var testPoint1 = new ObjectCopierTest.TestPoint();
var testPoint2 = new ObjectCopierTest.TestPoint();
const double testDouble = new double();
result = referenceComparer.Equals(testPoint1.GetType(), testPoint2.GetType());
Assert.That(result, Is.True);
result = referenceComparer.Equals(testDouble.GetType(), testPoint1.GetType());
Assert.That(result, Is.False);
}
///
/// Test for GetHashCode.
///
[Test]
public void GetHashCodeTest()
{
// initialise constants
var comparer = new ReferenceEqualityComparer();
const double doubleValue1 = 1;
const double doubleValue2 = doubleValue1;
const double doubleValue3 = doubleValue1;
const double doubleValue4 = 2;
const double doubleValue5 = 1;
// get hash codes
int hashCode2 = comparer.GetHashCode(doubleValue2);
int hashCode3 = comparer.GetHashCode(doubleValue3);
int hashCode4 = comparer.GetHashCode(doubleValue4);
int hashCode5 = comparer.GetHashCode(doubleValue5);
// compare
Assert.That(hashCode3, Is.EqualTo(hashCode2));
Assert.That(hashCode4, Is.Not.EqualTo(hashCode2));
Assert.That(hashCode5, Is.EqualTo(hashCode2));
// initialise geometry points
var geometryPoint1 = new ObjectCopierTest.TestPoint
{
X = 1,
Z = 1
};
var geometryPoint2 = new ObjectCopierTest.TestPoint
{
X = 1,
Z = 1
};
var geometryPoint3 = new ObjectCopierTest.TestPoint
{
X = 1,
Z = 99
};
ObjectCopierTest.TestPoint geometryPoint4 = geometryPoint1;
// get hash codes
int hashCode1 = comparer.GetHashCode(geometryPoint1);
hashCode2 = comparer.GetHashCode(geometryPoint2);
hashCode3 = comparer.GetHashCode(geometryPoint3);
hashCode4 = comparer.GetHashCode(geometryPoint4);
// compare
Assert.That(hashCode2, Is.Not.EqualTo(hashCode1));
Assert.That(hashCode3, Is.Not.EqualTo(hashCode1));
Assert.That(hashCode4, Is.EqualTo(hashCode1));
}
}