Skip to content

Commit cc08483

Browse files
authored
Merge pull request #653 from hongriTianqi/develop
Test: 4 func. of module_base/tool_check
2 parents 021a3dc + 3011092 commit cc08483

File tree

4 files changed

+140
-5
lines changed

4 files changed

+140
-5
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,9 +252,9 @@ function(AddTest)
252252
#dependencies & link library
253253
get_target_property(ABACUS_LINK_LIBRARIES ${ABACUS_BIN_NAME} LINK_LIBRARIES)
254254
target_link_libraries(${UT_TARGET} ${UT_LIBS} ${ABACUS_LINK_LIBRARIES}
255-
base cell symmetry md symmetry
255+
base cell symmetry md
256256
neighbor orb io ions lcao parallel mrrr pdiag pw ri driver
257-
pthread GTest::gtest_main)
257+
pthread GTest::gtest_main GTest::gmock_main)
258258
install(TARGETS ${UT_TARGET} DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/../../../tests )
259259
add_test(NAME ${UT_TARGET}
260260
COMMAND ${UT_TARGET}

source/module_base/test/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,7 @@ AddTest(
1010
TARGET base_complexarray
1111
SOURCES complexarray_test.cpp
1212
)
13+
AddTest(
14+
TARGET tool_check
15+
SOURCES tool_check_test.cpp
16+
)
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
#include "../tool_check.h"
2+
#include "gtest/gtest.h"
3+
#include "gmock/gmock.h"
4+
#include <cstdio>
5+
#include <fstream>
6+
7+
/************************************************
8+
* unit test of functions in tool_check.h
9+
***********************************************/
10+
11+
/**
12+
* - Tested Function
13+
* - ModuleBase::CHECK_NAME
14+
* - check the next input from ifs is std::string
15+
*
16+
* - ModuleBase::CHECK_INT
17+
* - check the next input from ifs is int
18+
*
19+
* - ModuleBase::CHECK_DOUBLE
20+
* - check the next input from ifs is double
21+
*
22+
* - ModuleBase::CHECK_STRING
23+
* - check the next input from ifs is string
24+
*
25+
* - Author: Tianqi Zhao
26+
*/
27+
28+
class ToolCheckTest : public testing::Test
29+
{
30+
protected:
31+
std::ofstream ofs;
32+
std::ifstream ifs;
33+
// define std::string, int, double variables
34+
std::string name = "abaqus";
35+
int ecut = 100;
36+
double occupation = 0.23;
37+
std::string caltype = "nscf";
38+
// quit is the swith to control performance of function
39+
bool quit = false;
40+
// for capturing stdout
41+
std::string output = "";
42+
void TearDown()
43+
{
44+
remove("tmp");
45+
}
46+
47+
};
48+
49+
TEST_F(ToolCheckTest, Name)
50+
{
51+
ofs.open("tmp");
52+
// double input to check continus check function
53+
ofs << name << std::endl;
54+
ofs << name << std::endl;
55+
ofs.close();
56+
ifs.open("tmp");
57+
// non-quit check
58+
testing::internal::CaptureStdout();
59+
ModuleBase::CHECK_NAME(ifs, "abacus", quit);
60+
output = testing::internal::GetCapturedStdout();
61+
EXPECT_THAT(output,testing::HasSubstr("not match"));
62+
// quit check: quit = false
63+
testing::internal::CaptureStdout();
64+
EXPECT_DEATH(ModuleBase::CHECK_NAME(ifs, "abacus"), "");
65+
output = testing::internal::GetCapturedStdout();
66+
EXPECT_THAT(output,testing::HasSubstr("NOTICE"));
67+
ifs.close();
68+
}
69+
70+
TEST_F(ToolCheckTest, Int)
71+
{
72+
ofs.open("tmp");
73+
// double input to check continus check function
74+
ofs << ecut << std::endl;
75+
ofs << ecut << std::endl;
76+
ofs.close();
77+
ifs.open("tmp");
78+
// non-quit check
79+
testing::internal::CaptureStdout();
80+
ModuleBase::CHECK_INT(ifs, 80, quit);
81+
output = testing::internal::GetCapturedStdout();
82+
EXPECT_THAT(output,testing::HasSubstr("not match"));
83+
// quit check: quit = false
84+
testing::internal::CaptureStdout();
85+
EXPECT_DEATH(ModuleBase::CHECK_INT(ifs, 80), "");
86+
output = testing::internal::GetCapturedStdout();
87+
EXPECT_THAT(output,testing::HasSubstr("NOTICE"));
88+
ifs.close();
89+
}
90+
91+
TEST_F(ToolCheckTest, Double)
92+
{
93+
ofs.open("tmp");
94+
// double input to check continus check function
95+
ofs << occupation << std::endl;
96+
ofs << occupation << std::endl;
97+
ofs.close();
98+
ifs.open("tmp");
99+
// non-quit check: quit = false
100+
testing::internal::CaptureStdout();
101+
ModuleBase::CHECK_DOUBLE(ifs, 0.23002, quit);
102+
output = testing::internal::GetCapturedStdout();
103+
EXPECT_THAT(output,testing::HasSubstr("not match"));
104+
// quit check
105+
testing::internal::CaptureStdout();
106+
EXPECT_DEATH(ModuleBase::CHECK_DOUBLE(ifs, 0.22998), "");
107+
output = testing::internal::GetCapturedStdout();
108+
EXPECT_THAT(output,testing::HasSubstr("NOTICE"));
109+
ifs.close();
110+
}
111+
112+
TEST_F(ToolCheckTest, String)
113+
{
114+
ofs.open("tmp");
115+
// double input to check continus check function
116+
ofs << caltype << std::endl;
117+
ofs << caltype << std::endl;
118+
ofs.close();
119+
ifs.open("tmp");
120+
// non-quit check: quit=false
121+
testing::internal::CaptureStdout();
122+
ModuleBase::CHECK_STRING(ifs, "scf", quit);
123+
output = testing::internal::GetCapturedStdout();
124+
EXPECT_THAT(output,testing::HasSubstr("not match"));
125+
// quit check
126+
testing::internal::CaptureStdout();
127+
EXPECT_DEATH(ModuleBase::CHECK_STRING(ifs, "scf"), "");
128+
output = testing::internal::GetCapturedStdout();
129+
EXPECT_THAT(output,testing::HasSubstr("NOTICE"));
130+
ifs.close();
131+
}

source/module_base/tool_check.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ void CHECK_DOUBLE(std::ifstream &ifs,const double &v,bool quit)
5151
const double tiny = 1.0e-5;
5252
double v_in;
5353
ifs >> v_in;
54-
if( (v - v_in) > tiny )
54+
if( fabs(v - v_in) > tiny )
5555
{
5656
if(quit)
5757
{
@@ -77,7 +77,7 @@ void CHECK_STRING(std::ifstream &ifs,const std::string &v,bool quit)
7777
{
7878
std::cout << " read in value = " << v_in << std::endl;
7979
std::cout << " the value should be = " << v << std::endl;
80-
WARNING_QUIT("CHECK_DOUBLE","the name of parameter wrong!");
80+
WARNING_QUIT("CHECK_STRING","the name of parameter wrong!");
8181
}
8282
else
8383
{
@@ -87,4 +87,4 @@ void CHECK_STRING(std::ifstream &ifs,const std::string &v,bool quit)
8787
return;
8888
}
8989

90-
}
90+
}

0 commit comments

Comments
 (0)