diff options
| author | Zuhaitz Méndez Fernández de Aránguiz <zuhaitz@debian> | 2026-01-19 12:53:47 +0000 |
|---|---|---|
| committer | Zuhaitz Méndez Fernández de Aránguiz <zuhaitz@debian> | 2026-01-19 12:53:47 +0000 |
| commit | 639c6ac65a1bd44b2ba0725fe7016a4920bf0950 (patch) | |
| tree | 47703f960633d3d4580022583134c28b96d5f36e /tests/features/test_iterator.zc | |
| parent | 526b7748cafcb5a00f8e30df88661f6059d79843 (diff) | |
Iterables and iterators :D
Diffstat (limited to 'tests/features/test_iterator.zc')
| -rw-r--r-- | tests/features/test_iterator.zc | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/tests/features/test_iterator.zc b/tests/features/test_iterator.zc new file mode 100644 index 0000000..1340a00 --- /dev/null +++ b/tests/features/test_iterator.zc @@ -0,0 +1,45 @@ +import "../../std/iter.zc" +import "../../std/option.zc" + +struct RangeIter { + current: int; + stop: int; +} + +impl RangeIter { + fn next(self) -> Option<int> { + if (self.current < self.stop) { + var v = self.current; + self.current += 1; + return Option<int>::Some(v); + } + return Option<int>::None(); + } +} + +struct MyRange { + start: int; + end: int; +} + +impl MyRange { + fn iterator(self) -> RangeIter { + return RangeIter { + current: self.start, + stop: self.end + }; + } +} + +test "iterator_desugaring" { + var range = MyRange { start: 0, end: 5 }; + var sum = 0; + + // This loop should be desugared by the compiler + for i in range { + println "Got: {i}"; // Verification print + sum += i; + } + + assert(sum == 10); // 0+1+2+3+4 = 10 +} |
