Skip to content
8 changes: 8 additions & 0 deletions source/source_base/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,20 @@ AddTest(
TARGET MODULE_BASE_opt_cg
LIBS parameter base device
SOURCES opt_cg_test.cpp opt_test_tools.cpp
# opt_cg/opt_tn call into base, which is built with __MPI and therefore
# reaches Parallel_Reduce::reduce_all. Keep __MPI here so this target's
# main() actually calls MPI_Init.
KEEP_FEATURE_DEFINITIONS __MPI
)

AddTest(
TARGET MODULE_BASE_opt_tn
LIBS parameter base device
SOURCES opt_tn_test.cpp opt_test_tools.cpp
# opt_cg/opt_tn call into base, which is built with __MPI and therefore
# reaches Parallel_Reduce::reduce_all. Keep __MPI here so this target's
# main() actually calls MPI_Init.
KEEP_FEATURE_DEFINITIONS __MPI
)

AddTest(
Expand Down
9 changes: 0 additions & 9 deletions source/source_base/test/global_function_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -695,9 +695,6 @@ TEST_F(GlobalFunctionTest,MemAvailable)

TEST_F(GlobalFunctionTest,BlockHere)
{
#ifdef __MPI
#undef __MPI
#endif
std::string output2;
std::string block_in="111";
GlobalV::MY_RANK=1;
Expand All @@ -712,9 +709,6 @@ TEST_F(GlobalFunctionTest,BlockHere)

TEST_F(GlobalFunctionTest,BlockHere2)
{
#ifdef __MPI
#undef __MPI
#endif
std::string output2;
std::string block_in="111";
GlobalV::MY_RANK=0;
Expand All @@ -733,9 +727,6 @@ TEST_F(GlobalFunctionTest,BlockHere2)

TEST_F(GlobalFunctionTest,BlockHere3)
{
#ifdef __MPI
#undef __MPI
#endif
std::string output2;
std::string block_in="111";
GlobalV::MY_RANK=0;
Expand Down
79 changes: 1 addition & 78 deletions source/source_base/test/math_chebyshev_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,55 +331,6 @@ TEST_F(MathChebyshevTest, tracepolyA)
delete p_chetest;
}

TEST_F(MathChebyshevTest, checkconverge)
{
#ifdef __MPI
#undef __MPI
const int norder = 100;
p_chetest = new ModuleBase::Chebyshev<double>(norder);
auto fun_sigma_y
= [&](std::complex<double>* in, std::complex<double>* out, const int m = 1) { fun.sigma_y(in, out, m); };

std::complex<double>* v = new std::complex<double>[4];
v[0] = 1.0;
v[1] = 0.0;
v[2] = 0.0;
v[3] = 1.0; //[1 0; 0 1]
double tmin = -1.1;
double tmax = 1.1;
bool converge;
converge = p_chetest->checkconverge(fun_sigma_y, v, 2, 2, tmax, tmin, 0.2);
EXPECT_TRUE(converge);
converge = p_chetest->checkconverge(fun_sigma_y, v + 2, 2, 2, tmax, tmin, 0.2);
EXPECT_TRUE(converge);
EXPECT_NEAR(tmin, -1.1, 1e-8);
EXPECT_NEAR(tmax, 1.1, 1e-8);

tmax = -1.1;
converge = p_chetest->checkconverge(fun_sigma_y, v, 2, 2, tmax, tmin, 2.2);
EXPECT_TRUE(converge);
EXPECT_NEAR(tmin, -1.1, 1e-8);
EXPECT_NEAR(tmax, 1.1, 1e-8);

// not converge
v[0] = std::complex<double>(0, 1), v[1] = 1;
fun.factor = 1.5;
tmin = -1.1, tmax = 1.1;
converge = p_chetest->checkconverge(fun_sigma_y, v, 2, 2, tmax, tmin, 0.2);
EXPECT_FALSE(converge);

fun.factor = -1.5;
tmin = -1.1, tmax = 1.1;
converge = p_chetest->checkconverge(fun_sigma_y, v, 2, 2, tmax, tmin, 0.2);
EXPECT_FALSE(converge);
fun.factor = 1;

delete[] v;
delete p_chetest;
#define __MPI
#endif
}

TEST_F(MathChebyshevTest, recurs)
{
testing::internal::CaptureStdout();
Expand Down Expand Up @@ -620,33 +571,5 @@ TEST_F(MathChebyshevTest, tracepolyA_float)
delete p_fchetest;
}

TEST_F(MathChebyshevTest, checkconverge_float)
{
#ifdef __MPI
#undef __MPI
const int norder = 100;
p_fchetest = new ModuleBase::Chebyshev<float>(norder);

std::complex<float>* v = new std::complex<float>[4];
v[0] = 1.0;
v[1] = 0.0;
v[2] = 0.0;
v[3] = 1.0; //[1 0; 0 1]
float tmin = -1.1;
float tmax = 1.1;
bool converge;

auto fun_sigma_yf
= [&](std::complex<float>* in, std::complex<float>* out, const int m = 1) { fun.sigma_y(in, out, m); };
converge = p_fchetest->checkconverge(fun_sigma_yf, v, 2, 2, tmax, tmin, 0.2);
EXPECT_TRUE(converge);
converge = p_fchetest->checkconverge(fun_sigma_yf, v + 2, 2, 2, tmax, tmin, 0.2);
EXPECT_TRUE(converge);
EXPECT_NEAR(tmin, -1.1, 1e-6);
EXPECT_NEAR(tmax, 1.1, 1e-6);
#endif

delete[] v;
delete p_fchetest;
#endif
}
#endif
40 changes: 18 additions & 22 deletions source/source_base/test/opt_cg_test.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "gtest/gtest.h"
#ifdef __MPI
#undef __MPI
#include <mpi.h>
#endif
#include "gtest/gtest.h"
#include "../opt_cg.h"
#include "../opt_dcsrch.h"
#include "./opt_test_tools.h"
Expand Down Expand Up @@ -151,71 +151,67 @@ class CG_test : public testing::Test

TEST_F(CG_test, Stand_Solve_LinearEq)
{
#ifdef __MPI
#undef __MPI
CG_Solve_LinearEq();
EXPECT_NEAR(x[0], 0.5, DOUBLETHRESHOLD);
EXPECT_NEAR(x[1], 1.6429086563584579739e-18, DOUBLETHRESHOLD);
EXPECT_NEAR(x[2], 1.5, DOUBLETHRESHOLD);
ASSERT_EQ(final_iter, 4);
ASSERT_EQ(cg.get_iter(), 4);
#define __MPI
#endif
}

TEST_F(CG_test, PR_Solve_LinearEq)
{
#ifdef __MPI
#undef __MPI
Solve(1, 0);
EXPECT_NEAR(x[0], 0.50000000000003430589, DOUBLETHRESHOLD);
EXPECT_NEAR(x[1], -3.4028335704761047964e-14, DOUBLETHRESHOLD);
EXPECT_NEAR(x[2], 1.5000000000000166533, DOUBLETHRESHOLD);
ASSERT_EQ(final_iter, 3);
ASSERT_EQ(cg.get_iter(), 3);
#define __MPI
#endif
}

TEST_F(CG_test, HZ_Solve_LinearEq)
{
#ifdef __MPI
#undef __MPI
Solve(2, 0);
EXPECT_NEAR(x[0], 0.49999999999999944489, DOUBLETHRESHOLD);
EXPECT_NEAR(x[1], -9.4368957093138305936e-16, DOUBLETHRESHOLD);
EXPECT_NEAR(x[2], 1.5000000000000011102, DOUBLETHRESHOLD);
ASSERT_EQ(final_iter, 3);
ASSERT_EQ(cg.get_iter(), 3);
#define __MPI
#endif
}

TEST_F(CG_test, PR_Min_Func)
{
#ifdef __MPI
#undef __MPI
Solve(1, 1);
EXPECT_NEAR(x[0], 4.0006805979150792396, DOUBLETHRESHOLD);
EXPECT_NEAR(x[1], 2.0713759992720870429, DOUBLETHRESHOLD);
EXPECT_NEAR(x[2], 9.2871067233169171118, DOUBLETHRESHOLD);
ASSERT_EQ(final_iter, 18);
ASSERT_EQ(cg.get_iter(), 18);
#define __MPI
#endif
}

TEST_F(CG_test, HZ_Min_Func)
{
#ifdef __MPI
#undef __MPI
Solve(2, 1);
EXPECT_NEAR(x[0], 4.0006825378033568086, DOUBLETHRESHOLD);
EXPECT_NEAR(x[1], 2.0691732100663737803, DOUBLETHRESHOLD);
EXPECT_NEAR(x[2], 9.2780872787668311474, DOUBLETHRESHOLD);
ASSERT_EQ(final_iter, 18);
ASSERT_EQ(cg.get_iter(), 18);
#define __MPI
}
// g++ -std=c++11 ../opt_CG.cpp ../opt_DCsrch.cpp ./CG_test.cpp ./test_tools.cpp -lgtest -lpthread -lgtest_main -o test.exe

int main(int argc, char** argv)
{
#ifdef __MPI
MPI_Init(&argc, &argv);
#endif

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

#ifdef __MPI
MPI_Finalize();
#endif

return result;
}
// g++ -std=c++11 ../opt_CG.cpp ../opt_DCsrch.cpp ./CG_test.cpp ./test_tools.cpp -lgtest -lpthread -lgtest_main -o test.exe
3 changes: 0 additions & 3 deletions source/source_base/test/opt_test_tools.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#ifdef __MPI
#undef __MPI
#endif
#include "./opt_test_tools.h"
#include <math.h>

Expand Down
27 changes: 19 additions & 8 deletions source/source_base/test/opt_tn_test.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#include "gtest/gtest.h"
#ifdef __MPI
#include <mpi.h>
#endif
#include "../opt_tn.hpp"
#include "../opt_dcsrch.h"
#include "./opt_test_tools.h"
Expand Down Expand Up @@ -114,28 +117,36 @@ class TN_test : public testing::Test

TEST_F(TN_test, TN_Solve_LinearEq)
{
#ifdef __MPI
#undef __MPI
Solve(0);
EXPECT_NEAR(x[0], 0.50000000000003430589, DOUBLETHRESHOLD);
EXPECT_NEAR(x[1], -3.4028335704761047964e-14, DOUBLETHRESHOLD);
EXPECT_NEAR(x[2], 1.5000000000000166533, DOUBLETHRESHOLD);
ASSERT_EQ(final_iter, 1);
ASSERT_EQ(tn.get_iter(), 1);
#define __MPI
#endif
}

TEST_F(TN_test, TN_Min_Func)
{
#ifdef __MPI
#undef __MPI
Solve(1);
EXPECT_NEAR(x[0], 4.0049968540891525137, DOUBLETHRESHOLD);
EXPECT_NEAR(x[1], 2.1208751163987624722, DOUBLETHRESHOLD);
EXPECT_NEAR(x[2], 9.4951527720891863993, DOUBLETHRESHOLD);
ASSERT_EQ(final_iter, 6);
ASSERT_EQ(tn.get_iter(), 6);
#define __MPI
}

int main(int argc, char** argv)
{
#ifdef __MPI
MPI_Init(&argc, &argv);
#endif
}

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

#ifdef __MPI
MPI_Finalize();
#endif

return result;
}
6 changes: 4 additions & 2 deletions source/source_cell/module_neighbor/test/sltk_grid_test.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "gmock/gmock.h"
#include "gtest/gtest.h"

#include "source_io/module_parameter/parameter.h"
#include "source_io/module_parameter/test_parameters.h"
#define private public
#include "source_cell/module_neighbor/sltk_grid.h"
#include "prepare_unitcell.h"
Expand Down Expand Up @@ -106,10 +108,10 @@ TEST_F(SltkGridTest, InitNoExpand)
ofs.open("test.out");
unitcell::check_dtau(ucell->atoms,ucell->ntype, ucell->lat0, ucell->latvec);
test_atom_in = 2;
PARAM.input.test_grid = 1;
TestParameters::input().test_grid = 1;
double radius = 1e-1000;
Atom_input Atom_inp(ofs, *ucell, ucell->nat, ucell->ntype, pbc, radius, test_atom_in);
Grid LatGrid(PARAM.input.test_grid);
Grid LatGrid(TestParameters::input().test_grid);
LatGrid.init(ofs, *ucell, Atom_inp);
ofs.close();
}
Expand Down
2 changes: 0 additions & 2 deletions source/source_cell/test/unitcell_test_setupcell.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#define private public
#undef private
#include "memory"
#include "source_base/mathzone.h"
#include "source_base/global_variable.h"
Expand Down
2 changes: 0 additions & 2 deletions source/source_cell/test_pw/unitcell_test_pw.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#define private public
#undef private
#include "memory"
#include "source_base/mathzone.h"
#include "source_base/global_variable.h"
Expand Down
1 change: 1 addition & 0 deletions source/source_esolver/test/esolver_dp_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define protected public
#include "../esolver_dp.h"
#include "source_io/module_parameter/input_parameter.h"
#undef protected
#undef private
/************************************************
* unit tests of class ESolver_DP
Expand Down
Loading
Loading