Skip to content

Commit

Permalink
Started alignment to Base_oM
Browse files Browse the repository at this point in the history
  • Loading branch information
alelom committed Jan 26, 2024
1 parent 38cd237 commit 4ab738c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 64 deletions.
90 changes: 28 additions & 62 deletions DiffingTests/DiffingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public void IDiffing_DiffWithHash()
// Test that the IDiffing() calls the eneric diffing method `DiffWithHash()` when:
// - the input objects do not have any ID assigned;
// - the input lists have different length.
var asd = BH.Engine.Geometry.Query.GeometryHash(default(IGeometry));

DiffingConfig diffingConfig = new DiffingConfig();

Expand Down Expand Up @@ -92,37 +93,6 @@ public void IDiffing_DiffWithHash()
Assert.IsTrue(!objectDifferences?.Differences?.Any() ?? true, "HashDiffing cannot return property Differences, but some were returned.");
}

/// <summary>
/// Tests the resistance of the Diffing() method against null IEnumerables set in the DiffingConfig.ComparisonConfig.
/// </summary>
[Test]
public void Diffing_NullIEnumerablesPropertiesInComparisonConfig()
{
// Diffing() is the private method called by DiffWithCustomIds(), DiffWithCustomDataKeyId() and DiffWithFragmentId().

// Generate some randomObjects and assign the same ID fragment, with a progressive ID, to first/second batch.
int totalObjectCount = 3;
List<IBHoMObject> firstBatch = BH.Engine.Diffing.Tests.Create.RandomBHoMObjects(typeof(Bar), totalObjectCount, true, true);
List<IBHoMObject> secondBatch = BH.Engine.Diffing.Tests.Create.RandomBHoMObjects(typeof(Bar), totalObjectCount, true, true);
List<string> allObjIds = Enumerable.Range(0, totalObjectCount).Select(i => i.ToString()).ToList();

DiffingConfig diffingConfig = new DiffingConfig();
diffingConfig.ComparisonConfig = new ComparisonConfig();

// Deliberately set the ComparisonConfig options that are IEnumerables to null.
diffingConfig.ComparisonConfig.CustomdataKeysExceptions = null;
diffingConfig.ComparisonConfig.CustomdataKeysToConsider = null;
diffingConfig.ComparisonConfig.PropertiesToConsider = null;
diffingConfig.ComparisonConfig.PropertyExceptions = null;
diffingConfig.ComparisonConfig.NamespaceExceptions = null;
diffingConfig.ComparisonConfig.PropertyNumericTolerances = null;
diffingConfig.ComparisonConfig.PropertySignificantFigures = null;

// Compute diffing.
// This should fail if there is no guard against null IEnumerables.
Diff diff = BH.Engine.Diffing.Compute.DiffWithCustomIds(firstBatch.OfType<object>().ToList(), allObjIds, secondBatch.OfType<object>().ToList(), allObjIds, diffingConfig);
}

[Test]
public void DiffWithFragmentId_allModifiedObjects()
{
Expand Down Expand Up @@ -313,10 +283,9 @@ public void ObjectDifferences_PropertiesToConsider_NonExistingPropertyToConsider
Bar bar1 = Engine.Diffing.Tests.Create.RandomObject<Bar>();
Bar bar2 = Engine.Diffing.Tests.Create.RandomObject<Bar>();

ComparisonConfig cc = new ComparisonConfig()
{
PropertiesToConsider = new List<string>() { "SomeRandomNotExistingPropertyName" }
};
ComparisonConfig cc = new ComparisonConfig(
propertiesToConsider: new HashSet<string>() { "SomeRandomNotExistingPropertyName" }
);

ObjectDifferences objectDifferences = BH.Engine.Diffing.Query.ObjectDifferences(bar1, bar2, cc);

Expand Down Expand Up @@ -347,7 +316,7 @@ public void ObjectDifferences_PropertiesToConsider_FullName_Equal()
};

// Consider ONLY differences in terms of `BH.oM.Structure.Elements.Bar.Start.Name`. We should not find any.
ComparisonConfig cc = new ComparisonConfig() { PropertiesToConsider = new List<string>() { $"{typeof(BH.oM.Structure.Elements.Bar).FullName}.Start.Name" } };
ComparisonConfig cc = new ComparisonConfig(propertiesToConsider: new HashSet<string>() { $"{typeof(BH.oM.Structure.Elements.Bar).FullName}.Start.Name" });
ObjectDifferences objectDifferences = BH.Engine.Diffing.Query.ObjectDifferences(bar1, bar2, cc);

