Skip to content

Commit

Permalink
Further beef up memory tests
Browse files Browse the repository at this point in the history
  • Loading branch information
r-owen committed Sep 23, 2017
1 parent b32b38b commit f937521
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 7 deletions.
45 changes: 45 additions & 0 deletions python/astshim/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,51 @@ def checkBasicSimplify(self, amap):
self.assertEqual(cmp3.nIn, cmp3simp.nIn)
self.assertEqual(cmp3.nOut, cmp3simp.nOut)

def checkMemoryForCompoundObject(self, obj1, obj2, cmpObj, isSeries):
"""Check the memory usage for a compoundObject
obj1: first object in compound object
obj2: second object in compound object
cmpObj: compound object (SeriesMap, ParallelMap, CmpMap or CmpFrame)
isSeries: is compound object in series? None to not test (e.g. CmpFrame)
"""
# if obj1 and obj2 are the same type then copying the compound object
# will increase the NObject of each by 2, otherwise 1
deltaObj = 2 if type(obj1) == type(obj2) else 1

initialNumObj1 = obj1.getNObject()
initialNumObj2 = obj2.getNObject()
initialNumCmpObj = cmpObj.getNObject()
initialRefCountObj1 = obj1.getRefCount()
initialRefCountObj2 = obj2.getRefCount()
initialRefCountCmpObj = cmpObj.getRefCount()
self.assertEqual(obj1.getNObject(), initialNumObj1)
self.assertEqual(obj2.getNObject(), initialNumObj2)
if isSeries is not None:
if isSeries is True:
self.assertTrue(cmpObj.series)
elif isSeries is False:
self.assertFalse(cmpObj.series)

# making a deep copy should increase the object count of the contained objects
# but should not affect the reference count
cp = cmpObj.copy()
self.assertEqual(cmpObj.getRefCount(), initialRefCountCmpObj)
self.assertEqual(cmpObj.getNObject(), initialNumCmpObj + 1)
self.assertEqual(obj1.getRefCount(), initialRefCountObj1)
self.assertEqual(obj2.getRefCount(), initialRefCountObj2)
self.assertEqual(obj1.getNObject(), initialNumObj1 + deltaObj)
self.assertEqual(obj2.getNObject(), initialNumObj2 + deltaObj)

# deleting the deep copy should restore ref count and nobject
del cp
self.assertEqual(cmpObj.getRefCount(), initialRefCountCmpObj)
self.assertEqual(cmpObj.getNObject(), initialNumCmpObj)
self.assertEqual(obj1.getRefCount(), initialRefCountObj1)
self.assertEqual(obj1.getNObject(), initialNumObj1)
self.assertEqual(obj2.getRefCount(), initialRefCountObj2)
self.assertEqual(obj2.getNObject(), initialNumObj2)


def makePolyMapCoeffs(nIn, nOut):
"""Make an array of coefficients for astshim.PolyMap for the following equation:
Expand Down
2 changes: 2 additions & 0 deletions tests/test_cmpFrame.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ def test_CmpFrame(self):
self.assertEqual(cmpframe.getLabel(3), "c")

self.checkPersistence(cmpframe)
self.checkMemoryForCompoundObject(frame1, frame2, cmpframe, isSeries=None)


if __name__ == "__main__":
unittest.main()
14 changes: 7 additions & 7 deletions tests/test_cmpMap.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,24 @@ def setUp(self):

def test_SeriesMap(self):
sermap = ast.SeriesMap(self.shiftmap, self.zoommap)
# adding to a SeriesMap increases by 1
self.assertEqual(self.shiftmap.getRefCount(), 2)
# adding to a SeriesMap increases by 1
self.assertEqual(self.zoommap.getRefCount(), 2)
self.assertEqual(sermap.nIn, self.nin)
self.assertEqual(sermap.nOut, self.nin)
self.assertTrue(sermap.series)
self.assertEqual(sermap.getRefCount(), 1)

self.checkBasicSimplify(sermap)
self.checkCopy(sermap)
self.checkPersistence(sermap)
self.checkMemoryForCompoundObject(self.shiftmap, self.zoommap, sermap, isSeries=True)

sermap2 = self.shiftmap.then(self.zoommap)
self.checkBasicSimplify(sermap2)
self.checkCopy(sermap2)
self.checkPersistence(sermap2)
self.checkMemoryForCompoundObject(self.shiftmap, self.zoommap, sermap2, isSeries=True)

sermap3 = ast.CmpMap(self.shiftmap, self.zoommap, True)
self.checkBasicSimplify(sermap3)
self.checkCopy(sermap3)
self.checkPersistence(sermap3)
self.checkMemoryForCompoundObject(self.shiftmap, self.zoommap, sermap3, isSeries=True)

indata = np.array([
[1.0, 2.0, -6.0, 30.0, 0.2],
Expand Down Expand Up @@ -75,11 +72,13 @@ def test_ParallelMap(self):
self.checkBasicSimplify(parmap)
self.checkCopy(parmap)
self.checkPersistence(parmap)
self.checkMemoryForCompoundObject(self.shiftmap, self.zoommap, parmap, isSeries=False)

parmap2 = self.shiftmap.under(self.zoommap)
self.checkBasicSimplify(parmap2)
self.checkCopy(parmap2)
self.checkPersistence(parmap2)
self.checkMemoryForCompoundObject(self.shiftmap, self.zoommap, parmap2, isSeries=False)

indata = np.array([
[3.0, 1.0, -6.0],
Expand All @@ -100,6 +99,7 @@ def test_ParallelMap(self):
self.checkBasicSimplify(parmap3)
self.checkCopy(parmap3)
self.checkPersistence(parmap3)
self.checkMemoryForCompoundObject(self.shiftmap, self.zoommap, parmap3, isSeries=False)

topos3 = parmap3.applyForward(indata)
assert_allclose(topos3, pred_outdata)
Expand Down

0 comments on commit f937521

Please sign in to comment.