Unverified Commit 69cffb2d authored by Axel Kohlmeyer's avatar Axel Kohlmeyer
Browse files

import test infrastructure for c, c++ and python library usage

parent dc241abb
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -3,6 +3,9 @@ include(GTest)
add_subdirectory(utils)
add_subdirectory(formats)
add_subdirectory(commands)
add_subdirectory(c-library)
add_subdirectory(cplusplus)
add_subdirectory(python)
add_subdirectory(force-styles)

find_package(ClangFormat 8.0)
+13 −0
Original line number Diff line number Diff line

add_executable(library-open library-open.cpp)
target_link_libraries(library-open PRIVATE lammps GTest::GTest GTest::GTestMain)
add_test(LibraryOpen library-open)

add_executable(library-commands library-commands.cpp)
target_link_libraries(library-commands PRIVATE lammps GTest::GTest GTest::GTestMain)
add_test(LibraryCommands library-commands)

add_executable(library-properties library-properties.cpp)
target_link_libraries(library-properties PRIVATE lammps GTest::GTest GTest::GTestMain)
add_test(LibraryProperties library-properties)
+103 −0
Original line number Diff line number Diff line
// unit tests for issuing command to a LAMMPS instance through the library interface

#include "library.h"
#include "lammps.h"
#include <string>

#include "gtest/gtest.h"

const char *demo_input[] = {
      "region       box block 0 $x 0 2 0 2",
      "create_box 1 box",
      "create_atoms 1 single 1.0 1.0 ${zpos}" };
const char *cont_input[] = {
    "create_atoms 1 single &",
    "0.2 0.1 0.1" };

class LAMMPS_commands : public ::testing::Test
{
protected:
    void *lmp;
    LAMMPS_commands() {};
    ~LAMMPS_commands() override {};

    void SetUp() override {
        const char *args[] = {"LAMMPS_test",
                              "-log", "none",
                              "-echo", "screen",
                              "-nocite", "-var","x","2",
                              "-var", "zpos", "1.5"};
        char **argv = (char **)args;
        int argc = sizeof(args)/sizeof(char *);

        ::testing::internal::CaptureStdout();
        lmp = lammps_open_no_mpi(argc, argv, NULL);
        std::string output = ::testing::internal::GetCapturedStdout();
        EXPECT_STREQ(output.substr(0,8).c_str(), "LAMMPS (");
    }
    void TearDown() override {
        ::testing::internal::CaptureStdout();
        lammps_close(lmp);
        std::string output = ::testing::internal::GetCapturedStdout();
        EXPECT_STREQ(output.substr(0,16).c_str(), "Total wall time:");
        lmp = nullptr;
    }
};

TEST_F(LAMMPS_commands, from_file) {
    FILE *fp;
    const char demo_file[] = "in.test";
    const char cont_file[] = "in.cont";

    fp = fopen(demo_file,"w");
    for (unsigned int i=0; i < sizeof(demo_input)/sizeof(char *); ++i) {
        fputs(demo_input[i],fp);
        fputc('\n',fp);
    }
    fclose(fp);
    fp = fopen(cont_file,"w");
    for (unsigned int i=0; i < sizeof(cont_input)/sizeof(char *); ++i) {
        fputs(cont_input[i],fp);
        fputc('\n',fp);
    }
    fclose(fp);

    EXPECT_EQ(lammps_get_natoms(lmp),0);
    lammps_file(lmp,demo_file);
    lammps_file(lmp,cont_file);
    EXPECT_EQ(lammps_get_natoms(lmp),2);

    unlink(demo_file);
    unlink(cont_file);
};

TEST_F(LAMMPS_commands, from_line) {
    EXPECT_EQ(lammps_get_natoms(lmp),0);
    for (unsigned int i=0; i < sizeof(demo_input)/sizeof(char *); ++i) {
        lammps_command(lmp,demo_input[i]);
    }
    EXPECT_EQ(lammps_get_natoms(lmp),1);
};

