summaryrefslogtreecommitdiff
path: root/tests/features/test_iterator_drop.zc
diff options
context:
space:
mode:
authorZuhaitz Méndez Fernández de Aránguiz <zuhaitz@debian>2026-01-19 12:53:47 +0000
committerZuhaitz Méndez Fernández de Aránguiz <zuhaitz@debian>2026-01-19 12:53:47 +0000
commit639c6ac65a1bd44b2ba0725fe7016a4920bf0950 (patch)
tree47703f960633d3d4580022583134c28b96d5f36e /tests/features/test_iterator_drop.zc
parent526b7748cafcb5a00f8e30df88661f6059d79843 (diff)
Iterables and iterators :D
Diffstat (limited to 'tests/features/test_iterator_drop.zc')
-rw-r--r--tests/features/test_iterator_drop.zc51
1 files changed, 51 insertions, 0 deletions
diff --git a/tests/features/test_iterator_drop.zc b/tests/features/test_iterator_drop.zc
new file mode 100644
index 0000000..43e22a7
--- /dev/null
+++ b/tests/features/test_iterator_drop.zc
@@ -0,0 +1,51 @@
+import "../../std/iter.zc"
+import "../../std/mem.zc"
+
+var DROP_CALLED = 0;
+
+struct DropIter {
+ count: int;
+}
+
+impl DropIter {
+ fn next(self) -> Option<int> {
+ if self.count > 0 {
+ self.count -= 1;
+ return Option<int>::Some(self.count);
+ }
+ return Option<int>::None();
+ }
+}
+
+impl Drop for DropIter {
+ fn drop(self) {
+ DROP_CALLED = 1;
+ println "Iterator dropped";
+ }
+}
+
+struct DropRange {
+ len: int;
+}
+
+impl DropRange {
+ fn iterator(self) -> DropIter {
+ return DropIter { count: self.len };
+ }
+}
+
+test "iterator_drop" {
+ {
+ var range = DropRange { len: 3 };
+ for i in range {
+ // Loop runs 3 times
+ var _ignore = i;
+ }
+ // Iterator should be dropped here
+ }
+
+ if (DROP_CALLED != 1) {
+ println "Error: Iterator was not dropped! (DROP_CALLED={DROP_CALLED})";
+ exit(1);
+ }
+}