openfoam/applications/test/exprValue1/Test-exprValue1.cxx
Mark Olesen cf2b305b4f ENH: upgrade to use some C++17 constructs
- 'if constexpr (...)'
   * instead of std::enable_if
   * terminate template recursion
   * compile-time elimination of code

- use C++14 '_t', '_v' versions,
  eg, std::is_integral_v<T> instead of std::is_integral<T>::value

- std::begin, std::end, std::void_t instead of prev stdFoam versions

- provide is_contiguous_v<..> as short form of is_contiguous<..>::value
  with the additional benefit of removing any cv qualifiers.

ENH: include is_rotational_vectorspace trait

- tests for vector-space and nComponents > 1 (ie, not sphericalTensor)

ENH: improve robustness of pTraits_.. tests by removing cv qualifiers
2025-01-31 09:51:44 +01:00

243 lines
5.5 KiB
C++

/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2021-2025 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
Application
Test-exprValue1
Description
Test low-level polymorphic value container (exprValue)
\*---------------------------------------------------------------------------*/
#include "argList.H"
#include "IOstreams.H"
#include "ITstream.H"
#include "OTstream.H"
#include "SpanStream.H"
#include "exprValue.H"
#include "Pstream.H"
using namespace Foam;
void printInfo(const expressions::exprValue& val)
{
Pout<< "Boxed type:" << int(val.typeCode())
<< " (" << val.valueTypeName() << ") good:"
<< val.good() << " => " << val << nl;
}
void write_read(const expressions::exprValue& val)
{
OCharStream os;
os << val;
ISpanStream is(os.view());
expressions::exprValue val2;
is >> val2;
Pout<< "wrote " << os.count() << " chars: " << os.str() << nl;
printInfo(val);
printInfo(val2);
Pout<< "====" << nl;
}
tokenList tokens_of(const expressions::exprValue& val)
{
OTstream toks;
toks << val;
Pout<< "val with tokens: ";
toks.writeList(Pout, 0) << nl;
for (const auto& t : toks)
{
Pout<< " " << t.info() << nl;
}
Pout<< nl;
return toks;
}
expressions::exprValue tryParse(const std::string& str)
{
expressions::exprValue val, val2;
ITstream is(str);
const bool ok = val.read(is);
Info<< "read " << Foam::name(val.typeCode()) << " from " << str;
if (ok)
{
Info<< " trailing tokens:" << is.nRemainingTokens() << nl
<< "value: " << val << nl;
}
else
{
Info<< " FAILED" << nl;
}
if (ok)
{
Info<< "Direct from string: ";
if (expressions::exprValue::read(str, val2))
{
Info<< "OK" << nl;
}
else
{
Info<< "NOK" << nl;
}
}
return val;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
int main(int argc, char *argv[])
{
argList::noBanner();
argList::noCheckProcessorDirectories();
#include "setRootCase.H"
// Aborts
// expressions::exprValue value(std::string(""));
// Regular broadcast doesn't work
Info<< "exprValue"
<< " sizeof:" << sizeof(expressions::exprValue)
<< " contiguous:" << is_contiguous_v<expressions::exprValue>
<< nl << nl;
{
expressions::exprValue value;
tokenList toks;
Info<< "exprValue"
<< " sizeof:" << value.size_bytes()
<< nl << nl;
// Info<< "value: " << value << nl;
// Nothing
printInfo(value);
toks = tokens_of(value);
write_read(value);
value.set(scalar(100));
printInfo(value); write_read(value); toks = tokens_of(value);
value.set(scalar(100.01));
printInfo(value); write_read(value); toks = tokens_of(value);
value.set(vector(1,2,3));
printInfo(value); write_read(value); toks = tokens_of(value);
value = vector(4,5,6);
printInfo(value); write_read(value); toks = tokens_of(value);
value = Zero;
printInfo(value); write_read(value); toks = tokens_of(value);
value.clear();
printInfo(value); write_read(value); toks = tokens_of(value);
value.set<bool>(true);
printInfo(value);
printInfo(value); write_read(value); toks = tokens_of(value);
if (UPstream::parRun())
{
Info<< "Before broadcast" << nl;
}
if (UPstream::master())
{
value = 100 * vector(1,0,0);
}
printInfo(value);
expressions::exprValue oldValue(value);
if (true)
{
// Broadcast with serialization
Pstream::broadcast(value);
}
else
{
// Broadcast manually
UPstream::broadcast
(
value.data_bytes(),
value.size_bytes(),
UPstream::worldComm
);
}
Pout<< "same values: " << (oldValue == value) << nl;
// Can put them in a Map and write, but reading will not work...
Map<expressions::exprValue> map;
map(1) = value;
Info<< "map: " << map << nl;
if (UPstream::parRun())
{
Info<< "After broadcast" << nl;
printInfo(value);
}
}
{
Info<< nl << "Test parsing" << nl << nl;
for
(
const auto& input :
stringList
({
"()", // bad
"( 1 2 ", // also bad
"( ", // really bad
"(1 16 12)",
"(1 bad)",
"(5)",
"1.2345",
"5.678 trailing",
"true",
"false",
" 1 ",
" yes no "
})
)
{
(void) tryParse(input);
}
}
return 0;
}
// ************************************************************************* //