def test_bubblesort(): # test some known list x = [1,2,4,0,1] assert np.array_equal(algs.bubblesort(x), np.array([0, 1, 1, 2, 4])) # check on zero- and one-length input x = [] assert np.array_equal(algs.bubblesort(x), x) x = [1] assert np.array_equal(algs.bubblesort(x), x) # check on even- and odd-length input x = np.random.rand(10) assert np.array_equal(algs.bubblesort(x), sorted(x)) x = np.random.rand(9) assert np.array_equal(algs.bubblesort(x), sorted(x)) # check on repeats x = np.random.randint(0, 3, 10) assert np.array_equal(algs.bubblesort(x), sorted(x)) x = [1] * 10 assert np.array_equal(algs.bubblesort(x), x) # check on only strings. sort by ascii order, as in canonical sort. x = ["a", "B", "A", "b"] assert np.array_equal(algs.bubblesort(x), sorted(x)) # check that mixed strings throws a TypeError. try: x = ["1", 1] algs.bubblesort(x) except TypeError: pass else: assert False
def test_bubblesort(): # Actually test bubblesort here. It might be useful to think about # some edge cases for your code, where it might fail. Some things to # think about: (1) does your code handle 0-element arrays without # failing, (2) does your code handle characters? B = np.array([3, 5, 4, 2, 6, 7, 1, 8]) assert np.array_equal(np.array(algs.bubblesort(B)), np.array([1, 2, 3, 4, 5, 6, 7, 8])) empty = np.array([]) assert np.array_equal(np.array(algs.bubblesort(empty)), np.array([])) duplication = np.array([4, 4, 5, 5, 7, 7, 1, 1]) assert np.array_equal(np.array(algs.bubblesort(duplication)), np.array([1, 1, 4, 4, 5, 5, 7, 7])) #list with odd number of elements odd = np.array([1, 3, 2, 4, 9]) assert np.array_equal(np.array(algs.bubblesort(odd)), np.array([1, 2, 3, 4, 9])) #list with characters a = np.array(['apple', 'banana', 'zebra', 'red']) assert np.array_equal(np.array(algs.bubblesort(a)), np.array(['apple', 'banana', 'red', 'zebra']))
def test_bubblesort(): x = [] sorted, assign, cond = algs.bubblesort(x) assert sorted == [] x = [1] sorted, assign, cond = algs.bubblesort(x) assert sorted == [1] x = [2, 5, 1, 0, 2] sorted, assign, cond = algs.bubblesort(x) assert sorted == [0, 1, 2, 2, 5]
def test_bubblesort(): # Actually test bubblesort here. It might be useful to think about # some edge cases for your code, where it might fail. Some things to # think about: (1) does your code handle 0-element arrays without # failing, (2) does your code handle characters? x = np.array([1, 2, 4, 0, 1]) # for now, just attempt to call the bubblesort function, should # actually check output algs.bubblesort(x)
def test_bubblesort(): # Actually test bubblesort here. It might be useful to think about # some edge cases for your code, where it might fail. Some things to # think about: (1) does your code handle 0-element arrays without # failing, (2) does your code handle characters? w, x, y, z = np.array([1, 2, 4, 0, 1]), np.array([]), np.array( [0]), np.array([2, 1, 0, -1, -2]) assert np.array_equal(algs.bubblesort(w)[0], sorted(w)) assert np.array_equal(algs.bubblesort(x)[0], sorted(x)) assert np.array_equal(algs.bubblesort(y)[0], sorted(y)) assert np.array_equal(algs.bubblesort(z)[0], sorted(z))
def test_bubblesort(): # Actually test bubblesort here. It might be useful to think about # some edge cases for your code, where it might fail. Some things to # think about: (1) does your code handle 0-element arrays without # failing, (2) does your code handle characters? x = np.array([1,2,4,0,1]) empty = [] single = [11] dup = [3, 12, 7, 7, -6, 9] odd = np.random.rand(11) even = np.random.rand(12) # for now, just attempt to call the bubblesort function, should # actually check output #algs.bubblesort(x) # test that bubblesort is sorting array x correctly assert np.array_equal(algs.bubblesort(x), [0,1,1,2,4]) # testing additional edge cases; empty, single element, duplicated # element, odd and even length vectors assert np.array_equal(algs.bubblesort(empty), []) assert np.array_equal(algs.bubblesort(single), [11]) assert np.array_equal(algs.bubblesort(dup), [-6, 3, 7, 7, 9, 12]) algs.bubblesort(odd) assert odd[0] < odd[10] algs.bubblesort(even) assert even[0] < even[11]
def test_bubblesort(): # Negatives, even (sortedL, con, asg) = algs.bubblesort(np.array([-2, 2, -51, 0])) assert (np.array_equal(sortedL, np.array([-51, -2, 0, 2]))) # Odd (sortedL, con, asg) = algs.bubblesort(np.array([-2, 2, -51])) assert (np.array_equal(sortedL, np.array([-51, -2, 2]))) # Duplicates (sortedL, con, asg) = algs.bubblesort(np.array([1, 1, 1])) assert (np.array_equal(sortedL, np.array([1, 1, 1]))) # Empty (sortedL, con, asg) = algs.bubblesort(np.array([])) assert (np.array_equal(sortedL, np.array([]))) # Characters (sortedL, con, asg) = algs.bubblesort(np.array(['a', 'd', 'b', 'c', 'a'])) assert (np.array_equal(sortedL, np.array(['a', 'a', 'b', 'c', 'd']))) # Strings (sortedL, con, asg) = algs.bubblesort(np.array(['cca', 'aac', 'aab', 'aca', 'aaa'])) assert (np.array_equal(sortedL, np.array(['aaa', 'aab', 'aac', 'aca', 'cca']))) # Sorted (sortedL, con, asg) = algs.bubblesort(np.array([1, 2, 3])) assert (np.array_equal(sortedL, np.array([1, 2, 3]))) # Reverse Sorted (sortedL, con, asg) = algs.bubblesort(np.array([3, 2, 1])) assert (np.array_equal(sortedL, np.array([1, 2, 3])))
def test_bubblesort(): # Actually test bubblesort here. It might be useful to think about # some edge cases for your code, where it might fail. Some things to # think about: (1) does your code handle 0-element arrays without # failing, (2) does your code handle characters? x = [9, 8, 7, 6, 5, 4, 3, 2, 1] #regular list x1 = [] #empty list x2 = ["b", "c", "a", "d", "z", "a", "c", "h"] #will it sort characters? x3 = [5, 7, 1, 5, 0, 2, 5, 7, 5, 5, 5, 8657] #how does it handle duplicates? # for now, just attempt to call the bubblesort function, should # actually check output assert algs.bubblesort(x) == [1, 2, 3, 4, 5, 6, 7, 8, 9] assert algs.bubblesort(x1) == [] assert algs.bubblesort(x2) == ['a', 'a', 'b', 'c', 'c', 'd', 'h', 'z'] assert algs.bubblesort(x3) == [0, 1, 2, 5, 5, 5, 5, 5, 5, 7, 7, 8657]
def test_bubblesort(): # Actually test bubblesort here. It might be useful to think about # some edge cases for your code, where it might fail. Some things to # think about: (1) does your code handle 0-element arrays without # failing, (2) does your code handle characters? l = num_arrays() for x in l: algs.bubblesort(x) assert all(x[i] <= x[i + 1] for i in range(0, len(x) - 1)) m = alph_arrays() algs.bubblesort(m) assert all(m[i] <= m[i + 1] for i in range(0, len(m) - 1)) b = bad_arrays() with pytest.raises(ValueError): for i in b: algs.quicksort(i)
def test_bubblesort(): # Actually test bubblesort here. It might be useful to think about # some edge cases for your code, where it might fail. Some things to # think about: (1) does your code handle 0-element arrays without # failing, (2) does your code handle characters? A = [1, 2, 4, 0, 1] #odd length B = [] #empty vector C = [2] #single element vector D = [23, 23, 42, 1, 32, 2] # Duplicated elements E = [1, 2, 4, 0] #even length # for now, just attempt to call the bubblesort function, should # actually check output assert algs.bubblesort(A)[0] == sorted(A) assert algs.bubblesort(B)[0] == sorted(B) assert algs.bubblesort(C)[0] == sorted(C) assert algs.bubblesort(D)[0] == sorted(D) assert algs.bubblesort(E)[0] == sorted(E)
def test_bubblesort(): print('\n############Bubblesort############') # # multi test for inputs x = [np.array([]), np.array([2]), np.array(['a']), np.array([2,2,2,2]), np.array([2,2,'a',2]), np.array([2,2,6,4,5]), np.array([2.4,2.1,6.7,4.7,5.0,10.1])] # # # single large test # x = [np.random.randint(0,10000,10000)] # multi large test # x = [np.random.rand(100,i) for i in [(i + 1) * 100 for i in list(range(10))]] # multi smallish test # x = [np.random.rand(i) for i in [(i + 1) * 10 for i in list(range(50))]] #test accounts = [] for i in x: if i.ndim == 1: ans = algs.bubblesort(i) accounts.append((len(i),ans)) elif i.ndim == 2: for j in i: ans = algs.bubblesort(j) accounts.append((len(j),ans)) else: None # speed testing # for i in [(i + 1) * 100 for i in list(range(10))]: # a = np.random.rand(100,i) # algs.bubblesort(a) return accounts
def test_bubblesort(): # Actually test bubblesort here. It might be useful to think about # some edge cases for your code, where it might fail. Some things to # think about: (1) does your code handle 0-element arrays without # failing, (2) does your code handle characters? x = np.array([1, 2, 4, 0, 1]) s, conds, assigns = algs.bubblesort(x) assert np.array_equal(s, np.array([0, 1, 1, 2, 4])) ### Test negative elements xneg = np.array([-1, 0, 5, 2, -10]) s, conds, assigns = algs.bubblesort(xneg) assert np.array_equal(s, np.array([-10, -1, 0, 2, 5])) ### test duplicated elements xdup = np.array([1, 1, 0, 5, 10]) s, conds, assigns = algs.bubblesort(xdup) assert np.array_equal(s, np.array([0, 1, 1, 5, 10])) xeven = np.array([0, 1, 5, 0, 2, -1]) s, conds, assigns = algs.bubblesort(xeven) assert np.array_equal(s, np.array([-1, 0, 0, 1, 2, 5])) ### Test empty length vector xempty = np.array([]) s, conds, assigns = algs.bubblesort(xempty) assert np.array_equal(s, np.array([])) ### test character arrays xchar = np.array(["c", "a", "d", "b", "a"]) s, conds, assigns = algs.bubblesort(xchar) assert np.array_equal(s, np.array(["a", "a", "b", "c", "d"])) ### test word arrays xwords = np.array(["apple", "aardvark", "banana", "accentuate", "down"]) s, conds, assigns = algs.bubblesort(xwords) assert np.array_equal( s, np.array(["aardvark", "accentuate", "apple", "banana", "down"]))
def test_bubblesort(): # Actually test bubblesort here. It might be useful to think about # some edge cases for your code, where it might fail. Some things to # think about: (1) does your code handle 0-element arrays without # failing, (2) does your code handle characters? # test odd length x = np.array([1,2,4,0,1]) assert np.array_equal(algs.bubblesort(x)['sorted'],np.array([0,1,1,2,4])) # test even length x = np.array([1,2,4,0]) assert np.array_equal(algs.bubblesort(x)['sorted'], np.array([0,1,2,4])) # test empty vector x = np.array([]) assert np.array_equal(algs.bubblesort(x)['sorted'],np.array([])) # test single element vector x = np.array([7]) assert np.array_equal(algs.bubblesort(x)['sorted'],np.array([7])) # test duplicated elements x = np.array([1,1,1,1]) assert np.array_equal(algs.bubblesort(x)['sorted'],np.array([1,1,1,1])) # test characters x = np.array(['b','d','a','c']) assert np.array_equal(algs.bubblesort(x)['sorted'],np.array(['a','b','c','d']))
def test_bubblesort(): # Actually test bubblesort here. It might be useful to think about # some edge cases for your code, where it might fail. Some things to # think about: (1) does your code handle 0-element arrays without # failing, (2) does your code handle characters? # for now, just attempt to call the bubblesort function, should # actually check output algs.bubblesort(odd) algs.bubblesort(even) algs.bubblesort(test2) algs.bubblesort(blank) algs.bubblesort(single) algs.bubblesort(duplicate) algs.bubblesort(duplicate2)
def test_bubblesort(): #Empty vector x = [] #check if algs.only_integers(x) == x: assert np.array_equal(algs.bubblesort(x), sorted(x)) else: print(algs.only_integers(x)) #One entry x = [1] #check if algs.only_integers(x) == x: assert np.array_equal(algs.bubblesort(x), sorted(x)) else: print(algs.only_integers(x)) #Character entry x = ["a", 1, 2] #check if algs.only_integers(x) == x: assert np.array_equal(algs.bubblesort(x), sorted(x)) else: print(algs.only_integers(x)) #Non-integer entry x = [0.5, 1, 2] #check if algs.only_integers(x) == x: assert np.array_equal(algs.bubblesort(x), sorted(x)) else: print(algs.only_integers(x)) #Repeating entry (case 1) x = [6, 1, 3, 6] #check if algs.only_integers(x) == x: assert np.array_equal(algs.bubblesort(x), sorted(x)) else: print(algs.only_integers(x)) #Repeating entry (case 2) x = [8, 9, 8, 9] #check if algs.only_integers(x) == x: assert np.array_equal(algs.bubblesort(x), sorted(x)) else: print(algs.only_integers(x)) #Odd length entry x = [7, 4, 5] #check if algs.only_integers(x) == x: assert np.array_equal(algs.bubblesort(x), sorted(x)) else: print(algs.only_integers(x)) #Even length entry x = [7, 4, 5, 1] #check if algs.only_integers(x) == x: assert np.array_equal(algs.bubblesort(x), sorted(x)) else: print(algs.only_integers(x))
def test_bubblesort(): # generate random vector of length 100 x = np.random.rand(100) algs.bubblesort(x)