summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorZuhaitz Méndez Fernández de Aránguiz <zuhaitz@debian>2026-01-31 01:15:25 +0000
committerZuhaitz Méndez Fernández de Aránguiz <zuhaitz@debian>2026-01-31 01:15:25 +0000
commit856c9fe56b412779e045ef86a767b93d5c7f563b (patch)
tree6f851bb9bf300970c4a0d7186db0de86e510a18d /tests
parent03a6a57f500ee4230ce2cee887866b3850ed7ed9 (diff)
Improvements for slice + better iteration for arrays
Diffstat (limited to 'tests')
-rw-r--r--tests/std/test_direct_array_iteration.zc37
-rw-r--r--tests/std/test_slice_iteration.zc29
2 files changed, 66 insertions, 0 deletions
diff --git a/tests/std/test_direct_array_iteration.zc b/tests/std/test_direct_array_iteration.zc
new file mode 100644
index 0000000..359951f
--- /dev/null
+++ b/tests/std/test_direct_array_iteration.zc
@@ -0,0 +1,37 @@
+import "std/slice.zc"
+
+test "direct array iteration" {
+ let arr: int[5] = [1, 2, 3, 4, 5];
+
+ let sum = 0;
+ for val in arr {
+ sum = sum + val;
+ }
+
+ assert(sum == 15, "Sum should be 1+2+3+4+5 = 15");
+}
+
+test "direct array iteration with different types" {
+ let floats: float[3] = [1.5, 2.5, 3.0];
+ let count = 0;
+
+ for f in floats {
+ count = count + 1;
+ }
+
+ assert(count == 3, "Should iterate over all 3 elements");
+}
+
+// TODO: Nested array iteration needs special handling
+// test "nested array iteration" {
+// let matrix: int[2][3] = [[1, 2, 3], [4, 5, 6]];
+// let total = 0;
+//
+// for row in matrix {
+// for val in row {
+// total = total + val;
+// }
+// }
+//
+// assert(total == 21, "Sum should be 1+2+3+4+5+6 = 21");
+// }
diff --git a/tests/std/test_slice_iteration.zc b/tests/std/test_slice_iteration.zc
new file mode 100644
index 0000000..b7eddf4
--- /dev/null
+++ b/tests/std/test_slice_iteration.zc
@@ -0,0 +1,29 @@
+import "std/slice.zc"
+import "std/io.zc"
+
+test "slice from array iteration" {
+ let ints: int[5] = [1, 2, 3, 4, 5];
+ let slice = Slice<int>::from_array((int*)(&ints), 5);
+
+ // Test iteration
+ let sum = 0;
+ for val in slice {
+ sum = sum + val;
+ }
+
+ if (sum != 15) {
+ panic("Slice iteration failed: expected sum 15");
+ }
+}
+
+test "slice methods" {
+ let arr: int[3] = [10, 20, 30];
+ let slice = Slice<int>::from_array((int*)(&arr), 3);
+
+ if (slice.length() != 3) panic("Slice length wrong");
+ if (slice.is_empty()) panic("Slice should not be empty");
+
+ let opt = slice.get(1);
+ if (opt.is_none()) panic("Slice get failed");
+ if (opt.unwrap() != 20) panic("Slice get returned wrong value");
+}