Skip to content

Commit

Permalink
Better float testing using Catch2's Approx
Browse files Browse the repository at this point in the history
  • Loading branch information
tdegeus committed Oct 14, 2020
1 parent 7467e81 commit edd349f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 27 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,7 @@ This requires the following changes:
## v0.7.0

* Exposing "checkYieldBoundLeft" and "checkYieldBoundRight" from QPot v0.2.0.
* Proper float testing using Catch2.

## v0.6.4

Expand Down
53 changes: 26 additions & 27 deletions test/Cartesian2d.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@

#include <catch2/catch.hpp>
#include <xtensor/xrandom.hpp>
#include <GMatElastoPlasticQPot/Cartesian2d.h>

#define ISCLOSE(a,b) REQUIRE_THAT((a), Catch::WithinAbs((b), 1.e-12));

#include <GMatElastoPlasticQPot/Cartesian2d.h>

namespace GM = GMatElastoPlasticQPot::Cartesian2d;

template <class T, class S>
Expand Down Expand Up @@ -89,7 +88,7 @@ SECTION("Hydrostatic - Tensor2")
GM::Tensor2 A = xt::random::randn<double>({2, 2});
A(0, 0) = 1.0;
A(1, 1) = 1.0;
ISCLOSE(GM::Hydrostatic(A)(), 1.0);
REQUIRE(GM::Hydrostatic(A)() == Approx(1.0));
}

SECTION("Hydrostatic - List")
Expand Down Expand Up @@ -127,7 +126,7 @@ SECTION("Epsd - Tensor2")
GM::Tensor2 A = xt::zeros<double>({2, 2});
A(0, 1) = 1.0;
A(1, 0) = 1.0;
ISCLOSE(GM::Epsd(A)(), 1.0);
REQUIRE(GM::Epsd(A)() == Approx(1.0));
}

SECTION("Epsd - List")
Expand Down Expand Up @@ -165,7 +164,7 @@ SECTION("Sigd - Tensor2")
GM::Tensor2 A = xt::zeros<double>({2, 2});
A(0, 1) = 1.0;
A(1, 0) = 1.0;
ISCLOSE(GM::Sigd(A)(), 2.0);
REQUIRE(GM::Sigd(A)() == Approx(2.0));
}

SECTION("Sigd - List")
Expand Down Expand Up @@ -212,11 +211,11 @@ SECTION("Elastic - stress")
mat.setStrain(Eps);
auto Sig = mat.Stress();

ISCLOSE(Sig(0, 0), K * epsm);
ISCLOSE(Sig(1, 1), K * epsm);
ISCLOSE(Sig(0, 1), G * gamma);
ISCLOSE(Sig(1, 0), G * gamma);
ISCLOSE(mat.energy(), K * std::pow(epsm, 2.0) + G * std::pow(gamma, 2.0));
REQUIRE(Sig(0, 0) == Approx(K * epsm));
REQUIRE(Sig(1, 1) == Approx(K * epsm));
REQUIRE(Sig(0, 1) == Approx(G * gamma));
REQUIRE(Sig(1, 0) == Approx(G * gamma));
REQUIRE(mat.energy() == Approx(K * std::pow(epsm, 2.0) + G * std::pow(gamma, 2.0)));
}

SECTION("Cusp - stress")
Expand All @@ -233,15 +232,15 @@ SECTION("Cusp - stress")
mat.setStrain(Eps);
auto Sig = mat.Stress();

ISCLOSE(Sig(0, 0), K * epsm);
ISCLOSE(Sig(1, 1), K * epsm);
ISCLOSE(Sig(0, 1), 0.0);
ISCLOSE(Sig(1, 0), 0.0);
ISCLOSE(mat.epsp(), 0.02);
REQUIRE(Sig(0, 0) == Approx(K * epsm));
REQUIRE(Sig(1, 1) == Approx(K * epsm));
REQUIRE(Sig(0, 1) == Approx(0.0));
REQUIRE(Sig(1, 0) == Approx(0.0));
REQUIRE(mat.epsp() == Approx(0.02));
REQUIRE(mat.currentIndex() == 1);
REQUIRE(mat.checkYieldBoundLeft());
REQUIRE(mat.checkYieldBoundRight());
ISCLOSE(mat.energy(), K * std::pow(epsm, 2.0) + G * (0.0 - std::pow(0.01, 2.0)));
REQUIRE(mat.energy() == Approx(K * std::pow(epsm, 2.0) + G * (0.0 - std::pow(0.01, 2.0))));

epsm *= 2.0;
gamma *= 1.9;
Expand All @@ -252,15 +251,15 @@ SECTION("Cusp - stress")
mat.setStrain(Eps);
Sig = mat.Stress();

ISCLOSE(Sig(0, 0), K * epsm);
ISCLOSE(Sig(1, 1), K * epsm);
ISCLOSE(Sig(0, 1), G * (gamma - 0.04));
ISCLOSE(Sig(1, 0), G * (gamma - 0.04));
ISCLOSE(mat.epsp(), 0.04);
REQUIRE(Sig(0, 0) == Approx(K * epsm));
REQUIRE(Sig(1, 1) == Approx(K * epsm));
REQUIRE(Sig(0, 1) == Approx(G * (gamma - 0.04)));
REQUIRE(Sig(1, 0) == Approx(G * (gamma - 0.04)));
REQUIRE(mat.epsp() == Approx(0.04));
REQUIRE(mat.currentIndex() == 2);
REQUIRE(mat.checkYieldBoundLeft());
REQUIRE(mat.checkYieldBoundRight());
ISCLOSE(mat.energy(), K * std::pow(epsm, 2.0) + G * (std::pow(gamma - 0.04, 2.0) - std::pow(0.01, 2.0)));
REQUIRE(mat.energy() == Approx(K * std::pow(epsm, 2.0) + G * (std::pow(gamma - 0.04, 2.0) - std::pow(0.01, 2.0))));
}

SECTION("Smooth - stress")
Expand All @@ -277,11 +276,11 @@ SECTION("Smooth - stress")
mat.setStrain(Eps);
auto Sig = mat.Stress();

ISCLOSE(Sig(0, 0), K * epsm);
ISCLOSE(Sig(1, 1), K * epsm);
ISCLOSE(Sig(0, 1), 0.0);
ISCLOSE(Sig(1, 0), 0.0);
ISCLOSE(mat.epsp(), 0.02);
REQUIRE(Sig(0, 0) == Approx(K * epsm));
REQUIRE(Sig(1, 1) == Approx(K * epsm));
REQUIRE(Sig(0, 1) == Approx(0.0));
REQUIRE(Sig(1, 0) == Approx(0.0));
REQUIRE(mat.epsp() == Approx(0.02));
REQUIRE(mat.currentIndex() == 1);
REQUIRE(mat.checkYieldBoundLeft());
REQUIRE(mat.checkYieldBoundRight());
Expand Down

0 comments on commit edd349f

Please sign in to comment.