|
rpm-build |
95f51c |
#!/usr/bin/env python3
|
|
rpm-build |
95f51c |
# Simple tests for the talloc python bindings.
|
|
rpm-build |
95f51c |
# Copyright (C) 2015 Petr Viktorin <pviktori@redhat.com>
|
|
rpm-build |
95f51c |
|
|
rpm-build |
95f51c |
import unittest
|
|
rpm-build |
95f51c |
import subprocess
|
|
rpm-build |
95f51c |
import sys
|
|
rpm-build |
95f51c |
import gc
|
|
rpm-build |
95f51c |
|
|
rpm-build |
95f51c |
import talloc
|
|
rpm-build |
95f51c |
import _test_pytalloc
|
|
rpm-build |
95f51c |
|
|
rpm-build |
95f51c |
|
|
rpm-build |
95f51c |
def dummy_func():
|
|
rpm-build |
95f51c |
pass
|
|
rpm-build |
95f51c |
|
|
rpm-build |
95f51c |
|
|
rpm-build |
95f51c |
class TallocTests(unittest.TestCase):
|
|
rpm-build |
95f51c |
|
|
rpm-build |
95f51c |
def test_report_full(self):
|
|
rpm-build |
95f51c |
# report_full is hardcoded to print to stdout, so use a subprocess
|
|
rpm-build |
95f51c |
process = subprocess.Popen([
|
|
rpm-build |
95f51c |
sys.executable, '-c',
|
|
rpm-build |
95f51c |
"""if True:
|
|
rpm-build |
95f51c |
import talloc, _test_pytalloc
|
|
rpm-build |
95f51c |
obj = _test_pytalloc.new()
|
|
rpm-build |
95f51c |
talloc.report_full(obj)
|
|
rpm-build |
95f51c |
"""
|
|
rpm-build |
95f51c |
], stdout=subprocess.PIPE)
|
|
rpm-build |
95f51c |
output, stderr = process.communicate()
|
|
rpm-build |
95f51c |
output = str(output)
|
|
rpm-build |
95f51c |
self.assertTrue("full talloc report on 'talloc.Object" in output)
|
|
rpm-build |
95f51c |
self.assertTrue("This is a test string" in output)
|
|
rpm-build |
95f51c |
|
|
rpm-build |
95f51c |
def test_totalblocks(self):
|
|
rpm-build |
95f51c |
obj = _test_pytalloc.new()
|
|
rpm-build |
95f51c |
# Two blocks: the string, and the name
|
|
rpm-build |
95f51c |
self.assertEqual(talloc.total_blocks(obj), 2)
|
|
rpm-build |
95f51c |
|
|
rpm-build |
95f51c |
def test_repr(self):
|
|
rpm-build |
95f51c |
obj = _test_pytalloc.new()
|
|
rpm-build |
95f51c |
prefix = '
|
|
rpm-build |
95f51c |
self.assertTrue(repr(obj).startswith(prefix))
|
|
rpm-build |
95f51c |
self.assertEqual(repr(obj), str(obj))
|
|
rpm-build |
95f51c |
|
|
rpm-build |
95f51c |
def test_base_repr(self):
|
|
rpm-build |
95f51c |
obj = _test_pytalloc.base_new()
|
|
rpm-build |
95f51c |
prefix = '
|
|
rpm-build |
95f51c |
self.assertTrue(repr(obj).startswith(prefix))
|
|
rpm-build |
95f51c |
self.assertEqual(repr(obj), str(obj))
|
|
rpm-build |
95f51c |
|
|
rpm-build |
95f51c |
def test_destructor(self):
|
|
rpm-build |
95f51c |
# Check correct lifetime of the talloc'd data
|
|
rpm-build |
95f51c |
lst = []
|
|
rpm-build |
95f51c |
obj = _test_pytalloc.DObject(lambda: lst.append('dead'))
|
|
rpm-build |
95f51c |
self.assertEqual(lst, [])
|
|
rpm-build |
95f51c |
del obj
|
|
rpm-build |
95f51c |
gc.collect()
|
|
rpm-build |
95f51c |
self.assertEqual(lst, ['dead'])
|
|
rpm-build |
95f51c |
|
|
rpm-build |
95f51c |
def test_base_destructor(self):
|
|
rpm-build |
95f51c |
# Check correct lifetime of the talloc'd data
|
|
rpm-build |
95f51c |
lst = []
|
|
rpm-build |
95f51c |
obj = _test_pytalloc.DBaseObject(lambda: lst.append('dead'))
|
|
rpm-build |
95f51c |
self.assertEqual(lst, [])
|
|
rpm-build |
95f51c |
del obj
|
|
rpm-build |
95f51c |
gc.collect()
|
|
rpm-build |
95f51c |
self.assertEqual(lst, ['dead'])
|
|
rpm-build |
95f51c |
|
|
rpm-build |
95f51c |
|
|
rpm-build |
95f51c |
class TallocComparisonTests(unittest.TestCase):
|
|
rpm-build |
95f51c |
|
|
rpm-build |
95f51c |
def test_compare_same(self):
|
|
rpm-build |
95f51c |
obj1 = _test_pytalloc.new()
|
|
rpm-build |
95f51c |
self.assertTrue(obj1 == obj1)
|
|
rpm-build |
95f51c |
self.assertFalse(obj1 != obj1)
|
|
rpm-build |
95f51c |
self.assertTrue(obj1 <= obj1)
|
|
rpm-build |
95f51c |
self.assertFalse(obj1 < obj1)
|
|
rpm-build |
95f51c |
self.assertTrue(obj1 >= obj1)
|
|
rpm-build |
95f51c |
self.assertFalse(obj1 > obj1)
|
|
rpm-build |
95f51c |
|
|
rpm-build |
95f51c |
def test_compare_different(self):
|
|
rpm-build |
95f51c |
# object comparison is consistent
|
|
rpm-build |
95f51c |
obj1, obj2 = sorted([
|
|
rpm-build |
95f51c |
_test_pytalloc.new(),
|
|
rpm-build |
95f51c |
_test_pytalloc.new()])
|
|
rpm-build |
95f51c |
self.assertFalse(obj1 == obj2)
|
|
rpm-build |
95f51c |
self.assertTrue(obj1 != obj2)
|
|
rpm-build |
95f51c |
self.assertTrue(obj1 <= obj2)
|
|
rpm-build |
95f51c |
self.assertTrue(obj1 < obj2)
|
|
rpm-build |
95f51c |
self.assertFalse(obj1 >= obj2)
|
|
rpm-build |
95f51c |
self.assertFalse(obj1 > obj2)
|
|
rpm-build |
95f51c |
|
|
rpm-build |
95f51c |
def test_compare_different_types(self):
|
|
rpm-build |
95f51c |
# object comparison falls back to comparing types
|
|
rpm-build |
95f51c |
if sys.version_info >= (3, 0):
|
|
rpm-build |
95f51c |
# In Python 3, types are unorderable -- nothing to test
|
|
rpm-build |
95f51c |
return
|
|
rpm-build |
95f51c |
if talloc.Object < _test_pytalloc.DObject:
|
|
rpm-build |
95f51c |
obj1 = _test_pytalloc.new()
|
|
rpm-build |
95f51c |
obj2 = _test_pytalloc.DObject(dummy_func)
|
|
rpm-build |
95f51c |
else:
|
|
rpm-build |
95f51c |
obj2 = _test_pytalloc.new()
|
|
rpm-build |
95f51c |
obj1 = _test_pytalloc.DObject(dummy_func)
|
|
rpm-build |
95f51c |
self.assertFalse(obj1 == obj2)
|
|
rpm-build |
95f51c |
self.assertTrue(obj1 != obj2)
|
|
rpm-build |
95f51c |
self.assertTrue(obj1 <= obj2)
|
|
rpm-build |
95f51c |
self.assertTrue(obj1 < obj2)
|
|
rpm-build |
95f51c |
self.assertFalse(obj1 >= obj2)
|
|
rpm-build |
95f51c |
self.assertFalse(obj1 > obj2)
|
|
rpm-build |
95f51c |
|
|
rpm-build |
95f51c |
|
|
rpm-build |
95f51c |
class TallocBaseComparisonTests(unittest.TestCase):
|
|
rpm-build |
95f51c |
|
|
rpm-build |
95f51c |
def test_compare_same(self):
|
|
rpm-build |
95f51c |
obj1 = _test_pytalloc.base_new()
|
|
rpm-build |
95f51c |
self.assertTrue(obj1 == obj1)
|
|
rpm-build |
95f51c |
self.assertFalse(obj1 != obj1)
|
|
rpm-build |
95f51c |
self.assertTrue(obj1 <= obj1)
|
|
rpm-build |
95f51c |
self.assertFalse(obj1 < obj1)
|
|
rpm-build |
95f51c |
self.assertTrue(obj1 >= obj1)
|
|
rpm-build |
95f51c |
self.assertFalse(obj1 > obj1)
|
|
rpm-build |
95f51c |
|
|
rpm-build |
95f51c |
def test_compare_different(self):
|
|
rpm-build |
95f51c |
# object comparison is consistent
|
|
rpm-build |
95f51c |
obj1, obj2 = sorted([
|
|
rpm-build |
95f51c |
_test_pytalloc.base_new(),
|
|
rpm-build |
95f51c |
_test_pytalloc.base_new()])
|
|
rpm-build |
95f51c |
self.assertFalse(obj1 == obj2)
|
|
rpm-build |
95f51c |
self.assertTrue(obj1 != obj2)
|
|
rpm-build |
95f51c |
self.assertTrue(obj1 <= obj2)
|
|
rpm-build |
95f51c |
self.assertTrue(obj1 < obj2)
|
|
rpm-build |
95f51c |
self.assertFalse(obj1 >= obj2)
|
|
rpm-build |
95f51c |
self.assertFalse(obj1 > obj2)
|
|
rpm-build |
95f51c |
|
|
rpm-build |
95f51c |
def test_compare_different_types(self):
|
|
rpm-build |
95f51c |
# object comparison falls back to comparing types
|
|
rpm-build |
95f51c |
if sys.version_info >= (3, 0):
|
|
rpm-build |
95f51c |
# In Python 3, types are unorderable -- nothing to test
|
|
rpm-build |
95f51c |
return
|
|
rpm-build |
95f51c |
if talloc.BaseObject < _test_pytalloc.DBaseObject:
|
|
rpm-build |
95f51c |
obj1 = _test_pytalloc.base_new()
|
|
rpm-build |
95f51c |
obj2 = _test_pytalloc.DBaseObject(dummy_func)
|
|
rpm-build |
95f51c |
else:
|
|
rpm-build |
95f51c |
obj2 = _test_pytalloc.base_new()
|
|
rpm-build |
95f51c |
obj1 = _test_pytalloc.DBaseObject(dummy_func)
|
|
rpm-build |
95f51c |
self.assertFalse(obj1 == obj2)
|
|
rpm-build |
95f51c |
self.assertTrue(obj1 != obj2)
|
|
rpm-build |
95f51c |
self.assertTrue(obj1 <= obj2)
|
|
rpm-build |
95f51c |
self.assertTrue(obj1 < obj2)
|
|
rpm-build |
95f51c |
self.assertFalse(obj1 >= obj2)
|
|
rpm-build |
95f51c |
self.assertFalse(obj1 > obj2)
|
|
rpm-build |
95f51c |
|
|
rpm-build |
95f51c |
|
|
rpm-build |
95f51c |
class TallocUtilTests(unittest.TestCase):
|
|
rpm-build |
95f51c |
|
|
rpm-build |
95f51c |
def test_get_type(self):
|
|
rpm-build |
95f51c |
self.assertTrue(talloc.Object is _test_pytalloc.get_object_type())
|
|
rpm-build |
95f51c |
|
|
rpm-build |
95f51c |
def test_reference(self):
|
|
rpm-build |
95f51c |
# Check correct lifetime of the talloc'd data with multiple references
|
|
rpm-build |
95f51c |
lst = []
|
|
rpm-build |
95f51c |
obj = _test_pytalloc.DObject(lambda: lst.append('dead'))
|
|
rpm-build |
95f51c |
ref = _test_pytalloc.reference(obj)
|
|
rpm-build |
95f51c |
del obj
|
|
rpm-build |
95f51c |
gc.collect()
|
|
rpm-build |
95f51c |
self.assertEqual(lst, [])
|
|
rpm-build |
95f51c |
del ref
|
|
rpm-build |
95f51c |
gc.collect()
|
|
rpm-build |
95f51c |
self.assertEqual(lst, ['dead'])
|
|
rpm-build |
95f51c |
|
|
rpm-build |
95f51c |
def test_get_base_type(self):
|
|
rpm-build |
95f51c |
self.assertTrue(talloc.BaseObject is _test_pytalloc.base_get_object_type())
|
|
rpm-build |
95f51c |
|
|
rpm-build |
95f51c |
def test_base_reference(self):
|
|
rpm-build |
95f51c |
# Check correct lifetime of the talloc'd data with multiple references
|
|
rpm-build |
95f51c |
lst = []
|
|
rpm-build |
95f51c |
obj = _test_pytalloc.DBaseObject(lambda: lst.append('dead'))
|
|
rpm-build |
95f51c |
ref = _test_pytalloc.base_reference(obj)
|
|
rpm-build |
95f51c |
del obj
|
|
rpm-build |
95f51c |
gc.collect()
|
|
rpm-build |
95f51c |
self.assertEqual(lst, [])
|
|
rpm-build |
95f51c |
del ref
|
|
rpm-build |
95f51c |
gc.collect()
|
|
rpm-build |
95f51c |
self.assertEqual(lst, ['dead'])
|
|
rpm-build |
95f51c |
|
|
rpm-build |
95f51c |
|
|
rpm-build |
95f51c |
if __name__ == '__main__':
|
|
rpm-build |
95f51c |
unittest.TestProgram()
|