Skip to content

Commit 244a8c2

Browse files
feiyun0112sfilipi
authored andcommitted
Baseline comparisons with tolerance should not exit when values don't match (#218) (#1420)
* Fail when MatchNumberWithTolerance is not in range * remove extra spaces
1 parent cffb3fe commit 244a8c2

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

test/Microsoft.ML.TestFramework/BaseTestBaseline.cs

+21-7
Original file line numberDiff line numberDiff line change
@@ -495,9 +495,9 @@ protected bool CheckEqualityFromPathsCore(string relPath, string basePath, strin
495495
}
496496

497497
count++;
498-
GetNumbersFromFile(ref line1, ref line2, digitsOfPrecision);
498+
var inRange = GetNumbersFromFile(ref line1, ref line2, digitsOfPrecision);
499499

500-
if (line1 != line2)
500+
if (!inRange || line1 != line2)
501501
{
502502
if (line1 == null || line2 == null)
503503
Fail("Output and baseline different lengths: '{0}'", relPath);
@@ -509,19 +509,26 @@ protected bool CheckEqualityFromPathsCore(string relPath, string basePath, strin
509509
}
510510
}
511511

512-
private void GetNumbersFromFile(ref string firstString, ref string secondString, int digitsOfPrecision)
512+
private bool GetNumbersFromFile(ref string firstString, ref string secondString, int digitsOfPrecision)
513513
{
514-
514+
515515
MatchCollection firstCollection = MatchNumbers.Matches(firstString);
516516
MatchCollection secondCollection = MatchNumbers.Matches(secondString);
517517

518518
if (firstCollection.Count == secondCollection.Count)
519-
MatchNumberWithTolerance(firstCollection, secondCollection, digitsOfPrecision);
519+
{
520+
if(!MatchNumberWithTolerance(firstCollection, secondCollection, digitsOfPrecision))
521+
{
522+
return false;
523+
}
524+
}
525+
520526
firstString = MatchNumbers.Replace(firstString, "%Number%");
521527
secondString = MatchNumbers.Replace(secondString, "%Number%");
528+
return true;
522529
}
523530

524-
private void MatchNumberWithTolerance(MatchCollection firstCollection, MatchCollection secondCollection, int digitsOfPrecision)
531+
private bool MatchNumberWithTolerance(MatchCollection firstCollection, MatchCollection secondCollection, int digitsOfPrecision)
525532
{
526533
for (int i = 0; i < firstCollection.Count; i++)
527534
{
@@ -547,9 +554,16 @@ private void MatchNumberWithTolerance(MatchCollection firstCollection, MatchColl
547554
if (!inRange)
548555
{
549556
delta = Math.Round(f1 - f2, digitsOfPrecision);
550-
Assert.InRange(delta, -allowedVariance, allowedVariance);
557+
inRange = delta >= -allowedVariance && delta <= allowedVariance;
558+
}
559+
560+
if(!inRange)
561+
{
562+
return false;
551563
}
552564
}
565+
566+
return true;
553567
}
554568

555569
private static double Round(double value, int digitsOfPrecision)

0 commit comments

Comments
 (0)