Files
lammps/unittest/commands/test_labelmap.cpp
Axel Kohlmeyer ad8a931fe4 revise/refacor Type Label section parsing and Atoms section parsing
- do not ignore numeric type in Type Label sections
- refuse invalid numeric types that will overflow arrays
- check for duplicate numeric type entries or non-unique labels
- better error messages
- use Tokenizer class instead of sscanf()
2022-09-04 01:01:36 -04:00

166 lines
6.4 KiB
C++

/* ----------------------------------------------------------------------
LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
https://www.lammps.org/, Sandia National Laboratories
Steve Plimpton, sjplimp@sandia.gov
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 "lammps.h"
#include "atom.h"
#include "compute.h"
#include "domain.h"
#include "label_map.h"
#include "math_const.h"
#include "modify.h"
#include "../testing/core.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#define STRINGIFY(val) XSTR(val)
#define XSTR(val) #val
// whether to print verbose output (i.e. not capturing LAMMPS screen output).
bool verbose = false;
using ::testing::ContainsRegex;
using ::testing::ExitedWithCode;
using ::testing::StrEq;
namespace LAMMPS_NS {
class SetTest : public LAMMPSTest {
protected:
Atom *atom;
Domain *domain;
void SetUp() override
{
testbinary = "SetTest";
args = {"-log", "none", "-echo", "screen", "-nocite", "-v", "num", "1"};
LAMMPSTest::SetUp();
atom = lmp->atom;
domain = lmp->domain;
}
void TearDown() override { LAMMPSTest::TearDown(); }
};
TEST_F(SetTest, NoBoxAtoms)
{
ASSERT_EQ(atom->natoms, 0);
ASSERT_EQ(domain->box_exist, 0);
ASSERT_EQ(atom->lmap, nullptr);
TEST_FAILURE(".*ERROR: Labelmap command before simulation box is.*",
command("labelmap atom 3 C1"););
BEGIN_HIDE_OUTPUT();
command("region box block 0 2 0 2 0 2");
command("create_box 4 box");
command("labelmap atom 2 N1");
command("labelmap atom 3 O1 4 H1");
END_HIDE_OUTPUT();
ASSERT_NE(atom->lmap, nullptr);
ASSERT_FALSE(atom->lmap->is_complete(Atom::ATOM));
BEGIN_HIDE_OUTPUT();
command("labelmap atom 1 C1 2 N2 3 ' O#' 1 C1 4 H# 2 N3"); // second '#' starts comment
END_HIDE_OUTPUT();
ASSERT_TRUE(atom->lmap->is_complete(Atom::ATOM));
ASSERT_EQ(atom->lmap->find("C1", Atom::ATOM), 1);
ASSERT_EQ(atom->lmap->find("N2", Atom::ATOM), 2);
ASSERT_EQ(atom->lmap->find("O#", Atom::ATOM), 3);
ASSERT_EQ(atom->lmap->find("H", Atom::ATOM), 4);
TEST_FAILURE(".*ERROR: Labelmap atom type 0 must be within 1-4.*",
command("labelmap atom 0 C1"););
TEST_FAILURE(".*ERROR: Labelmap atom type 5 must be within 1-4.*",
command("labelmap atom 5 C1"););
TEST_FAILURE(".*ERROR: Type label string 1C for atom type 1 is invalid.*",
command("labelmap atom 1 1C"););
TEST_FAILURE(".*ERROR: Type label string #C for atom type 1 is invalid.*",
command("labelmap atom 1 '#C'"););
TEST_FAILURE(".*ERROR: Type label string CA CB for atom type 1 is invalid.*",
command("labelmap atom 1 ' CA CB '"););
TEST_FAILURE(".*ERROR: Type label string \\*C for atom type 1 is invalid.*",
command("labelmap atom 1 *C"););
TEST_FAILURE(".*ERROR: The atom type label N2 is already in use for type 2.*",
command("labelmap atom 1 N2"););
TEST_FAILURE(".*ERROR: No bond types allowed with current box settings.*",
command("labelmap bond 1 C1-C1"););
TEST_FAILURE(".*ERROR: No angle types allowed with current box settings.*",
command("labelmap angle 1 C1-C1-C1"););
TEST_FAILURE(".*ERROR: No dihedral types allowed with current box settings.*",
command("labelmap dihedral 1 C1-C1-C1-C1"););
TEST_FAILURE(".*ERROR: No improper types allowed with current box settings.*",
command("labelmap improper 1 C1-C1-C1-C1"););
TEST_FAILURE(".*ERROR: Incorrect number of arguments for labelmap command.*",
command("labelmap atom 1 C1 2"););
TEST_FAILURE(".*ERROR: Incorrect number of arguments for labelmap command.*",
command("labelmap atom 1 C1 atom 2 C2"););
TEST_FAILURE(".*ERROR: Incorrect number of arguments for labelmap clear command.*",
command("labelmap clear atom"););
TEST_FAILURE(".*ERROR: Incorrect number of arguments for labelmap clear command.*",
command("labelmap clear atom bond"););
TEST_FAILURE(".*ERROR: Incorrect number of arguments for labelmap write command.*",
command("labelmap write"););
TEST_FAILURE(".*ERROR: Incorrect number of arguments for labelmap write command.*",
command("labelmap write filename xxx"););
TEST_FAILURE(".*ERROR: Illegal labelmap atom command: missing argument.*",
command("labelmap atom 1"););
TEST_FAILURE(".*ERROR: Illegal labelmap atom command: missing argument.*",
command("labelmap atom"););
BEGIN_HIDE_OUTPUT();
command("labelmap clear");
command("labelmap atom 3 C1 2 N2");
END_HIDE_OUTPUT();
ASSERT_FALSE(atom->lmap->is_complete(Atom::ATOM));
ASSERT_EQ(atom->lmap->find("C1", Atom::ATOM), 3);
ASSERT_EQ(atom->lmap->find("N2", Atom::ATOM), 2);
BEGIN_HIDE_OUTPUT();
command("labelmap clear");
command("labelmap atom 1 \"C1'\" 2 'C2\"' 3 \"\"\"C1'-C2\" \"\"\" 4 \"\"\" C2\"-C1'\"\"\"");
END_HIDE_OUTPUT();
ASSERT_TRUE(atom->lmap->is_complete(Atom::ATOM));
ASSERT_EQ(atom->lmap->find("C1'", Atom::ATOM), 1);
ASSERT_EQ(atom->lmap->find("C2\"", Atom::ATOM), 2);
ASSERT_EQ(atom->lmap->find("C1'-C2\"", Atom::ATOM), 3);
ASSERT_EQ(atom->lmap->find("C2\"-C1'", Atom::ATOM), 4);
}
} // namespace LAMMPS_NS
int main(int argc, char **argv)
{
MPI_Init(&argc, &argv);
::testing::InitGoogleMock(&argc, argv);
if (LAMMPS_NS::platform::mpi_vendor() == "Open MPI" && !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 = LAMMPS_NS::utils::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;
}