Assert.IsTrue(objectDifferences == null || objectDifferences.Differences.Count == 0, $"No difference should have been found. Differences: {objectDifferences?.ToText()}");
Expand Down Expand Up @@ -377,7 +346,7 @@ public void ObjectDifferences_PropertiesToConsider_PartialName_Equal()
};

// Consider ONLY differences in terms of `Location.Position`. We should not find any.
ComparisonConfig cc = new ComparisonConfig() { PropertiesToConsider = new List<string>() { "Location.Position" } }; // using "partial property path"
ComparisonConfig cc = new ComparisonConfig(propertiesToConsider: new HashSet<string>() { "Location.Position" }); // using "partial property path"
ObjectDifferences objectDifferences = BH.Engine.Diffing.Query.ObjectDifferences(object1, object2, cc);

Assert.IsTrue(objectDifferences == null || objectDifferences.Differences.Count == 0, $"No difference should have been found. Differences: {objectDifferences?.ToText()}");
Expand Down Expand Up @@ -407,7 +376,7 @@ public void ObjectDifferences_PropertiesToConsider_PartialName_Different()
};

// Consider ONLY differences in terms of `Location.Position`
ComparisonConfig cc = new ComparisonConfig() { PropertiesToConsider = new List<string>() { "Location.Position" } }; // using "partial property path"
ComparisonConfig cc = new ComparisonConfig(propertiesToConsider: new HashSet<string>() { "Location.Position" }); // using "partial property path"
ObjectDifferences objectDifferences = BH.Engine.Diffing.Query.ObjectDifferences(object1, object2, cc);

Assert.IsTrue(objectDifferences.Differences.Where(d => d.Name.Contains("Location.Position")).Count() == 3, "3 differences in terms of Location.Position should have been found.");
Expand Down Expand Up @@ -438,13 +407,13 @@ public void ObjectDifferences_PropertiesToConsider_WildCardPrefix_Equal()
};

// Consider ONLY differences in terms of names: `*.Name`. We should not find any.
ComparisonConfig cc = new ComparisonConfig() { PropertiesToConsider = new List<string>() { "*.Name" } };
ComparisonConfig cc = new ComparisonConfig(propertiesToConsider: new HashSet<string>() { "*.Name" });
ObjectDifferences objectDifferences = BH.Engine.Diffing.Query.ObjectDifferences(bar1, bar2, cc);

Assert.IsTrue(objectDifferences == null || objectDifferences.Differences.Count == 0, $"No difference should have been found. Differences: {objectDifferences?.ToText()}");

// Or equivalently, without any wildcard: `Name`. Result should be the same, we should not find any.
cc = new ComparisonConfig() { PropertiesToConsider = new List<string>() { "Name" } };
cc = new ComparisonConfig(propertiesToConsider: new HashSet<string>() { "Name" });
objectDifferences = BH.Engine.Diffing.Query.ObjectDifferences(bar1, bar2, cc);

Assert.IsTrue(objectDifferences == null || objectDifferences.Differences.Count == 0, $"No difference should have been found. Differences: {objectDifferences?.ToText()}");
Expand Down Expand Up @@ -484,7 +453,7 @@ public void ObjectDifferences_PropertiesToConsider_WildCardMiddle_Equal()
};

// Consider ONLY differences in terms of Start AND End names: `Bar.*.Name`. We should not find any.
ComparisonConfig cc = new ComparisonConfig() { PropertiesToConsider = new List<string>() { "Bar.*.Name" } };
ComparisonConfig cc = new ComparisonConfig(propertiesToConsider: new HashSet<string>() { "Bar.*.Name" });
ObjectDifferences objectDifferences = BH.Engine.Diffing.Query.ObjectDifferences(bar1, bar2, cc);

