From 7139412258f8892a3c0f8ed23b06a2e1476c17a4 Mon Sep 17 00:00:00 2001 From: Scott R Charlton Date: Fri, 20 Jan 2012 00:28:46 +0000 Subject: [PATCH] error routines working on windows git-svn-id: svn://136.177.114.72/svn_GW/IPhreeqc/branches/ErrorHandling@6082 1feff8c3-07ed-0310-ac33-dd36852eb9cd --- src/IPhreeqc.cpp | 13 ++- src/IPhreeqc.h | 2 +- src/IPhreeqc.hpp | 3 +- unit/TestIPhreeqcLib.cpp | 226 ++++++++++++++++++++++++++++++++++++++- unit/TestIPhreeqcLib.h | 12 +++ 5 files changed, 248 insertions(+), 8 deletions(-) diff --git a/src/IPhreeqc.cpp b/src/IPhreeqc.cpp index 1f363bf8..d45a4d60 100644 --- a/src/IPhreeqc.cpp +++ b/src/IPhreeqc.cpp @@ -33,6 +33,7 @@ IPhreeqc::IPhreeqc(void) , LogStringOn(false) , ErrorStringOn(true) , ErrorReporter(0) +, WarningStringOn(true) , WarningReporter(0) , SelectedOutput(0) , SelectedOutputStringOn(false) @@ -1212,10 +1213,13 @@ void IPhreeqc::error_msg(const char *str, bool stop) ASSERT(!(this->ErrorFileOn ^ (this->error_ostream != 0))); this->PHRQ_io::error_msg(str); - this->AddError(str); + if (this->ErrorStringOn && this->error_on) + { + this->AddError(str); + } if (stop) { - if (this->error_ostream) + if (this->error_ostream && this->error_on) { (*this->error_ostream) << "Stopping.\n"; this->error_ostream->flush(); @@ -1231,7 +1235,10 @@ void IPhreeqc::warning_msg(const char *str) std::ostringstream oss; oss << str << std::endl; - this->AddWarning(oss.str().c_str()); + if (this->WarningStringOn) + { + this->AddWarning(oss.str().c_str()); + } } void IPhreeqc::output_msg(const char * str) diff --git a/src/IPhreeqc.h b/src/IPhreeqc.h index a0b3e2a6..0947cbc1 100644 --- a/src/IPhreeqc.h +++ b/src/IPhreeqc.h @@ -1482,7 +1482,7 @@ Headings /** * Sets the error string switch on or off. This switch controls whether or not the data normally sent * to the error file are stored in a buffer for retrieval. The initial setting after calling - * \ref CreateIPhreeqc is off. + * \ref CreateIPhreeqc is on. * @param id The instance id returned from \ref CreateIPhreeqc. * @param output_string_on If non-zero, captures the error output into a string buffer; * if zero, error output is not captured to a string buffer. diff --git a/src/IPhreeqc.hpp b/src/IPhreeqc.hpp index 04de3b05..6837c6ad 100644 --- a/src/IPhreeqc.hpp +++ b/src/IPhreeqc.hpp @@ -688,7 +688,7 @@ public: /** * Sets the error string switch on or off. This switch controls whether or not the data normally sent - * to the error file are stored in a buffer for retrieval. The initial setting is false. + * to the error file are stored in a buffer for retrieval. The initial setting is true. * @param bValue If true, captures error output into a string buffer; if false, error output is not captured to a string buffer. * @see GetErrorFileOn, GetErrorString, GetErrorStringOn, GetErrorStringLine, GetErrorStringLineCount, SetErrorFileOn */ @@ -833,6 +833,7 @@ protected: std::string ErrorString; std::vector< std::string > ErrorLines; + bool WarningStringOn; IErrorReporter *WarningReporter; std::string WarningString; std::vector< std::string > WarningLines; diff --git a/unit/TestIPhreeqcLib.cpp b/unit/TestIPhreeqcLib.cpp index 94f26d76..8d992fc1 100644 --- a/unit/TestIPhreeqcLib.cpp +++ b/unit/TestIPhreeqcLib.cpp @@ -2084,10 +2084,27 @@ void TestIPhreeqcLib::TestGetErrorStringLine(void) CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::SetSelectedOutputFileOn(n, 0) ); CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::SetDumpFileOn(n, 0) ); CPPUNIT_ASSERT_EQUAL( 1, ::RunFile(n, "dump") ); - +#if 0 + CPPUNIT_ASSERT_EQUAL( 8, ::GetErrorStringLineCount(n) ); +#else CPPUNIT_ASSERT_EQUAL( 2, ::GetErrorStringLineCount(n) ); - CPPUNIT_ASSERT_EQUAL( std::string("ERROR: Gas not found in PHASES data base, Amm(g)."), std::string(::GetErrorStringLine(n, 0)) ); - CPPUNIT_ASSERT_EQUAL( std::string("ERROR: Calculations terminating due to input errors."), std::string(::GetErrorStringLine(n, 1)) ); +#endif + + int line = 0; +#if 0 + CPPUNIT_ASSERT_EQUAL( std::string("WARNING: DATABASE keyword is ignored by IPhreeqc."), std::string(::GetErrorStringLine(n, line++)) ); + CPPUNIT_ASSERT_EQUAL( std::string("WARNING: Cell-lengths were read for 1 cells. Last value is used till cell 100."), std::string(::GetErrorStringLine(n, line++)) ); + CPPUNIT_ASSERT_EQUAL( std::string("WARNING: No dispersivities were read; disp = 0 assumed."), std::string(::GetErrorStringLine(n, line++)) ); +#endif + CPPUNIT_ASSERT_EQUAL( std::string("ERROR: Gas not found in PHASES data base, Amm(g)."), std::string(::GetErrorStringLine(n, line++)) ); +#if 0 + CPPUNIT_ASSERT_EQUAL( std::string("WARNING: Could not find element in database, Amm."), std::string(::GetErrorStringLine(n, line++)) ); + CPPUNIT_ASSERT_EQUAL( std::string("\tConcentration is set to zero."), std::string(::GetErrorStringLine(n, line++)) ); +#endif + CPPUNIT_ASSERT_EQUAL( std::string("ERROR: Calculations terminating due to input errors."), std::string(::GetErrorStringLine(n, line++)) ); +#if 0 + CPPUNIT_ASSERT_EQUAL( std::string("Stopping."), std::string(::GetErrorStringLine(n, line++)) ); +#endif if (n >= 0) { @@ -3264,3 +3281,206 @@ void TestIPhreeqcLib::TestGetLogStringLine(void) CPPUNIT_ASSERT_EQUAL( std::string(""), std::string(::GetLogStringLine(n, -4)) ); } +void TestIPhreeqcLib::TestSetErrorFileName(void) +{ + char ERR_FILENAME[80]; + sprintf(ERR_FILENAME, "error.%06d.out", ::rand()); + if (::FileExists(ERR_FILENAME)) + { + ::DeleteFile(ERR_FILENAME); + } + + int n = ::CreateIPhreeqc(); + CPPUNIT_ASSERT(n >= 0); + + CPPUNIT_ASSERT_EQUAL( 0, ::LoadDatabase(n, "phreeqc.dat")); + + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::AccumulateLine(n, "SOLUTION 1") ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::AccumulateLine(n, " pH 7") ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::AccumulateLine(n, " Na 1") ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::AccumulateLine(n, "PHASES") ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::AccumulateLine(n, " Fix_H+") ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::AccumulateLine(n, " H+ = H+") ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::AccumulateLine(n, " log_k 0") ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::AccumulateLine(n, "EQUILIBRIUM_PHASES") ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::AccumulateLine(n, " Fix_H+ -10 HCl 10") ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::AccumulateLine(n, "END") ); + + // run + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::SetErrorFileOn(n, 1) ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::SetLogFileOn(n, 0) ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::SetOutputFileOn(n, 0) ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::SetSelectedOutputFileOn(n, 0) ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::SetDumpStringOn(n, 0) ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::SetDumpFileOn(n, 0) ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::SetErrorFileName(n, ERR_FILENAME) ); + + + CPPUNIT_ASSERT_EQUAL( 1, ::RunAccumulated(n) ); + + CPPUNIT_ASSERT_EQUAL( true, ::FileExists(ERR_FILENAME) ); + + std::string lines[100]; + { + std::ifstream ifs(ERR_FILENAME); + + size_t i = 0; + while (i < sizeof(lines)/sizeof(lines[0]) && std::getline(ifs, lines[i])) + { + ++i; + } + + CPPUNIT_ASSERT_EQUAL( (size_t)84, i ); + } + + CPPUNIT_ASSERT_EQUAL( std::string("WARNING: Maximum iterations exceeded, 100"), lines[0] ); + CPPUNIT_ASSERT_EQUAL( std::string("WARNING: Numerical method failed with this set of convergence parameters."), lines[2] ); + CPPUNIT_ASSERT_EQUAL( std::string("ERROR: Numerical method failed on all combinations of convergence parameters"), lines[82] ); + CPPUNIT_ASSERT_EQUAL( std::string("Stopping."), lines[83] ); + + if (::FileExists(ERR_FILENAME)) + { + ::DeleteFile(ERR_FILENAME); + } +} + +void TestIPhreeqcLib::TestErrorStringOnOff(void) +{ + int n = ::CreateIPhreeqc(); + CPPUNIT_ASSERT(n >= 0); + + CPPUNIT_ASSERT_EQUAL( true, ::GetErrorStringOn(n) != 0 ); + + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::SetErrorStringOn(n, 1) ); + CPPUNIT_ASSERT_EQUAL( true, ::GetErrorStringOn(n) != 0 ); + + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::SetErrorStringOn(n, 0) ); + CPPUNIT_ASSERT_EQUAL( false, ::GetErrorStringOn(n) != 0 ); + + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::SetErrorStringOn(n, 1) ); + CPPUNIT_ASSERT_EQUAL( true, ::GetErrorStringOn(n) != 0 ); +} + +void TestIPhreeqcLib::TestGetErrorString(void) +{ + char ERR_FILENAME[80]; + sprintf(ERR_FILENAME, "error.%06d.out", ::rand()); + if (::FileExists(ERR_FILENAME)) + { + ::DeleteFile(ERR_FILENAME); + } + CPPUNIT_ASSERT_EQUAL( false, ::FileExists(ERR_FILENAME) ); + + int n = ::CreateIPhreeqc(); + CPPUNIT_ASSERT(n >= 0); + + CPPUNIT_ASSERT_EQUAL( 0, ::LoadDatabase(n, "phreeqc.dat")); + + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::AccumulateLine(n, "SOLUTION 1") ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::AccumulateLine(n, " pH 7") ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::AccumulateLine(n, " Na 1") ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::AccumulateLine(n, "PHASES") ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::AccumulateLine(n, " Fix_H+") ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::AccumulateLine(n, " H+ = H+") ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::AccumulateLine(n, " log_k 0") ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::AccumulateLine(n, "EQUILIBRIUM_PHASES") ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::AccumulateLine(n, " Fix_H+ -10 HCl 10") ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::AccumulateLine(n, "END") ); + + // run + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::SetErrorFileOn(n, 1) ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::SetErrorStringOn(n, 1) ); + + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::SetDumpFileOn(n, 0) ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::SetDumpStringOn(n, 0) ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::SetLogFileOn(n, 0) ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::SetOutputFileOn(n, 0) ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::SetOutputStringOn(n, 0) ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::SetSelectedOutputFileOn(n, 0) ); + + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::SetErrorFileName(n, ERR_FILENAME) ); + CPPUNIT_ASSERT_EQUAL( std::string(ERR_FILENAME), std::string(::GetErrorFileName(n)) ); + + CPPUNIT_ASSERT_EQUAL( 1, ::RunAccumulated(n) ); + + CPPUNIT_ASSERT_EQUAL( std::string(ERR_FILENAME), std::string(::GetErrorFileName(n)) ); + + CPPUNIT_ASSERT_EQUAL( true, ::FileExists(ERR_FILENAME) ); + + { +#if 0 + std::ifstream ifs(ERR_FILENAME); + std::string fline((std::istreambuf_iterator(ifs)), std::istreambuf_iterator()); +#else + std::string fline("ERROR: Numerical method failed on all combinations of convergence parameters\n"); +#endif + + std::string sline(::GetErrorString(n)); + CPPUNIT_ASSERT( sline.size() > 0 ); + + CPPUNIT_ASSERT_EQUAL( fline, sline ); + } + + if (::FileExists(ERR_FILENAME)) + { + ::DeleteFile(ERR_FILENAME); + } +} + +void TestIPhreeqcLib::TestGetErrorStringLineCount(void) +{ + int n = ::CreateIPhreeqc(); + CPPUNIT_ASSERT(n >= 0); + + CPPUNIT_ASSERT_EQUAL( 0, ::GetErrorStringLineCount(n)); + + CPPUNIT_ASSERT_EQUAL( 0, ::LoadDatabase(n, "phreeqc.dat")); + + CPPUNIT_ASSERT_EQUAL( 0, ::GetErrorStringLineCount(n)); + + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::AccumulateLine(n, "SOLUTION 1") ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::AccumulateLine(n, " pH 7") ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::AccumulateLine(n, " Na 1") ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::AccumulateLine(n, "PHASES") ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::AccumulateLine(n, " Fix_H+") ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::AccumulateLine(n, " H+ = H+") ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::AccumulateLine(n, " log_k 0") ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::AccumulateLine(n, "EQUILIBRIUM_PHASES") ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::AccumulateLine(n, " Fix_H+ -10 HCl 10") ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::AccumulateLine(n, "END") ); + + CPPUNIT_ASSERT_EQUAL( true, ::GetErrorStringOn(n) != 0 ); + CPPUNIT_ASSERT_EQUAL( 1, ::RunAccumulated(n) ); + CPPUNIT_ASSERT_EQUAL( 1, ::GetErrorStringLineCount(n) ); + + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::AccumulateLine(n, "SOLUTION 1") ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::AccumulateLine(n, " pH 7") ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::AccumulateLine(n, " Na 1") ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::AccumulateLine(n, "PHASES") ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::AccumulateLine(n, " Fix_H+") ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::AccumulateLine(n, " H+ = H+") ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::AccumulateLine(n, " log_k 0") ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::AccumulateLine(n, "EQUILIBRIUM_PHASES") ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::AccumulateLine(n, " Fix_H+ -10 HCl 10") ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::AccumulateLine(n, "END") ); + + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::SetErrorStringOn(n, 1) ); + CPPUNIT_ASSERT_EQUAL( 1, ::RunAccumulated(n) ); + CPPUNIT_ASSERT_EQUAL( 1, ::GetErrorStringLineCount(n) ); + + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::AccumulateLine(n, "SOLUTION 1") ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::AccumulateLine(n, " pH 7") ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::AccumulateLine(n, " Na 1") ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::AccumulateLine(n, "PHASES") ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::AccumulateLine(n, " Fix_H+") ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::AccumulateLine(n, " H+ = H+") ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::AccumulateLine(n, " log_k 0") ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::AccumulateLine(n, "EQUILIBRIUM_PHASES") ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::AccumulateLine(n, " Fix_H+ -10 HCl 10") ); + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::AccumulateLine(n, "END") ); + + CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::SetErrorStringOn(n, 0) ); + + CPPUNIT_ASSERT_EQUAL( 1, ::RunAccumulated(n) ); + CPPUNIT_ASSERT_EQUAL( 0, ::GetErrorStringLineCount(n) ); +} diff --git a/unit/TestIPhreeqcLib.h b/unit/TestIPhreeqcLib.h index e187d1a5..b473ce84 100644 --- a/unit/TestIPhreeqcLib.h +++ b/unit/TestIPhreeqcLib.h @@ -40,7 +40,9 @@ class TestIPhreeqcLib : public CppUnit::TestFixture CPPUNIT_TEST( TestGetDumpStringLine ); CPPUNIT_TEST( TestGetComponentCount ); CPPUNIT_TEST( TestGetComponent ); +#if 1 CPPUNIT_TEST( TestGetErrorStringLine ); +#endif CPPUNIT_TEST( TestErrorFileOn ); CPPUNIT_TEST( TestLogFileOn ); CPPUNIT_TEST( TestGetWarningStringLine ); @@ -58,6 +60,10 @@ class TestIPhreeqcLib : public CppUnit::TestFixture CPPUNIT_TEST( TestGetLogStringLineCount ); CPPUNIT_TEST( TestGetLogStringLine ); + CPPUNIT_TEST( TestSetErrorFileName ); + CPPUNIT_TEST( TestErrorStringOnOff ); + CPPUNIT_TEST( TestGetErrorString ); + CPPUNIT_TEST( TestGetErrorStringLineCount ); CPPUNIT_TEST_SUITE_END(); public: @@ -115,6 +121,12 @@ public: void TestGetLogString(void); void TestGetLogStringLineCount(void); void TestGetLogStringLine(void); + + void TestSetErrorFileName(void); + void TestErrorStringOnOff(void); + void TestGetErrorString(void); + void TestGetErrorStringLineCount(void); + protected: void TestFileOnOff(const char* FILENAME, int output_file_on, int error_file_on, int log_file_on, int selected_output_file_on, int dump_file_on); };