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) Lumiera.org
5  2011, 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/meta/util.hpp"
30 #include "lib/meta/typelist.hpp"
31 
32 #include <string>
33 #include <iostream>
34 using std::cout;
35 using std::endl;
36 
37 
38 namespace lib {
39 namespace meta {
40 namespace test {
41 
42  using std::string;
43 
44 
45 
46 
47 
48 
49  /*********************************************************************/
55  class MetaUtils_test : public Test
56  {
57  void
58  run (Arg)
59  {
61  verify_genericTypeDisplay();
62 
63  detect_stringConversion();
64  detect_typeList();
65  }
66 
67 
74  void
76  {
77  CHECK (sizeof(Yes_t) != sizeof (No_t));
78 
79  CHECK (sizeof(Yes_t) == sizeof (probe (1)));
80  CHECK (sizeof(Yes_t) == sizeof (probe (1L))); // conversion long -> int
81  CHECK (sizeof(Yes_t) == sizeof (probe ('a'))); // conversion char -> int
82  CHECK (sizeof(No_t) == sizeof (probe ("a"))); // char * can't be converted
83  }
84 
85  static Yes_t probe (int);
86  static No_t probe (...);
87 
88 
89 
90  void
91  verify_genericTypeDisplay()
92  {
93  cout << typeStr<SubString>() <<endl;
94 
95  struct Lunatic
96  : Test
97  {
98  virtual void run (Arg) {}
99  }
100  lunatic;
101  cout << typeStr(lunatic) << endl;
102  cout << typeStr(&lunatic) << endl;
103  cout << typeStr((Test &)lunatic) << endl;
104  cout << typeStr((Test *) &lunatic) << endl;
105  cout << typeStr(&Lunatic::run) << endl;
106  }
107 
108 
109 
110  //-------------------------------------------------TEST-types--
111  class SubString : public string
112  {
113  public:
114  SubString() : string("sublunar") { }
115  };
116 
117  class Something { };
118 
120  {
121  operator string() { return "No such thing"; }
122  };
123 
125  {
126  operator SubString() { return SubString(); }
127  };
128 
129  class SomehowSubSub : public SomehowSubtle { };
130  //-------------------------------------------------TEST-types--
131 
132  template<typename TY>
133  static bool
134  can_convert (TY const&)
135  {
137  }
138 
139  void
140  detect_stringConversion()
141  {
142  CHECK ( can_convert (string("inline string")));
143  CHECK ( can_convert ("char literal"));
144  CHECK (!can_convert (23.34));
145  CHECK (!can_convert (23));
146  CHECK (!can_convert (1L));
147 
148  string str("mhm");
149  string & str_ref (str);
150  string const& str_const_ref (str);
151  string * str_ptr = &str;
152 
153  CHECK ( can_convert (str));
154  CHECK ( can_convert (str_ref));
155  CHECK ( can_convert (str_const_ref));
156  CHECK ( can_convert (*str_ptr));
157  CHECK (!can_convert (str_ptr));
158 
159  SubString sub;
160  Something thing;
161  const SomehowStringy stringy = SomehowStringy();
162  SomehowSubSub subsub;
163  SubString const& subRef(subsub);
164 
165  CHECK ( can_convert (sub));
166  CHECK (!can_convert (thing));
167  CHECK ( can_convert (stringy));
168  CHECK ( can_convert (subsub));
169  CHECK ( can_convert (subRef));
170  }
171 
172 
173 
174  //-------------------------------------------------TEST-types--
175  typedef Types< int
176  , uint
177  , int64_t
178  , uint64_t
179  >::List TheList;
180 
181  typedef Types< >::List EmptyList;
182  //-------------------------------------------------TEST-types--
183 
184 
185  void
186  detect_typeList()
187  {
191  }
192  };
193 
194 
196  LAUNCHER (MetaUtils_test, "unit meta");
197 
198 
199 
200 }}} // namespace lib::meta::test
A template metaprogramming technique for manipulating collections of types.
Definition: run.hpp:49
Simple and lightweight helpers for metaprogramming and type detection.
Implementation namespace for support and library code.
Simple test class runner.
Trait template for detecting a typelist type.
Definition: meta/util.hpp:208
char Yes_t
helper types to detect the overload resolution chosen by the compiler
Definition: meta/util.hpp:104
detect possibility of a conversion to string.
Definition: meta/util.hpp:172
std::string typeStr(TY const *obj=nullptr) noexcept
failsafe human readable type display
Definition: meta/util.hpp:325