From f72bd59f5b143b9b03a1a0b7364f817658d829cd Mon Sep 17 00:00:00 2001 From: ralfulrich <ralf.ulrich@kit.edu> Date: Thu, 22 Oct 2020 09:02:00 +0200 Subject: [PATCH] better unit tests --- .../ProcessSequence/testProcessSequence.cc | 38 ++++++++++++------- .../testSwitchProcessSequence.cc | 22 +++++------ 2 files changed, 36 insertions(+), 24 deletions(-) diff --git a/Framework/ProcessSequence/testProcessSequence.cc b/Framework/ProcessSequence/testProcessSequence.cc index f303d0e80..c1a47fafb 100644 --- a/Framework/ProcessSequence/testProcessSequence.cc +++ b/Framework/ProcessSequence/testProcessSequence.cc @@ -65,7 +65,7 @@ public: inline EProcessReturn DoContinuous(D& d, T&) const { cout << "ContinuousProcess2::DoContinuous" << endl; checkCont |= 2; - for (int i = 0; i < nData; ++i) d.data_[i] += 0.933; + for (int i = 0; i < nData; ++i) d.data_[i] += 0.111; return EProcessReturn::eOk; } }; @@ -84,7 +84,7 @@ public: inline EProcessReturn DoContinuous(D& d, T&) const { cout << "ContinuousProcess3::DoContinuous" << endl; checkCont |= 4; - for (int i = 0; i < nData; ++i) d.data_[i] += 0.933; + for (int i = 0; i < nData; ++i) d.data_[i] += 0.333; return EProcessReturn::eOk; } }; @@ -124,8 +124,9 @@ public: } template <typename TView> - inline EProcessReturn DoInteraction(TView&) const { + inline EProcessReturn DoInteraction(TView& v) const { checkInteract |= 2; + for (int i = 0; i < nData; ++i) v.parent().data_[i] /= 1.1; cout << "Process2::DoInteraction" << endl; return EProcessReturn::eOk; } @@ -147,8 +148,9 @@ public: } template <typename TView> - inline EProcessReturn DoInteraction(TView&) const { + inline EProcessReturn DoInteraction(TView& v) const { checkInteract |= 4; + for (int i = 0; i < nData; ++i) v.parent().data_[i] *= 1.01; cout << "Process3::DoInteraction" << endl; return EProcessReturn::eOk; } @@ -284,6 +286,9 @@ TEST_CASE("Process Sequence", "[Process Sequence]") { GrammageType const tot = sequence2.GetInteractionLength(particle); InverseGrammageType const tot_inv = sequence2.GetInverseInteractionLength(particle); cout << "lambda_tot=" << tot << "; lambda_tot_inv=" << tot_inv << endl; + + CHECK(tot/1_g*square(1_cm) == 12); + CHECK(tot_inv*1_g/square(1_cm) == 1./12); globalCount = 0; } @@ -301,15 +306,17 @@ TEST_CASE("Process Sequence", "[Process Sequence]") { InverseTimeType const tot_inv = sequence2.GetInverseLifetime(particle); cout << "lambda_tot=" << tot << "; lambda_tot_inv=" << tot_inv << endl; + CHECK(tot/1_s == 1); + CHECK(tot_inv*1_s == 1.); globalCount = 0; } SECTION("sectionTwo") { globalCount = 0; - ContinuousProcess1 cp1(0); - ContinuousProcess2 cp2(1); - Process2 m2(2); - Process3 m3(3); + ContinuousProcess1 cp1(0); // += 0.933 + ContinuousProcess2 cp2(1); // += 0.111 + Process2 m2(2); // /= 1.1 + Process3 m3(3); // *= 1.01 auto sequence2 = sequence(cp1, m2, m3, cp2); @@ -320,15 +327,20 @@ TEST_CASE("Process Sequence", "[Process Sequence]") { globalCount = 0; cout << "-->docont" << endl; - sequence2.DoContinuous(particle, track); - cout << "-->dodisc" << endl; - cout << "-->done" << endl; - + // validation data + double test_data[nData] = {0}; + const int nLoop = 5; cout << "Running loop with n=" << nLoop << endl; - for (int i = 0; i < nLoop; ++i) { sequence2.DoContinuous(particle, track); } + for (int iLoop = 0; iLoop < nLoop; ++iLoop) { + for (int i=0; i<nData; ++i) { + test_data[i] += 0.933 + 0.111; + } + sequence2.DoContinuous(particle, track); + } for (int i = 0; i < nData; i++) { cout << "data_[" << i << "]=" << particle.data_[i] << endl; + CHECK(particle.data_[i] == Approx(test_data[i]).margin(1e-9)); } cout << "done" << endl; } diff --git a/Framework/ProcessSequence/testSwitchProcessSequence.cc b/Framework/ProcessSequence/testSwitchProcessSequence.cc index c596613f4..a778f2efb 100644 --- a/Framework/ProcessSequence/testSwitchProcessSequence.cc +++ b/Framework/ProcessSequence/testSwitchProcessSequence.cc @@ -134,8 +134,8 @@ TEST_CASE("SwitchProcess from InteractionProcess") { // low energy process returns 1 kg/m² SECTION("interaction length") { - REQUIRE(switchProcess.GetInteractionLength(p) / kgMSq == Approx(1)); - REQUIRE(seq.GetInteractionLength(p) / kgMSq == Approx(3. / 4)); + CHECK(switchProcess.GetInteractionLength(p) / kgMSq == Approx(1)); + CHECK(seq.GetInteractionLength(p) / kgMSq == Approx(3. / 4)); } } @@ -145,8 +145,8 @@ TEST_CASE("SwitchProcess from InteractionProcess") { // high energy process returns 2 kg/m² SECTION("interaction length") { - REQUIRE(switchProcess.GetInteractionLength(p) / kgMSq == Approx(2)); - REQUIRE(seq.GetInteractionLength(p) / kgMSq == Approx(6. / 5)); + CHECK(switchProcess.GetInteractionLength(p) / kgMSq == Approx(2)); + CHECK(seq.GetInteractionLength(p) / kgMSq == Approx(6. / 5)); } // high energy process creates 2 secondaries @@ -159,7 +159,7 @@ TEST_CASE("SwitchProcess from InteractionProcess") { InverseGrammageType invLambda = 0 / kgMSq; switchProcess.SelectInteraction(p, projectile, 0.01 / kgMSq, invLambda); - REQUIRE(view.getSize() == 2); + CHECK(view.getSize() == 2); } } } @@ -182,8 +182,8 @@ TEST_CASE("SwitchProcess from ProcessSequence") { auto p = stack.GetNextParticle(); SECTION("interaction length") { - REQUIRE(switchProcess.GetInteractionLength(p) / kgMSq == Approx(2. / 3)); - REQUIRE(completeSeq.GetInteractionLength(p) / kgMSq == Approx(4. / 7)); + CHECK(switchProcess.GetInteractionLength(p) / kgMSq == Approx(2. / 3)); + CHECK(completeSeq.GetInteractionLength(p) / kgMSq == Approx(4. / 7)); } SECTION("SelectInteraction") { @@ -207,7 +207,7 @@ TEST_CASE("SwitchProcess from ProcessSequence") { auto const mean = std::accumulate(numberOfSecondaries.cbegin(), numberOfSecondaries.cend(), 0.) / numberOfSecondaries.size(); - REQUIRE(mean == Approx(12. / 7.).margin(0.01)); + CHECK(mean == Approx(12. / 7.).margin(0.01)); } } @@ -216,8 +216,8 @@ TEST_CASE("SwitchProcess from ProcessSequence") { auto p = stack.GetNextParticle(); SECTION("interaction length") { - REQUIRE(switchProcess.GetInteractionLength(p) / kgMSq == Approx(3)); - REQUIRE(completeSeq.GetInteractionLength(p) / kgMSq == Approx(12. / 7.)); + CHECK(switchProcess.GetInteractionLength(p) / kgMSq == Approx(3)); + CHECK(completeSeq.GetInteractionLength(p) / kgMSq == Approx(12. / 7.)); } SECTION("SelectInteraction") { @@ -241,7 +241,7 @@ TEST_CASE("SwitchProcess from ProcessSequence") { auto const mean = std::accumulate(numberOfSecondaries.cbegin(), numberOfSecondaries.cend(), 0.) / numberOfSecondaries.size(); - REQUIRE(mean == Approx(24. / 7.).margin(0.01)); + CHECK(mean == Approx(24. / 7.).margin(0.01)); } } } -- GitLab