Lumiera  0.pre.03
»edit your freedom«
tuple-record-init-test.cpp
Go to the documentation of this file.
1 /*
2  TupleRecordInit(Test) - to build a tuple from a GenNode sequence
3 
4  Copyright (C) Lumiera.org
5  2016, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 * *****************************************************/
22 
28 #include "lib/test/run.hpp"
29 #include "lib/test/test-helper.hpp"
30 #include "lib/time/timevalue.hpp"
32 #include "lib/format-cout.hpp"
33 #include "lib/format-util.hpp"
34 
35 #include <string>
36 
37 using lib::Symbol;
38 using lib::Variant;
39 using lib::idi::EntryID;
40 using lib::diff::Rec;
41 using lib::diff::MakeRec;
42 using lib::diff::GenNode;
43 using lib::meta::Types;
44 using lib::meta::Tuple;
47 using lib::time::Time;
48 using lib::hash::LuidH;
49 
50 using std::string;
51 using std::tuple;
52 using std::get;
53 
54 namespace lib {
55 namespace meta {
56 namespace test {
57 
58  using LERR_(WRONG_TYPE);
59 
60 
61 
62 
63  /*************************************************************************/
81  class TupleRecordInit_test : public Test
82  {
83  virtual void
84  run (Arg)
85  {
86  show_simpleUsage();
87  verify_errorHandling();
88  }
89 
90 
91  void
92  show_simpleUsage()
93  {
94  using NiceTypes = Types<string, int>;
95  using UgglyTypes = Types<EntryID<long>, Symbol, int, int64_t, double, Duration>; // various conversions and an immutable type (Duration)
96 
97  Rec args = MakeRec().scope("lalü", 42);
98  Rec urgs = MakeRec().scope("lalü", "lala", 12, 34, 5.6, Time(7,8,9));
99 
100  cout << args <<endl;
101  cout << urgs <<endl;
102 
103  cout << buildTuple<NiceTypes> (args) <<endl;
104  cout << buildTuple<UgglyTypes> (urgs) <<endl;
105  }
106 
107 
108  void
109  verify_errorHandling()
110  {
111  Rec args = MakeRec().scope("surprise", 42);
112 
113  using TooMany = Types<string, int, long>;
114  VERIFY_ERROR (WRONG_TYPE, buildTuple<TooMany> (args)); // number of types in tuple exceeds capacity of the supplied argument record
115 
116  using Unsigned = Types<string, uint>;
117  using Floating = Types<string, float>;
118  using Narrowing = Types<string, short>;
119  VERIFY_ERROR (WRONG_TYPE, buildTuple<Unsigned> (args)); // dangerous conversion from signed to unsigned int is prohibited
120  VERIFY_ERROR (WRONG_TYPE, buildTuple<Floating> (args)); // conversion from integral to floating point element is prohibited
121  VERIFY_ERROR (WRONG_TYPE, buildTuple<Narrowing> (args)); // narrowing conversion from int to short is prohibited
122 
123  // yet other (non-numeric) conversions are still possible
124  Rec timeArg = MakeRec().scope(Time(1,2,3,4));
125  using TupStr = Types<string>;
126  Tuple<TupStr> tup = buildTuple<TupStr> (timeArg);
127 
128  CHECK (std::get<string> (tup) == "4:03:02.001");
129  CHECK (string(Time(1,2,3,4)) == "4:03:02.001");
130 
131 
132  // conversions from LUID elements are handled restrictively
133  Rec hashArg = MakeRec().scope("random", LuidH());
134  VERIFY_ERROR (WRONG_TYPE, buildTuple<Unsigned> (args));
135  VERIFY_ERROR (WRONG_TYPE, buildTuple<Floating> (args));
136  VERIFY_ERROR (WRONG_TYPE, buildTuple<Narrowing> (args));
137 
138  using ToSizeT = Types<string, size_t>;
139  VERIFY_ERROR (WRONG_TYPE, (buildTuple<ToSizeT> (args))); // not even conversion to size_t is allowed
140 
141  struct Dummy
142  {
143  HashVal hash;
144 
145  Dummy (LuidH const& luid)
146  : hash(luid)
147  { }
148  };
149 
150  using WithDummy = Types<string, Dummy>;
151 
152  Tuple<WithDummy> tup2 = buildTuple<WithDummy> (hashArg); // while any type explicitly constructible from LUID are permitted.
153  VERIFY_ERROR (WRONG_TYPE, buildTuple<WithDummy> (args)); // building a Dummy from int(42) is disallowed, of course
154 
155  HashVal h = get<Dummy>(tup2).hash;
156  CHECK (h == hashArg.child(1).data.get<LuidH>()); // note: the narrowing conversion happens within LuidH::operator HashVal()
157  }
158  };
159 
160 
162  LAUNCHER (TupleRecordInit_test, "unit meta");
163 
164 
165 
166 }}} // namespace lib::meta::test
a mutable time value, behaving like a plain number, allowing copy and re-accessing ...
Definition: timevalue.hpp:241
Automatically use custom string conversion in C++ stream output.
typename BuildTupleType< TYPES >::Type Tuple
Build a std::tuple from types given as type sequence.
Definition: run.hpp:49
Typesafe union record.
Definition: variant.hpp:224
typed symbolic and hash ID for asset-like position accounting.
Definition: entry-id.hpp:135
#define VERIFY_ERROR(ERROR_ID, ERRONEOUS_STATEMENT)
Macro to verify that a statement indeed raises an exception.
Tuple< TYPES > buildTuple(SRC values)
convenience shortcut to build a tuple from some suitable source data.
Implementation namespace for support and library code.
Lumiera&#39;s internal time value datatype.
Definition: timevalue.hpp:308
Token or Atom with distinct identity.
Definition: symbol.hpp:126
Specialised adapter to consume a record of GenNode entries to build a tuple.
Simple test class runner.
A collection of frequently used helper functions to support unit testing.
Hash implementation based on a lumiera unique object id (LUID) When invoking the default ctor...
size_t HashVal
a STL compatible hash value
Definition: hash-value.h:56
Collection of small helpers and convenience shortcuts for diagnostics & formatting.
Duration is the internal Lumiera time metric.
Definition: timevalue.hpp:477
a family of time value like entities and their relationships.
object-like record of data.
Definition: record.hpp:150
generic data element node within a tree
Definition: gen-node.hpp:231