TEST_F(LAMMPS_commands, from_list) {
    EXPECT_EQ(lammps_get_natoms(lmp),0);
    lammps_commands_list(lmp,sizeof(demo_input)/sizeof(char *),demo_input);
    lammps_commands_list(lmp,sizeof(cont_input)/sizeof(char *),cont_input);
    EXPECT_EQ(lammps_get_natoms(lmp),2);
};

TEST_F(LAMMPS_commands, from_string) {
    std::string cmds("");

    for (unsigned int i=0; i < sizeof(demo_input)/sizeof(char *); ++i) {
        cmds += demo_input[i];
        cmds += "\n";
    }
    for (unsigned int i=0; i < sizeof(cont_input)/sizeof(char *); ++i) {
        cmds += cont_input[i];
        cmds += "\n";
    }
    EXPECT_EQ(lammps_get_natoms(lmp),0);
    lammps_commands_string(lmp,cmds.c_str());
    EXPECT_EQ(lammps_get_natoms(lmp),2);
};
+186 −0
Original line number Diff line number Diff line
// unit tests for the LAMMPS base class

#include "library.h"
#include "lammps.h"
#include <mpi.h>
#include <cstdio>  // for stdin, stdout
#include <string>

#include "gtest/gtest.h"

TEST(lammps_open, null_args) {
    ::testing::internal::CaptureStdout();
    void *handle = lammps_open(0,NULL, MPI_COMM_WORLD, NULL);
    std::string output = ::testing::internal::GetCapturedStdout();
    EXPECT_STREQ(output.substr(0,6).c_str(),"LAMMPS");
    int mpi_init=0;
    MPI_Initialized(&mpi_init);
    EXPECT_GT(mpi_init,0);
    LAMMPS_NS::LAMMPS *lmp = (LAMMPS_NS::LAMMPS *)handle;
    EXPECT_EQ(lmp->world, MPI_COMM_WORLD);
    EXPECT_EQ(lmp->infile, stdin);
    EXPECT_EQ(lmp->screen, stdout);
    EXPECT_NE(lmp->citeme, nullptr);
    ::testing::internal::CaptureStdout();
    lammps_close(handle);
    output = ::testing::internal::GetCapturedStdout();
    EXPECT_STREQ(output.substr(0,16).c_str(), "Total wall time:");
}

TEST(lammps_open, with_args) {
    const char *args[] = {"liblammps",
                          "-log", "none",
                          "-nocite"};
    char **argv = (char **)args;
    int argc = sizeof(args)/sizeof(char *);

    // MPI is already initialized
    MPI_Comm mycomm;
    MPI_Comm_split(MPI_COMM_WORLD, 0, 1, &mycomm);
    ::testing::internal::CaptureStdout();
    void *alt_ptr;
    void *handle = lammps_open(argc, argv, mycomm, &alt_ptr);
    std::string output = ::testing::internal::GetCapturedStdout();
    EXPECT_STREQ(output.substr(0,6).c_str(),"LAMMPS");
    EXPECT_EQ(handle,alt_ptr);
    LAMMPS_NS::LAMMPS *lmp = (LAMMPS_NS::LAMMPS *)handle;

    // MPI STUBS uses no real communicators
#if !defined(MPI_STUBS)
    EXPECT_NE(lmp->world, MPI_COMM_WORLD);
#endif

    EXPECT_EQ(lmp->world, mycomm);
    EXPECT_EQ(lmp->infile, stdin);
    EXPECT_EQ(lmp->logfile, nullptr);
    EXPECT_EQ(lmp->citeme, nullptr);
    EXPECT_EQ(lmp->kokkos, nullptr);
    EXPECT_EQ(lmp->atomKK, nullptr);
    EXPECT_EQ(lmp->memoryKK, nullptr);
    ::testing::internal::CaptureStdout();
    lammps_close(handle);
    output = ::testing::internal::GetCapturedStdout();
    EXPECT_STREQ(output.substr(0,16).c_str(), "Total wall time:");
}

