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
|
import sys
import weakref
import gc
import os.path
import copy
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)),
'..', '..', 'build', 'tests', 'boost'))
import bar
import unittest
class TestBar(unittest.TestCase):
def test_basic_gc(self):
count0 = bar.Foo.instance_count
f = bar.Foo("hello")
self.assertEqual(bar.Foo.instance_count, count0 + 1)
self.assertEqual(f.get_datum(), "hello")
del f
while gc.collect():
pass
self.assertEqual(bar.Foo.instance_count, count0)
def test_function_takes_foo(self):
count0 = bar.Foo.instance_count
f = bar.Foo("hello123")
self.assertEqual(bar.Foo.instance_count, count0 + 1)
self.assertEqual(f.get_datum(), "hello123")
bar.function_that_takes_foo(f)
del f
while gc.collect():
pass
self.assertEqual(bar.Foo.instance_count, count0+1) # the object stays alive
f1 = bar.function_that_returns_foo()
self.assertEqual(f1.get_datum(), "hello123")
self.assertEqual(bar.Foo.instance_count, count0+1)
del f1
while gc.collect():
pass
self.assertEqual(bar.Foo.instance_count, count0+1) # the object stays alive
def test_class_takes_foo(self):
count0 = bar.Foo.instance_count
f = bar.Foo("hello12")
self.assertEqual(bar.Foo.instance_count, count0 + 1)
self.assertEqual(f.get_datum(), "hello12")
takes = bar.ClassThatTakesFoo(f)
del f
while gc.collect():
pass
self.assertEqual(bar.Foo.instance_count, count0+1) # the object stays alive
f1 = takes.get_foo()
self.assertEqual(f1.get_datum(), "hello12")
self.assertEqual(bar.Foo.instance_count, count0+1)
del f1, takes
while gc.collect():
pass
self.assertEqual(bar.Foo.instance_count, count0)
def test_class_takes_foo_subclassing(self):
count0 = bar.Foo.instance_count
f = bar.Foo("hello45")
self.assertEqual(bar.Foo.instance_count, count0 + 1)
self.assertEqual(f.get_datum(), "hello45")
class Takes(bar.ClassThatTakesFoo):
def get_modified_foo(self, foo):
d = foo.get_datum()
return bar.Foo(d+"xxx")
takes = Takes(f)
del f
while gc.collect():
pass
self.assertEqual(bar.Foo.instance_count, count0+1)
f1 = takes.get_foo()
self.assertEqual(f1.get_datum(), "hello45")
self.assertEqual(bar.Foo.instance_count, count0+1)
f2 = bar.Foo("helloyyy")
self.assertEqual(bar.Foo.instance_count, count0+2)
f3 = takes.get_modified_foo(f2)
self.assertEqual(bar.Foo.instance_count, count0+3)
self.assertEqual(f3.get_datum(), "helloyyyxxx")
del f1, f2, f3, takes
while gc.collect():
pass
self.assertEqual(bar.Foo.instance_count, count0)
if __name__ == '__main__':
unittest.main()
|