-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathPyROOT_cpptests.py
More file actions
496 lines (368 loc) · 16.7 KB
/
PyROOT_cpptests.py
File metadata and controls
496 lines (368 loc) · 16.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
# File: roottest/python/cpp/PyROOT_cpptests.py
# Author: Wim Lavrijsen (LBNL, WLavrijsen@lbl.gov)
# Author: Enric Tejedor Saavedra
# Author: Vincenzo Eduardo Padulano (CERN)
# Created: 01/03/05
# Last: 10/08/21 (MM-DD-YY)
"""C++ language interface unit tests for PyROOT package."""
import sys, os, unittest
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
from sys import maxsize
import ROOT
from ROOT import TObject, TLorentzVector, kRed, kGreen, kBlue, TVectorF, TROOT, TCanvas, gInterpreter, gROOT, TMatrixD, TString, std
from ROOT import MakeNullPointer, AsCObject, BindObject, AddressOf, addressof
from common import *
from functools import partial
__all__ = [
'Cpp1LanguageFeatureTestCase',
'Cpp2ClassNamingTestCase',
'Cpp3UsingDeclarations',
'Cpp4InheritanceTreeOverloadResolution',
]
IS_WINDOWS = 0
if 'win32' in sys.platform:
import platform
if '64' in platform.architecture()[0]:
IS_WINDOWS = 64
else:
IS_WINDOWS = 32
### C++ language constructs test cases =======================================
class Cpp1LanguageFeatureTestCase( MyTestCase ):
def test01ClassEnum( self ):
"""Test class enum access and values"""
self.assertEqual( TObject.kBitMask, gROOT.ProcessLine( "return TObject::kBitMask;" ) )
self.assertEqual( TObject.kIsOnHeap, gROOT.ProcessLine( "return TObject::kIsOnHeap;" ) )
self.assertEqual( TObject.kNotDeleted, gROOT.ProcessLine( "return TObject::kNotDeleted;" ) )
self.assertEqual( TObject.kZombie, gROOT.ProcessLine( "return TObject::kZombie;" ) )
t = TObject()
self.assertEqual( TObject.kBitMask, t.kBitMask )
self.assertEqual( TObject.kIsOnHeap, t.kIsOnHeap )
self.assertEqual( TObject.kNotDeleted, t.kNotDeleted )
self.assertEqual( TObject.kZombie, t.kZombie )
def test02Globalenum( self ):
"""Test global enums access and values"""
self.assertEqual( kRed, gROOT.ProcessLine( "return kRed;" ) )
self.assertEqual( kGreen, gROOT.ProcessLine( "return kGreen;" ) )
self.assertEqual( kBlue, gROOT.ProcessLine( "return kBlue;" ) )
def test03GlobalEnumType(self):
"""Test lookup and type of global enum"""
ROOT.gInterpreter.Declare("enum foo { aa,bb };")
self.assertEqual(ROOT.aa, 0)
self.assertEqual(ROOT.bb, 1)
cppname = ROOT.foo.__cpp_name__
self.assertEqual(cppname, 'foo')
self.assertEqual(ROOT.foo.aa, 0)
self.assertEqual(ROOT.foo.bb, 1)
def test04NsEnumType(self):
"""Test lookup type of enum in namespace"""
ROOT.gInterpreter.Declare("namespace myns { enum foo { aa,bb }; }")
self.assertEqual(ROOT.myns.aa, 0)
self.assertEqual(ROOT.myns.bb, 1)
cppname = ROOT.myns.foo.__cpp_name__
self.assertEqual(cppname, 'myns::foo')
self.assertEqual(ROOT.myns.foo.aa, 0)
self.assertEqual(ROOT.myns.foo.bb, 1)
def test05EnumSignedUnsigned(self):
"""Test lookup of enums with signed & unsigned underlying types"""
ROOT.gInterpreter.Declare("enum bar { cc=-10,dd };")
self.assertEqual(ROOT.cc, -10)
ROOT.gInterpreter.Declare("enum bar2 { ee=4294967286,ff };")
self.assertEqual(ROOT.ee, 4294967286)
ROOT.gInterpreter.Declare("namespace myns { enum bar { cc=-10,dd }; }")
self.assertEqual(ROOT.myns.cc, -10)
ROOT.gInterpreter.Declare("namespace myns { enum bar2 { ee=4294967286,ff }; }")
self.assertEqual(ROOT.myns.ee, 4294967286)
def test06ScopedEnum(self):
"""Test lookup of scoped enums and their values"""
ROOT.gInterpreter.Declare("enum class scopedEnum { gg=1,hh };")
self.assertEqual(ROOT.scopedEnum.gg, 1)
ROOT.gInterpreter.Declare("namespace myns { enum class scopedEnum { gg=1,hh }; }")
self.assertEqual(ROOT.myns.scopedEnum.gg, 1)
def test07CopyContructor( self ):
"""Test copy constructor"""
t1 = TLorentzVector( 1., 2., 3., -4. )
t2 = TLorentzVector( 0., 0., 0., 0. )
t3 = TLorentzVector( t1 )
self.assertEqual( t1, t3 )
self.assertNotEqual( t1, t2 )
for i in range(4):
self.assertEqual( t1[i], t3[i] )
# Test copy constructor with null pointer
t4 = MakeNullPointer(TLorentzVector)
t4.__init__(TLorentzVector(0, 1, 2, 3))
t5 = MakeNullPointer(TLorentzVector)
TLorentzVector.__init__(t5, TLorentzVector(0, 1, 2, 3))
# Test __assign__ if the object already exists
t6 = TLorentzVector(0, 0, 0, 0)
t6.__assign__(TLorentzVector(0, 1, 2, 3))
t7 = TLorentzVector(0, 0, 0, 0)
TLorentzVector.__assign__(t7, TLorentzVector(0, 1, 2, 3))
for i in range(4):
self.assertEqual( t4[i], t5[i] )
self.assertEqual( t6[i], t7[i] )
def test08ObjectValidity( self ):
"""Test object validity checking"""
t1 = TObject()
self.assertTrue( t1 )
self.assertTrue( not not t1 )
t2 = gROOT.FindObject( "Nah, I don't exist" )
self.assertTrue( not t2 )
def test09ElementAccess( self ):
"""Test access to elements in matrix and array objects."""
n = 3
v = TVectorF( n )
m = TMatrixD( n, n )
for i in range(n):
self.assertEqual( v[i], 0.0 )
for j in range(n):
self.assertEqual( m[i][j], 0.0 )
def test10StaticFunctionCall( self ):
"""Test call to static function."""
c1 = TROOT.Class()
self.assertTrue( not not c1 )
c2 = gROOT.Class()
self.assertIs( c1, c2 )
old = gROOT.GetDirLevel()
TROOT.SetDirLevel( 2 )
self.assertEqual( 2, gROOT.GetDirLevel() )
gROOT.SetDirLevel( old )
old = TROOT.GetDirLevel()
gROOT.SetDirLevel( 3 )
self.assertEqual( 3, TROOT.GetDirLevel() )
TROOT.SetDirLevel( old )
def test11Namespaces( self ):
"""Test access to namespaces and inner classes"""
gROOT.LoadMacro( "Namespace.C+" )
PR_NS_A = ROOT.PR_NS_A
self.assertEqual( PR_NS_A.sa, 1 )
self.assertEqual( PR_NS_A.PR_ST_B.sb, 2 )
self.assertEqual( PR_NS_A.PR_ST_B().fb, -2 )
self.assertEqual( PR_NS_A.PR_ST_B.PR_ST_C.sc, 3 )
self.assertEqual( PR_NS_A.PR_ST_B.PR_ST_C().fc, -3 )
self.assertEqual( PR_NS_A.PR_NS_D.sd, 4 )
self.assertEqual( PR_NS_A.PR_NS_D.PR_ST_E.se, 5 )
self.assertEqual( PR_NS_A.PR_NS_D.PR_ST_E().fe, -5 )
self.assertEqual( PR_NS_A.PR_NS_D.PR_ST_E.PR_ST_F.sf, 6 )
self.assertEqual( PR_NS_A.PR_NS_D.PR_ST_E.PR_ST_F().ff, -6 )
# a few more, with namespaced typedefs
self.assertEqual( PR_NS_A.tsa, -1 )
self.assertEqual( PR_NS_A.ctsa, -1 )
# data members coming in from a different namespace block
self.assertEqual( PR_NS_A.tsa2, -1 )
self.assertEqual( PR_NS_A.ctsa2, -1 )
# data members from a different namespace in a separate file
self.assertRaises( AttributeError, getattr, PR_NS_A, 'tsa3' )
self.assertRaises( AttributeError, getattr, PR_NS_A, 'ctsa3' )
gROOT.LoadMacro( "Namespace2.C+" )
self.assertEqual( PR_NS_A.tsa3, -8 )
self.assertEqual( PR_NS_A.ctsa3, -9 )
# test equality of different lookup methods
self.assertEqual( getattr( PR_NS_A, "PR_ST_B::PR_ST_C" ), PR_NS_A.PR_ST_B.PR_ST_C )
self.assertEqual( getattr( PR_NS_A.PR_ST_B, "PR_ST_C" ), PR_NS_A.PR_ST_B.PR_ST_C )
def test12VoidPointerPassing( self ):
"""Test passing of variants of void pointer arguments"""
gROOT.LoadMacro( "PointerPassing.C+" )
Z = ROOT.Z
o = TObject()
oaddr = addressof(o)
self.assertEqual( oaddr, Z.GimeAddressPtr( o ) )
self.assertEqual( oaddr, Z.GimeAddressPtrRef( o ) )
pZ = Z.getZ(0)
self.assertEqual( Z.checkAddressOfZ( pZ ), True )
# self.assertEqual( pZ , Z.getZ(1) )
import array
# Not supported in p2.2
# and no 8-byte integer type array on Windows 64b
if hasattr( array.array, 'buffer_info' ) and IS_WINDOWS != 64:
# New cppyy uses unsigned long to represent void* returns, as in DynamicCast.
# To prevent an overflow error when converting the Python integer returned by
# DynamicCast into a 4-byte signed long in 32 bits, we use unsigned long ('L')
# as type of the array.array.
array_t = 'L'
addressofo = array.array( array_t, [o.IsA()._TClass__DynamicCast( o.IsA(), o )[0]] )
self.assertEqual( addressofo.buffer_info()[0], Z.GimeAddressPtrPtr( addressofo ) )
self.assertEqual( 0, Z.GimeAddressPtr( 0 ) );
self.assertEqual( 0, Z.GimeAddressObject( 0 ) );
ptr = MakeNullPointer( TObject )
# New Cppyy does not raise ValueError,
# it just returns zero
self.assertEqual(addressof(ptr), 0)
Z.SetAddressPtrRef( ptr )
self.assertEqual( addressof( ptr ), 0x1234 )
Z.SetAddressPtrPtr( ptr )
self.assertEqual( addressof( ptr ), 0x4321 )
def test13Macro( self ):
"""Test access to cpp macro's"""
gROOT.ProcessLine( '#define aap "aap"' )
gROOT.ProcessLine( '#define noot 1' )
gROOT.ProcessLine( '#define mies 2.0' )
# looking up macro's is slow, so needs to be explicit (note that NULL,
# see above, is a special case)
ROOT.PyConfig.ExposeCppMacros = True
# test also that garbage macros are not found
self.assertRaises( AttributeError, getattr, ROOT, "_this_does_not_exist_at_all" )
ROOT.PyConfig.ExposeCppMacros = False
def test14OpaquePointerPassing( self ):
"""Test passing around of opaque pointers"""
import ROOT
s = TString( "Hello World!" )
co = AsCObject( s )
ad = addressof( s )
self.assertTrue( s == BindObject( co, s.__class__ ) )
self.assertTrue( s == BindObject( co, "TString" ) )
self.assertTrue( s == BindObject( ad, s.__class__ ) )
self.assertTrue( s == BindObject( ad, "TString" ) )
def test15ObjectAndPointerComparisons( self ):
"""Verify object and pointer comparisons"""
c1 = MakeNullPointer( TCanvas )
self.assertFalse( c1 )
c2 = MakeNullPointer( TCanvas )
self.assertEqual( c1, c2 )
self.assertEqual( c2, c1 )
# TLorentzVector overrides operator==
l1 = MakeNullPointer( TLorentzVector )
self.assertFalse( l1 )
# Forbidden by the type system
with self.assertRaises(TypeError):
self.assertNotEqual( c1, l1 )
with self.assertRaises(TypeError):
self.assertNotEqual( l1, c1 )
l2 = MakeNullPointer( TLorentzVector )
self.assertEqual( l1, l2 )
self.assertEqual( l2, l1 )
l3 = TLorentzVector( 1, 2, 3, 4 )
l4 = TLorentzVector( 1, 2, 3, 4 )
l5 = TLorentzVector( 4, 3, 2, 1 )
self.assertEqual( l3, l4 )
self.assertEqual( l4, l3 )
self.assertTrue( l3 != None ) # like this to ensure __ne__ is called
self.assertTrue( None != l3 ) # id.
self.assertNotEqual( l3, l5 )
self.assertNotEqual( l5, l3 )
def test16AddressOfaddressof(self):
"""Test addresses returned by AddressOf and addressof"""
import ROOT
o = ROOT.TObject()
addr_as_int = ROOT.addressof(o)
addr_as_buffer = ROOT.AddressOf(o)
# The result of AddressOf can be passed to a function that expects a void*
# or an integer pointer (Long64_t* for 64bit, Int_t* for 32bit)
is_64bit = maxsize > 2**32
if is_64bit:
ROOT.gInterpreter.Declare("""
Long64_t get_address_in_buffer_vp(void *p) { return *(Long64_t*)p; }
Long64_t get_address_in_buffer_ip(Long64_t *p) { return *p; }
""")
else:
ROOT.gInterpreter.Declare("""
Int_t get_address_in_buffer_vp(void *p) { return *(Int_t*)p; }
Int_t get_address_in_buffer_ip(Int_t *p) { return *p; }
""")
self.assertEqual(addr_as_int, ROOT.get_address_in_buffer_vp(addr_as_buffer))
self.assertEqual(addr_as_int, ROOT.get_address_in_buffer_ip(addr_as_buffer))
self.assertEqual(addr_as_int, addr_as_buffer[0])
### C++ language naming of classes ===========================================
class Cpp2ClassNamingTestCase( MyTestCase ):
def test01Underscore( self ):
"""Test recognition of '_' as part of a valid class name"""
z = ROOT.Z_()
self.assertTrue( hasattr( z, 'myint' ) )
self.assertTrue( z.GimeZ_( z ) )
def test02DefaultCtorInNamespace( self ):
"""Check that constructor with default argument is found in namespace"""
PR_NS_A = ROOT.PR_NS_A
CtorWithDefaultInGBL = ROOT.CtorWithDefaultInGBL
a = CtorWithDefaultInGBL()
self.assertEqual( a.data, -1 )
b = CtorWithDefaultInGBL( 1 )
self.assertEqual( b.data, 1 )
c = PR_NS_A.CtorWithDefaultInNS()
self.assertEqual( c.data, -1 )
c = PR_NS_A.CtorWithDefaultInNS( 2 )
self.assertEqual( c.data, 2 )
def test03NamespaceInTemplates( self ):
"""Templated data members need to retain namespaces of arguments"""
PR_NS_A = ROOT.PR_NS_A
p = std.pair( std.vector( PR_NS_A.PR_ST_B ), std.vector( PR_NS_A.PR_NS_D.PR_ST_E ) )()
self.assertTrue( "vector<PR_NS_A::PR_ST_B>" in type(p.first).__name__ )
self.assertTrue( "vector<PR_NS_A::PR_NS_D::PR_ST_E>" in type(p.second).__name__ )
def test04NamespacedTemplateIdentity( self ):
"""Identity of templated classes with and w/o std:: should match"""
gInterpreter.Declare( 'namespace PR_HepMC { class GenParticle {}; }' )
gInterpreter.Declare( 'namespace PR_LoKi { template< typename T, typename S > class Functor {}; }' )
PR_LoKi = ROOT.PR_LoKi
f1 = PR_LoKi.Functor( "vector<const PR_HepMC::GenParticle*>", "vector<double>" )
f2 = PR_LoKi.Functor( "std::vector<const PR_HepMC::GenParticle*>", "std::vector<double>" )
self.assertTrue( f1 is f2 )
self.assertEqual( f1, f2 )
### C++ language using declarations ===========================================
class Cpp3UsingDeclarations( MyTestCase ):
def test1TGraphMultiErrorsSetLineColor(self):
"""Test that a using function declaration is picked up by the overload resolution"""
# This line breaks with the following error if the using function declaration is not picked up
# TypeError: void TGraphMultiErrors::SetLineColor(int e, short lcolor) =>
# TypeError: takes at least 2 arguments (1 given)
ROOT.TGraphMultiErrors().SetLineColor(0)
def test2TH1FConstructor(self):
"""Test that the using declaration of a constructor is picked up by the overload resolution"""
# ROOT-10786
ROOT.gInterpreter.Declare("""
class MyTH1F : public TH1F {
public:
using TH1F::TH1F;
};
""")
h = ROOT.MyTH1F("name", "title", 100, 0, 100)
class Cpp4InheritanceTreeOverloadResolution(MyTestCase):
"""
Tests correct overload resolution of functions accepting classes part of
the same inheritance tree.
"""
@classmethod
def setUpClass(cls):
"""Declare the classes and functions needed for the tests"""
ROOT.gInterpreter.Declare(
"""
namespace Cpp4 {
class A {};
class B: public A {};
class C: public B {};
class X {};
class Y: public X {};
class Z: public Y {};
int myfunc(const B &b){
return 1;
}
int myfunc(const C &c){
return 2;
}
int myfunc(const B &b, const Z &z){
return 1;
}
int myfunc(const C &c, const X &x){
return 2;
}
} // end namespace
""")
def test1SingleArgumentFunction(self):
"""Test reproducer of issue root-project/root/8817."""
self.assertEqual(ROOT.Cpp4.myfunc(ROOT.Cpp4.B()), 1)
self.assertEqual(ROOT.Cpp4.myfunc(ROOT.Cpp4.C()), 2)
def test2TwoArgumentFunctionAmbiguous(self):
"""
Test the behaviour of a scenario that would be ambiguous in C++.
In PyROOT, the function with the highest priority in the overload
resolution will be called. Would be nice to throw an error in this kind
of scenario.
"""
# In C++ calling myfunc(C(), Z()) would throw
# error: call to 'myfunc' is ambiguous
self.assertEqual(ROOT.Cpp4.myfunc(ROOT.Cpp4.C(), ROOT.Cpp4.Z()), 1)
## actual test run
if __name__ == '__main__':
from MyTextTestRunner import MyTextTestRunner
loader = unittest.TestLoader()
testSuite = loader.loadTestsFromModule( sys.modules[ __name__ ] )
runner = MyTextTestRunner( verbosity = 2 )
result = not runner.run( testSuite ).wasSuccessful()
sys.exit( result )