Skip to content

Commit

Permalink
added tests for uncons and split_when
Browse files Browse the repository at this point in the history
  • Loading branch information
WitoldFracek committed Nov 27, 2024
1 parent e729191 commit ee720a8
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 14 deletions.
12 changes: 4 additions & 8 deletions src/qwlist/qwlist.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from typing import TypeVar, Generic, Iterable, Callable, overload, Optional, Iterator, Type, Tuple, List
from collections import deque
from unittest.mock import right

T = TypeVar('T')
K = TypeVar('K')
Expand Down Expand Up @@ -720,13 +719,10 @@ def split_when(self, pred: Callable[[T], bool]) -> Optional[Tuple["QList[T]", "L
if the element is the split point.
Returns:
A tuple where:
- The first element is a fully evaluated `QList` containing all elements up to and
including the split point.
- The second element is a lazily evaluated sequence of all elements after the split point.
- Returns `None` if `self` is empty.
- If no element satisfies the predicate, the left part contains all elements from `self` and
the right part is an empty lazy sequence.
A tuple where the first element is a fully evaluated `QList` containing all elements up to and
including the split point, and the second element is a lazily evaluated sequence of all
elements after the split point. Returns `None` if `self` is empty. If no element satisfies the
predicate, the left part contains all elements from `self` and the right part is an empty lazy sequence.
"""
left = QList()
it = self.iter()
Expand Down
62 changes: 56 additions & 6 deletions tests/test_lazy.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import sys;
import sys; sys.path.append('../src')
from src.qwlist.qwlist import QList, Lazy

from src.qwlist.eager import EagerQList

sys.path.append('../src')
from src.qwlist.qwlist import QList, Lazy
import pytest
def naturals():
current = 0
while True:
yield current
current += 1


def test_lazy_from_iterable_constructor():
Expand Down Expand Up @@ -524,4 +526,52 @@ def test_get():
assert Lazy([]).get(10, default=100) == 100
assert Lazy(range(1, 11)).get(0, default=100) == 1
assert Lazy(range(1, 11)).get(9, default=100) == 10
assert Lazy(range(1, 11)).get(10, default=100) == 100
assert Lazy(range(1, 11)).get(10, default=100) == 100


def test_uncons():
assert Lazy([]).uncons() is None

expected = (0, QList([1, 2, 3]))
head, tail = Lazy(range(4)).uncons()
res = (head, tail.collect())
assert res == expected

expected = (0, QList())
head, tail = Lazy([0]).uncons()
res = (head, tail.collect())
assert res == expected

assert Lazy(range(4)).filter(lambda x: x > 5).uncons() is None

nat = Lazy(naturals())
head, tail = nat.uncons()
assert head == 0
head, tail = tail.uncons()
assert head == 1
head, tail = tail.uncons()
assert head == 2


def test_split_when():
assert Lazy([]).split_when(lambda x: True) is None

expected = (QList([0]), QList([1, 2, 3]))
left, right = Lazy(range(4)).split_when(lambda x: True)
res = (left, right.collect())
assert res == expected

expected = (QList([0, 1, 2]), QList([3, 4, 5]))
left, right = Lazy(range(6)).split_when(lambda x: x == 2)
res = (left, right.collect())
assert res == expected

expected = (QList([0, 1, 2]), QList())
left, right = Lazy(range(3)).split_when(lambda x: x == 2)
res = (left, right.collect())
assert res == expected

left, right = Lazy(naturals()).split_when(lambda x: x == 2)
assert left == QList([0, 1, 2])
left, right = right.split_when(lambda x: x == 5)
assert left == QList([3, 4, 5])

0 comments on commit ee720a8

Please sign in to comment.