SmallVectorUnitTest.cpp File Reference
#include <iostream>
#include <cstdlib>
#include <cmath>
#include "Math/SmallVector.h"
#include "Logger.h"

Functions

int main (int argc, char *argv[])
 

Function Documentation

◆ main()

int main ( int argc  ,
char argv[] 
)
31 {
32  //(operator[], operator() and size() don't have a stand-alone test, but are used throughout the test in multiple assertions
33 
34  //various constructors
35  double data[] = {1., 2., 3.};
36  SmallVector<0> x0;
37  SmallVector<1> x1, y1(x1);
38  SmallVector<3> fromArray(data);
39  SmallVector<4> destroy, convenient({5.,6.,8.,9.});
40  data[0] = 4;
41  logger.assert_always(x0.size() == 0, "Constructor creates a vector of the wrong size");
42  logger.assert_always(x1.size() == 1, "Constructor creates a vector of the wrong size");
43  logger.assert_always(y1.size() == 1, "Constructor creates a vector of the wrong size");
44  logger.assert_always(y1 == x1, "Copied array does not pass equality test");
45  logger.assert_always(std::abs(y1[0] - x1[0]) < 1e-12, "Copy constructor does not copy values");
46  logger.assert_always(destroy.size() == 4, "Constructor creates a vector of the wrong size");
47  logger.assert_always(convenient.size() == 4, "Constructor creates a vector of the wrong size");
48  logger.assert_always(fromArray.size() == 3, "Constructor creates a vector of the wrong size");
49  destroy[2] = 4;
50  SmallVector<4> moved(std::move(destroy));
51  logger.assert_always(moved.size() == 4, "Constructor creates a vector of the wrong size");
52  logger.assert_always(std::abs(moved[2] - 4.) < 1e-12, "Constructor from array does not copy!");
53  logger.assert_always(std::abs(fromArray(0) - 1.) < 1e-12, "Constructor from array does not copy!");
54  logger.assert_always(std::abs(fromArray(1) - 2.) < 1e-12, "Constructor from array does not copy!");
55  logger.assert_always(std::abs(fromArray[2] - 3.) < 1e-12, "Constructor from array does not copy!");
56  logger.assert_always(std::abs(convenient[0] - 5.) < 1e-12, "Initializer list constructor does not copy!");
57  logger.assert_always(std::abs(convenient[1] - 6.) < 1e-12, "Initializer list constructor does not copy!");
58  logger.assert_always(std::abs(convenient[2] - 8.) < 1e-12, "Initializer list constructor does not copy!");
59  logger.assert_always(std::abs(convenient[3] - 9.) < 1e-12, "Initializer list constructor does not copy!");
60  SmallVector<4> assigned = moved;
61  logger.assert_always(assigned.size() == 4, "Constructor creates a vector of the wrong size");
62  logger.assert_always(std::abs(assigned[2] - 4.) < 1e-12, "Constructor from array does not copy!");
63  destroy = convenient;
64  logger.assert_always(destroy.size() == 4, "Assignment operator creates a vector of the wrong size");
65  logger.assert_always(std::abs(convenient(0) - 5.) < 1e-12, "Assignment operator from array does not copy!");
66  logger.assert_always(std::abs(convenient[1] - 6.) < 1e-12, "Assignment operator from array does not copy!");
67  logger.assert_always(std::abs(convenient(2) - 8.) < 1e-12, "Assignment operator from array does not copy!");
68  logger.assert_always(std::abs(convenient(3) - 9.) < 1e-12, "Assignment operator from array does not copy!");
69 
70  SmallVector<2> p2 = {{0.8, 1.8}};
71  SmallVector<2> pc2;
72  SmallVector<2> pv2 = {{0.8, 0.8}};
73  SmallVector<2> pw2 = {{1.8, 1.8}};
74  SmallVector<2> px2 = {{1.8, 0.8}};
75  SmallVector<2> py2 = {{0.6, 0.7}};
76  const SmallVector<2> pr2 = pc2 = p2;
77  logger.assert_always(pr2.size() == 2, "Constructor creates a vector of the wrong size");
78  logger.assert_always(pc2.size() == 2, "Constructor creates a vector of the wrong size");
79  for (std::size_t i = 0; i < 2; ++i)
80  {
81  logger.assert_always((std::abs(pc2[i] - 0.8 - i) < 1e-12), "assignment operator");
82  logger.assert_always((std::abs(pr2[i] - 0.8 - i) < 1e-12), "assignment operator");
83  }
84  logger.assert_always((pr2 == pc2 && pc2 == pr2 && pc2 == p2 && !(pr2 == pv2 || pv2 == pr2 || p2 == pv2)), "equality operator");
85  logger.assert_always(!(pr2 == pw2 || pw2 == pr2 || p2 == pw2 || pr2 == px2 || px2 == pr2 || p2 == px2 || pr2 == py2 || py2 == pr2 || p2 == py2), "equality operator");
86  pc2 += p2;
87  for (std::size_t i = 0; i < 2; ++i)
88  {
89  logger.assert_always((std::abs(pc2[i] - 1.6 - 2 * i) < 1e-12), "increment operator");
90  }
91  pc2 -= pv2;
92  for (std::size_t i = 0; i < 2; ++i)
93  {
94  logger.assert_always((std::abs(pc2[i] - 0.8 - 2 * i) < 1e-12), "decrement operator");
95  }
96  pc2 *= 4.;
97  for (std::size_t i = 0; i < 2; ++i)
98  {
99  logger.assert_always((std::abs(pc2[i] - 3.2 - 8 * i) < 1e-12), "multiply operator");
100  }
101  for (std::size_t i = 0; i < 2; ++i)
102  {
103  logger.assert_always((std::abs((pc2 * -0.25)[i] + 0.8 + 2 * i) < 1e-12), "multiplication");
104  }
105  for (std::size_t i = 0; i < 2; ++i)
106  {
107  logger.assert_always((std::abs((pc2 + pv2)[i] - 4. - 8 * i) < 1e-12), "addition");
108  }
109  for (std::size_t i = 0; i < 2; ++i)
110  {
111  logger.assert_always((std::abs((pv2 - pc2)[i] + 2.4 + 8 * i) < 1e-12), "subtraction");
112  }
113  for (std::size_t i = 0; i < 2; ++i)
114  {
115  logger.assert_always((std::abs((-pc2)[i] + 3.2 + 8 * i) < 1e-12), "unary -");
116  }
117  for (std::size_t i = 0; i < 2; ++i)
118  {
119  logger.assert_always((std::abs((0.25 * pc2)[i] - 0.8 - 2 * i) < 1e-12), "left multiplication");
120  }
121  logger.assert_always((std::abs((pv2 * pc2) - 1.44 * 8) < 1e-12), "in-product");
122  pc2 /= 4.;
123  for (std::size_t i = 0; i < 2; ++i)
124  {
125  logger.assert_always((std::abs(pc2[i] - 0.8 - 2 * i) < 1e-12), "divide operator");
126  }
127  for (std::size_t i = 0; i < 2; ++i)
128  {
129  logger.assert_always((std::abs((pc2 / -0.25)[i] + 3.2 + 8 * i) < 1e-12), "division");
130  }
131  pc2.axpy(-3., p2);
132  for (std::size_t i = 0; i < 2; ++i)
133  {
134  logger.assert_always((std::abs(pc2[i] + 1.6 + i) < 1e-12), "combined multiply and addition (y = ax + y)");
135  }
136 
137  x0.data();
138 
139  SmallVector<4> magicCopy(convenient.data());
140  logger.assert_always(std::abs(magicCopy(0) - 5.) < 1e-12, "Assignment operator from array does not copy!");
141  logger.assert_always(std::abs(magicCopy(1) - 6.) < 1e-12, "Assignment operator from array does not copy!");
142  logger.assert_always(std::abs(magicCopy(2) - 8.) < 1e-12, "Assignment operator from array does not copy!");
143  logger.assert_always(std::abs(magicCopy(3) - 9.) < 1e-12, "Assignment operator from array does not copy!");
144  convenient.data()[3] = 2.;
145  logger.assert_always(std::abs(magicCopy(3) - 9.) < 1e-12, "Assignment operator from array does not copy!");
146  logger.assert_always(std::abs(convenient(3) - 2.) < 1e-12, "Assignment operator from array does not copy!");
147 
148 
149  logger(INFO, "%%", pc2, convenient);
150  return 0;
151 }
LL< Log::INFO > INFO
Info log level.
Definition: Logger.cc:55
Logger< MERCURYDPM_LOGLEVEL > logger("MercuryKernel")
Definition of different loggers with certain modules. A user can define its own custom logger here.
Definition: SmallVector.h:62
unsigned int size() const
Definition: SmallVector.h:233
const Mdouble * data() const
Definition: SmallVector.h:238
const std::complex< Mdouble > i
Definition: ExtendedMath.h:51

References SmallVector< numberOfRows >::axpy(), SmallVector< numberOfRows >::data(), constants::i, INFO, logger, and SmallVector< numberOfRows >::size().