// 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 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 { 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 (int 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 (int 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 (int i = 0; i < 5; i++) { var testPoint = new TestPoint(); TestPointDictionary.Add(i, testPoint); TestPointDictionary[i].X = i; TestPointDictionary[i].Z = i + 5; } } } /// /// 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 var result = compare.Compare(differentProperties, differentPropertiesCopy); Assert.AreEqual(0, result.Differences.Count); // change double array differentPropertiesCopy.DoubleArray[0] = 99; result = compare.Compare(differentProperties, differentPropertiesCopy); Assert.AreEqual(1, result.Differences.Count); Assert.AreNotEqual(differentProperties.DoubleArray[0], differentPropertiesCopy.DoubleArray[0]); // change integer array differentPropertiesCopy.IntArray[0] = 99; result = compare.Compare(differentProperties, differentPropertiesCopy); Assert.AreEqual(2, result.Differences.Count); Assert.AreNotEqual(differentProperties.IntArray[0], differentPropertiesCopy.IntArray[0]); // change string array differentPropertiesCopy.StrArray[0] = "99"; result = compare.Compare(differentProperties, differentPropertiesCopy); Assert.AreEqual(3, result.Differences.Count); Assert.AreNotEqual(differentProperties.StrArray[0], differentPropertiesCopy.StrArray[0]); // change boolean array differentPropertiesCopy.BoolArray[0] = false; differentPropertiesCopy.BoolArray[1] = false; result = compare.Compare(differentProperties, differentPropertiesCopy); Assert.AreEqual(5, result.Differences.Count); Assert.AreNotEqual(differentProperties.BoolArray[0], differentPropertiesCopy.BoolArray[0]); Assert.AreNotEqual(differentProperties.BoolArray[1], differentPropertiesCopy.BoolArray[1]); // change geometryPoint array testPointDifferentObjectsCopy.TestPointArray[1].X = 99; testPointDifferentObjectsCopy.TestPointArray[1].Z = 99; result = compare.Compare(testPointDifferentObjects, testPointDifferentObjectsCopy); Assert.AreEqual(2, result.Differences.Count); Assert.AreNotEqual(testPointDifferentObjects.TestPointArray[1].X, testPointDifferentObjectsCopy.TestPointArray[1].X); Assert.AreNotEqual(testPointDifferentObjects.TestPointArray[1].Z, testPointDifferentObjectsCopy.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 var result = compare.Compare(differentProperties, differentPropertiesCopy); Assert.AreEqual(0, result.Differences.Count); // change double list differentPropertiesCopy.DoubleList[0] = 99; result = compare.Compare(differentProperties, differentPropertiesCopy); Assert.AreEqual(1, result.Differences.Count); Assert.AreNotEqual(differentProperties.DoubleList[0], differentPropertiesCopy.DoubleList[0]); // change integer list differentPropertiesCopy.IntList[0] = 99; result = compare.Compare(differentProperties, differentPropertiesCopy); Assert.AreEqual(2, result.Differences.Count); Assert.AreNotEqual(differentProperties.IntList[0], differentPropertiesCopy.IntList[0]); // change string list differentPropertiesCopy.StrList[0] = "99"; result = compare.Compare(differentProperties, differentPropertiesCopy); Assert.AreEqual(3, result.Differences.Count); Assert.AreNotEqual(differentProperties.StrList[0], differentPropertiesCopy.StrList[0]); // change boolean list differentPropertiesCopy.BoolList[0] = false; differentPropertiesCopy.BoolList[1] = false; result = compare.Compare(differentProperties, differentPropertiesCopy); Assert.AreEqual(5, result.Differences.Count); Assert.AreNotEqual(differentProperties.BoolList[0], differentPropertiesCopy.BoolList[0]); Assert.AreNotEqual(differentProperties.BoolList[1], differentPropertiesCopy.BoolList[1]); // change geometryPoint list testPointDifferentObjectsCopy.TestPointList[1].X = 99; testPointDifferentObjectsCopy.TestPointList[1].Z = 99; result = compare.Compare(testPointDifferentObjects, testPointDifferentObjectsCopy); Assert.AreEqual(2, result.Differences.Count); Assert.AreNotEqual(testPointDifferentObjects.TestPointList[1].X, testPointDifferentObjectsCopy.TestPointList[1].X); Assert.AreNotEqual(testPointDifferentObjects.TestPointList[1].Z, testPointDifferentObjectsCopy.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 var result = compare.Compare(differentProperties, differentPropertiesCopy); Assert.AreEqual(0, result.Differences.Count); // change double dictionary differentPropertiesCopy.DoubleDictionary[0] = 99; result = compare.Compare(differentProperties, differentPropertiesCopy); Assert.AreEqual(1, result.Differences.Count); Assert.AreNotEqual(differentProperties.DoubleDictionary[0], differentPropertiesCopy.DoubleDictionary[0]); // change integer dictionary differentPropertiesCopy.IntDictionary[0] = 99; result = compare.Compare(differentProperties, differentPropertiesCopy); Assert.AreEqual(2, result.Differences.Count); Assert.AreNotEqual(differentProperties.IntDictionary[0], differentPropertiesCopy.IntDictionary[0]); // change string dictionary differentPropertiesCopy.StrDictionary["0"] = "99"; result = compare.Compare(differentProperties, differentPropertiesCopy); Assert.AreEqual(3, result.Differences.Count); Assert.AreNotEqual(differentProperties.StrDictionary["0"], differentPropertiesCopy.StrDictionary["0"]); // change boolean dictionary differentPropertiesCopy.BoolDictionary[0] = false; differentPropertiesCopy.BoolDictionary[1] = false; result = compare.Compare(differentProperties, differentPropertiesCopy); Assert.AreEqual(5, result.Differences.Count); Assert.AreNotEqual(differentProperties.BoolDictionary[0], differentPropertiesCopy.BoolDictionary[0]); Assert.AreNotEqual(differentProperties.BoolDictionary[1], differentPropertiesCopy.BoolDictionary[1]); // change geometryPoint dictionary testPointDifferentObjectsCopy.TestPointDictionary[1].X = 99; testPointDifferentObjectsCopy.TestPointDictionary[1].Z = 99; result = compare.Compare(testPointDifferentObjects, testPointDifferentObjectsCopy); Assert.AreEqual(2, result.Differences.Count); Assert.AreNotEqual(testPointDifferentObjects.TestPointDictionary[1].X, testPointDifferentObjectsCopy.TestPointDictionary[1].X); Assert.AreNotEqual(testPointDifferentObjects.TestPointDictionary[1].Z, testPointDifferentObjectsCopy.TestPointDictionary[1].Z); } } }