def test_gc_create0(self): gc.collect() cnt1 = gc.get_count() A = make_arrayclass("A", 2, gc=True) B = make_arrayclass("B", varsize=True, gc=True) b = B([], ()) a = A({}, b) cnt2 = gc.get_count() self.assertEqual(gc.is_tracked(a), True) self.assertEqual(gc.is_tracked(b), True) del a gc.collect() cnt3 = gc.get_count() self.assertEqual(cnt1, cnt3)
def test_gc_create1(self): gc.collect() cnt1 = gc.get_count() A = make_arrayclass("A", 2, gc=True) B = make_arrayclass("B", varsize=True, gc=True) C = make_arrayclass("C", 2, varsize=True, gc=True) b = A([], ()) c = C(1,2,{1:2,3:4},[1,2]) b1 = B(1, b) a = [b1, c] cnt2 = gc.get_count() self.assertEqual(gc.is_tracked(b), True) self.assertEqual(gc.is_tracked(b1), True) self.assertEqual(gc.is_tracked(c), True) del a gc.collect() cnt3 = gc.get_count() self.assertEqual(cnt1, cnt3)
def test_gc_varsize0(self): A = make_arrayclass("A", varsize=True, gc=True) a = A() self.assertEqual(repr(a), "A()") self.assertEqual(len(a), 0) with self.assertRaises(IndexError): a[0] with self.assertRaises(TypeError): weakref.ref(a) with self.assertRaises(AttributeError): a.__dict__ a = None
def test_fields0(self): A = make_arrayclass("A") a = A() self.assertEqual(len(a), 0) self.assertEqual(repr(a), "A()") with self.assertRaises(IndexError): a[0] with self.assertRaises(TypeError): weakref.ref(a) with self.assertRaises(AttributeError): a.__dict__ a = None with self.assertRaises(TypeError): A(1)
def test_varsize1(self): A = make_arrayclass("A", varsize=True) # print(A.__dict__) a = A(100) self.assertEqual(repr(a), "A(100)") self.assertEqual(len(a), 1) self.assertEqual(a[0], 100) self.assertEqual(a[-1], 100) with self.assertRaises(IndexError): a[1] with self.assertRaises(TypeError): weakref.ref(a) with self.assertRaises(AttributeError): a.__dict__ a = None
def test_fields1(self): A = make_arrayclass("A", 1) a = A(100) self.assertEqual(repr(a), "A(100)") self.assertEqual(len(a), 1) self.assertEqual(a[0], 100) self.assertEqual(a[-1], 100) with self.assertRaises(IndexError): a[1] with self.assertRaises(TypeError): weakref.ref(a) with self.assertRaises(AttributeError): a.__dict__ a = None with self.assertRaises(TypeError): A(1,2)
def test_varsize2(self): A = make_arrayclass("A", varsize=True) a = A(100,200) self.assertEqual(repr(a), "A(100, 200)") self.assertEqual(len(a), 2) self.assertEqual(a[0], 100) self.assertEqual(a[1], 200) a[0] = -100 a[1] = -200 self.assertEqual(a[0], -100) self.assertEqual(a[1], -200) with self.assertRaises(IndexError): a[100] with self.assertRaises(TypeError): weakref.ref(a) with self.assertRaises(AttributeError): a.__dict__ a = None
def test_missing_args(self): A = make_arrayclass("A", fields=3) a=A(1) self.assertEqual(a[0], 1) self.assertEqual(a[1], None) self.assertEqual(a[2], None)
def test_hash(self): A = make_arrayclass("A", fields=3, hashable=True) a=A(1, 2.0, "a") hash(a)
def test_iter(self): A = make_arrayclass("A", fields=3) a=A(1, 2.0, "a") self.assertEqual(list(iter(a)), [1, 2.0, "a"])
def test_tuple(self): A = make_arrayclass("A", 3) a=A(1, 2.0, "a") self.assertEqual(tuple(a), (1, 2.0, "a"))
import unittest import sys import gc import weakref import pickle, copy from recordclass import make_arrayclass, datatype from recordclass.utils import headgc_size, ref_size, pyobject_size, pyvarobject_size, pyssize TPickleV1 = make_arrayclass("TPickleV1", fields=3, varsize=True) TPickle1 = make_arrayclass("TPickle1", fields=3) class arrayobjectTest(unittest.TestCase): def test_create0(self): gc.collect() cnt1 = gc.get_count() A = make_arrayclass("A", 2) B = make_arrayclass("B", varsize=True) b = B([], ()) a = A({}, b) cnt2 = gc.get_count() self.assertEqual(gc.is_tracked(a), False) self.assertEqual(gc.is_tracked(b), False) del a gc.collect() cnt3 = gc.get_count() self.assertEqual(cnt1, cnt3) def test_create1(self): gc.collect()