File: test_error_class.cpp

package info (click to toggle)
lammps 20220106.git7586adbb6a%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 348,064 kB
  • sloc: cpp: 831,421; python: 24,896; xml: 14,949; f90: 10,845; ansic: 7,967; sh: 4,226; perl: 4,064; fortran: 2,424; makefile: 1,501; objc: 238; lisp: 163; csh: 16; awk: 14; tcl: 6
file content (150 lines) | stat: -rw-r--r-- 3,953 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// unit tests for issuing command to a LAMMPS instance through the Input class

#include "error.h"
#include "info.h"
#include "lammps.h"
#include "output.h"
#include "thermo.h"

#include "../testing/core.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

#include <string>

// whether to print verbose output (i.e. not capturing LAMMPS screen output).
bool verbose = false;

namespace LAMMPS_NS {

using ::testing::MatchesRegex;
using utils::split_words;

class Error_class : public LAMMPSTest {
protected:
    Error *error;
    Thermo *thermo;

    void SetUp() override
    {
        testbinary = "ErrorClass";
        LAMMPSTest::SetUp();
        error  = lmp->error;
        thermo = lmp->output->thermo;
    }
};

TEST_F(Error_class, message)
{
    auto output = CAPTURE_OUTPUT([&] {
        error->message(FLERR, "one message");
    });
    ASSERT_THAT(output, MatchesRegex("one message .*test_error_class.cpp:.*"));
};

TEST_F(Error_class, warning)
{
    // standard warning
    auto output = CAPTURE_OUTPUT([&] {
        error->warning(FLERR, "one warning");
    });
    ASSERT_THAT(output, MatchesRegex("WARNING: one warning .*test_error_class.cpp:.*"));
    ASSERT_THAT(error->get_maxwarn(), 100);

    // warnings disabled
    HIDE_OUTPUT([&] {
        command("thermo_modify warn ignore");
    });
    output = CAPTURE_OUTPUT([&] {
        error->warning(FLERR, "one warning");
    });
    ASSERT_THAT(error->get_maxwarn(), -1);

    BEGIN_HIDE_OUTPUT();
    command("thermo_modify warn 2");
    error->warning(FLERR, "one warning");
    error->warning(FLERR, "one warning");
    error->warning(FLERR, "one warning");
    END_HIDE_OUTPUT();
    ASSERT_THAT(error->get_maxwarn(), 2);
    ASSERT_THAT(error->get_numwarn(), 5);

    output = CAPTURE_OUTPUT([&] {
        thermo->lost_check();
    });
    ASSERT_THAT(output, MatchesRegex("WARNING: Too many warnings: 5 vs 2. All future.*"));

    output = CAPTURE_OUTPUT([&] {
        error->warning(FLERR, "one warning");
    });

    ASSERT_EQ(output, "");

    BEGIN_HIDE_OUTPUT();
    command("thermo_modify warn reset");
    thermo->lost_check();
    error->warning(FLERR, "one warning");
    END_HIDE_OUTPUT();
    ASSERT_THAT(error->get_maxwarn(), 2);
    ASSERT_THAT(error->get_numwarn(), 1);

    output = CAPTURE_OUTPUT([&] {
        error->warning(FLERR, "one warning");
    });
    ASSERT_THAT(output, MatchesRegex("WARNING: one warning.*"));

    BEGIN_HIDE_OUTPUT();
    command("thermo_modify warn default");
    thermo->lost_check();
    error->warning(FLERR, "one warning");
    END_HIDE_OUTPUT();
    ASSERT_THAT(error->get_maxwarn(), 100);
    ASSERT_THAT(error->get_numwarn(), 1);

    BEGIN_HIDE_OUTPUT();
    command("thermo_modify warn always");
    thermo->lost_check();
    error->warning(FLERR, "one warning");
    END_HIDE_OUTPUT();
    ASSERT_THAT(error->get_maxwarn(), 0);
    ASSERT_THAT(error->get_numwarn(), 2);
};

TEST_F(Error_class, one)
{
    TEST_FAILURE("ERROR on proc 0: one error.*test_error_class.cpp:.*",
                 error->one(FLERR, "one error"););
};

TEST_F(Error_class, all)
{
    TEST_FAILURE("ERROR: one error.*test_error_class.cpp:.*", error->all(FLERR, "one error"););
};

} // namespace LAMMPS_NS

int main(int argc, char **argv)
{
    MPI_Init(&argc, &argv);
    ::testing::InitGoogleMock(&argc, argv);

    if (Info::get_mpi_vendor() == "Open MPI" && !LAMMPS_NS::Info::has_exceptions())
        std::cout << "Warning: using OpenMPI without exceptions. "
                     "Death tests will be skipped\n";

    // handle arguments passed via environment variable
    if (const char *var = getenv("TEST_ARGS")) {
        std::vector<std::string> env = split_words(var);
        for (auto arg : env) {
            if (arg == "-v") {
                verbose = true;
            }
        }
    }

    if ((argc > 1) && (strcmp(argv[1], "-v") == 0)) verbose = true;

    int rv = RUN_ALL_TESTS();
    MPI_Finalize();
    return rv;
}