ChaiScript / ChaiScript
1
#include <chaiscript/utility/utility.hpp>
2

3

4
using namespace chaiscript;
5

6

7
template<typename T>
8 1
void use(T){}
9

10
template<typename To>
11 1
bool run_test_type_conversion(const Boxed_Value &bv, bool expectedpass)
12
{
13
  try {
14 1
    To ret = chaiscript::boxed_cast<To>(bv);
15 1
    use(ret);
16 1
  } catch (const chaiscript::exception::bad_boxed_cast &e) {
17 1
    if (expectedpass) {
18 0
      std::cerr << "Failure in run_test_type_conversion: " << e.what() << '\n';
19 0
      return false;
20
    } else {
21 1
      return true;
22
    }
23 0
  } catch (const std::exception &e) {
24 0
    std::cerr << "Unexpected standard exception when attempting cast_conversion: " << e.what() << '\n';
25 0
    return false;
26 0
  } catch (...) {
27 0
    std::cerr << "Unexpected unknown exception when attempting cast_conversion.\n";
28 0
    return false;
29
  }
30
 
31 1
  if (expectedpass)
32
  {
33 1
    return true;
34
  } else {
35 0
    return false;
36
  }
37
}
38

39
template<typename To>
40 1
bool test_type_conversion(const Boxed_Value &bv, bool expectedpass)
41
{
42 1
  bool ret = run_test_type_conversion<To>(bv, expectedpass);
43

44 1
  if (!ret)
45
  {
46
    std::cerr << "Error with type conversion test. From: " 
47 0
      << (bv.is_const()?(std::string("const ")):(std::string())) << bv.get_type_info().name() 
48
      << " To: "  
49 0
      << (std::is_const<To>::value?(std::string("const ")):(std::string())) << typeid(To).name() 
50 0
      << " test was expected to " << ((expectedpass)?(std::string("succeed")):(std::string("fail"))) << " but did not\n";
51
  }
52

53 1
  return ret;
54
}
55

56
template<typename Type>
57 1
bool do_test(const Boxed_Value &bv, 
58
    bool T, bool ConstT, bool TRef, bool ConstTRef, bool TPtr, 
59
    bool ConstTPtr, bool TPtrConst, bool ConstTPtrConst, bool SharedPtrT, bool SharedConstPtrT, 
60
    bool SharedPtrTRef, bool ConstSharedPtrT, bool ConstSharedConstPtrT, bool ConstSharedPtrTRef, bool ConstSharedPtrTConstRef,
61
    bool WrappedRef, bool WrappedConstRef, bool ConstWrappedRef, bool ConstWrappedConstRef, bool ConstWrappedRefRef, 
62
    bool ConstWrappedConstRefRef, bool Number, bool ConstNumber, bool ConstNumberRef, bool TPtrConstRef, 
63
    bool ConstTPtrConstRef)
