3.  Тестване и настройка 

Самостоятелно тестване на функции
* Данните, с които ще се тества функцията, се получават по 3 начена:
-- като вход от потребителя
-- като стойности, получени от цикъл
-- случайни числа

// sqrtest1.cpp
#include <iostream>
#include <cmath>
using namespace std;

bool not_equal(double x, double y)
{
   const double EPSILON = 1E-14;
   double dmax = fabs(x);
   if (fabs(y) > dmax) dmax = fabs(y);
   return fabs(x - y) > dmax * EPSILON;
}

/* Function to be tested */
double squareroot(double a)
{  if (a == 0) return 0;
   double xnew = a;
   double xold;
   do
   {  xold = xnew;
      xnew = (xold + a / xold) / 2;
   }
   while (not_equal(xnew, xold));
   return xnew;
}

/* Test stub */
int main()
{  double x;
   while (cin >> x)
   {  double y = squareroot(x);
      cout << "squareroot of " << x << " = " << y << "\n";
   }
   return 0;
}

// sqrtest2.cpp
#include <iostream>
#include <cmath>
using namespace std;

bool not_equal(double x, double y)
{
   const double EPSILON = 1E-14;
   double dmax = fabs(x);
   if (fabs(y) > dmax) dmax = fabs(y);
   return fabs(x - y) > dmax * EPSILON;
}

/* Function to be tested */
double squareroot(double a)
{
   if (a == 0) return 0;
   double xnew = a;
   double xold;
   do
   {  xold = xnew;
      xnew = (xold + a / xold) / 2;
   }
   while (not_equal(xnew, xold));
   return xnew;
}

/* Test stub */
int main()
{  double x;
   for (x = 0; x <= 10; x = x + 0.5)
   {  double y = squareroot(x);
      cout << "squareroot of " << x << " = " << y << "\n";
   }
   return 0;
}

// sqrtest3.cpp
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
using namespace std;

void rand_seed()
{
   int seed = static_cast<int>(time(0));
   srand(seed);
}

double rand_double(double a, double b)
{  return a + (b - a) * rand() * (1.0 / RAND_MAX);  }

bool not_equal(double x, double y)
{
   const double EPSILON = 1E-14;
   double dmax = fabs(x);
   if (fabs(y) > dmax) dmax = fabs(y);
   return fabs(x - y) > dmax * EPSILON;
}

/* Function to be tested */
double squareroot(double a)
{
   if (a == 0) return 0;
   double xnew = a;
   double xold;
   do
   {  xold = xnew;
      xnew = (xold + a / xold) / 2;
   }
   while (not_equal(xnew, xold));
   return xnew;
}

/* Test stub */
int main()
{  rand_seed();
   int i;
   for (i = 1; i <= 100; i++)
   {  double x = rand_double(0, 1E6);
      double y = squareroot(x);
      cout << "squareroot of " << x << " = " << y << "\n";
   }
   return 0;
}

Подбор на тестови примери

Оценка на резултатите от тестването
// sqrtest4.cpp
oo#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
using namespace std;

void rand_seed()
{
   int seed = static_cast<int>(time(0));
   srand(seed);
}

double rand_double(double a, double b)
{  return a + (b - a) * rand() * (1.0 / RAND_MAX);   }

bool not_equal(double x, double y)
{
   const double EPSILON = 1E-14;
   double dmax = fabs(x);
   if (fabs(y) > dmax) dmax = fabs(y);
   return fabs(x - y) > dmax * EPSILON;
}

/* Function to be tested */
double squareroot(double a)
{
   if (a == 0) return 0;
   double xnew = a;
   double xold;
   do
   {  xold = xnew;
      xnew = (xold + a / xold) / 2;
   }
   while (not_equal(xnew, xold));
   return xnew;
}

/* Test stub */
int main()
{  int i;
   for (i = 1; i <= 100; i++)
   {  double x = rand_double(0, 1E6);
      double y = squareroot(x);
      if (not_equal(y * y, x))  cout << "Test failed. ";
      else                      cout << "Test passed. ";
      cout << "squareroot of " << x << " = " << y << "\n";
   }
   return 0;
}

// sqrtest5.cpp
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
using namespace std;

void rand_seed()
{
   int seed = static_cast<int>(time(0));
   srand(seed);
}

double rand_double(double a, double b)
{  return a + (b - a) * rand() * (1.0 / RAND_MAX);   }

bool not_equal(double x, double y)
{
   const double EPSILON = 1E-14;
   double dmax = fabs(x);
   if (fabs(y) > dmax) dmax = fabs(y);
   return fabs(x - y) > dmax * EPSILON;
}

/* Function to be tested */
double squareroot(double a)
{
   if (a == 0) return 0;
   double xnew = a;
   double xold;
   do
   {  xold = xnew;
      xnew = (xold + a / xold) / 2;
   }
   while (not_equal(xnew, xold));
   return xnew;
}
 

/* Test stub */
int main()
{
   rand_seed();
   int i;
   for (i = 1; i <= 100; i++)
   {  double x = rand_double(0, 1E6);
      double y = squareroot(x);
      if (not_equal(y, pow(x, 0.5))) cout << "Test failed. ";
      else                           cout << "Test passed. ";
      cout << "squareroot of " << x << " = " << y << "\n";
   }
   return 0;
}

Трасиране на програмата

Работа на дебъгерът

Стратегии при тестването
** Възпроизвеждане на грешката
** "Разделяй и владей"
** Изяснете си какво трябва да прави програмата ви
** Обръщайте внимание на всички подробности
** Вниквайте във всяка грешка преди да я отстраните