File: wrap_create.cpp

package info (click to toggle)
lammps 20250204%2Bdfsg.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 474,368 kB
  • sloc: cpp: 1,060,070; python: 27,785; ansic: 8,956; f90: 7,254; sh: 6,044; perl: 4,171; fortran: 2,442; xml: 1,714; makefile: 1,352; objc: 238; lisp: 188; yacc: 58; csh: 16; awk: 14; tcl: 6; javascript: 2
file content (107 lines) | stat: -rw-r--r-- 3,520 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// unit tests for the LAMMPS base class

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

#include "gtest/gtest.h"

// prototypes for fortran reverse wrapper functions
extern "C" {
void *f_lammps_open_no_args();
void *f_lammps_open_with_args();
void *f_lammps_no_mpi_no_args();
void *f_lammps_no_mpi_with_args();
void f_lammps_close();
int f_lammps_get_comm();
}

// C wrapper to split MPI communicator w/o requiring a Fortran MPI lib
extern "C" int create_mpi_comm_split(int color, int key)
{
    MPI_Comm c_newcomm = MPI_COMM_NULL;
    MPI_Comm_split(MPI_COMM_WORLD, color, key, &c_newcomm);
    return MPI_Comm_c2f(c_newcomm);
}

TEST(open_no_mpi, no_args)
{
    ::testing::internal::CaptureStdout();
    int mpi_init = 0;
    MPI_Initialized(&mpi_init);
    EXPECT_EQ(mpi_init, 1);
    void *handle       = f_lammps_no_mpi_no_args();
    std::string output = ::testing::internal::GetCapturedStdout();
    EXPECT_STREQ(output.substr(0, 6).c_str(), "LAMMPS");
    auto *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();
    f_lammps_close();
    output = ::testing::internal::GetCapturedStdout();
    EXPECT_STREQ(output.substr(0, 16).c_str(), "Total wall time:");
}

TEST(open_no_mpi, with_args)
{
    ::testing::internal::CaptureStdout();
    void *handle       = f_lammps_no_mpi_with_args();
    std::string output = ::testing::internal::GetCapturedStdout();
    EXPECT_STREQ(output.substr(0, 6).c_str(), "LAMMPS");
    auto *lmp = (LAMMPS_NS::LAMMPS *)handle;
    EXPECT_EQ(lmp->infile, stdin);
    EXPECT_EQ(lmp->screen, stdout);
    EXPECT_EQ(lmp->logfile, nullptr);
    EXPECT_EQ(lmp->citeme, nullptr);
    EXPECT_EQ(lmp->world, MPI_COMM_WORLD);

    ::testing::internal::CaptureStdout();
    f_lammps_close();
    output = ::testing::internal::GetCapturedStdout();
    EXPECT_STREQ(output.substr(0, 16).c_str(), "Total wall time:");
}

TEST(fortran_open, no_args)
{
    ::testing::internal::CaptureStdout();
    void *handle       = f_lammps_open_no_args();
    std::string output = ::testing::internal::GetCapturedStdout();
    EXPECT_STREQ(output.substr(0, 6).c_str(), "LAMMPS");
    auto *lmp = (LAMMPS_NS::LAMMPS *)handle;

    int f_comm  = f_lammps_get_comm();
    auto mycomm = MPI_Comm_f2c(f_comm);
    EXPECT_EQ(lmp->world, mycomm);
    EXPECT_EQ(lmp->infile, stdin);
    EXPECT_EQ(lmp->screen, stdout);
    EXPECT_NE(lmp->citeme, nullptr);
    ::testing::internal::CaptureStdout();
    f_lammps_close();
    output = ::testing::internal::GetCapturedStdout();
    EXPECT_STREQ(output.substr(0, 16).c_str(), "Total wall time:");
}

TEST(fortran_open, with_args)
{
    ::testing::internal::CaptureStdout();
    void *handle       = f_lammps_open_with_args();
    std::string output = ::testing::internal::GetCapturedStdout();
    EXPECT_STREQ(output.substr(0, 6).c_str(), "LAMMPS");
    auto *lmp = (LAMMPS_NS::LAMMPS *)handle;

    int f_comm  = f_lammps_get_comm();
    auto mycomm = MPI_Comm_f2c(f_comm);
    EXPECT_EQ(lmp->world, mycomm);
    EXPECT_EQ(lmp->infile, stdin);
    EXPECT_EQ(lmp->screen, stdout);
    EXPECT_EQ(lmp->logfile, nullptr);
    EXPECT_EQ(lmp->citeme, nullptr);

    ::testing::internal::CaptureStdout();
    f_lammps_close();
    output = ::testing::internal::GetCapturedStdout();
    EXPECT_STREQ(output.substr(0, 16).c_str(), "Total wall time:");
}