Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore!: Remove empty value from bounded vec #4431

Merged
merged 5 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions noir_stdlib/src/collections/bounded_vec.nr
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
struct BoundedVec<T, MaxLen> {
storage: [T; MaxLen],
len: u64,
empty_value: T,
}

impl<T, MaxLen> BoundedVec<T, MaxLen> {
pub fn new(initial_value: T) -> Self {
BoundedVec { storage: [initial_value; MaxLen], len: 0, empty_value: initial_value }
pub fn new() -> Self {
let zeroed = crate::unsafe::zeroed();
BoundedVec { storage: [zeroed; MaxLen], len: 0 }
}

pub fn get(mut self: Self, index: u64) -> T {
Expand Down Expand Up @@ -68,7 +68,7 @@ impl<T, MaxLen> BoundedVec<T, MaxLen> {
self.len -= 1;

let elem = self.storage[self.len];
self.storage[self.len] = self.empty_value;
self.storage[self.len] = crate::unsafe::zeroed();
elem
}

Expand Down
32 changes: 16 additions & 16 deletions test_programs/noir_test_success/bounded_vec/src/main.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[test]
fn test_vec_push_pop() {
let mut vec: BoundedVec<Field, 3> = BoundedVec::new(0);
let mut vec: BoundedVec<Field, 3> = BoundedVec::new();
assert(vec.len == 0);
vec.push(2);
assert(vec.len == 1);
Expand All @@ -17,7 +17,7 @@ fn test_vec_push_pop() {

#[test]
fn test_vec_extend_from_array() {
let mut vec: BoundedVec<Field, 3> = BoundedVec::new(0);
let mut vec: BoundedVec<Field, 3> = BoundedVec::new();
vec.extend_from_array([2, 4]);
assert(vec.len == 2);
assert(vec.get(0) == 2);
Expand All @@ -26,69 +26,69 @@ fn test_vec_extend_from_array() {

#[test(should_fail_with="extend_from_array out of bounds")]
fn test_vec_extend_from_array_out_of_bound() {
let mut vec: BoundedVec<Field, 2> = BoundedVec::new(0);
let mut vec: BoundedVec<Field, 2> = BoundedVec::new();
vec.extend_from_array([2, 4, 6]);
}

#[test(should_fail_with="extend_from_array out of bounds")]
fn test_vec_extend_from_array_twice_out_of_bound() {
let mut vec: BoundedVec<Field, 2> = BoundedVec::new(0);
let mut vec: BoundedVec<Field, 2> = BoundedVec::new();
vec.extend_from_array([2]);
assert(vec.len == 1);
vec.extend_from_array([4, 6]);
}

#[test(should_fail)]
fn test_vec_get_out_of_bound() {
let mut vec: BoundedVec<Field, 2> = BoundedVec::new(0);
let mut vec: BoundedVec<Field, 2> = BoundedVec::new();
vec.extend_from_array([2, 4]);
let _x = vec.get(2);
}

#[test(should_fail)]
fn test_vec_get_not_declared() {
let mut vec: BoundedVec<Field, 2> = BoundedVec::new(0);
let mut vec: BoundedVec<Field, 2> = BoundedVec::new();
vec.extend_from_array([2]);
let _x = vec.get(1);
}

#[test(should_fail)]
fn test_vec_get_uninitialized() {
let mut vec: BoundedVec<Field, 2> = BoundedVec::new(0);
let mut vec: BoundedVec<Field, 2> = BoundedVec::new();
let _x = vec.get(0);
}

#[test(should_fail_with="push out of bounds")]
fn test_vec_push_out_of_bound() {
let mut vec: BoundedVec<Field, 1> = BoundedVec::new(0);
let mut vec: BoundedVec<Field, 1> = BoundedVec::new();
vec.push(1);
vec.push(2);
}

#[test(should_fail_with="extend_from_bounded_vec out of bounds")]
fn test_vec_extend_from_bounded_vec_out_of_bound() {
let mut vec: BoundedVec<Field, 2> = BoundedVec::new(0);
let mut vec: BoundedVec<Field, 2> = BoundedVec::new();

let mut another_vec: BoundedVec<Field, 3> = BoundedVec::new(0);
let mut another_vec: BoundedVec<Field, 3> = BoundedVec::new();
another_vec.extend_from_array([1, 2, 3]);

vec.extend_from_bounded_vec(another_vec);
}

#[test(should_fail_with="extend_from_bounded_vec out of bounds")]
fn test_vec_extend_from_bounded_vec_twice_out_of_bound() {
let mut vec: BoundedVec<Field, 2> = BoundedVec::new(0);
let mut vec: BoundedVec<Field, 2> = BoundedVec::new();
vec.extend_from_array([1, 2]);

let mut another_vec: BoundedVec<Field, 1> = BoundedVec::new(0);
let mut another_vec: BoundedVec<Field, 1> = BoundedVec::new();
another_vec.push(3);

vec.extend_from_bounded_vec(another_vec);
}

#[test]
fn test_vec_any() {
let mut vec: BoundedVec<Field, 3> = BoundedVec::new(0);
let mut vec: BoundedVec<Field, 3> = BoundedVec::new();
vec.extend_from_array([2, 4, 6]);
assert(vec.any(|v| v == 2) == true);
assert(vec.any(|v| v == 4) == true);
Expand All @@ -98,8 +98,8 @@ fn test_vec_any() {

#[test]
fn test_vec_any_not_default() {
let default_value = 1;
let mut vec: BoundedVec<Field, 3> = BoundedVec::new(default_value);
let default_value = 0;
let mut vec: BoundedVec<Field, 3> = BoundedVec::new();
vec.extend_from_array([2, 4]);
assert(vec.any(|v| v == default_value) == false);
}
}
Loading