TEST(lammps_open, with_kokkos) {
    if (!LAMMPS_NS::LAMMPS::is_installed_pkg("KOKKOS")) GTEST_SKIP();
    const char *args[] = {"liblammps",
                          "-k", "on", "t", "2",
                          "-sf", "kk",
                          "-log", "none" };
    char **argv = (char **)args;
    int argc = sizeof(args)/sizeof(char *);

    ::testing::internal::CaptureStdout();
    void *alt_ptr;
    void *handle = lammps_open(argc, argv, MPI_COMM_WORLD, &alt_ptr);
    std::string output = ::testing::internal::GetCapturedStdout();
    EXPECT_STREQ(output.substr(0,6).c_str(),"LAMMPS");
    EXPECT_EQ(handle,alt_ptr);
    LAMMPS_NS::LAMMPS *lmp = (LAMMPS_NS::LAMMPS *)handle;

    EXPECT_EQ(lmp->world, MPI_COMM_WORLD);
    EXPECT_EQ(lmp->infile, stdin);
    EXPECT_EQ(lmp->logfile, nullptr);
    EXPECT_NE(lmp->citeme, nullptr);
    EXPECT_EQ(lmp->num_package, 0);
    EXPECT_NE(lmp->kokkos, nullptr);
    EXPECT_NE(lmp->atomKK, nullptr);
    EXPECT_NE(lmp->memoryKK, nullptr);
    ::testing::internal::CaptureStdout();
    lammps_close(handle);
    output = ::testing::internal::GetCapturedStdout();
    EXPECT_STREQ(output.substr(0,16).c_str(), "Total wall time:");
}

TEST(lammps_open_no_mpi, no_screen) {
    const char *args[] = {"liblammps",
                          "-log", "none",
                          "-screen", "none",
                          "-nocite"};
    char **argv = (char **)args;
    int argc = sizeof(args)/sizeof(char *);

    ::testing::internal::CaptureStdout();
    void *alt_ptr;
    void *handle = lammps_open_no_mpi(argc, argv, &alt_ptr);
    std::string output = ::testing::internal::GetCapturedStdout();
    EXPECT_STREQ(output.c_str(),"");
    EXPECT_EQ(handle,alt_ptr);
    LAMMPS_NS::LAMMPS *lmp = (LAMMPS_NS::LAMMPS *)handle;

    EXPECT_EQ(lmp->world, MPI_COMM_WORLD);
    EXPECT_EQ(lmp->infile, stdin);
    EXPECT_EQ(lmp->screen, nullptr);
    EXPECT_EQ(lmp->logfile, nullptr);
    EXPECT_EQ(lmp->citeme, nullptr);
    EXPECT_EQ(lmp->suffix_enable, 0);

    EXPECT_STREQ(lmp->exename, "liblammps");
    EXPECT_EQ(lmp->num_package, 0);
    ::testing::internal::CaptureStdout();
    lammps_close(handle);
    output = ::testing::internal::GetCapturedStdout();
    EXPECT_STREQ(output.c_str(), "");
}

