Skip to content

Commit 33b6b21

Browse files
authored
Merge pull request #776 from hongriTianqi/unittest
UT for global_function in module_base
2 parents c9c36bc + 376ced9 commit 33b6b21

File tree

3 files changed

+435
-128
lines changed

3 files changed

+435
-128
lines changed

source/module_base/global_function.cpp

Lines changed: 100 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,55 @@
22
// AUTHOR : mohan
33
//==========================================================
44
#include "global_function.h"
5-
#include "global_file.h"
5+
66
#include "../src_parallel/parallel_common.h"
7+
#include "global_file.h"
78

89
//==========================================================
910
// USE FILE timer.h
1011
// ONLY : output time after quit.
1112
//==========================================================
12-
#include "timer.h"
1313
#include "memory.h"
14+
#include "timer.h"
1415

15-
#include <string>
1616
#include <fstream>
1717
#include <iostream>
18+
#include <string>
1819
namespace ModuleBase
1920
{
2021
namespace GlobalFunc
2122
{
2223

2324
void NOTE(const std::string &words)
2425
{
25-
return;
26-
if(GlobalV::ofs_running)
27-
{
28-
//GlobalV::ofs_running << " *********************************************************************************" << std::endl;
29-
GlobalV::ofs_running << " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
30-
GlobalV::ofs_running << " " << words << std::endl;
31-
GlobalV::ofs_running << " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
32-
}
26+
return;
27+
if (GlobalV::ofs_running)
28+
{
29+
// GlobalV::ofs_running << " *********************************************************************************"
30+
// << std::endl;
31+
GlobalV::ofs_running << " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
32+
<< std::endl;
33+
GlobalV::ofs_running << " " << words << std::endl;
34+
GlobalV::ofs_running << " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
35+
<< std::endl;
36+
}
3337
}
3438

3539
void NEW_PART(const std::string &words)
3640
{
37-
GlobalV::ofs_running << "\n ><><><><><><><><><><><><><><><><><><><><><><" << std::endl;
38-
GlobalV::ofs_running << "\n " << words << std::endl;
39-
GlobalV::ofs_running << "\n ><><><><><><><><><><><><><><><><><><><><><><\n" << std::endl;
40-
return;
41+
GlobalV::ofs_running << "\n ><><><><><><><><><><><><><><><><><><><><><><" << std::endl;
42+
GlobalV::ofs_running << "\n " << words << std::endl;
43+
GlobalV::ofs_running << "\n ><><><><><><><><><><><><><><><><><><><><><><\n" << std::endl;
44+
return;
4145
}
4246

43-
4447
//==========================================================
4548
// GLOBAL FUNCTION :
4649
// NAME : OUT( output date for checking )
4750
//==========================================================
48-
void OUT(std::ofstream &ofs,const std::string &name)
51+
void OUT(std::ofstream &ofs, const std::string &name)
4952
{
50-
ofs<<"\n"<<std::setw(18)<<name<<std::endl;
53+
ofs << "\n" << std::setw(18) << name << std::endl;
5154
return;
5255
}
5356

@@ -57,17 +60,17 @@ void OUT(std::ofstream &ofs,const std::string &name)
5760
//==========================================================
5861
void MAKE_DIR(const std::string &fn)
5962
{
60-
// ModuleBase::TITLE("global_function","MAKE_DIR");
61-
if (GlobalV::MY_RANK==0)
63+
// ModuleBase::TITLE("global_function","MAKE_DIR");
64+
if (GlobalV::MY_RANK == 0)
6265
{
6366
std::stringstream ss;
64-
ss << " test -d " << fn << " || mkdir " << fn ;
65-
//----------------------------------------------------------
66-
// EXPLAIN : 'system' function return '0' if success
67-
//----------------------------------------------------------
68-
if ( system( ss.str().c_str() ) )
67+
ss << " test -d " << fn << " || mkdir " << fn;
68+
//----------------------------------------------------------
69+
// EXPLAIN : 'system' function return '0' if success
70+
//----------------------------------------------------------
71+
if (system(ss.str().c_str()))
6972
{
70-
ModuleBase::WARNING_QUIT( "MAKE_DIR", fn );
73+
ModuleBase::WARNING_QUIT("MAKE_DIR", fn);
7174
}
7275
}
7376
#ifdef __MPI
@@ -76,31 +79,31 @@ void MAKE_DIR(const std::string &fn)
7679
return;
7780
}
7881

79-
80-
void DONE(std::ofstream &ofs,const std::string &description, const bool only_rank0)
82+
void DONE(std::ofstream &ofs, const std::string &description, const bool only_rank0)
8183
{
8284
if (only_rank0)
8385
{
84-
if (GlobalV::MY_RANK==0)
86+
if (GlobalV::MY_RANK == 0)
8587
{
86-
// ofs << " ---------------------------------------------------------------------------------\n";
88+
// ofs << " ---------------------------------------------------------------------------------\n";
8789
ofs << " DONE : " << description;
88-
ofs << " Time : "<< ModuleBase::timer::print_until_now() << " (SEC)" ;
89-
ofs << std::endl << std::endl;
90-
// ofs << "\n ---------------------------------------------------------------------------------\n";
90+
ofs << " Time : " << ModuleBase::timer::print_until_now() << " (SEC)";
91+
ofs << std::endl << std::endl;
92+
// ofs << "\n ---------------------------------------------------------------------------------\n";
9193
}
9294
}
9395
else
9496
{
95-
// ofs << " ---------------------------------------------------------------------------------\n";
97+
// ofs << " ---------------------------------------------------------------------------------\n";
9698
ofs << " DONE : " << description;
97-
ofs << " Time : "<< ModuleBase::timer::print_until_now() << " (SEC)" ;
98-
ofs << std::endl << std::endl;
99-
// ofs << "\n ---------------------------------------------------------------------------------\n";
99+
ofs << " Time : " << ModuleBase::timer::print_until_now() << " (SEC)";
100+
ofs << std::endl << std::endl;
101+
// ofs << "\n ---------------------------------------------------------------------------------\n";
100102
}
101-
// std::cout << "\n---------------------------------------------------------------------------------\n";
102-
std::cout << " DONE(" << std::setw(10) << ModuleBase::timer::print_until_now() <<" SEC) : "<< description << std::endl;
103-
// std::cout << "\n---------------------------------------------------------------------------------\n";
103+
// std::cout << "\n---------------------------------------------------------------------------------\n";
104+
std::cout << " DONE(" << std::setw(10) << ModuleBase::timer::print_until_now() << " SEC) : " << description
105+
<< std::endl;
106+
// std::cout << "\n---------------------------------------------------------------------------------\n";
104107
return;
105108
}
106109

@@ -142,13 +145,11 @@ void TEST_LEVEL(const std::string &name)
142145
else if (name == "pw_init")
143146
{
144147
GlobalV::test_pw = 1;
145-
146148
}
147149

148150
return;
149151
}
150152

151-
152153
bool SCAN_BEGIN(std::ifstream &ifs, const std::string &TargetName, const bool restart)
153154
{
154155
std::string SearchName;
@@ -162,97 +163,99 @@ bool SCAN_BEGIN(std::ifstream &ifs, const std::string &TargetName, const bool re
162163
while (ifs.good())
163164
{
164165
ifs >> SearchName;
165-
if ( SearchName == TargetName)
166+
if (SearchName == TargetName)
166167
{
167168
find = true;
168169
break;
169170
}
170171
}
171172
if (!find)
172173
{
173-
GlobalV::ofs_warning<<" In SCAN_BEGIN, can't find: "<<TargetName<<" block."<<std::endl;
174+
GlobalV::ofs_warning << " In SCAN_BEGIN, can't find: " << TargetName << " block." << std::endl;
174175
}
175176
return find;
176177
}
177178

178-
179179
void SCAN_END(std::ifstream &ifs, const std::string &TargetName)
180180
{
181181
std::string SearchName;
182182
ifs >> SearchName;
183-
if ( SearchName != TargetName)
183+
if (SearchName != TargetName)
184184
{
185-
GlobalV::ofs_warning<<" In SCAN_END, can't find: "<<TargetName<<" block."<<std::endl;
185+
GlobalV::ofs_warning << " In SCAN_END, can't find: " << TargetName << " block." << std::endl;
186186
}
187187
return;
188188
}
189189

190-
void BLOCK_HERE( const std::string &description)
190+
void BLOCK_HERE(const std::string &description)
191191
{
192-
// return;
193-
std::cout << "\n********************************************";
192+
// return;
193+
std::cout << "\n********************************************";
194194
std::cout << "\n Here is a Block, 1: go on 0: quit";
195195
std::cout << "\n " << description;
196-
std::cout << "\n********************************************" << std::endl;
196+
std::cout << "\n********************************************" << std::endl;
197197
bool go_on = false;
198-
if(GlobalV::MY_RANK==0)
199-
{
200-
std::cin >> go_on;
201-
}
198+
if (GlobalV::MY_RANK == 0)
199+
{
200+
std::cin >> go_on;
201+
}
202202

203203
#ifdef __MPI
204-
int swap = go_on;
205-
if(GlobalV::MY_RANK == 0)swap = go_on;
206-
MPI_Bcast(&swap, 1, MPI_INT, 0, MPI_COMM_WORLD);
207-
if(GlobalV::MY_RANK != 0)go_on = static_cast<bool>( swap );
204+
int swap = go_on;
205+
if (GlobalV::MY_RANK == 0)
206+
swap = go_on;
207+
MPI_Bcast(&swap, 1, MPI_INT, 0, MPI_COMM_WORLD);
208+
if (GlobalV::MY_RANK != 0)
209+
go_on = static_cast<bool>(swap);
208210
#endif
209-
if(go_on)
210-
{
211-
return;
212-
}
213-
else
214-
{
215-
ModuleBase::QUIT();
216-
}
211+
if (go_on)
212+
{
213+
return;
214+
}
215+
else
216+
{
217+
ModuleBase::QUIT();
218+
}
217219
}
218220

219-
220221
void OUT_TIME(const std::string &name, time_t &start, time_t &end)
221222
{
222-
double mini = difftime(end, start)/60.0;
223-
if(mini>0.1)
223+
double mini = difftime(end, start) / 60.0;
224+
if (mini > 0.1)
225+
{
226+
if(GlobalV::ofs_warning)
224227
{
225-
GlobalV::ofs_warning << std::setprecision(2);
226-
GlobalV::ofs_warning << " -------------------------------------------------------" << std::endl;
227-
GlobalV::ofs_warning << " NAME < " << name << " > = " << std::endl;
228-
GlobalV::ofs_warning << " -> " << ctime(&start) << " -> " << ctime(&end);
229-
GlobalV::ofs_warning << " TIME = " << mini << " [Minutes]" << std::endl;
230-
GlobalV::ofs_warning << " -------------------------------------------------------" << std::endl;
231-
GlobalV::ofs_warning << std::setprecision(6);
228+
GlobalV::ofs_warning << std::setprecision(2);
229+
GlobalV::ofs_warning << " -------------------------------------------------------" << std::endl;
230+
GlobalV::ofs_warning << " NAME < " << name << " > = " << std::endl;
231+
GlobalV::ofs_warning << " -> " << ctime(&start) << " -> " << ctime(&end);
232+
GlobalV::ofs_warning << " TIME = " << mini << " [Minutes]" << std::endl;
233+
GlobalV::ofs_warning << " -------------------------------------------------------" << std::endl;
234+
GlobalV::ofs_warning << std::setprecision(6);
232235
}
236+
}
233237
}
234238

235239
size_t MemAvailable()
236240
{
237-
size_t mem_sum = 0;
238-
int i=0;
239-
std::ifstream ifs("/proc/meminfo");
240-
while(ifs.good())
241-
{
242-
std::string label, size, kB;
243-
ifs>>label>>size>>kB;
244-
if(label=="MemAvailable:")
245-
return std::stol(size);
246-
else if(label=="MemFree:" || label=="Buffers:" || label=="Cached:")
247-
{
248-
mem_sum += std::stol(size);
249-
++i;
250-
}
251-
if(i==3)
252-
return mem_sum;
253-
}
254-
throw std::runtime_error("read /proc/meminfo error in "+TO_STRING(__FILE__)+" line "+TO_STRING(__LINE__));
241+
size_t mem_sum = 0;
242+
int i = 0;
243+
std::ifstream ifs("/proc/meminfo");
244+
while (ifs.good())
245+
{
246+
std::string label, size, kB;
247+
ifs >> label >> size >> kB;
248+
if (label == "MemAvailable:")
249+
return std::stol(size);
250+
else if (label == "MemFree:" || label == "Buffers:" || label == "Cached:")
251+
{
252+
mem_sum += std::stol(size);
253+
++i;
254+
}
255+
if (i == 3) return mem_sum;
256+
}
257+
throw std::runtime_error("read /proc/meminfo error in " + TO_STRING(__FILE__) + " line " + TO_STRING(__LINE__));
255258
}
256259

257-
}
258-
}
260+
} // namespace GlobalFunc
261+
} // namespace ModuleBase

0 commit comments

Comments
 (0)