Skip to content

Commit

Permalink
test: capture some variables in some tests, fix warnings, add a TODO
Browse files Browse the repository at this point in the history
add a TODO about returning closures
  • Loading branch information
alehander92 committed Aug 2, 2023
1 parent c0b2567 commit 45a3a52
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 23 deletions.
7 changes: 4 additions & 3 deletions crates/nargo_cli/tests/test_data/closures_mut_ref/src/main.nr
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use dep::std;

fn main(mut x: Field) {

let one = 1;
let add1 = |z| {
*z = *z + 1;
*z = *z + one;
};

let two = 2;
let add2 = |z| {
*z = *z + 2;
*z = *z + two;
};

add1(&mut x);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ fn fib_fn(a: Field, b: Field, res: &mut Field) {
fn main(mut prev: Field, mut cur: Field) {

let mut fib = prev + cur;
for i in 1..10 {
for _ in 1..10 {
prev = cur;
cur = fib;
fib_fn(prev, cur, &mut fib);
assert(prev + cur == fib);
}

}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use dep::std;

fn f(x: &mut Field) -> Field {
*x = *x - 1;
1
Expand All @@ -13,8 +15,6 @@ fn h(x: &mut Field) -> Field {
1
}

use dep::std;

fn selector(flag:&mut bool) -> fn(&mut Field) -> Field { //TODO: Can we have fn(&mut Field) -> () return type?
let mut my_func = f;

Expand All @@ -36,13 +36,13 @@ fn main() {

let mut x: Field = 100;
let returned_func = selector(&mut flag);
let status = returned_func(&mut x);
let _status = returned_func(&mut x);

assert(x == 200);

let mut y: Field = 100;
let returned_func2 = selector(&mut flag);
let status2 = returned_func2(&mut y);
let _status2 = returned_func2(&mut y);

assert(y == 300);

Expand Down
8 changes: 5 additions & 3 deletions crates/nargo_cli/tests/test_data/inner_outer_cl/src/main.nr
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
fn main() {
let z1 = 0;
let z2 = 1;
let cl_outer = |x| {
let cl_inner = |y| {
x + y
x + y + z2
};
cl_inner(1)
cl_inner(1) + z1
};
let result = cl_outer(1);
assert(result == 2);
assert(result == 3);
}
32 changes: 21 additions & 11 deletions crates/nargo_cli/tests/test_data/ret_fn_ret_cl/src/main.nr
Original file line number Diff line number Diff line change
@@ -1,29 +1,39 @@
use dep::std;

fn f(x: Field) -> Field {
x
x + 1
}

fn ret_fn() -> fn(Field) -> Field {
let y = 1;
let inner_closure = |z| -> Field{
z + y
};
std::println(inner_closure(1));
f
}

fn ret_closure() -> fn(Field) -> Field {
// TODO: in the advanced implicitly generic function with closures branch
// which would support higher-order functions in a better way
// support returning closures:
//
// fn ret_closure() -> fn(Field) -> Field {
// let y = 1;
// let inner_closure = |z| -> Field{
// z + y
// };
// inner_closure
// }

fn ret_lambda() -> fn(Field) -> Field {
let cl = |z: Field| -> Field {
z
z + 1
};
cl
}

fn main(x : Field) {
let result_fn = ret_fn();
assert(result_fn(x) == x); // Works
assert(result_fn(x) == x + 1);

// let result_closure = ret_closure();
// assert(result_closure(x) == x + 1);

let result_cl = ret_closure();
assert(result_cl(x) == x);
let result_lambda = ret_lambda();
assert(result_lambda(x) == x + 1);
}

0 comments on commit 45a3a52

Please sign in to comment.