Skip to content

Commit 9c3f2b4

Browse files
authored
Add Rust package with manual tests (#127)
1 parent c7bf528 commit 9c3f2b4

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Copyright Kani Contributors
2+
# SPDX-License-Identifier: Apache-2.0 OR MIT
3+
4+
[package]
5+
name = "simple-test"
6+
version = "0.1.0"
7+
edition = "2021"
8+
9+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
10+
11+
[dependencies]
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright Kani Contributors
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
4+
pub fn estimate_size(x: u32) -> u32 {
5+
assert!(x < 4096);
6+
7+
if x < 256 {
8+
if x < 128 {
9+
return 1;
10+
} else {
11+
return 3;
12+
}
13+
} else if x < 1024 {
14+
if x > 1022 {
15+
return 4;
16+
} else {
17+
return 5;
18+
}
19+
} else {
20+
if x < 2048 {
21+
return 7;
22+
} else {
23+
return 9;
24+
}
25+
}
26+
}
27+
28+
pub fn find_index(nums: &[i32], target: i32) -> Option<usize> {
29+
for (index, &num) in nums.iter().enumerate() { // coverage should be yellow
30+
if num == target { // coverage should be green
31+
return Some(index); // coverage should be green
32+
}
33+
}
34+
None // coverage should be red
35+
} // coverage should be yellow
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright Kani Contributors
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
4+
//! This package is intended to assist in manually testing the features of the
5+
//! extension. The tests to be performed are the following:
6+
//!
7+
//! 1. Run verification for `test_success` and check that it passes.
8+
//! 2. Run verification for `test_failure` and check that it fails with
9+
//! "assertion failed: x < 4096".
10+
//! 3. Click on "Generate concrete test for test_failure" and check that a new
11+
//! Rust unit test is added after "test_failure".
12+
//! 4. Check that the actions "Run Test (Kani)" and "Debug Harness (Kani)"
13+
//! appear above the Rust unit test that was generated in the previous step.
14+
//! 5. Click on the "Run Test (Kani)" action. Check that the test runs on a
15+
//! terminal and it panics as expected.
16+
//! 6. Click on the "Debug Harness (Kani)" action. Check that the debugging mode
17+
//! is started (debugging controls should appear on the top) and stop it by
18+
//! clicking on the red square button.
19+
//! 7. Toggle on the "Codelens-kani: Highlight" option in "Settings > Kani".
20+
//! 8. Check that the "Get coverage info" action appears for the "test_success"
21+
//! and "test_failure" harnesses.
22+
//! 9. Run the "Get coverage info" action for "test_coverage". Check that all
23+
//! lines in "test_coverage" are green. In addition, check that in
24+
//! "funs::find_index":
25+
//! - The first and last highlighted lines are yellow.
26+
//! - The second and third highlighted lines are green.
27+
//! - The remaining highlighted line is red.
28+
//! Comments indicating the correct colors are available in "funs::find_index".
29+
mod funs;
30+
31+
#[cfg(kani)]
32+
mod verify {
33+
use super::*;
34+
35+
#[kani::proof]
36+
fn test_success() {
37+
let x: u32 = kani::any();
38+
kani::assume(x < 4096);
39+
let y = funs::estimate_size(x);
40+
assert!(y < 10);
41+
}
42+
43+
#[kani::proof]
44+
fn test_failure() {
45+
let x: u32 = kani::any();
46+
let y = funs::estimate_size(x);
47+
assert!(y < 10);
48+
}
49+
50+
#[kani::proof]
51+
fn test_coverage() {
52+
let numbers = [10, 20, 30, 40, 50];
53+
let target = 30;
54+
let result = funs::find_index(&numbers, target);
55+
assert_eq!(result, Some(2));
56+
}
57+
}

0 commit comments

Comments
 (0)