| 1 |
#include <sstream> |
| 2 |
#include "gtest/gtest.h" |
| 3 |
#include "stk_middle_mesh/complex_utils.hpp" |
| 4 |
|
| 5 |
namespace { |
| 6 |
|
| 7 |
using Complex = std::complex<double>; |
| 8 |
|
| 9 |
void expect_near(const Complex& a, const Complex& b, double tol) |
| 10 |
{ |
| 11 |
EXPECT_NEAR(a.real(), b.real(), tol); |
| 12 |
EXPECT_NEAR(a.imag(), b.imag(), tol); |
| 13 |
} |
| 14 |
|
| 15 |
} |
| 16 |
|
| 17 |
TEST(ComplexUtils, Addition) |
| 18 |
{ |
| 19 |
Complex a(1, 2); |
| 20 |
expect_near(a + 2, Complex(3, 2), 1e-13); |
| 21 |
expect_near(2 + a, Complex(3, 2), 1e-13); |
| 22 |
} |
| 23 |
|
| 24 |
TEST(ComplexUtils, Subtraction) |
| 25 |
{ |
| 26 |
Complex a(1, 2); |
| 27 |
expect_near(a - 2, Complex(-1, 2), 1e-13); |
| 28 |
expect_near(2 - a, Complex( 1, -2), 1e-13); |
| 29 |
} |
| 30 |
|
| 31 |
TEST(ComplexUtils, Multiplication) |
| 32 |
{ |
| 33 |
Complex a(1, 2); |
| 34 |
expect_near(a * 2, Complex(2, 4), 1e-13); |
| 35 |
expect_near(2 * a, Complex(2, 4), 1e-13); |
| 36 |
} |
| 37 |
|
| 38 |
TEST(ComplexUtils, Division) |
| 39 |
{ |
| 40 |
Complex a(1, 2); |
| 41 |
expect_near(a / 2, Complex(0.5, 1), 1e-13); |
| 42 |
expect_near(2 / a, Complex(2.0/5, -4.0/5), 1e-13); |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
// This stuff is provided by the standard library, the tests are here just to |
| 47 |
// verify it really works |
| 48 |
TEST(ComplexUtils, Power) |
| 49 |
{ |
| 50 |
Complex a(1, 2); |
| 51 |
expect_near(std::pow(a, 2), Complex(-3, 4), 1e-13); |
| 52 |
expect_near(std::pow(a, 2.0), Complex(-3, 4), 1e-13); |
| 53 |
} |
| 54 |
|
| 55 |
TEST(ComplexUtils, OutputOperator) |
| 56 |
{ |
| 57 |
Complex a(1, 2); |
| 58 |
std::stringstream ss; |
| 59 |
ss << a; |
| 60 |
EXPECT_EQ(ss.str(), "(1,2)"); |
| 61 |
|
| 62 |
|
| 63 |
} |