Repository navigation
Expand file tree
/
Copy pathtool_check_test.cpp
More file actions
129 lines (121 loc) · 3.6 KB
/
Copy pathtool_check_test.cpp
File metadata and controls
129 lines (121 loc) · 3.6 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
#include "../tool_check.h"
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include <cstdio>
#include <fstream>
/************************************************
* unit test of functions in tool_check.h
***********************************************/
/**
* - Tested Function
* - ModuleBase::CHECK_NAME
* - check the next input from ifs is std::string
*
* - ModuleBase::CHECK_INT
* - check the next input from ifs is int
*
* - ModuleBase::CHECK_DOUBLE
* - check the next input from ifs is double
*
* - ModuleBase::CHECK_STRING
* - check the next input from ifs is string
*/
class ToolCheckTest : public testing::Test
{
protected:
std::ofstream ofs;
std::ifstream ifs;
// define std::string, int, double variables
std::string name = "abaqus";
int ecut = 100;
double occupation = 0.23;
std::string caltype = "nscf";
// quit is the swith to control performance of function
bool quit = false;
// for capturing stdout
std::string output = "";
void TearDown()
{
remove("tmp");
}
};
TEST_F(ToolCheckTest, Name)
{
ofs.open("tmp");
// double input to check continus check function
ofs << name << std::endl;
ofs << name << std::endl;
ofs.close();
ifs.open("tmp");
// non-quit check
testing::internal::CaptureStdout();
ModuleBase::CHECK_NAME(ifs, "abacus", quit);
output = testing::internal::GetCapturedStdout();
EXPECT_THAT(output,testing::HasSubstr("not match"));
// quit check: quit = false
testing::internal::CaptureStdout();
EXPECT_EXIT(ModuleBase::CHECK_NAME(ifs, "abacus"), ::testing::ExitedWithCode(1), "");
output = testing::internal::GetCapturedStdout();
EXPECT_THAT(output,testing::HasSubstr("NOTICE"));
ifs.close();
}
TEST_F(ToolCheckTest, Int)
{
ofs.open("tmp");
// double input to check continus check function
ofs << ecut << std::endl;
ofs << ecut << std::endl;
ofs.close();
ifs.open("tmp");
// non-quit check
testing::internal::CaptureStdout();
ModuleBase::CHECK_INT(ifs, 80, quit);
output = testing::internal::GetCapturedStdout();
EXPECT_THAT(output,testing::HasSubstr("not match"));
// quit check: quit = false
testing::internal::CaptureStdout();
EXPECT_EXIT(ModuleBase::CHECK_INT(ifs, 80), ::testing::ExitedWithCode(1), "");
output = testing::internal::GetCapturedStdout();
EXPECT_THAT(output,testing::HasSubstr("NOTICE"));
ifs.close();
}
TEST_F(ToolCheckTest, Double)
{
ofs.open("tmp");
// double input to check continus check function
ofs << occupation << std::endl;
ofs << occupation << std::endl;
ofs.close();
ifs.open("tmp");
// non-quit check: quit = false
testing::internal::CaptureStdout();
ModuleBase::CHECK_DOUBLE(ifs, 0.23002, quit);
output = testing::internal::GetCapturedStdout();
EXPECT_THAT(output,testing::HasSubstr("not match"));
// quit check
testing::internal::CaptureStdout();
EXPECT_EXIT(ModuleBase::CHECK_DOUBLE(ifs, 0.22998), ::testing::ExitedWithCode(1), "");
output = testing::internal::GetCapturedStdout();
EXPECT_THAT(output,testing::HasSubstr("NOTICE"));
ifs.close();
}
TEST_F(ToolCheckTest, String)
{
ofs.open("tmp");
// double input to check continus check function
ofs << caltype << std::endl;
ofs << caltype << std::endl;
ofs.close();
ifs.open("tmp");
// non-quit check: quit=false
testing::internal::CaptureStdout();
ModuleBase::CHECK_STRING(ifs, "scf", quit);
output = testing::internal::GetCapturedStdout();
EXPECT_THAT(output,testing::HasSubstr("not match"));
// quit check
testing::internal::CaptureStdout();
EXPECT_EXIT(ModuleBase::CHECK_STRING(ifs, "scf"), ::testing::ExitedWithCode(1), "");
output = testing::internal::GetCapturedStdout();
EXPECT_THAT(output,testing::HasSubstr("NOTICE"));
ifs.close();
}