PageRenderTime 36ms CodeModel.GetById 2ms app.highlight 30ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Examples/test-suite/python/li_boost_shared_ptr_runme.py

#
Python | 544 lines | 400 code | 89 blank | 55 comment | 23 complexity | 2f876b42912dd477efd5ea49d4bfc704 MD5 | raw file
  1import li_boost_shared_ptr
  2import gc
  3
  4debug = False
  5
  6# simple shared_ptr usage - created in C++
  7class li_boost_shared_ptr_runme:
  8  def main(self):
  9    if (debug):
 10      print "Started"
 11
 12    li_boost_shared_ptr.cvar.debug_shared = debug
 13
 14    # Change loop count to run for a long time to monitor memory
 15    loopCount = 1 #5000
 16    for i in range (0,loopCount):
 17      self.runtest()
 18
 19    # Expect 1 instance - the one global variable (GlobalValue)
 20    if (li_boost_shared_ptr.Klass.getTotal_count() != 1):
 21      raise RuntimeError("Klass.total_count=%s" % li_boost_shared_ptr.Klass.getTotal_count())
 22
 23    wrapper_count = li_boost_shared_ptr.shared_ptr_wrapper_count() 
 24    if (wrapper_count != li_boost_shared_ptr.NOT_COUNTING):
 25      # Expect 1 instance - the one global variable (GlobalSmartValue)
 26      if (wrapper_count != 1):
 27        raise RuntimeError("shared_ptr wrapper count=%s" % wrapper_count)
 28
 29    if (debug):
 30      print "Finished"
 31
 32  def runtest(self):
 33    # simple shared_ptr usage - created in C++
 34    k = li_boost_shared_ptr.Klass("me oh my")
 35    val = k.getValue()
 36    self.verifyValue("me oh my", val)
 37    self.verifyCount(1, k)
 38
 39    # simple shared_ptr usage - not created in C++
 40    k = li_boost_shared_ptr.factorycreate()
 41    val = k.getValue()
 42    self.verifyValue("factorycreate", val)
 43    self.verifyCount(1, k)
 44
 45    # pass by shared_ptr
 46    k = li_boost_shared_ptr.Klass("me oh my")
 47    kret = li_boost_shared_ptr.smartpointertest(k)
 48    val = kret.getValue()
 49    self.verifyValue("me oh my smartpointertest", val)
 50    self.verifyCount(2, k)
 51    self.verifyCount(2, kret)
 52
 53    # pass by shared_ptr pointer
 54    k = li_boost_shared_ptr.Klass("me oh my")
 55    kret = li_boost_shared_ptr.smartpointerpointertest(k)
 56    val = kret.getValue()
 57    self.verifyValue("me oh my smartpointerpointertest", val)
 58    self.verifyCount(2, k)
 59    self.verifyCount(2, kret)
 60
 61    # pass by shared_ptr reference
 62    k = li_boost_shared_ptr.Klass("me oh my")
 63    kret = li_boost_shared_ptr.smartpointerreftest(k)
 64    val = kret.getValue()
 65    self.verifyValue("me oh my smartpointerreftest", val)
 66    self.verifyCount(2, k)
 67    self.verifyCount(2, kret)
 68
 69    # pass by shared_ptr pointer reference
 70    k = li_boost_shared_ptr.Klass("me oh my")
 71    kret = li_boost_shared_ptr.smartpointerpointerreftest(k)
 72    val = kret.getValue()
 73    self.verifyValue("me oh my smartpointerpointerreftest", val)
 74    self.verifyCount(2, k)
 75    self.verifyCount(2, kret)
 76
 77    # const pass by shared_ptr
 78    k = li_boost_shared_ptr.Klass("me oh my")
 79    kret = li_boost_shared_ptr.constsmartpointertest(k)
 80    val = kret.getValue()
 81    self.verifyValue("me oh my", val)
 82    self.verifyCount(2, k)
 83    self.verifyCount(2, kret)
 84
 85    # const pass by shared_ptr pointer
 86    k = li_boost_shared_ptr.Klass("me oh my")
 87    kret = li_boost_shared_ptr.constsmartpointerpointertest(k)
 88    val = kret.getValue()
 89    self.verifyValue("me oh my", val)
 90    self.verifyCount(2, k)
 91    self.verifyCount(2, kret)
 92
 93    # const pass by shared_ptr reference
 94    k = li_boost_shared_ptr.Klass("me oh my")
 95    kret = li_boost_shared_ptr.constsmartpointerreftest(k)
 96    val = kret.getValue()
 97    self.verifyValue("me oh my", val)
 98    self.verifyCount(2, k)
 99    self.verifyCount(2, kret)
