2015-04-02 03:30:20 +02:00
|
|
|
|
/*
|
2016-05-26 02:16:34 +02:00
|
|
|
|
TreeMutatorBinding(Test) - techniques to map generic changes onto concrete tree shaped data
|
2015-04-02 03:30:20 +02:00
|
|
|
|
|
Copyright: clarify and simplify the file headers
* Lumiera source code always was copyrighted by individual contributors
* there is no entity "Lumiera.org" which holds any copyrights
* Lumiera source code is provided under the GPL Version 2+
== Explanations ==
Lumiera as a whole is distributed under Copyleft, GNU General Public License Version 2 or above.
For this to become legally effective, the ''File COPYING in the root directory is sufficient.''
The licensing header in each file is not strictly necessary, yet considered good practice;
attaching a licence notice increases the likeliness that this information is retained
in case someone extracts individual code files. However, it is not by the presence of some
text, that legally binding licensing terms become effective; rather the fact matters that a
given piece of code was provably copyrighted and published under a license. Even reformatting
the code, renaming some variables or deleting parts of the code will not alter this legal
situation, but rather creates a derivative work, which is likewise covered by the GPL!
The most relevant information in the file header is the notice regarding the
time of the first individual copyright claim. By virtue of this initial copyright,
the first author is entitled to choose the terms of licensing. All further
modifications are permitted and covered by the License. The specific wording
or format of the copyright header is not legally relevant, as long as the
intention to publish under the GPL remains clear. The extended wording was
based on a recommendation by the FSF. It can be shortened, because the full terms
of the license are provided alongside the distribution, in the file COPYING.
2024-11-17 23:42:55 +01:00
|
|
|
|
Copyright (C)
|
|
|
|
|
|
2016, Hermann Vosseler <Ichthyostega@web.de>
|
2015-04-02 03:30:20 +02:00
|
|
|
|
|
Copyright: clarify and simplify the file headers
* Lumiera source code always was copyrighted by individual contributors
* there is no entity "Lumiera.org" which holds any copyrights
* Lumiera source code is provided under the GPL Version 2+
== Explanations ==
Lumiera as a whole is distributed under Copyleft, GNU General Public License Version 2 or above.
For this to become legally effective, the ''File COPYING in the root directory is sufficient.''
The licensing header in each file is not strictly necessary, yet considered good practice;
attaching a licence notice increases the likeliness that this information is retained
in case someone extracts individual code files. However, it is not by the presence of some
text, that legally binding licensing terms become effective; rather the fact matters that a
given piece of code was provably copyrighted and published under a license. Even reformatting
the code, renaming some variables or deleting parts of the code will not alter this legal
situation, but rather creates a derivative work, which is likewise covered by the GPL!
The most relevant information in the file header is the notice regarding the
time of the first individual copyright claim. By virtue of this initial copyright,
the first author is entitled to choose the terms of licensing. All further
modifications are permitted and covered by the License. The specific wording
or format of the copyright header is not legally relevant, as long as the
intention to publish under the GPL remains clear. The extended wording was
based on a recommendation by the FSF. It can be shortened, because the full terms
of the license are provided alongside the distribution, in the file COPYING.
2024-11-17 23:42:55 +01:00
|
|
|
|
**Lumiera** 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. See the file COPYING for further details.
|
2015-04-02 03:30:20 +02:00
|
|
|
|
|
Copyright: clarify and simplify the file headers
* Lumiera source code always was copyrighted by individual contributors
* there is no entity "Lumiera.org" which holds any copyrights
* Lumiera source code is provided under the GPL Version 2+
== Explanations ==
Lumiera as a whole is distributed under Copyleft, GNU General Public License Version 2 or above.
For this to become legally effective, the ''File COPYING in the root directory is sufficient.''
The licensing header in each file is not strictly necessary, yet considered good practice;
attaching a licence notice increases the likeliness that this information is retained
in case someone extracts individual code files. However, it is not by the presence of some
text, that legally binding licensing terms become effective; rather the fact matters that a
given piece of code was provably copyrighted and published under a license. Even reformatting
the code, renaming some variables or deleting parts of the code will not alter this legal
situation, but rather creates a derivative work, which is likewise covered by the GPL!
The most relevant information in the file header is the notice regarding the
time of the first individual copyright claim. By virtue of this initial copyright,
the first author is entitled to choose the terms of licensing. All further
modifications are permitted and covered by the License. The specific wording
or format of the copyright header is not legally relevant, as long as the
intention to publish under the GPL remains clear. The extended wording was
based on a recommendation by the FSF. It can be shortened, because the full terms
of the license are provided alongside the distribution, in the file COPYING.
2024-11-17 23:42:55 +01:00
|
|
|
|
* *****************************************************************/
|
2015-04-02 03:30:20 +02:00
|
|
|
|
|
2017-02-22 01:54:20 +01:00
|
|
|
|
/** @file tree-mutator-binding-test.cpp
|
2017-02-22 03:17:18 +01:00
|
|
|
|
** unit test \ref TreeMutatorBinding_test
|
2016-11-03 18:20:10 +01:00
|
|
|
|
*/
|
|
|
|
|
|
|
2015-04-02 03:30:20 +02:00
|
|
|
|
|
|
|
|
|
|
#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-03-25 23:12:54 +01:00
|
|
|
|
#include "lib/iter-adapter-stl.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"
|
2016-03-06 03:55:31 +01:00
|
|
|
|
#include "lib/error.hpp"
|
2015-04-02 03:30:20 +02:00
|
|
|
|
#include "lib/util.hpp"
|
|
|
|
|
|
|
2016-06-09 01:10:52 +02:00
|
|
|
|
#include <vector>
|
2015-04-02 03:30:20 +02:00
|
|
|
|
#include <string>
|
|
|
|
|
|
|
2016-03-04 20:55:52 +01:00
|
|
|
|
using util::join;
|
2015-04-02 03:30:20 +02:00
|
|
|
|
using util::isnil;
|
2016-03-11 17:39:25 +01:00
|
|
|
|
using util::contains;
|
2016-03-25 23:12:54 +01:00
|
|
|
|
using util::stringify;
|
|
|
|
|
|
using lib::iter_stl::eachElm;
|
2016-02-27 01:47:33 +01:00
|
|
|
|
using lib::time::Time;
|
2015-04-02 03:30:20 +02:00
|
|
|
|
using std::string;
|
2016-03-25 23:12:54 +01:00
|
|
|
|
|
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{
|
|
|
|
|
|
|
2024-03-16 02:04:47 +01:00
|
|
|
|
using LERR_(LOGIC);
|
2015-04-02 03:30:20 +02:00
|
|
|
|
|
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
|
2016-04-16 02:20:23 +02:00
|
|
|
|
SUB_NODE = MakeRec().genNode(), // empty anonymous node used to open a sub scope
|
2016-02-26 23:45:59 +01:00
|
|
|
|
ATTRIB_NODE = MakeRec().genNode("δ"), // empty named node to be attached as attribute δ
|
2016-06-10 04:30:02 +02:00
|
|
|
|
GAMMA_PI("γ", 3.14159265); // happens to have the same identity (ID) as ATTRIB3
|
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
|
|
|
|
|
|
* - verify an adapter to apply structure modification to a generic collection
|
2016-03-25 21:40:30 +01:00
|
|
|
|
* - use closures to translate mutation into manipulation of private attributes
|
2016-09-04 20:55:21 +02:00
|
|
|
|
* - integrate the standard case of tree diff application to `Rec<GenNode>`
|
2016-05-26 01:56:13 +02:00
|
|
|
|
*
|
|
|
|
|
|
* @remark even while this is a very long and detail oriented test, it barely
|
|
|
|
|
|
* scratches the surface of what is possible with _layering multiple bindings_
|
|
|
|
|
|
* on top of each other. In fact, what follows are several self contained tests,
|
|
|
|
|
|
* each performing roughly the same scenario, yet targeted at different local
|
|
|
|
|
|
* data structures through appropriate special bindings given as lambda.
|
|
|
|
|
|
* @remark _you should note_ that the scenario executed in each of these tests
|
|
|
|
|
|
* precisely corresponds to the application of the test diff used in
|
2016-08-29 17:52:35 +02:00
|
|
|
|
* (\ref DiffComplexApplication_test)
|
2016-05-26 01:56:13 +02:00
|
|
|
|
* @remark _to help with understanding this,_ please consider how diff application is
|
|
|
|
|
|
* actually implemented on top of a set of "primitives". The TreeMutator interface
|
|
|
|
|
|
* on the other hand offers precisely these building blocks necessary to implement
|
|
|
|
|
|
* diff application to an arbitrary hierarchical data structure. In this way, the
|
|
|
|
|
|
* following test cases demonstrate the intermediary steps executed when applying
|
|
|
|
|
|
* this test diff through the concrete binding exemplified in each case
|
2016-06-09 01:10:52 +02:00
|
|
|
|
* @remark the **test diff** implied here reads as follows
|
2016-05-26 01:56:13 +02:00
|
|
|
|
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
* ins(ATTRIB1)
|
|
|
|
|
|
* ins(ATTRIB3)
|
|
|
|
|
|
* ins(ATTRIB3)
|
|
|
|
|
|
* ins(CHILD_B)
|
|
|
|
|
|
* ins(CHILD_B)
|
|
|
|
|
|
* ins(CHILD_T)
|
|
|
|
|
|
* // ==> ATTRIB1, ATTRIB3, ATTRIB3, CHILD_B, CHILD_B, CHILD_T
|
2016-08-13 18:05:15 +02:00
|
|
|
|
* after(Ref::ATTRIBS)
|
2016-05-26 01:56:13 +02:00
|
|
|
|
* ins(ATTRIB2)
|
|
|
|
|
|
* del(CHILD_B)
|
|
|
|
|
|
* ins(SUB_NODE)
|
2016-08-13 18:05:15 +02:00
|
|
|
|
* find(CHILD_T)
|
2016-05-26 01:56:13 +02:00
|
|
|
|
* pick(CHILD_B)
|
2016-08-13 18:05:15 +02:00
|
|
|
|
* skip(CHILD_T)
|
|
|
|
|
|
* // ==> ATTRIB1, ATTRIB3, (ATTRIB3), ATTRIB2, SUB_NODE, CHILD_T, CHILD_B
|
2016-06-10 03:19:33 +02:00
|
|
|
|
* after(CHILD_B)
|
2016-05-26 01:56:13 +02:00
|
|
|
|
* after(Ref::END)
|
2016-06-10 03:19:33 +02:00
|
|
|
|
* set(GAMMA_PI)
|
2016-05-26 01:56:13 +02:00
|
|
|
|
* mut(SUB_NODE)
|
|
|
|
|
|
* ins(TYPE_X)
|
|
|
|
|
|
* ins(ATTRIB2)
|
|
|
|
|
|
* ins(CHILD_B)
|
|
|
|
|
|
* ins(CHILD_A)
|
|
|
|
|
|
* emu(SUB_NODE)
|
|
|
|
|
|
* ins(ATTRIB_NODE)
|
|
|
|
|
|
* mut(ATTRIB_NODE)
|
|
|
|
|
|
* ins(TYPE_Z)
|
|
|
|
|
|
* ins(CHILD_A)
|
|
|
|
|
|
* ins(CHILD_A)
|
|
|
|
|
|
* ins(CHILD_A)
|
|
|
|
|
|
* emu(ATTRIB_NODE)
|
2016-08-13 18:05:15 +02:00
|
|
|
|
* // ==> ATTRIB1, ATTRIB3 := π, (ATTRIB3), ATTRIB2,
|
2016-05-26 01:56:13 +02:00
|
|
|
|
* // ATTRIB_NODE{ type ζ, CHILD_A, CHILD_A, CHILD_A }
|
2016-08-13 18:05:15 +02:00
|
|
|
|
* // SUB_NODE{ type ξ, ATTRIB2, CHILD_B, CHILD_A },
|
|
|
|
|
|
* // CHILD_T, CHILD_B
|
2016-05-26 01:56:13 +02:00
|
|
|
|
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2016-02-26 22:57:49 +01:00
|
|
|
|
*
|
2015-04-02 03:30:20 +02:00
|
|
|
|
* @see TreeMutator
|
2016-02-26 22:57:49 +01:00
|
|
|
|
* @see TreeMutator_test
|
|
|
|
|
|
* @see DiffTreeApplication_test
|
2019-12-12 23:41:26 +01:00
|
|
|
|
* @see GenNode_test
|
2016-02-26 22:57:49 +01:00
|
|
|
|
* @see AbstractTangible_test::mutate()
|
2015-04-02 03:30:20 +02:00
|
|
|
|
*/
|
2016-05-26 02:16:34 +02:00
|
|
|
|
class TreeMutatorBinding_test : public Test
|
2015-04-02 03:30:20 +02:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
|
|
run (Arg)
|
|
|
|
|
|
{
|
2016-02-26 22:57:49 +01:00
|
|
|
|
mutateDummy();
|
|
|
|
|
|
mutateCollection();
|
2016-05-26 01:56:13 +02:00
|
|
|
|
mutateAttribute();
|
|
|
|
|
|
mutateGenNode();
|
2016-02-26 22:57:49 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
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-10-03 23:54:09 +02:00
|
|
|
|
mutator.init();
|
2016-02-26 23:45:59 +01:00
|
|
|
|
|
|
|
|
|
|
CHECK (isnil (target));
|
2016-05-24 21:34:08 +02:00
|
|
|
|
CHECK (not mutator.hasSrc());
|
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));
|
2016-03-11 17:39:25 +01:00
|
|
|
|
CHECK (contains(target.showContent(), "α = 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);
|
2016-05-24 22:23:06 +02:00
|
|
|
|
CHECK (mutator.completeScope());
|
2016-03-04 19:23:21 +01:00
|
|
|
|
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-05-24 22:23:06 +02:00
|
|
|
|
.beforeEvent("completeScope","scope completed")
|
2016-03-04 19:23:21 +01:00
|
|
|
|
);
|
2016-03-11 17:39:25 +01:00
|
|
|
|
CHECK (target.showContent() == "α = 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-11 17:39:25 +01:00
|
|
|
|
<< target.showContent() <<endl;
|
2016-03-04 20:55:52 +01:00
|
|
|
|
|
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-10-03 23:54:09 +02:00
|
|
|
|
mutator2.init();
|
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-05-24 21:34:08 +02:00
|
|
|
|
CHECK (mutator2.hasSrc()); // content was moved into hidden "src" buffer
|
2016-03-11 17:39:25 +01:00
|
|
|
|
CHECK (target.showSrcBuffer() == "α = 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
|
|
|
|
|
2016-08-13 18:05:15 +02:00
|
|
|
|
CHECK (mutator2.accept_until(Ref::ATTRIBS)); // accept_until
|
|
|
|
|
|
CHECK (mutator2.hasSrc());
|
|
|
|
|
|
CHECK (!isnil (target)); // the fast forward did accept some entries
|
|
|
|
|
|
CHECK (target.showContent() == "α = 1, γ = 3.45, γ = 3.45");
|
|
|
|
|
|
CHECK (mutator2.matchSrc (CHILD_B)); // ...and we're located behind the attributes, at first child
|
2016-03-04 20:55:52 +01:00
|
|
|
|
|
2016-03-04 23:52:50 +01:00
|
|
|
|
mutator2.injectNew (ATTRIB2); // injectNew
|
2016-08-13 18:05:15 +02:00
|
|
|
|
CHECK (target.showContent() == "α = 1, γ = 3.45, γ = 3.45, β = 2");
|
2016-03-04 20:55:52 +01:00
|
|
|
|
|
2016-03-19 01:42:27 +01:00
|
|
|
|
// now proceeding with the children.
|
2016-03-04 20:55:52 +01:00
|
|
|
|
// NOTE: the TestWireTap / TestMutationTarget does not enforce the attribute / children distinction!
|
2016-08-13 18:05:15 +02:00
|
|
|
|
CHECK (mutator2.hasSrc()); // still located behind the attributes...
|
2016-03-04 21:13:49 +01:00
|
|
|
|
CHECK (mutator2.matchSrc (CHILD_B)); // first child waiting in src is CHILD_B
|
2016-08-07 01:58:26 +02:00
|
|
|
|
mutator2.skipSrc (CHILD_B); // ...which will be skipped (and thus discarded) // skipSrc
|
2016-03-04 23:52:50 +01:00
|
|
|
|
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-05-24 21:34:08 +02:00
|
|
|
|
CHECK (mutator2.hasSrc());
|
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-08-13 18:05:15 +02:00
|
|
|
|
CHECK (mutator2.hasSrc());
|
|
|
|
|
|
CHECK (mutator2.findSrc (CHILD_T)); // search for an element further down into src... // findSrc
|
|
|
|
|
|
CHECK (!isnil (target)); // ...pick and accept it into the "visible" part of target
|
|
|
|
|
|
CHECK (target.showContent() == "α = 1, γ = 3.45, γ = 3.45, β = 2, Rec(), 78:56:34.012");
|
|
|
|
|
|
|
|
|
|
|
|
CHECK (mutator2.matchSrc (CHILD_B)); // element at head of src is still CHILD_B (as before)
|
|
|
|
|
|
CHECK (mutator2.acceptSrc (CHILD_B)); // now pick and accept this src element as child // acceptSrc
|
|
|
|
|
|
|
|
|
|
|
|
CHECK (mutator2.hasSrc()); // next we have to clean up waste
|
|
|
|
|
|
mutator2.skipSrc (CHILD_T); // left behind by the findSrc() operation // skipSrc
|
|
|
|
|
|
CHECK (target.showContent() == "α = 1, γ = 3.45, γ = 3.45, β = 2, Rec(), 78:56:34.012, b");
|
2016-05-24 21:34:08 +02:00
|
|
|
|
CHECK (not mutator2.hasSrc()); // source contents exhausted
|
2016-03-04 21:13:49 +01:00
|
|
|
|
CHECK (not mutator2.acceptSrc (CHILD_T));
|
2016-05-24 22:23:06 +02:00
|
|
|
|
CHECK (mutator2.completeScope()); // no pending elements left, everything resolved
|
2016-03-04 23:52:50 +01:00
|
|
|
|
CHECK (target.verify("attachMutator")
|
|
|
|
|
|
.beforeEvent("injectNew","78:56:34.012")
|
|
|
|
|
|
.before("attachMutator")
|
2016-08-13 18:05:15 +02:00
|
|
|
|
.beforeEvent("accept_until after ATTRIBS","α = 1")
|
|
|
|
|
|
.beforeEvent("accept_until after ATTRIBS","γ = 3.45")
|
|
|
|
|
|
.beforeEvent("accept_until after ATTRIBS","γ = 3.45")
|
2016-03-04 23:52:50 +01:00
|
|
|
|
.beforeEvent("injectNew","β = 2")
|
|
|
|
|
|
.beforeEvent("skipSrc","b")
|
|
|
|
|
|
.beforeEvent("injectNew","Rec()")
|
2016-08-13 18:05:15 +02:00
|
|
|
|
.beforeEvent("findSrc","78:56:34.012")
|
2016-03-04 23:52:50 +01:00
|
|
|
|
.beforeEvent("acceptSrc","b")
|
2016-08-13 18:05:15 +02:00
|
|
|
|
.beforeEvent("skipSrc","⟂")
|
|
|
|
|
|
.beforeEvent("completeScope","scope completed / 6 waste elm(s)")
|
2016-03-04 23:52:50 +01:00
|
|
|
|
);
|
2016-08-13 18:05:15 +02:00
|
|
|
|
CHECK (target.showContent() == "α = 1, γ = 3.45, γ = 3.45, β = 2, Rec(), 78:56:34.012, b");
|
2016-03-04 20:55:52 +01:00
|
|
|
|
cout << "Content after reordering; "
|
2016-03-11 17:39:25 +01:00
|
|
|
|
<< target.showContent() <<endl;
|
2016-03-04 20:55:52 +01:00
|
|
|
|
|
|
|
|
|
|
|
2016-03-06 03:55:31 +01:00
|
|
|
|
|
|
|
|
|
|
// the third round will cover tree mutation primitives...
|
|
|
|
|
|
auto mutator3 =
|
|
|
|
|
|
TreeMutator::build()
|
|
|
|
|
|
.attachDummy (target);
|
2016-10-03 23:54:09 +02:00
|
|
|
|
mutator3.init();
|
2016-03-06 03:55:31 +01:00
|
|
|
|
|
2016-06-10 03:19:33 +02:00
|
|
|
|
// the first thing we try out is how to navigate through the sequence partially
|
2016-03-06 03:55:31 +01:00
|
|
|
|
CHECK (isnil (target));
|
2016-08-13 18:05:15 +02:00
|
|
|
|
CHECK (mutator3.matchSrc (ATTRIB1)); // new mutator starts out anew at the beginning
|
|
|
|
|
|
CHECK (mutator3.accept_until (CHILD_T)); // fast forward behind the second-last child (CHILD_T) // accept_until
|
|
|
|
|
|
CHECK (mutator3.matchSrc (CHILD_B)); // this /would/ be the next source element...
|
|
|
|
|
|
CHECK (not mutator3.completeScope()); // CHILD_B is still pending, not done yet...
|
2016-06-10 03:19:33 +02:00
|
|
|
|
CHECK (mutator3.accept_until (Ref::END)); // fast forward, since we do not want to re-order anything // accept_until
|
|
|
|
|
|
CHECK ( mutator3.completeScope()); // now any pending elements where default-resolved
|
2016-03-06 03:55:31 +01:00
|
|
|
|
|
2016-06-10 03:19:33 +02:00
|
|
|
|
// next thing will be an assignment to some element by ID
|
2016-03-11 21:30:25 +01:00
|
|
|
|
CHECK (not contains(target.showContent(), "γ = 3.1415927"));
|
2016-05-26 01:56:13 +02:00
|
|
|
|
CHECK (mutator3.assignElm(GAMMA_PI)); // ...we assign a new payload to the current element first // assignElm
|
2016-03-11 21:30:25 +01:00
|
|
|
|
CHECK ( contains(target.showContent(), "γ = 3.1415927"));
|
2016-05-24 22:23:06 +02:00
|
|
|
|
CHECK ( mutator3.completeScope()); // now any pending elements where default-resolved
|
2016-03-11 21:30:25 +01:00
|
|
|
|
cout << "Content after assignment; "
|
|
|
|
|
|
<< target.showContent() <<endl;
|
2016-03-06 03:55:31 +01:00
|
|
|
|
|
|
|
|
|
|
// for mutation of an enclosed scope, in real usage the managing TreeDiffInterpreter
|
|
|
|
|
|
// would maintain a stack of "mutation frames", where each one provides an OpaqueHolder
|
|
|
|
|
|
// to place a suitable sub-mutator for this nested scope. At this point, we can't get any further
|
2016-03-11 21:30:25 +01:00
|
|
|
|
// with this TestWireTap / TestMutationTarget approach, since the latter just records actions and
|
|
|
|
|
|
// otherwise forwards operation to the rest of the TreeMutator. In case there is no /real/ mutator
|
2016-03-06 03:55:31 +01:00
|
|
|
|
// in any "onion layer" below the TestWireTap within this TreeMutator, we'll just get a default (NOP)
|
|
|
|
|
|
// implementation of TreeMutator without any further functionality.
|
|
|
|
|
|
|
|
|
|
|
|
InPlaceBuffer<TreeMutator, sizeof(mutator3)> subMutatorBuffer;
|
2016-06-09 01:18:21 +02:00
|
|
|
|
TreeMutator::Handle placementHandle(subMutatorBuffer);
|
2016-03-06 03:55:31 +01:00
|
|
|
|
|
2016-05-26 01:56:13 +02:00
|
|
|
|
CHECK (mutator3.mutateChild (SUB_NODE, placementHandle)); // mutateChild
|
2016-05-24 21:34:08 +02:00
|
|
|
|
CHECK (not subMutatorBuffer->hasSrc()); // ...this is all we can do here
|
2016-03-06 03:55:31 +01:00
|
|
|
|
// the real implementation would instead find a suitable
|
|
|
|
|
|
// sub-mutator within this buffer and recurse into that.
|
|
|
|
|
|
|
|
|
|
|
|
// error handling: assignment might throw
|
|
|
|
|
|
GenNode differentTime{CHILD_T.idi.getSym(), Time(11,22)};
|
|
|
|
|
|
VERIFY_ERROR (LOGIC, mutator3.assignElm (differentTime));
|
|
|
|
|
|
|
2016-08-13 18:05:15 +02:00
|
|
|
|
CHECK (target.showContent() == "α = 1, γ = 3.1415927, γ = 3.45, β = 2, Rec(), 78:56:34.012, b");
|
|
|
|
|
|
CHECK (target.verifyEvent("findSrc","78:56:34.012")
|
|
|
|
|
|
.beforeEvent("acceptSrc","b")
|
2016-03-11 21:30:25 +01:00
|
|
|
|
.before("attachMutator TestWireTap")
|
2016-08-13 18:05:15 +02:00
|
|
|
|
.beforeEvent("accept_until _CHILD_Time.","α = 1")
|
|
|
|
|
|
.beforeEvent("accept_until _CHILD_Time.","γ = 3.45")
|
|
|
|
|
|
.beforeEvent("accept_until _CHILD_Time.","γ = 3.45")
|
|
|
|
|
|
.beforeEvent("accept_until _CHILD_Time.","β = 2")
|
|
|
|
|
|
.beforeEvent("accept_until _CHILD_Time.","Rec()")
|
|
|
|
|
|
.beforeEvent("accept_until _CHILD_Time.","78:56:34.012")
|
2016-05-24 22:23:06 +02:00
|
|
|
|
.beforeEvent("completeScope","scope NOT completed")
|
2016-08-13 18:05:15 +02:00
|
|
|
|
.beforeEvent("accept_until END","b")
|
|
|
|
|
|
.beforeEvent("completeScope","scope completed / 7 waste elm(s)")
|
2016-06-10 03:19:33 +02:00
|
|
|
|
.beforeEvent("assignElm","γ: 3.45 ⤅ 3.1415927")
|
2016-08-13 18:05:15 +02:00
|
|
|
|
.beforeEvent("completeScope","scope completed / 7 waste elm(s)")
|
2016-06-10 03:19:33 +02:00
|
|
|
|
.beforeEvent("mutateChild","start mutation...Rec()")
|
2016-03-11 21:30:25 +01:00
|
|
|
|
);
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-03-26 01:22:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @test map mutation primitives onto a STL collection managed locally.
|
|
|
|
|
|
* - we perform _literally_ the same diff steps as in mutateDummy()
|
|
|
|
|
|
* - but now we have a completely opaque implementation data structure,
|
|
|
|
|
|
* where even the data type is unknown beyond this functions's scope.
|
|
|
|
|
|
* - thus we build a custom mutator, installing lambdas to tie into this
|
|
|
|
|
|
* local data structure, without disclosing any details. In fact we even
|
|
|
|
|
|
* install different lambdas on each usage cycle, according to the specific
|
|
|
|
|
|
* mutation operations to perform. Of course, it would be pointless to do so
|
|
|
|
|
|
* in real world usage, yet nicely demonstrates the point that the implementation
|
|
|
|
|
|
* really remains in control about anything regarding its private data structure.
|
|
|
|
|
|
* - and still, by exposing such a custom configured mutator, this private structure
|
|
|
|
|
|
* can be populated, reordered and even altered recursively, by generic instructions.
|
|
|
|
|
|
*/
|
2015-04-02 03:30:20 +02:00
|
|
|
|
void
|
2016-03-19 01:42:27 +01:00
|
|
|
|
mutateCollection()
|
2015-04-02 03:30:20 +02:00
|
|
|
|
{
|
2016-03-19 16:47:40 +01:00
|
|
|
|
MARK_TEST_FUN;
|
|
|
|
|
|
|
2016-03-26 01:22:40 +01:00
|
|
|
|
// private data structures to be mutated
|
2016-03-19 16:47:40 +01:00
|
|
|
|
struct Data
|
|
|
|
|
|
{
|
|
|
|
|
|
string key;
|
|
|
|
|
|
string val;
|
|
|
|
|
|
|
2016-03-25 03:12:02 +01:00
|
|
|
|
operator string() const { return _Fmt{"≺%s∣%s≻"} % key % val; }
|
2016-03-19 16:47:40 +01:00
|
|
|
|
bool operator== (Data const& o) const { return key==o.key and val==o.val; }
|
|
|
|
|
|
bool operator!= (Data const& o) const { return not (*this == o); }
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
using VecD = std::vector<Data>;
|
|
|
|
|
|
using MapD = std::map<string, VecD>;
|
|
|
|
|
|
|
|
|
|
|
|
VecD target;
|
2016-04-16 02:20:23 +02:00
|
|
|
|
MapD subScopes;
|
2016-03-19 16:47:40 +01:00
|
|
|
|
|
|
|
|
|
|
// now set up a binding to these opaque private structures...
|
2016-04-17 04:51:19 +02:00
|
|
|
|
auto mutator1 =
|
2016-03-19 16:47:40 +01:00
|
|
|
|
TreeMutator::build()
|
2016-03-19 17:09:16 +01:00
|
|
|
|
.attach (collection(target)
|
|
|
|
|
|
.constructFrom ([&](GenNode const& spec) -> Data
|
|
|
|
|
|
{
|
|
|
|
|
|
cout << "constructor invoked on "<<spec<<endl;
|
|
|
|
|
|
return {spec.idi.getSym(), render(spec.data)};
|
2016-03-26 01:22:40 +01:00
|
|
|
|
}));
|
2016-03-25 02:51:56 +01:00
|
|
|
|
|
2016-04-17 04:51:19 +02:00
|
|
|
|
CHECK (sizeof(mutator1) <= sizeof(VecD) // the buffer for pending elements
|
|
|
|
|
|
+ sizeof(VecD*) // the reference to the original collection
|
|
|
|
|
|
+ 2 * sizeof(VecD::iterator) // one Lumiera RangeIter (comprised of pos and end iterators)
|
|
|
|
|
|
+ 4 * sizeof(void*) // the four unused default configured binding functions
|
2016-09-02 20:38:14 +02:00
|
|
|
|
+ 1 * sizeof(void*) // one back reference from the closure to this scope
|
|
|
|
|
|
+ sizeof(void*)); // the TreeMutator VTable
|
2016-03-25 03:12:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// --- first round: populate the collection ---
|
|
|
|
|
|
|
2016-10-03 23:54:09 +02:00
|
|
|
|
mutator1.init();
|
|
|
|
|
|
|
2016-03-25 03:12:02 +01:00
|
|
|
|
CHECK (isnil (target));
|
2016-05-24 21:34:08 +02:00
|
|
|
|
CHECK (not mutator1.hasSrc());
|
2016-03-25 03:12:02 +01:00
|
|
|
|
|
2016-04-17 04:51:19 +02:00
|
|
|
|
mutator1.injectNew (ATTRIB1);
|
2016-03-25 03:12:02 +01:00
|
|
|
|
CHECK (!isnil (target));
|
|
|
|
|
|
CHECK (contains(join(target), "≺α∣1≻"));
|
|
|
|
|
|
|
2016-04-17 04:51:19 +02:00
|
|
|
|
mutator1.injectNew (ATTRIB3);
|
|
|
|
|
|
mutator1.injectNew (ATTRIB3);
|
|
|
|
|
|
mutator1.injectNew (CHILD_B);
|
|
|
|
|
|
mutator1.injectNew (CHILD_B);
|
|
|
|
|
|
mutator1.injectNew (CHILD_T);
|
2016-05-24 22:23:06 +02:00
|
|
|
|
CHECK (mutator1.completeScope());
|
2016-03-25 03:12:02 +01:00
|
|
|
|
|
2016-03-25 23:12:54 +01:00
|
|
|
|
auto contents = stringify(eachElm(target));
|
|
|
|
|
|
CHECK ("≺α∣1≻" == *contents);
|
|
|
|
|
|
++contents;
|
|
|
|
|
|
CHECK ("≺γ∣3.45≻" == *contents);
|
|
|
|
|
|
++contents;
|
|
|
|
|
|
CHECK ("≺γ∣3.45≻" == *contents);
|
|
|
|
|
|
++contents;
|
|
|
|
|
|
CHECK (contains(*contents, "∣b≻"));
|
|
|
|
|
|
++contents;
|
|
|
|
|
|
CHECK (contains(*contents, "∣b≻"));
|
|
|
|
|
|
++contents;
|
|
|
|
|
|
CHECK (contains(*contents, "∣78:56:34.012≻"));
|
|
|
|
|
|
++contents;
|
|
|
|
|
|
CHECK (isnil (contents));
|
|
|
|
|
|
|
|
|
|
|
|
cout << "injected......" << join(target) <<endl;
|
2016-03-25 23:45:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// --- second round: reorder the collection ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Mutators are one-time disposable objects,
|
|
|
|
|
|
// thus we'll have to build a new one for the second round...
|
|
|
|
|
|
auto mutator2 =
|
|
|
|
|
|
TreeMutator::build()
|
|
|
|
|
|
.attach (collection(target)
|
|
|
|
|
|
.constructFrom ([&](GenNode const& spec) -> Data
|
|
|
|
|
|
{
|
|
|
|
|
|
cout << "constructor invoked on "<<spec<<endl;
|
|
|
|
|
|
return {spec.idi.getSym(), render(spec.data)};
|
|
|
|
|
|
})
|
|
|
|
|
|
.matchElement ([&](GenNode const& spec, Data const& elm)
|
|
|
|
|
|
{
|
|
|
|
|
|
cout << "match? "<<spec.idi.getSym()<<"=?="<<elm.key<<endl;
|
|
|
|
|
|
return spec.idi.getSym() == elm.key;
|
|
|
|
|
|
}));
|
|
|
|
|
|
|
2016-03-26 01:22:40 +01:00
|
|
|
|
// we have two lambdas now and thus can save on the size of one function pointer....
|
2016-04-17 04:51:19 +02:00
|
|
|
|
CHECK (sizeof(mutator1) - sizeof(mutator2) == sizeof(void*));
|
2016-03-26 01:22:40 +01:00
|
|
|
|
|
2016-10-03 23:54:09 +02:00
|
|
|
|
mutator2.init();
|
2016-03-25 23:45:32 +01:00
|
|
|
|
CHECK (isnil (target)); // the "visible" new content is still void
|
|
|
|
|
|
|
|
|
|
|
|
CHECK (mutator2.matchSrc (ATTRIB1)); // current head element of src "matches" the given spec
|
|
|
|
|
|
CHECK (isnil (target)); // the match didn't change anything
|
|
|
|
|
|
|
2016-09-02 18:40:16 +02:00
|
|
|
|
CHECK (mutator2.accept_until(Ref::ATTRIBS));
|
|
|
|
|
|
CHECK (mutator2.matchSrc (ATTRIB1)); // NOTE: collection values can be anything; thus this
|
2016-08-13 18:34:52 +02:00
|
|
|
|
// collection binding layer can not have any notion of
|
2016-09-02 18:40:16 +02:00
|
|
|
|
// "this is an attribute". It will not accept anything
|
|
|
|
|
|
// and just delegate to the next lower layer, which here
|
|
|
|
|
|
// is the empty binding and thus finally returns true
|
2016-08-13 18:34:52 +02:00
|
|
|
|
|
|
|
|
|
|
CHECK (mutator2.accept_until(ATTRIB3)); // ...but of course we can fast forward to dedicated values // accept_until
|
|
|
|
|
|
CHECK (!isnil (target)); // the fast forward did indeed accept some entries
|
|
|
|
|
|
CHECK (mutator2.acceptSrc(ATTRIB3)); // we have a duplicate in list, need to accept that as well // accept
|
|
|
|
|
|
CHECK (mutator2.hasSrc());
|
|
|
|
|
|
CHECK (mutator2.matchSrc (CHILD_B)); // ...now we're located behind the attributes, at first child
|
2016-03-25 23:45:32 +01:00
|
|
|
|
mutator2.injectNew (ATTRIB2); // injectNew
|
|
|
|
|
|
|
|
|
|
|
|
CHECK (mutator2.matchSrc (CHILD_B)); // first child waiting in src is CHILD_B
|
2016-08-07 01:58:26 +02:00
|
|
|
|
mutator2.skipSrc (CHILD_B); // ...which will be skipped (and thus discarded) // skipSrc
|
2016-03-26 01:22:40 +01:00
|
|
|
|
mutator2.injectNew (SUB_NODE); // inject a nested sub-structure (implementation defined) // injectNew
|
2016-03-25 23:45:32 +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-05-24 21:34:08 +02:00
|
|
|
|
CHECK (mutator2.hasSrc());
|
2016-03-25 23:45:32 +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-08-13 18:34:52 +02:00
|
|
|
|
CHECK (mutator2.hasSrc());
|
|
|
|
|
|
CHECK (mutator2.findSrc (CHILD_T)); // search for an element further down into src... // findSrc
|
|
|
|
|
|
CHECK (mutator2.matchSrc (CHILD_B)); // element at head of src is still CHILD_B (as before)
|
|
|
|
|
|
CHECK (mutator2.acceptSrc (CHILD_B)); // now pick and accept this src element as child // acceptSrc
|
|
|
|
|
|
|
|
|
|
|
|
CHECK (mutator2.hasSrc()); // next we have to clean up waste
|
|
|
|
|
|
mutator2.skipSrc (CHILD_T); // left behind by the findSrc() operation // skipSrc
|
2016-05-24 21:34:08 +02:00
|
|
|
|
CHECK (not mutator2.hasSrc()); // source contents exhausted
|
2016-03-25 23:45:32 +01:00
|
|
|
|
CHECK (not mutator2.acceptSrc (CHILD_T)); // ...anything beyond is NOP
|
2016-05-24 22:23:06 +02:00
|
|
|
|
CHECK (mutator2.completeScope()); // no pending elements left, everything resolved
|
2016-03-26 01:22:40 +01:00
|
|
|
|
|
|
|
|
|
|
// verify reordered shape
|
|
|
|
|
|
contents = stringify(eachElm(target));
|
|
|
|
|
|
CHECK ("≺α∣1≻" == *contents);
|
|
|
|
|
|
++contents;
|
2016-08-13 18:34:52 +02:00
|
|
|
|
CHECK ("≺γ∣3.45≻" == *contents);
|
2016-03-26 01:22:40 +01:00
|
|
|
|
++contents;
|
|
|
|
|
|
CHECK ("≺γ∣3.45≻" == *contents);
|
|
|
|
|
|
++contents;
|
2016-08-13 18:34:52 +02:00
|
|
|
|
CHECK ("≺β∣2≻" == *contents);
|
2016-03-26 01:22:40 +01:00
|
|
|
|
++contents;
|
2016-08-13 18:34:52 +02:00
|
|
|
|
CHECK (contains(*contents, "∣Rec()≻"));
|
2016-03-26 01:22:40 +01:00
|
|
|
|
++contents;
|
|
|
|
|
|
CHECK (contains(*contents, "∣78:56:34.012≻"));
|
|
|
|
|
|
++contents;
|
2016-08-13 18:34:52 +02:00
|
|
|
|
CHECK (contains(*contents, "∣b≻"));
|
|
|
|
|
|
++contents;
|
2016-03-26 01:22:40 +01:00
|
|
|
|
CHECK (isnil (contents));
|
|
|
|
|
|
|
2016-03-25 23:45:32 +01:00
|
|
|
|
cout << "Content after reordering...."
|
|
|
|
|
|
<< join(target) <<endl;
|
2016-04-16 02:20:23 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// --- third round: mutate data and sub-scopes ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// This time we build the Mutator bindings in a way to allow mutation
|
|
|
|
|
|
// For one, "mutation" means to assign a changed value to a simple node / attribute.
|
|
|
|
|
|
// And beyond that, mutation entails to open a nested scope and delve into that recursively.
|
2021-01-23 11:54:19 +01:00
|
|
|
|
// Here, as this is really just a test and demonstration, we implement those nested scopes aside,
|
2016-04-16 02:20:23 +02:00
|
|
|
|
// managed within a map and keyed by the sub node's ID.
|
|
|
|
|
|
auto mutator3 =
|
|
|
|
|
|
TreeMutator::build()
|
|
|
|
|
|
.attach (collection(target)
|
|
|
|
|
|
.constructFrom ([&](GenNode const& spec) -> Data
|
|
|
|
|
|
{
|
|
|
|
|
|
cout << "constructor invoked on "<<spec<<endl;
|
|
|
|
|
|
return {spec.idi.getSym(), render(spec.data)};
|
|
|
|
|
|
})
|
|
|
|
|
|
.matchElement ([&](GenNode const& spec, Data const& elm) -> bool
|
|
|
|
|
|
{
|
|
|
|
|
|
cout << "match? "<<spec.idi.getSym()<<"=?="<<elm.key<<endl;
|
|
|
|
|
|
return spec.idi.getSym() == elm.key;
|
|
|
|
|
|
})
|
|
|
|
|
|
.assignElement ([&](Data& target, GenNode const& spec) -> bool
|
|
|
|
|
|
{
|
2016-04-17 01:07:07 +02:00
|
|
|
|
cout << "assign "<<target<<" <- "<<spec<<endl;
|
|
|
|
|
|
CHECK (target.key == spec.idi.getSym(), "assignment to target with wrong identity");
|
|
|
|
|
|
target.val = render(spec.data);
|
|
|
|
|
|
return true;
|
2016-04-16 02:20:23 +02:00
|
|
|
|
})
|
2016-06-09 01:18:21 +02:00
|
|
|
|
.buildChildMutator ([&](Data& target, GenNode::ID const& subID, TreeMutator::Handle buff) -> bool
|
2016-04-16 02:20:23 +02:00
|
|
|
|
{
|
2016-04-17 04:51:19 +02:00
|
|
|
|
// use our "inside knowledge" to get at the nested scope implementation
|
|
|
|
|
|
VecD& subScope = subScopes[subID];
|
2021-05-02 18:31:47 +02:00
|
|
|
|
buff.emplace (
|
2016-04-17 04:51:19 +02:00
|
|
|
|
TreeMutator::build()
|
|
|
|
|
|
.attach (collection(subScope)
|
|
|
|
|
|
.constructFrom ([&](GenNode const& spec) -> Data
|
|
|
|
|
|
{
|
|
|
|
|
|
cout << "SubScope| constructor invoked on "<<spec<<endl;
|
|
|
|
|
|
return {spec.idi.getSym(), render(spec.data)};
|
|
|
|
|
|
})));
|
|
|
|
|
|
|
|
|
|
|
|
// NOTE: mutation of sub scope has not happened yet
|
|
|
|
|
|
// we can only document the sub scope to be opened now
|
|
|
|
|
|
cout << "openSub("<<subID.getSym()<<") ⟻ "<<target<<endl;
|
|
|
|
|
|
target.val = "Rec(--"+subID.getSym()+"--)";
|
|
|
|
|
|
return true;
|
2016-04-16 02:20:23 +02:00
|
|
|
|
}));
|
|
|
|
|
|
|
2016-10-03 23:54:09 +02:00
|
|
|
|
mutator3.init();
|
|
|
|
|
|
|
2016-04-16 02:20:23 +02:00
|
|
|
|
CHECK (isnil (target));
|
2016-08-13 18:34:52 +02:00
|
|
|
|
CHECK (mutator3.matchSrc (ATTRIB1)); // new mutator starts out anew at the beginning
|
|
|
|
|
|
CHECK (mutator3.accept_until (CHILD_T)); // fast forward behind the second-last child (CHILD_T) // accept_until
|
|
|
|
|
|
CHECK (mutator3.matchSrc (CHILD_B)); // this /would/ be the next source element, but rather...
|
|
|
|
|
|
CHECK (not mutator3.completeScope()); // CHILD_B is still pending, not done yet...
|
2016-06-10 03:19:33 +02:00
|
|
|
|
CHECK (mutator3.accept_until (Ref::END)); // fast forward, since we do not want to re-order anything // accept_until
|
|
|
|
|
|
CHECK ( mutator3.completeScope()); // now any pending elements where default-resolved
|
2016-04-16 02:20:23 +02:00
|
|
|
|
|
2016-04-17 01:07:07 +02:00
|
|
|
|
CHECK (not contains(join(target), "≺γ∣3.1415927≻"));
|
2016-09-03 20:17:46 +02:00
|
|
|
|
CHECK (mutator3.assignElm(GAMMA_PI)); // ...we assign a new payload to the designated element // assignElm
|
2016-04-17 01:07:07 +02:00
|
|
|
|
CHECK ( contains(join(target), "≺γ∣3.1415927≻"));
|
2016-08-13 18:34:52 +02:00
|
|
|
|
CHECK ( mutator3.completeScope());
|
2016-09-04 20:55:21 +02:00
|
|
|
|
cout << "Content after assignment...."
|
2016-04-16 02:20:23 +02:00
|
|
|
|
<< join(target) <<endl;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// prepare for recursion into sub scope..
|
|
|
|
|
|
// Since this is a demonstration, we do not actually recurse into anything,
|
|
|
|
|
|
// rather we invoke the operations on a nested mutator right from here.
|
|
|
|
|
|
|
2016-04-17 04:51:19 +02:00
|
|
|
|
InPlaceBuffer<TreeMutator, sizeof(mutator1)> subMutatorBuffer;
|
2016-06-09 01:18:21 +02:00
|
|
|
|
TreeMutator::Handle placementHandle(subMutatorBuffer);
|
2016-04-16 02:20:23 +02:00
|
|
|
|
|
2016-05-26 01:56:13 +02:00
|
|
|
|
CHECK (mutator3.mutateChild (SUB_NODE, placementHandle)); // mutateChild
|
2016-04-16 02:20:23 +02:00
|
|
|
|
|
2016-04-17 04:51:19 +02:00
|
|
|
|
CHECK (isnil (subScopes[SUB_NODE.idi])); // ...this is where the nested mutator is expected to work on
|
2016-05-24 21:34:08 +02:00
|
|
|
|
CHECK (not subMutatorBuffer->hasSrc());
|
2016-04-16 02:20:23 +02:00
|
|
|
|
|
|
|
|
|
|
// now use the Mutator *interface* to talk to the nested mutator...
|
|
|
|
|
|
// This code might be confusing, because in fact we're playing two roles here!
|
|
|
|
|
|
// For one, above, in the definition of mutator3 and in the declaration of MapD subScopes,
|
|
|
|
|
|
// the test code represents what a private data structure and binding would do.
|
2016-06-05 16:14:18 +02:00
|
|
|
|
// But below we enact the TreeDiffAplicator, which *would* use the Mutator interface
|
2016-04-16 02:20:23 +02:00
|
|
|
|
// to talk to an otherwise opaque nested mutator implementation. Actually, here this
|
2016-04-17 04:51:19 +02:00
|
|
|
|
// nested opaque mutator is created on-the-fly, embedded within the .buildChildMutator(..lambda...)
|
|
|
|
|
|
// Incidentally, we "just happen to know" how large the buffer needs to be to hold that mutator,
|
|
|
|
|
|
// since this is a topic beyond the scope of this test. In real usage, the DiffApplicator cares
|
|
|
|
|
|
// to provide a stack of suitably sized buffers for the nested mutators.
|
2016-04-16 02:20:23 +02:00
|
|
|
|
|
2016-05-26 01:56:13 +02:00
|
|
|
|
subMutatorBuffer->injectNew (TYPE_X); // >> // injectNew
|
|
|
|
|
|
subMutatorBuffer->injectNew (ATTRIB2); // >> // injectNew
|
|
|
|
|
|
subMutatorBuffer->injectNew (CHILD_B); // >> // injectNew
|
|
|
|
|
|
subMutatorBuffer->injectNew (CHILD_A); // >> // injectNew
|
2016-04-16 02:20:23 +02:00
|
|
|
|
|
2016-04-17 04:51:19 +02:00
|
|
|
|
CHECK (not isnil (subScopes[SUB_NODE.idi])); // ...and "magically" these instructions happened to insert
|
|
|
|
|
|
cout << "Sub|" << join(subScopes[SUB_NODE.idi]) <<endl; // some new content into our implementation defined sub scope!
|
2016-04-16 02:20:23 +02:00
|
|
|
|
|
2016-04-17 04:51:19 +02:00
|
|
|
|
// verify contents of nested scope after mutation
|
|
|
|
|
|
contents = stringify(eachElm(subScopes[SUB_NODE.idi]));
|
|
|
|
|
|
CHECK ("≺type∣ξ≻" == *contents);
|
|
|
|
|
|
++contents;
|
|
|
|
|
|
CHECK ("≺β∣2≻" == *contents);
|
|
|
|
|
|
++contents;
|
|
|
|
|
|
CHECK (contains(*contents, "∣b≻"));
|
|
|
|
|
|
++contents;
|
|
|
|
|
|
CHECK (contains(*contents, "∣a≻"));
|
|
|
|
|
|
++contents;
|
|
|
|
|
|
CHECK (isnil (contents));
|
2016-04-16 02:20:23 +02:00
|
|
|
|
|
|
|
|
|
|
|
2016-04-17 04:51:19 +02:00
|
|
|
|
// now back to parent scope....
|
2016-04-18 01:41:41 +02:00
|
|
|
|
// ...add a new attribute and immediately recurse into it
|
2016-09-03 21:05:16 +02:00
|
|
|
|
mutator3.injectNew (ATTRIB_NODE);
|
2016-04-18 01:41:41 +02:00
|
|
|
|
CHECK (mutator3.mutateChild (ATTRIB_NODE, placementHandle)); // NOTE: we're just recycling the buffer. InPlaceHolder handles lifecycle properly
|
|
|
|
|
|
subMutatorBuffer->injectNew (TYPE_Z);
|
|
|
|
|
|
subMutatorBuffer->injectNew (CHILD_A);
|
|
|
|
|
|
subMutatorBuffer->injectNew (CHILD_A);
|
|
|
|
|
|
subMutatorBuffer->injectNew (CHILD_A);
|
2016-05-24 22:23:06 +02:00
|
|
|
|
CHECK (subMutatorBuffer->completeScope()); // no pending "open ends" left in sub-scope
|
|
|
|
|
|
CHECK (mutator3.completeScope()); // and likewise in the enclosing main scope
|
2016-04-18 01:41:41 +02:00
|
|
|
|
|
|
|
|
|
|
// and thus we've gotten a second nested scope, populated with new values
|
|
|
|
|
|
cout << "Sub|" << join(subScopes[ATTRIB_NODE.idi]) <<endl;
|
|
|
|
|
|
|
|
|
|
|
|
// verify contents of this second nested scope
|
|
|
|
|
|
contents = stringify(eachElm(subScopes[ATTRIB_NODE.idi]));
|
|
|
|
|
|
CHECK ("≺type∣ζ≻" == *contents);
|
|
|
|
|
|
++contents;
|
|
|
|
|
|
CHECK (contains(*contents, "∣a≻"));
|
|
|
|
|
|
++contents;
|
|
|
|
|
|
CHECK (contains(*contents, "∣a≻"));
|
|
|
|
|
|
++contents;
|
|
|
|
|
|
CHECK (contains(*contents, "∣a≻"));
|
|
|
|
|
|
++contents;
|
|
|
|
|
|
CHECK (isnil (contents));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// back to parent scope....
|
|
|
|
|
|
// verify the marker left by our "nested sub-scope lambda"
|
|
|
|
|
|
CHECK (contains (join(target), "Rec(--"+SUB_NODE.idi.getSym()+"--)"));
|
|
|
|
|
|
CHECK (contains (join(target), "Rec(--"+ATTRIB_NODE.idi.getSym()+"--)"));
|
|
|
|
|
|
|
2016-09-04 20:55:21 +02:00
|
|
|
|
cout << "Content after nested mutation...."
|
2016-04-17 04:51:19 +02:00
|
|
|
|
<< join(target) <<endl;
|
2015-04-02 03:30:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-05-26 04:05:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-06-09 01:10:52 +02:00
|
|
|
|
/** @test translate generic mutation into attribute manipulation
|
|
|
|
|
|
* - here we bind directly to data fields local to this scope
|
|
|
|
|
|
* - we execute the same diff primitives used in the preceding tests
|
|
|
|
|
|
* - yet binding to data fields has certain intrinsic limits; due to the
|
|
|
|
|
|
* fixed non-dynamic nature of data fields, it is impossible to define an
|
|
|
|
|
|
* "ordering" and consequently there is no _sequence of diff application._
|
|
|
|
|
|
* - so the only form of actually _applying_ a change is to invoke the given
|
|
|
|
|
|
* setter or use the given mechanism to construct a nested mutator. */
|
2015-04-02 03:30:20 +02:00
|
|
|
|
void
|
2016-05-26 01:56:13 +02:00
|
|
|
|
mutateAttribute ()
|
2015-04-02 03:30:20 +02:00
|
|
|
|
{
|
2016-05-26 04:05:37 +02:00
|
|
|
|
MARK_TEST_FUN;
|
|
|
|
|
|
|
|
|
|
|
|
// local data fields to be handled as "attributes"
|
|
|
|
|
|
int alpha = -1;
|
|
|
|
|
|
int64_t beta = -1;
|
|
|
|
|
|
double gamma = -1;
|
|
|
|
|
|
|
|
|
|
|
|
// we'll use this as an attribute with nested scope ("object valued attribute")
|
|
|
|
|
|
TestMutationTarget delta;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define LOG_SETTER(KEY) cout << STRINGIFY(KEY) " := "<<val<<endl;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// set up a binding to these opaque private structures...
|
|
|
|
|
|
auto mutator1 =
|
|
|
|
|
|
TreeMutator::build()
|
2016-05-27 03:39:22 +02:00
|
|
|
|
.change("α", [&](int val)
|
2016-05-26 04:05:37 +02:00
|
|
|
|
{
|
|
|
|
|
|
LOG_SETTER ("alpha")
|
|
|
|
|
|
alpha = val;
|
|
|
|
|
|
})
|
2016-05-27 03:39:22 +02:00
|
|
|
|
.change("γ", [&](double val)
|
2016-05-26 04:05:37 +02:00
|
|
|
|
{
|
|
|
|
|
|
LOG_SETTER ("gamma")
|
|
|
|
|
|
gamma = val;
|
|
|
|
|
|
});
|
2016-10-03 23:54:09 +02:00
|
|
|
|
mutator1.init();
|
2016-05-26 04:05:37 +02:00
|
|
|
|
|
2016-06-09 01:10:52 +02:00
|
|
|
|
CHECK (sizeof(mutator1) <= sizeof(void*) // the TreeMutator VTable
|
|
|
|
|
|
+ 2 * sizeof(void*) // one closure reference for each lambda
|
|
|
|
|
|
+ 2 * sizeof(GenNode::ID)); // one attribute-key for each binding
|
|
|
|
|
|
|
2016-05-26 04:05:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// --- first round: introduce new "attributes" ---
|
|
|
|
|
|
|
|
|
|
|
|
CHECK (-1 == alpha);
|
|
|
|
|
|
CHECK (-1 == beta);
|
|
|
|
|
|
CHECK (-1 == gamma);
|
|
|
|
|
|
|
2016-08-29 23:04:44 +02:00
|
|
|
|
CHECK (not mutator1.hasSrc()); // NOTE: the attribute binding has no "reference source sequence" and thus no dynamic state.
|
2016-06-09 01:10:52 +02:00
|
|
|
|
// (in fact it is predetermined, because it relies on a likewise fixed class definition)
|
2016-05-26 04:05:37 +02:00
|
|
|
|
CHECK (mutator1.completeScope()); // NOTE: this is always true and NOP, for the same reason: the structure of the binding is fixed
|
|
|
|
|
|
|
|
|
|
|
|
mutator1.injectNew (ATTRIB1);
|
|
|
|
|
|
CHECK ( 1 == alpha);
|
|
|
|
|
|
CHECK (-1 == beta);
|
|
|
|
|
|
CHECK (-1 == gamma);
|
|
|
|
|
|
|
|
|
|
|
|
mutator1.injectNew (ATTRIB3);
|
|
|
|
|
|
CHECK ( 1 == alpha);
|
|
|
|
|
|
CHECK (-1 == beta);
|
|
|
|
|
|
CHECK (3.45 == gamma);
|
|
|
|
|
|
|
|
|
|
|
|
mutator1.injectNew (ATTRIB3);
|
|
|
|
|
|
CHECK ( 1 == alpha);
|
|
|
|
|
|
CHECK (-1 == beta);
|
|
|
|
|
|
CHECK (3.45 == gamma);
|
|
|
|
|
|
|
2016-05-28 01:17:45 +02:00
|
|
|
|
CHECK (not mutator1.injectNew (ATTRIB2)); // ...because we didn't define a binding for ATTRIB2 (aka "beta")
|
2016-05-26 04:05:37 +02:00
|
|
|
|
|
|
|
|
|
|
// any changes to something other than attributes are just delegated to the next "onion layer"
|
2016-06-09 01:10:52 +02:00
|
|
|
|
// since in this case here, there is only one layer (our attribute binding), these other changes will be ignored silently
|
2016-05-26 04:05:37 +02:00
|
|
|
|
mutator1.injectNew (CHILD_B);
|
|
|
|
|
|
mutator1.injectNew (CHILD_B);
|
|
|
|
|
|
mutator1.injectNew (CHILD_T);
|
|
|
|
|
|
CHECK (mutator1.completeScope()); // this invocation typically happens at this point, but is NOP (see above)
|
|
|
|
|
|
|
|
|
|
|
|
CHECK ( 1 == alpha);
|
|
|
|
|
|
CHECK (-1 == beta);
|
|
|
|
|
|
CHECK (3.45 == gamma);
|
|
|
|
|
|
cout << "successfully 'injected' new attributes." <<endl;
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-06-09 01:10:52 +02:00
|
|
|
|
|
2016-05-26 04:05:37 +02:00
|
|
|
|
// --- second round: reordering ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// in fact any re-ordering of "attributes" is prohibited,
|
|
|
|
|
|
// because "attributes" are mapped to object or data fields,
|
|
|
|
|
|
// which are fixed by definition and don't expose any ordering.
|
|
|
|
|
|
// While any mutations beyond attributes are passed on / ignored
|
|
|
|
|
|
auto mutator2 =
|
|
|
|
|
|
TreeMutator::build()
|
2016-06-04 15:08:10 +02:00
|
|
|
|
.change("α", [&](int val)
|
|
|
|
|
|
{
|
|
|
|
|
|
LOG_SETTER ("alpha")
|
|
|
|
|
|
alpha = val;
|
|
|
|
|
|
})
|
|
|
|
|
|
.change("β", [&](int64_t val)
|
|
|
|
|
|
{
|
|
|
|
|
|
LOG_SETTER ("beta")
|
|
|
|
|
|
beta = val;
|
|
|
|
|
|
})
|
|
|
|
|
|
.change("γ", [&](double val)
|
|
|
|
|
|
{
|
|
|
|
|
|
LOG_SETTER ("gamma")
|
|
|
|
|
|
gamma = val;
|
|
|
|
|
|
});
|
2016-10-03 23:54:09 +02:00
|
|
|
|
mutator2.init();
|
|
|
|
|
|
|
2016-05-26 04:05:37 +02:00
|
|
|
|
|
investigate and confirm the logic underlying the matchSrc, skipSrc and acceptSrc primitives
In Theory, acceptSrc and skipSrc are to operate symmetrically,
with the sole difference that skipSrc does not move anything
into the new content.
BUT, since skipSrc is also used to implement the `skip` verb,
which serves to discard garbage left back by a preceeding `find`,
we cannot touch the data found in the src position without risk
of SEGFAULT. For this reason, there is a dedicated matchSrc operation,
which shall be used to generate the verification step to properly
implement the `del` verb.
I've spent quite some time to verify the logic of predicate evaluation.
It seems to be OK: whenever the SELECTOR applies, then we'll perform
the local match, and then also we'll perform the skipSrc. Otherwise,
we'll delegate both operations likewise to the next lower layer,
without touching anything here.
2016-08-09 23:42:42 +02:00
|
|
|
|
CHECK (sizeof(mutator2) <= sizeof(void*) // the TreeMutator VTable
|
2016-06-09 01:10:52 +02:00
|
|
|
|
+ 3 * sizeof(void*) // one closure reference for each lambda
|
|
|
|
|
|
+ 3 * sizeof(GenNode::ID)); // one attribute-key for each binding
|
|
|
|
|
|
|
2016-05-26 04:05:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
2016-06-04 15:08:10 +02:00
|
|
|
|
CHECK ( 1 == alpha);
|
|
|
|
|
|
CHECK (-1 == beta);
|
|
|
|
|
|
CHECK (3.45 == gamma); // values not affected by attaching a new mutator
|
2016-05-26 04:05:37 +02:00
|
|
|
|
|
2016-06-09 01:10:52 +02:00
|
|
|
|
CHECK (mutator2.matchSrc (ATTRIB1)); // this "match" is positive, since our binding supports this attribute
|
|
|
|
|
|
CHECK ( 1 == alpha); // the (NOP) match didn't change anything...
|
2016-06-04 15:08:10 +02:00
|
|
|
|
CHECK (-1 == beta);
|
|
|
|
|
|
CHECK (3.45 == gamma);
|
2016-05-26 04:05:37 +02:00
|
|
|
|
|
2016-06-04 15:08:10 +02:00
|
|
|
|
VERIFY_ERROR (LOGIC, mutator2.findSrc (ATTRIB3));
|
|
|
|
|
|
// search for an element and thus reordering is explicitly rejected...
|
|
|
|
|
|
// If we hadn't defined a binding for "γ", then the same operation
|
|
|
|
|
|
// would have been passed on silently to other binding layers.
|
2016-05-26 04:05:37 +02:00
|
|
|
|
|
2016-06-09 01:10:52 +02:00
|
|
|
|
CHECK (mutator2.matchSrc (ATTRIB1)); // behaviour of the binding remains unaffected
|
|
|
|
|
|
CHECK (mutator2.acceptSrc (ATTRIB1)); // now pick and "accept" this src element (also a NOP) // acceptSrc
|
2016-05-26 04:05:37 +02:00
|
|
|
|
|
investigate and confirm the logic underlying the matchSrc, skipSrc and acceptSrc primitives
In Theory, acceptSrc and skipSrc are to operate symmetrically,
with the sole difference that skipSrc does not move anything
into the new content.
BUT, since skipSrc is also used to implement the `skip` verb,
which serves to discard garbage left back by a preceeding `find`,
we cannot touch the data found in the src position without risk
of SEGFAULT. For this reason, there is a dedicated matchSrc operation,
which shall be used to generate the verification step to properly
implement the `del` verb.
I've spent quite some time to verify the logic of predicate evaluation.
It seems to be OK: whenever the SELECTOR applies, then we'll perform
the local match, and then also we'll perform the skipSrc. Otherwise,
we'll delegate both operations likewise to the next lower layer,
without touching anything here.
2016-08-09 23:42:42 +02:00
|
|
|
|
VERIFY_ERROR (LOGIC, mutator2.skipSrc (ATTRIB3));
|
|
|
|
|
|
// and 'skip' likewise is just not implemented for attributes // skipSrc
|
2016-06-04 15:08:10 +02:00
|
|
|
|
CHECK ( 1 == alpha);
|
|
|
|
|
|
CHECK (-1 == beta);
|
|
|
|
|
|
CHECK (3.45 == gamma); // all these non-operations actually didn't change anything...
|
2016-05-26 04:05:37 +02:00
|
|
|
|
|
2016-08-13 19:03:42 +02:00
|
|
|
|
CHECK (mutator2.accept_until(Ref::ATTRIBS)); // accept_until ATTRIBS
|
|
|
|
|
|
// what /is/ allowed though, for reasons of logic,
|
|
|
|
|
|
// is to "fast forward behind all attributes"
|
|
|
|
|
|
// of course this is implemented as NOP
|
2016-09-02 18:40:16 +02:00
|
|
|
|
CHECK (mutator2.accept_until(Ref::END)); // likewise for Ref::END // accept_until END
|
2016-08-13 19:03:42 +02:00
|
|
|
|
|
2016-05-26 04:05:37 +02:00
|
|
|
|
mutator2.injectNew (ATTRIB2); // injectNew
|
2016-06-04 15:08:10 +02:00
|
|
|
|
|
|
|
|
|
|
CHECK ( 1 == alpha);
|
|
|
|
|
|
CHECK ( 2 == beta); // the first operation actually causing a tangible effect
|
|
|
|
|
|
CHECK (3.45 == gamma);
|
|
|
|
|
|
|
2016-05-26 04:05:37 +02:00
|
|
|
|
|
2016-06-04 15:08:10 +02:00
|
|
|
|
// for sake of completeness, we'll be applying the same sequence of operations as in the other tests
|
|
|
|
|
|
// but since all those operations are not relevant for our attribute binding, they will be passed on
|
|
|
|
|
|
// to lower binding layers. And since, moreover, there /are no lower binding layers/ in our setup,
|
|
|
|
|
|
// they will just do nothing and return false
|
investigate and confirm the logic underlying the matchSrc, skipSrc and acceptSrc primitives
In Theory, acceptSrc and skipSrc are to operate symmetrically,
with the sole difference that skipSrc does not move anything
into the new content.
BUT, since skipSrc is also used to implement the `skip` verb,
which serves to discard garbage left back by a preceeding `find`,
we cannot touch the data found in the src position without risk
of SEGFAULT. For this reason, there is a dedicated matchSrc operation,
which shall be used to generate the verification step to properly
implement the `del` verb.
I've spent quite some time to verify the logic of predicate evaluation.
It seems to be OK: whenever the SELECTOR applies, then we'll perform
the local match, and then also we'll perform the skipSrc. Otherwise,
we'll delegate both operations likewise to the next lower layer,
without touching anything here.
2016-08-09 23:42:42 +02:00
|
|
|
|
CHECK (not mutator2.matchSrc (CHILD_B));
|
|
|
|
|
|
mutator2.skipSrc (CHILD_B); // ...no setter binding, thus no effect // skipSrc
|
2016-06-04 15:08:10 +02:00
|
|
|
|
CHECK (not mutator2.injectNew (SUB_NODE));// ...no setter binding, thus no effect // injectNew
|
|
|
|
|
|
CHECK (not mutator2.matchSrc (CHILD_B));
|
2016-08-13 19:03:42 +02:00
|
|
|
|
CHECK (not mutator2.findSrc (CHILD_T)); // find for non-attribute is just passed down // findSrc
|
2016-06-04 15:08:10 +02:00
|
|
|
|
CHECK (not mutator2.acceptSrc (CHILD_B)); // acceptSrc
|
2016-08-13 19:03:42 +02:00
|
|
|
|
mutator2.skipSrc (CHILD_T); // skipSrc
|
2016-05-26 04:05:37 +02:00
|
|
|
|
|
2016-06-04 15:08:10 +02:00
|
|
|
|
CHECK ( 1 == alpha);
|
|
|
|
|
|
CHECK ( 2 == beta);
|
|
|
|
|
|
CHECK (3.45 == gamma); // no further effect on our attribute fields
|
|
|
|
|
|
|
2016-06-09 01:10:52 +02:00
|
|
|
|
cout << "ignored all 'reordering' operations (as expected)..." <<endl;
|
2016-05-26 04:05:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// --- third round: mutate data and sub-scopes ---
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-06-05 16:14:18 +02:00
|
|
|
|
// This third part of the test covers the actual purpose of attribute binding:
|
2016-06-09 01:10:52 +02:00
|
|
|
|
// the ability to assign values or even to open a sub-scope enabling recursion
|
|
|
|
|
|
// into a nested object stored within a data field.
|
2016-05-26 04:05:37 +02:00
|
|
|
|
auto mutator3 =
|
|
|
|
|
|
TreeMutator::build()
|
2016-06-05 16:14:18 +02:00
|
|
|
|
.change("γ", [&](double val)
|
|
|
|
|
|
{
|
|
|
|
|
|
LOG_SETTER ("gamma")
|
|
|
|
|
|
gamma = val;
|
2016-06-05 17:26:48 +02:00
|
|
|
|
})
|
2016-06-09 01:18:21 +02:00
|
|
|
|
.mutateAttrib("δ", [&](TreeMutator::Handle buff)
|
2016-06-05 17:26:48 +02:00
|
|
|
|
{
|
|
|
|
|
|
// NOTE: we use "implementation inside knowledge" regarding the nested scope,
|
|
|
|
|
|
// which is here represented as TestMutationTarget
|
2021-05-02 18:31:47 +02:00
|
|
|
|
buff.emplace (
|
2016-06-05 17:26:48 +02:00
|
|
|
|
TreeMutator::build()
|
|
|
|
|
|
.attachDummy (delta));
|
|
|
|
|
|
|
|
|
|
|
|
// NOTE: when this closure is invoked, we're about to open the sub scope,
|
|
|
|
|
|
// while mutation has not happened yet
|
|
|
|
|
|
cout << "openSub()...\n"
|
|
|
|
|
|
<< join(delta.getLog(), "\n") <<endl;
|
2016-06-05 16:14:18 +02:00
|
|
|
|
});
|
2016-10-03 23:54:09 +02:00
|
|
|
|
mutator3.init();
|
2016-05-26 04:05:37 +02:00
|
|
|
|
|
2016-06-09 01:10:52 +02:00
|
|
|
|
CHECK (sizeof(mutator1) <= sizeof(void*) // the TreeMutator VTable
|
|
|
|
|
|
+ 2 * sizeof(void*) // one closure reference for each lambda
|
|
|
|
|
|
+ 2 * sizeof(GenNode::ID)); // one attribute-key for each binding
|
|
|
|
|
|
|
2016-06-05 16:52:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
2016-06-05 16:31:29 +02:00
|
|
|
|
VERIFY_ERROR (LOGIC, mutator3.accept_until (ATTRIB3)); // rejected; no support for ordering // accept_until
|
|
|
|
|
|
CHECK (not mutator3.accept_until (ATTRIB2)); // unknown binding, no one is responsible
|
|
|
|
|
|
CHECK (not mutator3.accept_until (ATTRIB1));
|
2016-08-13 19:03:42 +02:00
|
|
|
|
CHECK (mutator3.accept_until (Ref::ATTRIBS)); // only the generic end-of-scope marks supported
|
2016-09-02 18:40:16 +02:00
|
|
|
|
CHECK (mutator3.accept_until (Ref::END)); // (and implemented as NOP plus forwarding down)
|
2016-05-26 04:05:37 +02:00
|
|
|
|
|
2016-06-05 16:14:18 +02:00
|
|
|
|
// explanation: due to the nature of a 'data field',
|
|
|
|
|
|
// this binding has no notion of 'ordering' and thus no 'current position'.
|
|
|
|
|
|
// Rather, the decision if some diff verb is applicable can be done statically.
|
2016-06-10 03:19:33 +02:00
|
|
|
|
CHECK (mutator3.completeScope()); // always true (for the same reason)
|
|
|
|
|
|
|
2016-06-05 16:14:18 +02:00
|
|
|
|
|
|
|
|
|
|
CHECK (not mutator3.acceptSrc (ATTRIB1));
|
|
|
|
|
|
CHECK (not mutator3.acceptSrc (ATTRIB2));
|
2025-06-07 23:59:57 +02:00
|
|
|
|
CHECK ( mutator3.acceptSrc (ATTRIB3)); // in this round we just have a binding for ATTRIB3 (== "γ")
|
2016-06-05 16:52:37 +02:00
|
|
|
|
CHECK ( mutator3.acceptSrc (ATTRIB_NODE));
|
|
|
|
|
|
// ...and of course a binding for a nested ATTRIB_NODE
|
2016-06-05 16:14:18 +02:00
|
|
|
|
|
|
|
|
|
|
CHECK (3.45 == gamma);
|
2016-05-26 04:05:37 +02:00
|
|
|
|
CHECK (mutator3.assignElm(GAMMA_PI)); // ...we assign a new payload to the current element first // assignElm
|
2016-06-05 16:14:18 +02:00
|
|
|
|
CHECK (3.14159265 == gamma);
|
2025-06-07 23:59:57 +02:00
|
|
|
|
CHECK ( 1 == alpha); // the other fields remain unaffected
|
2016-06-05 16:14:18 +02:00
|
|
|
|
CHECK ( 2 == beta);
|
2016-05-26 04:05:37 +02:00
|
|
|
|
|
2016-06-05 16:14:18 +02:00
|
|
|
|
cout << "successfully assigned a new value." <<endl;
|
2016-05-26 04:05:37 +02:00
|
|
|
|
|
2016-06-05 16:14:18 +02:00
|
|
|
|
|
2016-06-05 16:52:37 +02:00
|
|
|
|
// prepare for recursion into sub scope...
|
|
|
|
|
|
// In this demonstration, the nested scope is declared to live within an attribute `ATTRIB_NODE` (== "δ").
|
|
|
|
|
|
// It is implemented as `TestMutationTarget delta`, which allows us to verify a fully operational nested mutator.
|
2016-05-26 04:05:37 +02:00
|
|
|
|
|
2016-06-05 16:52:37 +02:00
|
|
|
|
const size_t BUFF_SIZ = sizeof(TreeMutator::build().attachDummy (delta));
|
2016-06-09 01:10:52 +02:00
|
|
|
|
// use some suitable size here, not the point in focus for this test
|
2016-06-05 16:52:37 +02:00
|
|
|
|
|
|
|
|
|
|
InPlaceBuffer<TreeMutator, BUFF_SIZ> subMutatorBuffer;
|
2016-06-09 01:18:21 +02:00
|
|
|
|
TreeMutator::Handle placementHandle(subMutatorBuffer);
|
2016-05-26 04:05:37 +02:00
|
|
|
|
|
2016-06-05 16:52:37 +02:00
|
|
|
|
CHECK (mutator3.mutateChild (ATTRIB_NODE, placementHandle)); // mutateChild
|
2016-05-26 04:05:37 +02:00
|
|
|
|
|
2016-06-05 16:52:37 +02:00
|
|
|
|
CHECK (isnil (delta)); // ...this is where the nested mutator is expected to work on
|
2016-05-26 04:05:37 +02:00
|
|
|
|
CHECK (not subMutatorBuffer->hasSrc());
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-06-05 16:52:37 +02:00
|
|
|
|
// now use the Mutator *interface* to talk to the nested mutator...
|
2016-05-26 04:05:37 +02:00
|
|
|
|
subMutatorBuffer->injectNew (TYPE_X); // >> // injectNew
|
|
|
|
|
|
subMutatorBuffer->injectNew (ATTRIB2); // >> // injectNew
|
|
|
|
|
|
subMutatorBuffer->injectNew (CHILD_B); // >> // injectNew
|
|
|
|
|
|
subMutatorBuffer->injectNew (CHILD_A); // >> // injectNew
|
|
|
|
|
|
|
2016-06-05 16:52:37 +02:00
|
|
|
|
CHECK (not isnil (delta)); // ...and "magically" these instructions happened to insert
|
|
|
|
|
|
cout << "Sub|" << delta.showContent() <<endl; // some new content into our implementation defined sub scope!
|
2016-06-05 17:26:48 +02:00
|
|
|
|
cout << "____Mutation-Log(nested)______\n"
|
|
|
|
|
|
<< join(delta.getLog(), "\n")
|
|
|
|
|
|
<< "\n───╼━━━━━━━━━╾────────────────"<<endl;
|
2016-05-26 04:05:37 +02:00
|
|
|
|
|
|
|
|
|
|
// verify contents of nested scope after mutation
|
2016-06-05 16:52:37 +02:00
|
|
|
|
CHECK (delta.showContent() == "type = ξ, β = 2, b, a");
|
2016-05-26 04:05:37 +02:00
|
|
|
|
|
2016-06-05 16:52:37 +02:00
|
|
|
|
// verify unaffected parent scope (data fields)
|
|
|
|
|
|
CHECK (3.14159265 == gamma);
|
|
|
|
|
|
CHECK ( 1 == alpha);
|
|
|
|
|
|
CHECK ( 2 == beta);
|
2015-04-02 03:30:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-05-26 04:05:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-09-02 03:10:27 +02:00
|
|
|
|
/** @test apply mutation primitives to a GenNode tree.
|
|
|
|
|
|
* - again we perform _literally_ the same diff steps as before
|
|
|
|
|
|
* - but we use the pre-configured binding for Record<GenNode>
|
|
|
|
|
|
* - internally this is comprised of two collection binding layers
|
|
|
|
|
|
* - we start with an empty root node, to be populated and transformed
|
|
|
|
|
|
*/
|
2015-04-02 03:30:20 +02:00
|
|
|
|
void
|
2016-03-19 01:42:27 +01:00
|
|
|
|
mutateGenNode()
|
2015-04-02 03:30:20 +02:00
|
|
|
|
{
|
2016-09-02 03:10:27 +02:00
|
|
|
|
MARK_TEST_FUN;
|
|
|
|
|
|
|
|
|
|
|
|
// private target data be mutated
|
|
|
|
|
|
Rec::Mutator target;
|
|
|
|
|
|
|
|
|
|
|
|
// set up a GenNode binding to work on this root node...
|
|
|
|
|
|
auto mutator1 =
|
|
|
|
|
|
TreeMutator::build()
|
|
|
|
|
|
.attach (target);
|
2016-10-03 23:54:09 +02:00
|
|
|
|
mutator1.init();
|
2016-09-02 03:10:27 +02:00
|
|
|
|
|
2016-09-02 20:38:14 +02:00
|
|
|
|
|
2016-09-03 19:54:54 +02:00
|
|
|
|
#if false /////////////////////////////////////////////////////////////////////////////////////////////////////////////TICKET #1007 : strange behaviour, getting additional storage
|
2016-09-02 20:38:14 +02:00
|
|
|
|
using VecG = RecordSetup<GenNode>::Storage;
|
|
|
|
|
|
|
|
|
|
|
|
CHECK (sizeof(mutator1) <= 2 * (sizeof(VecG) // we use two collection bindings...
|
|
|
|
|
|
+sizeof(VecG*) // with a buffer for pending elements and a reference to the original collection
|
|
|
|
|
|
+ 2* sizeof(VecG::iterator) // and one Lumiera RangeIter (comprised of pos and end iterators)
|
|
|
|
|
|
+sizeof(void*) // the VTable for each layer of TreeMutator impl
|
|
|
|
|
|
)
|
|
|
|
|
|
+ 1 * sizeof(void*)); // plus one unused selector, implemented as pointer to the default impl
|
2020-02-21 23:55:09 +01:00
|
|
|
|
//////////
|
|
|
|
|
|
//////////NOTE: unexpected behaviour confirmed with GCC-8
|
|
|
|
|
|
//////////
|
|
|
|
|
|
////////// However, the practice of verifying data size and layout assumptions
|
|
|
|
|
|
////////// is increasingly questionable, given that all modern compilers do data flow based optimisations.
|
2016-09-02 20:38:14 +02:00
|
|
|
|
#endif /////////////////////////////////////////////////////////////////////////////////////////////////////////////TICKET #1007
|
2016-09-02 03:10:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// --- first round: populate the collection ---
|
|
|
|
|
|
|
|
|
|
|
|
CHECK (isnil (target));
|
|
|
|
|
|
CHECK (not mutator1.hasSrc());
|
|
|
|
|
|
|
|
|
|
|
|
mutator1.injectNew (ATTRIB1);
|
|
|
|
|
|
CHECK (!isnil (target));
|
|
|
|
|
|
CHECK (contains(renderRecord(target), "α = 1"));
|
|
|
|
|
|
|
|
|
|
|
|
mutator1.injectNew (ATTRIB3);
|
|
|
|
|
|
mutator1.injectNew (ATTRIB3);
|
|
|
|
|
|
mutator1.injectNew (CHILD_B);
|
|
|
|
|
|
mutator1.injectNew (CHILD_B);
|
|
|
|
|
|
mutator1.injectNew (CHILD_T);
|
|
|
|
|
|
CHECK (mutator1.completeScope());
|
|
|
|
|
|
|
|
|
|
|
|
Rec& root = target;
|
|
|
|
|
|
|
|
|
|
|
|
CHECK (!isnil (root)); // nonempty -- content has been added
|
|
|
|
|
|
CHECK (Rec::TYPE_NIL == root.getType()); // type field was not touched
|
|
|
|
|
|
CHECK (1 == root.get("α").data.get<int>()); // has gotten our int attribute "α"
|
|
|
|
|
|
CHECK (3.45 == root.get("γ").data.get<double>()); // ... and double attribute "γ"
|
|
|
|
|
|
auto scope = root.scope(); // look into the scope contents...
|
|
|
|
|
|
CHECK ( *scope == CHILD_B); // there we find is CHILD_B
|
|
|
|
|
|
CHECK (*++scope == CHILD_B); // followed by a second CHILD_B
|
|
|
|
|
|
CHECK (*++scope == CHILD_T); // and another one CHILD_T
|
|
|
|
|
|
|
2016-09-04 20:55:21 +02:00
|
|
|
|
cout << "injected...................."
|
|
|
|
|
|
<< renderRecord(target)<<endl;
|
2016-09-02 03:10:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// --- second round: reorder the collection ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Mutators are one-time disposable objects,
|
|
|
|
|
|
// thus we'll have to build a new one for the second round...
|
|
|
|
|
|
auto mutator2 =
|
|
|
|
|
|
TreeMutator::build()
|
2016-09-03 19:54:54 +02:00
|
|
|
|
.attach (target);
|
2016-09-02 03:10:27 +02:00
|
|
|
|
|
2016-10-03 23:54:09 +02:00
|
|
|
|
mutator2.init();
|
2016-09-02 03:10:27 +02:00
|
|
|
|
|
2016-09-03 19:54:54 +02:00
|
|
|
|
CHECK (isnil (target)); // old content moved aside, visible new content still void
|
2016-09-02 03:10:27 +02:00
|
|
|
|
|
|
|
|
|
|
CHECK (mutator2.matchSrc (ATTRIB1)); // current head element of src "matches" the given spec
|
|
|
|
|
|
CHECK (isnil (target)); // the match didn't change anything
|
|
|
|
|
|
|
2016-09-03 19:54:54 +02:00
|
|
|
|
CHECK (mutator2.accept_until(ATTRIB3)); // accept and fast forward behind a given value // accept_until
|
2016-09-02 03:10:27 +02:00
|
|
|
|
CHECK (!isnil (target)); // the fast forward did indeed accept some entries
|
|
|
|
|
|
|
2016-09-03 19:54:54 +02:00
|
|
|
|
CHECK (mutator2.matchSrc (ATTRIB3)); // we had a duplicate attribute entry (and Record<GenNode>
|
|
|
|
|
|
// indeed represents duplicates), so this is waiting next
|
|
|
|
|
|
|
|
|
|
|
|
CHECK (mutator2.accept_until(Ref::ATTRIBS)); // accept_until
|
|
|
|
|
|
|
|
|
|
|
|
CHECK (mutator2.hasSrc()); // ...we did a "blind" fast forward, accepting all attributes
|
|
|
|
|
|
CHECK (mutator2.matchSrc (CHILD_B)); // thus we're now located behind the attributes, at first child
|
|
|
|
|
|
mutator2.injectNew (ATTRIB2); // ..no one prevents us from injecting another attribute... // injectNew
|
|
|
|
|
|
|
|
|
|
|
|
CHECK (mutator2.matchSrc (CHILD_B)); // first child still waiting in src is CHILD_B
|
2016-09-02 03:10:27 +02:00
|
|
|
|
mutator2.skipSrc (CHILD_B); // ...which will be skipped (and thus discarded) // skipSrc
|
2016-09-03 19:54:54 +02:00
|
|
|
|
mutator2.injectNew (SUB_NODE); // inject a nested sub-structure (here a Record<GenNode>) // injectNew
|
2016-09-02 03:10:27 +02: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
|
|
|
|
|
|
CHECK (mutator2.hasSrc());
|
|
|
|
|
|
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
|
|
|
|
|
|
CHECK (mutator2.hasSrc());
|
|
|
|
|
|
CHECK (mutator2.findSrc (CHILD_T)); // search for an element further down into src... // findSrc
|
|
|
|
|
|
CHECK (mutator2.matchSrc (CHILD_B)); // element at head of src is still CHILD_B (as before)
|
|
|
|
|
|
CHECK (mutator2.acceptSrc (CHILD_B)); // now pick and accept this src element as child // acceptSrc
|
|
|
|
|
|
|
|
|
|
|
|
CHECK (mutator2.hasSrc()); // next we have to clean up waste
|
|
|
|
|
|
mutator2.skipSrc (CHILD_T); // left behind by the findSrc() operation // skipSrc
|
|
|
|
|
|
CHECK (not mutator2.hasSrc()); // source contents exhausted
|
|
|
|
|
|
CHECK (not mutator2.acceptSrc (CHILD_T)); // ...anything beyond is NOP
|
|
|
|
|
|
CHECK (mutator2.completeScope()); // no pending elements left, everything resolved
|
|
|
|
|
|
|
|
|
|
|
|
// verify reordered shape
|
2016-09-03 19:54:54 +02:00
|
|
|
|
CHECK (!isnil (root)); // nonempty -- content has been moved back
|
|
|
|
|
|
CHECK (Rec::TYPE_NIL == root.getType()); // type field was not touched
|
|
|
|
|
|
CHECK (1 == root.get("α").data.get<int>()); // all attributes accessible
|
|
|
|
|
|
CHECK (2 == root.get("β").data.get<int64_t>());
|
|
|
|
|
|
CHECK (3.45 == root.get("γ").data.get<double>());
|
|
|
|
|
|
auto attrs = root.attribs(); // verify the sequence of attributes...
|
|
|
|
|
|
CHECK ( *attrs == ATTRIB1); // first attribute "α" was left as it was
|
|
|
|
|
|
CHECK (*++attrs == ATTRIB3); // same for the attribute "γ"
|
|
|
|
|
|
CHECK (*++attrs == ATTRIB3); // ...and its duplicate
|
|
|
|
|
|
CHECK (*++attrs == ATTRIB2); // and here is the newly inserted "β"
|
|
|
|
|
|
CHECK (isnil (++attrs));
|
|
|
|
|
|
scope = root.scope(); // look into the scope contents...
|
|
|
|
|
|
CHECK ( *scope == SUB_NODE); // first the new empty nested child node
|
|
|
|
|
|
CHECK (*++scope == CHILD_T); // but now followed immediately by CHILD_T
|
|
|
|
|
|
CHECK (*++scope == CHILD_B); // while CHILD_B has be shuffled back
|
|
|
|
|
|
CHECK (isnil (++scope)); // ...and that's all
|
2016-09-02 03:10:27 +02:00
|
|
|
|
|
|
|
|
|
|
cout << "Content after reordering...."
|
2016-09-03 19:54:54 +02:00
|
|
|
|
<< renderRecord(target) <<endl;
|
2016-09-02 03:10:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// --- third round: mutate data and sub-scopes ---
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-09-03 20:17:46 +02:00
|
|
|
|
// since our target data here is comprised of GenNode elements,
|
|
|
|
|
|
// we're both able to assign data and to enter a nested Record<GenNode>
|
|
|
|
|
|
// The default configuration is outfitted for this use as-is right away.
|
2016-09-02 03:10:27 +02:00
|
|
|
|
auto mutator3 =
|
|
|
|
|
|
TreeMutator::build()
|
2016-09-03 20:17:46 +02:00
|
|
|
|
.attach (target);
|
2016-09-02 03:10:27 +02:00
|
|
|
|
|
2016-10-03 23:54:09 +02:00
|
|
|
|
mutator3.init();
|
|
|
|
|
|
|
2016-09-02 03:10:27 +02:00
|
|
|
|
CHECK (isnil (target));
|
|
|
|
|
|
CHECK (mutator3.matchSrc (ATTRIB1)); // new mutator starts out anew at the beginning
|
|
|
|
|
|
CHECK (mutator3.accept_until (CHILD_T)); // fast forward behind the second-last child (CHILD_T) // accept_until
|
|
|
|
|
|
CHECK (mutator3.matchSrc (CHILD_B)); // this /would/ be the next source element, but rather...
|
|
|
|
|
|
CHECK (not mutator3.completeScope()); // CHILD_B is still pending, not done yet...
|
|
|
|
|
|
CHECK (mutator3.accept_until (Ref::END)); // fast forward, since we do not want to re-order anything // accept_until
|
|
|
|
|
|
CHECK ( mutator3.completeScope()); // now any pending elements where default-resolved
|
|
|
|
|
|
|
2016-09-03 20:17:46 +02:00
|
|
|
|
CHECK (not contains(renderRecord(target), "γ = 3.1415927"));
|
|
|
|
|
|
CHECK (mutator3.assignElm(GAMMA_PI)); // ...we assign a new payload to the designated element // assignElm
|
|
|
|
|
|
CHECK ( contains(renderRecord(target), "γ = 3.1415927"));
|
2016-09-02 03:10:27 +02:00
|
|
|
|
CHECK ( mutator3.completeScope());
|
2016-09-04 20:55:21 +02:00
|
|
|
|
cout << "Content after assignment...."
|
2016-09-03 20:17:46 +02:00
|
|
|
|
<< renderRecord(target) <<endl;
|
2016-09-02 03:10:27 +02:00
|
|
|
|
|
2016-09-03 20:17:46 +02:00
|
|
|
|
// Note: it is up to the implementation of the target data how to deal with duplicate attributes
|
|
|
|
|
|
// Record<GenNode> represents attributes as a list of named sub GenNode elements, and the
|
|
|
|
|
|
// access to attributes uses the first match found
|
|
|
|
|
|
attrs = root.attribs(); // visit all attributes sequentially...
|
|
|
|
|
|
CHECK ( *attrs == ATTRIB1); // first attribute "α" was left as it was
|
|
|
|
|
|
CHECK (*++attrs == GAMMA_PI); // this is where the value assignment happened...
|
|
|
|
|
|
CHECK ( attrs->data.get<double>() == GAMMA_PI.data.get<double>());
|
|
|
|
|
|
CHECK (*++attrs == ATTRIB3); // ...while the duplicate "γ"...
|
|
|
|
|
|
CHECK ( attrs->data.get<double>() == 3.45); // ...still holds the original value
|
|
|
|
|
|
CHECK (*++attrs == ATTRIB2);
|
|
|
|
|
|
CHECK (isnil (++attrs));
|
2016-09-02 03:10:27 +02:00
|
|
|
|
|
|
|
|
|
|
// prepare for recursion into sub scope..
|
|
|
|
|
|
// Since this is a demonstration, we do not actually recurse into anything,
|
2016-09-03 21:05:16 +02:00
|
|
|
|
// rather we just let the binding generate a nested mutator into some buffer
|
|
|
|
|
|
// and then we invoke the operations this nested mutator right from here.
|
2016-09-02 03:10:27 +02:00
|
|
|
|
|
|
|
|
|
|
InPlaceBuffer<TreeMutator, sizeof(mutator1)> subMutatorBuffer;
|
|
|
|
|
|
TreeMutator::Handle placementHandle(subMutatorBuffer);
|
|
|
|
|
|
|
|
|
|
|
|
CHECK (mutator3.mutateChild (SUB_NODE, placementHandle)); // mutateChild
|
|
|
|
|
|
|
2016-09-03 21:05:16 +02:00
|
|
|
|
GenNode const& subNode = *root.scope();
|
|
|
|
|
|
CHECK (subNode == SUB_NODE); // ...this is the sub node
|
|
|
|
|
|
CHECK (isnil (subNode.data.get<Rec>())); // where the nested mutator is expected to work on
|
2016-09-02 03:10:27 +02:00
|
|
|
|
|
|
|
|
|
|
// now use the Mutator *interface* to talk to the nested mutator...
|
2016-09-03 21:05:16 +02:00
|
|
|
|
// which was built and placed into the provided buffer
|
|
|
|
|
|
CHECK (not subMutatorBuffer->hasSrc());
|
2016-09-02 03:10:27 +02:00
|
|
|
|
subMutatorBuffer->injectNew (TYPE_X); // >> // injectNew
|
|
|
|
|
|
subMutatorBuffer->injectNew (ATTRIB2); // >> // injectNew
|
|
|
|
|
|
subMutatorBuffer->injectNew (CHILD_B); // >> // injectNew
|
|
|
|
|
|
subMutatorBuffer->injectNew (CHILD_A); // >> // injectNew
|
|
|
|
|
|
|
2016-09-03 21:05:16 +02:00
|
|
|
|
Rec const& nestedRec = subNode.data.get<Rec>();
|
|
|
|
|
|
CHECK (not isnil (nestedRec)); // ...and "magically" these instructions happened to insert
|
|
|
|
|
|
cout << "Sub-" << renderNode(subNode) <<endl; // some new content into our implementation defined sub scope!
|
2016-09-02 03:10:27 +02:00
|
|
|
|
|
|
|
|
|
|
// verify contents of nested scope after mutation
|
2016-09-03 22:34:36 +02:00
|
|
|
|
CHECK ("ξ" == nestedRec.getType()); // type of nested node has been set to Xi
|
2016-09-03 21:05:16 +02:00
|
|
|
|
attrs = nestedRec.attribs(); // look into the nested node's attributes...
|
2016-09-03 22:34:36 +02:00
|
|
|
|
CHECK ( *attrs == ATTRIB2);
|
2016-09-03 21:05:16 +02:00
|
|
|
|
CHECK (isnil (++attrs));
|
|
|
|
|
|
scope = nestedRec.scope(); // look into the nested nodes's scope contents...
|
|
|
|
|
|
CHECK ( *scope == CHILD_B);
|
|
|
|
|
|
CHECK (*++scope == CHILD_A);
|
|
|
|
|
|
CHECK (isnil (++scope)); // ...and that's all
|
2016-09-02 03:10:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// now back to parent scope....
|
|
|
|
|
|
// ...add a new attribute and immediately recurse into it
|
2016-09-03 21:05:16 +02:00
|
|
|
|
mutator3.injectNew (ATTRIB_NODE);
|
2016-09-02 03:10:27 +02:00
|
|
|
|
CHECK (mutator3.mutateChild (ATTRIB_NODE, placementHandle)); // NOTE: we're just recycling the buffer. InPlaceHolder handles lifecycle properly
|
2016-09-03 22:34:36 +02:00
|
|
|
|
subMutatorBuffer->injectNew (TYPE_X);
|
2016-09-02 03:10:27 +02:00
|
|
|
|
subMutatorBuffer->injectNew (CHILD_A);
|
|
|
|
|
|
subMutatorBuffer->injectNew (CHILD_A);
|
|
|
|
|
|
subMutatorBuffer->injectNew (CHILD_A);
|
2016-09-03 22:34:36 +02:00
|
|
|
|
subMutatorBuffer->assignElm (TYPE_Z); // NOTE use assignment to *change* the type field
|
2016-09-02 03:10:27 +02:00
|
|
|
|
CHECK (subMutatorBuffer->completeScope()); // no pending "open ends" left in sub-scope
|
|
|
|
|
|
CHECK (mutator3.completeScope()); // and likewise in the enclosing main scope
|
|
|
|
|
|
|
|
|
|
|
|
// and thus we've gotten a second nested scope, populated with new values
|
2016-09-03 21:05:16 +02:00
|
|
|
|
Rec const& attrRec = root.get("δ").data.get<Rec>();
|
|
|
|
|
|
cout << "Att-" << renderNode(attrRec) <<endl;
|
2016-09-02 03:10:27 +02:00
|
|
|
|
|
|
|
|
|
|
// verify contents of this second nested scope
|
2016-09-03 21:05:16 +02:00
|
|
|
|
CHECK (not isnil (attrRec));
|
2016-09-03 22:34:36 +02:00
|
|
|
|
CHECK ("ζ" == attrRec.getType());
|
|
|
|
|
|
CHECK (isnil (attrRec.attribs()));
|
2016-09-03 21:05:16 +02:00
|
|
|
|
scope = attrRec.scope();
|
|
|
|
|
|
CHECK (not isnil (scope));
|
|
|
|
|
|
CHECK ( *scope == CHILD_A);
|
|
|
|
|
|
CHECK (*++scope == CHILD_A);
|
|
|
|
|
|
CHECK (*++scope == CHILD_A);
|
|
|
|
|
|
CHECK (isnil (++scope));
|
2016-09-02 03:10:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// back to parent scope....
|
2016-09-03 21:05:16 +02:00
|
|
|
|
// verify the parent scope indeed contains the nested elements in new shape
|
|
|
|
|
|
CHECK (contains (renderRecord(target), renderRecord(attrRec)));
|
|
|
|
|
|
CHECK (contains (renderRecord(target), renderRecord(nestedRec)));
|
2016-09-02 03:10:27 +02:00
|
|
|
|
|
2016-09-04 20:55:21 +02:00
|
|
|
|
cout << "Content after sub mutation.."
|
2016-09-03 21:05:16 +02:00
|
|
|
|
<< renderRecord(target) <<endl;
|
2015-04-02 03:30:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Register this test class... */
|
2016-05-26 02:16:34 +02:00
|
|
|
|
LAUNCHER (TreeMutatorBinding_test, "unit common");
|
2015-04-02 03:30:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}}} // namespace lib::diff::test
|