|
| 1 | +#include "source_base/clebsch_gordan_coeff.h" |
| 2 | +#include "source_base/cubic_spline.h" |
| 3 | +#include "source_base/global_function.h" |
| 4 | +#include "source_base/math_integral.h" |
| 5 | +#include "source_base/math_lebedev_laikov.h" |
| 6 | +#include "source_base/mathzone_add1.h" |
| 7 | +#include "source_base/matrix.h" |
| 8 | +#include "source_base/module_device/device.h" |
| 9 | +#include "source_base/module_mixing/mixing_data.h" |
| 10 | +#include "source_base/module_out/file_reader.h" |
| 11 | +#include "source_base/output.h" |
| 12 | +#include "source_base/tool_quit.h" |
| 13 | +#include "source_base/vector3.h" |
| 14 | +#include "source_base/ylm.h" |
| 15 | + |
| 16 | +#include "gtest/gtest.h" |
| 17 | +#include <cmath> |
| 18 | +#include <complex> |
| 19 | +#include <cstdio> |
| 20 | +#include <fstream> |
| 21 | +#include <string> |
| 22 | +#include <utility> |
| 23 | +#include <vector> |
| 24 | + |
| 25 | +TEST(SourceBaseAdditionalCoverage, ClebschGordanLifecycle) |
| 26 | +{ |
| 27 | + ModuleBase::Clebsch_Gordan clebsch_gordan; |
| 28 | +} |
| 29 | + |
| 30 | +TEST(SourceBaseAdditionalCoverage, MatrixMove) |
| 31 | +{ |
| 32 | + ModuleBase::matrix source_matrix(2, 2, true); |
| 33 | + source_matrix(0, 0) = 3.0; |
| 34 | + ModuleBase::matrix moved_matrix(std::move(source_matrix)); |
| 35 | + EXPECT_EQ(moved_matrix.nr, 2); |
| 36 | + EXPECT_DOUBLE_EQ(moved_matrix(0, 0), 3.0); |
| 37 | +} |
| 38 | + |
| 39 | +TEST(SourceBaseAdditionalCoverage, VectorConstructors) |
| 40 | +{ |
| 41 | + const int x = 1; |
| 42 | + const int y = 2; |
| 43 | + const int z = 3; |
| 44 | + ModuleBase::Vector3<int> integer_vector(x, y, z); |
| 45 | + EXPECT_EQ(integer_vector.z, 3); |
| 46 | + |
| 47 | + ModuleBase::Vector3<double> source_vector(1.0, 2.0, 3.0); |
| 48 | + ModuleBase::Vector3<double> moved_vector(std::move(source_vector)); |
| 49 | + EXPECT_DOUBLE_EQ(moved_vector.y, 2.0); |
| 50 | +} |
| 51 | + |
| 52 | +TEST(SourceBaseAdditionalCoverage, MixingDataLifecycle) |
| 53 | +{ |
| 54 | + Base_Mixing::Mixing_Data mixing_data(2, 3, sizeof(double)); |
| 55 | + EXPECT_NE(mixing_data.data, nullptr); |
| 56 | + EXPECT_EQ(mixing_data.ndim_tot, 2); |
| 57 | + EXPECT_EQ(mixing_data.length, 3); |
| 58 | +} |
| 59 | + |
| 60 | +TEST(SourceBaseAdditionalCoverage, NumericalWrappers) |
| 61 | +{ |
| 62 | + const int count = 3; |
| 63 | + double points[count] = {}; |
| 64 | + double weights[count] = {}; |
| 65 | + ModuleBase::Integral::Gauss_Legendre_grid_and_weight(count, points, weights); |
| 66 | + EXPECT_NEAR(points[0], -std::sqrt(3.0 / 5.0), 1.0e-12); |
| 67 | + EXPECT_NEAR(points[1], 0.0, 1.0e-12); |
| 68 | + EXPECT_NEAR(weights[0] + weights[1] + weights[2], 2.0, 1.0e-12); |
| 69 | + |
| 70 | + double scaled_points[count] = {}; |
| 71 | + double scaled_weights[count] = {}; |
| 72 | + ModuleBase::Integral::Gauss_Legendre_grid_and_weight(0.0, 2.0, count, scaled_points, scaled_weights); |
| 73 | + EXPECT_NEAR(scaled_points[1], 1.0, 1.0e-12); |
| 74 | + EXPECT_NEAR(scaled_weights[0] + scaled_weights[1] + scaled_weights[2], 2.0, 1.0e-12); |
| 75 | + |
| 76 | + const std::complex<float> left[2] = {{1.0F, 2.0F}, {3.0F, 4.0F}}; |
| 77 | + const std::complex<float> right[2] = {{2.0F, 1.0F}, {4.0F, 3.0F}}; |
| 78 | + EXPECT_FLOAT_EQ(ModuleBase::GlobalFunc::ddot_real(2, left, right, false), 28.0F); |
| 79 | + |
| 80 | + const double knots[3] = {0.0, 1.0, 2.0}; |
| 81 | + ModuleBase::CubicSpline spline(3, knots); |
| 82 | + EXPECT_DOUBLE_EQ(spline.xmin(), 0.0); |
| 83 | + EXPECT_DOUBLE_EQ(spline.xmax(), 2.0); |
| 84 | + |
| 85 | + const double radial_values[3] = {0.0, 1.0, 4.0}; |
| 86 | + double derivative[3] = {}; |
| 87 | + ModuleBase::Mathzone_Add1::Uni_Deriv_Phi(radial_values, 3, 1.0, 1, derivative); |
| 88 | + EXPECT_TRUE(std::isfinite(derivative[1])); |
| 89 | +} |
| 90 | + |
| 91 | +TEST(SourceBaseAdditionalCoverage, LegacySphericalHarmonics) |
| 92 | +{ |
| 93 | + const int lmax = 2; |
| 94 | + const ModuleBase::Vector3<double> direction(1.0, 0.0, 0.0); |
| 95 | + double values[4] = {}; |
| 96 | + double gradients[4][3] = {}; |
| 97 | + ModuleBase::Ylm::get_ylm_real(lmax, direction, values, gradients); |
| 98 | + EXPECT_TRUE(std::isfinite(values[0])); |
| 99 | + EXPECT_TRUE(std::isfinite(gradients[1][0])); |
| 100 | + |
| 101 | + double solid_values[4] = {}; |
| 102 | + ModuleBase::Ylm::rlylm(lmax, 1.0, 0.0, 0.0, solid_values); |
| 103 | + EXPECT_TRUE(std::isfinite(solid_values[0])); |
| 104 | +} |
| 105 | + |
| 106 | +TEST(SourceBaseAdditionalCoverage, OutputAndConfigurationHelpers) |
| 107 | +{ |
| 108 | + const std::string output_file = "source_base_additional_coverage.log"; |
| 109 | + std::ofstream output_stream(output_file.c_str()); |
| 110 | + const ModuleBase::Matrix3 matrix(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0); |
| 111 | + output::printM3(output_stream, "matrix", matrix); |
| 112 | + base_device::information::record_device_memory<base_device::DEVICE_CPU>(nullptr, output_stream, "cpu", 0); |
| 113 | + output_stream.close(); |
| 114 | + |
| 115 | + std::ifstream input_stream(output_file.c_str()); |
| 116 | + std::string line; |
| 117 | + std::getline(input_stream, line); |
| 118 | + EXPECT_NE(line.find("matrix"), std::string::npos); |
| 119 | + input_stream.close(); |
| 120 | + std::remove(output_file.c_str()); |
| 121 | + |
| 122 | + ModuleBase::set_quit_out_dir("coverage-output/"); |
| 123 | + EXPECT_EQ(ModuleBase::get_global_out_dir(), "coverage-output/"); |
| 124 | + ModuleBase::set_quit_calculation("unit-test"); |
| 125 | + ModuleBase::CHECK_WARNING_QUIT(false, "coverage", "no error"); |
| 126 | + ModuleBase::GlobalFunc::NOTE("covered no-op"); |
| 127 | +} |
| 128 | + |
| 129 | +TEST(SourceBaseAdditionalCoverage, UnitCellReader) |
| 130 | +{ |
| 131 | + const std::string unit_cell_file = "source_base_unit_cell_coverage.txt"; |
| 132 | + std::ofstream unit_cell_output(unit_cell_file.c_str()); |
| 133 | + unit_cell_output << "lattice name\n"; |
| 134 | + unit_cell_output << "1.0\n"; |
| 135 | + unit_cell_output << "1 0 0\n"; |
| 136 | + unit_cell_output << "0 1 0\n"; |
| 137 | + unit_cell_output << "0 0 1\n"; |
| 138 | + unit_cell_output << "H He\n"; |
| 139 | + unit_cell_output << "1 1\n"; |
| 140 | + unit_cell_output << "Direct\n"; |
| 141 | + unit_cell_output << "0 0 0\n"; |
| 142 | + unit_cell_output << "0.5 0.5 0.5\n"; |
| 143 | + unit_cell_output << "after unit cell\n"; |
| 144 | + unit_cell_output.close(); |
| 145 | + |
| 146 | + ModuleIO::FileReader unit_cell_reader(unit_cell_file); |
| 147 | + unit_cell_reader.read_ucell(); |
| 148 | + unit_cell_reader.readLine(); |
| 149 | + EXPECT_EQ(unit_cell_reader.ss.str(), "after unit cell"); |
| 150 | + std::remove(unit_cell_file.c_str()); |
| 151 | +} |
| 152 | + |
| 153 | +TEST(SourceBaseAdditionalCoverage, FileScanningAndLebedevOutput) |
| 154 | +{ |
| 155 | + const std::string scan_file = "source_base_scan_coverage.txt"; |
| 156 | + std::ofstream scan_output(scan_file.c_str()); |
| 157 | + scan_output << "# target in a comment\n"; |
| 158 | + scan_output << "prefix target suffix\n"; |
| 159 | + scan_output.close(); |
| 160 | + |
| 161 | + std::ifstream scan_input(scan_file.c_str()); |
| 162 | + EXPECT_TRUE(ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(scan_input, "target", true, false)); |
| 163 | + scan_input.close(); |
| 164 | + std::remove(scan_file.c_str()); |
| 165 | + |
| 166 | + ModuleBase::Lebedev_laikov_grid grid(6); |
| 167 | + grid.generate_grid_points(); |
| 168 | + grid.print_grid_and_weight("source_base_lebedev_coverage"); |
| 169 | + std::ifstream lebedev_output("source_base_lebedev_coverage_degree6"); |
| 170 | + EXPECT_TRUE(lebedev_output.good()); |
| 171 | + lebedev_output.close(); |
| 172 | + std::remove("source_base_lebedev_coverage_degree6"); |
| 173 | +} |
0 commit comments