64
{
65 1
  bool passed = true;
66 1
  passed &= test_type_conversion<Type>(bv, T);
67 1
  passed &= test_type_conversion<const Type>(bv, ConstT);
68 1
  passed &= test_type_conversion<Type &>(bv, TRef);
69 1
  passed &= test_type_conversion<const Type &>(bv, ConstTRef);
70 1
  passed &= test_type_conversion<Type *>(bv, TPtr);
71 1
  passed &= test_type_conversion<const Type *>(bv, ConstTPtr);
72 1
  passed &= test_type_conversion<Type * const>(bv, TPtrConst);
73 1
  passed &= test_type_conversion<const Type * const>(bv, ConstTPtrConst);
74 1
  passed &= test_type_conversion<std::shared_ptr<Type> >(bv, SharedPtrT);
75 1
  passed &= test_type_conversion<std::shared_ptr<const Type> >(bv, SharedConstPtrT);
76 1
  passed &= test_type_conversion<std::shared_ptr<Type> &>(bv, SharedPtrTRef);
77
  //passed &= test_type_conversion<std::shared_ptr<const Type> &>(bv, false);
78 1
  passed &= test_type_conversion<const std::shared_ptr<Type> >(bv, ConstSharedPtrT);
79 1
  passed &= test_type_conversion<const std::shared_ptr<const Type> >(bv, ConstSharedConstPtrT);
80 1
  passed &= test_type_conversion<const std::shared_ptr<Type> &>(bv, ConstSharedPtrTRef);
81 1
  passed &= test_type_conversion<const std::shared_ptr<const Type> &>(bv, ConstSharedPtrTConstRef);
82 1
  passed &= test_type_conversion<std::reference_wrapper<Type> >(bv, WrappedRef);
83 1
  passed &= test_type_conversion<std::reference_wrapper<const Type> >(bv, WrappedConstRef);
84 1
  passed &= test_type_conversion<std::reference_wrapper<Type> &>(bv, false);
85 1
  passed &= test_type_conversion<std::reference_wrapper<const Type> &>(bv, false);
86 1
  passed &= test_type_conversion<const std::reference_wrapper<Type> >(bv, ConstWrappedRef);
87 1
  passed &= test_type_conversion<const std::reference_wrapper<const Type> >(bv, ConstWrappedConstRef);
88 1
  passed &= test_type_conversion<const std::reference_wrapper<Type> &>(bv, ConstWrappedRefRef);
89 1
  passed &= test_type_conversion<const std::reference_wrapper<const Type> &>(bv, ConstWrappedConstRefRef);
90 1
  passed &= test_type_conversion<Boxed_Number>(bv, Number);
91 1
  passed &= test_type_conversion<const Boxed_Number>(bv, ConstNumber);
92 1
  passed &= test_type_conversion<Boxed_Number &>(bv, false);
93 1
  passed &= test_type_conversion<const Boxed_Number &>(bv, ConstNumberRef);
94 1
  passed &= test_type_conversion<Boxed_Number *>(bv, false);
95 1
  passed &= test_type_conversion<const Boxed_Number *>(bv, false);
96 1
  passed &= test_type_conversion<Boxed_Number * const>(bv, false);
97 1
  passed &= test_type_conversion<const Boxed_Number *const>(bv, false);
98 1
  passed &= test_type_conversion<Type *&>(bv, false);
99 1
  passed &= test_type_conversion<const Type *&>(bv, false);
100 1
  passed &= test_type_conversion<Type * const&>(bv, TPtrConstRef);
101 1
  passed &= test_type_conversion<const Type * const&>(bv, ConstTPtrConstRef);
102 1
  passed &= test_type_conversion<Boxed_Value>(bv, true);
103 1
  passed &= test_type_conversion<const Boxed_Value>(bv, true);
104 1
  passed &= test_type_conversion<const Boxed_Value &>(bv, true);
105

106 1
  return passed;
107
}
108

