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/regex1/Test-regex1.C b/applications/test/regex1/Test-regex1.C index ca5bfb60cd..9bc38d64b2 100644 --- a/applications/test/regex1/Test-regex1.C +++ b/applications/test/regex1/Test-regex1.C @@ -27,12 +27,16 @@ Description \*---------------------------------------------------------------------------*/ #include "argList.H" +#include "IOobject.H" #include "IOstreams.H" #include "IFstream.H" #include "Switch.H" +#include "SubStrings.H" #include "regExpCxx.H" +#ifndef _WIN32 #include "regExpPosix.H" +#endif using namespace Foam; @@ -83,6 +87,7 @@ static Ostream& operator<<(Ostream& os, const regExpCxx::results_type& sm) // Simple output of match groups +#ifndef _WIN32 static Ostream& operator<<(Ostream& os, const regExpPosix::results_type& sm) { for (std::smatch::size_type i = 1; i < sm.size(); ++i) @@ -92,6 +97,7 @@ static Ostream& operator<<(Ostream& os, const regExpPosix::results_type& sm) return os; } +#endif template @@ -209,7 +215,6 @@ void generalTests() } - template void testExpressions(const UList& tests) { @@ -293,11 +298,13 @@ int main(int argc, char *argv[]) "Test C++11 regular expressions" ); + #ifndef _WIN32 argList::addBoolOption ( "posix", "Test POSIX regular expressions" ); + #endif argList::addArgument("file"); argList::addArgument("..."); @@ -306,6 +313,17 @@ int main(int argc, char *argv[]) #include "setRootCase.H" + if (std::is_same::value) + { + Info<<"Foam::regExp uses C++11 regex" << nl << nl; + } + #ifndef _WIN32 + if (std::is_same::value) + { + Info<<"Foam::regExp uses POSIX regex" << nl << nl; + } + #endif + if (!args.count({"cxx", "posix"})) { Info<< "Specified one or more of -cxx, -posix" << nl; @@ -321,10 +339,12 @@ int main(int argc, char *argv[]) generalTests(); } + #ifndef _WIN32 if (args.found("posix")) { generalTests(); } + #endif } for (label argi = 1; argi < args.size(); ++argi) @@ -339,10 +359,12 @@ int main(int argc, char *argv[]) testExpressions(tests); } + #ifndef _WIN32 if (args.found("posix")) { testExpressions(tests); } + #endif } Info<< "\nDone" << nl << endl; diff --git a/applications/test/regex1/testRegexps2 b/applications/test/regex1/testRegexps2 new file mode 100644 index 0000000000..207c05b048 --- /dev/null +++ b/applications/test/regex1/testRegexps2 @@ -0,0 +1,19 @@ +/*--------------------------------*- C++ -*----------------------------------*\ +| ========= | | +| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox | +| \\ / O peration | Version: v1812 | +| \\ / A nd | Web: www.OpenFOAM.com | +| \\/ M anipulation | | +\*---------------------------------------------------------------------------*/ + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +// Pattern, String +( + ( true "(U|k|epsilon)" "U" ) + ( false "(U|k|epsilon)" "alpha" ) + ( true "ab.*" "abc" ) + ( true ".*" "abc" ) +) + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // 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