100
101    # pass by value
102    k = li_boost_shared_ptr.Klass("me oh my")
103    kret = li_boost_shared_ptr.valuetest(k)
104    val = kret.getValue()
105    self.verifyValue("me oh my valuetest", val)
106    self.verifyCount(1, k)
107    self.verifyCount(1, kret)
108
109    # pass by pointer
110    k = li_boost_shared_ptr.Klass("me oh my")
111    kret = li_boost_shared_ptr.pointertest(k)
112    val = kret.getValue()
113    self.verifyValue("me oh my pointertest", val)
114    self.verifyCount(1, k)
115    self.verifyCount(1, kret)
116
117    # pass by reference
118    k = li_boost_shared_ptr.Klass("me oh my")
119    kret = li_boost_shared_ptr.reftest(k)
120    val = kret.getValue()
121    self.verifyValue("me oh my reftest", val)
122    self.verifyCount(1, k)
123    self.verifyCount(1, kret)
124
125    # pass by pointer reference
126    k = li_boost_shared_ptr.Klass("me oh my")
127    kret = li_boost_shared_ptr.pointerreftest(k)
128    val = kret.getValue()
129    self.verifyValue("me oh my pointerreftest", val)
130    self.verifyCount(1, k)
131    self.verifyCount(1, kret)
132
133    # null tests
134    k = None
135
136    if (li_boost_shared_ptr.smartpointertest(k) != None):
137      raise RuntimeError("return was not null")
138
139    if (li_boost_shared_ptr.smartpointerpointertest(k) != None):
140      raise RuntimeError("return was not null")
141
142    if (li_boost_shared_ptr.smartpointerreftest(k) != None):
143      raise RuntimeError("return was not null")
144
145    if (li_boost_shared_ptr.smartpointerpointerreftest(k) != None):
146      raise RuntimeError("return was not null")
147
148    if (li_boost_shared_ptr.nullsmartpointerpointertest(None) != "null pointer"):
149      raise RuntimeError("not null smartpointer pointer")
150
151    try:
152      li_boost_shared_ptr.valuetest(k)
153      raise RuntimeError("Failed to catch null pointer")
154    except ValueError:
155      pass
156
157    if (li_boost_shared_ptr.pointertest(k) != None):
158      raise RuntimeError("return was not null")
159
160    try:
161      li_boost_shared_ptr.reftest(k)
162      raise RuntimeError("Failed to catch null pointer")
163    except ValueError:
164      pass
165
166    # $owner
167    k = li_boost_shared_ptr.pointerownertest()
168    val = k.getValue()
169    self.verifyValue("pointerownertest", val)
170    self.verifyCount(1, k)
171    k = li_boost_shared_ptr.smartpointerpointerownertest()
172    val = k.getValue()
173    self.verifyValue("smartpointerpointerownertest", val)
174    self.verifyCount(1, k)
175
176    # //////////////////////////////// Derived class ////////////////////////////////////////
177    # derived pass by shared_ptr
178    k = li_boost_shared_ptr.KlassDerived("me oh my")
179    kret = li_boost_shared_ptr.derivedsmartptrtest(k)
180    val = kret.getValue()
181    self.verifyValue("me oh my derivedsmartptrtest-Derived", val)
182    self.verifyCount(2, k)
183    self.verifyCount(2, kret)
184
185    # derived pass by shared_ptr pointer
186    k = li_boost_shared_ptr.KlassDerived("me oh my")
187    kret = li_boost_shared_ptr.derivedsmartptrpointertest(k)
188    val = kret.getValue()
189    self.verifyValue("me oh my derivedsmartptrpointertest-Derived", val)
190    self.verifyCount(2, k)
191    self.verifyCount(2, kret)
192
193    # derived pass by shared_ptr ref
194    k = li_boost_shared_ptr.KlassDerived("me oh my")
195    kret = li_boost_shared_ptr.derivedsmartptrreftest(k)
196    val = kret.getValue()
197    self.verifyValue("me oh my derivedsmartptrreftest-Derived", val)
198    self.verifyCount(2, k)
199    self.verifyCount(2, kret)
200
201    # derived pass by shared_ptr pointer ref
202    k = li_boost_shared_ptr.KlassDerived("me oh my")
203    kret = li_boost_shared_ptr.derivedsmartptrpointerreftest(k)
204    val = kret.getValue()
205    self.verifyValue("me oh my derivedsmartptrpointerreftest-Derived", val)
206    self.verifyCount(2, k)
207    self.verifyCount(2, kret)
208
209    # derived pass by pointer
210    k = li_boost_shared_ptr.KlassDerived("me oh my")
211    kret = li_boost_shared_ptr.derivedpointertest(k)
212    val = kret.getValue()
213    self.verifyValue("me oh my derivedpointertest-Derived", val)
214    self.verifyCount(1, k)
215    self.verifyCount(1, kret)
216
217    # derived pass by ref
218    k = li_boost_shared_ptr.KlassDerived("me oh my")
219    kret = li_boost_shared_ptr.derivedreftest(k)
220    val = kret.getValue()
221    self.verifyValue("me oh my derivedreftest-Derived", val)
222    self.verifyCount(1, k)
223    self.verifyCount(1, kret)
224
225    # //////////////////////////////// Derived and base class mixed ////////////////////////////////////////
226    # pass by shared_ptr (mixed)
227    k = li_boost_shared_ptr.KlassDerived("me oh my")
228    kret = li_boost_shared_ptr.smartpointertest(k)
229    val = kret.getValue()
230    self.verifyValue("me oh my smartpointertest-Derived", val)
231    self.verifyCount(2, k)
232    self.verifyCount(2, kret)
233
234    # pass by shared_ptr pointer (mixed)
235    k = li_boost_shared_ptr.KlassDerived("me oh my")
236    kret = li_boost_shared_ptr.smartpointerpointertest(k)
237    val = kret.getValue()
238    self.verifyValue("me oh my smartpointerpointertest-Derived", val)
239    self.verifyCount(2, k)
240    self.verifyCount(2, kret)
241
242    # pass by shared_ptr reference (mixed)
243    k = li_boost_shared_ptr.KlassDerived("me oh my")
244    kret = li_boost_shared_ptr.smartpointerreftest(k)
245    val = kret.getValue()
246    self.verifyValue("me oh my smartpointerreftest-Derived", val)
247    self.verifyCount(2, k)
248    self.verifyCount(2, kret)
249
250    # pass by shared_ptr pointer reference (mixed)
251    k = li_boost_shared_ptr.KlassDerived("me oh my")
252    kret = li_boost_shared_ptr.smartpointerpointerreftest(k)
253    val = kret.getValue()
254    self.verifyValue("me oh my smartpointerpointerreftest-Derived", val)
255    self.verifyCount(2, k)
256    self.verifyCount(2, kret)
257
258    # pass by value (mixed)
259    k = li_boost_shared_ptr.KlassDerived("me oh my")
260    kret = li_boost_shared_ptr.valuetest(k)
261    val = kret.getValue()
262    self.verifyValue("me oh my valuetest", val) # note slicing
263    self.verifyCount(1, k)
264    self.verifyCount(1, kret)
265
266    # pass by pointer (mixed)
267    k = li_boost_shared_ptr.KlassDerived("me oh my")
268    kret = li_boost_shared_ptr.pointertest(k)
269    val = kret.getValue()
270    self.verifyValue("me oh my pointertest-Derived", val)
271    self.verifyCount(1, k)
272    self.verifyCount(1, kret)
273
274    # pass by ref (mixed)
275    k = li_boost_shared_ptr.KlassDerived("me oh my")
276    kret = li_boost_shared_ptr.reftest(k)
277    val = kret.getValue()
278    self.verifyValue("me oh my reftest-Derived", val)
279    self.verifyCount(1, k)
280    self.verifyCount(1, kret)
281
282    # //////////////////////////////// Overloading tests ////////////////////////////////////////
283    # Base class
284    k = li_boost_shared_ptr.Klass("me oh my")
285    self.verifyValue(li_boost_shared_ptr.overload_rawbyval(k), "rawbyval")
286    self.verifyValue(li_boost_shared_ptr.overload_rawbyref(k), "rawbyref")
287    self.verifyValue(li_boost_shared_ptr.overload_rawbyptr(k), "rawbyptr")
288    self.verifyValue(li_boost_shared_ptr.overload_rawbyptrref(k), "rawbyptrref")
289
290    self.verifyValue(li_boost_shared_ptr.overload_smartbyval(k), "smartbyval")
291    self.verifyValue(li_boost_shared_ptr.overload_smartbyref(k), "smartbyref")
292    self.verifyValue(li_boost_shared_ptr.overload_smartbyptr(k), "smartbyptr")
293    self.verifyValue(li_boost_shared_ptr.overload_smartbyptrref(k), "smartbyptrref")
294
295    # Derived class
296    k = li_boost_shared_ptr.KlassDerived("me oh my")
297    self.verifyValue(li_boost_shared_ptr.overload_rawbyval(k), "rawbyval")
298    self.verifyValue(li_boost_shared_ptr.overload_rawbyref(k), "rawbyref")
299    self.verifyValue(li_boost_shared_ptr.overload_rawbyptr(k), "rawbyptr")
300    self.verifyValue(li_boost_shared_ptr.overload_rawbyptrref(k), "rawbyptrref")
301
302    self.verifyValue(li_boost_shared_ptr.overload_smartbyval(k), "smartbyval")
303    self.verifyValue(li_boost_shared_ptr.overload_smartbyref(k), "smartbyref")
304    self.verifyValue(li_boost_shared_ptr.overload_smartbyptr(k), "smartbyptr")
305    self.verifyValue(li_boost_shared_ptr.overload_smartbyptrref(k), "smartbyptrref")
306
307    # 3rd derived class
308    k = li_boost_shared_ptr.Klass3rdDerived("me oh my")
309    val = k.getValue()
310    self.verifyValue("me oh my-3rdDerived", val)
311    self.verifyCount(1, k)
312    val = li_boost_shared_ptr.test3rdupcast(k)
313    self.verifyValue("me oh my-3rdDerived", val)
314    self.verifyCount(1, k)
315
316    # //////////////////////////////// Member variables ////////////////////////////////////////
317    # smart pointer by value
318    m = li_boost_shared_ptr.MemberVariables()
319    k = li_boost_shared_ptr.Klass("smart member value")
320    m.SmartMemberValue = k
321    val = k.getValue()
322    self.verifyValue("smart member value", val)
323    self.verifyCount(2, k)
324
325    kmember = m.SmartMemberValue
326    val = kmember.getValue()
327    self.verifyValue("smart member value", val)
328    self.verifyCount(3, kmember)
329    self.verifyCount(3, k)
330
331    del m
332    self.verifyCount(2, kmember)
333    self.verifyCount(2, k)
334
335    # smart pointer by pointer
336    m = li_boost_shared_ptr.MemberVariables()
337    k = li_boost_shared_ptr.Klass("smart member pointer")
338    m.SmartMemberPointer = k
339    val = k.getValue()
340    self.verifyValue("smart member pointer", val)
341    self.verifyCount(1, k)
342
343    kmember = m.SmartMemberPointer
344    val = kmember.getValue()
345    self.verifyValue("smart member pointer", val)
346    self.verifyCount(2, kmember)
347    self.verifyCount(2, k)
348
349    del m
350    self.verifyCount(2, kmember)
351    self.verifyCount(2, k)
352
353    # smart pointer by reference
354    m = li_boost_shared_ptr.MemberVariables()
355    k = li_boost_shared_ptr.Klass("smart member reference")
356    m.SmartMemberReference = k
357    val = k.getValue()
358    self.verifyValue("smart member reference", val)
359    self.verifyCount(2, k)
360
361    kmember = m.SmartMemberReference
362    val = kmember.getValue()
363    self.verifyValue("smart member reference", val)
364    self.verifyCount(3, kmember)
365    self.verifyCount(3, k)
366
367    # The C++ reference refers to SmartMemberValue...
368    kmemberVal = m.SmartMemberValue
369    val = kmember.getValue()
370    self.verifyValue("smart member reference", val)
371    self.verifyCount(4, kmemberVal)
372    self.verifyCount(4, kmember)
373    self.verifyCount(4, k)
374
375    del m
376    self.verifyCount(3, kmemberVal)
377    self.verifyCount(3, kmember)
378    self.verifyCount(3, k)
379
380    # plain by value
381    m = li_boost_shared_ptr.MemberVariables()
382    k = li_boost_shared_ptr.Klass("plain member value")
383    m.MemberValue = k
384    val = k.getValue()
385    self.verifyValue("plain member value", val)
386    self.verifyCount(1, k)
387
388    kmember = m.MemberValue
389    val = kmember.getValue()
390    self.verifyValue("plain member value", val)
391    self.verifyCount(1, kmember)
392    self.verifyCount(1, k)
393
394    del m
395    self.verifyCount(1, kmember)
396    self.verifyCount(1, k)
397
398    # plain by pointer
399    m = li_boost_shared_ptr.MemberVariables()
400    k = li_boost_shared_ptr.Klass("plain member pointer")
401    m.MemberPointer = k
402    val = k.getValue()
403    self.verifyValue("plain member pointer", val)
404    self.verifyCount(1, k)
405
406    kmember = m.MemberPointer
407    val = kmember.getValue()
408    self.verifyValue("plain member pointer", val)
409    self.verifyCount(1, kmember)
410    self.verifyCount(1, k)
411
412    del m
413    self.verifyCount(1, kmember)
414    self.verifyCount(1, k)
415
416    # plain by reference
417    m = li_boost_shared_ptr.MemberVariables()
418    k = li_boost_shared_ptr.Klass("plain member reference")
419    m.MemberReference = k
420    val = k.getValue()
421    self.verifyValue("plain member reference", val)
422    self.verifyCount(1, k)
423
424    kmember = m.MemberReference
425    val = kmember.getValue()
426    self.verifyValue("plain member reference", val)
427    self.verifyCount(1, kmember)
428    self.verifyCount(1, k)
429
430    del m
431    self.verifyCount(1, kmember)
432    self.verifyCount(1, k)
433
434    # null member variables
435    m = li_boost_shared_ptr.MemberVariables()
436
437    # shared_ptr by value
438    k = m.SmartMemberValue
439    if (k != None):
440      raise RuntimeError("expected null")
441    m.SmartMemberValue = None
442    k = m.SmartMemberValue
443    if (k != None):
444      raise RuntimeError("expected null")
445    self.verifyCount(0, k)
446
447    # plain by value
448    try:
449      m.MemberValue = None
450      raise RuntimeError("Failed to catch null pointer")
451    except ValueError:
452      pass
453
454    # ////////////////////////////////// Global variables ////////////////////////////////////////
455    # smart pointer
456    kglobal = li_boost_shared_ptr.cvar.GlobalSmartValue
457    if (kglobal != None):
458      raise RuntimeError("expected null")
459
460    k = li_boost_shared_ptr.Klass("smart global value")
461    li_boost_shared_ptr.cvar.GlobalSmartValue = k
462    self.verifyCount(2, k)
463
464    kglobal = li_boost_shared_ptr.cvar.GlobalSmartValue
465    val = kglobal.getValue()
466    self.verifyValue("smart global value", val)
467    self.verifyCount(3, kglobal)
468    self.verifyCount(3, k)
469    self.verifyValue("smart global value", li_boost_shared_ptr.cvar.GlobalSmartValue.getValue())
470    li_boost_shared_ptr.cvar.GlobalSmartValue = None
471
472    # plain value
473    k = li_boost_shared_ptr.Klass("global value")
474    li_boost_shared_ptr.cvar.GlobalValue = k
475    self.verifyCount(1, k)
476
477    kglobal = li_boost_shared_ptr.cvar.GlobalValue
478    val = kglobal.getValue()
479    self.verifyValue("global value", val)
480    self.verifyCount(1, kglobal)
481    self.verifyCount(1, k)
482    self.verifyValue("global value", li_boost_shared_ptr.cvar.GlobalValue.getValue())
483
484    try:
485      li_boost_shared_ptr.cvar.GlobalValue = None
486      raise RuntimeError("Failed to catch null pointer")
487    except ValueError:
488      pass
489
490    # plain pointer
491    kglobal = li_boost_shared_ptr.cvar.GlobalPointer
492    if (kglobal != None):
493      raise RuntimeError("expected null")
494
495    k = li_boost_shared_ptr.Klass("global pointer")
496    li_boost_shared_ptr.cvar.GlobalPointer = k
497    self.verifyCount(1, k)
498
499    kglobal = li_boost_shared_ptr.cvar.GlobalPointer
500    val = kglobal.getValue()
501    self.verifyValue("global pointer", val)
502    self.verifyCount(1, kglobal)
503    self.verifyCount(1, k)
504    li_boost_shared_ptr.cvar.GlobalPointer = None
505
506    # plain reference
507    kglobal
508
509    k = li_boost_shared_ptr.Klass("global reference")
510    li_boost_shared_ptr.cvar.GlobalReference = k
511    self.verifyCount(1, k)
512
513    kglobal = li_boost_shared_ptr.cvar.GlobalReference
514    val = kglobal.getValue()
515    self.verifyValue("global reference", val)
516    self.verifyCount(1, kglobal)
517    self.verifyCount(1, k)
518
519    try:
520      li_boost_shared_ptr.cvar.GlobalReference = None 
521      raise RuntimeError("Failed to catch null pointer")
522    except ValueError:
523      pass
524
525    # ////////////////////////////////// Templates ////////////////////////////////////////
526    pid = li_boost_shared_ptr.PairIntDouble(10, 20.2)
527    if (pid.baseVal1 != 20 or pid.baseVal2 != 40.4):
528      raise RuntimeError("Base values wrong")
529    if (pid.val1 != 10 or pid.val2 != 20.2):
530      raise RuntimeError("Derived Values wrong")
531
532  def verifyValue(self, expected, got):
533    if (expected != got):
534      raise RuntimeError("verify value failed. Expected: ", expected, " Got: ", got)
535
536  def verifyCount(self, expected, k):
537    got = li_boost_shared_ptr.use_count(k)
538    if (expected != got):
539      raise RuntimeError("verify use_count failed. Expected: ", expected, " Got: ", got)
540
541
542runme = li_boost_shared_ptr_runme()
543runme.main()
544