109
/** Tests intended for built int types **/
110
template<typename T>
111 1
bool built_in_type_test(const T &initial, bool ispod)
112
{
113 1
  bool passed = true;
114

115
  /** value tests **/
116 1
  T i = T(initial);
117 1
  passed &= do_test<T>(var(i), true, true, true, true, true, 
118
                                 true, true, true, true, true,
119
                                 true, true, true, true, true, true,
120
                                 true, true, true, true, true,
121
                                 ispod, ispod, ispod, true, true);
122

123 1
  passed &= do_test<T>(const_var(i), true, true, false, true, false, 
124
                                       true, false, true, false, true,
125
                                       false, false, true, false, true, false,
126
                                       true, false, true, false, true,
127
                                       ispod, ispod, ispod, false, true);
128

129 1
  passed &= do_test<T>(var(&i), true, true, true, true, true, 
130
                                 true, true, true, false, false,
131
                                 false, false, false, false, false, true,
132
                                 true, true, true, true, true,
133
                                 ispod, ispod, ispod, true, true);
134

135 1
  passed &= do_test<T>(const_var(&i), true, true, false, true, false, 
136
                                 true, false, true, false, false,
137
                                 false, false, false, false, false, false,
138
                                 true, false, true, false, true,
139
                                 ispod, ispod, ispod, false, true);
140

141 1
  passed &= do_test<T>(var(std::ref(i)), true, true, true, true, true, 
142
                                 true, true, true, false, false,
143
                                 false, false, false, false, false, true,
144
                                 true, true, true, true, true,
145
                                 ispod, ispod, ispod, true, true);
146

147 1
  passed &= do_test<T>(var(std::cref(i)), true, true, false, true, false, 
148
                                 true, false, true, false, false,
149
                                 false, false, false, false, false, false,
150
                                 true, false, true, false, true,
151
                                 ispod, ispod, ispod, false, true);
152

153
  /** Const Reference Variable tests */
154

155
  // This reference will be copied on input, which is expected
156 1
  const T &ir = i;
157

158 1
  passed &= do_test<T>(var(i), true, true, true, true, true, 
159
                                 true, true, true, true, true,
160
                                 true, true, true, true, true, true,
161
                                 true, true, true, true, true,
162
                                 ispod, ispod, ispod, true, true);
163

164
  // But a pointer or reference to it should be necessarily const
165 1
  passed &= do_test<T>(var(&ir), true, true, false, true, false, 
166
                                 true, false, true, false, false,
167
                                 false, false, false, false, false, false,
168
                                 true, false, true, false, true,
169
                                 ispod, ispod, ispod, false, true);
170

171 1
  passed &= do_test<T>(var(std::ref(ir)), true, true, false, true, false, 
172
                                 true, false, true, false, false,
173
                                 false, false, false, false, false, false,
174
                                 true, false, true, false, true,
175
                                 ispod, ispod, ispod, false, true);
176

177
  // Make sure const of const works too
178 1
  passed &= do_test<T>(const_var(&ir), true, true, false, true, false, 
179
                                 true, false, true, false, false,
180
                                 false, false, false, false, false, false,
181
                                 true, false, true, false, true,
182
                                 ispod, ispod, ispod, false, true);
183

184 1
  passed &= do_test<T>(const_var(std::ref(ir)), true, true, false, true, false, 
185
                                 true, false, true, false, false,
186
                                 false, false, false, false, false, false,
187
                                 true, false, true, false, true,
188
                                 ispod, ispod, ispod, false, true);
189

190
  /** Const Reference Variable tests */
191

192
  // This will always be seen as a const
193 1
  const T*cip = &i;
194 1
  passed &= do_test<T>(var(cip), true, true, false, true, false, 
195
                                 true, false, true, false, false,
196
                                 false, false, false, false, false, false,
197
                                 true, false, true, false, true,
198
                                 ispod, ispod, ispod, false, true);
199

200
  // make sure const of const works
201 1
  passed &= do_test<T>(const_var(cip), true, true, false, true, false, 
202
                                 true, false, true, false, false,
203
                                 false, false, false, false, false, false,
204
                                 true, false, true, false, true,
205
                                 ispod, ispod, ispod, false, true);
206

207
  /** shared_ptr tests **/
208

209 1
  auto ip = std::make_shared<T>(initial);
210

211 1
  passed &= do_test<T>(var(ip), true, true, true, true, true, 
212
                                 true, true, true, true, true,
213
                                 true, true, true, true, true, true,
214
                                 true, true, true, true, true,
215
                                 ispod, ispod, ispod, true, true);
216

217 1
  passed &= do_test<T>(const_var(ip), true, true, false, true, false, 
218
                                       true, false, true, false, true,
219
                                       false, false, true, false, true, false,
220
                                       true, false, true, false, true,
221
                                       ispod, ispod, ispod, false, true);
222

223
  /** const shared_ptr tests **/
224 1
  auto ipc = std::make_shared<const T>(T(initial));
225

226 1
  passed &= do_test<T>(var(ipc), true, true, false, true, false, 
227
                                       true, false, true, false, true,
228
                                       false, false, true, false, true, false,
229
                                       true, false, true, false, true,
230
                                       ispod, ispod, ispod, false, true);
231

232
  // const of this should be the same, making sure it compiles
233 1
  passed &= do_test<T>(const_var(ipc), true, true, false, true, false, 
234
                                       true, false, true, false, true,
235
                                       false, false, true, false, true, false,
236
                                       true, false, true, false, true,
237
                                       ispod, ispod, ispod, false, true);
238

239

240
  /** Double ptr tests **/
241

242
  /*
243
  T **doublep;
244

245
  passed &= do_test<T*>(var(doublep), true, true, false, true, false, 
246
                                       true, false, true, false, true,
247
                                       false, true, false, true, false,
248
                                       true, false, true, false, true,
249
                                       ispod, ispod, ispod, false, true);
250
*/
251

252 1
  return passed;
253
}
254

