Skip to content

Commit c981303

Browse files
committed
fix unitest bug
1 parent 67211c5 commit c981303

File tree

5 files changed

+31
-19
lines changed

5 files changed

+31
-19
lines changed

source/module_cell/read_atom_species.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ bool read_atom_species(std::ifstream& ifa,
1414
{
1515
ModuleBase::TITLE("UnitCell","read_atom_species");
1616
const int ntype = ucell.ntype;
17-
18-
delete[] ucell.orbital_fn;
19-
ucell.orbital_fn = new std::string[ntype]; // filename of orbitals
2017
std::string word;
2118

2219
//==========================================

source/module_cell/test/unitcell_test.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,10 @@ class UcellTestReadStru : public ::testing::Test
11021102
void SetUp() override
11031103
{
11041104
ucell->ntype = 2;
1105+
ucell->atom_mass.resize(ucell->ntype);
1106+
ucell->atom_label.resize(ucell->ntype);
1107+
ucell->pseudo_fn.resize(ucell->ntype);
1108+
ucell->pseudo_type.resize(ucell->ntype);
11051109
ucell->orbital_fn.resize(ucell->ntype);
11061110
}
11071111
void TearDown() override
@@ -1132,7 +1136,7 @@ TEST_F(UcellTestReadStru, ReadAtomSpecies)
11321136
remove("read_atom_species.tmp");
11331137
}
11341138

1135-
TEST_F(UcellDeathTest, ReadAtomSpeciesWarning1)
1139+
TEST_F(UcellTestReadStru, ReadAtomSpeciesWarning1)
11361140
{
11371141
std::string fn = "./support/STRU_MgO_Warning1";
11381142
std::ifstream ifa(fn.c_str());
@@ -1150,7 +1154,7 @@ TEST_F(UcellDeathTest, ReadAtomSpeciesWarning1)
11501154
remove("read_atom_species.tmp");
11511155
}
11521156

1153-
TEST_F(UcellDeathTest, ReadLatticeConstantWarning1)
1157+
TEST_F(UcellTestReadStru, ReadLatticeConstantWarning1)
11541158
{
11551159
std::string fn = "./support/STRU_MgO_Warning2";
11561160
std::ifstream ifa(fn.c_str());
@@ -1168,7 +1172,7 @@ TEST_F(UcellDeathTest, ReadLatticeConstantWarning1)
11681172
remove("read_atom_species.tmp");
11691173
}
11701174

1171-
TEST_F(UcellDeathTest, ReadLatticeConstantWarning2)
1175+
TEST_F(UcellTestReadStru, ReadLatticeConstantWarning2)
11721176
{
11731177
std::string fn = "./support/STRU_MgO_Warning3";
11741178
std::ifstream ifa(fn.c_str());
@@ -1187,7 +1191,7 @@ TEST_F(UcellDeathTest, ReadLatticeConstantWarning2)
11871191
remove("read_atom_species.tmp");
11881192
}
11891193

1190-
TEST_F(UcellDeathTest, ReadLatticeConstantWarning3)
1194+
TEST_F(UcellTestReadStru, ReadLatticeConstantWarning3)
11911195
{
11921196
std::string fn = "./support/STRU_MgO_Warning4";
11931197
std::ifstream ifa(fn.c_str());
@@ -1793,7 +1797,7 @@ TEST_F(UcellTestReadStru, ReadAtomPositionsWarning3)
17931797
remove("read_atom_positions.warn");
17941798
}
17951799

1796-
TEST_F(UcellDeathTest, ReadAtomPositionsWarning4)
1800+
TEST_F(UcellTestReadStru, ReadAtomPositionsWarning4)
17971801
{
17981802
std::string fn = "./support/STRU_MgO_WarningC4";
17991803
std::ifstream ifa(fn.c_str());

source/module_cell/test/unitcell_test_setupcell.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ class UcellTest : public ::testing::Test
6767
void SetUp()
6868
{
6969
ucell->lmaxmax = 2;
70+
ucell->ntype = 2;
71+
ucell->atom_mass.resize(ucell->ntype);
72+
ucell->atom_label.resize(ucell->ntype);
73+
ucell->pseudo_fn.resize(ucell->ntype);
74+
ucell->pseudo_type.resize(ucell->ntype);
75+
ucell->orbital_fn.resize(ucell->ntype);
7076
}
7177
};
7278

@@ -78,7 +84,7 @@ TEST_F(UcellTest,SetupCellS1)
7884
std::ofstream ofs_running;
7985
ofs_running.open("setup_cell.tmp");
8086
PARAM.input.nspin = 1;
81-
ucell->ntype = 2;
87+
8288
ucell->setup_cell(fn,ofs_running);
8389
ofs_running.close();
8490
remove("setup_cell.tmp");
@@ -90,7 +96,7 @@ TEST_F(UcellTest,SetupCellS2)
9096
std::ofstream ofs_running;
9197
ofs_running.open("setup_cell.tmp");
9298
PARAM.input.nspin = 2;
93-
ucell->ntype = 2;
99+
94100
ucell->setup_cell(fn,ofs_running);
95101
ofs_running.close();
96102
remove("setup_cell.tmp");
@@ -102,7 +108,7 @@ TEST_F(UcellTest,SetupCellS4)
102108
std::ofstream ofs_running;
103109
ofs_running.open("setup_cell.tmp");
104110
PARAM.input.nspin = 4;
105-
ucell->ntype = 2;
111+
106112
ucell->setup_cell(fn,ofs_running);
107113
ofs_running.close();
108114
remove("setup_cell.tmp");
@@ -113,7 +119,7 @@ TEST_F(UcellDeathTest,SetupCellWarning1)
113119
std::string fn = "./STRU_MgO";
114120
std::ofstream ofs_running;
115121
ofs_running.open("setup_cell.tmp");
116-
ucell->ntype = 2;
122+
117123
testing::internal::CaptureStdout();
118124
EXPECT_EXIT(ucell->setup_cell(fn,ofs_running),::testing::ExitedWithCode(1),"");
119125
output = testing::internal::GetCapturedStdout();
@@ -127,7 +133,7 @@ TEST_F(UcellDeathTest,SetupCellWarning2)
127133
std::string fn = "./support/STRU_MgO_WarningC2";
128134
std::ofstream ofs_running;
129135
ofs_running.open("setup_cell.tmp");
130-
ucell->ntype = 2;
136+
131137
testing::internal::CaptureStdout();
132138
EXPECT_EXIT(ucell->setup_cell(fn,ofs_running),::testing::ExitedWithCode(1),"");
133139
output = testing::internal::GetCapturedStdout();
@@ -142,7 +148,7 @@ TEST_F(UcellTest,SetupCellAfterVC)
142148
std::ofstream ofs_running;
143149
ofs_running.open("setup_cell.tmp");
144150
PARAM.input.nspin = 1;
145-
ucell->ntype = 2;
151+
146152
delete[] ucell->magnet.start_magnetization;
147153
ucell->magnet.start_magnetization = new double[ucell->ntype];
148154

source/module_cell/test_pw/unitcell_test_pw.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ class UcellTest : public ::testing::Test
4444
void SetUp()
4545
{
4646
ucell->lmaxmax = 2;
47+
ucell->ntype = 2;
48+
ucell->atom_mass.resize(ucell->ntype);
49+
ucell->atom_label.resize(ucell->ntype);
50+
ucell->pseudo_fn.resize(ucell->ntype);
51+
ucell->pseudo_type.resize(ucell->ntype);
52+
ucell->orbital_fn.resize(ucell->ntype);
4753
}
4854
};
4955

