Commit 55e5a777 authored by shunbo's avatar shunbo
Browse files

initial commit

parents
Test-argList.C
EXE = $(FOAM_USER_APPBIN)/Test-argList
/* EXE_INC = -I$(LIB_SRC)/finiteVolume/lnInclude */
/* EXE_LIBS = -lfiniteVolume */
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2017-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
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/>.
Description
\*---------------------------------------------------------------------------*/
#include "argList.H"
#include "IOstreams.H"
#include "MinMax.H"
#include "Switch.H"
#include "StringStream.H"
using namespace Foam;
void predicateTests_label(const word& optName, const argList& args)
{
Info<< "predicate tests for " << optName << nl;
const bool oldThrowingError = FatalError.throwing(true);
try
{
label val;
val = args.getCheck<label>(optName, labelMinMax::ge(0));
}
catch (const Foam::error& err)
{
Info<< "Caught FatalError "
<< err << nl << endl;
}
FatalError.throwing(oldThrowingError);
}
void predicateTests_scalar(const word& optName, const argList& args)
{
Info<< "predicate tests for " << optName << nl;
const bool oldThrowingError = FatalError.throwing(true);
try
{
scalar val;
val = args.getCheck<scalar>(optName, scalarMinMax::ge(0));
}
catch (const Foam::error& err)
{
Info<< "Caught FatalError "
<< err << nl << endl;
}
FatalError.throwing(oldThrowingError);
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
argList::noBanner();
argList::noCheckProcessorDirectories(); // parallel OK, but without checks
// argList::noFunctionObjects();
argList::addOption("label", "value", "Test parsing of label");
argList::addOption("scalar", "value", "Test parsing of scalar");
argList::addOption("string", "value", "Test string lookup");
argList::addOption("relative", "PATH", "Test relativePath");
argList::addBoolOption
(
"predicates",
"Apply some predicate tests (for label and scalar)"
);
// These are actually lies (never had -parseLabel, -parseScalar etc),
// but good for testing...
// Emits warning about it being old
argList::addOptionCompat("label", {"parseLabel", 1612});
// Specifying version=0 to use alias without any warnings
argList::addOptionCompat("scalar", {"parseScalar", 0});
// Fake a future option...
argList::addOptionCompat("label", {"parse-label", 2112});
// Ignore an old bool option
argList::ignoreOptionCompat({"xml", 1700}, false);
// Ignore an old option with arg. Specified version=0 to suppress warnings
argList::ignoreOptionCompat({"format", 0}, true);
// Ignore a future option? Fairly pointless
argList::ignoreOptionCompat({"ascii", 2112}, false);
argList::addArgument("label");
argList::addArgument("...");
argList::addArgument("label");
argList::noMandatoryArgs();
argList::addDryRunOption("Just for testing");
argList::addVerboseOption("Increase verbosity");
#include "setRootCase.H"
Pout<< "command-line ("
<< args.options().size() << " options, "
<< args.args().size() << " args)" << nl
<< " " << args.commandLine().c_str() << nl << nl;
Pout<< "rootPath: " << args.rootPath() << nl
<< "globalCase: " << args.globalCaseName() << nl
<< "globalPath: " << args.globalPath() << nl
<< nl;
Pout<< "dry-run: " << args.dryRun()
<< " verbose: " << args.verbose() << nl;
if (args.found("relative"))
{
Pout<< "input path: " << args["relative"] << nl
<< "relative : " << args.relativePath(args["relative"], true) << nl
<< nl;
}
Info<< "have: "
<< args.count({"label", "scalar"}) << " options" << nl;
label ival;
scalar sval;
Info<< nl;
Info<< "-label = " << flush;
if (args.readIfPresent("label", ival))
{
Info<< ival << nl;
if (args.found("predicates"))
{
predicateTests_label("label", args);
}
}
else
{
Info<< "not specified" << nl;
}
Info<< "-scalar = " << flush;
if (args.readIfPresent("scalar", sval))
{
Info<< sval << nl;
if (args.found("predicates"))
{
predicateTests_scalar("scalar", args);
}
}
else
{
Info<< "not specified" << nl;
}
// Using direct reading
Info<< nl;
if (args.found("label"))
{
Info<< "-label = " << args.get<label>("label")
<< " or " << args.opt<label>("label")
#ifdef Foam_argList_1712
<< " or " << args.optionRead<label>("label") // old-compat
#endif
<< " or " << readLabel(args["label"]) // with function
<< nl;
}
if (args.found("scalar"))
{
Info<< "-scalar = " << args.get<scalar>("scalar")
<< " or " << args.opt<scalar>("label")
#ifdef Foam_argList_1712
<< " or " << args.optionRead<scalar>("scalar") // old-compat
#endif
<< " or " << readScalar(args["scalar"]) // with function
<< nl;
}
if (args.found("string"))
{
Info<< "-string = " << args.get("string")
<< " or " << args.opt("string")
#ifdef Foam_argList_1712
<< " or " << args.optionRead<scalar>("string") // old-compat
#endif
<< nl;
}
// Arg reading
Info<< nl;
for (label argi=1; argi < args.size(); ++argi)
{
Info<< "arg[" << argi << "] = " << args.get<string>(argi)
#ifdef Foam_argList_1712
<< " or " << args.read<label>(argi) // old-compat
<< " or " << args.argRead<label>(argi) // old-compat
#endif
<< nl;
}
Info<< "\nEnd\n" << endl;
return 0;
}
// ************************************************************************* //
Test-autoPtr.C
EXE = $(FOAM_USER_APPBIN)/Test-autoPtr
EXE_INC = \
-I$(LIB_SRC)/thermophysicalModels/thermophysicalProperties/lnInclude
EXE_LIBS = \
-lthermophysicalProperties
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2018-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
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/>.
\*---------------------------------------------------------------------------*/
// #define Foam_autoPtr_deprecate_setMethod
#include <memory>
#include "autoPtr.H"
#include "labelList.H"
#include "ListOps.H"
#include "IOstreams.H"
#include "Switch.H"
#include "C7H16.H"
using namespace Foam;
// An example of bad use, since our autoPtr is too generous when being passed
// around
void testTransfer1(autoPtr<labelList> ap)
{
// Passed in copy, so automatically removes content
// Transfer would be nice, but not actually needed
Info<< "recv " << Switch::name(bool(ap)) << nl;
}
// An example of good use. We are allowed to manage the memory (or not)
// and not automatically start losing things.
void testTransfer2(autoPtr<labelList>&& ap)
{
// As rvalue, so this time we actually get to manage content
Info<< "recv " << Switch::name(bool(ap)) << nl;
}
// Constructor from literal nullptr is implicit
template<class T>
autoPtr<T> testNullReturn1()
{
return nullptr;
}
// Constructor from raw pointer is explicit
template<class T>
autoPtr<T> testNullReturn2()
{
T* p = new T;
return p;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
{
auto list = autoPtr<labelList>::New(10, label(-1));
Info<<"create: " << *list << nl;
const labelList* plist = list;
Info<<"pointer: " << name(plist) << nl
<<"content: " << *plist << nl;
Info<<"create: " << autoPtr<labelList>::New(10, label(-1))()
<< nl << nl;
// Transfer to unique_ptr
std::unique_ptr<labelList> list2(list.release());
Info<<"move to unique_ptr: " << *list2 << nl;
Info<<"old is " << Switch(bool(list)) << nl;
autoPtr<labelList> list3(list2.release());
Info<<"move unique to autoPtr: " << *list3 << nl;
Info<<"old is " << Switch(bool(list2)) << nl;
}
// Confirm that forwarding with move construct actually works as expected
{
auto source = identity(8);
Info<<"move construct from "
<< flatOutput(source) << " @ " << name(source.cdata())
<< nl << nl;
auto list = autoPtr<labelList>::New(std::move(source));
Info<<"created: "
<< flatOutput(*list) << " @ " << name(list->cdata())
<< nl << nl;
Info<<"orig: "
<< flatOutput(source) << " @ " << name(source.cdata())
<< nl << nl;
}
// Explicit construct Base from Derived
{
autoPtr<liquidProperties> liqProp
(
autoPtr<C7H16>::New()
);
Info<<"liq 1: " << liqProp() << nl << nl;
}
// Construct Base from Derived
{
autoPtr<liquidProperties> liqProp =
autoPtr<liquidProperties>::NewFrom<C7H16>();
Info<<"liq 2: " << liqProp() << nl << nl;
}
// Construct Base from Derived
{
const autoPtr<liquidProperties> liqProp(autoPtr<C7H16>::New());
Info<<"liq: " << liqProp() << nl << nl;
Info<<"liq-type: " << liqProp->type() << nl << nl;
Info<<"type: " << typeid(liqProp.get()).name() << nl;
}
// Memory transfer
{
Info<< nl << nl;
auto list = autoPtr<labelList>::New(identity(8));
Info<<"forward to function from "
<< flatOutput(*list) << " @ " << name(list->cdata())
<< nl << nl;
testTransfer2(std::move(list));
Info<<"now have valid=" << Switch::name(bool(list));
if (list)
{
Info<< nl
<< flatOutput(*list) << " @ " << name(list->cdata())
<< nl;
}
else
{
Info<< nl;
}
// These should fail to compile
#if 0
label val0 = 0;
if (true)
{
val0 = list;
}
label val1 = 10;
if (val1 == list)
{
}
#endif
}
// Memory transfer
{
Info<< nl << nl;
testTransfer2(autoPtr<labelList>::New(identity(8)));
}
// Memory transfer
{
Info<< nl << nl;
auto list = autoPtr<labelList>::New(identity(8));
Info<<"forward to function from "
<< flatOutput(*list) << " @ " << name(list->cdata())
<< nl << nl;
testTransfer2(std::move(list));
Info<<"now have valid=" << Switch::name(bool(list));
if (list)
{
Info<< nl
<< flatOutput(*list) << " @ " << name(list->cdata())
<< nl;
}
else
{
Info<< nl;
}
}
// Memory transfer
{
auto ptr1 = autoPtr<labelList>::New();
auto ptr2 = autoPtr<labelList>::New();
Info<<"ptr valid: " << bool(ptr1) << nl;
// Refuses to compile (good!): ptr1 = new labelList(10);
// Does compile (good!): ptr1 = nullptr;
ptr1.reset(std::move(ptr2));
autoPtr<labelList> ptr3;
// set() method - deprecated warning?
ptr3.set(ptr1.release());
}
{
// Good this work:
autoPtr<labelList> ptr1 = testNullReturn1<labelList>();
// Good this does not compile:
// autoPtr<labelList> ptr2 = testNullReturn2<labelList>();
}
return 0;
}
// ************************************************************************* //
Test-base64Encoding.C
EXE = $(FOAM_USER_APPBIN)/Test-base64Encoding
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2016 OpenCFD Ltd.
-------------------------------------------------------------------------------
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-base64Encoding
Description
Test base64 encoding layer.
Target values generated with "base64 encode ..." in Google.
A simple independent source for comparison.
\*---------------------------------------------------------------------------*/
#include "base64Layer.H"
#include "List.H"
#include "Pair.H"
#include <sstream>
#include <initializer_list>
using namespace Foam;
bool test(const Pair<string>& unit)
{
const string& input = unit.first();
const string& expected = unit.second();
std::ostringstream os;
base64Layer b64(os);
b64.write(input.data(), input.size());
b64.close();
const string encoded = os.str();
Info<< input << nl;
if (encoded == expected)
{
Info<< " encoded: " << encoded << " (OK)" << nl
<< endl;
return true;
}
else
{
Info<< " encoded: " << encoded << " (ERROR)" << nl
<< " expected: " << expected << nl
<< endl;
return false;
}
}
bool test(std::initializer_list<Pair<string>> list)
{
bool good = true;
for (const Pair<string>& t : list)
{
good = test(t) && good;
}
return good;
}
bool test(const UList<Pair<string>>& list)
{
bool good = true;
for (const Pair<string>& t : list)
{
good = test(t) && good;
}
return good;
}
void testMixed(std::ostream& os, const UList<Pair<string>>& list)
{
base64Layer b64(os);
os << "<test-mixed>" << nl;
int i=0;
for (const Pair<string>& t : list)
{
const string& input = t.first();
os << "<input" << ++i << " value='" << input << "'>" << nl;
os << " ";
b64.write(input.data(), input.size());
b64.close();
os << nl
<< "</input" << i << ">" << nl;
}
os << "</test-mixed>" << nl
<< nl;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
int main(int argc, char * argv[])
{
Info<< "Test base64 encode layer" << nl << endl;
List<Pair<string>> testList
{
{
"abcdef", // 6 input, 8 output
"YWJjZGVm"
},
{
"OpenFOAM",
"T3BlbkZPQU0="
},
{
"OpenFOAM: The Open Source CFD Toolbox",
"T3BlbkZPQU06IFRoZSBPcGVuIFNvdXJjZSBDRkQgVG9vbGJveA=="
}
};
bool good = test(testList);
// Test mixing output
testMixed(std::cout, testList);
if (good)
{
Info<< "All tests passed" << endl;
return 0;
}
else
{
Info<< "One or more tests failed" << endl;
return 1;
}
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Test-bitSet1.C
EXE = $(FOAM_USER_APPBIN)/Test-bitSet1
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2018-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
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-bitSet1
Description
Basic bitSet characteristics
\*---------------------------------------------------------------------------*/
#include "argList.H"
#include "boolList.H"
#include "bitSet.H"
#include "HashSet.H"
#include "ListOps.H"
#include "cpuTime.H"
#include "StringStream.H"
#include "FlatOutput.H"
#include <vector>
#include <unordered_set>
using namespace Foam;
inline Ostream& report
(
const bitSet& bitset,
bool showBits = false,
bool debugOutput = false
)
{
Info<< "size=" << bitset.size() << "/" << bitset.capacity()
<< " count=" << bitset.count()
<< " all:" << bitset.all()
<< " any:" << bitset.any()
<< " none:" << bitset.none() << nl;
Info<< "values: " << flatOutput(bitset) << nl;
if (showBits)
{
bitset.printBits(Info, debugOutput) << nl;
}
return Info;
}
// Create equivalent to flat output
inline void undecorated
(
Ostream& os,
const bitSet& list
)
{
const label len = list.size();
os << token::BEGIN_LIST;
// Contents
for (label i=0; i < len; ++i)
{
if (i) os << token::SPACE;
os << label(list.get(i));
}
os << token::END_LIST;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
int main(int argc, char *argv[])
{
bitSet set1(100);
Info<<"bitSet(label): "; report(set1, true);
bitSet set2(100, { -1, 10, 25, 45});
Info<<"bitSet(label, labels): "; report(set2, true);
bitSet set2b(set2, labelRange(15, 30));
Info<<"bitSet slice(15,30) :"; report(set2b, true);
Info<< "set1 == set2: " << (set2 == set2b) << nl;
Info<< "set1 != set2: " << (set2 != set2b) << nl;
{
FixedList<label, 4> locs({ -1, 3, 4, 12});
bitSet set3a(20, locs);
Info<<"bitSet(FixedList<label>): "; report(set3a, true);
bitSet set3b(locs);
Info<<"bitSet(FixedList<label>): "; report(set3b, true);
set3b.unset(FixedList<label, 3>({ 1, 2, 3}));
Info<<"bitSet unset(FixedList<label>): "; report(set3b, true);
Info<<"bits used: " << flatOutput(set3b.toc()) << nl;
Info<<"inverted: " << flatOutput(invert(set3b)) << nl;
Info<< "Test read/write (ASCII)" << nl;
OStringStream ostr;
undecorated(ostr, set3a); // like flatOutput
ostr << bitSet();
set3a.flip();
undecorated(ostr, set3a); // like flatOutput
{
IStringStream istr(ostr.str());
Info<< "parse: " << istr.str() << nl;
bitSet bset1(istr);
bitSet bset2(istr);
bitSet bset3(istr);
Info<< "got: " << bset1 << nl
<< "and: " << bset2 << nl
<< "and: " << bset3 << nl;
}
}
Info<< "End\n" << endl;
return 0;
}
// ************************************************************************* //
Test-bitSet2.C
EXE = $(FOAM_USER_APPBIN)/Test-bitSet2
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2018-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
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-bitSet2
Description
Test bitSet functionality
\*---------------------------------------------------------------------------*/
#include "uLabel.H"
#include "boolList.H"
#include "DynamicList.H"
#include "IOstreams.H"
#include "ITstream.H"
#include "StringStream.H"
#include "bitSet.H"
#include "FlatOutput.H"
// #define TEST_SFINAE
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
using namespace Foam;
inline Ostream& extent(const bitSet& bitset)
{
Info<< "first: " << bitset.find_first()
<< " last: " << bitset.find_last()
<< " first_not: " << bitset.find_first_not()
<< endl;
return Info;
}
inline Ostream& info(const bitSet& bitset)
{
Info<< "size=" << bitset.size() << "/" << bitset.capacity()
<< " count=" << bitset.count()
<< " !count=" << bitset.count(false)
<< " all:" << bitset.all()
<< " any:" << bitset.any()
<< " none:" << bitset.none() << nl;
return Info;
}
inline Ostream& info(const UList<bool>& bools)
{
Info<< "size=" << bools.size()
<< " count=" << BitOps::count(bools)
<< " !count=" << BitOps::count(bools, false)
<< " all:" << BitOps::all(bools)
<< " any:" << BitOps::any(bools)
<< " none:" << BitOps::none(bools) << nl;
return Info;
}
inline Ostream& report
(
const bitSet& bitset,
bool showBits = false,
bool debugOutput = false
)
{
info(bitset);
Info<< "values: " << flatOutput(bitset) << nl;
if (showBits)
{
bitset.printBits(Info, debugOutput) << nl;
}
return Info;
}
inline Ostream& report(const UList<bool>& bools)
{
info(bools);
return Info;
}
template<class UIntType>
std::string toString(UIntType value, char off='.', char on='1')
{
std::string str(std::numeric_limits<UIntType>::digits, off);
unsigned n = 0;
// Starting from most significant bit - makes for easy reading.
for
(
unsigned test = (1u << (std::numeric_limits<UIntType>::digits-1));
test;
test >>= 1u
)
{
str[n++] = ((value & test) ? on : off);
}
return str;
}
inline bool compare
(
const bitSet& bitset,
const std::string& expected
)
{
const List<unsigned int>& store = bitset.storage();
std::string has;
for (label blocki=0; blocki < bitset.nBlocks(); ++blocki)
{
has += toString(store[blocki]);
}
if (has == expected)
{
Info<< "pass: " << has << nl;
return true;
}
Info<< "fail: " << has << nl;
Info<< "expect: " << expected << nl;
return false;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
Info<< "bitSet::null()" << nl
<< "sizeof : " << sizeof(bitSet::null()) << " bytes" << nl;
info(bitSet::null());
{
bitSet emptySet;
info(emptySet);
extent(emptySet);
emptySet.resize(10);
info(emptySet);
extent(emptySet);
}
Info<< nl << "Tests" << nl;
bitSet list1(22);
// Set every third one on
forAll(list1, i)
{
list1[i] = !(i % 3);
}
Info<< "\nalternating bit pattern\n";
compare(list1, "..........1..1..1..1..1..1..1..1");
// As boolList
{
boolList bools = list1.values();
Info<<"===============" << nl;
Info<<"bools: " << flatOutput(bools) << nl;
for (int i : { -10, 0, 8, 15, 32})
{
Info<< i << " is " << (bools.test(i) ? "set" : "unset")
<< " = " << bools.get(i) << nl;
}
Info<<"bools: " << flatOutput(bools) << nl;
for (int i : { -10, 5, 24})
{
Info<< "set(" << i << ") = "
<< (bools.set(i) ? "true" : "false")
<< nl;
}
Info<<"bools: " << flatOutput(bools) << nl;
for (int i : { -10, 12, 32, 150})
{
Info<< "unset(" << i << ") = "
<< (bools.unset(i) ? "true" : "false")
<< nl;
}
Info<<"bools: " << flatOutput(bools) << nl;
#if 0
boolList bools2(6, false);
Info<<"other bools: " << flatOutput(bools2) << nl;
bools2.set(4);
Info<<"other bools: " << flatOutput(bools2) << nl;
bools2.clear();
bools2.set(3);
bools2.resize(8);
Info<<"other bools: " << flatOutput(bools2) << nl;
#endif
Info<<"===============" << nl;
}
#ifdef TEST_SFINAE
{
labelList labels = list1.toc();
if (labels.test(0))
{
Info<<"no" << endl;
}
List<double*> ptrs(10, nullptr);
if (ptrs.get(0))
{
Info<<"no" << endl;
}
}
#endif
list1.unset(labelRange(13, 20)); // In range
Info<< "\nafter clear [13,..]\n";
compare(list1, "...................1..1..1..1..1");
report(list1, true);
list1.unset(labelRange(40, 20)); // out of range
Info<< "\nafter clear [40,..]\n";
compare(list1, "...................1..1..1..1..1");
report(list1, true);
extent(list1);
Info<< "iterate through:";
for (const label idx : list1)
{
Info<<" " << idx;
}
Info<< nl;
Info<< "\nalternating bit pattern\n";
report(list1, true);
bitSet list2 = ~list1;
Info<< "\nflipped bit pattern\n";
report(list2, true);
extent(list2);
Info<< "\nsparse set\n";
{
bitSet sparse(1000);
sparse.set(300);
info(sparse);
extent(sparse);
sparse.set(0);
info(sparse);
extent(sparse);
}
// set every other on
forAll(list2, i)
{
list2[i] = !(i % 2);
}
Info<< "\nstarting pattern\n";
report(list2, true);
list2.resize(28, false);
list2.resize(34, true);
list2.resize(40, false);
for (label i=0; i < 4; ++i)
{
list2[i] = true;
}
Info<< "\nresized with false, [28,34) true + 6 false, bottom 4 bits true\n";
compare
(
list2,
"1111.......1.1.1.1.1.1.1.1.11111"
"..............................11"
);
report(list2, true);
labelList list2Labels = list2.toc();
Info<< "\noperator|\n";
list1.printBits(Info);
list2.printBits(Info);
Info<< "==\n";
(list1 | list2).printBits(Info);
Info<< "\noperator& : does trim\n";
report((list1 & list2), true);
Info<< "\noperator^\n";
report((list1 ^ list2), true);
Info<< "\noperator|=\n";
{
bitSet list3 = list1;
report((list3 |= list2), true);
}
Info<< "\noperator&=\n";
{
bitSet list3 = list1;
report((list3 &= list2), true);
}
Info<< "\noperator^=\n";
{
bitSet list3 = list1;
report((list3 ^= list2), true);
}
Info<< "\noperator-=\n";
{
bitSet list3 = list1;
report((list3 -= list2), true);
}
bitSet list4
(
ITstream
(
"(1 n 1 n 1 n 1 1 off 0 0 f f 0 y yes y true y false on t)"
)()
);
Info<< "\ntest Istream constructor\n";
report(list4, true);
Info<< "\nclear/assign from labelList\n";
list4.clear();
list4.set(labelList{0, 1, 2, 3, 12, 13, 14, 19, 20, 21});
report(list4, true);
// Not yet:
// bitSet list5{0, 1, 2, 3, 12, 13, 14, 19, 20, 21};
// list5.printInfo(Info, true);
// Info<< list5 << " indices: " << list5.toc() << nl;
Info<< "\nassign from indices\n";
list4.readList
(
IStringStream
(
"{0 1 2 3 12 13 14 19 20 21}"
)()
);
report(list4, true);
compare(list4, "..........111....111........1111");
list4.set(labelRange(28, 6)); // extends size
Info<<"extended\n";
compare
(
list4,
"1111......111....111........1111"
"..............................11"
);
list4.set(labelRange(40, 6)); // extends size
Info<<"extended\n";
compare
(
list4,
"1111......111....111........1111"
"..................111111......11"
);
list4.unset(labelRange(14, 19));
Info<<"cleared [14,33)\n";
compare
(
list4,
"..................11........1111"
"..................111111......1."
);
// Test begin vs find_first etc
{
// Clear some values
list4.unset(labelRange(0, 10));
Info<< nl
<< "Test first vs begin" << nl
<< " values:" << flatOutput(list4.toc()) << nl
<< " first:" << list4.find_first() << nl
<< " begin:" << *(list4.begin()) << nl
<< " begin(0):" << *(list4.begin(0)) << nl;
// With offset
Info<< nl
<< "Test first vs begin" << nl
<< " next(35):" << list4.find_next(35)
<< " begin(35):" << *(list4.begin(35)) << nl
<< " next(40):" << list4.find_next(40)
<< " begin(40):" << *(list4.begin(40)) << nl
<< nl;
}
// Construct from labelUList, labelUIndList
{
DynamicList<label> indices({10, 50, 300});
Info<< "set: " << flatOutput(indices) << endl;
bitSet bools1(indices);
Info<< "used: " << bools1.size() << " "
<< flatOutput(bools1.toc()) << endl;
}
// Check bitSet vs boolList
{
boolList bools(list4.values());
Info<< nl << "Check BitOps on boolList" << nl << nl;
Info<<"bitSet ";
report(list4);
list4.shrink();
Info<<"shrunk ";
report(list4);
Info<< nl;
Info<<"bools ";
report(bools);
bools = false;
Info<<"bools (all unset) ";
report(bools);
bools = true;
Info<<"bools (all set) ";
report(bools);
}
Info<< "\nDone" << nl << endl;
return 0;
}
// ************************************************************************* //
Test-bitops.C
EXE = $(FOAM_USER_APPBIN)/Test-bitops
/* EXE_INC = -I$(LIB_SRC)/finiteVolume/lnInclude */
/* EXE_LIBS = -lfiniteVolume */
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2018 OpenCFD Ltd.
-------------------------------------------------------------------------------
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/>.
Description
Test some bit-operations.
\*---------------------------------------------------------------------------*/
#include "bool.H"
#include "BitOps.H"
#include "IOstreams.H"
#include "stdFoam.H"
#include <algorithm>
#include <type_traits>
#include <limits>
namespace Foam
{
namespace Detail
{
template<typename UIntType, UIntType v, unsigned int n>
struct bitops_setlower
:
std::integral_constant
<
UIntType,
v | (v >> n) | bitops_setlower<UIntType, v | (v >> n), (n >> 1)>::value
>
{};
template<typename UIntType, UIntType v>
struct bitops_setlower<UIntType, v, 1>
:
std::integral_constant<UIntType, v | (v >> 1)>
{};
template<size_t N>
struct bitops_topbit
:
std::integral_constant
<
size_t,
bitops_topbit<(N >> 1)>{} + 1
>
{};
template<> struct bitops_topbit<2> : std::integral_constant<size_t,1> {};
template<> struct bitops_topbit<1> : std::integral_constant<size_t,1> {};
template<> struct bitops_topbit<0> : std::integral_constant<size_t,0> {};
template<typename UIntType, UIntType v>
struct pow2ceil
:
public std::integral_constant
<
UIntType,
bitops_setlower
<
UIntType,
v - 1,
(std::numeric_limits<UIntType>::digits >> 1)
>::value + 1
>
{};
template<size_t N>
struct pow2ceil_shift
:
std::integral_constant
<
size_t,
bitops_topbit<pow2ceil<size_t, N>::value>::value
>
{};
}
}
template<typename UIntType, UIntType v>
struct pow2ceil
:
Foam::Detail::pow2ceil<UIntType,v>
{};
template<size_t N>
struct pow2topbit
:
Foam::Detail::pow2ceil_shift<N>
{};
using namespace Foam;
template<size_t N>
void printTopbit()
{
std::cout
<< "pow2ceil<" << N << "> = "
<< pow2ceil<size_t, N>::value
<< " shift = " << pow2topbit<N>::value << '\n';
}
template<class T, int Offset = 19>
void printOffset()
{
std::cout
<< "pow2ceil of " << typeid(T).name() << " <" << sizeof(T) << "> = "
<< pow2ceil<size_t, sizeof(T)>::value
<< " shift = " << pow2topbit<sizeof(T)>::value << '\n';
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
int main(int argc, char *argv[])
{
Info<<"pow2ceil<10>: "
<< pow2ceil<unsigned, 10>::value << nl;
Info<<"pow2ceil<16>: "
<< pow2ceil<unsigned, 16>::value << nl;
Info<<"pow2ceil<369>: "
<< pow2ceil<unsigned, 369>::value << nl;
printTopbit<0>();
printTopbit<1>();
printTopbit<2>();
printTopbit<3>();
printTopbit<4>();
printTopbit<5>();
printTopbit<6>();
printTopbit<7>();
printTopbit<8>();
printTopbit<9>();
printTopbit<10>();
printTopbit<11>();
printTopbit<15>();
printTopbit<16>();
printTopbit<17>();
printTopbit<18>();
printTopbit<29>();
printTopbit<30>();
printTopbit<31>();
printTopbit<32>();
printTopbit<33>();
printTopbit<4095>();
printTopbit<4096>();
printTopbit<4097>();
printOffset<double>();
Info<<nl << "Test repeat_value" << nl << nl;
Info<< BitOps::bitInfo<unsigned>(BitOps::repeat_value<unsigned, 3>(1u))
<< nl;
Info<< BitOps::bitInfo<unsigned>(BitOps::repeat_value<unsigned, 1>(1u))
<< nl;
Info << "---\nEnd\n" << endl;
return 0;
}
// ************************************************************************* //
Test-boolList.C
EXE = $(FOAM_USER_APPBIN)/Test-boolList
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment