def test_lengths(self): """Composition should return correct number of elements""" self.assertEqual(len(self.bases_10pct), len(Partition(10,4,0))) self.assertEqual(len(self.bases_5pct), len(Partition(20,4,1))) self.assertEqual(len(self.bases_extra), len(Partition(10,5,0))) self.assertEqual(len(self.small), len(Partition(5, 2, 0))) self.assertEqual(len(self.unique), len(Partition(5, 1, 1)))
def test_single_partition(self): """If number of objects = bins * min, only one way to partition""" for num_bins in range(1, 10): for occupancy in range(10): self.assertEqual( len(Partition(num_bins * occupancy, num_bins, occupancy)), 1)
def test_str(self): """str(partition) should work as expected""" p = Partition(20,4,1) self.assertEqual(str(p), "Items: 20 Pieces: 4 Min Per Piece: 1") p.NumItems = 13 p.NumPieces = 2 p.MinOccupancy = 0 self.assertEqual(len(p), len(Partition(13, 2, 0))) self.assertEqual(str(p), "Items: 13 Pieces: 2 Min Per Piece: 0")
def test_partitions(self): """Test several properties of partitions, especially start/end""" for num_bins in range(1, 5): for occupancy in range(5): for num_items in \ range(num_bins*occupancy, num_bins*occupancy + 10): p = Partition(num_items, num_bins, occupancy) l = [i for i in p] l2 = [i for i in p] #check that calling it twice doesn't break it self.assertEqual(l, l2) #check the lengths self.assertEqual(len(p), len(l)) #check the ranges are the same... self.assertEqual(l[0][1:], l[-1][0:-1]) #and that they contain the right values. self.assertEqual(l[0][1:], [occupancy]*(num_bins - 1)) #check the first and last elements self.assertEqual(l[0][0], l[-1][-1]) self.assertEqual(l[0][0], \ num_items - occupancy * (num_bins - 1))
def test_values(self): """Partition should match precalculated values""" self.assertEqual(len(Partition(20, 4, 1)), 969)