Skip to content

Commit 5dcb8d5

Browse files
author
abacus_fixer
committed
refactor(read_rhog_test): replace raw new/delete with std::vector
- PW_Basis* rhopw (new+delete) -> value member PW_Basis rhopw - new complex*[1] + new complex[1471] (double-delete chain) -> vector<vector<complex>> rhog_data + vector<complex*> rhog - Remove TearDown() entirely (RAII handles cleanup) - Update call sites to use &rhopw and rhog.data()
1 parent 1be8c20 commit 5dcb8d5

1 file changed

Lines changed: 16 additions & 28 deletions

File tree

source/source_io/test/read_rhog_test.cpp

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,15 @@
1717
class ReadRhogTest : public ::testing::Test
1818
{
1919
protected:
20-
ModulePW::PW_Basis* rhopw = nullptr;
21-
std::complex<double>** rhog = nullptr;
20+
ModulePW::PW_Basis rhopw;
21+
std::vector<std::vector<std::complex<double>>> rhog_data;
22+
std::vector<std::complex<double>*> rhog;
2223
Parallel::ParaWorld pw_world = Parallel::make_pw_world();
2324

2425
virtual void SetUp()
2526
{
26-
rhopw = new ModulePW::PW_Basis;
27-
rhog = new std::complex<double>*[1];
28-
rhog[0] = new std::complex<double>[1471];
29-
}
30-
virtual void TearDown()
31-
{
32-
if (rhopw != nullptr) {
33-
delete rhopw;
34-
}
35-
if (rhog[0] != nullptr) {
36-
delete[] rhog[0];
37-
}
38-
if (rhog != nullptr) {
39-
delete[] rhog;
40-
}
27+
rhog_data.resize(1, std::vector<std::complex<double>>(1471));
28+
rhog.push_back(rhog_data[0].data());
4129
}
4230
};
4331

@@ -46,14 +34,14 @@ TEST_F(ReadRhogTest, ReadRhog)
4634
{
4735
std::string filename = "./support/charge-density.dat";
4836
#ifdef __MPI
49-
rhopw->initmpi(GlobalV::NPROC_IN_POOL, GlobalV::RANK_IN_POOL, MPI_COMM_WORLD);
37+
rhopw.initmpi(pw_world.size(), pw_world.rank(), pw_world.comm());
5038
#endif
51-
rhopw->initgrids(6.5, ModuleBase::Matrix3(-0.5, 0.0, 0.5, 0.0, 0.5, 0.5, -0.5, 0.5, 0.0), 120);
52-
rhopw->initparameters(false, 120);
53-
rhopw->setuptransform();
54-
rhopw->collect_local_pw();
39+
rhopw.initgrids(6.5, ModuleBase::Matrix3(-0.5, 0.0, 0.5, 0.0, 0.5, 0.5, -0.5, 0.5, 0.0), 120);
40+
rhopw.initparameters(false, 120);
41+
rhopw.setuptransform();
42+
rhopw.collect_local_pw();
5543

56-
bool result = ModuleIO::read_rhog(filename, rhopw, 1, rhog, pw_world);
44+
bool result = ModuleIO::read_rhog(filename, &rhopw, 1, rhog.data(), pw_world);
5745

5846
EXPECT_TRUE(result);
5947
EXPECT_DOUBLE_EQ(rhog[0][0].real(), -1.0304462993299456e-05);
@@ -70,7 +58,7 @@ TEST_F(ReadRhogTest, NotFoundFile)
7058
std::string filename = "notfound.txt";
7159

7260
GlobalV::ofs_warning.open("test_read_rhog.txt");
73-
bool result = ModuleIO::read_rhog(filename, rhopw, 1, rhog, pw_world);
61+
bool result = ModuleIO::read_rhog(filename, &rhopw, 1, rhog.data(), pw_world);
7462
GlobalV::ofs_warning.close();
7563

7664
std::ifstream ifs_running("test_read_rhog.txt");
@@ -90,10 +78,10 @@ TEST_F(ReadRhogTest, NotFoundFile)
9078
TEST_F(ReadRhogTest, InconsistentGammaOnly)
9179
{
9280
std::string filename = "./support/charge-density.dat";
93-
rhopw->gamma_only = true;
81+
rhopw.gamma_only = true;
9482

9583
GlobalV::ofs_warning.open("test_read_rhog.txt");
96-
bool result = ModuleIO::read_rhog(filename, rhopw, 2, rhog, pw_world);
84+
bool result = ModuleIO::read_rhog(filename, &rhopw, 2, rhog.data(), pw_world);
9785
GlobalV::ofs_warning.close();
9886

9987
std::ifstream ifs_running("test_read_rhog.txt");
@@ -116,10 +104,10 @@ TEST_F(ReadRhogTest, InconsistentGammaOnly)
116104
TEST_F(ReadRhogTest, SomePWMissing)
117105
{
118106
std::string filename = "./support/charge-density.dat";
119-
rhopw->npwtot = 2000;
107+
rhopw.npwtot = 2000;
120108

121109
GlobalV::ofs_warning.open("test_read_rhog.txt");
122-
bool result = ModuleIO::read_rhog(filename, rhopw, 1, rhog, pw_world);
110+
bool result = ModuleIO::read_rhog(filename, &rhopw, 1, rhog.data(), pw_world);
123111
GlobalV::ofs_warning.close();
124112

125113
std::ifstream ifs_running("test_read_rhog.txt");

0 commit comments

Comments
 (0)