Assert.IsTrue(objectDifferences == null || objectDifferences.Differences.Count == 0, $"No difference should have been found. Differences: {objectDifferences?.ToText()}");
Expand Down Expand Up @@ -534,7 +503,7 @@ public void ObjectDifferences_PropertiesExceptions_Equal()
public void ObjectDifferences_NumericTolerance_Equal()
{
// Set a numerical tolerance (different from the default value).
ComparisonConfig cc = new ComparisonConfig() { NumericTolerance = 0.01 };
ComparisonConfig cc = new ComparisonConfig(numericTolerance: 0.01);

// Create one node.
Node node1 = new Node();
Expand All @@ -554,7 +523,7 @@ public void ObjectDifferences_NumericTolerance_Equal()
public void ObjectDifferences_SignificantFigures_Equal()
{
// Set SignificantFigures (different from the default value).
ComparisonConfig cc = new ComparisonConfig() { SignificantFigures = 3 };
ComparisonConfig cc = new ComparisonConfig(significantFigures: 3);

// Create one node.
Node node1 = new Node();
Expand Down Expand Up @@ -585,27 +554,26 @@ public void ObjectDifferences_SignificantFigures_Different()

// Set significantFigures so that X and Y are rounded to 0.312, while Z is rounded to 122.
// This means that only Z should be identified as different.
cc.SignificantFigures = 3;
cc = new ComparisonConfig(significantFigures: 3);
ObjectDifferences objectDifferences = BH.Engine.Diffing.Query.ObjectDifferences(node1, node2, cc);

Assert.IsTrue(objectDifferences == null || objectDifferences.Differences.Count == 1, $"Wrong number of differences identified. Differences: {objectDifferences?.ToText()}");

// Set significantFigures so that X and Y are rounded to 0.31, while Z is rounded to 120.
// This means that only X and Y should be identified as different.
cc.SignificantFigures = 2;
Assert.IsTrue(objectDifferences == null || objectDifferences.Differences.Count != 2, $"Wrong number of differences identified. Differences: {objectDifferences?.ToText()}");
cc = new ComparisonConfig(significantFigures: 2);
Assert.IsTrue(objectDifferences == null || objectDifferences.Differences.Count != 2, $"Wrong number of differences identified. Differences: {objectDifferences?.ToText()}");
}

[Test]
public void ObjectDifferences_PropertySignificantFigures_Equal()
{
// Testing property-specific Significant Figures.
// Set SignificantFigures (different from the default value).
ComparisonConfig cc = new ComparisonConfig()
{
SignificantFigures = 3,
PropertySignificantFigures = new HashSet<NamedSignificantFigures>() { new NamedSignificantFigures() { Name = "*.Z", SignificantFigures = 1 } }
};
ComparisonConfig cc = new ComparisonConfig(
significantFigures: 3,
propertySignificantFigures: new HashSet<NamedSignificantFigures>() { new NamedSignificantFigures() { Name = "*.Z", SignificantFigures = 1 } }
);

// Create one node.
Node node1 = new Node();
Expand All @@ -626,11 +594,10 @@ public void ObjectDifferences_PropertyNumericTolerances_Equal()
{
// Testing property-specific Significant Figures.
// Set SignificantFigures (different from the default value).
ComparisonConfig cc = new ComparisonConfig()
{
NumericTolerance = 1E-3,
PropertyNumericTolerances = new HashSet<NamedNumericTolerance>() { new NamedNumericTolerance() { Name = "*.Z", Tolerance = 1E-1 } }
};
ComparisonConfig cc = new ComparisonConfig(
numericTolerance: 1E-3,
propertyNumericTolerances: new HashSet<NamedNumericTolerance>() { new NamedNumericTolerance() { Name = "*.Z", Tolerance = 1E-1 } }
);

// Create one node.
Node node1 = new Node();
Expand All @@ -651,15 +618,14 @@ public void ObjectDifferences_PropertyNumericTolerances_Different()
{
// Testing property-specific Significant Figures.
// Set SignificantFigures (different from the default value).
ComparisonConfig cc = new ComparisonConfig()
{
NumericTolerance = 1E-2,
PropertyNumericTolerances = new HashSet<NamedNumericTolerance>()
{
ComparisonConfig cc = new ComparisonConfig(
numericTolerance: 1E-2,
propertyNumericTolerances: new HashSet<NamedNumericTolerance>()
{
new NamedNumericTolerance() { Name = "*.Y", Tolerance = 1E-3 },
new NamedNumericTolerance() { Name = "*.Z", Tolerance = 1E-3 },
}
};
);

// Create one node.
Node node1 = new Node();
Expand Down
3 changes: 1 addition & 2 deletions RevitDiffingTests/HashTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ public void DifferentObjects_SameRevitParameter_ConsiderOnlyRevitParameters_Same
followingObjs = followingObjs.Select(o => o.SetRevitParameter(someParameter.Name, someParameter.Value)).ToList();

// By specifying an unlikely PropertyName in PropertiesToConsider, we can get the differences in terms of RevitParameters only.
RevitComparisonConfig rcc = new RevitComparisonConfig() { PropertiesToConsider = new List<string>() { "Only Revit Parameters" } };

RevitComparisonConfig rcc = new RevitComparisonConfig(propertiesToConsider: new HashSet<string>() { "Only Revit Parameters" });
// Check that the objects are seen as the same.
List<string> pastObjectsHashes = pastObjects.Select(o => o.Hash(rcc)).ToList();
List<string> followingObjectsHashes = followingObjs.Select(o => o.Hash(rcc)).ToList();
Expand Down

0 comments on commit 4ab738c

Please sign in to comment.