Просмотр исходного кода

Add more line coverage in ctkUtils

Cover more of significantDecimals(), orderOfMagnitude() and
closestPowerOfTen()
Julien Finet лет назад: 14
Родитель
Сommit
2a3390bdb6

+ 9 - 0
Libs/Core/Testing/Cpp/ctkUtilsClosestPowerOfTenTest1.cpp

@@ -121,5 +121,14 @@ int ctkUtilsClosestPowerOfTenTest1(int , char *  [] )
     return EXIT_FAILURE;
     }
 
+  closest = ctk::closestPowerOfTen(0.);
+  if (closest != 0)
+    {
+    std::cerr << "closest power of 10 failed for number:"
+              << 0. << ". Found " << closest
+              << " instead of 0" << std::endl;
+    return EXIT_FAILURE;
+    }
+
   return EXIT_SUCCESS;
 }

+ 9 - 1
Libs/Core/Testing/Cpp/ctkUtilsOrderOfMagnitudeTest1.cpp

@@ -26,8 +26,9 @@
 #include "ctkUtils.h"
 
 // STD includes
-#include <stdlib.h>
 #include <iostream>
+#include <limits>
+#include <stdlib.h>
 #include <string>
 #include <vector>
 
@@ -142,6 +143,13 @@ int ctkUtilsOrderOfMagnitudeTest1(int , char *  [] )
     return EXIT_FAILURE;
     }
 
+  if (ctk::orderOfMagnitude(0) != std::numeric_limits<int>::min())
+    {
+    std::cerr << "order of magnitude failed for number:"
+              << 0 << ". Found " << ctk::orderOfMagnitude(0)
+              << " instead of " << std::numeric_limits<int>::min() << std::endl;
+    return EXIT_FAILURE;
+    }
 
   return EXIT_SUCCESS;
 }

+ 16 - 0
Libs/Core/Testing/Cpp/ctkUtilsSignificantDecimalsTest1.cpp

@@ -132,5 +132,21 @@ int ctkUtilsSignificantDecimalsTest1(int , char *  [] )
     {
     return EXIT_FAILURE;
     }
+  if (testSignificantDecimals(0.5, 1))
+    {
+    return EXIT_FAILURE;
+    }
+  if (testSignificantDecimals(0.25, 2))
+    {
+    return EXIT_FAILURE;
+    }
+  if (testSignificantDecimals(0.125, 3))
+    {
+    return EXIT_FAILURE;
+    }
+  if (testSignificantDecimals(0.1234567891013151, 16))
+    {
+    return EXIT_FAILURE;
+    }
   return EXIT_SUCCESS;
 }

+ 6 - 6
Libs/Core/ctkUtils.cpp

@@ -165,10 +165,7 @@ int ctk::significantDecimals(double value)
 {
   QString number = QString::number(value, 'f', 16);
   QString fractional = number.section('.', 1, 1);
-  if (fractional.length() <= 2)
-    {
-    return fractional.length() - 1;
-    }
+  Q_ASSERT(fractional.length() == 16);
   QChar previous;
   int previousRepeat=0;
   bool only0s = true;
@@ -179,6 +176,7 @@ int ctk::significantDecimals(double value)
       {
       only0s = false;
       }
+    // Has the digit been repeated too many times ?
     if (digit == previous && previousRepeat == 2 &&
         !only0s)
       {
@@ -188,13 +186,14 @@ int ctk::significantDecimals(double value)
         }
       return i;
       }
+    // Last digit
     if (i == fractional.length() - 1)
       {
       if (previousRepeat > 2)
         {
         return i - previousRepeat;
         }
-      return i;
+      return fractional.length();
       }
     // get ready for next
     if (previous != digit)
@@ -207,7 +206,8 @@ int ctk::significantDecimals(double value)
       ++previousRepeat;
       }
     }
-  return -1;
+  Q_ASSERT(false);
+  return fractional.length();
 }
 
 //-----------------------------------------------------------------------------