// 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 System.Collections.Generic;
using Deltares.DamEngine.Data.Standard;
using KellermanSoftware.CompareNetObjects;
using NUnit.Framework;
namespace Deltares.DamEngine.Data.Tests.Standard;
///
/// Tests to check the object deep copier
///
[TestFixture]
public class ObjectCopierTest
{
///
/// Copies the class with different properties in arrays test.
///
[Test]
public void CopyClassWithDifferentPropertiesInArraysTest()
{
// initialise
var differentProperties = new ClassWithDifferentProperties();
var testPointDifferentObjects = new TestPointDifferentObjects();
var compare = new CompareLogic
{
Config =
{
MaxDifferences = 100
}
};
differentProperties.AssignArrayValues();
testPointDifferentObjects.AssignArrayValues();
// copy
ClassWithDifferentProperties differentPropertiesCopy = differentProperties.Copy();
TestPointDifferentObjects testPointDifferentObjectsCopy = testPointDifferentObjects.Copy();
// check if copy is done correctly
ComparisonResult result = compare.Compare(differentProperties, differentPropertiesCopy);
Assert.That(result.Differences.Count, Is.EqualTo(0));
// change double array
differentPropertiesCopy.DoubleArray[0] = 99;
result = compare.Compare(differentProperties, differentPropertiesCopy);
Assert.That(result.Differences.Count, Is.EqualTo(1));
Assert.That(differentPropertiesCopy.DoubleArray[0], Is.Not.EqualTo(differentProperties.DoubleArray[0]));
// change integer array
differentPropertiesCopy.IntArray[0] = 99;
result = compare.Compare(differentProperties, differentPropertiesCopy);
Assert.That(result.Differences.Count, Is.EqualTo(2));
Assert.That(differentPropertiesCopy.IntArray[0], Is.Not.EqualTo(differentProperties.IntArray[0]));
// change string array
differentPropertiesCopy.StrArray[0] = "99";
result = compare.Compare(differentProperties, differentPropertiesCopy);
Assert.That(result.Differences.Count, Is.EqualTo(3));
Assert.That(differentPropertiesCopy.StrArray[0], Is.Not.EqualTo(differentProperties.StrArray[0]));
// change boolean array
differentPropertiesCopy.BoolArray[0] = false;
differentPropertiesCopy.BoolArray[1] = false;
result = compare.Compare(differentProperties, differentPropertiesCopy);
Assert.That(result.Differences.Count, Is.EqualTo(5));
Assert.That(differentPropertiesCopy.BoolArray[0], Is.Not.EqualTo(differentProperties.BoolArray[0]));
Assert.That(differentPropertiesCopy.BoolArray[1], Is.Not.EqualTo(differentProperties.BoolArray[1]));
// change geometryPoint array
testPointDifferentObjectsCopy.TestPointArray[1].X = 99;
testPointDifferentObjectsCopy.TestPointArray[1].Z = 99;
result = compare.Compare(testPointDifferentObjects, testPointDifferentObjectsCopy);
Assert.That(result.Differences.Count, Is.EqualTo(2));
Assert.That(testPointDifferentObjectsCopy.TestPointArray[1].X, Is.Not.EqualTo(testPointDifferentObjects.TestPointArray[1].X));
Assert.That(testPointDifferentObjectsCopy.TestPointArray[1].Z, Is.Not.EqualTo(testPointDifferentObjects.TestPointArray[1].Z));
}
///
/// Copies the class with different properties in lists test.
///
[Test]
public void CopyClassWithDifferentPropertiesInListsTest()
{
// initialise
var differentProperties = new ClassWithDifferentProperties();
var testPointDifferentObjects = new TestPointDifferentObjects();
var compare = new CompareLogic
{
Config =
{
MaxDifferences = 100
}
};
differentProperties.AssignListValues();
testPointDifferentObjects.AssignListValues();
// copy
ClassWithDifferentProperties differentPropertiesCopy = differentProperties.Copy();
TestPointDifferentObjects testPointDifferentObjectsCopy = testPointDifferentObjects.Copy();
// check if copy is done correctly
ComparisonResult result = compare.Compare(differentProperties, differentPropertiesCopy);
Assert.That(result.Differences.Count, Is.EqualTo(0));
// change double list
differentPropertiesCopy.DoubleList[0] = 99;
result = compare.Compare(differentProperties, differentPropertiesCopy);
Assert.That(result.Differences.Count, Is.EqualTo(1));
Assert.That(differentPropertiesCopy.DoubleList[0], Is.Not.EqualTo(differentProperties.DoubleList[0]));
// change integer list
differentPropertiesCopy.IntList[0] = 99;
result = compare.Compare(differentProperties, differentPropertiesCopy);
Assert.That(result.Differences.Count, Is.EqualTo(2));
Assert.That(differentPropertiesCopy.IntList[0], Is.Not.EqualTo(differentProperties.IntList[0]));
// change string list
differentPropertiesCopy.StrList[0] = "99";
result = compare.Compare(differentProperties, differentPropertiesCopy);
Assert.That(result.Differences.Count, Is.EqualTo(3));
Assert.That(differentPropertiesCopy.StrList[0], Is.Not.EqualTo(differentProperties.StrList[0]));
// change boolean list
differentPropertiesCopy.BoolList[0] = false;
differentPropertiesCopy.BoolList[1] = false;
result = compare.Compare(differentProperties, differentPropertiesCopy);
Assert.That(result.Differences.Count, Is.EqualTo(5));
Assert.That(differentPropertiesCopy.BoolList[0], Is.Not.EqualTo(differentProperties.BoolList[0]));
Assert.That(differentPropertiesCopy.BoolList[1], Is.Not.EqualTo(differentProperties.BoolList[1]));
// change geometryPoint list
testPointDifferentObjectsCopy.TestPointList[1].X = 99;
testPointDifferentObjectsCopy.TestPointList[1].Z = 99;
result = compare.Compare(testPointDifferentObjects, testPointDifferentObjectsCopy);
Assert.That(result.Differences.Count, Is.EqualTo(2));
Assert.That(testPointDifferentObjectsCopy.TestPointList[1].X, Is.Not.EqualTo(testPointDifferentObjects.TestPointList[1].X));
Assert.That(testPointDifferentObjectsCopy.TestPointList[1].Z, Is.Not.EqualTo(testPointDifferentObjects.TestPointList[1].Z));
}
///
/// Copies the class with different properties in dictionaries test.
///
[Test]
public void CopyClassWithDifferentPropertiesInDictionariesTest()
{
// initialise
var differentProperties = new ClassWithDifferentProperties();
var testPointDifferentObjects = new TestPointDifferentObjects();
var compare = new CompareLogic
{
Config =
{
MaxDifferences = 100
}
};
differentProperties.AssignDictionaryValues();
testPointDifferentObjects.AssignDictionaryValues();
// copy
ClassWithDifferentProperties differentPropertiesCopy = differentProperties.Copy();
TestPointDifferentObjects testPointDifferentObjectsCopy = testPointDifferentObjects.Copy();
// check if copy is done correctly
ComparisonResult result = compare.Compare(differentProperties, differentPropertiesCopy);
Assert.That(result.Differences.Count, Is.EqualTo(0));
// change double dictionary
differentPropertiesCopy.DoubleDictionary[0] = 99;
result = compare.Compare(differentProperties, differentPropertiesCopy);
Assert.That(result.Differences.Count, Is.EqualTo(1));
Assert.That(differentPropertiesCopy.DoubleDictionary[0], Is.Not.EqualTo(differentProperties.DoubleDictionary[0]));
// change integer dictionary
differentPropertiesCopy.IntDictionary[0] = 99;
result = compare.Compare(differentProperties, differentPropertiesCopy);
Assert.That(result.Differences.Count, Is.EqualTo(2));
Assert.That(differentPropertiesCopy.IntDictionary[0], Is.Not.EqualTo(differentProperties.IntDictionary[0]));
// change string dictionary
differentPropertiesCopy.StrDictionary["0"] = "99";
result = compare.Compare(differentProperties, differentPropertiesCopy);
Assert.That(result.Differences.Count, Is.EqualTo(3));
Assert.That(differentPropertiesCopy.StrDictionary["0"], Is.Not.EqualTo(differentProperties.StrDictionary["0"]));
// change boolean dictionary
differentPropertiesCopy.BoolDictionary[0] = false;
differentPropertiesCopy.BoolDictionary[1] = false;
result = compare.Compare(differentProperties, differentPropertiesCopy);
Assert.That(result.Differences.Count, Is.EqualTo(5));
Assert.That(differentPropertiesCopy.BoolDictionary[0], Is.Not.EqualTo(differentProperties.BoolDictionary[0]));
Assert.That(differentPropertiesCopy.BoolDictionary[1], Is.Not.EqualTo(differentProperties.BoolDictionary[1]));
// change geometryPoint dictionary
testPointDifferentObjectsCopy.TestPointDictionary[1].X = 99;
testPointDifferentObjectsCopy.TestPointDictionary[1].Z = 99;
result = compare.Compare(testPointDifferentObjects, testPointDifferentObjectsCopy);
Assert.That(result.Differences.Count, Is.EqualTo(2));
Assert.That(testPointDifferentObjectsCopy.TestPointDictionary[1].X, Is.Not.EqualTo(testPointDifferentObjects.TestPointDictionary[1].X));
Assert.That(testPointDifferentObjectsCopy.TestPointDictionary[1].Z, Is.Not.EqualTo(testPointDifferentObjects.TestPointDictionary[1].Z));
}
internal class TestPoint
{
public double X;
public double Z;
}
internal class TestPointDifferentObjects
{
public TestPoint[] TestPointArray = new TestPoint[5];
public List TestPointList = new List();
public Dictionary TestPointDictionary = new Dictionary();
///
/// Assigns the array values.
///
public void AssignArrayValues()
{
for (var i = 0; i < 5; i++)
{
var testPoint = new TestPoint();
TestPointArray[i] = testPoint;
TestPointArray[i].X = i;
TestPointArray[i].Z = i + 5;
}
}
///
/// Assigns the list values.
///
public void AssignListValues()
{
for (var i = 0; i < 5; i++)
{
var testPoint = new TestPoint();
TestPointList.Add(testPoint);
TestPointList[i].X = i;
TestPointList[i].Z = i + 5;
}
}
///
/// Assigns the dictionary values.
///
public void AssignDictionaryValues()
{
for (var i = 0; i < 5; i++)
{
var testPoint = new TestPoint();
TestPointDictionary.Add(i, testPoint);
TestPointDictionary[i].X = i;
TestPointDictionary[i].Z = i + 5;
}
}
}
}