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)
5  2016, Hermann Vosseler <Ichthyostega@web.de>
6 
7   **Lumiera** is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by the
9   Free Software Foundation; either version 2 of the License, or (at your
10   option) any later version. See the file COPYING for further details.
11 
12 * *****************************************************************/
13 
19 #include "lib/test/run.hpp"
20 #include "lib/test/test-helper.hpp"
21 #include "lib/time/timevalue.hpp"
23 #include "lib/format-cout.hpp"
24 #include "lib/format-util.hpp"
25 
26 #include <string>
27 
28 using lib::Symbol;
29 using lib::Variant;
30 using lib::idi::EntryID;
31 using lib::diff::Rec;
32 using lib::diff::MakeRec;
33 using lib::diff::GenNode;
34 using lib::meta::Types;
35 using lib::meta::Tuple;
38 using lib::time::Time;
39 using lib::hash::LuidH;
40 
41 using std::string;
42 using std::tuple;
43 using std::get;
44 
45 namespace lib {
46 namespace meta {
47 namespace test {
48 
49  using LERR_(WRONG_TYPE);
50 
51 
52 
53 
54  /*************************************************************************/
72  class TupleRecordInit_test : public Test
73  {
74  virtual void
75  run (Arg)
76  {
77  show_simpleUsage();
78  verify_errorHandling();
79  }
80 
81 
82  void
83  show_simpleUsage()
84  {
85  using NiceTypes = Types<string, int>;
86  using UgglyTypes = Types<EntryID<long>, Symbol, int, int64_t, double, Duration>; // various conversions and an immutable type (Duration)
87 
88  Rec args = MakeRec().scope("lalü", 42);
89  Rec urgs = MakeRec().scope("lalü", "lala", 12, 34, 5.6, Time(7,8,9));
90 
91  cout << args <<endl;
92  cout << urgs <<endl;
93 
94  cout << buildTuple<NiceTypes> (args) <<endl;
95  cout << buildTuple<UgglyTypes> (urgs) <<endl;
96  }
97 
98 
99  void
100  verify_errorHandling()
101  {
102  Rec args = MakeRec().scope("surprise", 42);
103 
104  using TooMany = Types<string, int, long>;
105  VERIFY_ERROR (WRONG_TYPE, buildTuple<TooMany> (args)); // number of types in tuple exceeds capacity of the supplied argument record
106 
107  using Unsigned = Types<string, uint>;
108  using Floating = Types<string, float>;
109  using Narrowing = Types<string, short>;
110  VERIFY_ERROR (WRONG_TYPE, buildTuple<Unsigned> (args)); // dangerous conversion from signed to unsigned int is prohibited
111  VERIFY_ERROR (WRONG_TYPE, buildTuple<Floating> (args)); // conversion from integral to floating point element is prohibited
112  VERIFY_ERROR (WRONG_TYPE, buildTuple<Narrowing> (args)); // narrowing conversion from int to short is prohibited
113 
114  // yet other (non-numeric) conversions are still possible
115  Rec timeArg = MakeRec().scope(Time(1,2,3,4));
116  using TupStr = Types<string>;
117  Tuple<TupStr> tup = buildTuple<TupStr> (timeArg);
118 
119  CHECK (std::get<string> (tup) == "4:03:02.001");
120  CHECK (string(Time(1,2,3,4)) == "4:03:02.001");
121 
122 
123  // conversions from LUID elements are handled restrictively
124  Rec hashArg = MakeRec().scope("random", LuidH());
125  VERIFY_ERROR (WRONG_TYPE, buildTuple<Unsigned> (args));
126  VERIFY_ERROR (WRONG_TYPE, buildTuple<Floating> (args));
127  VERIFY_ERROR (WRONG_TYPE, buildTuple<Narrowing> (args));
128 
129  using ToSizeT = Types<string, size_t>;
130  VERIFY_ERROR (WRONG_TYPE, (buildTuple<ToSizeT> (args))); // not even conversion to size_t is allowed
131 
132  struct Dummy
133  {
134  HashVal hash;
135 
136  Dummy (LuidH const& luid)
137  : hash(luid)
138  { }
139  };
140 
141  using WithDummy = Types<string, Dummy>;
142 
143  Tuple<WithDummy> tup2 = buildTuple<WithDummy> (hashArg); // while any type explicitly constructible from LUID are permitted.
144  VERIFY_ERROR (WRONG_TYPE, buildTuple<WithDummy> (args)); // building a Dummy from int(42) is disallowed, of course
145 
146  HashVal h = get<Dummy>(tup2).hash;
147  CHECK (h == hashArg.child(1).data.get<LuidH>()); // note: the narrowing conversion happens within LuidH::operator HashVal()
148  }
149  };
150 
151 
153  LAUNCHER (TupleRecordInit_test, "unit meta");
154 
155 
156 
157 }}} // namespace lib::meta::test
a mutable time value, behaving like a plain number, allowing copy and re-accessing ...
Definition: timevalue.hpp:232
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:40
Typesafe union record.
Definition: variant.hpp:215
typed symbolic and hash ID for asset-like position accounting.
Definition: entry-id.hpp:126
#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:299
Token or Atom with distinct identity.
Definition: symbol.hpp:117
Specialised adapter to consume a record of GenNode entries to build a tuple.
Simplistic 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:52
Collection of small helpers and convenience shortcuts for diagnostics & formatting.
Duration is the internal Lumiera time metric.
Definition: timevalue.hpp:468
a family of time value like entities and their relationships.
object-like record of data.
Definition: record.hpp:141
generic data element node within a tree
Definition: gen-node.hpp:222