ENH: add dimensionSet provisioning for a dimensioned clip() method

- use file-local function to reduce some code clutter
This commit is contained in:
Mark Olesen
2019-01-09 23:44:17 +01:00
parent d0bb3670a7
commit 7720b59066
5 changed files with 228 additions and 96 deletions

View File

@ -27,7 +27,9 @@ Description
\*---------------------------------------------------------------------------*/
#include "dimensionSet.H"
#include "dimensionedScalar.H"
#include "IOmanip.H"
#include <tuple>
using namespace Foam;
@ -39,6 +41,96 @@ using namespace Foam;
Info<< STRING_QUOTE(arg) << " " << arg << nl
bool hadDimensionError
(
const std::tuple<bool, dimensionSet, dimensionSet>& input,
bool dimsOk,
std::string errMsg
)
{
if (dimsOk)
{
if (std::get<0>(input))
{
Info<< "(pass) dimension check ok ";
}
else
{
Info<< "(fail) unexpected success for dimension check ";
}
Info<< std::get<1>(input) << " == " << std::get<2>(input) << nl;
}
else
{
if (std::get<0>(input))
{
Info<< "(fail) unexpected";
}
else
{
Info<< "(pass) expected";
}
Info<< " failure" << nl << errMsg.c_str() << nl;
}
return (std::get<0>(input) != dimsOk);
}
unsigned checkDimensions
(
std::initializer_list
<
std::tuple<bool, dimensionSet, dimensionSet>
> tests
)
{
Info<< nl << "Verify dimension checks" << nl << nl;
unsigned nFail = 0;
std::string errMsg;
// Expect some failures
const bool prev = FatalError.throwExceptions();
for
(
const std::tuple<bool, dimensionSet, dimensionSet>& test
: tests
)
{
const bool expected = std::get<0>(test);
const dimensionSet& a = std::get<1>(test);
const dimensionSet& b = std::get<2>(test);
bool dimsOk = false;
try
{
// min(a, b);
clip(a, b);
dimsOk = true;
}
catch (Foam::error& err)
{
errMsg = err.message();
}
if (expected != dimsOk)
{
++nFail;
}
hadDimensionError(test, dimsOk, errMsg);
}
FatalError.throwExceptions(prev);
return nFail;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
@ -92,6 +184,21 @@ int main(int argc, char *argv[])
}
unsigned nFail = 0;
nFail += checkDimensions
({
{ true, dimless, dimless },
{ false, dimless, dimPressure }
});
if (nFail)
{
Info<< nl << "failed " << nFail << " tests" << nl;
return 1;
}
Info<< nl << "End\n" << endl;
return 0;

View File

@ -47,14 +47,49 @@ inline scalar readNasScalar(const std::string& str)
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
template<class TYPE>
template<class T>
bool hadParsingError
(
const std::pair<bool, std::string>& input,
const std::pair<bool, T>& result,
std::string errMsg
)
{
if (result.first)
{
if (input.first)
{
Info<< "(pass) parsed "
<< input.second << " = " << result.second << nl;
}
else
{
Info<< "(fail) unexpected success for " << input.second << nl;
}
}
else
{
if (input.first)
{
Info<< "(fail) unexpected";
}
else
{
Info<< "(pass) expected";
}
Info<< " failure " << input.second << " >> " << errMsg.c_str() << nl;
}
return (input.first != result.first);
}
template<class T>
unsigned testParsing
(
TYPE (*function)(const std::string&),
std::initializer_list
<
Tuple2<bool, std::string>
> tests
T (*function)(const std::string&),
std::initializer_list<std::pair<bool, std::string>> tests
)
{
unsigned nFail = 0;
@ -63,51 +98,26 @@ unsigned testParsing
// Expect some failures
const bool prev = FatalIOError.throwExceptions();
for (const Tuple2<bool, std::string>& test : tests)
for (const std::pair<bool, std::string>& test : tests)
{
const bool expected = test.first();
const std::string& str = test.second();
std::pair<bool, T> result(false, T());
bool parsed = true;
TYPE val;
try
{
val = function (str);
result.second = function (test.second);
result.first = true;
}
catch (Foam::error& err)
{
parsed = false;
errMsg = err.message();
}
if (parsed)
if (test.first != result.first)
{
if (expected)
{
Info<< "(pass) parsed " << str << " = " << val << nl;
}
else
{
++nFail;
Info<< "(fail) unexpected success for " << str << nl;
}
++nFail;
}
else
{
if (expected)
{
++nFail;
Info<< "(fail) unexpected";
}
else
{
Info<< "(pass) expected";
}
Info<< " failure " << str
<< " >> " << errMsg.c_str() << nl;
}
hadParsingError(test, result, errMsg);
}
FatalIOError.throwExceptions(prev);