|
| 1 | +#ifdef __MPI |
| 2 | +#include "mpi.h" |
| 3 | +#include "gtest/gtest.h" |
| 4 | +#include "module_base/global_variable.h" |
| 5 | +#include "src_parallel/parallel_common.h" |
| 6 | +#include <complex> |
| 7 | +#include <string> |
| 8 | +#include <cstring> |
| 9 | + |
| 10 | +/************************************************ |
| 11 | + * unit test of functions in parallel_common.cpp |
| 12 | + ***********************************************/ |
| 13 | + |
| 14 | +/** |
| 15 | + * The tested functions are wrappers of MPI_Bcast |
| 16 | + * in ABACUS, as defined in src_parallel/parallel_common.h. |
| 17 | + * The source is process 0 in all MPI_Bcast |
| 18 | + * wrappers. |
| 19 | + */ |
| 20 | + |
| 21 | +class ParaCommon : public testing::Test |
| 22 | +{ |
| 23 | +protected: |
| 24 | + bool boo=true; |
| 25 | + int is=0; |
| 26 | + double fs = 0.0; |
| 27 | + std::complex<double> imgs{0.0,0.0}; |
| 28 | + std::string chs ="abacus"; |
| 29 | + char cha[7]="abacus"; |
| 30 | + int iv[10]={0}; |
| 31 | + double fv[10] = {0}; |
| 32 | + std::complex<double> imgv[10]={0.0,0.0}; |
| 33 | + std::string chv[10] = {""}; |
| 34 | +}; |
| 35 | + |
| 36 | +TEST_F(ParaCommon,Bcast) |
| 37 | +{ |
| 38 | + // reset data in the first process |
| 39 | + if(GlobalV::MY_RANK==0) |
| 40 | + { |
| 41 | + boo=false; |
| 42 | + is=1; |
| 43 | + fs=1.0; |
| 44 | + imgs=std::complex<double>(1.0,-1.0); |
| 45 | + chs ="ABACUS"; |
| 46 | + strcpy(cha,chs.c_str()); |
| 47 | + for (int i=0;i<10;i++) |
| 48 | + { |
| 49 | + double ii=static_cast<double>(i); |
| 50 | + iv[i] = i; |
| 51 | + fv[i] = ii; |
| 52 | + imgv[i]=std::complex<double>(ii,-ii); |
| 53 | + std::stringstream ss; |
| 54 | + ss<<chs<<i; |
| 55 | + chv[i] = ss.str(); |
| 56 | + } |
| 57 | + } |
| 58 | + // call bcast wrappers |
| 59 | + Parallel_Common::bcast_bool(boo); |
| 60 | + Parallel_Common::bcast_int(is); |
| 61 | + Parallel_Common::bcast_double(fs); |
| 62 | + Parallel_Common::bcast_complex_double(imgs); |
| 63 | + Parallel_Common::bcast_string(chs); |
| 64 | + Parallel_Common::bcast_char(cha,7); |
| 65 | + Parallel_Common::bcast_int(iv,10); |
| 66 | + Parallel_Common::bcast_double(fv,10); |
| 67 | + Parallel_Common::bcast_complex_double(imgv,10); |
| 68 | + Parallel_Common::bcast_string(chv,10); |
| 69 | + // make comparisons |
| 70 | + EXPECT_FALSE(boo); |
| 71 | + EXPECT_EQ(is,1); |
| 72 | + EXPECT_EQ(fs,1.0); |
| 73 | + EXPECT_NEAR(imgs.real(),1.0,1E-15); |
| 74 | + EXPECT_NEAR(imgs.imag(),-1.0,1E-15); |
| 75 | + EXPECT_EQ(chs,"ABACUS"); |
| 76 | + EXPECT_STREQ(cha,"ABACUS"); |
| 77 | + for (int i=0;i<10;i++) |
| 78 | + { |
| 79 | + double ii=static_cast<double>(i); |
| 80 | + EXPECT_EQ(iv[i],i); |
| 81 | + EXPECT_NEAR(fv[i],ii,1E-15); |
| 82 | + EXPECT_NEAR(imgv[i].real(),ii,1E-15); |
| 83 | + EXPECT_NEAR(imgv[i].imag(),-ii,1E-15); |
| 84 | + std::stringstream ss; |
| 85 | + ss<<chs<<i; |
| 86 | + EXPECT_EQ(chv[i],ss.str()); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +int main(int argc, char **argv) |
| 91 | +{ |
| 92 | + |
| 93 | + MPI_Init(&argc, &argv); |
| 94 | + testing::InitGoogleTest(&argc, argv); |
| 95 | + |
| 96 | + MPI_Comm_size(MPI_COMM_WORLD,&GlobalV::NPROC); |
| 97 | + MPI_Comm_rank(MPI_COMM_WORLD,&GlobalV::MY_RANK); |
| 98 | + |
| 99 | + int result = RUN_ALL_TESTS(); |
| 100 | + |
| 101 | + MPI_Finalize(); |
| 102 | + |
| 103 | + return result; |
| 104 | +} |
| 105 | +#endif |
0 commit comments