add c/python unit tests for lammps_error()

This commit is contained in:
Axel Kohlmeyer
2022-09-23 18:59:29 -04:00
parent 86d1aacf7e
commit a94cfe175b
2 changed files with 41 additions and 2 deletions

View File

@ -198,3 +198,32 @@ TEST(lammps_open_fortran, no_args)
if (verbose) std::cout << output; if (verbose) std::cout << output;
MPI_Comm_free(&mycomm); MPI_Comm_free(&mycomm);
} }
TEST(lammps_open_no_mpi, lammps_error)
{
const char *args[] = {"liblammps", "-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_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_NE(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_error(handle, 0, "test_warning");
output = ::testing::internal::GetCapturedStdout();
EXPECT_THAT(output, HasSubstr("WARNING: test_warning"));
}

View File

@ -45,11 +45,21 @@ class PythonOpen(unittest.TestCase):
def testWithArgs(self): def testWithArgs(self):
"""Create LAMMPS instance with a few arguments""" """Create LAMMPS instance with a few arguments"""
lmp=lammps(name=self.machine, lmp=lammps(name=self.machine,cmdargs=['-nocite','-sf','opt','-log','none'])
cmdargs=['-nocite','-sf','opt','-log','none'])
self.assertIsNot(lmp.lmp,None) self.assertIsNot(lmp.lmp,None)
self.assertEqual(lmp.opened,1) self.assertEqual(lmp.opened,1)
def testError(self):
"""Print warning message through LAMMPS Error class"""
lmp=lammps(name=self.machine,cmdargs=['-nocite','-log','none','-screen','tmp.error.output'])
lmp.error(0,'test_warning')
lmp.close()
with open('tmp.error.output','r') as f:
output = f.read()
self.assertTrue('LAMMPS' in output)
self.assertTrue('Total wall time' in output)
self.assertTrue('WARNING: test_warning' in output)
def testContextManager(self): def testContextManager(self):
"""Automatically clean up LAMMPS instance""" """Automatically clean up LAMMPS instance"""
with lammps(name=self.machine) as lmp: with lammps(name=self.machine) as lmp: