Debian Codegraph @

pypy/module/_cppyy/test/test_advancedcpp.py

raw symbols SCIP
1 import py, os, sys
2 from .support import setup_make, soext
3
4
5 currpath = py.path.local(__file__).dirpath()
6 test_dct = str(currpath.join("advancedcppDict"))+soext
7
8 def setup_module(mod):
9 setup_make("advancedcpp")
10
11 def setup_module(mod):
12 if sys.platform == 'win32':
13 py.test.skip("win32 not supported so far")
14 for refl_dict in ["advancedcppDict.so", "advancedcpp2Dict.so"]:
15 err = os.system("cd '%s' && make %s" % (currpath, refl_dict))
16 if err:
17 raise OSError("'make' failed (see stderr)")
18
19 class AppTestADVANCEDCPP:
20 spaceconfig = dict(usemodules=['_cppyy', '_rawffi', 'itertools'])
21
22 def setup_class(cls):
23 cls.w_test_dct = cls.space.newtext(test_dct)
24 cls.w_advanced = cls.space.appexec([], """():
25 import ctypes, _cppyy
26 _cppyy._post_import_startup()
27 return ctypes.CDLL(%r, ctypes.RTLD_GLOBAL)""" % (test_dct, ))
28
29 def test01_default_arguments(self):
30 """Test usage of default arguments"""
31
32 import _cppyy as cppyy
33 def test_defaulter(n, t):
34 defaulter = getattr(cppyy.gbl, '%s_defaulter' % n)
35
36 d = defaulter()
37 assert d.m_a == t(11)
38 assert d.m_b == t(22)
39 assert d.m_c == t(33)
40 d.__destruct__()
41
42 d = defaulter(0)
43 assert d.m_a == t(0)
44 assert d.m_b == t(22)
45 assert d.m_c == t(33)
46 d.__destruct__()
47
48 d = defaulter(1, 2)
49 assert d.m_a == t(1)
50 assert d.m_b == t(2)
51 assert d.m_c == t(33)
52 d.__destruct__()
53
54 d = defaulter(3, 4, 5)
55 assert d.m_a == t(3)
56 assert d.m_b == t(4)
57 assert d.m_c == t(5)
58 d.__destruct__()
59
60 defaulter_func = getattr(cppyy.gbl, '%s_defaulter_func' %n)
61 answers = [11, 22, 33, 3]
62 for idx in range(4):
63 assert defaulter_func(idx) == answers[idx]
64
65 test_defaulter('short', int)
66 test_defaulter('ushort', int)
67 test_defaulter('int', int)
68 test_defaulter('uint', int)
69 test_defaulter('long', long)
70 test_defaulter('ulong', long)
71 test_defaulter('llong', long)
72 test_defaulter('ullong', long)
73 test_defaulter('float', float)
74 test_defaulter('double', float)
75
76 assert cppyy.gbl.string_defaulter_func(0) == "aap"
77 assert cppyy.gbl.string_defaulter_func(0, "zus") == "zus"
78 assert cppyy.gbl.string_defaulter_func(1) == "noot"
79 assert cppyy.gbl.string_defaulter_func(1, "zus") == "noot"
80 assert cppyy.gbl.string_defaulter_func(1, "zus", "jet") == "jet"
81 assert cppyy.gbl.string_defaulter_func(2) == "mies"
82
83 def test02_simple_inheritance(self):
84 """Test binding of a basic inheritance structure"""
85
86 import _cppyy as cppyy
87 base_class = cppyy.gbl.base_class
88 derived_class = cppyy.gbl.derived_class
89
90 assert issubclass(derived_class, base_class)
91 assert not issubclass(base_class, derived_class)
92
93 b = base_class()
94 assert isinstance(b, base_class)
95 assert not isinstance(b, derived_class)
96
97 assert b.m_b == 1
98 assert b.get_value() == 1
99 assert b.m_db == 1.1
100 assert b.get_base_value() == 1.1
101
102 b.m_b, b.m_db = 11, 11.11
103 assert b.m_b == 11
104 assert b.get_value() == 11
105 assert b.m_db == 11.11
106 assert b.get_base_value() == 11.11
107
108 b.__destruct__()
109
110 d = derived_class()
111 assert isinstance(d, derived_class)
112 assert isinstance(d, base_class)
113
114 assert d.m_d == 2
115 assert d.get_value() == 2
116 assert d.m_dd == 2.2
117 assert d.get_derived_value() == 2.2
118
119 assert d.m_b == 1
120 assert d.m_db == 1.1
121 assert d.get_base_value() == 1.1
122
123 d.m_b, d.m_db = 11, 11.11
124 d.m_d, d.m_dd = 22, 22.22
125
126 assert d.m_d == 22
127 assert d.get_value() == 22
128 assert d.m_dd == 22.22
129 assert d.get_derived_value() == 22.22
130
131 assert d.m_b == 11
132 assert d.m_db == 11.11
133 assert d.get_base_value() == 11.11
134
135 d.__destruct__()
136
137 def test03_namespaces(self):
138 """Test access to namespaces and inner classes"""
139
140 import _cppyy as cppyy
141 gbl = cppyy.gbl
142
143 assert gbl.a_ns is gbl.a_ns
144 assert gbl.a_ns.d_ns is gbl.a_ns.d_ns
145
146 assert gbl.a_ns.b_class is gbl.a_ns.b_class
147 assert gbl.a_ns.b_class.c_class is gbl.a_ns.b_class.c_class
148 assert gbl.a_ns.d_ns.e_class is gbl.a_ns.d_ns.e_class
149 assert gbl.a_ns.d_ns.e_class.f_class is gbl.a_ns.d_ns.e_class.f_class
150
151 assert gbl.a_ns.g_a == 11
152 assert gbl.a_ns.get_g_a() == 11
153 assert gbl.a_ns.b_class.s_b == 22
154 assert gbl.a_ns.b_class().m_b == -2
155 assert gbl.a_ns.b_class.c_class.s_c == 33
156 assert gbl.a_ns.b_class.c_class().m_c == -3
157 assert gbl.a_ns.d_ns.g_d == 44
158 assert gbl.a_ns.d_ns.get_g_d() == 44
159 assert gbl.a_ns.d_ns.e_class.s_e == 55
160 assert gbl.a_ns.d_ns.e_class().m_e == -5
161 assert gbl.a_ns.d_ns.e_class.f_class.s_f == 66
162 assert gbl.a_ns.d_ns.e_class.f_class().m_f == -6
163
164 raises(TypeError, gbl.a_ns)
165
166 def test03a_namespace_lookup_on_update(self):
167 """Test whether namespaces can be shared across dictionaries."""
168
169 import _cppyy as cppyy, ctypes
170 gbl = cppyy.gbl
171
172 lib2 = ctypes.CDLL("./advancedcpp2Dict.so", ctypes.RTLD_GLOBAL)
173
174 assert gbl.a_ns is gbl.a_ns
175 assert gbl.a_ns.d_ns is gbl.a_ns.d_ns
176
177 assert gbl.a_ns.g_class is gbl.a_ns.g_class
178 assert gbl.a_ns.g_class.h_class is gbl.a_ns.g_class.h_class
179 assert gbl.a_ns.d_ns.i_class is gbl.a_ns.d_ns.i_class
180 assert gbl.a_ns.d_ns.i_class.j_class is gbl.a_ns.d_ns.i_class.j_class
181
182 assert gbl.a_ns.g_g == 77
183 assert gbl.a_ns.get_g_g() == 77
184 assert gbl.a_ns.g_class.s_g == 88
185 assert gbl.a_ns.g_class().m_g == -7
186 assert gbl.a_ns.g_class.h_class.s_h == 99
187 assert gbl.a_ns.g_class.h_class().m_h == -8
188 assert gbl.a_ns.d_ns.g_i == 111
189 assert gbl.a_ns.d_ns.get_g_i() == 111
190 assert gbl.a_ns.d_ns.i_class.s_i == 222
191 assert gbl.a_ns.d_ns.i_class().m_i == -9
192 assert gbl.a_ns.d_ns.i_class.j_class.s_j == 333
193 assert gbl.a_ns.d_ns.i_class.j_class().m_j == -10
194
195 def test04_template_types(self):
196 """Test bindings of templated types"""
197
198 import _cppyy as cppyy
199 gbl = cppyy.gbl
200
201 assert gbl.T1 is gbl.T1
202 assert gbl.T2 is gbl.T2
203 assert gbl.T3 is gbl.T3
204 assert not gbl.T1 is gbl.T2
205 assert not gbl.T2 is gbl.T3
206
207 assert gbl.T1('int') is gbl.T1('int')
208 assert gbl.T1(int) is gbl.T1('int')
209 assert gbl.T2('T1<int>') is gbl.T2('T1<int>')
210 assert gbl.T2(gbl.T1('int')) is gbl.T2('T1<int>')
211 assert gbl.T2(gbl.T1(int)) is gbl.T2('T1<int>')
212 assert gbl.T3('int,double') is gbl.T3('int,double')
213 assert gbl.T3('int', 'double') is gbl.T3('int,double')
214 assert gbl.T3(int, 'double') is gbl.T3('int,double')
215 assert gbl.T3('T1<int>,T2<T1<int> >') is gbl.T3('T1<int>,T2<T1<int> >')
216 assert gbl.T3('T1<int>', gbl.T2(gbl.T1(int))) is gbl.T3('T1<int>,T2<T1<int> >')
217
218 assert gbl.a_ns.T4(int) is gbl.a_ns.T4('int')
219 assert gbl.a_ns.T4('a_ns::T4<T3<int,double> >')\
220 is gbl.a_ns.T4(gbl.a_ns.T4(gbl.T3(int, 'double')))
221
222 #----- mix in some of the alternative syntax
223 assert gbl.T1['int'] is gbl.T1('int')
224 assert gbl.T1[int] is gbl.T1('int')
225 assert gbl.T2['T1<int>'] is gbl.T2('T1<int>')
226 assert gbl.T2[gbl.T1('int')] is gbl.T2('T1<int>')
227 assert gbl.T2[gbl.T1(int)] is gbl.T2('T1<int>')
228 assert gbl.T3['int,double'] is gbl.T3('int,double')
229 assert gbl.T3['int', 'double'] is gbl.T3('int,double')
230 assert gbl.T3[int, 'double'] is gbl.T3('int,double')
231 assert gbl.T3['T1<int>,T2<T1<int> >'] is gbl.T3('T1<int>,T2<T1<int> >')
232 assert gbl.T3['T1<int>', gbl.T2[gbl.T1[int]]] is gbl.T3('T1<int>,T2<T1<int> >')
233
234 assert gbl.a_ns.T4[int] is gbl.a_ns.T4('int')
235 assert gbl.a_ns.T4['a_ns::T4<T3<int,double> >']\
236 is gbl.a_ns.T4(gbl.a_ns.T4(gbl.T3(int, 'double')))
237
238 #-----
239 t1 = gbl.T1(int)()
240 assert t1.m_t1 == 1
241 assert t1.get_value() == 1
242 t1.__destruct__()
243
244 #-----
245 t1 = gbl.T1(int)(11)
246 assert t1.m_t1 == 11
247 assert t1.get_value() == 11
248 t1.m_t1 = 111
249 assert t1.get_value() == 111
250 assert t1.m_t1 == 111
251 t1.__destruct__()
252
253 #-----
254 t2 = gbl.T2(gbl.T1(int))(gbl.T1(int)(32))
255 t2.m_t2.m_t1 = 32
256 assert t2.m_t2.get_value() == 32
257 assert t2.m_t2.m_t1 == 32
258 t2.__destruct__()
259
260
261 def test05_abstract_classes(self):
262 """Test non-instatiatability of abstract classes"""
263
264 import _cppyy as cppyy
265 gbl = cppyy.gbl
266
267 raises(TypeError, gbl.a_class)
268 raises(TypeError, gbl.some_abstract_class)
269
270 assert issubclass(gbl.some_concrete_class, gbl.some_abstract_class)
271
272 c = gbl.some_concrete_class()
273 assert isinstance(c, gbl.some_concrete_class)
274 assert isinstance(c, gbl.some_abstract_class)
275
276 def test06_datamembers(self):
277 """Test data member access when using virtual inheritence"""
278
279 import _cppyy as cppyy
280 a_class = cppyy.gbl.a_class
281 b_class = cppyy.gbl.b_class
282 c_class_1 = cppyy.gbl.c_class_1
283 c_class_2 = cppyy.gbl.c_class_2
284 d_class = cppyy.gbl.d_class
285
286 assert issubclass(b_class, a_class)
287 assert issubclass(c_class_1, a_class)
288 assert issubclass(c_class_1, b_class)
289 assert issubclass(c_class_2, a_class)
290 assert issubclass(c_class_2, b_class)
291 assert issubclass(d_class, a_class)
292 assert issubclass(d_class, b_class)
293 assert issubclass(d_class, c_class_2)
294
295 #-----
296 b = b_class()
297 assert b.m_a == 1
298 assert b.m_da == 1.1
299 assert b.m_b == 2
300 assert b.m_db == 2.2
301
302 b.m_a = 11
303 assert b.m_a == 11
304 assert b.m_b == 2
305
306 b.m_da = 11.11
307 assert b.m_da == 11.11
308 assert b.m_db == 2.2
309
310 b.m_b = 22
311 assert b.m_a == 11
312 assert b.m_da == 11.11
313 assert b.m_b == 22
314 assert b.get_value() == 22
315
316 b.m_db = 22.22
317 assert b.m_db == 22.22
318
319 b.__destruct__()
320
321 #-----
322 c1 = c_class_1()
323 assert c1.m_a == 1
324 assert c1.m_b == 2
325 assert c1.m_c == 3
326
327 c1.m_a = 11
328 assert c1.m_a == 11
329
330 c1.m_b = 22
331 assert c1.m_a == 11
332 assert c1.m_b == 22
333
334 c1.m_c = 33
335 assert c1.m_a == 11
336 assert c1.m_b == 22
337 assert c1.m_c == 33
338 assert c1.get_value() == 33
339
340 c1.__destruct__()
341
342 #-----
343 d = d_class()
344 assert d.m_a == 1
345 assert d.m_b == 2
346 assert d.m_c == 3
347 assert d.m_d == 4
348
349 d.m_a = 11
350 assert d.m_a == 11
351
352 d.m_b = 22
353 assert d.m_a == 11
354 assert d.m_b == 22
355
356 d.m_c = 33
357 assert d.m_a == 11
358 assert d.m_b == 22
359 assert d.m_c == 33
360
361 d.m_d = 44
362 assert d.m_a == 11
363 assert d.m_b == 22
364 assert d.m_c == 33
365 assert d.m_d == 44
366 assert d.get_value() == 44
367
368 d.__destruct__()
369
370 def test07_pass_by_reference(self):
371 """Test reference passing when using virtual inheritance"""
372
373 import _cppyy as cppyy
374 gbl = cppyy.gbl
375 b_class = gbl.b_class
376 c_class = gbl.c_class_2
377 d_class = gbl.d_class
378
379 #-----
380 b = b_class()
381 b.m_a, b.m_b = 11, 22
382 assert gbl.get_a(b) == 11
383 assert gbl.get_b(b) == 22
384 b.__destruct__()
385
386 #-----
387 c = c_class()
388 c.m_a, c.m_b, c.m_c = 11, 22, 33
389 assert gbl.get_a(c) == 11
390 assert gbl.get_b(c) == 22
391 assert gbl.get_c(c) == 33
392 c.__destruct__()
393
394 #-----
395 d = d_class()
396 d.m_a, d.m_b, d.m_c, d.m_d = 11, 22, 33, 44
397 assert gbl.get_a(d) == 11
398 assert gbl.get_b(d) == 22
399 assert gbl.get_c(d) == 33
400 assert gbl.get_d(d) == 44
401 d.__destruct__()
402
403 def test08_void_pointer_passing(self):
404 """Test passing of variants of void pointer arguments"""
405
406 import _cppyy as cppyy
407 pointer_pass = cppyy.gbl.pointer_pass
408 some_concrete_class = cppyy.gbl.some_concrete_class
409
410 pp = pointer_pass()
411 o = some_concrete_class()
412
413 assert cppyy.addressof(o) == pp.gime_address_ptr(o)
414 assert cppyy.addressof(o) == pp.gime_address_ptr_ptr(o)
415 assert cppyy.addressof(o) == pp.gime_address_ptr_ref(o)
416
417 import array
418 addressofo = array.array('l', [cppyy.addressof(o)])
419 assert addressofo[0] == pp.gime_address_ptr_ptr(addressofo)
420
421 assert 0 == pp.gime_address_ptr(0)
422 raises(TypeError, pp.gime_address_ptr, None)
423
424 ptr = cppyy.bind_object(0, some_concrete_class)
425 assert cppyy.addressof(ptr) == 0
426 pp.set_address_ptr_ref(ptr)
427 assert cppyy.addressof(ptr) == 0x1234
428 pp.set_address_ptr_ptr(ptr)
429 assert cppyy.addressof(ptr) == 0x4321
430
431 assert cppyy.addressof(cppyy.nullptr) == 0
432 raises(TypeError, cppyy.addressof, None)
433 assert cppyy.addressof(0) == 0
434
435 def test09_opaque_pointer_passing(self):
436 """Test passing around of opaque pointers"""
437
438 import _cppyy as cppyy
439 some_concrete_class = cppyy.gbl.some_concrete_class
440
441 o = some_concrete_class()
442
443 # TODO: figure out the PyPy equivalent of CObject (may have to do this
444 # through the C-API from C++)
445
446 #cobj = cppyy.as_cobject(o)
447 addr = cppyy.addressof(o)
448
449 #assert o == cppyy.bind_object(cobj, some_concrete_class)
450 #assert o == cppyy.bind_object(cobj, type(o))
451 #assert o == cppyy.bind_object(cobj, o.__class__)
452 #assert o == cppyy.bind_object(cobj, "some_concrete_class")
453 assert cppyy.addressof(o) == cppyy.addressof(cppyy.bind_object(addr, some_concrete_class))
454 assert o == cppyy.bind_object(addr, some_concrete_class)
455 assert o == cppyy.bind_object(addr, type(o))
456 assert o == cppyy.bind_object(addr, o.__class__)
457 assert o == cppyy.bind_object(addr, "some_concrete_class")
458 raises(TypeError, cppyy.bind_object, addr, "does_not_exist")
459 raises(TypeError, cppyy.bind_object, addr, 1)
460
461 def test10_object_identity(self):
462 """Test object identity"""
463
464 import _cppyy as cppyy
465 some_concrete_class = cppyy.gbl.some_concrete_class
466 some_class_with_data = cppyy.gbl.some_class_with_data
467
468 o = some_concrete_class()
469 addr = cppyy.addressof(o)
470
471 o2 = cppyy.bind_object(addr, some_concrete_class)
472 assert o is o2
473
474 o3 = cppyy.bind_object(addr, some_class_with_data)
475 assert not o is o3
476
477 d1 = some_class_with_data()
478 d2 = d1.gime_copy()
479 assert not d1 is d2
480
481 dd1a = d1.gime_data()
482 dd1b = d1.gime_data()
483 assert dd1a is dd1b
484
485 dd2 = d2.gime_data()
486 assert not dd1a is dd2
487 assert not dd1b is dd2
488
489 d2.__destruct__()
490 d1.__destruct__()
491
492 RTS = cppyy.gbl.refers_to_self
493
494 r1 = RTS()
495 r2 = RTS()
496 r1.m_other = r2
497
498 r3 = r1.m_other
499 r4 = r1.m_other
500 assert r3 is r4
501
502 assert r3 == r2
503 assert r3 is r2
504
505 r3.extra = 42
506 assert r2.extra == 42
507 assert r4.extra == 42
508
509 def test11_multi_methods(self):
510 """Test calling of methods from multiple inheritance"""
511
512 import _cppyy as cppyy
513 multi = cppyy.gbl.multi
514
515 assert cppyy.gbl.multi1 is multi.__bases__[0]
516 assert cppyy.gbl.multi2 is multi.__bases__[1]
517
518 dict_keys = list(multi.__dict__.keys())
519 assert dict_keys.count('get_my_own_int') == 1
520 assert dict_keys.count('get_multi1_int') == 0
521 assert dict_keys.count('get_multi2_int') == 0
522
523 m = multi(1, 2, 3)
524 assert m.get_multi1_int() == 1
525 assert m.get_multi2_int() == 2
526 assert m.get_my_own_int() == 3
527
528 def test12_actual_type(self):
529 """Test that a pointer to base return does an auto-downcast"""
530
531 import _cppyy as cppyy
532 base_class = cppyy.gbl.base_class
533 derived_class = cppyy.gbl.derived_class
534
535 b = base_class()
536 d = derived_class()
537
538 assert b == b.cycle(b)
539 assert id(b) == id(b.cycle(b))
540 assert b == d.cycle(b)
541 assert id(b) == id(d.cycle(b))
542 assert d == b.cycle(d)
543 assert id(d) == id(b.cycle(d))
544 assert d == d.cycle(d)
545 assert id(d) == id(d.cycle(d))
546
547 assert isinstance(b.cycle(b), base_class)
548 assert isinstance(d.cycle(b), base_class)
549 assert isinstance(b.cycle(d), derived_class)
550 assert isinstance(d.cycle(d), derived_class)
551
552 assert isinstance(b.clone(), base_class) # TODO: clone() leaks
553 assert isinstance(d.clone(), derived_class) # TODO: clone() leaks
554
555 # special case when round-tripping through a void* ptr
556 voidp = b.mask(d)
557 assert not isinstance(voidp, base_class)
558 assert not isinstance(voidp, derived_class)
559
560 d1 = cppyy.bind_object(voidp, base_class, cast=True)
561 assert isinstance(d1, derived_class)
562 assert d1 is d
563
564 b1 = cppyy.bind_object(voidp, base_class)
565 assert isinstance(b1, base_class)
566 assert cppyy.addressof(b1) == cppyy.addressof(d)
567 assert not (b1 is d)
568
569 def test13_actual_type_virtual_multi(self):
570 """Test auto-downcast in adverse inheritance situation"""
571
572 import _cppyy as cppyy
573
574 c1 = cppyy.gbl.create_c1()
575 assert type(c1) == cppyy.gbl.c_class_1
576 assert c1.m_c == 3
577 c1.__destruct__()
578
579 c2 = cppyy.gbl.create_c2()
580 assert type(c2) == cppyy.gbl.c_class_2
581 assert c2.m_c == 3
582 c2.__destruct__()
583
584 def test14_new_overloader(self):
585 """Verify that class-level overloaded new/delete are called"""
586
587 import _cppyy as cppyy
588
589 assert cppyy.gbl.new_overloader.s_instances == 0
590 nl = cppyy.gbl.new_overloader()
591 assert cppyy.gbl.new_overloader.s_instances == 1
592 nl.__destruct__()
593
594 import gc
595 gc.collect()
596 assert cppyy.gbl.new_overloader.s_instances == 0
597
598 def test15_template_instantiation_with_vector_of_float(self):
599 """Test template instantiation with a std::vector<float>"""
600
601 import _cppyy as cppyy
602
603 # the following will simply fail if there is a naming problem (e.g.
604 # std::, allocator<int>, etc., etc.); note the parsing required ...
605 b = cppyy.gbl.my_templated_class(cppyy.gbl.std.vector(float))()
606
607 for i in range(5):
608 b.m_b.push_back(i)
609 assert round(b.m_b[i], 5) == float(i)
610
611 def test16_template_global_functions(self):
612 """Test template global function lookup and calls"""
613
614 import _cppyy as cppyy
615
616 f = cppyy.gbl.my_templated_function
617
618 assert f('c') == 'c'
619 assert type(f('c')) == type('c')
620 assert f(3.) == 3.
621 assert type(f(4.)) == type(4.)
622
623 def test17_assign_to_return_byref( self ):
624 """Test assignment to an instance returned by reference"""
625
626 from _cppyy import gbl
627
628 a = gbl.std.vector(gbl.ref_tester)()
629 a.push_back(gbl.ref_tester(42))
630
631 assert len(a) == 1
632 assert a[0].m_i == 42
633
634 # TODO:
635 # a[0] = gbl.ref_tester(33)
636 # assert len(a) == 1
637 # assert a[0].m_i == 33
638
639 def test18_math_converters(self):
640 """Test operator int/long/double incl. typedef"""
641
642 from _cppyy import gbl
643
644 a = gbl.some_convertible()
645 a.m_i = 1234
646 a.m_d = 4321.
647
648 assert int(a) == 1234
649 assert int(a) == a.m_i
650 assert long(a) == a.m_i
651
652 assert float(a) == 4321.
653 assert float(a) == a.m_d
654
655 def test19_comparator(self):
656 """Check that the global operator!=/== is picked up"""
657
658 from _cppyy import gbl
659
660 a, b = gbl.some_comparable(), gbl.some_comparable()
661
662 assert a == b
663 assert b == a
664 assert a.__eq__(b)
665 assert b.__eq__(a)
666 assert a.__ne__(a)
667 assert b.__ne__(b)
668 assert a.__eq__(b) == True
669 assert b.__eq__(a) == True
670 assert a.__eq__(a) == False
671 assert b.__eq__(b) == False
672
673 def test20_overload_order_with_proper_return(self):
674 """Test return type against proper overload w/ const and covariance"""
675
676 import _cppyy as cppyy
677
678 assert cppyy.gbl.overload_one_way().gime() == 1
679 assert cppyy.gbl.overload_the_other_way().gime() == "aap"
680
681 def test21_access_to_global_variables(self):
682 """Access global_variables_and_pointers"""
683
684 import _cppyy as cppyy
685
686 assert cppyy.gbl.my_global_double == 12.
687 assert len(cppyy.gbl.my_global_array) == 500
688 assert cppyy.gbl.my_global_string1 == "aap noot mies"
689 assert cppyy.gbl.my_global_string2 == "zus jet teun"
690 # TODO: currently fails b/c double** not understood as &double*
691 #assert cppyy.gbl.my_global_ptr[0] == 1234.
692
693 v = cppyy.gbl.my_global_int_holders
694 assert len(v) == 5
695 expected_vals = [13, 42, 88, -1, 17]
696 for i in range(len(v)):
697 assert v[i].m_val == expected_vals[i]
698
699 def test22_exceptions(self):
700 """Catching of C++ exceptions"""
701
702 import _cppyy as cppyy
703 Thrower = cppyy.gbl.Thrower
704
705 Thrower.throw_anything.__useffi__ = False
706 Thrower.throw_exception.__useffi__ = False
707
708 t = Thrower()
709
710 assert raises(Exception, t.throw_anything)
711 assert raises(Exception, t.throw_exception)
712
713 try:
714 t.throw_exception()
715 except Exception as e:
716 "C++ function failed" in str(e)
717
718 def test23_using(self):
719 """Accessibility of using declarations"""
720
721 import _cppyy as cppyy
722
723 assert cppyy.gbl.UsingBase().vcheck() == 'A'
724
725 B = cppyy.gbl.UsingDerived
726 assert not 'UsingBase' in B.__init__.__doc__
727
728 b1 = B()
729 assert b1.m_int == 13
730 assert b1.m_int2 == 42
731 assert b1.vcheck() == 'B'
732
733 b2 = B(10)
734 assert b2.m_int == 10
735 assert b2.m_int2 == 42
736 assert b2.vcheck() == 'B'
737
738 b3 = B(b2)
739 assert b3.m_int == 10
740 assert b3.m_int2 == 42
741 assert b3.vcheck() == 'B'
742
743 def test24_typedef_to_private_class(self):
744 """Typedefs to private classes should not resolve"""
745
746 import _cppyy as cppyy
747
748 assert cppyy.gbl.TypedefToPrivateClass().f().m_val == 42