class Test_Bag(unittest.TestCase): def setUp(self): self.alist = ['d', 'a', 'b', 'd', 'c', 'b', 'd'] self.bag = Bag(self.alist) def testlen(self): random.shuffle(self.alist) size = 7 while size: self.bag.remove(self.alist[size - 1]) size -= 1 assert len(self.bag) == size def testunique(self): random.shuffle(self.alist) size = 7 while size: self.bag.remove(self.alist[size - 1]) size -= 1 assert len(set(i for i in self.bag)) == self.bag.unique() def testcontains(self): assert all([i in self.bag for i in ['a', 'b', 'c', 'd']]) and not 'x' in self.bag def testcount(self): assert all( self.bag.count(i) == j for i, j in { 'a': 1, 'b': 2, 'c': 1, 'd': 3, 'x': 0 }.items()) random.shuffle(self.alist) size = 7 while size: self.bag.remove(self.alist[size - 1]) size -= 1 assert len(self.bag) == size def test__eq__(self): l = [random.randint(1, 10) for i in range(1000)] first = Bag(l) random.shuffle(l) second = Bag(l) assert first == second first.remove(l[0]) assert first != second def testadd(self): l = [random.randint(1, 10) for i in range(1000)] first = Bag(l) second = Bag([]) random.shuffle(l) for i in l: second.add(i) assert first == second def testremove(self): l = [random.randint(1, 10) for i in range(1000)] first = Bag(l) try: first.remove(42) except Exception as e: assert type(e) == ValueError second = Bag(l) random.shuffle(l) for i in l: second.add(i) for i in l: second.remove(i) assert first == second
class Test_Bag(unittest.TestCase): def setUp(self): self.alist = ['d', 'a', 'b', 'd', 'c', 'b', 'd'] self.bag = Bag(self.alist) def test_len(self): initialv = 7 while initialv >= 0: self.assertEqual(self.bag.__len__(), initialv, 'unexpected len') random.shuffle(self.alist) if initialv > 0: toremove = self.alist[0] self.bag.remove(toremove) self.alist.remove(toremove) initialv -= 1 def test_unique(self): initialv = 4 while initialv >= 0: uniquevals = list(self.bag.counts) random.shuffle(uniquevals) self.assertEqual(self.bag.unique(), initialv, 'unexpected unique val') self.assertEqual(self.bag.unique(), len(self.bag.counts), 'unexpected unique val') if initialv > 0: toremove = uniquevals[0] del self.bag.counts[toremove] initialv -= 1 def test_contains(self): for value in ['a', 'b', 'c', 'd']: self.assertTrue(self.bag.__contains__(value), 'unexpected contains') self.assertFalse(self.bag.__contains__('x'), 'bag contains x') def test_count(self): masterdict = {'a': 1, 'b': 2, 'c': 1, 'd': 3, 'x': 0} totcount = 7 for value in masterdict: self.assertEqual(self.bag.count(value), masterdict[value], 'unexpected count') masterdict = { 'a': 1, 'b': 2, 'c': 1, 'd': 3, } while totcount >= 0: totalvalue = 0 for value in self.bag.counts: totalvalue += self.bag.counts[value] self.assertEqual(totalvalue, totcount) listkeys = list(masterdict) random.shuffle(listkeys) if totcount > 0: toremove = listkeys[0] self.bag.remove(toremove) masterdict[toremove] -= 1 if masterdict[toremove] == 0: del masterdict[toremove] totcount -= 1 def test_equals(self): listone = [] listtwo = [] for x in range(1000): c = random.randint(1, 10) listone.append(c) listtwo.append(c) random.shuffle(listone) b1 = Bag(listone) b2 = Bag(listtwo) self.assertTrue(b1 == b2) def test_add(self): listone = [] listtwo = [] for x in range(1000): c = random.randint(1, 10) listone.append(c) listtwo.append(c) random.shuffle(listone) b1 = Bag(listone) b2 = Bag() for item in listtwo: b2.add(item) self.assertTrue(b1 == b2) def test_remove(self): listone = [] listtwo = [] for x in range(1000): c = random.randint(1, 10) listone.append(c) listtwo.append(c) random.shuffle(listtwo) b1 = Bag(listone) b2 = Bag(listone) self.assertRaises(ValueError, b1.remove, 53) for item in listtwo: b2.add(item) for item in listtwo: b2.remove(item) self.assertTrue(b1 == b2)
class Test_Bag(unittest.TestCase): def setUp(self): self.alist = ['d', 'a', 'b', 'd', 'c', 'b', 'd'] self.bag = Bag(self.alist) def test_len(self): self.assertEqual(len(self.bag), 7) expected = 6 random.shuffle(self.alist) for v in self.alist: self.bag.remove(v) self.assertEqual(len(self.bag), expected) expected -= 1 def test_unique(self): self.assertEqual(self.bag.unique(), 4) random.shuffle(self.alist) for v in self.alist: self.bag.remove(v) self.assertEqual(self.bag.unique(), len(self.bag.counts)) def test_contains(self): for s in 'abcd': self.assertIn(s, self.bag) self.assertNotIn('x', self.bag) def test_count(self): self.assertEqual(self.bag.count('a'), 1) self.assertEqual(self.bag.count('b'), 2) self.assertEqual(self.bag.count('c'), 1) self.assertEqual(self.bag.count('d'), 3) self.assertEqual(self.bag.count('x'), 0) random.shuffle(self.alist) expected = 6 for v in self.alist: self.bag.remove(v) self.assertEqual(sum([self.bag.count(s) for s in 'abcd']), expected) expected -= 1 def test_equal(self): test = [random.randint(1, 10) for _ in range(1000)] bag1 = Bag(test) random.shuffle(test) bag2 = Bag(test) self.assertEqual(bag1, bag2) bag1.remove(test[0]) self.assertNotEqual(bag1, bag2) def test_add(self): test = [random.randint(1, 10) for _ in range(1000)] bag1 = Bag(test) bag2 = Bag() random.shuffle(test) for v in test: bag2.add(v) self.assertEqual(bag1, bag2) def test_remove(self): test = [random.randint(1, 10) for _ in range(1000)] bag1 = Bag(test) self.assertRaises(ValueError, bag1.remove, 86) bag2 = Bag(test) random.shuffle(test) test1 = [random.randint(1, 10) for _ in range(1000)] for v in test1: bag2.add(v) for v in test1: bag2.remove(v) self.assertEqual(bag1, bag2)
class Test_Bag(unittest.TestCase): def setup(self): self.alist = ['d', 'a', 'b', 'd', 'c', 'b', 'd'] self.bag = Bag(self.alist) def test_len(self): self.setup() self.assertEqual(self.bag.__len__(), 7) #Initial Bag should have 7 items count = 7 random.shuffle(self.alist) #Shuffle alist for x in self.alist: self.bag.remove(x) #Remove random item in bag count -= 1 self.assertEqual(self.bag.__len__(), count) #Check if item count went down def test_unique(self): self.setup() self.assertEqual(self.bag.unique(), 4) random.shuffle(self.alist) #Shuffle alist for x in self.alist: self.bag.remove(x) #Remove random item in bag self.assertEqual(self.bag.unique(), len(self.bag.counts)) def test_contains(self): self.setup() self.assertTrue(self.bag.__contains__('a')) and self.assertTrue( self.bag.__contains__('b')) and self.assertTrue( self.bag.__contains__('c')) self.assertFalse(self.bag.__contains__('x')) def test_count(self): self.setup() self.assertEqual(self.bag.counts['a'], 1) self.assertEqual(self.bag.counts['b'], 2) self.assertEqual(self.bag.counts['c'], 1) self.assertEqual(self.bag.counts['d'], 3) self.assertEqual(self.bag.counts['x'], 0) count = sum(self.bag.counts.values()) random.shuffle(self.alist) #Shuffle alist for x in self.alist: self.bag.remove(x) count -= 1 self.assertEqual(sum(self.bag.counts.values()), count) def test_equals(self): rand_list = [random.randint(1, 10) for i in range(0, 1000)] #Make 1000-value random list bag1 = Bag(rand_list) #Create bag from random list random.shuffle(rand_list) #Shuffle random list bag2 = Bag(rand_list) #Create another bag from shuffled random list self.assertTrue(bag1.__eq__(bag2)) #They should be equal bag1.remove(rand_list[0]) #Remove 1 value from one bag self.assertFalse(bag1.__eq__(bag2)) #They should NOT be equal def test_add(self): rand_list = [random.randint(1, 10) for i in range(0, 1000)] #Make 1000-value random list bag1 = Bag(rand_list) #Create bag from random list bag2 = Bag() #Create another bag from shuffled random list random.shuffle(rand_list) #Shuffle random list for x in rand_list: bag2.add(x) self.assertEqual(bag1, bag2) def test_remove(self): rand_list = [random.randint(1, 10) for i in range(0, 1000)] #Make 1000-value random list bag1 = Bag(rand_list) #Create bag from random list with self.assertRaises(ValueError): bag1.remove(43) bag2 = Bag(rand_list) random.shuffle(rand_list) #Shuffle random list for x in rand_list: bag2.add(x) for x in rand_list: bag2.remove(x) self.assertEqual(bag1, bag2)
class Test_Bag(unittest.TestCase): def setUp(self): self.original = ['d', 'a', 'b', 'd', 'c', 'b', 'd'] self.bag = Bag(['d', 'a', 'b', 'd', 'c', 'b', 'd']) def test_len(self): self.assertEqual(len(self.bag), 7, msg = "Length of constructed bag is not 7.") my_list = self.original.copy() for i in range(6, -1, -1): s = random.choice(my_list) self.bag.remove(s) my_list.remove(s) self.assertEqual(len(self.bag), i, msg = "Length of constructed bag is not " + str(i) +".") self.assertEqual(len(self.bag), 0, msg = "Final length of constructed bag is not 0.") def test_unique(self): comp_set = set(self.original) self.assertIs(self.bag.unique(), 4, msg = "Bag is not unique.") my_list = self.original.copy() while len(comp_set) != 0: s = random.choice(my_list) self.bag.remove(s) my_list.remove(s) comp_set = set(my_list) self.assertIs(self.bag.unique(), len(comp_set), msg = "Bag is not unique.") def test_contains(self): comp_set = ['a', 'b', 'c', 'd'] while len(comp_set) != 0: self.assertTrue(Bag.__contains__(self.bag, comp_set[0]), "'{}' not in bag.".format(str(comp_set[0]))) comp_set.remove(comp_set[0]) self.assertFalse(Bag.__contains__(self.bag, 'x'), "'x' is in bag.") def test_count(self): self.assertEqual(self.bag.count('a'), 1, msg = "count('a') should be 1") self.assertEqual(self.bag.count('b'), 2, msg = "count('b') should be 2") self.assertEqual(self.bag.count('c'), 1, msg = "count('c') should be 1") self.assertEqual(self.bag.count('d'), 3, msg = "count('d') should be 3") self.assertEqual(self.bag.count('x'), 0, msg = "count('x') should be 0") total_count = self.bag.count('a') + self.bag.count('b') + self.bag.count('c') + self.bag.count('d') my_list = self.original.copy() while total_count != 0: total_count -= 1 s = random.choice(my_list) self.bag.remove(s) my_list.remove(s) self.assertEqual(self.bag.count('a') + self.bag.count('b') + self.bag.count('c') + self.bag.count('d'), total_count, "Sum of counts not equal " + str(total_count)) self.assertEqual(self.bag.count('a') + self.bag.count('b') + self.bag.count('c') + self.bag.count('d'), total_count, "Final sum of counts not equal to 0.") def test_equal(self): to_bag = [random.randrange(1, 11) for i in range(1000)] bag1 = Bag(to_bag) bag2 = Bag(to_bag) self.assertEqual(bag1, bag2, "Bags are not equal.") bag1.remove(to_bag[0]) self.assertNotEqual(bag1, bag2, "Bags should not be equal.") def test_add(self): to_bag = [random.randrange(1, 11) for i in range(1000)] bag1 = Bag(to_bag) random.shuffle(to_bag) bag2 = Bag() for i in to_bag: bag2.add(i) self.assertEqual(bag1, bag2, "Bags are not equal.") def test_remove(self): to_bag = [random.randrange(1, 11) for i in range(1000)] bag1 = Bag(to_bag) self.assertRaises(ValueError, Bag.remove, bag1, 32) bag2 = Bag(to_bag) for i in to_bag: bag2.add(i) for i in to_bag: bag2.remove(i) self.assertEqual(bag1, bag2, "Bags are not equal.")
class Test_Bag(unittest.TestCase): def setUp(self): self.alist = ['d', 'a', 'b', 'd', 'c', 'b', 'd'] self.bag = Bag(self.alist) def test_len(self): if len(self.bag) != 7: raise count = 0 random.shuffle(self.alist) count += 7 for i in self.alist: self.bag.remove(i) count -= 1 if len(self.bag) != count: raise if len(self.bag) != 0: raise def test_unique(self): if self.bag.unique() != 4: raise random.shuffle(self.alist) for i in self.alist: self.bag.remove(i) if self.bag.unique() != len(set(self.bag)): raise def test_contains(self): for i in ['a', 'b', 'c', 'd']: if i not in self.bag: raise if 'x' in self.bag: raise def test_count(self): for a, b in zip(('a', 'b', 'c', 'd'), (1, 2, 1, 3)): if self.bag.count(a) != b: raise random.shuffle(self.alist) size = 7 for i in self.alist: self.bag.remove(i) size -= 1 sum_count = sum([self.bag.count(c) for c in ['a', 'b', 'c', 'd']]) if sum_count != size: raise if len(self.bag) != 0: raise def test_equals(self): alist = [] for i in range(1000): alist.append(random.randint(1, 10)) b1 = Bag(alist) random.shuffle(alist) b2 = Bag(alist) if b1 != b2: raise b1.remove(alist[0]) if b1 == b2: raise def test_add(self): alist = [] for i in range(1000): alist.append(random.randint(1, 10)) b1 = Bag(alist) random.shuffle(alist) b2 = Bag() for a in alist: b2.add(a) if b1 != b2: raise def test_remove(self): alist = [] for i in range(1000): alist.append(random.randint(1, 10)) b1 = Bag(alist) self.assertRaises(ValueError, b1.remove, 11) b2 = Bag(alist) random.shuffle(alist) for v in alist: b2.add(v) for v in alist: b2.remove(v) if b1 != b2: raise
class Test_Bag(unittest.TestCase): def setUp(self): self.alist = ['d', 'a', 'b', 'd', 'c', 'b', 'd'] self.bag = Bag(self.alist) def test_len(self): random.shuffle(self.alist) self.assertTrue(len(self.bag) == 7, 'len of bag not 7') for item in self.alist: count = len(self.bag) self.bag.remove(item) self.assertTrue(count - 1 == len(self.bag), 'len of bag did not decrease by 1') self.assertTrue( len(self.bag) == 0, 'len of bag did not eventually become 0') return True def test_unique(self): random.shuffle(self.alist) self.assertTrue( len(self.bag.counts) == 4, 'incorrect number of initial unique items') while len(self.alist) != 0: self.bag.remove(self.alist.pop(0)) self.assertTrue( len(self.bag.counts) == self.bag.unique(), 'incorrect number of unique values') def test_contains(self): for item in ['a', 'b', 'c', 'd', 'x']: self.assertIn( item, self.bag, 'a,b,c,d not in bag') if item is not 'x' else self.assertNotIn( item, self.bag, 'x is in bag') def test_count(self): self.assertDictEqual(dict(self.bag.counts), { 'a': 1, 'b': 2, 'c': 1, 'd': 3 }, 'incorrect number of initial values') random.shuffle(self.alist) for item in self.alist: sum_of_counts = sum( self.bag.count(key) for key in ['a', 'b', 'c', 'd']) self.bag.remove(item) sum_of_counts2 = sum( self.bag.count(key) for key in ['a', 'b', 'c', 'd']) self.assertTrue(sum_of_counts - 1 == sum_of_counts2, 'count did not decrease by 1') self.assertTrue( sum(self.bag.count(key) for key in ['a', 'b', 'c', 'd']) == 0, 'sum of counts did not reach 0') def test_equals(self): vals = [random.randint(1, 10) for i in range(1000)] bag1 = Bag(vals) random.shuffle(vals) bag2 = Bag(vals) self.assertTrue(bag1 == bag2, 'bags are not equal') bag1.remove(vals[0]) self.assertFalse(bag1 == bag2, 'bags are not supposed to be equal') def test_add(self): vals = [random.randint(1, 10) for i in range(1000)] bag1 = Bag(vals) random.shuffle(vals) bag2 = Bag() for item in vals: bag2.add(item) self.assertEqual(bag1, bag2, 'bags are not equal') def test_remove(self): vals = [random.randint(1, 10) for i in range(1000)] bag1 = Bag(vals) self.assertRaises(ValueError, lambda: bag1.remove(35)) bag2 = Bag(vals) random.shuffle(vals) for item in vals: bag2.add(item) for item in vals: bag2.remove(item) self.assertEqual(bag1, bag2, 'bags are not equal')
class Test_Bag(unittest.TestCase): def setUp(self): self.alist = ['d','a','b','d','c','b','d'] self.bag = Bag(self.alist) def testlen(self): assert len(self.bag) == 7 random.shuffle(self.alist) for i in irange(1, len(self.alist)): self.bag.remove(self.alist[i - 1]) assert len(self.bag) == 7 - i, '{}, {}'.format(i, self.bag.counts) def testunique(self): assert self.bag.unique() == 4 random.shuffle(self.alist) for i in irange(1, len(self.alist)): self.bag.remove(self.alist[i - 1]) assert self.bag.unique() == len(self.bag.counts) def testcontains(self): for letter in ['a', 'b', 'c']: assert self.bag.__contains__(letter) assert not self.bag.__contains__('x') def testcount(self): assert list(map(lambda x: self.bag.count(x), ['a', 'b', 'c', 'd', 'x'])) == [1, 2, 1, 3, 0] random.shuffle(self.alist) for i in irange(1, len(self.alist)): self.bag.remove(self.alist[i - 1]) assert sum([self.bag.count(x) for x in self.bag.counts]) == len(self.alist) - i def testequals(self): rand_list = [] for i in range(1000): rand_list.append(random.randint(1, 10)) bag = Bag(rand_list) random.shuffle(rand_list) bag2 = Bag(rand_list) assert bag == bag2 bag.remove(rand_list[0]) assert bag != bag2 def testadd(self): rand_list = [] for i in range(1000): rand_list.append(random.randint(1, 10)) bag = Bag(rand_list) bag2 = Bag() random.shuffle(rand_list) for value in rand_list: bag2.add(value) assert bag == bag2 def testremove(self): rand_list = [] for i in range(1000): rand_list.append(random.randint(1, 10)) bag = Bag(rand_list) try: bag.remove(62) except ValueError: pass bag2 = Bag(rand_list) random.shuffle(rand_list) for value in rand_list: bag2.add(value) for value in rand_list: bag2.remove(value) assert bag == bag2
class Test_Bag(unittest.TestCase): def setUp(self): self.alist = ['d','a','b','d','c','b','d'] self.bag = Bag(self.alist) def test_len(self): self.assertEqual(len(self.bag),7) size = 7 random.shuffle(self.alist) for i in self.alist: self.bag.remove(i) size -= 1 self.assertEqual(len(self.bag),size) self.assertEqual(len(self.bag),0) def test_unique(self): self.assertEqual(self.bag.unique(),4) random.shuffle(self.alist) for i in self.alist: self.bag.remove(i) self.assertEqual(self.bag.unique(),len(set(self.bag))) def test_contains(self): for v in ['a','b','c','d']: self.assertIn(v,self.bag) self.assertNotIn('x',self.bag) def test_count(self): for v,c in zip(('a','b','c','d'), (1,2, 1, 3)): self.assertEqual(self.bag.count(v),c) random.shuffle(self.alist) size = 7 for i in self.alist: self.bag.remove(i) size -= 1 sum_count = sum([self.bag.count(c) for c in ['a','b','c','d']]) self.assertEqual(sum_count,size) self.assertEqual(len(self.bag),0) def test_equals(self): alist = [random.randint(1,10) for i in range(1000)] b1 = Bag(alist) random.shuffle(alist) b2 = Bag(alist) self.assertEqual(b1,b2) b1.remove(alist[0]) self.assertNotEquals(b1,b2) def test_add(self): alist = [random.randint(1,10) for i in range(1000)] b1 = Bag(alist) random.shuffle(alist) b2 = Bag() for v in alist: b2.add(v) self.assertEqual(b1,b2) def test_remove(self): alist = [random.randint(1,10) for i in range(1000)] b1 = Bag(alist) self.assertRaises(ValueError,b1.remove,11) b2 = Bag(alist) random.shuffle(alist) for v in alist: b2.add(v) for v in alist: b2.remove(v) self.assertEqual(b1,b2)
class Test_Bag(unittest.TestCase): def setUp(self): self.alist = ['d','a','b','d','c','b','d'] self.bag = Bag(self.alist) def test_len(self): self.assertEqual(len(self.alist),7) len_size = 7 random.shuffle(self.alist) for i in self.alist: self.bag.remove(i) len_size -= 1 self.assertEqual(len(self.bag), len_size) self.assertEqual(len(self.bag), 0) def test_unique(self): self.assertEqual(len(self.bag.counts),4) random.shuffle(self.alist) for i in self.alist: self.bag.remove(i) self.assertEqual(len(self.bag.counts),self.bag.unique()) def test_contains(self): for x in ['a','b','c','d']: self.assertIn(x,self.alist) self.assertNotIn('x',self.alist) def test_count(self): self.assertTrue(self.bag.counts['a'] == 1) self.assertTrue(self.bag.counts['b'] == 2) self.assertTrue(self.bag.counts['c'] == 1) self.assertTrue(self.bag.counts['d'] == 3) self.assertTrue(self.bag.counts['x'] == 0) random.shuffle(self.alist) s_counts = sum(self.bag.counts.values()) for i in self.alist: self.bag.remove(i) s_counts -= 1 self.assertEqual(len(self.bag),s_counts) def test_eq(self): temp_list = [random.randint(1,10) for i in range(1,1001)] b1 = Bag(temp_list) random.shuffle(temp_list) b2 = Bag(temp_list) self.assertEqual(b1,b2) b2.remove(temp_list[0]) self.assertNotEqual(b1,b2) def test_add(self): temp_list = [random.randint(1,10) for i in range(1,1001)] b1 = Bag(temp_list) b2 = Bag() random.shuffle(temp_list) for i in temp_list: b2.add(i) self.assertEqual(b1,b2) def test_remove(self): temp_list = [] for i in range(1,1001): temp_list.append(random.randint(1,10)) b1 = Bag(temp_list) self.assertRaises(ValueError,self.bag.remove,33) b2 = Bag(temp_list) for i in temp_list: b2.add(i) b2.remove(i) self.assertEqual(b1,b2)
class Test_Bag(unittest.TestCase): def setUp(self): self.alist = ['d','a','b','d','c','b','d'] self.bag = Bag(self.alist) def test_len(self): self.assertEqual(len(self.bag), 7) for i in range(len(self.bag)): self.bag.remove(self.alist[0]) self.alist.pop(0) random.shuffle(self.alist) self.assertEqual(len(self.bag), 7-(i+1)) def test_unique(self): self.assertEqual(self.bag.unique(), 4) for i in range (len(self.bag)): self.bag.remove(self.alist[0]) self.alist.pop(0) random.shuffle(self.alist) self.assertEqual(self.bag.unique(), len(set(self.alist))) def test_contains(self): self.assertIn('a', self.bag) self.assertIn('b', self.bag) self.assertIn('c', self.bag) self.assertIn('d', self.bag) self.assertNotIn('x', self.bag) def test_count(self): self.assertEqual(self.bag.count('a'),1) self.assertEqual(self.bag.count('b'),2) self.assertEqual(self.bag.count('c'),1) self.assertEqual(self.bag.count('d'),3) self.assertEqual(self.bag.count('x'),0) for i in range(len(self.bag)): self.bag.remove(self.alist[0]) self.alist.pop(0) random.shuffle(self.alist) sum_num = 0 for v in self.bag.counts.values(): sum_num+=v self.assertEqual(sum_num, 7-(i+1)) def test_eq(self): bag2 = [] for i in range(0,1000): bag2.append((random.randint(1,10))) check_bag = Bag(bag2) random.shuffle(bag2) check_bag2 = Bag(bag2) self.assertEqual(check_bag,check_bag2) check_bag.remove(bag2[0]) self.assertNotEqual(check_bag,check_bag2) def test_add(self): bag2 = [] for i in range(0,1000): bag2.append((random.randint(1,10))) check_bag = Bag(bag2) check_bag2 = Bag() random.shuffle(bag2) for i in check_bag: check_bag2.add(i) self.assertEqual(check_bag,check_bag2) def test_remove(self): bag2 = [] for i in range(0,1000): bag2.append((random.randint(1,10))) check_bag = Bag(bag2) self.assertRaises(ValueError, check_bag.remove,21) check_bag2 = Bag(bag2) for i in bag2: check_bag2.add(i) for i in bag2: check_bag2.remove(i) self.assertEqual(check_bag,check_bag2)
class Test_Bag(unittest.TestCase): def setUp(self): self.alist = ['d','a','b','d','c','b','d'] self.bag = Bag(self.alist) def test_len(self): print('Checking for length:',self.alist) self.assertEqual(len(self.bag),7, 'Initial length of the self.bag is not 7') bag_length = 7 for l in self.alist: self.bag.remove(l) bag_length -= 1 self.assertEqual(len(self.bag),bag_length, 'length of bag object is {lenbag} but it is supposed to be {good}'.format(lenbag = str(len(self.bag)), good = str(bag_length))) def test_unique(self): print('Checking for length:',self.alist) self.assertEqual(self.bag.unique(), 4, 'Intial unique values in self.bag is not 4') alist = self.alist for i in range(len(self.alist)): self.bag.remove(self.alist[i]) alist = self.alist[i+1:] self.assertEqual(self.bag.unique(),len(set(alist)), 'Number of unique value in bag is {uniqueb} but should be {setlength}'.format(uniqueb = str(self.bag.unique()) ,setlength = str(len(set(alist))))) def test_contains(self): print('Checking for contains:',self.alist) for el in ['a', 'b', 'c','d']: self.assertTrue(el in self.bag, '{el} is not in self.bag') self.assertFalse('x' in self.bag, 'x is in self.bag') def test_count(self): print('Checking for counts:',self.alist) for num, el in [(1,'a'), (2, 'b'), (1, 'c'), (3, 'd'), (0, 'x')]: self.assertEqual(num, self.bag.count(el), 'Initially self.bag contains {num}{el} but it contains {numb}'.format(num = str(num), el = str(el), numb = str(self.bag.count(el)))) init_sum = 7 for l in self.alist: self.bag.remove(l) init_sum -= 1 bag_sum = sum([self.bag.count(i) for i in ['a','b','c','d']]) self.assertEqual(init_sum, bag_sum , 'sum of the counts of abcd should {init_sum} but it is {bag_sum}'.format(init_sum = init_sum, bag_sum = bag_sum)) def test_equal(self): print('Checking for equal') alist = [random.randint(1,10) for i in range(1000)] bag1 = Bag(alist) random.shuffle(alist) bag2 = Bag(alist) self.assertEqual(bag1, bag2, 'Two back must be equal initially') bag2.remove(alist[0]) self.assertNotEqual(bag1, bag2, 'Two back must not be equal after removing the first element of bag2') def test_add(self): print('Checking for add') bag1 = Bag([random.randint(1,10) for i in range(1000)]) bag2 = Bag() for el in iter(bag1): bag2.add(el) self.assertEqual(bag1,bag2, 'bag1 and bag 2 must be equal after adding all terms') def test_remove(self): print('Checking for remove') alist = [random.randint(1,10) for i in range(1000)] bag1 = Bag(alist) self.assertRaises(ValueError, Bag.remove,bag1,32) bag2 = Bag(alist) for el in alist: bag2.add(el) for el in alist: bag2.remove(el) self.assertEqual(bag1,bag2, 'Two bag must be same after removing elements from bag2')
class Test_Bag(unittest.TestCase): def setUp(self): self.test_list = ['d','a','b','d','c','b','d'] self.bag = Bag(['d','a','b','d','c','b','d']) def testLen(self): self.assertEqual(len(self.bag), 7) removal = ['d','a','b','d','c','b','d'] random.shuffle(removal) for r in range(len(removal)): self.bag.remove(removal[r]) self.assertEqual(len(self.bag), 7-(r+1)) def testUnique(self): self.setUp() self.assertEqual(self.bag.unique(),4) for i in self.test_list: self.bag.remove(i) self.test_list.remove(i) self.assertEqual(self.bag.unique(),len(set(self.test_list))) def testContains(self): self.setUp() self.assertTrue('a' in self.bag) self.assertTrue('b' in self.bag) self.assertTrue('c' in self.bag) self.assertTrue('d' in self.bag) self.assertFalse('x' in self.bag) def testCount(self): self.setUp() self.assertEqual(self.bag.count('a'),1) self.assertEqual(self.bag.count('b'),2) self.assertEqual(self.bag.count('c'),1) self.assertEqual(self.bag.count('d'),3) self.assertEqual(self.bag.count('x'),0) random.shuffle(self.test_list) for i in range(len(self.test_list)): self.bag.remove(self.test_list[i]) sum_of_count = self.bag.count('a')+self.bag.count('b')+self.bag.count('c')+self.bag.count('d') self.assertEqual(sum_of_count, 7-(i+1)) def testEqual(self): test_list = [random.randint(1,10) for i in range(1000)] test_bag1 = Bag(test_list) random.shuffle(test_list) test_bag2 = Bag(test_list) self.assertTrue(test_bag1==test_bag2) test_bag2.remove(test_list[0]) self.assertFalse(test_bag1==test_bag2) def testAdd(self): test_list = [random.randint(1,10) for i in range(1000)] test_bag1 = Bag(test_list) test_bag2 = Bag() random.shuffle(test_list) for i in test_list: test_bag2.add(i) self.assertTrue(test_bag1==test_bag2) def testRemove(self): test_list = [random.randint(1,10) for i in range(1000)] test_bag1 = Bag(test_list) self.assertRaises(ValueError,test_bag1.remove, 21) test_bag2 = Bag(test_list) for i in test_list: test_bag2.add(i) for i in test_list: test_bag2.remove(i) self.assertEqual(test_bag1,test_bag2)