diff --git a/applications/test/dictionary2/Test-dictionary2.C b/applications/test/dictionary2/Test-dictionary2.C index d66e0d0590..b05c18c3c3 100644 --- a/applications/test/dictionary2/Test-dictionary2.C +++ b/applications/test/dictionary2/Test-dictionary2.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2017-2018 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2017-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -34,6 +34,8 @@ Description #include "IOobject.H" #include "IFstream.H" #include "dictionary.H" +#include "ops.H" +#include "scalarRange.H" #include "stringOps.H" using namespace Foam; @@ -108,6 +110,42 @@ scalar try_getScalar(const dictionary& dict, const word& k) } +// Try with getCheck +template +scalar try_getCheckScalar +( + const dictionary& dict, + const word& k, + const Predicate& pred +) +{ + scalar val(-GREAT); + + const bool throwingIOError = FatalIOError.throwExceptions(); + const bool throwingError = FatalError.throwExceptions(); + + try + { + val = dict.getCheck(k, pred); + Info<< "getCheck(" << k << ") = " << val << nl; + } + catch (const Foam::IOerror& err) + { + Info<< "getCheck(" << k << ") Caught FatalIOError " + << err << nl << endl; + } + catch (const Foam::error& err) + { + Info<< "getCheck(" << k << ") Caught FatalError " + << err << nl << endl; + } + FatalError.throwExceptions(throwingError); + FatalIOError.throwExceptions(throwingIOError); + + return val; +} + + // Try with *entry (from findEntry) and get scalar try_getScalar(const entry* eptr, const word& k) { @@ -311,6 +349,7 @@ int main(int argc, char *argv[]) IStringStream ( "good 3.14159;\n" + "negative -3.14159;\n" "empty;\n" // "bad text;\n" // always fails // "bad 3.14159 1234;\n" // fails for readScalar @@ -338,6 +377,26 @@ int main(int argc, char *argv[]) try_getScalar(dict2, "empty"); } + + // With getCheck + { + Info<< nl << "Test some input with getCheck()" << nl; + + try_getCheckScalar(dict2, "good", scalarRange::gt0()); + try_getCheckScalar(dict2, "negative", scalarRange::gt0()); + + try_getCheckScalar(dict2, "good", greaterOp1(0)); + try_getCheckScalar(dict2, "negative", greaterOp1(0)); + + Info<< nl << "with lambda" << nl; + try_getCheckScalar + ( + dict2, + "good", + [](const scalar x) { return x > 0; } + ); + } + // With findEntry and get { Info<< nl diff --git a/applications/test/scalarPredicates/Test-scalarPredicates.C b/applications/test/scalarPredicates/Test-scalarPredicates.C index 22338e9174..587ca38b0d 100644 --- a/applications/test/scalarPredicates/Test-scalarPredicates.C +++ b/applications/test/scalarPredicates/Test-scalarPredicates.C @@ -36,6 +36,8 @@ Description #include "FlatOutput.H" #include "Tuple2.H" #include "StringStream.H" +#include "ops.H" +#include "bitSet.H" using namespace Foam; @@ -44,7 +46,7 @@ void doTest(const scalarList& values, const predicates::scalars& accept) { // Also tests that output is suppressed Info<<"Have: " << accept.size() << " predicates" << accept << endl; - Info<<"values: " << flatOutput(values) << endl; + Info<<"values: " << flatOutput(values) << endl; for (const scalar& value : values) { @@ -60,6 +62,30 @@ void doTest(const scalarList& values, const predicates::scalars& accept) } +template +void testPredicate(const scalarList& values, const Predicate& pred) +{ + bitSet matches; + + label i=0; + + for (const scalar& value : values) + { + if (pred(value)) + { + matches.set(i); + } + + ++i; + } + + IndirectList matched(values, matches.toc()); + + Info<< "matched: " << flatOutput(matched.addressing()) + << " = " << flatOutput(matched) << nl; +} + + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // Main program: @@ -149,6 +175,16 @@ int main(int argc, char *argv[]) } + Info<< nl << "Test with ops" << nl; + Info<<"values: " << flatOutput(values) << endl; + { + testPredicate(values, lessOp1(10)); + testPredicate(values, greaterOp1(100)); + + // Also with dissimilar type + testPredicate(values, lessEqOp1