Skip to content

Commit 6d96228

Browse files
authored
Test: UT of line search in relax_new (#1822)
* Test: UT of line search in relax_new * change name * update CMakeLists.txt
1 parent 89d4264 commit 6d96228

File tree

3 files changed

+83
-1
lines changed

3 files changed

+83
-1
lines changed

source/module_relax/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,8 @@ add_library(
2222

2323
if(ENABLE_COVERAGE)
2424
add_coverage(relax)
25-
endif()
25+
endif()
26+
27+
if(BUILD_TESTING)
28+
add_subdirectory(relax_new/test)
29+
endif()
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
remove_definitions(-D__MPI)
2+
remove_definitions(-D__LCAO)
3+
remove_definitions(-D__DEEPKS)
4+
remove_definitions(-D__CUDA)
5+
remove_definitions(-D__ROCM)
6+
AddTest(
7+
TARGET relax_new_line_search
8+
SOURCES line_search_test.cpp ../line_search.cpp ../../../module_base/tool_quit.cpp ../../../module_base/global_variable.cpp ../../../module_base/global_file.cpp ../../../module_base/memory.cpp ../../../module_base/timer.cpp
9+
)
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include "gtest/gtest.h"
2+
3+
/************************************************
4+
* unit test of class Line_Search
5+
***********************************************/
6+
7+
/**
8+
* - Tested Functions:
9+
* - Line_Search::line_search()
10+
* - Line_Search::first_order()
11+
* - Line_Search::third_order()
12+
* - Line_Search::init_brent()
13+
* - Line_Search::update_brent()
14+
* - Line_Search::brent()
15+
*/
16+
17+
18+
#define private public
19+
#include "module_relax/relax_new/line_search.h"
20+
21+
class LineSearchPrepare
22+
{
23+
public:
24+
LineSearchPrepare(
25+
bool restart_in,
26+
double x_in,
27+
double y_in,
28+
double f_in,
29+
double xnew_in,
30+
double conv_thr_in):
31+
restart(restart_in),
32+
x(x_in),
33+
y(y_in),
34+
f(f_in),
35+
xnew(xnew_in),
36+
conv_thr(conv_thr_in)
37+
{}
38+
bool restart;
39+
double x;
40+
double y;
41+
double f;
42+
double xnew;
43+
double conv_thr;
44+
};
45+
46+
47+
class LineSearchTest : public ::testing::TestWithParam<LineSearchPrepare>{};
48+
49+
TEST_P(LineSearchTest,LineSearch)
50+
{
51+
LineSearchPrepare lsp = GetParam();
52+
Line_Search ls;
53+
EXPECT_EQ(ls.ls_step,0);
54+
bool test_conv = ls.line_search(lsp.restart,lsp.x,lsp.y,lsp.f,lsp.xnew,lsp.conv_thr);
55+
EXPECT_EQ(ls.ls_step,1);
56+
while (!test_conv)
57+
{
58+
test_conv = ls.line_search(false,lsp.x,lsp.y,lsp.f,lsp.xnew,lsp.conv_thr);
59+
}
60+
EXPECT_GE(ls.ls_step,3);
61+
EXPECT_TRUE(test_conv);
62+
}
63+
64+
INSTANTIATE_TEST_SUITE_P(LineSearch,LineSearchTest,::testing::Values(
65+
// restart, x, y, f, xnew, conv_thr
66+
LineSearchPrepare(true,5.0,100.1,-10.0,0.0,1e-6)
67+
));
68+
69+
#undef private

0 commit comments

Comments
 (0)