12 #include <gtest/gtest.h> 
   17 const double eps = 1e-9;
 
   24         double coefs_roots[][6] = {{1, -2, 1, 2, 1.0, 1.0},
 
   25                                                            {1, 0, -1, 2, -1.0, 1.0},
 
   26                                                            {1, -1, -56, 2, -7.0, 8.0},
 
   28                                                            {2.0, 0, 0, 2, 0, 0}};
 
   30         const unsigned int nTests = 
sizeof(coefs_roots) / 
sizeof(coefs_roots[0]);
 
   32         for (
unsigned int i = 0; i < nTests; i++)
 
   34                 const double a = coefs_roots[i][0], 
b = coefs_roots[i][1],
 
   35                                          c = coefs_roots[i][2];
 
   36                 const int num_roots_good = 
static_cast<int>(coefs_roots[i][3]);
 
   37                 const double r1_good = coefs_roots[i][4], r2_good = coefs_roots[i][5];
 
   43                         "\nSolving: %.02f * x^2 + %.02f * x + %.02f = 0\n", 
a, 
b, 
c);
 
   45                 EXPECT_EQ(num_roots, num_roots_good);
 
   48                         EXPECT_NEAR(r1, r1_good, 
eps) << sTestStr;
 
   52                         EXPECT_NEAR(r2, r2_good, 
eps) << sTestStr;
 
   62         double coefs_roots[][7] = {{-6, 11, -6, 3, 1.0, 2.0, 3.0},
 
   63                                                            {2, 3, 4, 1, -1.650629191439386, 0, 0},
 
   64                                                            {0, -91, -90, 3, -1.0, -9.0, 10.0}};
 
   66         const unsigned int nTests = 
sizeof(coefs_roots) / 
sizeof(coefs_roots[0]);
 
   68         for (
unsigned int i = 0; i < nTests; i++)
 
   70                 const double a = coefs_roots[i][0], 
b = coefs_roots[i][1],
 
   71                                          c = coefs_roots[i][2];
 
   72                 const int num_roots_good = 
static_cast<int>(coefs_roots[i][3]);
 
   73                 const double roots_good[3] = {coefs_roots[i][4], coefs_roots[i][5],
 
   80                         "\nSolving: x^3 + %.02f * x^2 + %.02f * x + %.02f = 0\n", 
a, 
b, 
c);
 
   82                 EXPECT_EQ(num_roots, num_roots_good);
 
   83                 for (
int k = 0; k < num_roots; k++)
 
   86                         for (
int j = 0; j < num_roots; j++)
 
   87                                 if (std::abs(roots[k] - roots_good[j]) < 
eps) match = 
true;
 
   89                         EXPECT_TRUE(match) << sTestStr << 
"k: " << k << std::endl;
 
   99         double coefs_roots[][9] = {{-10, 35, -50, 24, 4, 1.0, 2.0, 3.0, 4.0},
 
  100                                                            {-14, 35, 50, 0, 4, -1, 0, 5, 10}};
 
  102         const unsigned int nTests = 
sizeof(coefs_roots) / 
sizeof(coefs_roots[0]);
 
  104         for (
unsigned int i = 0; i < nTests; i++)
 
  106                 const double a = coefs_roots[i][0], 
b = coefs_roots[i][1],
 
  107                                          c = coefs_roots[i][2], d = coefs_roots[i][3];
 
  108                 const int num_roots_good = 
static_cast<int>(coefs_roots[i][4]);
 
  109                 const double roots_good[4] = {coefs_roots[i][5], coefs_roots[i][6],
 
  110                                                                           coefs_roots[i][7], coefs_roots[i][8]};
 
  116                         "\nSolving: x^4 + %.02f * x^3 + %.02f * x^2 + %.02f * x + %.02f = " 
  120                 EXPECT_EQ(num_roots, num_roots_good);
 
  121                 for (
int k = 0; k < num_roots; k++)
 
  124                         for (
int j = 0; j < num_roots; j++)
 
  125                                 if (std::abs(roots[k] - roots_good[j]) < 
eps) match = 
true;
 
  127                         EXPECT_TRUE(match) << sTestStr << 
"k: " << k << std::endl;