Skip to content

Commit

Permalink
Implement kani::Arbitrary for Box<T> (#2404)
Browse files Browse the repository at this point in the history
  • Loading branch information
adpaco-aws authored Apr 25, 2023
1 parent b5c116d commit b10ef23
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 0 deletions.
9 changes: 9 additions & 0 deletions library/kani/src/arbitrary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,12 @@ impl Arbitrary for std::marker::PhantomPinned {
PhantomPinned
}
}

impl<T> Arbitrary for std::boxed::Box<T>
where
T: Arbitrary,
{
fn any() -> Self {
Box::new(T::any())
}
}
11 changes: 11 additions & 0 deletions tests/expected/derive-arbitrary/box/expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Status: SATISFIED\
Description: "cover condition: *foo.boxed == i32::MIN"
Status: SATISFIED\
Description: "cover condition: *foo.boxed == 0"
Status: SATISFIED\
Description: "cover condition: *foo.boxed == i32::MAX"
Status: UNSATISFIABLE\
Description: "cover condition: *foo.boxed < i32::MIN"
Status: UNSATISFIABLE\
Description: "cover condition: *foo.boxed > i32::MAX"
VERIFICATION:- SUCCESSFUL
20 changes: 20 additions & 0 deletions tests/expected/derive-arbitrary/box/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright Kani Contributors
// SPDX-License-Identifier: Apache-2.0 OR MIT

//! Check that Kani can automatically derive `Arbitrary` on a struct with a
//! member of type `Box<T>`
#[derive(kani::Arbitrary)]
struct Foo<T> {
boxed: Box<T>,
}

#[kani::proof]
fn main() {
let foo: Foo<i32> = kani::any();
kani::cover!(*foo.boxed == i32::MIN);
kani::cover!(*foo.boxed == 0);
kani::cover!(*foo.boxed == i32::MAX);
kani::cover!(*foo.boxed < i32::MIN); // <-- this condition should be `UNSATISFIABLE`
kani::cover!(*foo.boxed > i32::MAX); // <-- this condition should be `UNSATISFIABLE`
}

0 comments on commit b10ef23

Please sign in to comment.