// Copyright (C) Stichting Deltares 2018. All rights reserved.
//
// This file is part of the Delta Shell Light Library.
//
// The Delta Shell Light Library is free software: you can redistribute it and/or modify
// it under the terms of the GNU 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 General Public License for more details.
//
// You should have received a copy of the GNU 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.IsTrue(result);
result = referenceComparer.Equals(doubleValue2.GetType(), intValue1.GetType());
Assert.IsFalse(result);
var testPoint1 = new ObjectCopierTest.TestPoint();
var testPoint2 = new ObjectCopierTest.TestPoint();
const double testDouble = new double();
result = referenceComparer.Equals(testPoint1.GetType(), testPoint2.GetType());
Assert.IsTrue(result);
result = referenceComparer.Equals(testDouble.GetType(), testPoint1.GetType());
Assert.IsFalse(result);
}
///
/// 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.AreEqual(hashCode2, hashCode3);
Assert.AreNotEqual(hashCode2, hashCode4);
Assert.AreEqual(hashCode2, hashCode5);
// 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 };
var geometryPoint4 = geometryPoint1;
// get hash codes
int hashCode1 = comparer.GetHashCode(geometryPoint1);
hashCode2 = comparer.GetHashCode(geometryPoint2);
hashCode3 = comparer.GetHashCode(geometryPoint3);
hashCode4 = comparer.GetHashCode(geometryPoint4);
// compare
Assert.AreNotEqual(hashCode1, hashCode2);
Assert.AreNotEqual(hashCode1, hashCode3);
Assert.AreEqual(hashCode1, hashCode4);
}
}
}