def test_count(self): self.assertEquals(10, s.count("qwertyuiop")) self.assertEquals(0, s.count([])) self.assertEquals(0, s.count(())) self.assertEquals(0, s.count({})) self.assertEquals(1, s.count({"foo": "bar"})) self.assertEquals(10, s.count(s.take(10, s.range())))
def test_interleave(self): self.assertEqual( [1, 2, 3, 1, 2, 3, 1], list(s.take(7, s.interleave(s.repeat(1), s.repeat(2), s.repeat(3))))) self.assertEquals([0, 500, 1, 501], list(s.interleave(range(2), range(500, 1000)))) self.assertEquals([500, 0, 501, 1], list(s.interleave(range(500, 1000), range(2))))
def test_repeatedly(self): els = [] def add_el(): els.append(42) s.dorun(s.take(3, s.repeatedly(add_el))) self.assertEquals([42, 42, 42], els) els = [] s.dorun(s.repeatedly(add_el, 2)) self.assertEquals([42, 42], els)
def test_take(self): self.assertEquals([], list(s.take(40, []))) self.assertEquals([], list(s.take(0, [1, 2, 3]))) self.assertEquals([1, 2], list(s.take(2, [1, 2, 3]))) self.assertEquals([1, 2, 3], list(s.take(3, [1, 2, 3]))) self.assertEquals([1, 2, 3], list(s.take(4, [1, 2, 3]))) els = [] def _gen(): for x in range(10): els.append(x) yield x s.dorun(s.take(0, _gen())) self.assertEquals([], els) self.assertEquals(els, list(s.take(5, _gen())))
def test_cycle(self): self.assertEqual([1, 2, 3, 1, 2, 3, 1, 2], list(s.take(8, s.cycle([1, 2, 3]))))
def test_range(self): self.assertEquals([], list(s.range(2, 1))) self.assertEquals([2], list(s.range(2, 1, -1))) self.assertEquals([0, 1, 2, 3], list(s.range(4))) self.assertEquals([0, 1, 2, 3], list(s.take(4, s.range())))
def test_repeat(self): self.assertEquals([2, 2, 2], list(s.take(3, s.repeat(2)))) self.assertEquals([2, 2, 2], list(s.repeat(2, 3))) self.assertEquals([], list(s.repeat(2, 0))) self.assertEquals([], list(s.repeat(2, -1)))
def test_take_nth(self): ls = [42, 25, 17, 1, 34, 87] self.assertEquals([1, 1, 1], list(s.take(3, s.take_nth(0, [1, 2])))) self.assertEquals([1, 1, 1], list(s.take(3, s.take_nth(-1, [1, 2])))) self.assertEquals(ls, list(s.take_nth(1, ls))) self.assertEquals(ls, list(s.take_nth(2, s.interleave(ls, range(20)))))
def hr(): print(40 * "-") print() def print_sequence(sequence): for item in sequence: print(item) hr() sequence = range() item = nth(sequence, 10) print(item) # pozor na rozdilne chovani! item = nth(sequence, 10) print(item) item = nth(sequence, 10) print(item) sequence = range() s1 = take(10, sequence) s2 = take(10, sequence) s3 = take(10, sequence) print(list(s1)) print(list(s2)) print(list(s3))