Files
openfoam/applications/test/primitives/Test-primitives.C
Mark Olesen a3f960e36f ENH: add 'default' as possible Switch state, but not as input/output
- in some circumstances we need to pass a bool value upwards to the
  caller and know if the true/false value was set based on real input
  or is a default value.

  Eg, in the object::read() we might normally have

     enabled_(dict.readIfPresent(key, true));

  but would lose information about why the value is true/false.

  We can change that by using

     enabled_(dict.readIfPresent<Switch>(key, Switch::DEFAULT_ON));

  After which we can use this information is testing.

      if
      (
          child.enabled().nonDefault()
        ? child.enabled()
        : parent.enabled()
      )
      { ... }

   And thus enable output if the parent requested it explicitly or by
   default and it has not been explicitly disabled in the child.

  No difference when testing as a bool and the text representation
  of DEFAULT_ON / DEFAULT_OFF will simply be "true" / "false".

ENH: add construction of Switch from dictionary (similar to Enum)
2019-01-28 23:18:21 +01:00

285 lines
7.1 KiB
C

/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2017-2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Application
Test-primitives
Description
Parsing etc for primitives.
\*---------------------------------------------------------------------------*/
#include "scalar.H"
#include "label.H"
#include "StringStream.H"
#include "NASCore.H"
#include "parsing.H"
#include "Tuple2.H"
#include "Switch.H"
#include "dictionary.H"
using namespace Foam;
// Shadow fileFormats::NASCore::readNasScalar
inline scalar readNasScalar(const std::string& str)
{
return fileFormats::NASCore::readNasScalar(str);
}
// As a function
inline Switch readSwitch(const std::string& str)
{
Switch sw(str);
if (sw.type() == Switch::ON)
{
Info<< "Was 'on'" << nl;
}
return sw;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
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
(
T (*function)(const std::string&),
std::initializer_list<std::pair<bool, std::string>> tests
)
{
unsigned nFail = 0;
string errMsg;
// Expect some failures
const bool prev1 = FatalError.throwExceptions();
const bool prev2 = FatalIOError.throwExceptions();
for (const std::pair<bool, std::string>& test : tests)
{
std::pair<bool, T> result(false, T());
try
{
result.second = function (test.second);
result.first = true;
}
catch (const Foam::error& err)
{
errMsg = err.message();
}
if (test.first != result.first)
{
++nFail;
}
hadParsingError(test, result, errMsg);
}
FatalError.throwExceptions(prev1);
FatalIOError.throwExceptions(prev2);
return nFail;
}
int main(int argc, char *argv[])
{
unsigned nFail = 0;
{
Info<< nl << "Test Switch parsing:" << nl;
nFail += testParsing
(
&readSwitch,
{
{ false, "True" },
{ true, "false" },
{ true, "on" },
{ false, "None" },
{ false, "default" },
}
);
dictionary dict;
dict.add("key1" , "true");
{
Switch sw("key", dict, Switch::DEFAULT_ON);
Info<<"got: " << sw << " type is DEFAULT_ON? "
<< (sw.type() == Switch::DEFAULT_ON) << nl;
}
{
Switch sw("key1", dict, Switch::DEFAULT_ON);
Info<<"got: " << sw << " type is DEFAULT_ON? "
<< (sw.type() == Switch::DEFAULT_ON) << nl;
}
}
{
Info<< nl << "Test readDouble: (small=" << doubleScalarVSMALL
<< " great=" << doubleScalarVSMALL << "):" << nl;
nFail += testParsing
(
&readDouble,
{
{ false, "" },
{ false, " " },
{ false, " xxx " },
{ false, " 1234E-" },
{ false, " 1234E junk" },
{ true, " 3.14159 " },
{ true, " 31.4159E-1 " },
{ false, " 100E1000 " },
{ true, " 1E-40 " },
{ true, " 1E-305 " },
{ true, " 1E-37 " },
{ true, " 1E-300 " },
}
);
}
{
Info<< nl << "Test readFloat: (small=" << floatScalarVSMALL
<< " great=" << floatScalarVGREAT << "):" << nl;
nFail += testParsing
(
&readFloat,
{
{ true, " 3.14159 " },
{ true, " 31.4159E-1 " },
{ false, " 31.4159E200 " },
{ true, " 31.4159E20 " },
{ true, " 1E-40 " },
{ true, " 1E-305 " },
{ true, " 1E-37 " },
{ true, " 1E-300 " },
}
);
}
{
Info<< nl << "Test readNasScalar:" << nl;
nFail += testParsing
(
&readNasScalar,
{
{ true, " 3.14159 " },
{ true, " 31.4159E-1 " },
{ true, " 314.159-2 " },
{ true, " 31.4159E200 " },
{ true, " 31.4159E20 " },
{ true, " 1E-40 " },
{ true, " 1E-305 " },
{ true, " 1E-37 " },
{ true, " 1E-300 " },
}
);
}
{
Info<< nl << "Test readInt32 (max=" << INT32_MAX << "):" << nl;
nFail += testParsing
(
&readInt32,
{
{ false, " 3.14159 " },
{ false, " 31E1 " },
{ false, " 31.4159E-1 " },
{ true, "100" },
{ true, " 2147483644" },
{ false, " 2147483700 " },
}
);
}
{
Info<< nl << "Test readUint32 (max="
<< unsigned(UINT32_MAX) << "):" << nl;
nFail += testParsing
(
&readUint32,
{
{ true, "\t2147483644" },
{ true, " 2147483700 " },
{ true, " 4294967295 " },
{ false, " 4294968000 " },
}
);
}
if (nFail)
{
Info<< nl << "failed " << nFail << " tests" << nl;
return 1;
}
Info<< nl << "passed all tests" << nl;
return 0;
}
// ************************************************************************* //