2015-10-02 18:47:44 +02:00
|
|
|
|
/*
|
2019-12-12 23:41:26 +01:00
|
|
|
|
DiffTreeApplication(Test) - demonstrate the main features of tree diff representation
|
2015-10-02 18:47:44 +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)
|
|
|
|
|
|
2015, Hermann Vosseler <Ichthyostega@web.de>
|
2015-10-02 18:47:44 +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-10-02 18:47:44 +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-10-02 18:47:44 +02:00
|
|
|
|
|
2017-02-22 01:54:20 +01:00
|
|
|
|
/** @file diff-tree-application-test.cpp
|
2017-08-11 15:23:33 +02:00
|
|
|
|
** unit test \ref DiffTreeApplication_test.
|
2019-12-12 23:41:26 +01:00
|
|
|
|
** Demonstrates the most relevant operations for reshaping structured data
|
|
|
|
|
|
** through a tree-diff sequence. Here, we use a lib::diff:Record<GenNode>
|
|
|
|
|
|
** as target data, and the test focuses on demonstrating the possible operations.
|
|
|
|
|
|
** @see [Introductory demonstration](\ref diff-tree-application-simple-test.cpp)
|
|
|
|
|
|
** @see [More complex setup with opaque data](\ref diff-complex-application-test.cpp)
|
2016-11-03 18:20:10 +01:00
|
|
|
|
*/
|
|
|
|
|
|
|
2015-10-02 18:47:44 +02:00
|
|
|
|
|
|
|
|
|
|
#include "lib/test/run.hpp"
|
2015-11-15 01:30:08 +01:00
|
|
|
|
#include "lib/format-util.hpp"
|
2015-10-02 18:47:44 +02:00
|
|
|
|
#include "lib/diff/tree-diff-application.hpp"
|
|
|
|
|
|
#include "lib/iter-adapter-stl.hpp"
|
2015-10-09 03:03:27 +02:00
|
|
|
|
#include "lib/time/timevalue.hpp"
|
2015-10-10 03:05:30 +02:00
|
|
|
|
#include "lib/format-util.hpp"
|
2015-10-02 18:47:44 +02:00
|
|
|
|
#include "lib/util.hpp"
|
|
|
|
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
|
|
using lib::iter_stl::snapshot;
|
2015-10-10 03:05:30 +02:00
|
|
|
|
using util::isnil;
|
|
|
|
|
|
using util::join;
|
2015-10-02 18:47:44 +02:00
|
|
|
|
using std::string;
|
|
|
|
|
|
using std::vector;
|
2015-10-09 03:03:27 +02:00
|
|
|
|
using lib::time::Time;
|
2015-10-02 18:47:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace lib {
|
|
|
|
|
|
namespace diff{
|
|
|
|
|
|
namespace test{
|
|
|
|
|
|
|
|
|
|
|
|
namespace {//Test fixture....
|
|
|
|
|
|
|
2015-10-09 03:03:27 +02:00
|
|
|
|
// define some GenNode elements
|
|
|
|
|
|
// to act as templates within the concrete diff
|
|
|
|
|
|
// NOTE: everything in this diff language is by-value
|
2017-08-11 15:23:33 +02:00
|
|
|
|
const GenNode ATTRIB1("α", 1), // attribute α = 1
|
2015-11-14 21:20:32 +01:00
|
|
|
|
ATTRIB2("β", int64_t(2)), // attribute α = 2L (int64_t)
|
2015-10-09 03:03:27 +02:00
|
|
|
|
ATTRIB3("γ", 3.45), // attribute γ = 3.45 (double)
|
2015-11-01 07:03:47 +01:00
|
|
|
|
TYPE_X("type", "X"), // a "magic" type attribute "X"
|
2015-10-09 03:03:27 +02:00
|
|
|
|
TYPE_Y("type", "Y"), //
|
|
|
|
|
|
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
|
2015-11-01 07:03:47 +01:00
|
|
|
|
ATTRIB_NODE = MakeRec().genNode("δ"), // empty named node to be attached as attribute δ
|
2015-10-10 03:05:30 +02:00
|
|
|
|
CHILD_NODE = SUB_NODE; // yet another child node, same ID as SUB_NODE (!)
|
2015-10-02 18:47:44 +02:00
|
|
|
|
|
|
|
|
|
|
}//(End)Test fixture
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************//**
|
2015-11-01 07:03:47 +01:00
|
|
|
|
* @test Demonstration/Concept: a description language for tree differences.
|
2015-10-02 18:47:44 +02:00
|
|
|
|
* The representation is given as a linearised sequence of verb tokens.
|
2015-11-01 07:03:47 +01:00
|
|
|
|
* In addition to the verbs used for list diffing, here we additionally
|
|
|
|
|
|
* have to deal with nested scopes, which can be entered thorough a
|
|
|
|
|
|
* bracketing construct \c mut(ID)...emu(ID).
|
|
|
|
|
|
* This test demonstrates the application of such diff sequences
|
|
|
|
|
|
* - in the first step, an empty root #Record<GenNode> is populated
|
|
|
|
|
|
* with a type-ID, three named attribute values, three child values
|
|
|
|
|
|
* and a nested child-Record.
|
|
|
|
|
|
* - the second step demonstrates various diff language constructs
|
|
|
|
|
|
* to alter, reshape and mutate this data structure
|
2019-12-12 23:41:26 +01:00
|
|
|
|
* After applying those two diff sequences, we verify that
|
|
|
|
|
|
* the mutated data is indeed in the reordered shape.
|
2015-11-01 07:03:47 +01:00
|
|
|
|
* @remarks to follow this test, you should be familiar both with our
|
2017-08-12 14:48:35 +02:00
|
|
|
|
* [generic data record](\ref diff::Record), as well as with the
|
|
|
|
|
|
* [variant data node](\ref diff::GenNode). The key point to note
|
|
|
|
|
|
* is the usage of Record elements as payload within GenNode, which
|
2019-12-12 23:41:26 +01:00
|
|
|
|
* allows to represent tree shaped object-like data structures.
|
2017-08-13 07:09:06 +02:00
|
|
|
|
* @note literally the same test case is repeated in MutationMessage_test,
|
|
|
|
|
|
* just there the diff is transported in a MutationMessage capsule,
|
2019-12-12 23:41:26 +01:00
|
|
|
|
* as would be the case in the real application.
|
2016-08-29 17:52:35 +02:00
|
|
|
|
* @see DiffComplexApplication_test handling arbitrary data structures
|
2019-12-12 23:41:26 +01:00
|
|
|
|
* @see DiffTreeApplicationSimple_test introductory example demonstration
|
|
|
|
|
|
* @see GenericRecord_test
|
|
|
|
|
|
* @see GenNode_test
|
2015-11-01 07:03:47 +01:00
|
|
|
|
* @see DiffListApplication_test
|
2025-11-05 02:55:45 +01:00
|
|
|
|
* @see tree-diff-application.hpp
|
2015-11-01 07:03:47 +01:00
|
|
|
|
* @see tree-diff.hpp
|
2015-10-02 18:47:44 +02:00
|
|
|
|
*/
|
2015-10-10 01:29:58 +02:00
|
|
|
|
class DiffTreeApplication_test
|
|
|
|
|
|
: public Test
|
|
|
|
|
|
, TreeDiffLanguage
|
2015-10-02 18:47:44 +02:00
|
|
|
|
{
|
2015-10-10 01:29:58 +02:00
|
|
|
|
using DiffSeq = iter_stl::IterSnapshot<DiffStep>;
|
|
|
|
|
|
|
|
|
|
|
|
DiffSeq
|
|
|
|
|
|
populationDiff()
|
|
|
|
|
|
{
|
|
|
|
|
|
return snapshot({ins(TYPE_X)
|
|
|
|
|
|
, ins(ATTRIB1)
|
|
|
|
|
|
, ins(ATTRIB2)
|
|
|
|
|
|
, ins(ATTRIB3)
|
|
|
|
|
|
, ins(CHILD_A)
|
|
|
|
|
|
, ins(CHILD_T)
|
|
|
|
|
|
, ins(CHILD_T)
|
|
|
|
|
|
, ins(SUB_NODE)
|
|
|
|
|
|
, mut(SUB_NODE)
|
|
|
|
|
|
, ins(CHILD_B)
|
|
|
|
|
|
, ins(CHILD_A)
|
|
|
|
|
|
, emu(SUB_NODE)
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DiffSeq
|
|
|
|
|
|
mutationDiff()
|
|
|
|
|
|
{
|
2016-04-16 02:20:23 +02:00
|
|
|
|
// prepare for direct assignment of new value
|
2016-02-19 17:22:41 +01:00
|
|
|
|
// NOTE: the target ID will be reconstructed, including hash
|
|
|
|
|
|
GenNode childA_upper(CHILD_A.idi.getSym(), "A");
|
|
|
|
|
|
|
2015-11-01 02:33:35 +01:00
|
|
|
|
return snapshot({after(Ref::ATTRIBS) // fast forward to the first child
|
2015-10-10 01:29:58 +02:00
|
|
|
|
, find(CHILD_T)
|
|
|
|
|
|
, pick(CHILD_A)
|
|
|
|
|
|
, skip(CHILD_T)
|
|
|
|
|
|
, del(CHILD_T)
|
2016-09-05 04:04:02 +02:00
|
|
|
|
, after(Ref::END) // accept anything beyond as-is
|
|
|
|
|
|
, mut(SUB_NODE)
|
2015-10-10 01:29:58 +02:00
|
|
|
|
, ins(ATTRIB3)
|
2015-11-01 07:03:47 +01:00
|
|
|
|
, ins(ATTRIB_NODE) // attributes can also be nested objects
|
2015-10-10 01:29:58 +02:00
|
|
|
|
, find(CHILD_A)
|
|
|
|
|
|
, del(CHILD_B)
|
|
|
|
|
|
, ins(CHILD_NODE)
|
|
|
|
|
|
, ins(CHILD_T)
|
|
|
|
|
|
, skip(CHILD_A)
|
|
|
|
|
|
, mut(CHILD_NODE)
|
|
|
|
|
|
, ins(TYPE_Y)
|
|
|
|
|
|
, ins(ATTRIB2)
|
|
|
|
|
|
, emu(CHILD_NODE)
|
2016-02-19 17:22:41 +01:00
|
|
|
|
, set(childA_upper) // direct assignment, target found by ID (out of order)
|
2015-11-01 07:03:47 +01:00
|
|
|
|
, mut(ATTRIB_NODE) // mutation can be out-of order, target found by ID
|
2015-10-10 01:29:58 +02:00
|
|
|
|
, ins(CHILD_A)
|
|
|
|
|
|
, ins(CHILD_A)
|
|
|
|
|
|
, ins(CHILD_A)
|
|
|
|
|
|
, emu(ATTRIB_NODE)
|
2016-09-05 04:04:02 +02:00
|
|
|
|
, emu(SUB_NODE)
|
2015-10-10 01:29:58 +02:00
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-10-02 18:47:44 +02:00
|
|
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
|
|
run (Arg)
|
|
|
|
|
|
{
|
2015-10-23 01:32:47 +02:00
|
|
|
|
Rec::Mutator target;
|
|
|
|
|
|
Rec& subject = target;
|
|
|
|
|
|
DiffApplicator<Rec::Mutator> application(target);
|
2015-11-01 07:03:47 +01:00
|
|
|
|
|
|
|
|
|
|
// Part I : apply diff to populate
|
2017-08-12 14:48:35 +02:00
|
|
|
|
application.consume (populationDiff());
|
2015-10-02 19:41:14 +02:00
|
|
|
|
|
2015-11-01 07:03:47 +01:00
|
|
|
|
CHECK (!isnil (subject)); // nonempty -- content has been added
|
|
|
|
|
|
CHECK ("X" == subject.getType()); // type was set to "X"
|
|
|
|
|
|
CHECK (1 == subject.get("α").data.get<int>()); // has gotten our int attribute "α"
|
|
|
|
|
|
CHECK (2L == subject.get("β").data.get<int64_t>()); // ... the long attribute "β"
|
|
|
|
|
|
CHECK (3.45 == subject.get("γ").data.get<double>()); // ... and double attribute "γ"
|
|
|
|
|
|
auto scope = subject.scope(); // look into the scope contents...
|
|
|
|
|
|
CHECK ( *scope == CHILD_A); // there is CHILD_A
|
|
|
|
|
|
CHECK (*++scope == CHILD_T); // followed by a copy of CHILD_T
|
|
|
|
|
|
CHECK (*++scope == CHILD_T); // and another copy of CHILD_T
|
2025-06-07 23:59:57 +02:00
|
|
|
|
CHECK (*++scope == MakeRec().appendChild(CHILD_B) // and there is a nested Record
|
2015-11-01 07:03:47 +01:00
|
|
|
|
.appendChild(CHILD_A) // with CHILD_B
|
|
|
|
|
|
.genNode(SUB_NODE.idi.getSym())); // and CHILD_A
|
|
|
|
|
|
CHECK (isnil(++scope)); // thats all -- no more children
|
2015-10-02 18:47:44 +02:00
|
|
|
|
|
2015-11-01 07:03:47 +01:00
|
|
|
|
// Part II : apply the second diff
|
2017-08-12 14:48:35 +02:00
|
|
|
|
application.consume (mutationDiff());
|
2025-06-07 23:59:57 +02:00
|
|
|
|
CHECK (join (subject.keys()) == "α, β, γ"); // the attributes weren't altered
|
2015-11-01 07:03:47 +01:00
|
|
|
|
scope = subject.scope(); // but the scope was reordered
|
|
|
|
|
|
CHECK ( *scope == CHILD_T); // CHILD_T
|
|
|
|
|
|
CHECK (*++scope == CHILD_A); // CHILD_A
|
|
|
|
|
|
Rec nested = (++scope)->data.get<Rec>(); // and our nested Record, which too has been altered:
|
|
|
|
|
|
CHECK (nested.get("γ").data.get<double>() == 3.45); // it carries now an attribute "δ", which is again
|
|
|
|
|
|
CHECK (nested.get("δ") == MakeRec().appendChild(CHILD_A) // a nested Record with three children CHILD_A
|
2025-06-07 23:59:57 +02:00
|
|
|
|
.appendChild(CHILD_A) //
|
|
|
|
|
|
.appendChild(CHILD_A) //
|
|
|
|
|
|
.genNode("δ")); //
|
2015-11-01 07:03:47 +01:00
|
|
|
|
auto subScope = nested.scope(); // and within the nested sub-scope we find
|
2017-08-12 14:48:35 +02:00
|
|
|
|
CHECK ( *subScope != CHILD_A); // CHILD_A has been altered by assignment
|
2016-02-19 17:22:41 +01:00
|
|
|
|
CHECK (CHILD_A.idi == subScope->idi); // ...: same ID as CHILD_A
|
|
|
|
|
|
CHECK ("A" == subScope->data.get<string>()); // ...: but mutated payload
|
2015-11-01 07:03:47 +01:00
|
|
|
|
CHECK (*++subScope == MakeRec().type("Y") // a yet-again nested sub-Record of type "Y"
|
2015-11-14 21:20:32 +01:00
|
|
|
|
.set("β", int64_t(2)) // with just an attribute "β" == 2L
|
2015-11-01 07:03:47 +01:00
|
|
|
|
.genNode(CHILD_NODE.idi.getSym())); // (and an empty child scope)
|
|
|
|
|
|
CHECK (*++subScope == CHILD_T); // followed by another copy of CHILD_T
|
2025-06-07 23:59:57 +02:00
|
|
|
|
CHECK (isnil (++subScope)); //
|
2015-11-01 07:03:47 +01:00
|
|
|
|
CHECK (isnil (++scope)); // and nothing beyond that.
|
2015-10-02 18:47:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Register this test class... */
|
|
|
|
|
|
LAUNCHER (DiffTreeApplication_test, "unit common");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}}} // namespace lib::diff::test
|