Lumiera  0.pre.03
»edit your freedom«
meta-utils-test.cpp
Go to the documentation of this file.
1 /*
2  MetaUtils(Test) - check some simple type trait helpers
3 
4  Copyright (C)
5  2011, 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/symbol.hpp"
20 #include "lib/test/run.hpp"
21 #include "lib/meta/util.hpp"
22 #include "lib/meta/typelist.hpp"
23 
24 #include <string>
25 #include <iostream>
26 using std::cout;
27 using std::endl;
28 
29 
30 namespace lib {
31 namespace meta {
32 namespace test {
33 
34  using std::string;
35 
36 
37 
38 
39 
40 
41  /*********************************************************************/
47  class MetaUtils_test : public Test
48  {
49  void
50  run (Arg)
51  {
53  verify_genericTypeDisplay();
54 
55  detect_stringConversion();
56  detect_typeList();
57  }
58 
59 
66  void
68  {
69  CHECK (sizeof(Yes_t) != sizeof (No_t));
70 
71  CHECK (sizeof(Yes_t) == sizeof (probe (1)));
72  CHECK (sizeof(Yes_t) == sizeof (probe (1L))); // conversion long -> int
73  CHECK (sizeof(Yes_t) == sizeof (probe ('a'))); // conversion char -> int
74  CHECK (sizeof(No_t) == sizeof (probe ("a"))); // char * can't be converted
75  }
76 
77  static Yes_t probe (int);
78  static No_t probe (...);
79 
80 
81 
82  void
83  verify_genericTypeDisplay()
84  {
85  cout << typeStr<SubString>() <<endl;
86 
87  struct Lunatic
88  : Test
89  {
90  virtual void run (Arg) {}
91  }
92  lunatic;
93  cout << typeStr(lunatic) << endl;
94  cout << typeStr(&lunatic) << endl;
95  cout << typeStr((Test &)lunatic) << endl;
96  cout << typeStr((Test *) &lunatic) << endl;
97  cout << typeStr(&Lunatic::run) << endl;
98  }
99 
100 
101 
102  //-------------------------------------------------TEST-types--
103  class SubString : public string
104  {
105  public:
106  SubString() : string("sublunar") { }
107  };
108 
109  class Something { };
110 
112  {
113  operator string() { return "No such thing"; }
114  };
115 
117  {
118  operator SubString() { return SubString(); }
119  };
120 
121  class SomehowSubSub : public SomehowSubtle { };
122  //-------------------------------------------------TEST-types--
123 
124  template<typename TY>
125  static bool
126  can_convert (TY const&)
127  {
129  }
130 
131  void
132  detect_stringConversion()
133  {
134  CHECK ( can_convert (string("inline string")));
135  CHECK ( can_convert ("char literal"));
136  CHECK (!can_convert (23.34));
137  CHECK (!can_convert (23));
138  CHECK (!can_convert (1L));
139 
140  string str("mhm");
141  string & str_ref (str);
142  string const& str_const_ref (str);
143  string * str_ptr = &str;
144 
145  CHECK ( can_convert (str));
146  CHECK ( can_convert (str_ref));
147  CHECK ( can_convert (str_const_ref));
148  CHECK ( can_convert (*str_ptr));
149  CHECK (!can_convert (str_ptr));
150 
151  SubString sub;
152  Something thing;
153  const SomehowStringy stringy = SomehowStringy();
154  SomehowSubSub subsub;
155  SubString const& subRef(subsub);
156 
157  CHECK ( can_convert (sub));
158  CHECK (!can_convert (thing));
159  CHECK ( can_convert (stringy));
160  CHECK ( can_convert (subsub));
161  CHECK ( can_convert (subRef));
162  }
163 
164 
165 
166  //-------------------------------------------------TEST-types--
167  typedef Types< int
168  , uint
169  , int64_t
170  , uint64_t
171  >::List TheList;
172 
173  typedef Types< >::List EmptyList;
174  //-------------------------------------------------TEST-types--
175 
176 
177  void
178  detect_typeList()
179  {
183  }
184  };
185 
186 
188  LAUNCHER (MetaUtils_test, "unit meta");
189 
190 
191 
192 }}} // namespace lib::meta::test
A template metaprogramming technique for manipulating collections of types.
Definition: run.hpp:40
Simple and lightweight helpers for metaprogramming and type detection.
Implementation namespace for support and library code.
Marker types to indicate a literal string and a Symbol.
Simplistic test class runner.
Trait template for detecting a typelist type.
Definition: meta/util.hpp:199
char Yes_t
helper types to detect the overload resolution chosen by the compiler
Definition: meta/util.hpp:95
detect possibility of a conversion to string.
Definition: meta/util.hpp:163
std::string typeStr(TY const *obj=nullptr) noexcept
failsafe human readable type display
Definition: meta/util.hpp:316