diff --git a/source/source_cell/read_atom_species.cpp b/source/source_cell/read_atom_species.cpp index 538d57ec2a..abc81d380e 100644 --- a/source/source_cell/read_atom_species.cpp +++ b/source/source_cell/read_atom_species.cpp @@ -1,11 +1,35 @@ #include "read_stru.h" +#include #include #include "source_base/tool_title.h" namespace unitcell { +namespace +{ +// SCAN_LINE_BEGIN matches the keyword as any token in a line, including +// tokens after '#'; a lattice section header must be the first token of its +// line, otherwise a comment mention of the keyword is taken as the section. +bool scan_lattice_section(std::ifstream& ifs, const std::string& name) +{ + ifs.clear(); + ifs.seekg(0); + std::string line; + while (std::getline(ifs, line)) + { + std::istringstream iss(line); + std::string first_token; + if (iss >> first_token && first_token == name) + { + return true; + } + } + return false; +} +} // namespace + bool read_atom_species(std::ifstream& ifa, std::ofstream& ofs_running, UnitCell& ucell, @@ -165,10 +189,7 @@ bool read_lattice_constant(std::ifstream& ifa, if(latName=="user_defined_lattice") { // check the existence of keyword "LATTICE_PARAMETERS" - if (ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifa, - "LATTICE_PARAMETERS", - true, - false)) + if (scan_lattice_section(ifa, "LATTICE_PARAMETERS")) { ModuleBase::WARNING_QUIT("unitcell::read_lattice_constant", "do not use LATTICE_PARAMETERS without explicit specification of lattice type"); @@ -233,19 +254,33 @@ bool read_lattice_constant(std::ifstream& ifa, latvec.e11 = 1.0; latvec.e12 = 0.0; latvec.e13 = 0.0; latvec.e21 =-0.5; latvec.e22 = e22; latvec.e23 = 0.0; latvec.e31 = 0.0; latvec.e32 = 0.0; latvec.e33 = 0.0; - if( ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifa, "LATTICE_PARAMETERS") ) + if( scan_lattice_section(ifa, "LATTICE_PARAMETERS") ) { - ModuleBase::GlobalFunc::READ_VALUE(ifa, latvec.e33); + ifa >> latvec.e33; + if (ifa.fail()) + { + ModuleBase::WARNING_QUIT("unitcell::read_lattice_constant", + "invalid LATTICE_PARAMETERS for lattice type hexagonal"); + } } } else if(latName=="trigonal") {//trigonal, ibrav = 5 double t1 = 0.0; double t2 = 0.0; - if( ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifa, "LATTICE_PARAMETERS") ) + if( scan_lattice_section(ifa, "LATTICE_PARAMETERS") ) { double cosab=0.0; - ModuleBase::GlobalFunc::READ_VALUE(ifa, cosab); + // READ_VALUE would hide an extraction failure here: a failed + // read leaves cosab = 0, silently building the simple-cubic + // (alpha = 90 degree) special case instead of the intended + // trigonal cell. + ifa >> cosab; + if (ifa.fail()) + { + ModuleBase::WARNING_QUIT("unitcell::read_lattice_constant", + "invalid cos(alpha) in LATTICE_PARAMETERS for lattice type trigonal"); + } t1 = sqrt(1.0 + 2.0*cosab); t2 = sqrt(1.0 - cosab); } @@ -262,17 +297,27 @@ bool read_lattice_constant(std::ifstream& ifa, latvec.e11 = 1.0; latvec.e12 = 0.0; latvec.e13 = 0.0; latvec.e21 = 0.0; latvec.e22 = 1.0; latvec.e23 = 0.0; latvec.e31 = 0.0; latvec.e32 = 0.0; latvec.e33 = 0.0; - if( ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifa, "LATTICE_PARAMETERS") ) + if( scan_lattice_section(ifa, "LATTICE_PARAMETERS") ) { - ModuleBase::GlobalFunc::READ_VALUE(ifa, latvec.e33); + ifa >> latvec.e33; + if (ifa.fail()) + { + ModuleBase::WARNING_QUIT("unitcell::read_lattice_constant", + "invalid LATTICE_PARAMETERS for lattice type st"); + } } } else if(latName=="bct") {//body-centered tetragonal, ibrav = 7 double cba = 0.0; - if( ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifa, "LATTICE_PARAMETERS") ) + if( scan_lattice_section(ifa, "LATTICE_PARAMETERS") ) { - ModuleBase::GlobalFunc::READ_VALUE(ifa, cba); + ifa >> cba; + if (ifa.fail()) + { + ModuleBase::WARNING_QUIT("unitcell::read_lattice_constant", + "invalid LATTICE_PARAMETERS for lattice type bct"); + } cba = cba / 2.0; } latvec.e11 = 0.5; latvec.e12 =-0.5; latvec.e13 = cba; @@ -284,10 +329,14 @@ bool read_lattice_constant(std::ifstream& ifa, latvec.e11 = 1.0; latvec.e12 = 0.0; latvec.e13 = 0.0; latvec.e21 = 0.0; latvec.e22 = 0.0; latvec.e23 = 0.0; latvec.e31 = 0.0; latvec.e32 = 0.0; latvec.e33 = 0.0; - if( ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifa, "LATTICE_PARAMETERS") ) + if( scan_lattice_section(ifa, "LATTICE_PARAMETERS") ) { - ifa >> latvec.e22; - ModuleBase::GlobalFunc::READ_VALUE(ifa, latvec.e33); + ifa >> latvec.e22 >> latvec.e33; + if (ifa.fail()) + { + ModuleBase::WARNING_QUIT("unitcell::read_lattice_constant", + "invalid LATTICE_PARAMETERS for lattice type so"); + } } } else if(latName=="baco") @@ -295,21 +344,29 @@ bool read_lattice_constant(std::ifstream& ifa, latvec.e11 = 0.5; latvec.e12 = 0.0; latvec.e13 = 0.0; latvec.e21 =-0.5; latvec.e22 = 0.0; latvec.e23 = 0.0; latvec.e31 = 0.0; latvec.e32 = 0.0; latvec.e33 = 0.0; - if( ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifa, "LATTICE_PARAMETERS") ) + if( scan_lattice_section(ifa, "LATTICE_PARAMETERS") ) { - ifa >> latvec.e12; + ifa >> latvec.e12 >> latvec.e33; + if (ifa.fail()) + { + ModuleBase::WARNING_QUIT("unitcell::read_lattice_constant", + "invalid LATTICE_PARAMETERS for lattice type baco"); + } latvec.e12 = latvec.e12 / 2.0; latvec.e22 = latvec.e12; - ModuleBase::GlobalFunc::READ_VALUE(ifa, latvec.e33); } } else if(latName=="fco") {//face-centered orthorhombic, ibrav = 10 double bba = 0.0; double cba = 0.0; - if( ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifa, "LATTICE_PARAMETERS") ) + if( scan_lattice_section(ifa, "LATTICE_PARAMETERS") ) { - ifa >> bba; - ModuleBase::GlobalFunc::READ_VALUE(ifa, cba); + ifa >> bba >> cba; + if (ifa.fail()) + { + ModuleBase::WARNING_QUIT("unitcell::read_lattice_constant", + "invalid LATTICE_PARAMETERS for lattice type fco"); + } bba = bba / 2.0; cba = cba / 2.0; } latvec.e11 = 0.5; latvec.e12 = 0.0; latvec.e13 = cba; @@ -319,10 +376,14 @@ bool read_lattice_constant(std::ifstream& ifa, else if(latName=="bco") {//body-centered orthorhombic, ibrav = 11 double bba = 0.0; double cba = 0.0; - if( ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifa, "LATTICE_PARAMETERS") ) + if( scan_lattice_section(ifa, "LATTICE_PARAMETERS") ) { - ifa >> bba; - ModuleBase::GlobalFunc::READ_VALUE(ifa, cba); + ifa >> bba >> cba; + if (ifa.fail()) + { + ModuleBase::WARNING_QUIT("unitcell::read_lattice_constant", + "invalid LATTICE_PARAMETERS for lattice type bco"); + } bba = bba / 2.0; cba = cba / 2.0; } latvec.e11 = 0.5; latvec.e12 = bba; latvec.e13 = cba; @@ -334,10 +395,14 @@ bool read_lattice_constant(std::ifstream& ifa, double bba = 0.0; double cba = 0.0; double cosab = 0.0; double e21 = 0.0; double e22 = 0.0; - if( ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifa, "LATTICE_PARAMETERS") ) + if( scan_lattice_section(ifa, "LATTICE_PARAMETERS") ) { - ifa >> bba >> cba; - ModuleBase::GlobalFunc::READ_VALUE(ifa, cosab); + ifa >> bba >> cba >> cosab; + if (ifa.fail()) + { + ModuleBase::WARNING_QUIT("unitcell::read_lattice_constant", + "invalid LATTICE_PARAMETERS for lattice type sm"); + } e21 = bba * cosab; e22 = bba * sqrt(1.0-cosab*cosab); } @@ -350,10 +415,14 @@ bool read_lattice_constant(std::ifstream& ifa, double bba = 0.0; double cba = 0.0; double cosab = 0.0; double e21 = 0.0; double e22 = 0.0; - if( ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifa, "LATTICE_PARAMETERS") ) + if( scan_lattice_section(ifa, "LATTICE_PARAMETERS") ) { - ifa >> bba >> cba; - ModuleBase::GlobalFunc::READ_VALUE(ifa, cosab); + ifa >> bba >> cba >> cosab; + if (ifa.fail()) + { + ModuleBase::WARNING_QUIT("unitcell::read_lattice_constant", + "invalid LATTICE_PARAMETERS for lattice type bacm"); + } e21 = bba * cosab; e22 = bba * sqrt(1.0-cosab*cosab); cba = cba / 2.0; @@ -371,10 +440,14 @@ bool read_lattice_constant(std::ifstream& ifa, double cosbc = 0.0; double sinab = 0.0; double term = 0.0; - if( ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifa, "LATTICE_PARAMETERS") ) + if( scan_lattice_section(ifa, "LATTICE_PARAMETERS") ) { - ifa >> bba >> cba >> cosab >> cosac; - ModuleBase::GlobalFunc::READ_VALUE(ifa, cosbc); + ifa >> bba >> cba >> cosab >> cosac >> cosbc; + if (ifa.fail()) + { + ModuleBase::WARNING_QUIT("unitcell::read_lattice_constant", + "invalid LATTICE_PARAMETERS for lattice type triclinic"); + } sinab = sqrt(1.0-cosab*cosab); } latvec.e11 = 1.0; latvec.e12 = 0.0; latvec.e13 = 0.0; @@ -385,9 +458,31 @@ bool read_lattice_constant(std::ifstream& ifa, latvec.e33 = cba * term; } else - { + { ModuleBase::WARNING_QUIT("unitcell::read_lattice_constant","latname not supported!"); } + // Non-cubic named lattices cannot be constructed without the shape + // parameters in LATTICE_PARAMETERS; without them the shape variables + // stay zero and produce a zero-volume or NaN cell. The check comes + // after the branches so an unsupported latName reports itself first. + if(latName != "sc" && latName != "fcc" && latName != "bcc") + { + const bool has_params = scan_lattice_section(ifa, "LATTICE_PARAMETERS"); + if (!has_params) + { + const std::string msg = "LATTICE_PARAMETERS is required for lattice type " + latName; + ModuleBase::WARNING_QUIT("unitcell::read_lattice_constant", msg); + } + } + } + + // Reject degenerate (zero-volume) or non-finite lattice vectors, whether + // they came from LATTICE_VECTORS or from a named lattice. + const double latvec_det = latvec.Det(); + if (!std::isfinite(latvec_det) || latvec_det == 0.0) + { + ModuleBase::WARNING_QUIT("unitcell::read_lattice_constant", + "the lattice vectors form a zero-volume or non-finite cell"); } // lattice vectors in another form. diff --git a/source/source_cell/unittests/test_read_atom_species.cpp b/source/source_cell/unittests/test_read_atom_species.cpp index b204ff3a3b..e0eb23ec81 100644 --- a/source/source_cell/unittests/test_read_atom_species.cpp +++ b/source/source_cell/unittests/test_read_atom_species.cpp @@ -8,15 +8,50 @@ #include #include +#include "source_cell/basecell.h" #include "source_cell/read_stru.h" #include "source_cell/unitcell.h" /************************************************ - * unit test of read_atom_species.cpp malformed-input handling + * unit test of read_atom_species.cpp: + * named-lattice validation and malformed-input handling ***********************************************/ /** * - Tested Functions: + * - HexagonalMissingParamsRejected / TrigonalMissingParamsRejected + * - read_lattice_constant(): non-cubic named lattices require + * LATTICE_PARAMETERS; without it the shape variables stayed zero and + * produced a zero-volume or NaN cell (issue #8354) + * - CommentMentionDoesNotSatisfyParamsRequirement + * - a "# LATTICE_PARAMETERS" comment token is not a parameter section; + * SCAN_LINE_BEGIN matches any token in a line, so the section lookup + * must require the keyword to begin a line + * - CommentMentionDoesNotShadowRealParams + * - with a real LATTICE_PARAMETERS section present, the value must come + * from that section, not from the token after a comment mention + * - TrigonalMalformedParamRejected + * - a failed cos(alpha) extraction must not silently become the + * simple-cubic (alpha = 90 degree) special case + * - TrigonalImpossibleAngleRejected + * - cos(alpha) = 1.1 parses fine but yields NaN lattice vectors, which + * the finiteness check must reject + * - SmPartiallyMalformedParamsRejected / BacmPartiallyMalformedParamsRejected + * / TriclinicPartiallyMalformedParamsRejected + * - a partially malformed multi-value LATTICE_PARAMETERS line (e.g. + * "2 3 bad") leaves a finite nonzero-volume cell, so the determinant + * check alone is not enough for these branches + * - ValidHexagonalWithParams / ValidSmWithParams + * - a hexagonal lattice with c/a given still reads normally + * - SoRangeOverflowRejected / StRangeOverflowRejected + * - an out-of-range value such as 1e999 sets failbit but stores + * DBL_MAX, leaving a finite nonzero-volume cell that the determinant + * check alone would accept; every LATTICE_PARAMETERS extraction is + * therefore validated directly + * - CubicScNeedsNoParams + * - cubic lattice names do not need LATTICE_PARAMETERS + * - DegenerateLatticeVectorsRejected + * - explicit LATTICE_VECTORS forming a zero-volume cell are rejected * - SpeciesMissingMassRejected * - read_atom_species(): an ATOMIC_SPECIES line without a mass left * mass = 0 silently (issue #8356); it must now quit with a message @@ -32,6 +67,276 @@ * common in real STRU files, must still be accepted */ +class ReadLatticeValidationTest : public ::testing::Test +{ + protected: + Lattice lat; + std::ofstream ofs_running; + std::string log_path; + std::vector stru_paths; + + void SetUp() override + { + log_path = testing::TempDir() + "read_lattice_validation.log"; + ofs_running.open(log_path); + } + + void TearDown() override + { + ofs_running.close(); + std::remove(log_path.c_str()); + for (const std::string& path : stru_paths) + { + std::remove(path.c_str()); + } + } + + std::string write_stru_file(const std::string& name, const std::string& content) + { + const std::string path = testing::TempDir() + name; + std::ofstream ofs(path); + ofs << content; + ofs.close(); + stru_paths.push_back(path); + ofs_running << "created test STRU file: " << path << std::endl; + return path; + } + + std::ifstream open_stru_file(const std::string& path) + { + const std::string msg = "reading test STRU file: " + path; + std::cout << msg << std::endl; + ofs_running << msg << std::endl; + return std::ifstream(path); + } +}; + +TEST_F(ReadLatticeValidationTest, HexagonalMissingParamsRejected) +{ + const std::string stru = write_stru_file("hex_no_param.stru", "LATTICE_CONSTANT\n4.0\n"); + lat.latName = "hexagonal"; + std::ifstream ifa = open_stru_file(stru); + testing::internal::CaptureStdout(); + EXPECT_EXIT(unitcell::read_lattice_constant(ifa, ofs_running, lat), ::testing::ExitedWithCode(1), ""); + const std::string output = testing::internal::GetCapturedStdout(); + EXPECT_THAT(output, testing::HasSubstr("LATTICE_PARAMETERS is required for lattice type hexagonal")); + ifa.close(); +} + +TEST_F(ReadLatticeValidationTest, TrigonalMissingParamsRejected) +{ + const std::string stru = write_stru_file("trigonal_no_param.stru", "LATTICE_CONSTANT\n4.0\n"); + lat.latName = "trigonal"; + std::ifstream ifa = open_stru_file(stru); + testing::internal::CaptureStdout(); + EXPECT_EXIT(unitcell::read_lattice_constant(ifa, ofs_running, lat), ::testing::ExitedWithCode(1), ""); + const std::string output = testing::internal::GetCapturedStdout(); + EXPECT_THAT(output, testing::HasSubstr("LATTICE_PARAMETERS is required for lattice type trigonal")); + ifa.close(); +} + +TEST_F(ReadLatticeValidationTest, CommentMentionDoesNotSatisfyParamsRequirement) +{ + // a comment token "# LATTICE_PARAMETERS" is not a parameter section: + // SCAN_LINE_BEGIN matches any token in a line, so the section lookup + // must require the keyword to begin a line + const std::string stru = + write_stru_file("hex_comment_only.stru", "LATTICE_CONSTANT # LATTICE_PARAMETERS\n4.0\n"); + lat.latName = "hexagonal"; + std::ifstream ifa = open_stru_file(stru); + testing::internal::CaptureStdout(); + EXPECT_EXIT(unitcell::read_lattice_constant(ifa, ofs_running, lat), ::testing::ExitedWithCode(1), ""); + const std::string output = testing::internal::GetCapturedStdout(); + EXPECT_THAT(output, testing::HasSubstr("LATTICE_PARAMETERS is required for lattice type hexagonal")); + ifa.close(); +} + +TEST_F(ReadLatticeValidationTest, CommentMentionDoesNotShadowRealParams) +{ + // with a real LATTICE_PARAMETERS section present, the value must come + // from that section, not from the token after the comment mention + const std::string stru = write_stru_file("hex_comment_plus_real.stru", + "LATTICE_CONSTANT # LATTICE_PARAMETERS\n4.0\n" + "LATTICE_PARAMETERS\n1.633\n"); + lat.latName = "hexagonal"; + std::ifstream ifa = open_stru_file(stru); + EXPECT_TRUE(unitcell::read_lattice_constant(ifa, ofs_running, lat)); + EXPECT_DOUBLE_EQ(lat.latvec.e33, 1.633); + EXPECT_GT(lat.latvec.Det(), 0.0); + ifa.close(); +} + +TEST_F(ReadLatticeValidationTest, TrigonalMalformedParamRejected) +{ + // a failed cos(alpha) read must not silently become the simple-cubic + // (alpha = 90 degree) special case + const std::string stru = + write_stru_file("trigonal_bad_param.stru", "LATTICE_CONSTANT\n4.0\nLATTICE_PARAMETERS\nabc\n"); + lat.latName = "trigonal"; + std::ifstream ifa = open_stru_file(stru); + testing::internal::CaptureStdout(); + EXPECT_EXIT(unitcell::read_lattice_constant(ifa, ofs_running, lat), ::testing::ExitedWithCode(1), ""); + const std::string output = testing::internal::GetCapturedStdout(); + EXPECT_THAT(output, testing::HasSubstr("invalid cos(alpha)")); + ifa.close(); +} + +TEST_F(ReadLatticeValidationTest, TrigonalImpossibleAngleRejected) +{ + // cos(alpha) = 1.1 parses fine but gives sqrt of a negative number, so + // the lattice vectors become NaN; the finiteness check must reject them + const std::string stru = + write_stru_file("trigonal_impossible.stru", "LATTICE_CONSTANT\n4.0\nLATTICE_PARAMETERS\n1.1\n"); + lat.latName = "trigonal"; + std::ifstream ifa = open_stru_file(stru); + testing::internal::CaptureStdout(); + EXPECT_EXIT(unitcell::read_lattice_constant(ifa, ofs_running, lat), ::testing::ExitedWithCode(1), ""); + const std::string output = testing::internal::GetCapturedStdout(); + EXPECT_THAT(output, testing::HasSubstr("zero-volume or non-finite cell")); + ifa.close(); +} + +TEST_F(ReadLatticeValidationTest, UnsupportedLatNameReportsItself) +{ + // an unsupported lattice name must report "latname not supported", not + // be asked for LATTICE_PARAMETERS: the presence guard runs after the + // name cascade. The legacy Warning5 fixture contains LATTICE_PARAMETERS, + // so it passes with either guard order and cannot pin this ordering. + const std::string stru = write_stru_file("arbitrary_no_param.stru", "LATTICE_CONSTANT\n4.0\n"); + lat.latName = "arbitrary"; + std::ifstream ifa = open_stru_file(stru); + testing::internal::CaptureStdout(); + EXPECT_EXIT(unitcell::read_lattice_constant(ifa, ofs_running, lat), ::testing::ExitedWithCode(1), ""); + const std::string output = testing::internal::GetCapturedStdout(); + EXPECT_THAT(output, testing::HasSubstr("latname not supported")); + ifa.close(); +} + +TEST_F(ReadLatticeValidationTest, ValidHexagonalWithParams) +{ + const std::string stru = + write_stru_file("hex_with_param.stru", "LATTICE_CONSTANT\n4.0\nLATTICE_PARAMETERS\n1.633\n"); + lat.latName = "hexagonal"; + std::ifstream ifa = open_stru_file(stru); + EXPECT_TRUE(unitcell::read_lattice_constant(ifa, ofs_running, lat)); + EXPECT_DOUBLE_EQ(lat.latvec.e33, 1.633); + EXPECT_GT(lat.latvec.Det(), 0.0); + ifa.close(); +} + +TEST_F(ReadLatticeValidationTest, SmPartiallyMalformedParamsRejected) +{ + // "2 3 bad": the first two values read fine and the failed cosine read + // leaves a finite diag(1,2,3)-like cell, which the determinant check + // alone would accept + const std::string stru = + write_stru_file("sm_partial_bad.stru", "LATTICE_CONSTANT\n4.0\nLATTICE_PARAMETERS\n2 3 bad\n"); + lat.latName = "sm"; + std::ifstream ifa = open_stru_file(stru); + testing::internal::CaptureStdout(); + EXPECT_EXIT(unitcell::read_lattice_constant(ifa, ofs_running, lat), ::testing::ExitedWithCode(1), ""); + const std::string output = testing::internal::GetCapturedStdout(); + EXPECT_THAT(output, testing::HasSubstr("invalid LATTICE_PARAMETERS for lattice type sm")); + ifa.close(); +} + +TEST_F(ReadLatticeValidationTest, BacmPartiallyMalformedParamsRejected) +{ + const std::string stru = + write_stru_file("bacm_partial_bad.stru", "LATTICE_CONSTANT\n4.0\nLATTICE_PARAMETERS\n2 3 bad\n"); + lat.latName = "bacm"; + std::ifstream ifa = open_stru_file(stru); + testing::internal::CaptureStdout(); + EXPECT_EXIT(unitcell::read_lattice_constant(ifa, ofs_running, lat), ::testing::ExitedWithCode(1), ""); + const std::string output = testing::internal::GetCapturedStdout(); + EXPECT_THAT(output, testing::HasSubstr("invalid LATTICE_PARAMETERS for lattice type bacm")); + ifa.close(); +} + +TEST_F(ReadLatticeValidationTest, TriclinicPartiallyMalformedParamsRejected) +{ + const std::string stru = write_stru_file("triclinic_partial_bad.stru", + "LATTICE_CONSTANT\n4.0\nLATTICE_PARAMETERS\n0.5 0.5 bad\n"); + lat.latName = "triclinic"; + std::ifstream ifa = open_stru_file(stru); + testing::internal::CaptureStdout(); + EXPECT_EXIT(unitcell::read_lattice_constant(ifa, ofs_running, lat), ::testing::ExitedWithCode(1), ""); + const std::string output = testing::internal::GetCapturedStdout(); + EXPECT_THAT(output, testing::HasSubstr("invalid LATTICE_PARAMETERS for lattice type triclinic")); + ifa.close(); +} + +TEST_F(ReadLatticeValidationTest, ValidSmWithParams) +{ + const std::string stru = + write_stru_file("sm_valid.stru", "LATTICE_CONSTANT\n4.0\nLATTICE_PARAMETERS\n2 3 0.5\n"); + lat.latName = "sm"; + std::ifstream ifa = open_stru_file(stru); + EXPECT_TRUE(unitcell::read_lattice_constant(ifa, ofs_running, lat)); + EXPECT_DOUBLE_EQ(lat.latvec.e21, 2.0 * 0.5); + EXPECT_DOUBLE_EQ(lat.latvec.e33, 3.0); + EXPECT_GT(lat.latvec.Det(), 0.0); + ifa.close(); +} + +TEST_F(ReadLatticeValidationTest, SoRangeOverflowRejected) +{ + // "1 1e999": the out-of-range read sets failbit but stores DBL_MAX, + // leaving det = DBL_MAX (finite and nonzero), so the determinant check + // alone would accept this malformed input + const std::string stru = + write_stru_file("so_range_overflow.stru", "LATTICE_CONSTANT\n4.0\nLATTICE_PARAMETERS\n1 1e999\n"); + lat.latName = "so"; + std::ifstream ifa = open_stru_file(stru); + testing::internal::CaptureStdout(); + EXPECT_EXIT(unitcell::read_lattice_constant(ifa, ofs_running, lat), ::testing::ExitedWithCode(1), ""); + const std::string output = testing::internal::GetCapturedStdout(); + EXPECT_THAT(output, testing::HasSubstr("invalid LATTICE_PARAMETERS for lattice type so")); + ifa.close(); +} + +TEST_F(ReadLatticeValidationTest, StRangeOverflowRejected) +{ + const std::string stru = + write_stru_file("st_range_overflow.stru", "LATTICE_CONSTANT\n4.0\nLATTICE_PARAMETERS\n1e999\n"); + lat.latName = "st"; + std::ifstream ifa = open_stru_file(stru); + testing::internal::CaptureStdout(); + EXPECT_EXIT(unitcell::read_lattice_constant(ifa, ofs_running, lat), ::testing::ExitedWithCode(1), ""); + const std::string output = testing::internal::GetCapturedStdout(); + EXPECT_THAT(output, testing::HasSubstr("invalid LATTICE_PARAMETERS for lattice type st")); + ifa.close(); +} + +TEST_F(ReadLatticeValidationTest, CubicScNeedsNoParams) +{ + const std::string stru = write_stru_file("sc_no_param.stru", "LATTICE_CONSTANT\n4.0\n"); + lat.latName = "sc"; + std::ifstream ifa = open_stru_file(stru); + EXPECT_TRUE(unitcell::read_lattice_constant(ifa, ofs_running, lat)); + EXPECT_DOUBLE_EQ(lat.latvec.Det(), 1.0); + ifa.close(); +} + +TEST_F(ReadLatticeValidationTest, DegenerateLatticeVectorsRejected) +{ + const std::string stru = write_stru_file("degenerate.stru", + "LATTICE_CONSTANT\n4.0\n" + "LATTICE_VECTORS\n" + "1 0 0\n" + "0 1 0\n" + "0 0 0\n"); + lat.latName = "user_defined_lattice"; + std::ifstream ifa = open_stru_file(stru); + testing::internal::CaptureStdout(); + EXPECT_EXIT(unitcell::read_lattice_constant(ifa, ofs_running, lat), + ::testing::ExitedWithCode(1), + ""); + const std::string output = testing::internal::GetCapturedStdout(); + EXPECT_THAT(output, testing::HasSubstr("zero-volume")); + ifa.close(); +} + class ReadSpeciesMalformedTest : public ::testing::Test { protected: