File: test_input_class.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 (109 lines) | stat: -rw-r--r-- 3,086 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
108
109
// unit tests for issuing command to a LAMMPS instance through the Input class

#include "atom.h"
#include "input.h"
#include "lammps.h"
#include "memory.h"
#include <cstring>
#include <mpi.h>
#include <string>

#include "../testing/utils.h"
#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"};

namespace LAMMPS_NS {

class Input_commands : public ::testing::Test {
protected:
    LAMMPS *lmp;
    Input_commands()
    {
        const char *args[] = {"LAMMPS_test", nullptr};
        char **argv        = (char **)args;
        int argc           = 1;

        int flag;
        MPI_Initialized(&flag);
        if (!flag) MPI_Init(&argc, &argv);
    }
    ~Input_commands() override = default;

    void SetUp() override
    {
        LAMMPS::argv args = {"LAMMPS_test", "-log", "none", "-echo", "screen", "-nocite",
                             "-var",        "zpos", "1.5",  "-var",  "x",      "2"};

        ::testing::internal::CaptureStdout();
        lmp                = new LAMMPS(args, MPI_COMM_WORLD);
        std::string output = ::testing::internal::GetCapturedStdout();
        EXPECT_STREQ(output.substr(0, 8).c_str(), "LAMMPS (");
    }
    void TearDown() override
    {
        ::testing::internal::CaptureStdout();
        delete lmp;
        std::string output = ::testing::internal::GetCapturedStdout();
        EXPECT_STREQ(output.substr(0, 16).c_str(), "Total wall time:");
        lmp = nullptr;
    }
};

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

    fp = fopen(demo_file, "w");
    for (auto &inp : demo_input) {
        fputs(inp, fp);
        fputc('\n', fp);
    }
    fclose(fp);
    fp = fopen(cont_file, "w");
    for (auto &inp : cont_input) {
        fputs(inp, fp);
        fputc('\n', fp);
    }
    fclose(fp);

    EXPECT_EQ(lmp->atom->natoms, 0);
    lmp->input->file(demo_file);
    lmp->input->file(cont_file);
    EXPECT_EQ(lmp->atom->natoms, 2);

    platform::unlink(demo_file);
    platform::unlink(cont_file);
};

TEST_F(Input_commands, from_line)
{
    EXPECT_EQ(lmp->atom->natoms, 0);
    for (auto &inp : demo_input) {
        lmp->input->one(inp);
    }
    EXPECT_EQ(lmp->atom->natoms, 1);
};

TEST_F(Input_commands, substitute)
{
    char *string, *scratch;
    int nstring = 100, nscratch = 100;

    lmp->memory->create(string, nstring, "test:string");
    lmp->memory->create(scratch, nscratch, "test:scratch");
    strcpy(string, demo_input[0]);
    lmp->input->substitute(string, scratch, nstring, nscratch, 0);
    EXPECT_STREQ(string, "region       box block 0 2 0 2 0 2");

    strcpy(string, demo_input[2]);
    lmp->input->substitute(string, scratch, nstring, nscratch, 0);
    EXPECT_STREQ(string, "create_atoms 1 single 1.0 1.0 1.5");
    lmp->memory->destroy(string);
    lmp->memory->destroy(scratch);
};
} // namespace LAMMPS_NS