Lumiera 0.pre.04~rc.1
»edit your freedom«
Loading...
Searching...
No Matches
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"
23#include "lib/format-cout.hpp"
24#include "lib/format-util.hpp"
25
26#include <string>
27
28using lib::Symbol;
29using lib::Variant;
31using lib::diff::Rec;
38using lib::time::Time;
40
41using std::string;
42using std::tuple;
43using std::get;
44
45namespace lib {
46namespace meta {
47namespace 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 {
79 }
80
81
82 void
84 {
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
96
97 cout << argT <<endl;
98 cout << urgT <<endl;
99
100 CHECK (get<0>(argT) == "lalü"_expect);
101 CHECK (get<0>(urgT) == "ID<long>-lal"_expect);
102 CHECK (get<1>(urgT) == "lala"_expect);
103 }
104
105
106 void
108 {
109 Rec args = MakeRec().scope("surprise", 42);
110
112 VERIFY_ERROR (WRONG_TYPE, buildTuple<TooMany> (args)); // number of types in tuple exceeds capacity of the supplied argument record
113
117 VERIFY_ERROR (WRONG_TYPE, buildTuple<Unsigned> (args)); // dangerous conversion from signed to unsigned int is prohibited
118 VERIFY_ERROR (WRONG_TYPE, buildTuple<Floating> (args)); // conversion from integral to floating point element is prohibited
119 VERIFY_ERROR (WRONG_TYPE, buildTuple<Narrowing> (args)); // narrowing conversion from int to short is prohibited
120
121 // yet other (non-numeric) conversions are still possible
122 Rec timeArg = MakeRec().scope(Time(1,2,3,4));
123 using TupStr = Types<string>;
125
126 CHECK (std::get<string> (tup) == "4:03:02.001");
127 CHECK (string(Time(1,2,3,4)) == "4:03:02.001");
128
129
130 // conversions from LUID elements are handled restrictively
131 Rec hashArg = MakeRec().scope("random", LuidH());
132 VERIFY_ERROR (WRONG_TYPE, buildTuple<Unsigned> (args));
133 VERIFY_ERROR (WRONG_TYPE, buildTuple<Floating> (args));
135
137 VERIFY_ERROR (WRONG_TYPE, (buildTuple<ToSizeT> (args))); // not even conversion to size_t is allowed
138
139 struct Hashy
140 {
142
143 Hashy (LuidH const& luid)
144 : hash(luid)
145 { }
146 };
147
149
150 Tuple<WithHashy> tup2 = buildTuple<WithHashy> (hashArg); // while any type explicitly constructible from LUID are permitted.
151 VERIFY_ERROR (WRONG_TYPE, buildTuple<WithHashy> (args)); // building a `Hashy` from int(42) is disallowed, of course
152
153 HashVal h = get<Hashy>(tup2).hash;
154 CHECK (h == hashArg.child(1).data.get<LuidH>()); // note: the narrowing conversion happens within LuidH::operator HashVal()
155 }
156 };
157
158
161
162
163
164}}} // namespace lib::meta::test
Token or Atom with distinct identity.
Definition symbol.hpp:120
Typesafe union record.
Definition variant.hpp:216
Mutator && scope(X const &initialiser, ARGS &&...args)
Definition record.hpp:578
object-like record of data.
Definition record.hpp:142
Hash implementation based on a lumiera unique object id (LUID) When invoking the default ctor,...
Duration is the internal Lumiera time metric.
Lumiera's internal time value datatype.
#define LERR_(_NAME_)
Definition error.hpp:45
Automatically use custom string conversion in C++ stream output.
Collection of small helpers and convenience shortcuts for diagnostics & formatting.
#define hash
BuildTupleType< TYPES >::Type Tuple
Build a std::tuple from types given as type sequence.
enable_if_c< Cond::value, T >::type enable_if
SFINAE helper to control the visibility of specialisations and overloads.
Definition meta/util.hpp:87
Tuple< TYPES > buildTuple(SRC &&values)
convenience shortcut to build a tuple from some suitable source data.
variadic sequence of types
Definition typelist.hpp:102
Implementation namespace for support and library code.
size_t HashVal
a STL compatible hash value
Definition hash-value.h:52
Test runner and basic definitions for tests.
Simplistic test class runner.
#define LAUNCHER(_TEST_CLASS_, _GROUPS_)
Definition run.hpp:116
generic data element node within a tree
Definition gen-node.hpp:224
typed symbolic and hash ID for asset-like position accounting.
Definition entry-id.hpp:219
A collection of frequently used helper functions to support unit testing.
#define VERIFY_ERROR(ERROR_ID, ERRONEOUS_STATEMENT)
Macro to verify that a statement indeed raises an exception.
a family of time value like entities and their relationships.
Specialised adapter to consume a record of GenNode entries to build a tuple.