summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorZuhaitz Méndez Fernández de Aránguiz <zuhaitz@debian>2026-01-20 11:16:11 +0000
committerZuhaitz Méndez Fernández de Aránguiz <zuhaitz@debian>2026-01-20 11:16:11 +0000
commitdb690b368f7e05b242f2e775f620f35ab0df5bc3 (patch)
treec70d8e82bf81515e48b7bd2701595d6e62ce93e3 /tests
parentf027a812707d68ca0690b7544175b9f302dd57ad (diff)
Smart derives...
Diffstat (limited to 'tests')
-rw-r--r--tests/features/test_copy_trait.zc12
-rw-r--r--tests/features/test_move_semantics.zc41
-rw-r--r--tests/features/test_smart_derive.zc26
-rw-r--r--tests/misc/test_edge_cases.zc6
4 files changed, 49 insertions, 36 deletions
diff --git a/tests/features/test_copy_trait.zc b/tests/features/test_copy_trait.zc
index 120dc5d..994ccee 100644
--- a/tests/features/test_copy_trait.zc
+++ b/tests/features/test_copy_trait.zc
@@ -12,7 +12,7 @@ struct Mover {
val: int;
}
-fn test_copy_trait() {
+test "copy_trait" {
var p1 = Point { x: 10, y: 20 };
var p2 = p1; // Copy, not move
@@ -24,18 +24,14 @@ fn test_copy_trait() {
p2.x = 30;
assert(p1.x == 10, "p1 changed after p2 modification");
assert(p2.x == 30, "p2 modification failed");
+
+ println "Copy Trait Works!";
}
-fn test_move_default() {
+test "move_default" {
var m1 = Mover { val: 1 };
var m2 = m1; // Moved
// Uncommenting this should cause compile error
// var m3 = m1;
}
-
-fn main() {
- test_copy_trait();
- test_move_default();
- "Copy Trait Works!";
-}
diff --git a/tests/features/test_move_semantics.zc b/tests/features/test_move_semantics.zc
index 70390c2..bf0d717 100644
--- a/tests/features/test_move_semantics.zc
+++ b/tests/features/test_move_semantics.zc
@@ -7,7 +7,7 @@ struct Mover {
val: int;
}
-fn test_basic_move() {
+test "basic_move" {
var p1 = Mover { val: 10 };
var p2 = p1; // p1 moved to p2
@@ -18,14 +18,14 @@ fn test_basic_move() {
// var p3 = p1;
}
-fn test_primitive_copy() {
+test "primitive_copy" {
var i = 10;
var j = i; // Copy
var k = i; // Copy again - should be valid
assert(k == 10, "Primitive copy failed");
}
-fn test_reassignment() {
+test "reassignment" {
var m1 = Mover { val: 1 };
var m2 = m1; // m1 moved
@@ -34,31 +34,22 @@ fn test_reassignment() {
assert(m3.val == 2, "Resurrection failed");
}
-fn test_func_arg(m: Mover) {
+fn consume(m: Mover) {
assert(m.val == 10, "Func arg failed");
}
-fn main() {
- test_basic_move();
- test_primitive_copy();
- test_reassignment();
-
+test "func_arg" {
var m = Mover { val: 10 };
- test_func_arg(m); // m moved
-
- // ** Negative Tests (Uncomment to verify) **
-
- // 1. Use after move (Assignment)
- // test_basic_move(); // See line 18 inside function
+ consume(m); // m moved
- // 2. Use after move (Call)
- // test_func_arg(m); // Should fail: Use of moved value 'm'
-
- // 3. Use after return
- /*
- fn fail_return(m: Mover) -> Mover {
- var m2 = m;
- return m; // Should fail: Use of moved value 'm'
- }
- */
+ // 2. Use after move (Call - Negative Test)
+ // consume(m); // Should fail: Use of moved value 'm'
+}
+
+/*
+// 3. Use after return (Negative Test)
+fn fail_return(m: Mover) -> Mover {
+ var m2 = m;
+ return m; // Should fail: Use of moved value 'm'
}
+*/
diff --git a/tests/features/test_smart_derive.zc b/tests/features/test_smart_derive.zc
new file mode 100644
index 0000000..705a12f
--- /dev/null
+++ b/tests/features/test_smart_derive.zc
@@ -0,0 +1,26 @@
+
+@derive(Eq)
+struct Container {
+ val: int;
+}
+
+// Ensure derived eq uses pointers by trying to use 'c2' after comparison
+test "eq_moves" {
+ var c1 = Container { val: 10 };
+ var c2 = Container { val: 10 };
+
+ // This should call Container__eq(&c1, &c2)
+ // If it passed by value, c2 would be moved here
+ if c1 == c2 {
+ // c2 must still be valid
+ assert(c2.val == 10, "c2 moved during equality check!");
+ } else {
+ assert(false, "c1 != c2");
+ }
+
+ // Explicitly verify c2 is still valid
+ c2.val = 20;
+ assert(c2.val == 20, "c2 invalid");
+
+ println "Smart Derive Eq Works!";
+}
diff --git a/tests/misc/test_edge_cases.zc b/tests/misc/test_edge_cases.zc
index f1a4cf5..0d50744 100644
--- a/tests/misc/test_edge_cases.zc
+++ b/tests/misc/test_edge_cases.zc
@@ -9,7 +9,7 @@ fn test_empty_struct() {
var e1 = Empty {};
var e2 = Empty {};
- if (!e1.eq(e2)) {
+ if (!e1.eq(&e2)) {
println "FAIL: Empty struct eq failed";
exit(1);
}
@@ -35,12 +35,12 @@ fn test_many_fields() {
var m2 = ManyFields { a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8 };
var m3 = ManyFields { a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 9 }; // h differs
- if (!m1.eq(m2)) {
+ if (!m1.eq(&m2)) {
println "FAIL: equal structs not detected as equal";
exit(1);
}
- if (m1.eq(m3)) {
+ if (m1.eq(&m3)) {
println "FAIL: different structs detected as equal";
exit(1);
}