Skip to content

Commit 12b4a22

Browse files
authored
test: 3 functions in tool_quit.h
Merge pull request #658 from hongriTianqi/develop
2 parents cc08483 + e30b109 commit 12b4a22

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

source/module_base/test/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@ AddTest(
1414
TARGET tool_check
1515
SOURCES tool_check_test.cpp
1616
)
17+
AddTest(
18+
TARGET tool_quit
19+
SOURCES tool_quit_test.cpp
20+
)
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#include "../tool_quit.h"
2+
#include "../global_variable.h"
3+
#include "gtest/gtest.h"
4+
#include "gmock/gmock.h"
5+
#include "mpi.h"
6+
7+
/************************************************
8+
* unit test of functions in tool_quit.h
9+
***********************************************/
10+
11+
/**
12+
* - Tested Function
13+
* - ModuleBase::WARNING
14+
* - print out warning info. in warning.log
15+
*
16+
* - ModuleBase::QUIT
17+
* - close log files and exit
18+
*
19+
* - ModuleBase::WARNING_QUIT
20+
* - combine the above 2 functions
21+
*
22+
* - Author: Tianqi Zhao
23+
*/
24+
25+
class ToolQuitTest : public testing::Test
26+
{
27+
protected:
28+
std::ifstream ifs;
29+
// for capturing output on screen
30+
std::string output;
31+
void SetUp()
32+
{
33+
GlobalV::ofs_warning.open("warning.log");
34+
GlobalV::ofs_running.open("running.log");
35+
GlobalV::global_out_dir = "OUT/";
36+
}
37+
void TearDown()
38+
{
39+
remove("warning.log");
40+
remove("running.log");
41+
}
42+
};
43+
44+
45+
TEST_F(ToolQuitTest,warning)
46+
{
47+
ModuleBase::WARNING("INPUT","bad input parameter");
48+
GlobalV::ofs_warning.close();
49+
ifs.open("warning.log");
50+
getline(ifs,output);
51+
// test output in warning.log file
52+
EXPECT_THAT(output,testing::HasSubstr("warning"));
53+
ifs.close();
54+
}
55+
56+
TEST_F(ToolQuitTest,quit)
57+
{
58+
testing::internal::CaptureStdout();
59+
EXPECT_EXIT(ModuleBase::QUIT(), ::testing::ExitedWithCode(0), "");
60+
output = testing::internal::GetCapturedStdout();
61+
// test output on screen
62+
EXPECT_THAT(output,testing::HasSubstr("TIME(Sec)-----"));
63+
}
64+
65+
// use EXPECT_EXIT to test exit codes
66+
TEST_F(ToolQuitTest,warningquit)
67+
{
68+
testing::internal::CaptureStdout();
69+
EXPECT_EXIT(ModuleBase::WARNING_QUIT("INPUT","bad input parameter"),
70+
::testing::ExitedWithCode(0), "");
71+
output = testing::internal::GetCapturedStdout();
72+
// test output on screening
73+
EXPECT_THAT(output,testing::HasSubstr("TIME(Sec)-----"));
74+
GlobalV::ofs_warning.close();
75+
GlobalV::ofs_running.close();
76+
ifs.open("warning.log");
77+
getline(ifs,output);
78+
// test output in warning.log file
79+
EXPECT_THAT(output,testing::HasSubstr("warning"));
80+
ifs.close();
81+
ifs.open("running.log");
82+
getline(ifs,output);
83+
// test output in running.log file
84+
EXPECT_THAT(output,testing::HasSubstr("!!!!!!!"));
85+
ifs.close();
86+
}
87+
88+
// use __MPI to activate parallel environment
89+
int main(int argc, char **argv)
90+
{
91+
92+
#ifdef __MPI
93+
MPI_Init(&argc,&argv);
94+
#endif
95+
96+
testing::InitGoogleTest(&argc,argv);
97+
int result = RUN_ALL_TESTS();
98+
99+
#ifdef __MPI
100+
MPI_Finalize();
101+
#endif
102+
103+
return result;
104+
}
105+

0 commit comments

Comments
 (0)