Repository navigation
Expand file tree
/
Copy pathglobal_file_test.cpp
More file actions
160 lines (147 loc) · 4.59 KB
/
Copy pathglobal_file_test.cpp
File metadata and controls
160 lines (147 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#define private public
#include "source_io/module_parameter/parameter.h"
#undef private
#include "../global_file.h"
#include "../global_variable.h"
#include <fstream>
#include <unistd.h>
#ifdef __MPI
#include "mpi.h"
#endif
/************************************************
* unit test of functions in global_file.cpp
***********************************************/
/**
* - Tested Function
* - mkdiratom
* - generate atom dir for each type of atom
* - openlog
* - Open the out file with the name *.log
*/
class GlobalFile : public testing::Test
{
};
TEST_F(GlobalFile,mkdirout)
{
std::string output;
testing::internal::CaptureStdout();
ModuleBase::Global_File::make_dir_out("Si","m",false,false,0,true,true);
output = testing::internal::GetCapturedStdout();
EXPECT_THAT(output,testing::HasSubstr("MAKE THE DIR"));
GlobalV::ofs_warning.close();
EXPECT_TRUE(GlobalV::ofs_running.is_open());
GlobalV::ofs_running.close();
std::string dd = "OUT.Si/running_m_1.log";
remove(dd.c_str());
testing::internal::CaptureStdout();
ModuleBase::Global_File::make_dir_out("Si","md",false,false,0,true,false);
output = testing::internal::GetCapturedStdout();
EXPECT_THAT(output,testing::HasSubstr("MAKE THE STRU DIR"));
EXPECT_TRUE(GlobalV::ofs_running.is_open());
GlobalV::ofs_running.close();
GlobalV::ofs_warning.close();
std::string bb = "OUT.Si/running_md.log";
remove(bb.c_str());
testing::internal::CaptureStdout();
ModuleBase::Global_File::make_dir_out("Si","md",true,false,0,true,true);
output = testing::internal::GetCapturedStdout();
EXPECT_THAT(output,testing::HasSubstr("MAKE THE MATRIX DIR"));
EXPECT_TRUE(GlobalV::ofs_running.is_open());
GlobalV::ofs_running.close();
std::string cc = "OUT.Si/running_md_1.log";
remove(cc.c_str());
std::string aa = "OUT.Si/warning.log";
remove(aa.c_str());
rmdir(PARAM.sys.global_stru_dir.c_str());
rmdir(PARAM.sys.global_matrix_dir.c_str());
rmdir(PARAM.sys.global_out_dir.c_str());
}
TEST_F(GlobalFile,mkdiratom)
{
PARAM.sys.global_out_dir = "./";
ModuleBase::Global_File::make_dir_atom("Si");
int a = access("./Si/",0);
EXPECT_EQ(a , 0);
std::string ss = "./Si/";
rmdir(ss.c_str());
}
TEST_F(GlobalFile,openlog)
{
std::ofstream ofs;
ModuleBase::Global_File::open_log(ofs,"Si.log","md",true);
EXPECT_TRUE(ofs.is_open());
ofs.close();
ModuleBase::Global_File::open_log(ofs,"Si.log","md",false);
EXPECT_TRUE(ofs.is_open());
ofs.close();
std::string sss = "Si.log";
remove(sss.c_str());
}
TEST_F(GlobalFile,closelog)
{
std::ofstream ofs;
std::string sss = "Si.log";
ofs.open(sss.c_str());
ModuleBase::Global_File::close_log(ofs, sss);
EXPECT_FALSE(ofs.is_open());
if (ofs.is_open())
{
ofs.close();
}
remove(sss.c_str());
}
TEST_F(GlobalFile,closealllog)
{
/*
For source_io/input.cpp:line3578 close_log() is a void function,
All its contents is calling close_all_log() in source_base/global_file.cpp
For Input::close_log() what is left to test are the validities of parameters
GlobalV::MY_RANK and this->out_alllog.
*/
/* Test out_alllog == true case */
std::string header = "running_";
std::string tailCpuRank0 = "_cpu0.log";
std::string tail = ".log";
std::string f1 = header + PARAM.input.calculation + tailCpuRank0;
if (GlobalV::ofs_running.is_open())
{
GlobalV::ofs_running.close();
}
if (GlobalV::ofs_warning.is_open())
{
GlobalV::ofs_warning.close();
}
GlobalV::ofs_running.open(f1.c_str());
GlobalV::ofs_warning.open("warning.log");
ModuleBase::Global_File::close_all_log(0,true,PARAM.input.calculation);
EXPECT_FALSE(GlobalV::ofs_running.is_open());
if (GlobalV::ofs_running.is_open())
{
GlobalV::ofs_running.close();
}
EXPECT_FALSE(GlobalV::ofs_warning.is_open());
if (GlobalV::ofs_warning.is_open())
{
GlobalV::ofs_warning.close();
}
remove(f1.c_str());
//remove("warning.log");
/* Test out_alllog == false case */
GlobalV::ofs_running.open("running.log");
GlobalV::ofs_warning.open("warning.log");
ModuleBase::Global_File::close_all_log(0,false,PARAM.input.calculation);
EXPECT_FALSE(GlobalV::ofs_running.is_open());
if (GlobalV::ofs_running.is_open())
{
GlobalV::ofs_running.close();
}
EXPECT_FALSE(GlobalV::ofs_warning.is_open());
if (GlobalV::ofs_warning.is_open())
{
GlobalV::ofs_warning.close();
}
remove("running.log");
remove("warning.log");
}