diff --git a/source/source_cell/magnetism.h b/source/source_cell/magnetism.h index b5359a82580..a2fabe50b6d 100644 --- a/source/source_cell/magnetism.h +++ b/source/source_cell/magnetism.h @@ -5,11 +5,16 @@ #include "source_base/vector3.h" #include +class MagnetismTest; + /** * @brief Class for magnetism calculations. */ class Magnetism { + /// @brief the unit test drives the private judge_parallel() helper directly + friend class MagnetismTest; + public: /// @brief Constructor Magnetism(); diff --git a/source/source_cell/read_pp.h b/source/source_cell/read_pp.h index c5bd8b3ec01..9d692c7c3b5 100644 --- a/source/source_cell/read_pp.h +++ b/source/source_cell/read_pp.h @@ -11,6 +11,10 @@ #include "source_base/matrix.h" #include "source_base/realarray.h" +class AtomPseudoTest; +class NCPPTest; +class ReadPPTest; + /** * @brief Pseudopot_upf class for reading pseudopotential files. * @@ -19,6 +23,12 @@ */ class Pseudopot_upf { + /// @brief the unit tests drive the private format readers and the + /// complete_default_* helpers directly; see source_cell/test/ + friend class AtomPseudoTest; + friend class NCPPTest; + friend class ReadPPTest; + public: /// PP_INFO /// PP_HEADER diff --git a/source/source_cell/test/atom_pseudo_test.cpp b/source/source_cell/test/atom_pseudo_test.cpp index 9386b223f81..38dc827e549 100644 --- a/source/source_cell/test/atom_pseudo_test.cpp +++ b/source/source_cell/test/atom_pseudo_test.cpp @@ -22,16 +22,22 @@ * - bcast upf201 pp info to other processes */ -#define private public #include "source_cell/read_pp.h" #include "source_cell/pseudo.h" #include "source_cell/atom_pseudo.h" -#undef private class AtomPseudoTest : public testing::Test { protected: std::unique_ptr upf{new Pseudopot_upf}; std::unique_ptr atom_pseudo{new Atom_pseudo}; + + // Pseudopot_upf declares this fixture a friend, but a TEST_F body lives in + // a class derived from it, and friendship is not inherited -- so the call + // into the private format reader has to happen here. + int read_pseudo_upf201(std::ifstream& ifs, Atom_pseudo& pp) const + { + return upf->read_pseudo_upf201(ifs, pp); + } }; TEST_F(AtomPseudoTest, SetDSo) @@ -43,7 +49,7 @@ TEST_F(AtomPseudoTest, SetDSo) std::ifstream ifs; ifs.open("./support/C.upf"); const double pseudo_rcut = 15.0; - upf->read_pseudo_upf201(ifs, *atom_pseudo); + read_pseudo_upf201(ifs, *atom_pseudo); upf->complete_default(*atom_pseudo, pseudo_rcut); ifs.close(); EXPECT_EQ(atom_pseudo->nh,14); @@ -75,7 +81,7 @@ TEST_F(AtomPseudoTest, BcastAtomPseudo) std::ifstream ifs; ifs.open("./support/C.upf"); const double pseudo_rcut = 15.0; - upf->read_pseudo_upf201(ifs, *atom_pseudo); + read_pseudo_upf201(ifs, *atom_pseudo); upf->complete_default(*atom_pseudo, pseudo_rcut); ifs.close(); } diff --git a/source/source_cell/test/magnetism_test.cpp b/source/source_cell/test/magnetism_test.cpp index cf1e949ec05..2df95ae3916 100644 --- a/source/source_cell/test/magnetism_test.cpp +++ b/source/source_cell/test/magnetism_test.cpp @@ -17,9 +17,7 @@ * - and non-collinear case with nspin = 4 */ -#define private public #include "source_cell/magnetism.h" -#undef private class MagnetismTest : public ::testing::Test @@ -34,6 +32,14 @@ class MagnetismTest : public ::testing::Test { delete magnetism; } + + // Magnetism declares this fixture a friend, but a TEST_F body lives in a + // class derived from it, and friendship is not inherited -- so the call + // into the private helper has to happen here. + bool judge_parallel(const double a[3], const ModuleBase::Vector3& b) const + { + return magnetism->judge_parallel(a, b); + } }; TEST_F(MagnetismTest, Magnetism) @@ -47,9 +53,9 @@ TEST_F(MagnetismTest, JudgeParallel) { double a[3] = {1.0, 0.0, 0.0}; ModuleBase::Vector3 b(1.0, 0.0, 0.0); - EXPECT_TRUE(magnetism->judge_parallel(a, b)); + EXPECT_TRUE(judge_parallel(a, b)); b = ModuleBase::Vector3(0.0, 1.0, 0.0); - EXPECT_FALSE(magnetism->judge_parallel(a, b)); + EXPECT_FALSE(judge_parallel(a, b)); } TEST_F(MagnetismTest, ComputeMagnetizationS2) diff --git a/source/source_cell/test/pseudo_nc_test.cpp b/source/source_cell/test/pseudo_nc_test.cpp index c5b0b636bc4..7c63df63838 100644 --- a/source/source_cell/test/pseudo_nc_test.cpp +++ b/source/source_cell/test/pseudo_nc_test.cpp @@ -21,15 +21,29 @@ * - print_pseudo */ -#define private public #include "source_cell/read_pp.h" #include "source_cell/atom_pseudo.h" -#undef private class NCPPTest : public testing::Test { protected: std::unique_ptr upf{new Pseudopot_upf}; std::unique_ptr ncpp{new Atom_pseudo}; + + // Pseudopot_upf declares this fixture a friend, but a TEST_F body lives in + // a class derived from it, and friendship is not inherited -- so the calls + // into the private reader and the complete_default_* helpers happen here. + int read_pseudo_upf201(std::ifstream& ifs, Atom_pseudo& pp) const + { + return upf->read_pseudo_upf201(ifs, pp); + } + void complete_default_h(Atom_pseudo& pp) const + { + upf->complete_default_h(pp); + } + void complete_default_atom(Atom_pseudo& pp, const double pseudo_rcut) const + { + upf->complete_default_atom(pp, pseudo_rcut); + } }; TEST_F(NCPPTest, SetPseudoH) @@ -37,9 +51,9 @@ TEST_F(NCPPTest, SetPseudoH) std::ifstream ifs; //set ifs.open("./support/C.upf"); - upf->read_pseudo_upf201(ifs, *ncpp); + read_pseudo_upf201(ifs, *ncpp); //set_pseudo_h - upf->complete_default_h(*ncpp); + complete_default_h(*ncpp); if(!ncpp->has_so) { @@ -62,10 +76,10 @@ TEST_F(NCPPTest, SetPseudoAtom) //set ifs.open("./support/C.upf"); const double pseudo_rcut = 15.0; - upf->read_pseudo_upf201(ifs, *ncpp); + read_pseudo_upf201(ifs, *ncpp); //set_pseudo_atom - upf->complete_default_h(*ncpp); - upf->complete_default_atom(*ncpp, pseudo_rcut); + complete_default_h(*ncpp); + complete_default_atom(*ncpp, pseudo_rcut); EXPECT_EQ(ncpp->rcut,pseudo_rcut); if(!ncpp->nlcc) @@ -86,12 +100,12 @@ TEST_F(NCPPTest, SetPseudoNC) ifs.open("./support/C.upf"); const double pseudo_rcut = 15.0; // set pseudo nbeta = 0 - upf->read_pseudo_upf201(ifs, *ncpp); + read_pseudo_upf201(ifs, *ncpp); ncpp->nbeta = 0; upf->complete_default(*ncpp, pseudo_rcut); EXPECT_EQ(ncpp->nh,0); // set pseudo nbeta > 0 - upf->read_pseudo_upf201(ifs, *ncpp); + read_pseudo_upf201(ifs, *ncpp); upf->complete_default(*ncpp, pseudo_rcut); EXPECT_EQ(ncpp->nh,14); EXPECT_EQ(ncpp->kkbeta,132); @@ -105,7 +119,7 @@ TEST_F(NCPPTest, PrintNC) //set ifs.open("./support/C.upf"); const double pseudo_rcut = 15.0; - upf->read_pseudo_upf201(ifs, *ncpp); + read_pseudo_upf201(ifs, *ncpp); upf->complete_default(*ncpp, pseudo_rcut); ifs.close(); //print diff --git a/source/source_cell/test/read_pp_test.cpp b/source/source_cell/test/read_pp_test.cpp index 66e3db009e1..ab354d48630 100644 --- a/source/source_cell/test/read_pp_test.cpp +++ b/source/source_cell/test/read_pp_test.cpp @@ -59,23 +59,63 @@ * - average_p: modulate the soc effect in pseudopotential */ -#define private public #include "source_cell/read_pp.h" #include "source_cell/atom_pseudo.h" -#undef private class ReadPPTest : public testing::Test { protected: std::string output; std::unique_ptr read_pp{new Pseudopot_upf}; std::unique_ptr upf{new Atom_pseudo}; + + // Pseudopot_upf declares this fixture a friend, but a TEST_F body lives in + // a class derived from it, and friendship is not inherited -- so every call + // into a private format reader or helper is routed through these wrappers. + int read_pseudo_upf(std::ifstream& ifs, Atom_pseudo& pp) const + { + return read_pp->read_pseudo_upf(ifs, pp); + } + int read_pseudo_upf201(std::ifstream& ifs, Atom_pseudo& pp) const + { + return read_pp->read_pseudo_upf201(ifs, pp); + } + int read_pseudo_vwr(std::ifstream& ifs, Atom_pseudo& pp) const + { + return read_pp->read_pseudo_vwr(ifs, pp); + } + int read_pseudo_blps(std::ifstream& ifs, Atom_pseudo& pp) const + { + return read_pp->read_pseudo_blps(ifs, pp); + } + int set_pseudo_type(const std::string& fn, std::string& type) const + { + return read_pp->set_pseudo_type(fn, type); + } + void setqfnew(const int& nqf, + const int& mesh, + const int& l, + const int& n, + const double* qfcoef, + const double* r, + double* rho) const + { + read_pp->setqfnew(nqf, mesh, l, n, qfcoef, r, rho); + } + std::string& trim(std::string& in_str) const + { + return read_pp->trim(in_str); + } + std::string trimend(std::string& in_str) const + { + return read_pp->trimend(in_str); + } }; TEST_F(ReadPPTest, ReadUPF100_Coulomb) { std::ifstream ifs; ifs.open("./support/Te.pbe-coulomb.UPF"); - read_pp->read_pseudo_upf(ifs, *upf); + read_pseudo_upf(ifs, *upf); EXPECT_TRUE(upf->vloc_at.empty()); EXPECT_EQ(read_pp->coulomb_potential, true); EXPECT_EQ(upf->tvanp, false); @@ -89,7 +129,7 @@ TEST_F(ReadPPTest, ReadUPF100) { std::ifstream ifs; ifs.open("./support/Te.pbe-rrkj.UPF"); - read_pp->read_pseudo_upf(ifs, *upf); + read_pseudo_upf(ifs, *upf); EXPECT_FALSE(upf->has_so); // no soc info EXPECT_EQ(upf->nv,0); // number of version EXPECT_EQ(upf->psd,"Te"); // element label @@ -170,7 +210,7 @@ TEST_F(ReadPPTest, ReadUPF100USPP) { std::ifstream ifs; ifs.open("./support/fe_pbe_v1.5.uspp.F.UPF"); - read_pp->read_pseudo_upf(ifs, *upf); + read_pseudo_upf(ifs, *upf); EXPECT_FALSE(upf->has_so); // has soc info EXPECT_FALSE(read_pp->q_with_l); // q_with_l EXPECT_EQ(upf->nv, 0); // number of version @@ -286,7 +326,7 @@ TEST_F(ReadPPTest, ReadUPF201_Coulomb) { std::ifstream ifs; ifs.open("./support/Al.pbe-coulomb.UPF"); - read_pp->read_pseudo_upf201(ifs, *upf); + read_pseudo_upf201(ifs, *upf); EXPECT_TRUE(upf->vloc_at.empty()); EXPECT_EQ(read_pp->coulomb_potential, true); EXPECT_EQ(upf->nbeta, 0); @@ -299,7 +339,7 @@ TEST_F(ReadPPTest, ReadUPF201) { std::ifstream ifs; ifs.open("./support/Cu_ONCV_PBE-1.0.upf"); - read_pp->read_pseudo_upf201(ifs, *upf); + read_pseudo_upf201(ifs, *upf); EXPECT_EQ(upf->psd,"Cu"); EXPECT_EQ(upf->pp_type,"NC"); EXPECT_FALSE(upf->has_so); @@ -352,7 +392,7 @@ TEST_F(ReadPPTest, ReadUSPPUPF201) { std::ifstream ifs; ifs.open("./support/Al.pbe-sp-van.UPF"); - read_pp->read_pseudo_upf201(ifs, *upf); + read_pseudo_upf201(ifs, *upf); EXPECT_EQ(upf->psd, "Al"); EXPECT_EQ(upf->pp_type, "US"); EXPECT_EQ(read_pp->relativistic, "no"); @@ -430,9 +470,9 @@ TEST_F(ReadPPTest, HeaderErr2011) std::ifstream ifs; // 1st ifs.open("./support/HeaderError1"); - //read_pp->read_pseudo_upf201(ifs, *upf); + //read_pseudo_upf201(ifs, *upf); testing::internal::CaptureStdout(); - EXPECT_EXIT(read_pp->read_pseudo_upf201(ifs, *upf), + EXPECT_EXIT(read_pseudo_upf201(ifs, *upf), ::testing::ExitedWithCode(1),""); output = testing::internal::GetCapturedStdout(); EXPECT_THAT(output, testing::HasSubstr("Found no PP_HEADER")); @@ -444,9 +484,9 @@ TEST_F(ReadPPTest, HeaderErr2012) std::ifstream ifs; // 2nd ifs.open("./support/HeaderError2"); - //read_pp->read_pseudo_upf201(ifs, *upf); + //read_pseudo_upf201(ifs, *upf); testing::internal::CaptureStdout(); - EXPECT_EXIT(read_pp->read_pseudo_upf201(ifs, *upf), + EXPECT_EXIT(read_pseudo_upf201(ifs, *upf), ::testing::ExitedWithCode(1),""); output = testing::internal::GetCapturedStdout(); EXPECT_THAT(output, testing::HasSubstr("SEMI-LOCAL PSEUDOPOTENTIAL IS NOT SUPPORTED")); @@ -458,9 +498,9 @@ TEST_F(ReadPPTest, HeaderErr2013) std::ifstream ifs; // 3rd ifs.open("./support/HeaderError3"); - //read_pp->read_pseudo_upf201(ifs, *upf); + //read_pseudo_upf201(ifs, *upf); testing::internal::CaptureStdout(); - EXPECT_EXIT(read_pp->read_pseudo_upf201(ifs, *upf), + EXPECT_EXIT(read_pseudo_upf201(ifs, *upf), ::testing::ExitedWithCode(1),""); output = testing::internal::GetCapturedStdout(); EXPECT_THAT(output, testing::HasSubstr("PAW POTENTIAL IS NOT SUPPORTED")); @@ -474,7 +514,7 @@ TEST_F(ReadPPTest, HeaderErr2015) GlobalV::ofs_warning.open("warning.log"); ifs.open("./support/HeaderError5"); upf->mesh = 1; // avoid assert(pp.mesh > 0) in line 406 of read_pp_upf201.cpp - read_pp->read_pseudo_upf201(ifs, *upf); + read_pseudo_upf201(ifs, *upf); GlobalV::ofs_warning.close(); ifs.close(); ifs.open("warning.log"); @@ -491,7 +531,7 @@ TEST_F(ReadPPTest, ReadUPF201FR) std::ifstream ifs; // this is a dojo full-relativisitic pp ifs.open("./support/C.upf"); - read_pp->read_pseudo_upf201(ifs, *upf); + read_pseudo_upf201(ifs, *upf); EXPECT_EQ(upf->psd,"C"); EXPECT_TRUE(upf->has_so); EXPECT_TRUE(upf->nlcc); @@ -544,7 +584,7 @@ TEST_F(ReadPPTest, ReadUPF201MESH2) std::ifstream ifs; // this pp file has gipaw, thus a different header ifs.open("./support/Fe.pbe-sp-mt_gipaw.UPF"); - read_pp->read_pseudo_upf201(ifs, *upf); + read_pseudo_upf201(ifs, *upf); EXPECT_EQ(upf->psd,"Fe"); ifs.close(); } @@ -554,7 +594,7 @@ TEST_F(ReadPPTest, VWR) std::ifstream ifs; // this pp file is a vwr type of pp ifs.open("./support/vwr.Si"); - read_pp->read_pseudo_vwr(ifs, *upf); + read_pseudo_vwr(ifs, *upf); EXPECT_EQ(upf->xc_func,"PZ"); EXPECT_EQ(upf->pp_type,"NC"); EXPECT_FALSE(upf->tvanp); @@ -589,7 +629,7 @@ TEST_F(ReadPPTest, BLPS) std::ifstream ifs; // this pp file is a vwr type of pp ifs.open("./support/si.lda.lps"); - read_pp->read_pseudo_blps(ifs, *upf); + read_pseudo_blps(ifs, *upf); EXPECT_FALSE(upf->nlcc); EXPECT_FALSE(upf->tvanp); EXPECT_FALSE(upf->has_so); @@ -612,20 +652,20 @@ TEST_F(ReadPPTest, SetPseudoType) { std::string pp_address = "./support/Cu_ONCV_PBE-1.0.upf"; std::string type = "auto"; - read_pp->set_pseudo_type(pp_address,type); + set_pseudo_type(pp_address,type); EXPECT_EQ(type,"upf201"); pp_address = "./support/Te.pbe-rrkj.UPF"; - read_pp->set_pseudo_type(pp_address,type); + set_pseudo_type(pp_address,type); EXPECT_EQ(type,"upf"); } TEST_F(ReadPPTest, Trim) { std::string tmp_string = " aaa \t bbb\t "; - output = read_pp->trim(tmp_string); + output = trim(tmp_string); EXPECT_EQ(output,"aaabbb"); tmp_string = " \taaa\tbbb\t "; - output = read_pp->trimend(tmp_string); + output = trimend(tmp_string); EXPECT_EQ(output,"aaa\tbbb"); } @@ -655,7 +695,7 @@ TEST_F(ReadPPTest, SetUpfQ) { std::ifstream ifs; ifs.open("./support/Al.pbe-sp-van.UPF"); - read_pp->read_pseudo_upf201(ifs, *upf); + read_pseudo_upf201(ifs, *upf); read_pp->set_upf_q(*upf); EXPECT_DOUBLE_EQ(upf->qfuncl(0, 0, 0), 0.0); EXPECT_DOUBLE_EQ(upf->qfuncl(0, 0, 100), 7.8994151918886213e-06); @@ -691,7 +731,7 @@ TEST_F(ReadPPTest, SetQfNew) } // Call the function under test - read_pp->setqfnew(nqf, mesh, l, n, qfcoef, r, rho); + setqfnew(nqf, mesh, l, n, qfcoef, r, rho); // Validate the output for (int ir = 0; ir < mesh; ++ir) @@ -761,7 +801,7 @@ TEST_F(ReadPPTest, AverageErrReturns) // LSPINORB = 0 std::ifstream ifs; ifs.open("./support/Si.rel-pbe-rrkj.UPF"); - read_pp->read_pseudo_upf(ifs, *upf); + read_pseudo_upf(ifs, *upf); EXPECT_TRUE(upf->has_so); // has soc info const bool lspinorb_0 = false; ierr = read_pp->average_p(lambda, *upf, lspinorb_0); @@ -779,7 +819,7 @@ TEST_F(ReadPPTest, AverageLSPINORB0) std::ifstream ifs; // this is a dojo full-relativisitic pp ifs.open("./support/C.upf"); - read_pp->read_pseudo_upf201(ifs, *upf); + read_pseudo_upf201(ifs, *upf); EXPECT_TRUE(upf->has_so); // has soc info int ierr; double lambda = 1.0; @@ -796,7 +836,7 @@ TEST_F(ReadPPTest, AverageLSPINORB1) std::ifstream ifs; // this is a dojo full-relativisitic pp ifs.open("./support/C.upf"); - read_pp->read_pseudo_upf201(ifs, *upf); + read_pseudo_upf201(ifs, *upf); EXPECT_TRUE(upf->has_so); // has soc info int ierr; double lambda = 1.1;