Files
openfoam/applications/test/exprValue/Test-exprValue.cxx
Mark Olesen 636a654f4a ENH: add standard _byte access for exprValue
- allows UPstream::broadcast and direct read/write

- add operator== for exprValue
2023-11-18 15:24:01 +01:00

194 lines
4.5 KiB
C++

/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2021-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
Application
Test-exprValue
Description
Test low-level polymorphic value container (exprValue)
\*---------------------------------------------------------------------------*/
#include "argList.H"
#include "IOstreams.H"
#include "ITstream.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;
}
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<< "good" << nl;
}
else
{
Info<< "bad" << 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<expressions::exprValue>::value
<< nl << nl;
{
expressions::exprValue value;
Info<< "exprValue"
<< " sizeof:" << value.size_bytes()
<< nl << nl;
// Info<< "value: " << value << nl;
// Nothing
printInfo(value);
value.set(scalar(100));
printInfo(value);
value.set(vector(1,2,3));
printInfo(value);
value = vector(4,5,6);
printInfo(value);
value = Zero;
printInfo(value);
value.clear();
printInfo(value);
if (UPstream::parRun())
{
Info<< "Before broadcast" << nl;
}
if (UPstream::master())
{
value = 100 * vector(1,0,0);
}
printInfo(value);
expressions::exprValue oldValue(value);
// Since the IO serialization is not symmetric (in ASCII) there
// is no 'operator>>' defined and thus the regular Pstream::broadcast
// will not compile.
// Even although the data are contiguous and that code branch is never
// used.
// Fails to compile: Pstream::broadcast(value);
// 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;
}
// ************************************************************************* //