Esempio n. 1
0
def _test_common_sort(sort, *args, **kwargs):
    random.seed(1000)

    n = random.randint(10, 20)
    arr = DynamicOneDimensionalArray(int, 0)
    for _ in range(n):
        arr.append(random.randint(1, 1000))
    for _ in range(n // 3):
        arr.delete(random.randint(0, n // 2))
    expected_arr = [
        686, 779, 102, 134, 362, 448, 480, 548, None, None, None, 228, 688,
        247, 373, 696, None, None, None, None, None, None, None, None, None,
        None, None, None, None, None, None
    ]
    sort(arr, *args, **kwargs, start=2, end=10)
    assert arr._data == expected_arr
    sort(arr, *args, **kwargs)
    expected_arr = [
        102, 134, 228, 247, 362, 373, 448, 480, 548, 686, 688, 696, 779, None,
        None, None, None, None, None, None, None, None, None, None, None, None,
        None, None, None, None, None
    ]
    assert arr._data == expected_arr
    assert (arr._last_pos_filled, arr._num, arr._size) == (12, 13, 31)

    n = random.randint(10, 20)
    arr = OneDimensionalArray(int, n)
    for i in range(n):
        arr[i] = random.randint(1, 1000)
    expected_arr = [42, 695, 147, 500, 768, 998, 473, 732, 728, 426, 709, 910]
    sort(arr, *args, **kwargs, start=2, end=5)
    assert arr._data == expected_arr
Esempio n. 2
0
def test_merge_sort_parallel():

    random.seed(1000)

    n = random.randint(10, 20)
    arr = DynamicOneDimensionalArray(int, 0)
    for _ in range(n):
        arr.append(random.randint(1, 1000))
    for _ in range(n // 3):
        arr.delete(random.randint(0, n // 2))
    expected_arr = [
        686, 779, 102, 134, 362, 448, 480, 548, 228, 688, 247, 373, 696, None,
        None, None, None, None, None, None, None, None, None, None, None, None,
        None
    ]
    merge_sort_parallel(arr, 5, start=2, end=10)
    assert arr._data == expected_arr

    n = random.randint(10, 20)
    arr = OneDimensionalArray(int, n)
    for i in range(n):
        arr[i] = random.randint(1, 1000)
    expected_arr = [42, 695, 147, 500, 768, 998, 473, 732, 728, 426, 709, 910]
    merge_sort_parallel(arr, 5, start=2, end=5)
    assert arr._data == expected_arr
Esempio n. 3
0
    def _test_append(size):
        data = [random.randint(-size, size) for _ in range(size)]
        doda_cpp = DynamicOneDimensionalArray(int, 0, backend=Backend.CPP)
        doda_python = DynamicOneDimensionalArray(int, 0)
        python_list = []

        cpp_backend, python_backend, list_time = (0, 0, 0)
        for datum in data:
            timer_cpp = timeit.Timer(functools.partial(doda_cpp.append, datum))
            cpp_backend += min(timer_cpp.repeat(repeat, number))

            timer_python = timeit.Timer(functools.partial(doda_python.append, datum))
            python_backend += min(timer_python.repeat(repeat, number))

            timer_list = timeit.Timer(functools.partial(python_list.append, datum))
            list_time += min(timer_list.repeat(repeat, number))

        assert cpp_backend < python_backend
        assert cpp_backend/list_time < 1.5
Esempio n. 4
0
    def _test_delete(size):
        data = [random.random() * 2 * size for _ in range(size)]
        doda_cpp = DynamicOneDimensionalArray(float, data, backend=Backend.CPP)
        doda_python = DynamicOneDimensionalArray(float, data)
        python_list = [datum for datum in data]
        list_indices = [i for i in range(size)]
        random.seed(0)
        random.shuffle(list_indices)

        def _list_remove(obj, index):
            del obj[index]

        list_time = 0
        for i in range(size):
            idx = list_indices[i]

            timer_list = timeit.Timer(functools.partial(_list_remove, python_list, idx))
            list_time += min(timer_list.repeat(1, 1))

            for j in range(i + 1, size):
                if list_indices[j] > idx:
                    list_indices[j] -= 1

        cpp_backend, python_backend = (0, 0)
        while doda_cpp._num > 0:
            indices = [i for i in range(doda_cpp._last_pos_filled + 1)]
            random.shuffle(indices)
            for idx in indices:
                timer_cpp = timeit.Timer(functools.partial(doda_cpp.delete, idx))
                cpp_backend += min(timer_cpp.repeat(1, 1))

                timer_python = timeit.Timer(functools.partial(doda_python.delete, idx))
                python_backend += min(timer_python.repeat(1, 1))

                if doda_cpp._num == 0:
                    break

        assert cpp_backend < python_backend
        assert cpp_backend < list_time
Esempio n. 5
0
def test_counting_sort():
    random.seed(1000)

    n = random.randint(10, 20)
    arr = DynamicOneDimensionalArray(int, 0)
    for _ in range(n):
        arr.append(random.randint(1, 1000))
    for _ in range(n//3):
        arr.delete(random.randint(0, n//2))

    expected_arr = [102, 134, 228, 247, 362, 373, 448,
                    480, 548, 686, 688, 696, 779]
    assert counting_sort(arr)._data == expected_arr