Example #1
0
def test_quicksort():

    A = np.array([3, 5, 4, 2, 6, 7, 1, 8])
    assert np.array_equal(np.array(algs.quicksort(A)),
                          np.array([1, 2, 3, 4, 5, 6, 7, 8]))

    #Empty array
    empty = np.array([])
    assert np.array_equal(np.array(algs.quicksort(empty)), np.array([]))

    #Duplication array
    duplication = np.array([4, 4, 5, 5, 7, 7, 1, 1])
    assert np.array_equal(np.array(algs.quicksort(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.quicksort(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.quicksort(a)),
                          np.array(['apple', 'banana', 'red', 'zebra']))
Example #2
0
def test_quicksort():
    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.quicksort(w, 0, len(w) - 1, 0, 0)[0], sorted(w))
    assert np.array_equal(algs.quicksort(x, 0, len(x) - 1, 0, 0)[0], sorted(x))
    assert np.array_equal(algs.quicksort(y, 0, len(y) - 1, 0, 0)[0], sorted(y))
    assert np.array_equal(algs.quicksort(z, 0, len(z) - 1, 0, 0)[0], sorted(z))
Example #3
0
def test_quicksort():
    # test some known list
    x = [1,2,4,0,1]
    assert np.array_equal(algs.quicksort(x), np.array([0, 1, 1, 2, 4]))
    # check on zero- and one-length input
    x = []
    assert np.array_equal(algs.quicksort(x), x)
    x = [1]
    assert np.array_equal(algs.quicksort(x), x)
    # check on even- and odd-length input
    x = np.random.rand(10)
    assert np.array_equal(algs.quicksort(x), sorted(x))
    x = np.random.rand(9)
    assert np.array_equal(algs.quicksort(x), sorted(x))
    # check on repeats
    x = np.random.randint(0, 3, 10)
    assert np.array_equal(algs.quicksort(x), sorted(x))
    x = [1] * 10
    assert np.array_equal(algs.quicksort(x), x)
    # check on only strings. sort by ascii order, as in canonical sort.
    x = ["a", "B", "A", "b"]
    assert np.array_equal(algs.quicksort(x), sorted(x))
    # check that mixed strings throws a TypeError.
    try:
        x = ["1", 1]
        algs.quicksort(x)
    except TypeError:
        pass
    else:
        assert False
Example #4
0
def test_quicksort():
    x = []
    sorted, assign, cond = algs.quicksort(x, 0, len(x) - 1, 0, 0)
    assert sorted == []
    x = [1]
    sorted, assign, cond = algs.quicksort(x, 0, len(x) - 1, 0, 0)
    assert sorted == [1]
    x = [2, 5, 1, 0, 2]
    sorted, assign, cond = algs.quicksort(x, 0, len(x) - 1, 0, 0)
    assert sorted == [0, 1, 2, 2, 5]
Example #5
0
def test_quicksort():

    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 quicksort function, should
    # actually check output
    assert algs.quicksort(x) == [1, 2, 3, 4, 5, 6, 7, 8, 9]
    assert algs.quicksort(x1) == []
    assert algs.quicksort(x2) == ['a', 'a', 'b', 'c', 'c', 'd', 'h', 'z']
    assert algs.quicksort(x3) == [0, 1, 2, 5, 5, 5, 5, 5, 5, 7, 7, 8657]
Example #6
0
def test_quicksort():

    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 quicksort function, should
    # actually check output
    assert algs.quicksort(A, 0, len(A) - 1, 0, 0)[0] == sorted(A)
    assert algs.quicksort(B, 0, len(B) - 1, 0, 0)[0] == sorted(B)
    assert algs.quicksort(C, 0, len(C) - 1, 0, 0)[0] == sorted(C)
    assert algs.quicksort(D, 0, len(D) - 1, 0, 0)[0] == sorted(D)
    assert algs.quicksort(E, 0, len(E) - 1, 0, 0)[0] == sorted(E)
Example #7
0
def test_quicksort():

    # Negatives, even
    (sortedL, con, asg) = algs.quicksort(np.array([-2, 2, -51, 0]))
    assert (np.array_equal(sortedL, np.array([-51, -2, 0, 2])))

    # Odd
    (sortedL, con, asg) = algs.quicksort(np.array([-2, 2, -51]))
    assert (np.array_equal(sortedL, np.array([-51, -2, 2])))

    # Duplicates
    (sortedL, con, asg) = algs.quicksort(np.array([1, 1, 1]))
    assert (np.array_equal(sortedL, np.array([1, 1, 1])))

    # Empty
    (sortedL, con, asg) = algs.quicksort(np.array([]))
    assert (np.array_equal(sortedL, np.array([])))

    # Characters
    (sortedL, con, asg) = algs.quicksort(np.array(['a', 'd', 'b', 'c', 'a']))
    assert (np.array_equal(sortedL, np.array(['a', 'a', 'b', 'c', 'd'])))

    # Sorted
    (sortedL, con, asg) = algs.quicksort(np.array([1, 2, 3]))
    assert (np.array_equal(sortedL, np.array([1, 2, 3])))

    # Reverse Sorted
    (sortedL, con, asg) = algs.quicksort(np.array([3, 2, 1]))
    assert (np.array_equal(sortedL, np.array([1, 2, 3])))
Example #8
0
def test_quicksort():
    TestOdd = np.array([1, 2, 4, 0, 1])
    Test_Empty = np.array([])
    Test_Ordered = np.array([1, 2, 3, 4, 5, 6, 7, 8])
    Test_Reversed = np.array([8, 7, 6, 5, 4, 3, 2, 1])
    Test_Char = np.array(['cat', 'Dog', 'elephant', 'Snake', 'bird'])

    # Empty
    assert np.array_equal(algs.quicksort(Test_Empty), np.array([]))

    # Odd
    assert np.array_equal(algs.quicksort(TestOdd), np.array([0, 1, 1, 2, 4]))

    # Ordered array
    assert np.array_equal(algs.quicksort(Test_Ordered),
                          np.array([1, 2, 3, 4, 5, 6, 7, 8]))

    # Reversed array
    assert np.array_equal(algs.quicksort(Test_Reversed),
                          np.array([1, 2, 3, 4, 5, 6, 7, 8]))

    # Character
    assert np.array_equal(
        algs.quicksort(Test_Char),
        np.array(['Dog', 'Snake', 'bird', 'cat', 'elephant']))

    # Run function on range of length
    X = np.arange(100, 1100, 100)
    for i in X:
        Array = np.random.rand(i)
        algs.quicksort(Array)[0]
Example #9
0
def test_quicksort():    
    # test odd length
    x = np.array([1,2,4,0,3])
    assert np.array_equal(algs.quicksort(x)['sorted'],np.array([0,1,2,3,4]))
    # test even length
    x = np.array([1,2,4,0])
    assert np.array_equal(algs.quicksort(x)['sorted'], np.array([0,1,2,4]))
    # test empty vector
    x = np.array([])
    assert np.array_equal(algs.quicksort(x)['sorted'],np.array([]))
    # test single element vector
    x = np.array([7])
    assert np.array_equal(algs.quicksort(x)['sorted'],np.array([7]))
    # test duplicated elements
    x = np.array([1,1,1,1])
    #assert np.array_equal(algs.quicksort(x)['sorted'],np.array([1,1,1,1]))
    # test characters
    x = np.array(['b','d','a','c'])
    assert np.array_equal(algs.quicksort(x)['sorted'],np.array(['a','b','c','d']))
Example #10
0
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)
Example #11
0
def test_quicksort():
    print('\n############Quicksort############')
