• R/O
  • SSH

Commit

Tags
Aucun tag

Frequently used words (click to add to your profile)

javac++androidlinuxc#objective-cqt誰得cocoawindowspythonphprubygameguibathyscaphec翻訳omegat計画中(planning stage)frameworktwittertestdombtronvb.netdirectxarduinopreviewerゲームエンジン

C preprocessor written in Python


Commit MetaInfo

Révision962505cdb449dfb3a1da780d9e075848c2e7a97e (tree)
l'heure2022-02-01 15:45:43
AuteurEric Hopper <hopper@omni...>
CommiterEric Hopper

Message de Log

Add test of LazySequence and fix errors found by test.

Some of these errors include bad type hint syntax.

Change Summary

Modification

diff -r 54a3ee518ecc -r 962505cdb449 src/pycpre/lazy_sequence.py
--- a/src/pycpre/lazy_sequence.py Mon Jan 31 22:44:20 2022 -0800
+++ b/src/pycpre/lazy_sequence.py Mon Jan 31 22:45:43 2022 -0800
@@ -83,13 +83,17 @@
8383
8484 def __getitem__(self, index: Union[int, slice]) \
8585 -> Union[T, "LazySequence[T]", List[T]]:
86- if index < 0:
87- raise ValueError("Lazy sequences may not have an end and don't "
88- "support indexing from the end.")
8986 if isinstance(index, int):
90- return self.state.ensure_index(self.index + index)
87+ if index < 0:
88+ raise ValueError("Lazy sequences may not have an end and don't "
89+ "support indexing from the end.")
90+ try:
91+ return self.state.ensure_index(self.index + index)
92+ except StopIteration:
93+ raise IndexError(f"LazySequence index {index} out of range")
9194 else:
92- if index.start < 0 or index.stop < 0:
95+ if (index.start is not None and index.start < 0) or \
96+ (index.stop is not None and index.stop < 0):
9397 raise ValueError("Lazy sequences may not have an end and don't "
9498 "support indexing from the end.")
9599 if index.step is not None and index.stop is None:
@@ -105,7 +109,10 @@
105109 start += self.index
106110 stop += self.index
107111 assert self.state.index_of_0 <= start
108- self.state.ensure_index(self.index + stop)
112+ try:
113+ self.state.ensure_index(self.index + stop)
114+ except StopIteration:
115+ pass
109116 start -= self.state.index_of_0
110117 stop -= self.state.index_of_0
111118 if index.step is None:
diff -r 54a3ee518ecc -r 962505cdb449 tests/test_lazy_sequence.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test_lazy_sequence.py Mon Jan 31 22:45:43 2022 -0800
@@ -0,0 +1,49 @@
1+import pytest
2+
3+from src.pycpre.lazy_sequence import LazySequence
4+import gc
5+
6+
7+def test_simple_generator():
8+ def squares(n: int):
9+ for i in range(1, n + 1):
10+ yield i * i
11+
12+ ls = LazySequence(squares(5))
13+ assert ls.index == 0
14+ assert len(ls.state.buffer) == 0
15+ assert len(ls.state.heads) == 1
16+ assert ls.state.index_of_0 == 0
17+
18+ assert ls[1] == 4
19+
20+ assert ls.index == 0
21+ assert len(ls.state.buffer) == 2
22+ assert len(ls.state.heads) == 1
23+ assert ls.state.index_of_0 == 0
24+
25+ tmp = ls[2:]
26+
27+ assert ls.index == 0
28+ assert tmp.index == 2
29+ assert len(ls.state.heads) == 2
30+ assert len(ls.state.buffer) == 2
31+ assert ls.state.index_of_0 == 0
32+
33+ ls = tmp
34+ del tmp
35+ gc.collect()
36+
37+ assert ls.index == 2
38+ assert len(ls.state.heads) == 1
39+ assert len(ls.state.buffer) == 0
40+ assert ls.state.index_of_0 == 2
41+
42+ assert ls[0] == 9
43+ assert ls[1] == 16
44+ assert ls[2] == 25
45+ with pytest.raises(IndexError):
46+ assert ls[3] == 36
47+
48+ assert ls[:10] == [9, 16, 25]
49+ assert ls[1:2] == [16]