Repository navigation
Expand file tree
/
Copy pathtool_quit_test.cpp
More file actions
137 lines (123 loc) · 3.4 KB
/
Copy pathtool_quit_test.cpp
File metadata and controls
137 lines (123 loc) · 3.4 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
#include "../tool_quit.h"
#include "../global_variable.h"
#define private public
#include "source_io/module_parameter/parameter.h"
#undef private
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#ifdef __MPI
#include "mpi.h"
#endif
/************************************************
* unit test of functions in tool_quit.h
***********************************************/
/**
* - Tested Function
* - ModuleBase::WARNING
* - print out warning info. in warning.log
*
* - ModuleBase::QUIT
* - close log files and exit
*
* - ModuleBase::WARNING_QUIT
* - combine the above 2 functions
*/
class ToolQuitTest : public testing::Test
{
protected:
std::ifstream ifs;
// for capturing output on screen
std::string output;
void SetUp()
{
GlobalV::ofs_warning.open("warning.log");
GlobalV::ofs_running.open("running.log");
PARAM.sys.global_out_dir = "OUT/";
}
void TearDown()
{
remove("warning.log");
remove("running.log");
}
};
TEST_F(ToolQuitTest,warning)
{
ModuleBase::WARNING("INPUT","bad input parameter");
GlobalV::ofs_warning.close();
ifs.open("warning.log");
getline(ifs,output);
// test output in warning.log file
EXPECT_THAT(output,testing::HasSubstr("warning"));
ifs.close();
}
TEST_F(ToolQuitTest,quit)
{
testing::internal::CaptureStdout();
EXPECT_EXIT(ModuleBase::QUIT(), ::testing::ExitedWithCode(0), "");
output = testing::internal::GetCapturedStdout();
// test output on screen
EXPECT_THAT(output,testing::HasSubstr("TIME STATISTICS"));
}
TEST_F(ToolQuitTest,quit_with_ret)
{
testing::internal::CaptureStdout();
EXPECT_EXIT(ModuleBase::QUIT(1), ::testing::ExitedWithCode(1), "");
output = testing::internal::GetCapturedStdout();
// test output on screen
EXPECT_THAT(output,testing::HasSubstr("TIME STATISTICS"));
}
// use EXPECT_EXIT to test exit codes
TEST_F(ToolQuitTest,warningquit)
{
testing::internal::CaptureStdout();
EXPECT_EXIT(ModuleBase::WARNING_QUIT("INPUT","bad input parameter"),
::testing::ExitedWithCode(1), "");
output = testing::internal::GetCapturedStdout();
// test output on screening
EXPECT_THAT(output,testing::HasSubstr("TIME STATISTICS"));
GlobalV::ofs_warning.close();
GlobalV::ofs_running.close();
ifs.open("warning.log");
getline(ifs,output);
// test output in warning.log file
EXPECT_THAT(output,testing::HasSubstr("warning"));
ifs.close();
ifs.open("running.log");
getline(ifs,output);
// test output in running.log file
EXPECT_THAT(output,testing::HasSubstr("-------"));
ifs.close();
}
// use EXPECT_EXIT to test exit codes
TEST_F(ToolQuitTest,warningquit_with_ret)
{
testing::internal::CaptureStdout();
EXPECT_EXIT(ModuleBase::WARNING_QUIT("INPUT","bad input parameter",1),
::testing::ExitedWithCode(1), "");
output = testing::internal::GetCapturedStdout();
// test output on screening
EXPECT_THAT(output,testing::HasSubstr("TIME STATISTICS"));
GlobalV::ofs_warning.close();
GlobalV::ofs_running.close();
ifs.open("warning.log");
getline(ifs,output);
// test output in warning.log file
EXPECT_THAT(output,testing::HasSubstr("warning"));
ifs.close();
ifs.open("running.log");
getline(ifs,output);
// test output in running.log file
EXPECT_THAT(output,testing::HasSubstr("-------"));
ifs.close();
}
// use __MPI to activate parallel environment
#ifdef __MPI
int main(int argc, char **argv)
{
MPI_Init(&argc,&argv);
testing::InitGoogleTest(&argc,argv);
int result = RUN_ALL_TESTS();
MPI_Finalize();
return result;
}
#endif