def problem3(self, s): """Test the Backpack class's magic methods. 10 points.""" # Test Backpack.__eq__() in False case (3 points). b1 = s.Backpack("Name1", "Color", 10) b2 = s.Backpack("Name2", "Color", 10) points = self._evalTest(not b1==b2, "Backpack.__eq__() failed (different names)") b1 = s.Backpack("Name", "Color1", 10) b2 = s.Backpack("Name", "Color2", 10) points += self._evalTest(not b1==b2, "Backpack.__eq__() failed (different colors)") b1 = s.Backpack("Name", "Color", 10) b2 = s.Backpack("Name", "Color", 10) for item in [1, 2, 3]: b1.put(item); b2.put(item) b2.put(4) points += self._evalTest(not b1==b2, "Backpack.__eq__() failed (different number of contents)") # Test Backpack.__eq__() in True case (2 points). b1 = s.Backpack("Name", "Color", 100) b2 = s.Backpack("Name", "Color", 10) points += self._evalTest(b1==b2, "Backpack.__eq__() failed (equal objects)") for item in ["apple", "banana", "carrot"]: b1.put(item); b2.put(item) points += self._evalTest(b1==b2, "Backpack.__eq__() failed (equal objects)") # Test Backpack.__str__() on an empty Backpack (2 points). b1 = Backpack("Student", "green", 4) b2 = s.Backpack("Student", "green", 4) print("\nCorrect output:"); print(b1) print("\nStudent output:"); print(b2) points += self._grade(2, "Incorrect Backpack.__str__():\n{}\n".format(b2)) # Test Backpack.__str__() on a Backpack with contents (3 points). b1 = Backpack("Master Yoda", "tan", 7) b2 = s.Backpack("Master Yoda", "tan", 7) for item in ["Crystal", "Lightsaber", "Commlink", "Banana"]: b1.put(item); b2.put(item) print("\nCorrect output:"); print(b1) print("\nStudent output:"); print(b2) points += self._grade(3, "Incorrect Backpack.__str__():\n{}\n".format(b2)) return points
def problem1(self, s): """Test the Backpack class. 10 points.""" # Test the constructor (2 points) b1 = Backpack("Teacher", "silver") b2 = s.Backpack("Student", "green") points = 2 # Test put() (6 points). for item in range(5): b1.put(item); b2.put(item) points += self._eqTest(b1.contents, b2.contents, "Backpack.put() failed to update Backpack.contents correctly") print("\nCorrect output:\t"),; b1.put(5) print("Student output:\t"),; b2.put(5) points += self._grade(1, 'Backpack.put() failed to print "No Room!"') points += self._eqTest(b1.contents, b2.contents, "Backpack.put() failed to update Backpack.contents correctly") b1 = Backpack("Teacher", "silver", 1) b2 = s.Backpack("Student", "green", 1) b1.put(100); b2.put(100) print("\nCorrect output:\t"),; b1.put(200) print("Student output:\t"),; b2.put(200) points += self._grade(1, 'Backpack.put() failed to print "No Room!"') points += 2*self._eqTest(b1.contents, b2.contents, "Backpack.put() failed to update Backpack.contents correctly") # Test dump() (2 points) b1.dump(); b2.dump() points += 2*self._eqTest(b1.contents, b2.contents, "Backpack.dump() failed to empty contents") return points