@@ -57,11 +63,11 @@ if(GlobalV::MY_RANK==0)
5763
std::ifstream ifa(fn.c_str());
5864
std::ofstream ofs_running;
5965
ofs_running.open("read_atom_species.tmp");
60-
ucell->ntype = 2;
6166
ucell->atoms = new Atom[ucell->ntype];
6267
ucell->set_atom_flag = true;
6368
PARAM.input.test_pseudo_cell = 2;
6469
EXPECT_NO_THROW(unitcell::read_atom_species(ifa, ofs_running,*ucell));
70+
EXPECT_NO_THROW(unitcell::read_lattice_constant(ifa, ofs_running,ucell->lat));
6571
EXPECT_DOUBLE_EQ(ucell->latvec.e11,4.27957);
6672
EXPECT_DOUBLE_EQ(ucell->latvec.e22,4.27957);
6773
EXPECT_DOUBLE_EQ(ucell->latvec.e33,4.27957);
@@ -85,13 +91,13 @@ if(GlobalV::MY_RANK==0)
8591
std::ofstream ofs_warning;
8692
ofs_running.open("read_atom_species.tmp");
8793
ofs_warning.open("read_atom_species.warn");
88-
ucell->ntype = 2;
8994
ucell->atoms = new Atom[ucell->ntype];
9095
ucell->set_atom_flag = true;
9196
PARAM.input.test_pseudo_cell = 2;
9297
PARAM.input.basis_type = "pw";
9398
//call read_atom_species
9499
EXPECT_NO_THROW(unitcell::read_atom_species(ifa, ofs_running,*ucell));
100+
EXPECT_NO_THROW(unitcell::read_lattice_constant(ifa, ofs_running,ucell->lat));
95101
EXPECT_DOUBLE_EQ(ucell->latvec.e11,4.27957);
96102
EXPECT_DOUBLE_EQ(ucell->latvec.e22,4.27957);
97103
EXPECT_DOUBLE_EQ(ucell->latvec.e33,4.27957);
@@ -116,7 +122,6 @@ TEST_F(UcellTest,SetupCell)
116122
std::ofstream ofs_running;
117123
ofs_running.open("setup_cell.tmp");
118124
PARAM.input.nspin = 1;
119-
ucell->ntype = 2;
120125
ucell->setup_cell(fn,ofs_running);
121126
ofs_running.close();
122127
remove("setup_cell.tmp");

source/module_cell/unitcell.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,9 @@ void UnitCell::setup_cell(const std::string& fn, std::ofstream& log) {
230230
// (3) read in atom information
231231
this->atom_mass.resize(ntype);
232232
this->atom_label.resize(ntype);
233-
this->pseudo_fn.reszie(ntype);
233+
this->pseudo_fn.resize(ntype);
234234
this->pseudo_type.resize(ntype);
235-
orbital_fn.resize(ntype);
235+
this->orbital_fn.resize(ntype);
236236
if (GlobalV::MY_RANK == 0) {
237237
// open "atom_unitcell" file.
238238
std::ifstream ifa(fn.c_str(), std::ios::in);

0 commit comments

Comments
 (0)