use same main() function as with c-library interface.

This commit is contained in:
Axel Kohlmeyer
2023-02-02 21:09:45 -05:00
parent 8a0e9f6018
commit f717debbd4
4 changed files with 109 additions and 32 deletions

View File

@ -30,69 +30,69 @@ if(CMAKE_Fortran_COMPILER)
target_link_libraries(flammps PUBLIC gfortran) target_link_libraries(flammps PUBLIC gfortran)
endif() endif()
add_executable(test_fortran_create wrap_create.cpp test_fortran_create.f90) add_executable(test_fortran_create wrap_create.cpp test_fortran_create.f90 test_main.cpp)
target_link_libraries(test_fortran_create PRIVATE flammps lammps GTest::GTestMain) target_link_libraries(test_fortran_create PRIVATE flammps lammps GTest::GMock)
target_include_directories(test_fortran_create PRIVATE "${LAMMPS_SOURCE_DIR}/../fortran") target_include_directories(test_fortran_create PRIVATE "${LAMMPS_SOURCE_DIR}/../fortran")
add_test(NAME FortranOpen COMMAND test_fortran_create) add_test(NAME FortranOpen COMMAND test_fortran_create)
add_executable(test_fortran_commands wrap_commands.cpp test_fortran_commands.f90) add_executable(test_fortran_commands wrap_commands.cpp test_fortran_commands.f90 test_main.cpp)
target_link_libraries(test_fortran_commands PRIVATE flammps lammps GTest::GTestMain) target_link_libraries(test_fortran_commands PRIVATE flammps lammps GTest::GMock)
add_test(NAME FortranCommands COMMAND test_fortran_commands) add_test(NAME FortranCommands COMMAND test_fortran_commands)
add_executable(test_fortran_get_thermo wrap_get_thermo.cpp test_fortran_get_thermo.f90) add_executable(test_fortran_get_thermo wrap_get_thermo.cpp test_fortran_get_thermo.f90 test_main.cpp)
target_link_libraries(test_fortran_get_thermo PRIVATE flammps lammps GTest::GTestMain) target_link_libraries(test_fortran_get_thermo PRIVATE flammps lammps GTest::GMock)
add_test(NAME FortranGetThermo COMMAND test_fortran_get_thermo) add_test(NAME FortranGetThermo COMMAND test_fortran_get_thermo)
add_executable(test_fortran_box wrap_box.cpp test_fortran_box.f90) add_executable(test_fortran_box wrap_box.cpp test_fortran_box.f90 test_main.cpp)
target_link_libraries(test_fortran_box PRIVATE flammps lammps GTest::GTestMain) target_link_libraries(test_fortran_box PRIVATE flammps lammps GTest::GMock)
add_test(NAME FortranBox COMMAND test_fortran_box) add_test(NAME FortranBox COMMAND test_fortran_box)
add_executable(test_fortran_properties wrap_properties.cpp test_fortran_properties.f90 test_fortran_commands.f90) add_executable(test_fortran_properties wrap_properties.cpp test_fortran_properties.f90 test_fortran_commands.f90 test_main.cpp)
target_link_libraries(test_fortran_properties PRIVATE flammps lammps GTest::GMockMain) target_link_libraries(test_fortran_properties PRIVATE flammps lammps GTest::GMockMain)
add_test(NAME FortranProperties COMMAND test_fortran_properties) add_test(NAME FortranProperties COMMAND test_fortran_properties)
add_executable(test_fortran_extract_global wrap_extract_global.cpp test_fortran_extract_global.f90) add_executable(test_fortran_extract_global wrap_extract_global.cpp test_fortran_extract_global.f90 test_main.cpp)
target_link_libraries(test_fortran_extract_global PRIVATE flammps lammps GTest::GTestMain) target_link_libraries(test_fortran_extract_global PRIVATE flammps lammps GTest::GMock)
add_test(NAME FortranExtractGlobal COMMAND test_fortran_extract_global) add_test(NAME FortranExtractGlobal COMMAND test_fortran_extract_global)
add_executable(test_fortran_extract_atom wrap_extract_atom.cpp test_fortran_extract_atom.f90) add_executable(test_fortran_extract_atom wrap_extract_atom.cpp test_fortran_extract_atom.f90 test_main.cpp)
target_link_libraries(test_fortran_extract_atom PRIVATE flammps lammps GTest::GTestMain) target_link_libraries(test_fortran_extract_atom PRIVATE flammps lammps GTest::GMock)
add_test(NAME FortranExtractAtom COMMAND test_fortran_extract_atom) add_test(NAME FortranExtractAtom COMMAND test_fortran_extract_atom)
add_executable(test_fortran_extract_compute wrap_extract_compute.cpp test_fortran_extract_compute.f90) add_executable(test_fortran_extract_compute wrap_extract_compute.cpp test_fortran_extract_compute.f90 test_main.cpp)
target_link_libraries(test_fortran_extract_compute PRIVATE flammps lammps GTest::GTestMain) target_link_libraries(test_fortran_extract_compute PRIVATE flammps lammps GTest::GMock)
add_test(NAME FortranExtractCompute COMMAND test_fortran_extract_compute) add_test(NAME FortranExtractCompute COMMAND test_fortran_extract_compute)
add_executable(test_fortran_extract_fix wrap_extract_fix.cpp test_fortran_extract_fix.f90) add_executable(test_fortran_extract_fix wrap_extract_fix.cpp test_fortran_extract_fix.f90 test_main.cpp)
target_link_libraries(test_fortran_extract_fix PRIVATE flammps lammps GTest::GTestMain) target_link_libraries(test_fortran_extract_fix PRIVATE flammps lammps GTest::GMock)
add_test(NAME FortranExtractFix COMMAND test_fortran_extract_fix) add_test(NAME FortranExtractFix COMMAND test_fortran_extract_fix)
add_executable(test_fortran_extract_variable wrap_extract_variable.cpp test_fortran_extract_variable.f90) add_executable(test_fortran_extract_variable wrap_extract_variable.cpp test_fortran_extract_variable.f90 test_main.cpp)
target_compile_definitions(test_fortran_extract_variable PRIVATE -DTEST_INPUT_FOLDER=${CMAKE_CURRENT_SOURCE_DIR}) target_compile_definitions(test_fortran_extract_variable PRIVATE -DTEST_INPUT_FOLDER=${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(test_fortran_extract_variable PRIVATE flammps lammps GTest::GTestMain) target_link_libraries(test_fortran_extract_variable PRIVATE flammps lammps GTest::GMock)
add_test(NAME FortranExtractVariable COMMAND test_fortran_extract_variable) add_test(NAME FortranExtractVariable COMMAND test_fortran_extract_variable)
add_executable(test_fortran_gather_scatter wrap_gather_scatter.cpp test_fortran_gather_scatter.f90) add_executable(test_fortran_gather_scatter wrap_gather_scatter.cpp test_fortran_gather_scatter.f90 test_main.cpp)
target_compile_definitions(test_fortran_gather_scatter PRIVATE -DTEST_INPUT_FOLDER=${CMAKE_CURRENT_SOURCE_DIR}) target_compile_definitions(test_fortran_gather_scatter PRIVATE -DTEST_INPUT_FOLDER=${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(test_fortran_gather_scatter PRIVATE flammps lammps GTest::GTestMain) target_link_libraries(test_fortran_gather_scatter PRIVATE flammps lammps GTest::GMock)
add_test(NAME FortranGatherScatter COMMAND test_fortran_gather_scatter) add_test(NAME FortranGatherScatter COMMAND test_fortran_gather_scatter)
add_executable(test_fortran_create_atoms wrap_create_atoms.cpp test_fortran_create_atoms.f90) add_executable(test_fortran_create_atoms wrap_create_atoms.cpp test_fortran_create_atoms.f90 test_main.cpp)
target_link_libraries(test_fortran_create_atoms PRIVATE flammps lammps GTest::GTestMain) target_link_libraries(test_fortran_create_atoms PRIVATE flammps lammps GTest::GMock)
add_test(NAME FortranCreateAtoms COMMAND test_fortran_create_atoms) add_test(NAME FortranCreateAtoms COMMAND test_fortran_create_atoms)
add_executable(test_fortran_configuration wrap_configuration.cpp test_fortran_configuration.f90 test_fortran_commands.f90) add_executable(test_fortran_configuration wrap_configuration.cpp test_fortran_configuration.f90 test_fortran_commands.f90 test_main.cpp)
target_link_libraries(test_fortran_configuration PRIVATE flammps lammps GTest::GMockMain) target_link_libraries(test_fortran_configuration PRIVATE flammps lammps GTest::GMock)
add_test(NAME FortranConfiguration COMMAND test_fortran_configuration) add_test(NAME FortranConfiguration COMMAND test_fortran_configuration)
add_executable(test_fortran_neighlist wrap_neighlist.cpp test_fortran_neighlist.f90) add_executable(test_fortran_neighlist wrap_neighlist.cpp test_fortran_neighlist.f90 test_main.cpp)
target_link_libraries(test_fortran_neighlist PRIVATE flammps lammps GTest::GMockMain) target_link_libraries(test_fortran_neighlist PRIVATE flammps lammps GTest::GMock)
add_test(NAME FortranNeighlist COMMAND test_fortran_neighlist) add_test(NAME FortranNeighlist COMMAND test_fortran_neighlist)
# gfortran from GCC 9 on ubuntu has an ICE compiling this test. # gfortran from GCC 9 on ubuntu has an ICE compiling this test.
if ((CMAKE_Fortran_COMPILER_ID STREQUAL "GNU") AND (CMAKE_Fortran_COMPILER_VERSION VERSION_GREATER 9.9)) if ((CMAKE_Fortran_COMPILER_ID STREQUAL "GNU") AND (CMAKE_Fortran_COMPILER_VERSION VERSION_GREATER 9.9))
add_executable(test_fortran_fixexternal wrap_fixexternal.cpp test_fortran_fixexternal.f90) add_executable(test_fortran_fixexternal wrap_fixexternal.cpp test_fortran_fixexternal.f90 test_main.cpp)
target_link_libraries(test_fortran_fixexternal PRIVATE flammps lammps GTest::GMockMain) target_link_libraries(test_fortran_fixexternal PRIVATE flammps lammps GTest::GMock)
add_test(NAME FortranFixExternal COMMAND test_fortran_fixexternal) add_test(NAME FortranFixExternal COMMAND test_fortran_fixexternal)
endif() endif()

View File

@ -0,0 +1,60 @@
/* ----------------------------------------------------------------------
LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
https://www.lammps.org/, Sandia National Laboratories
LAMMPS Development team: developers@lammps.org
Copyright (2003) Sandia Corporation. Under the terms of Contract
DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains
certain rights in this software. This software is distributed under
the GNU General Public License.
See the README file in the top-level LAMMPS directory.
------------------------------------------------------------------------- */
#include "test_main.h"
#include "pointers.h"
#include "utils.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <iostream>
#include <mpi.h>
// whether to print verbose output (i.e. not capturing LAMMPS screen output).
bool verbose = false;
int main(int argc, char **argv)
{
MPI_Init(&argc, &argv);
::testing::InitGoogleMock(&argc, argv);
if (argc < 1) {
return 1;
}
// handle arguments passed via environment variable
if (const char *var = getenv("TEST_ARGS")) {
std::vector<std::string> env = LAMMPS_NS::utils::split_words(var);
for (auto arg : env) {
if (arg == "-v") {
verbose = true;
}
}
}
int iarg = 1;
while (iarg < argc) {
if (strcmp(argv[iarg], "-v") == 0) {
verbose = true;
++iarg;
} else {
std::cerr << "unknown option: " << argv[iarg] << "\n\n";
MPI_Finalize();
return 1;
}
}
int rv = RUN_ALL_TESTS();
MPI_Finalize();
return rv;
}

View File

@ -0,0 +1,19 @@
/* -*- c++ -*- ----------------------------------------------------------
LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
https://www.lammps.org/ Sandia National Laboratories
LAMMPS Development team: developers@lammps.org
Copyright (2003) Sandia Corporation. Under the terms of Contract
DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains
certain rights in this software. This software is distributed under
the GNU General Public License.
See the README file in the top-level LAMMPS directory.
------------------------------------------------------------------------- */
#ifndef TEST_MAIN_H
#define TEST_MAIN_H
extern bool verbose;
#endif

View File

@ -30,13 +30,11 @@ TEST(open_no_mpi, no_args)
::testing::internal::CaptureStdout(); ::testing::internal::CaptureStdout();
int mpi_init = 0; int mpi_init = 0;
MPI_Initialized(&mpi_init); MPI_Initialized(&mpi_init);
EXPECT_EQ(mpi_init, 0); EXPECT_EQ(mpi_init, 1);
void *handle = f_lammps_no_mpi_no_args(); void *handle = f_lammps_no_mpi_no_args();
std::string output = ::testing::internal::GetCapturedStdout(); std::string output = ::testing::internal::GetCapturedStdout();
EXPECT_STREQ(output.substr(0, 6).c_str(), "LAMMPS"); EXPECT_STREQ(output.substr(0, 6).c_str(), "LAMMPS");
LAMMPS_NS::LAMMPS *lmp = (LAMMPS_NS::LAMMPS *)handle; LAMMPS_NS::LAMMPS *lmp = (LAMMPS_NS::LAMMPS *)handle;
MPI_Initialized(&mpi_init);
EXPECT_NE(mpi_init, 0);
EXPECT_EQ(lmp->world, MPI_COMM_WORLD); EXPECT_EQ(lmp->world, MPI_COMM_WORLD);
EXPECT_EQ(lmp->infile, stdin); EXPECT_EQ(lmp->infile, stdin);
EXPECT_EQ(lmp->screen, stdout); EXPECT_EQ(lmp->screen, stdout);