Пример #1
0
def test_list_append(xs: List[int], ys: List[int]):
    expected = xs + ys
    fx = frozenlist.of_seq(xs)
    fy = frozenlist.of_seq(ys)
    fz = fx.append(fy)
    fh = fx + fy

    assert list(fz) == list(fh) == expected
Пример #2
0
def test_list_skip(xs: List[int], x: int):
    ys: FrozenList[int]
    try:
        ys = frozenlist.of_seq(xs).skip(x)
        assert list(ys) == xs[x:]
    except ValueError:
        assert x > len(xs)
Пример #3
0
def test_list_slice(xs: PyList[int], x: int, y: int):
    expected = xs[x:y]

    ys: FrozenList[int]
    ys = frozenlist.of_seq(xs)[x:y]

    assert pylist(ys) == expected
Пример #4
0
def test_list_take(xs: PyList[int], x: int):
    ys: FrozenList[int]
    try:
        ys = frozenlist.of_seq(xs).take(x)
        assert pylist(ys) == xs[:x]
    except ValueError:
        assert x > len(xs)
Пример #5
0
def test_list_sort_with(xs: List[str]):
    expected = sorted(xs, key=lambda x: x[1])
    ys: FrozenList[str] = frozenlist.of_seq(xs)
    func: Callable[[str], str] = lambda x: x[1]
    result = pipe(ys, frozenlist.sort_with(func))

    assert list(result) == list(expected)
Пример #6
0
def test_list_monad_law_associativity_iterable(xs: List[int]):
    # (m >>= f) >>= g is just like doing m >>= (\x -> f x >>= g)
    f: Callable[[int], FrozenList[int]] = lambda x: rtn(x + 10)
    g: Callable[[int], FrozenList[int]] = lambda y: rtn(y * 42)

    m = frozenlist.of_seq(xs)
    assert m.collect(f).collect(g) == m.collect(lambda x: f(x).collect(g))
Пример #7
0
def test_list_slice(xs: List[int], x: int, y: int):
    expected = xs[x:y]

    ys: FrozenList[int] = frozenlist.of_seq(xs)
    zs = ys[x:y]

    assert list(zs) == expected
Пример #8
0
def test_list_indexed(xs: List[int]):

    expected = list(enumerate(xs))

    ys: FrozenList[int] = frozenlist.of_seq(xs)
    zs = frozenlist.indexed(ys)

    assert list(zs) == expected
Пример #9
0
def test_list_filter(xs: List[int], limit: int):
    expected = filter(lambda x: x < limit, xs)

    ys: FrozenList[int] = frozenlist.of_seq(xs)
    predicate: Callable[[int], bool] = lambda x: x < limit
    result = pipe(ys, frozenlist.filter(predicate))

    assert list(result) == list(expected)
Пример #10
0
def test_seq_pipe_map3(xs: List[Tuple[int, int, int]]):
    mapper: Callable[[int, int, int], int] = lambda x, y, z: x + y + z
    ys = pipe(
        frozenlist.of_seq(xs),
        frozenlist.map3(mapper),
    )

    assert isinstance(ys, FrozenList)
    assert [y for y in ys] == [x + y + z for (x, y, z) in xs]
Пример #11
0
def test_list_pipe_mapi(xs: List[int]):
    def mapper(i: int, x: int):
        return x + i

    ys = frozenlist.of_seq(xs)
    zs = ys.pipe(frozenlist.mapi(mapper))

    assert isinstance(zs, FrozenList)
    assert [z for z in zs] == [x + i for i, x in enumerate(xs)]
Пример #12
0
def test_list_pipe_map(xs: List[int]):
    def mapper(x: int):
        return x + 1

    ys = frozenlist.of_seq(xs)
    zs = ys.pipe(frozenlist.map(mapper))

    assert isinstance(zs, FrozenList)
    assert [y for y in zs] == [mapper(x) for x in xs]
Пример #13
0
def test_seq_pipe_starmap(xs: List[Tuple[int, int]]):
    mapper: Callable[[int, int], int] = lambda x, y: x + y
    ys = pipe(
        frozenlist.of_seq(xs),
        frozenlist.starmap(mapper),
    )

    assert isinstance(ys, FrozenList)
    assert [y for y in ys] == [x + y for (x, y) in xs]
Пример #14
0
def test_list_fold(xs: List[int]):
    def folder(x: int, y: int) -> int:
        return x + y

    expected: int = functools.reduce(folder, xs, 0)

    ys: FrozenList[int] = frozenlist.of_seq(xs)
    result = pipe(ys, frozenlist.fold(folder, 0))

    assert result == expected
Пример #15
0
def test_list_index(xs: List[int], x: int):

    x = x % len(xs) if x > 0 else x
    expected = xs[x]

    ys: FrozenList[int] = frozenlist.of_seq(xs)
    y = ys[x]

    item: Callable[[FrozenList[int]], int] = frozenlist.item(x)
    h = ys.pipe(item)

    i = ys.item(x)

    assert y == h == i == expected
Пример #16
0
def test_list_large_list(x: int):
    xs = frozenlist.of_seq(range(x))
    assert len(xs) == x
Пример #17
0
def test_list_item(xs: List[int], index: int):
    ys = frozenlist.of_seq(xs)
    while index and index >= len(xs):
        index //= 2
    assert xs[index] == ys[index]
Пример #18
0
def test_list_length(xs: List[int]):
    ys = frozenlist.of_seq(xs)
    assert len(xs) == len(ys)
Пример #19
0
def test_list_sort(xs: List[int]):
    expected = sorted(xs)
    ys: FrozenList[int] = frozenlist.of_seq(xs)
    result = pipe(ys, frozenlist.sort())

    assert list(result) == list(expected)
Пример #20
0
def test_list_take_last(xs: List[int], x: int):
    expected = xs[-x:]
    ys: FrozenList[int]
    ys = frozenlist.of_seq(xs).take_last(x)
    assert list(ys) == expected
Пример #21
0
def test_list_skip_last(xs: List[int], x: int):
    expected = xs[:-x]
    ys: FrozenList[int]
    ys = frozenlist.of_seq(xs).skip_last(x)
    assert list(ys) == expected