255

256
template<typename T>
257 1
bool pointer_test(const T& default_value, const T& new_value)
258
{
259 1
  T *p = new T(default_value);
260

261
  // we store a pointer to a pointer, so we can get a pointer to a pointer
262
  try {
263 1
    T **result = boxed_cast<T **>(var(&p));
264 1
    *(*result) = new_value;
265

266

267 1
    if (p != (*result) ) { 
268 0
      std::cerr << "Pointer passed in different than one returned\n";
269 0
      delete p;
270 0
      return false;
271
    }
272

273 1
    if (*p != *(*result) ) {
274 0
      std::cerr << "Somehow dereferenced pointer values are not the same?\n";
275 0
      delete p;
276 0
      return false;
277
    }
278

279 1
    delete p;
280 1
    return true;
281 0
  } catch (const exception::bad_boxed_cast &) {
282 0
    std::cerr << "Bad boxed cast performing ** to ** test\n";
283 0
    delete p;
284 0
    return false;
285 0
  } catch (...) {
286 0
    std::cerr << "Unknown exception performing ** to ** test\n";
287 0
    delete p;
288 0
    return false;
289
  }
290

291

292
}
293

294

295 1
int main()
296
{
297 1
  bool passed = true;
298

299
  /*
300
  bool T, bool ConstT, bool TRef, bool ConstTRef, bool TPtr, 
301
  bool ConstTPtr, bool TPtrConst, bool ConstTPtrConst, bool SharedPtrT, bool SharedConstPtrT,
302
    bool ConstSharedPtrT, bool ConstSharedConstPtrT, bool ConstSharedPtrTRef, bool ConstSharedPtrTConstRef, bool WrappedRef, 
303
    bool WrappedConstRef, bool ConstWrappedRef, bool ConstWrappedConstRef, bool ConstWrappedRefRef, bool ConstWrappedConstRefRef, 
304
    bool Number, bool ConstNumber, bool ConstNumberRef
305
    */
306

307 1
  passed &= built_in_type_test<int>(5, true);
308 1
  passed &= built_in_type_test<double>(1.1, true);
309 1
  passed &= built_in_type_test<char>('a', true);
310 1
  passed &= built_in_type_test<uint8_t>('a', true);
311 1
  passed &= built_in_type_test<int64_t>('a', true);
312 1
  passed &= built_in_type_test<bool>(false, false);
313 1
  passed &= built_in_type_test<std::string>("Hello World", false);
314
  
315
  // storing a pointer 
316 1
  passed &= pointer_test<int>(1, 0);
317

318 1
  if (passed)
319
  {
320 1
    return EXIT_SUCCESS;
321
  } else {
322 0
    return EXIT_FAILURE;
323
  }
324

325
}

Read our documentation on viewing source code .

Loading