def test400_010_shouldReturnInt(self): maxCapacity = 10 theRepository = Repository.Repository(maxCapacity) for i in range(maxCapacity): theRepository.addComponent( Component.Component("C" + str(i), i + 1, i + 1)) self.assertIsInstance(theRepository.validCount(), int)
def test500_010_shouldReturnValidCount(self): maxCapacity = 10 theRepository = Repository.Repository(maxCapacity) for i in range(maxCapacity): theRepository.addComponent( Component.Component("C" + str(i), 0, i + 1)) self.assertEqual(0, theRepository.validCount())
def test600_030_shouldReturnSizeList(self): # test set: #name methods LOC ln(loc/methods) #C0 0 42 NA #C1 1 19 2.944438979 #C2 2 58 3.36729583 #C3 3 60 2.995732274 #C4 4 70 2.862200881 #C5 5 82 2.797281335 #C6 6 57 2.251291799 #C7 7 89 2.542726221 #C8 8 80 2.302585093 #C9 9 128 2.654805687 # avg = 2.746484233 # stdev = 0.352645834 # This yields [8, 11, 16, 23, 32] maxCapacity = 4 theRepository = Repository.Repository(maxCapacity) methodCounts = [1, 4, 7, 5] locCounts = [76, 116, 113, 103] for i in range(maxCapacity): theRepository.addComponent( Component.Component("C" + str(i), methodCounts[i], locCounts[i])) self.assertListEqual([8, 15, 30, 58, 115], theRepository.determineRelativeSizes())
def testInit(self): ''' Test init method. ''' # Instantiate a repository with capacity = 0. # ValueError is raised. Repository is not constructed. self.assertRaises(ValueError, Repository.Repository, 0) # Instantiate a repository with capacity = 1. # Repository is constructed. Capacity of repository is 1. rep1 = Repository.Repository(1) self.assertEqual(rep1.capacity, 1) # Instantiate a repository without specifying a capacity. # Repository is constructed. Capacity of repository is 100. rep2 = Repository.Repository() self.assertEqual(rep2.capacity, 100)
def test200_010_shouldAddComponent(self): maxCapacity = 2 theRepository = Repository.Repository(maxCapacity) for i in range(maxCapacity): self.assertEquals( i + 1, theRepository.addComponent( Component.Component("C" + str(i), i + 1, i + 1)))
def testDetermineRelativeSizes(self): ''' Test determineRelativeSizes method. ''' # Determine the relative sizes of the repository created in CA01-2.1#3 # (an empty repository). ValueError is raised, because validCount() is 0 # (i.e. < 2). rep1 = Repository.Repository() self.assertRaises(ValueError, rep1.determineRelativeSizes) # Determine the relative sizes of the repository created in CA01-2.1#2 # after test CA01-2.2#1 is run (a small non-empty repository). # ValueError is raised, because validCount() is 0 (i.e. < 2). com1 = Component.Component("a", 0, 1) rep2 = Repository.Repository(1) rep2.addComponent(com1) self.assertRaises(ValueError, rep2.determineRelativeSizes) # Determine the relative sizes of the repository created in CA01-2.1#2 # after test CA01-2.2#2 is run (a small non-empty repository). # ValueError is raised, because validCount() is 1 (i.e. < 2). com2 = Component.Component("a", 1, 1) rep3 = Repository.Repository(1) rep3.addComponent(com2) self.assertRaises(ValueError, rep3.determineRelativeSizes) # Determine the relative sizes of the repository created in CA01-2.1#3 # after 3 components (with methodCount = 1 and locCount = 5, 10 and 15) # are added (a medium non-empty repository). # [3.0, 6.0, 10.0, 16.0, 28.0] is returned. rep4 = Repository.Repository() com3 = Component.Component("a", 1, 5) com4 = Component.Component("b", 1, 10) com5 = Component.Component("c", 1, 15) rep4.addComponent(com3) rep4.addComponent(com4) rep4.addComponent(com5) relativeSizes = rep4.determineRelativeSizes() self.assertEqual(relativeSizes[0], 3.0) self.assertEqual(relativeSizes[1], 6.0) self.assertEqual(relativeSizes[2], 10.0) self.assertEqual(relativeSizes[3], 16.0) self.assertEqual(relativeSizes[4], 28.0)
def testCount(self): ''' Test count method. ''' # Get the number of components in the repository created in CA01-2.1#3 # (an empty repository). 0 is returned. CA01-2.1#3's number of # components is unchanged. rep1 = Repository.Repository() self.assertEqual(rep1.count(), 0) self.assertEqual(len(rep1.queue), 0) # Get the number of components in the repository created in CA01-2.1#2 # after test CA01-2.2#2 is run (a non-empty repository). 1 is returned. # CA01-2.2#2's number of components is unchanged. rep2 = Repository.Repository() com1 = Component.Component("a", 1, 1) rep2.addComponent(com1) self.assertEqual(rep2.count(), 1) self.assertEqual(len(rep2.queue), 1)
def test200_020_shouldAddComponentPastCapacity(self): maxCapacity = 2 theRepository = Repository.Repository(maxCapacity) for i in range(maxCapacity): theRepository.addComponent( Component.Component("C" + str(i), i + 1, i + 1)) self.assertEquals( maxCapacity, theRepository.addComponent(Component.Component("overflow", 1, 10)))
def test300_020_shouldReturnCount(self): maxCapacity = 10 theRepository = Repository.Repository(maxCapacity) for i in range(maxCapacity): theRepository.addComponent( Component.Component("C" + str(i), i + 1, i + 1)) self.assertEqual(i + 1, theRepository.count()) self.assertEquals( maxCapacity, theRepository.addComponent(Component.Component("overflow", 1, 10)))
def test100_920_ShouldRaiseExceptionOnInvalidCapacity(self): expectedString = "Repository.__init__:" try: myRepository = Repository.Repository(capacity=0) self.fail("exception was not raised") except ValueError as raisedException: diagnosticString = raisedException.args[0] self.assertEquals(expectedString, diagnosticString[0:len(expectedString)]) except: self.fail("incorrect exception was raised")
def test200_020_shouldDeleteOldestPastCapacity(self): maxCapacity = 2 theRepository = Repository.Repository(maxCapacity) # Add maxCapacity+1 components # Ensure the first one has a zero method count, all others have non-zero method count # We infer that the first component -- the only component with a non-zero method count -- has # been deleted if validCount() == 2 for i in range(maxCapacity + 1): theRepository.addComponent( Component.Component("C" + str(i), i, i + 1)) self.assertEquals(2, theRepository.validCount())
def testExceptionCapacity0(self): #Exception if the capacity is 0 expectedString = "Repository.__init__: The capacity needs to be grand than 0" try: testRepository = repository.Repository(0) self.fail("exception was not raised") except ValueError as raisedException: diagnosticString = raisedException.args[0] self.assertEquals(expectedString, diagnosticString[0:len(expectedString)]) except: self.fail("incorrect exception was raised")
def test200_910_shouldRaiseExceptionIfComponentMissing(self): #AMBexpectedString = "Repository.addComponent: " expectedString = "Repository.addComponent:" theRepository = Repository.Repository() try: theRepository.addComponent() self.fail("exception was not raised") except ValueError as raisedException: diagnosticString = raisedException.args[0] self.assertEquals(expectedString, diagnosticString[0:len(expectedString)]) except: self.fail("incorrect exception was raised")
def test600_010_shouldReturnIntList(self): maxCapacity = 10 theRepository = Repository.Repository(maxCapacity) methodCounts = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] locCounts = [42, 19, 58, 60, 70, 82, 57, 89, 80, 128] for i in range(maxCapacity): theRepository.addComponent( Component.Component("C" + str(i), methodCounts[i], locCounts[i])) listOfSizes = theRepository.determineRelativeSizes() self.assertIsInstance(listOfSizes, list) for size in listOfSizes: self.assertIsInstance(size, int)
def test600_910_shouldRaiseExceptionOnSmallCapacity(self): expectedString = "Repository.determineRelativeSizes:" maxCapacity = 100 theRepository = Repository.Repository(maxCapacity) theRepository.addComponent(Component.Component("LoneComponent", 1, 10)) try: theRepository.determineRelativeSizes() self.fail("exception was not raised") except ValueError as raisedException: diagnosticString = raisedException.args[0] self.assertEquals(expectedString, diagnosticString[0:len(expectedString)]) except: self.fail("incorrect exception was raised")
def testValidCount(self): ''' Test validCount method. ''' # Get the number of components that have methodCount > 0 from the # repository created in CA01-2.1#3 (an empty repository). 0 is returned. rep1 = Repository.Repository() self.assertEqual(rep1.validCount(), 0) # Get the number of components that have methodCount > 0 from the # repository created in CA01-2.1#2 after CA01-2.2#1 is run (a non-empty # repository). 0 is returned. com1 = Component.Component("a", 0, 1) rep2 = Repository.Repository(1) rep2.addComponent(com1) self.assertEqual(rep2.validCount(), 0) # Get the number of components that have methodCount > 0 from the # repository created in CA01-2.1#2 after CA01-2.2#2 is run (a non-empty # repository). 1 is returned. com2 = Component.Component("a", 1, 1) rep3 = Repository.Repository(1) rep3.addComponent(com2) self.assertEqual(rep3.validCount(), 1)
def testAddComponent(self): ''' Test addComponent method. ''' # Add a component (with methodCount = 0) to the repository created in # CA01 - 2.1#2. Repository contains one component (this one). 1 is # returned. com1 = Component.Component("a", 0, 1) rep1 = Repository.Repository(1) self.assertEqual(rep1.addComponent(com1), 1) self.assertEqual(rep1.queue[0].methodCount, 0) # Add another component (with methodCount = 1) to the repository created # in CA01-2.1#2. Repository contains one component (this one). The # component added in CA01-2.2#1 is popped. 1 is returned. com2 = Component.Component("a", 1, 1) self.assertEqual(rep1.addComponent(com2), 1) self.assertEqual(rep1.queue[0].methodCount, 1)
def testRelativeSizes(self): #test the relative sizes of the components in the repository self.testRepository = repository.Repository(5) component1 = component.Component(name="Component01", methodCount=1, locCount=76) component2 = component.Component("Component02", locCount=116, methodCount=4) component3 = component.Component("Component03", 7, locCount=113) component4 = component.Component("Component04", 5, 103) component5 = component.Component("Component5", 0, 10) self.testRepository.addComponent(component1) self.testRepository.addComponent(component2) self.testRepository.addComponent(component3) self.testRepository.addComponent(component4) self.testRepository.addComponent(component5) relativeSizes = self.testRepository.determineRelativeSizes()
def setUp(self): self.testRepository = repository.Repository(2)
def testRepositoryNoParam(self): # test the creation of a repository without parameter self.testRepository = repository.Repository()
def testRepositoryCap2(self): # test the creation of a repository with capacity=2 self.testRepository = repository.Repository(2)
def testAddComponent(self): # test the addition of a component self.testRepository = repository.Repository(2) C1 = component.Component("C1", 4, 20) self.testRepository.addComponent(C1)
def test100_020_ShouldConstructRepositoryDefaultCapacity(self): self.assertIsInstance(Repository.Repository(), Repository.Repository)
def test100_010_ShouldConstructRepositoryExplicitCapacity(self): self.assertIsInstance(Repository.Repository(capacity=100), Repository.Repository)
def testInstantiateRep200(self): # test the creation of a repository with capacity=200 testRepository = repository.Repository(200)