def test_two_element_list(self): first = "first" second = "second" source = [first, second] output = randomlist.random_list(source) self.assertEquals(2, len(output), "Number of elements changed.") self.assertEquals(set(source), set(output), "Elements changed.") self.assertNotEquals(output[0], output[1]) order_same = output == source order_reversed = output == [second, first] self.assert_(order_same or order_reversed, "The order is weird.")
#!/usr/bin/python import randomlist def selection_sort(list1): nitems = len(list1) for index1 in range(nitems - 1): small = index1 for index2 in range(index1 + 1, nitems): if (list1[index2] < list1[small]): small = index2 if (index1 != small): list1[index1], list1[small] = list1[small], list1[index1] return #list1=[5,2,1,6,3,4,0] list1 = randomlist.random_list() print "befor sort:", list1 selection_sort(list1) print "after sort:", list1
def test_single_element(self): source = ["foo"] output = randomlist.random_list(source) self.assertEquals(source, output, "Didn't return the same element as a list")