# # 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.quicksort(i)
            accounts.append((len(i),ans))
        elif i.ndim == 2:
            for j in i:
                ans = algs.quicksort(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
Example #12
0
def test_quicksort():

    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 quicksort function, should
    # actually check output
    #algs.quicksort(x)

    # testing the quicksort is sorting array x correctly
    assert np.array_equal(algs.quicksort(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.quicksort(x), [0,1,1,2,4])
    assert np.array_equal(algs.quicksort(empty), [])
    assert np.array_equal(algs.quicksort(single), [11])
    assert np.array_equal(algs.quicksort(dup), [-6, 3, 7, 7, 9, 12])
    algs.quicksort(odd)
    assert odd[0] < odd[10]
    algs.quicksort(even)
    assert even[0] < even[11]
Example #13
0
def test_quicksort():
    l = num_arrays()
    for x in l:
        algs.quicksort(x)
        print(x)
        assert all(x[i] <= x[i + 1] for i in range(0, len(x) - 1))

    m = alph_arrays()
    with pytest.raises(ValueError):
        algs.quicksort(m)

    b = bad_arrays()
    with pytest.raises(ValueError):
        for i in b:
            algs.quicksort(i)
Example #14
0
def test_quicksort():

    x = np.array([1, 2, 4, 0, 1])
    s, conds, assigns = algs.quicksort(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.quicksort(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.quicksort(xdup)
    assert np.array_equal(s, np.array([0, 1, 1, 5, 10]))

    ### test even length vectors
    xeven = np.array([0, 1, 5, 0, 2, -1])
    s, conds, assigns = algs.quicksort(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.quicksort(xempty)
    assert np.array_equal(s, np.array([]))

    ### test character arrays
    xchar = np.array(["c", "a", "d", "b", "a"])
    s, conds, assigns = algs.quicksort(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.quicksort(xwords)
    assert np.array_equal(
        s, np.array(["aardvark", "accentuate", "apple", "banana", "down"]))
Example #15
0
def test_quicksort():
    #Empty vector
    x = []
    #check
    if algs.only_integers(x) == x:
        assert np.array_equal(algs.quicksort(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.quicksort(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.quicksort(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.quicksort(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.quicksort(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.quicksort(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.quicksort(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.quicksort(x), sorted(x))
    else:
        print(algs.only_integers(x))
Example #16
0
def test_quicksort():
    # generate random vector of length 100
    x = np.random.rand(100)
    algs.quicksort(x, 0, int(len(x) - 1))
Example #17
0
def test_quicksort():
    # for now, just attempt to call the quicksort function, should
    # actually check output
    algs.quicksort(odd)
    algs.quicksort(even)
    algs.quicksort(test2)
    algs.quicksort(blank)
    algs.quicksort(single)
    algs.quicksort(duplicate)
    algs.quicksort(duplicate2)
def test_quicksort():

    x = np.array([1, 2, 4, 0, 1])
    # for now, just attempt to call the quicksort function, should
    # actually check output
    algs.quicksort(x)