def test_returns_int(self): """ Test list_odd to make sure it returns an int. """ return_type = type(count_odd(1)) self.assertEqual( return_type, int, "count_odd should return type " + "int, but instead returned type {}.".format(return_type)) return_type = type(count_odd([1])) self.assertEqual( return_type, int, "count_odd should return type " + "int, but instead returned type {}.".format(return_type))
def test_count_odd(self, list_values, list_indices): """ Test the submitted count_odd against a randomly generated list. """ # Gather all of the odd values expected = 0 for n in list_values: if n % 2 == 1: expected += 1 # Create up to 2 levels of nesting for the values in list_values, where # the indices we turn into sublists are contained in # nesting_indices_1 and nesting_indices_2 nesting_indices_1 = list_indices[:len(list_indices) // 2] nesting_indices_2 = list_indices[len(list_indices) // 2:] nesting_indices_1.sort() nesting_indices_2.sort() obj = create_nested_list(list_values, [nesting_indices_1, nesting_indices_2]) # Ignoring the order of the values, in case students have an # out-of-order version for some reason. actual = count_odd(obj) self.assertEqual(actual, expected, ("Using count_odd on {} returned" + " {} instead of {}.").format(obj, actual, expected))
def test_one_nested_sublist(self): """ Test count_odd on a list with only one sublist in it. """ self.assertEqual( count_odd([[3]]), 1, "count_odd should return " + "the number of odd integers in nested sublists.")
def test_odd_integer(self): """ Test count_odd on an odd integer. """ self.assertEqual( count_odd(3), 1, "count_odd should return 1 " + "when given an odd integer.")
def test_even_integer(self): """ Test count_odd on an even integer. """ self.assertEqual( count_odd(2), 0, "count_odd should return 0 " + "when passed an even integer.")
def test_one_nested_sublist_and_integers(self): """ Test count_odd on a list with at most one level of nested sublists in it mixed with integers. """ self.assertEqual( count_odd([[3], 4, [5]]), 2, "count_odd should return the number of odd " + "integers in nested sublists and in the list passed " + "in.")