aboutsummaryrefslogtreecommitdiff
path: root/tests/programs
diff options
context:
space:
mode:
Diffstat (limited to 'tests/programs')
-rw-r--r--tests/programs/borrows_clause.zc11
-rw-r--r--tests/programs/forward_borrow.zc16
2 files changed, 27 insertions, 0 deletions
diff --git a/tests/programs/borrows_clause.zc b/tests/programs/borrows_clause.zc
new file mode 100644
index 0000000..6f43581
--- /dev/null
+++ b/tests/programs/borrows_clause.zc
@@ -0,0 +1,11 @@
+namespace;
+include rt;
+// a borrows clause on a prototype and its definition: freeing the other argument keeps the result.
+char* first(char* a, char* b) borrows a;
+fn main() {
+ char* t = rt.new(4); t[0] = 'x'; t[1] = 0;
+ char* u = rt.new(4); u[0] = 'y'; u[1] = 0;
+ let r = first(t, u); rt.free(u); rt.put_c(r[0]); rt.free(t);
+ return 0;
+}
+char* first(char* a, char* b) borrows a { return a; }
diff --git a/tests/programs/forward_borrow.zc b/tests/programs/forward_borrow.zc
new file mode 100644
index 0000000..7cc44b1
--- /dev/null
+++ b/tests/programs/forward_borrow.zc
@@ -0,0 +1,16 @@
+namespace;
+include rt;
+// forward calls whose results do not outlive the owner, or do not borrow it, are accepted.
+fn main() {
+ char* t = rt.new(8); t[0] = ' '; t[1] = 'a'; t[2] = 0;
+ let n = count(t); rt.free(t); rt.put_n(n);
+ let u = rt.new(8); u[0] = 'b'; u[1] = 0;
+ let r = skip(u); rt.put_c(r[0]); rt.free(u);
+ let v = rt.new(8); v[0] = 0;
+ let q = skip(v); rt.free(v); q = 0; rt.put_n(q);
+ let w = rt.new(8); w[0] = 0;
+ let k = skip(w)[0]; rt.free(w); rt.put_n(k);
+ return 0;
+}
+int count(char* s) { int i = 0; while (s[i]) i++; return i; }
+char* skip(char* s) { int i = 0; while (s[i] == ' ') i++; return s + i; }