Skip to content

Commit 2a6365b

Browse files
authored
Merge pull request #688 from hongriTianqi/unittest
test: base_timer
2 parents adc3b67 + c610431 commit 2a6365b

File tree

3 files changed

+146
-4
lines changed

3 files changed

+146
-4
lines changed

source/module_base/test/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
remove_definitions(-D__MPI)
2+
AddTest(
3+
TARGET base_timer
4+
SOURCES timer_test.cpp ../timer.cpp
5+
)
26
AddTest(
37
TARGET base_tool_quit
48
SOURCES tool_quit_test.cpp ../tool_quit.cpp ../global_variable.cpp ../global_file.cpp ../memory.cpp ../timer.cpp
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
#include "../timer.h"
2+
#include "gtest/gtest.h"
3+
#include "gmock/gmock.h"
4+
#include <fstream>
5+
#include <cstdio>
6+
#include <chrono>
7+
#include <thread>
8+
#ifdef __MPI
9+
#include "mpi.h"
10+
#endif
11+
12+
/************************************************
13+
* unit test of class timer
14+
***********************************************/
15+
16+
/**
17+
* - Tested Functions:
18+
* - Tick
19+
* - tick 1st time, set start_flag to false
20+
* - tick 2nd time, calculate time duration
21+
* - Start
22+
* - start total time calculation
23+
* - PrintAll
24+
* - print computational processes with time > 0.1 s
25+
* - Finish
26+
* - finish total time calculation
27+
* - print computational processes with time > 0.1 s
28+
* - PrintUntilNow
29+
* - stop total time calculation
30+
* - print total time until now
31+
* - then start total time calculation again
32+
*/
33+
34+
class TimerTest : public testing::Test
35+
{
36+
protected:
37+
//
38+
// for capturing stdout
39+
std::string output;
40+
// for output in file
41+
std::ofstream ofs;
42+
std::ifstream ifs;
43+
int telapse = 100; // microseconds = 0.1 milliseconds
44+
void TearDown()
45+
{
46+
remove("tmp");
47+
}
48+
};
49+
50+
51+
TEST_F(TimerTest, Tick)
52+
{
53+
ModuleBase::timer::tick("wavefunc","evc");
54+
// after 1st call of tick, start_flag becomes false
55+
EXPECT_FALSE(ModuleBase::timer::timer_pool["wavefunc"]["evc"].start_flag);
56+
std::this_thread::sleep_for(std::chrono::microseconds(telapse)); // 0.1 ms
57+
// then we can have time elapsed in cpu_second
58+
ModuleBase::timer::tick("wavefunc","evc");
59+
EXPECT_GT(ModuleBase::timer::timer_pool["wavefunc"]["evc"].cpu_second,0.0001);
60+
}
61+
62+
TEST_F(TimerTest, Start)
63+
{
64+
ModuleBase::timer::start();
65+
// start() called tick() once
66+
EXPECT_FALSE(ModuleBase::timer::timer_pool[""]["total"].start_flag);
67+
}
68+
69+
TEST_F(TimerTest, PrintAll)
70+
{
71+
ModuleBase::timer::tick("wavefunc","evc");
72+
std::this_thread::sleep_for(std::chrono::microseconds(telapse)); // 0.1 ms
73+
ModuleBase::timer::tick("wavefunc","evc");
74+
// call print_all
75+
ofs.open("tmp");
76+
testing::internal::CaptureStdout();
77+
ModuleBase::timer::print_all(ofs);
78+
output = testing::internal::GetCapturedStdout();
79+
ofs.close();
80+
// checout output on screen
81+
EXPECT_THAT(output,testing::HasSubstr("TIME(Sec)"));
82+
// check output in file
83+
ifs.open("tmp");
84+
getline(ifs,output);
85+
getline(ifs,output);
86+
getline(ifs,output);
87+
getline(ifs,output);
88+
getline(ifs,output);
89+
EXPECT_THAT(output,testing::HasSubstr("TIME(Sec)"));
90+
ifs.close();
91+
}
92+
93+
TEST_F(TimerTest, PrintUntilNow)
94+
{
95+
long double time = ModuleBase::timer::print_until_now();
96+
EXPECT_TRUE(time>0.0);
97+
}
98+
99+
TEST_F(TimerTest, Finish)
100+
{
101+
ModuleBase::timer::tick("wavefunc","evc");
102+
std::this_thread::sleep_for(std::chrono::microseconds(telapse)); // 0.1 ms
103+
ModuleBase::timer::tick("wavefunc","evc");
104+
// call print_all
105+
ofs.open("tmp");
106+
testing::internal::CaptureStdout();
107+
ModuleBase::timer::finish(ofs);
108+
output = testing::internal::GetCapturedStdout();
109+
ofs.close();
110+
// checout output on screen
111+
EXPECT_THAT(output,testing::HasSubstr("TIME(Sec)"));
112+
// check output in file
113+
ifs.open("tmp");
114+
getline(ifs,output);
115+
getline(ifs,output);
116+
getline(ifs,output);
117+
getline(ifs,output);
118+
getline(ifs,output);
119+
EXPECT_THAT(output,testing::HasSubstr("TIME(Sec)"));
120+
ifs.close();
121+
}
122+
123+
// use __MPI to activate parallel environment
124+
#ifdef __MPI
125+
int main(int argc, char **argv)
126+
{
127+
128+
MPI_Init(&argc,&argv);
129+
130+
testing::InitGoogleTest(&argc,argv);
131+
int result = RUN_ALL_TESTS();
132+
133+
MPI_Finalize();
134+
135+
return result;
136+
}
137+
#endif
138+

source/module_base/test/tool_quit_test.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
#include "../global_variable.h"
33
#include "gtest/gtest.h"
44
#include "gmock/gmock.h"
5+
#ifdef __MPI
56
#include "mpi.h"
7+
#endif
68

79
/************************************************
810
* unit test of functions in tool_quit.h
@@ -84,20 +86,18 @@ TEST_F(ToolQuitTest,warningquit)
8486
}
8587

8688
// use __MPI to activate parallel environment
89+
#ifdef __MPI
8790
int main(int argc, char **argv)
8891
{
8992

90-
#ifdef __MPI
9193
MPI_Init(&argc,&argv);
92-
#endif
9394

9495
testing::InitGoogleTest(&argc,argv);
9596
int result = RUN_ALL_TESTS();
9697

97-
#ifdef __MPI
9898
MPI_Finalize();
99-
#endif
10099

101100
return result;
102101
}
102+
#endif
103103

0 commit comments

Comments
 (0)