TEST(lammps_open_no_mpi, with_omp) {
    if (!LAMMPS_NS::LAMMPS::is_installed_pkg("USER-OMP")) GTEST_SKIP();
    const char *args[] = {"liblammps",
                          "-pk", "omp", "2", "neigh", "no",
                          "-sf", "omp",
                          "-log", "none",
                          "-nocite"};
    char **argv = (char **)args;
    int argc = sizeof(args)/sizeof(char *);

    ::testing::internal::CaptureStdout();
    void *alt_ptr;
    void *handle = lammps_open_no_mpi(argc, argv, &alt_ptr);
    std::string output = ::testing::internal::GetCapturedStdout();
    EXPECT_STREQ(output.substr(0,6).c_str(),"LAMMPS");
    EXPECT_EQ(handle,alt_ptr);
    LAMMPS_NS::LAMMPS *lmp = (LAMMPS_NS::LAMMPS *)handle;

    EXPECT_EQ(lmp->world, MPI_COMM_WORLD);
    EXPECT_EQ(lmp->infile, stdin);
    EXPECT_EQ(lmp->logfile, nullptr);
    EXPECT_EQ(lmp->citeme, nullptr);
    EXPECT_EQ(lmp->suffix_enable, 1);
    EXPECT_STREQ(lmp->suffix, "omp");
    EXPECT_EQ(lmp->suffix2, nullptr);
    EXPECT_STREQ(lmp->exename, "liblammps");
    EXPECT_EQ(lmp->num_package, 1);
    ::testing::internal::CaptureStdout();
    lammps_close(handle);
    output = ::testing::internal::GetCapturedStdout();
    EXPECT_STREQ(output.substr(0,16).c_str(), "Total wall time:");
}

TEST(lammps_open_fortran, no_args) {
    // MPI is already initialized
    MPI_Comm mycomm;
    MPI_Comm_split(MPI_COMM_WORLD, 0, 1, &mycomm);
    int fcomm = MPI_Comm_c2f(mycomm);
    ::testing::internal::CaptureStdout();
    void *handle = lammps_open_fortran(0, NULL, fcomm, NULL);
    std::string output = ::testing::internal::GetCapturedStdout();
    EXPECT_STREQ(output.substr(0,6).c_str(),"LAMMPS");
    LAMMPS_NS::LAMMPS *lmp = (LAMMPS_NS::LAMMPS *)handle;

    // MPI STUBS uses no real communicators
#if !defined(MPI_STUBS)
    EXPECT_NE(lmp->world, MPI_COMM_WORLD);
#endif

    EXPECT_EQ(lmp->world, mycomm);
    EXPECT_EQ(lmp->infile, stdin);
    EXPECT_EQ(lmp->screen, stdout);
    EXPECT_NE(lmp->logfile, nullptr);
    EXPECT_NE(lmp->citeme, nullptr);
    ::testing::internal::CaptureStdout();
    lammps_close(handle);
    output = ::testing::internal::GetCapturedStdout();
    EXPECT_STREQ(output.substr(0,16).c_str(), "Total wall time:");
}
+45 −0
Original line number Diff line number Diff line
// unit tests for checking and changing simulation properties through the library interface

#include "library.h"
#include "lammps.h"
#include <string>

#include "gtest/gtest.h"

const char *demo_input[] = {
      "region       box block 0 $x 0 2 0 2",
      "create_box 1 box",
      "create_atoms 1 single 1.0 1.0 ${zpos}" };
const char *cont_input[] = {
    "create_atoms 1 single &",
    "0.2 0.1 0.1" };

class LAMMPS_properties : public ::testing::Test
{
protected:
    void *lmp;
    LAMMPS_properties() {};
    ~LAMMPS_properties() override {};

    void SetUp() override {
        const char *args[] = {"LAMMPS_test", "-log", "none",
                              "-echo", "screen", "-nocite" };
        char **argv = (char **)args;
        int argc = sizeof(args)/sizeof(char *);

        ::testing::internal::CaptureStdout();
        lmp = lammps_open_no_mpi(argc, argv, NULL);
        std::string output = ::testing::internal::GetCapturedStdout();
        EXPECT_STREQ(output.substr(0,8).c_str(), "LAMMPS (");
    }
    void TearDown() override {
        ::testing::internal::CaptureStdout();
        lammps_close(lmp);
        std::string output = ::testing::internal::GetCapturedStdout();
        EXPECT_STREQ(output.substr(0,16).c_str(), "Total wall time:");
        lmp = nullptr;
    }
};

TEST_F(LAMMPS_properties, box) {
};
Loading