示例#1
0
def _test_RangeQueryStatic_common(func, gen_expected):

    array = OneDimensionalArray(int, [])
    raises(ValueError, lambda: RangeQueryStatic(array, func))

    array = OneDimensionalArray(int, [1])
    rq = RangeQueryStatic(array, func)
    assert rq.query(0, 0) == 1
    raises(ValueError, lambda: rq.query(0, -1))
    raises(IndexError, lambda: rq.query(0, 1))

    array_sizes = [3, 6, 12, 24, 48, 96]
    random.seed(0)
    for array_size in array_sizes:
        data = random.sample(range(-2 * array_size, 2 * array_size),
                             array_size)
        array = OneDimensionalArray(int, data)

        expected = []
        inputs = []
        for i in range(array_size):
            for j in range(i + 1, array_size):
                inputs.append((i, j))
                expected.append(gen_expected(data, i, j))

        data_structures = ["array", "sparse_table"]
        for ds in data_structures:
            rmq = RangeQueryStatic(array, func, data_structure=ds)
            for input, correct in zip(inputs, expected):
                assert rmq.query(input[0], input[1]) == correct
示例#2
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
示例#3
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
def test_tim_sort():

    arr = OneDimensionalArray(int, [-2, 7, 15, -14, 0, 15, 0])
    expected_arr = [-14, -2, 0, 0, 7, 15, 15]
    return timsort(arr, 0, len(arr) - 1) == expected_arr