2015-04-02 03:30:20 +02:00
|
|
|
|
/*
|
2016-02-26 22:57:49 +01:00
|
|
|
|
TreeManipulationBinding(Test) - techniques to map generic changes to concrete tree shaped data
|
2015-04-02 03:30:20 +02:00
|
|
|
|
|
|
|
|
|
|
Copyright (C) Lumiera.org
|
2016-02-26 22:57:49 +01:00
|
|
|
|
2016, Hermann Vosseler <Ichthyostega@web.de>
|
2015-04-02 03:30:20 +02:00
|
|
|
|
|
|
|
|
|
|
This program 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 2 of
|
|
|
|
|
|
the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
|
|
This program 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 this program; if not, write to the Free Software
|
|
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
|
|
|
|
|
|
|
* *****************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "lib/test/run.hpp"
|
2015-08-28 23:09:10 +02:00
|
|
|
|
#include "lib/format-util.hpp"
|
2015-04-02 03:30:20 +02:00
|
|
|
|
#include "lib/test/test-helper.hpp"
|
|
|
|
|
|
#include "lib/diff/tree-mutator.hpp"
|
2016-02-27 01:47:33 +01:00
|
|
|
|
#include "lib/diff/test-mutation-target.hpp"
|
2016-02-26 23:45:59 +01:00
|
|
|
|
#include "lib/time/timevalue.hpp"
|
2016-01-07 03:58:29 +01:00
|
|
|
|
#include "lib/format-cout.hpp"
|
2016-03-04 19:23:21 +01:00
|
|
|
|
#include "lib/format-util.hpp"
|
2015-04-02 03:30:20 +02:00
|
|
|
|
#include "lib/util.hpp"
|
|
|
|
|
|
|
|
|
|
|
|
//#include <utility>
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
//#include <vector>
|
|
|
|
|
|
|
2016-03-04 20:55:52 +01:00
|
|
|
|
using util::join;
|
2015-04-02 03:30:20 +02:00
|
|
|
|
using util::isnil;
|
2016-02-27 01:47:33 +01:00
|
|
|
|
using lib::time::Time;
|
2015-04-02 03:30:20 +02:00
|
|
|
|
using std::string;
|
|
|
|
|
|
//using std::vector;
|
|
|
|
|
|
//using std::swap;
|
|
|
|
|
|
|
2016-01-08 08:20:59 +01:00
|
|
|
|
using util::typeStr;
|
settle on a concrete implementation approach based on inheritance chain
After some reconsideration, I decide to stick to the approach with the closures,
but to use a metaprotramming technique to build an inheritance chain.
While I can not decide on the real world impact of storing all those closures,
in theory this approach should enable the compiler to remove all of the
storage overhead. Since, when storing the result into an auto variable
right within scope (as demonstrated in the test), the compiler
sees the concrete type and might be able to boil down the actual
generated virtual function implementations, thereby inlining the
given closures.
Whereas, on the other hand, if we'd go the obvious conventional route
and place the closures into a Map allocated on the stack, I wouldn't
expect the compiler to do data flow analysis to prove this allocation
is not necessary and inline it away.
NOTE: there is now guarantee this inlining trick will ever work.
And, moreover, we don't know anything regarding the runtime effect.
The whole picture is way more involved as it might seem at first sight.
Even if we go the completely conventional route and require every
participating object to supply an implementation of some kind of
"Serializable" interface, we'll end up with a (hand written!)
implementation class for each participating setup, which takes
up space in the code segment of the executable. While the closure
based approach chosen here, consumes data segment (or heap) space
per instance for the functors (or function pointers) representing
the closures, plus code segment space for the closures, but the
latter with a way higher potential for inlining, since the closure
code and the generated virtual functions are necessarily emitted
within the same compilation unit and within a local (inline, not
publickly exposed) scope.
2015-04-05 18:26:49 +02:00
|
|
|
|
|
2015-04-02 03:30:20 +02:00
|
|
|
|
|
|
|
|
|
|
namespace lib {
|
|
|
|
|
|
namespace diff{
|
|
|
|
|
|
namespace test{
|
|
|
|
|
|
|
|
|
|
|
|
// using lumiera::error::LUMIERA_ERROR_LOGIC;
|
|
|
|
|
|
|
2016-02-26 23:45:59 +01:00
|
|
|
|
|
2015-04-02 03:30:20 +02:00
|
|
|
|
namespace {//Test fixture....
|
|
|
|
|
|
|
2016-02-26 23:45:59 +01:00
|
|
|
|
// define some GenNode elements
|
|
|
|
|
|
// to act as templates within the concrete diff
|
|
|
|
|
|
// NOTE: everything in this diff language is by-value
|
|
|
|
|
|
const GenNode ATTRIB1("α", 1), // attribute α = 1
|
|
|
|
|
|
ATTRIB2("β", int64_t(2)), // attribute α = 2L (int64_t)
|
|
|
|
|
|
ATTRIB3("γ", 3.45), // attribute γ = 3.45 (double)
|
|
|
|
|
|
TYPE_X("type", "ξ"), // a "magic" type attribute "Xi"
|
|
|
|
|
|
TYPE_Z("type", "ζ"), //
|
|
|
|
|
|
CHILD_A("a"), // unnamed string child node
|
|
|
|
|
|
CHILD_B('b'), // unnamed char child node
|
|
|
|
|
|
CHILD_T(Time(12,34,56,78)), // unnamed time value child
|
|
|
|
|
|
SUB_NODE = MakeRec().genNode(), // empty anonymous node used to open a sub scope
|
|
|
|
|
|
ATTRIB_NODE = MakeRec().genNode("δ"), // empty named node to be attached as attribute δ
|
|
|
|
|
|
CHILD_NODE = SUB_NODE; // yet another child node, same ID as SUB_NODE (!)
|
2015-04-02 03:30:20 +02:00
|
|
|
|
|
|
|
|
|
|
}//(End)Test fixture
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-26 22:57:49 +01:00
|
|
|
|
/********************************************************************************//**
|
|
|
|
|
|
* @test Building blocks to map generic changes to arbitrary private data structures.
|
|
|
|
|
|
* - use a dummy diagnostic implementation to verify the interface
|
|
|
|
|
|
* - integrate the standard case of tree diff application to `Rec<GenNode>`
|
|
|
|
|
|
* - verify an adapter to apply structure modification to a generic collection
|
|
|
|
|
|
* - use closures to translate mutation into manipulation of private attribues
|
|
|
|
|
|
*
|
2015-04-02 03:30:20 +02:00
|
|
|
|
* @see TreeMutator
|
2016-02-26 22:57:49 +01:00
|
|
|
|
* @see TreeMutator_test
|
|
|
|
|
|
* @see DiffTreeApplication_test
|
2015-04-02 03:30:20 +02:00
|
|
|
|
* @see GenNodeBasic_test
|
2016-02-26 22:57:49 +01:00
|
|
|
|
* @see AbstractTangible_test::mutate()
|
2015-04-02 03:30:20 +02:00
|
|
|
|
*/
|
2016-02-26 22:57:49 +01:00
|
|
|
|
class TreeManipulationBinding_test : public Test
|
2015-04-02 03:30:20 +02:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
|
|
run (Arg)
|
|
|
|
|
|
{
|
2016-02-26 22:57:49 +01:00
|
|
|
|
mutateDummy();
|
|
|
|
|
|
mutateGenNode();
|
|
|
|
|
|
mutateCollection();
|
|
|
|
|
|
mutateAttributeMap();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-03-03 23:11:36 +01:00
|
|
|
|
/** @test diagnostic binding: how to monitor and verify the mutations applied */
|
2016-02-26 22:57:49 +01:00
|
|
|
|
void
|
|
|
|
|
|
mutateDummy()
|
|
|
|
|
|
{
|
2016-03-04 19:23:21 +01:00
|
|
|
|
MARK_TEST_FUN;
|
2016-02-26 23:45:59 +01:00
|
|
|
|
TestMutationTarget target;
|
|
|
|
|
|
auto mutator =
|
|
|
|
|
|
TreeMutator::build()
|
2016-02-27 01:47:33 +01:00
|
|
|
|
.attachDummy (target);
|
2016-02-26 23:45:59 +01:00
|
|
|
|
|
|
|
|
|
|
CHECK (isnil (target));
|
2016-03-04 23:18:25 +01:00
|
|
|
|
CHECK (mutator.emptySrc());
|
2016-02-26 23:45:59 +01:00
|
|
|
|
|
2016-03-03 22:02:01 +01:00
|
|
|
|
mutator.injectNew (ATTRIB1);
|
2016-02-26 23:45:59 +01:00
|
|
|
|
CHECK (!isnil (target));
|
|
|
|
|
|
CHECK (target.contains("α = 1"));
|
2016-03-03 23:52:21 +01:00
|
|
|
|
CHECK (target.verifyEvent("injectNew","α = 1")
|
2016-02-26 23:45:59 +01:00
|
|
|
|
.after("attachMutator"));
|
2016-03-04 19:23:21 +01:00
|
|
|
|
|
|
|
|
|
|
mutator.injectNew (ATTRIB3);
|
|
|
|
|
|
mutator.injectNew (ATTRIB3);
|
|
|
|
|
|
mutator.injectNew (CHILD_B);
|
|
|
|
|
|
mutator.injectNew (CHILD_B);
|
|
|
|
|
|
mutator.injectNew (CHILD_T);
|
|
|
|
|
|
CHECK (target.verify("attachMutator")
|
|
|
|
|
|
.beforeEvent("injectNew","α = 1")
|
|
|
|
|
|
.beforeEvent("injectNew","γ = 3.45")
|
|
|
|
|
|
.beforeEvent("injectNew","γ = 3.45")
|
|
|
|
|
|
.beforeEvent("injectNew","b")
|
|
|
|
|
|
.beforeEvent("injectNew","b")
|
|
|
|
|
|
.beforeEvent("injectNew","78:56:34.012")
|
|
|
|
|
|
);
|
2016-03-04 20:55:52 +01:00
|
|
|
|
CHECK (join(target) == "α = 1, γ = 3.45, γ = 3.45, b, b, 78:56:34.012");
|
2016-03-04 19:23:21 +01:00
|
|
|
|
cout << "Content after population; "
|
2016-03-04 20:55:52 +01:00
|
|
|
|
<< join(target) <<endl;
|
|
|
|
|
|
|
2016-03-04 21:26:25 +01:00
|
|
|
|
|
2016-03-04 20:55:52 +01:00
|
|
|
|
// now attach new mutator for second round...
|
2016-03-04 21:13:49 +01:00
|
|
|
|
auto mutator2 =
|
2016-03-04 20:55:52 +01:00
|
|
|
|
TreeMutator::build()
|
|
|
|
|
|
.attachDummy (target);
|
2016-03-04 23:52:50 +01:00
|
|
|
|
|
|
|
|
|
|
CHECK (target.verify("attachMutator")
|
|
|
|
|
|
.beforeEvent("injectNew","78:56:34.012")
|
|
|
|
|
|
.before("attachMutator"));
|
|
|
|
|
|
|
2016-03-04 21:13:49 +01:00
|
|
|
|
CHECK (isnil (target)); // the "visible" new content is still void
|
2016-03-04 23:18:25 +01:00
|
|
|
|
CHECK (not mutator2.emptySrc()); // content was moved into hiden "src" buffer
|
2016-03-04 23:52:50 +01:00
|
|
|
|
CHECK (join(target.srcIter()) == "α = 1, γ = 3.45, γ = 3.45, b, b, 78:56:34.012");
|
2016-03-04 20:55:52 +01:00
|
|
|
|
|
2016-03-04 23:52:50 +01:00
|
|
|
|
CHECK (mutator2.matchSrc (ATTRIB1)); // current head element of src "matches" the given spec
|
2016-03-04 21:13:49 +01:00
|
|
|
|
CHECK (isnil (target)); // the match didn't change anything
|
2016-03-04 23:52:50 +01:00
|
|
|
|
|
|
|
|
|
|
CHECK (mutator2.findSrc (ATTRIB3)); // serach for an element further down into src... // findSrc
|
2016-03-04 21:13:49 +01:00
|
|
|
|
CHECK (!isnil (target)); // ...pick and accept it into the "visible" part of target
|
2016-03-04 20:55:52 +01:00
|
|
|
|
CHECK (join(target) == "γ = 3.45");
|
|
|
|
|
|
|
2016-03-04 21:13:49 +01:00
|
|
|
|
CHECK (mutator2.matchSrc (ATTRIB1)); // element at head of src is still ATTRIB1 (as before)
|
2016-03-04 23:52:50 +01:00
|
|
|
|
CHECK (mutator2.acceptSrc (ATTRIB1)); // now pick and accept this src element // acceptSrc
|
2016-03-04 20:55:52 +01:00
|
|
|
|
CHECK (join(target) == "γ = 3.45, α = 1");
|
|
|
|
|
|
|
2016-03-04 23:18:25 +01:00
|
|
|
|
CHECK (not mutator2.emptySrc()); // next we have to clean up waste
|
2016-03-04 23:52:50 +01:00
|
|
|
|
mutator2.skipSrc(); // left behind by the findSrc() operation // skipSrc
|
2016-03-04 20:55:52 +01:00
|
|
|
|
CHECK (join(target) == "γ = 3.45, α = 1");
|
|
|
|
|
|
|
2016-03-04 23:52:50 +01:00
|
|
|
|
mutator2.injectNew (ATTRIB2); // injectNew
|
2016-03-04 23:18:25 +01:00
|
|
|
|
CHECK (not mutator2.emptySrc());
|
2016-03-04 21:13:49 +01:00
|
|
|
|
CHECK (mutator2.matchSrc (ATTRIB3));
|
2016-03-04 23:52:50 +01:00
|
|
|
|
CHECK (mutator2.acceptSrc (ATTRIB3)); // acceptSrc
|
2016-03-04 20:55:52 +01:00
|
|
|
|
CHECK (join(target) == "γ = 3.45, α = 1, β = 2, γ = 3.45");
|
|
|
|
|
|
|
|
|
|
|
|
// now proceding with the children.
|
|
|
|
|
|
// NOTE: the TestWireTap / TestMutationTarget does not enforce the attribute / children distinction!
|
2016-03-04 23:18:25 +01:00
|
|
|
|
CHECK (not mutator2.emptySrc());
|
2016-03-04 21:13:49 +01:00
|
|
|
|
CHECK (mutator2.matchSrc (CHILD_B)); // first child waiting in src is CHILD_B
|
2016-03-04 23:52:50 +01:00
|
|
|
|
mutator2.skipSrc(); // ...which will be skipt (and thus discarded) // skipSrc
|
|
|
|
|
|
mutator2.injectNew (SUB_NODE); // inject a new nested sub-structure here // injectNew
|
2016-03-04 21:13:49 +01:00
|
|
|
|
CHECK (mutator2.matchSrc (CHILD_B)); // yet another B-child is waiting
|
|
|
|
|
|
CHECK (not mutator2.findSrc (CHILD_A)); // unsuccessful find operation won't do anything
|
2016-03-04 23:18:25 +01:00
|
|
|
|
CHECK (not mutator2.emptySrc());
|
2016-03-04 21:13:49 +01:00
|
|
|
|
CHECK (mutator2.matchSrc (CHILD_B)); // child B still waiting, unaffected
|
|
|
|
|
|
CHECK (not mutator2.acceptSrc (CHILD_T)); // refusing to accept/pick a non matching element
|
|
|
|
|
|
CHECK (mutator2.matchSrc (CHILD_B)); // child B still patiently waiting, unaffected
|
2016-03-04 23:52:50 +01:00
|
|
|
|
CHECK (mutator2.acceptSrc (CHILD_B)); // acceptSrc
|
2016-03-04 21:13:49 +01:00
|
|
|
|
CHECK (mutator2.matchSrc (CHILD_T));
|
2016-03-04 23:52:50 +01:00
|
|
|
|
CHECK (mutator2.acceptSrc (CHILD_T)); // acceptSrc
|
2016-03-04 23:18:25 +01:00
|
|
|
|
CHECK (mutator2.emptySrc()); // source contents exhausted
|
2016-03-04 21:13:49 +01:00
|
|
|
|
CHECK (not mutator2.acceptSrc (CHILD_T));
|
2016-03-04 23:52:50 +01:00
|
|
|
|
CHECK (target.verify("attachMutator")
|
|
|
|
|
|
.beforeEvent("injectNew","78:56:34.012")
|
|
|
|
|
|
.before("attachMutator")
|
|
|
|
|
|
.beforeEvent("findSrc","γ = 3.45")
|
|
|
|
|
|
.beforeEvent("acceptSrc","α = 1")
|
|
|
|
|
|
.beforeEvent("skipSrc","⟂")
|
|
|
|
|
|
.beforeEvent("injectNew","β = 2")
|
|
|
|
|
|
.beforeEvent("acceptSrc","γ = 3.45")
|
|
|
|
|
|
.beforeEvent("skipSrc","b")
|
|
|
|
|
|
.beforeEvent("injectNew","Rec()")
|
|
|
|
|
|
.beforeEvent("acceptSrc","b")
|
|
|
|
|
|
.beforeEvent("acceptSrc","78:56:34.012")
|
|
|
|
|
|
);
|
|
|
|
|
|
CHECK (join(target.srcIter(), "#") == "###b##"); // we've left back lots of waste, and one abandoned Child "b" (isn't that horrifying?)
|
|
|
|
|
|
CHECK (join(target) == "γ = 3.45, α = 1, β = 2, γ = 3.45, Rec(), b, 78:56:34.012");
|
2016-03-04 20:55:52 +01:00
|
|
|
|
cout << "Content after reordering; "
|
|
|
|
|
|
<< join(target) <<endl;
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-03-04 19:23:21 +01:00
|
|
|
|
cout << "____Mutation-Log______________\n"
|
2016-03-04 20:55:52 +01:00
|
|
|
|
<< join(target.getLog(), "\n")
|
2016-03-04 19:23:21 +01:00
|
|
|
|
<< "\n───╼━━━━━━━━━╾────────────────"<<endl;
|
2015-04-02 03:30:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2016-02-26 22:57:49 +01:00
|
|
|
|
mutateGenNode()
|
2015-04-02 03:30:20 +02:00
|
|
|
|
{
|
2016-02-26 22:57:49 +01:00
|
|
|
|
TODO ("define how to fit GenNode tree mutation into the framework");
|
2015-04-02 03:30:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2016-02-26 22:57:49 +01:00
|
|
|
|
mutateCollection()
|
2015-04-02 03:30:20 +02:00
|
|
|
|
{
|
2016-03-03 23:11:36 +01:00
|
|
|
|
TODO ("define how to map the mutation primitives onto a generic collection");
|
2015-04-02 03:30:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2016-02-26 22:57:49 +01:00
|
|
|
|
mutateAttributeMap ()
|
2015-04-02 03:30:20 +02:00
|
|
|
|
{
|
2016-02-26 22:57:49 +01:00
|
|
|
|
TODO ("define how to translate generic mutation into attribute manipulation");
|
2015-04-02 03:30:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Register this test class... */
|
2016-02-26 22:57:49 +01:00
|
|
|
|
LAUNCHER (TreeManipulationBinding_test, "unit common");
|
2015-04-02 03:30:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}}} // namespace lib::diff::test
|