@@ -23,7 +23,7 @@ namespace Microsoft.ML.Runtime.RunTests
23
23
/// </summary>
24
24
public abstract partial class BaseTestBaseline : BaseTestClass
25
25
{
26
- public const decimal Tolerance = 10_000_000 ;
26
+ public const int DigitsOfPrecision = 7 ;
27
27
28
28
protected BaseTestBaseline ( ITestOutputHelper output ) : base ( output )
29
29
{
@@ -352,12 +352,12 @@ protected bool CheckEquality(string dir, string name, string nameBase = null)
352
352
/// Check whether two files are same ignoring volatile differences (path, dates, times, etc).
353
353
/// Returns true if the check passes.
354
354
/// </summary>
355
- protected bool CheckEqualityNormalized ( string dir , string name , string nameBase = null , decimal precision = Tolerance )
355
+ protected bool CheckEqualityNormalized ( string dir , string name , string nameBase = null , int digitsOfPrecision = DigitsOfPrecision )
356
356
{
357
- return CheckEqualityCore ( dir , name , nameBase ?? name , true , precision ) ;
357
+ return CheckEqualityCore ( dir , name , nameBase ?? name , true , digitsOfPrecision ) ;
358
358
}
359
359
360
- protected bool CheckEqualityCore ( string dir , string name , string nameBase , bool normalize , decimal precision = Tolerance )
360
+ protected bool CheckEqualityCore ( string dir , string name , string nameBase , bool normalize , int digitsOfPrecision = DigitsOfPrecision )
361
361
{
362
362
Contracts . Assert ( IsActive ) ;
363
363
Contracts . AssertValue ( dir ) ; // Can be empty.
@@ -384,7 +384,7 @@ protected bool CheckEqualityCore(string dir, string name, string nameBase, bool
384
384
if ( ! CheckBaseFile ( basePath ) )
385
385
return false ;
386
386
387
- bool res = CheckEqualityFromPathsCore ( relPath , basePath , outPath , precision : precision ) ;
387
+ bool res = CheckEqualityFromPathsCore ( relPath , basePath , outPath , digitsOfPrecision : digitsOfPrecision ) ;
388
388
389
389
// No need to keep the raw (unnormalized) output file.
390
390
if ( normalize && res )
@@ -501,7 +501,7 @@ protected bool CheckOutputIsSuffix(string basePath, string outPath, int skip = 0
501
501
/// skipping the given number of lines on the output, and finding the corresponding line
502
502
/// in the baseline.
503
503
/// </summary>
504
- protected bool CheckEqualityNormalized ( string dir , string name , string suffix , int skip , decimal precision = Tolerance )
504
+ protected bool CheckEqualityNormalized ( string dir , string name , string suffix , int skip , int digitsOfPrecision = DigitsOfPrecision )
505
505
{
506
506
Contracts . Assert ( IsActive ) ;
507
507
Contracts . AssertValue ( dir ) ; // Can be empty.
@@ -522,7 +522,7 @@ protected bool CheckEqualityNormalized(string dir, string name, string suffix, i
522
522
if ( ! CheckBaseFile ( basePath ) )
523
523
return false ;
524
524
525
- bool res = CheckEqualityFromPathsCore ( relPath , basePath , outPath , skip , precision ) ;
525
+ bool res = CheckEqualityFromPathsCore ( relPath , basePath , outPath , skip , digitsOfPrecision ) ;
526
526
527
527
// No need to keep the raw (unnormalized) output file.
528
528
if ( res )
@@ -531,7 +531,7 @@ protected bool CheckEqualityNormalized(string dir, string name, string suffix, i
531
531
return res ;
532
532
}
533
533
534
- protected bool CheckEqualityFromPathsCore ( string relPath , string basePath , string outPath , int skip = 0 , decimal precision = Tolerance )
534
+ protected bool CheckEqualityFromPathsCore ( string relPath , string basePath , string outPath , int skip = 0 , int digitsOfPrecision = DigitsOfPrecision )
535
535
{
536
536
Contracts . Assert ( skip >= 0 ) ;
537
537
@@ -578,9 +578,7 @@ protected bool CheckEqualityFromPathsCore(string relPath, string basePath, strin
578
578
}
579
579
580
580
count ++ ;
581
-
582
- if ( ! RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
583
- GetNumbersFromFile ( ref line1 , ref line2 , precision ) ;
581
+ GetNumbersFromFile ( ref line1 , ref line2 , digitsOfPrecision ) ;
584
582
585
583
if ( line1 != line2 )
586
584
{
@@ -594,28 +592,45 @@ protected bool CheckEqualityFromPathsCore(string relPath, string basePath, strin
594
592
}
595
593
}
596
594
597
- private static void GetNumbersFromFile ( ref string firstString , ref string secondString , decimal precision )
595
+ private static void GetNumbersFromFile ( ref string firstString , ref string secondString , int digitsOfPrecision )
598
596
{
599
597
Regex _matchNumer = new Regex ( @"\b[0-9]+\.?[0-9]*\b" , RegexOptions . IgnoreCase | RegexOptions . Compiled ) ;
600
598
MatchCollection firstCollection = _matchNumer . Matches ( firstString ) ;
601
599
MatchCollection secondCollection = _matchNumer . Matches ( secondString ) ;
602
600
603
- MatchNumberWithTolerance ( firstCollection , secondCollection , precision ) ;
601
+ MatchNumberWithTolerance ( firstCollection , secondCollection , digitsOfPrecision ) ;
604
602
firstString = _matchNumer . Replace ( firstString , "%Number%" ) ;
605
603
secondString = _matchNumer . Replace ( secondString , "%Number%" ) ;
606
604
}
607
605
608
- private static void MatchNumberWithTolerance ( MatchCollection firstCollection , MatchCollection secondCollection , decimal precision )
606
+ private static void MatchNumberWithTolerance ( MatchCollection firstCollection , MatchCollection secondCollection , int digitsOfPrecision )
609
607
{
610
608
for ( int i = 0 ; i < firstCollection . Count ; i ++ )
611
609
{
612
- decimal f1 = decimal . Parse ( firstCollection [ i ] . ToString ( ) ) ;
613
- decimal f2 = decimal . Parse ( secondCollection [ i ] . ToString ( ) ) ;
610
+ double f1 = double . Parse ( firstCollection [ i ] . ToString ( ) ) ;
611
+ double f2 = double . Parse ( secondCollection [ i ] . ToString ( ) ) ;
612
+
613
+ double allowedVariance = Math . Pow ( 10 , - digitsOfPrecision ) ;
614
+ double delta = Round ( f1 , digitsOfPrecision ) - Round ( f2 , digitsOfPrecision ) ;
614
615
615
- Assert . InRange ( f1 , f2 - ( f2 / precision ) , f2 + ( f2 / precision ) ) ;
616
+ Assert . InRange ( delta , - allowedVariance , allowedVariance ) ;
616
617
}
617
618
}
618
619
620
+ private static double Round ( double value , int digitsOfPrecision )
621
+ {
622
+ if ( ( value == 0 ) || double . IsInfinity ( value ) || double . IsNaN ( value ) )
623
+ {
624
+ return value ;
625
+ }
626
+
627
+ double absValue = Math . Abs ( value ) ;
628
+ double integralDigitCount = Math . Floor ( Math . Log10 ( absValue ) + 1 ) ;
629
+
630
+ double scale = Math . Pow ( 10 , integralDigitCount ) ;
631
+ return scale * Math . Round ( value / scale , digitsOfPrecision ) ;
632
+ }
633
+
619
634
#if TOLERANCE_ENABLED
620
635
// This corresponds to how much relative error is tolerable for a value of 0.
621
636
const Float RelativeToleranceStepSize = ( Float ) 0.001 ;
0 commit comments