Skip to content

Commit

Permalink
chore: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench committed Feb 27, 2024
1 parent d809292 commit a1a4286
Show file tree
Hide file tree
Showing 18 changed files with 100 additions and 66 deletions.
38 changes: 22 additions & 16 deletions test_programs/compile_success_empty/field_comparisons/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ fn check_plo_phi() {
}

fn check_decompose_unsafe() {
assert_eq(decompose_unsafe(TWO_POW_128), (0, 1));
assert_eq(decompose_unsafe(TWO_POW_128 + 0x1234567890), (0x1234567890, 1));
assert_eq(decompose_unsafe(0x1234567890), (0x1234567890, 0));
unsafe {
assert_eq(decompose_unsafe(TWO_POW_128), (0, 1));
assert_eq(decompose_unsafe(TWO_POW_128 + 0x1234567890), (0x1234567890, 1));
assert_eq(decompose_unsafe(0x1234567890), (0x1234567890, 0));
}
}

fn check_decompose() {
Expand All @@ -29,22 +31,26 @@ fn check_decompose() {
}

fn check_lt_unsafe() {
assert(lt_unsafe(0, 1, 16));
assert(lt_unsafe(0, 0x100, 16));
assert(lt_unsafe(0x100, TWO_POW_128 - 1, 16));
assert(!lt_unsafe(0, TWO_POW_128, 16));
unsafe {
assert(lt_unsafe(0, 1, 16));
assert(lt_unsafe(0, 0x100, 16));
assert(lt_unsafe(0x100, TWO_POW_128 - 1, 16));
assert(!lt_unsafe(0, TWO_POW_128, 16));
}
}

fn check_lte_unsafe() {
assert(lte_unsafe(0, 1, 16));
assert(lte_unsafe(0, 0x100, 16));
assert(lte_unsafe(0x100, TWO_POW_128 - 1, 16));
assert(!lte_unsafe(0, TWO_POW_128, 16));
unsafe {
assert(lte_unsafe(0, 1, 16));
assert(lte_unsafe(0, 0x100, 16));
assert(lte_unsafe(0x100, TWO_POW_128 - 1, 16));
assert(!lte_unsafe(0, TWO_POW_128, 16));

assert(lte_unsafe(0, 0, 16));
assert(lte_unsafe(0x100, 0x100, 16));
assert(lte_unsafe(TWO_POW_128 - 1, TWO_POW_128 - 1, 16));
assert(lte_unsafe(TWO_POW_128, TWO_POW_128, 16));
assert(lte_unsafe(0, 0, 16));
assert(lte_unsafe(0x100, 0x100, 16));
assert(lte_unsafe(TWO_POW_128 - 1, TWO_POW_128 - 1, 16));
assert(lte_unsafe(TWO_POW_128, TWO_POW_128, 16));
}
}

fn check_assert_gt() {
Expand Down Expand Up @@ -81,8 +87,8 @@ unconstrained fn checks_in_brillig() {
}

fn main() {
checks();
unsafe {
checks();
checks_in_brillig();
}
}
4 changes: 3 additions & 1 deletion test_programs/execution_success/brillig_assert/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
//
// The features being tested is using assert on brillig
fn main(x: Field) {
assert(1 == conditional(x as bool));
unsafe {
assert(1 == conditional(x as bool));
}
}

unconstrained fn conditional(x: bool) -> Field {
Expand Down
10 changes: 6 additions & 4 deletions test_programs/execution_success/brillig_calls/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
//
// The features being tested is brillig calls
fn main(x: u32) {
assert(entry_point(x) == 2);
swap_entry_point(x, x + 1);
assert(deep_entry_point(x) == 4);
multiple_values_entry_point(x);
unsafe {
assert(entry_point(x) == 2);
swap_entry_point(x, x + 1);
assert(deep_entry_point(x) == 4);
multiple_values_entry_point(x);
}
}

unconstrained fn returns_multiple_values(x: u32) -> (u32, u32, u32, u32) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ struct MyStruct {
}

fn main(x: u32) {
assert(wrapper(increment, x) == x + 1);
assert(wrapper(increment_acir, x) == x + 1);
assert(wrapper(decrement, x) == std::wrapping_sub(x, 1));
assert(wrapper_with_struct(MyStruct { operation: increment }, x) == x + 1);
assert(wrapper_with_struct(MyStruct { operation: decrement }, x) == std::wrapping_sub(x, 1));
// https://github.com/noir-lang/noir/issues/1975
assert(increment(x) == x + 1);
unsafe {
assert(wrapper(increment, x) == x + 1);
assert(wrapper(increment_acir, x) == x + 1);
assert(wrapper(decrement, x) == std::wrapping_sub(x, 1));
assert(wrapper_with_struct(MyStruct { operation: increment }, x) == x + 1);
assert(wrapper_with_struct(MyStruct { operation: decrement }, x) == std::wrapping_sub(x, 1));
// https://github.com/noir-lang/noir/issues/1975
assert(increment(x) == x + 1);
}
}

unconstrained fn wrapper(func: fn(u32) -> u32, param: u32) -> u32 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use dep::std;
//
// The features being tested is hash_to_field in brillig
fn main(input: Field) -> pub Field {
hash_to_field(input)
unsafe {
hash_to_field(input)
}
}

unconstrained fn hash_to_field(input: Field) -> Field {
Expand Down
6 changes: 4 additions & 2 deletions test_programs/execution_success/brillig_loop/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
//
// The features being tested is basic looping on brillig
fn main(sum: u32) {
assert(loop(4) == sum);
assert(plain_loop() == sum);
unsafe {
assert(loop(4) == sum);
assert(plain_loop() == sum);
}
}

unconstrained fn loop(x: u32) -> u32 {
Expand Down
20 changes: 11 additions & 9 deletions test_programs/execution_success/brillig_nested_arrays/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@ unconstrained fn create_and_assert_inside_brillig(x: Field, y: Field) {
}

fn main(x: Field, y: Field) {
let header = Header { params: [1, 2, 3] };
let note0 = MyNote { array: [1, 2], plain: 3, header };
let note1 = MyNote { array: [4, 5], plain: 6, header };

assert(access_nested([note0, note1], x, y) == (2 + 4 + 3 + 1));

let notes = create_inside_brillig();
assert_inside_brillig(notes, x, y);
create_and_assert_inside_brillig(x, y);
unsafe {
let header = Header { params: [1, 2, 3] };
let note0 = MyNote { array: [1, 2], plain: 3, header };
let note1 = MyNote { array: [4, 5], plain: 6, header };

assert(access_nested([note0, note1], x, y) == (2 + 4 + 3 + 1));

let notes = create_inside_brillig();
assert_inside_brillig(notes, x, y);
create_and_assert_inside_brillig(x, y);
}
}

6 changes: 4 additions & 2 deletions test_programs/execution_success/brillig_not/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
//
// The features being tested is not instruction on brillig
fn main(x: Field, y: Field) {
assert(false == not_operator(x as bool));
assert(true == not_operator(y as bool));
unsafe {
assert(false == not_operator(x as bool));
assert(true == not_operator(y as bool));
}
}

unconstrained fn not_operator(x: bool) -> bool {
Expand Down
30 changes: 16 additions & 14 deletions test_programs/execution_success/brillig_oracle/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@ use dep::std::test::OracleMock;

// Tests oracle usage in brillig/unconstrained functions
fn main(_x: Field) {
let size = 20;
// TODO: Add a method along the lines of `(0..size).to_array()`.
let mut mock_oracle_response = [0; 20];
// TODO: Add an `array.reverse()` method.
let mut reversed_mock_oracle_response = [0; 20];
for i in 0..size {
mock_oracle_response[i] = i;
reversed_mock_oracle_response[19 - i] = i;
unsafe {
let size = 20;
// TODO: Add a method along the lines of `(0..size).to_array()`.
let mut mock_oracle_response = [0; 20];
// TODO: Add an `array.reverse()` method.
let mut reversed_mock_oracle_response = [0; 20];
for i in 0..size {
mock_oracle_response[i] = i;
reversed_mock_oracle_response[19 - i] = i;
}

// TODO: this method of returning a slice feels hacky.
let _ = OracleMock::mock("get_number_sequence").with_params(size).returns((20, mock_oracle_response));
let _ = OracleMock::mock("get_reverse_number_sequence").with_params(size).returns((20, reversed_mock_oracle_response));

get_number_sequence_wrapper(size as Field);
}

// TODO: this method of returning a slice feels hacky.
let _ = OracleMock::mock("get_number_sequence").with_params(size).returns((20, mock_oracle_response));
let _ = OracleMock::mock("get_reverse_number_sequence").with_params(size).returns((20, reversed_mock_oracle_response));

get_number_sequence_wrapper(size as Field);
}

// Define oracle functions which we have mocked above
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
//
// The feature being tested is brillig recursion
fn main(x: u32) {
assert(fibonacci(x) == 55);
unsafe {
assert(fibonacci(x) == 55);
}
}

unconstrained fn fibonacci(x: u32) -> u32 {
Expand Down
4 changes: 3 additions & 1 deletion test_programs/execution_success/brillig_sha256/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use dep::std;
//
// The features being tested is sha256 in brillig
fn main(x: Field, result: [u8; 32]) {
assert(result == sha256(x));
unsafe {
assert(result == sha256(x));
}
}

unconstrained fn sha256(x: Field) -> [u8; 32] {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
fn main(x: Field, y: Field) -> pub Field {
let notes = create_notes(x, y);
sum_x(notes, x, y)
unsafe {
let notes = create_notes(x, y);
sum_x(notes, x, y)
}
}

fn sum_x(notes: [Field; 2], x: Field, y: Field) -> Field {
Expand Down
4 changes: 3 additions & 1 deletion test_programs/execution_success/databus/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use dep::std;

fn main(mut x: u32, y: call_data u32, z: call_data [u32; 4]) -> return_data u32 {
let a = z[x];
a + foo(y)
unsafe {
a + foo(y)
}
}

// Use an unconstrained function to force the compiler to avoid inlining
Expand Down
2 changes: 1 addition & 1 deletion test_programs/execution_success/global_consts/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ unconstrained fn calculate_global_value() -> Field {
}

// Regression test for https://github.com/noir-lang/noir/issues/4318
global CALCULATED_GLOBAL: Field = calculate_global_value();
global CALCULATED_GLOBAL: Field = unsafe { calculate_global_value() };

fn main(
a: [Field; M + N - N],
Expand Down
2 changes: 1 addition & 1 deletion test_programs/execution_success/hashmap/src/utils.nr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pub(crate) fn cut<T, N, M>(input: [T; N]) -> [T; M] {
assert(M as u64 < N as u64, "M should be less than N.");

let mut new = [dep::std::unsafe::zeroed(); M];
let mut new = [dep::std::unsafe_func::zeroed(); M];
for i in 0..M {
new[i] = input[i];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ unconstrained fn create_inside_brillig(values: [Field; 6]) -> [MyNote; 2] {
}

fn main(values: [Field; 6]) {
let notes = create_inside_brillig(values);
let notes = unsafe {
create_inside_brillig(values)
};
assert(access_nested(notes) == (2 + 4 + 3 + 1));
}

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ impl MyDeserialize<1> for Field {
}

pub fn storage_read<N>() -> [Field; N] {
dep::std::unsafe::zeroed()
dep::std::unsafe_func::zeroed()
}

struct PublicMutable<T> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ fn test_acir() {

#[test(should_fail)]
fn test_brillig() {
assert_eq(out_of_bounds_unconstrained_wrapper([0; 50], [0; 50]), 0);
unsafe {
assert_eq(out_of_bounds_unconstrained_wrapper([0; 50], [0; 50]), 0);
}
}

0 comments on commit a1a4286

Please sign in to comment.