|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 OR MIT |
| 3 | + |
| 4 | +// ANCHOR: rectangle_definition |
| 5 | +#[derive(Debug, Copy, Clone)] |
| 6 | +struct Rectangle { |
| 7 | + width: u64, |
| 8 | + height: u64, |
| 9 | +} |
| 10 | + |
| 11 | +impl Rectangle { |
| 12 | + fn can_hold(&self, other: &Rectangle) -> bool { |
| 13 | + self.width > other.width && self.height > other.height |
| 14 | + } |
| 15 | +} |
| 16 | +// ANCHOR_END: rectangle_definition |
| 17 | + |
| 18 | +impl Rectangle { |
| 19 | + // ANCHOR: stretch_definition |
| 20 | + fn stretch(&self, factor: u64) -> Option<Self> { |
| 21 | + let w = self.width.checked_mul(factor)?; |
| 22 | + let h = self.height.checked_mul(factor)?; |
| 23 | + Some(Rectangle { width: w, height: h }) |
| 24 | + } |
| 25 | + // ANCHOR_END: stretch_definition |
| 26 | +} |
| 27 | + |
| 28 | +#[cfg(test)] |
| 29 | +mod tests { |
| 30 | + use super::*; |
| 31 | + |
| 32 | + // ANCHOR: stretch_unit_test |
| 33 | + #[test] |
| 34 | + fn stretched_rectangle_can_hold_original() { |
| 35 | + let original = Rectangle { width: 8, height: 7 }; |
| 36 | + let factor = 2; |
| 37 | + let larger = original.stretch(factor); |
| 38 | + assert!(larger.unwrap().can_hold(&original)); |
| 39 | + } |
| 40 | + // ANCHOR_END: stretch_unit_test |
| 41 | +} |
| 42 | + |
| 43 | +#[cfg(test)] |
| 44 | +mod proptests { |
| 45 | + use super::*; |
| 46 | + use proptest::num::u64; |
| 47 | + use proptest::prelude::*; |
| 48 | + |
| 49 | + proptest! { |
| 50 | + // ANCHOR: stretch_proptest |
| 51 | + #[test] |
| 52 | + fn stretched_rectangle_can_hold_original(width in u64::ANY, |
| 53 | + height in u64::ANY, |
| 54 | + factor in u64::ANY) { |
| 55 | + let original = Rectangle { |
| 56 | + width: width, |
| 57 | + height: height, |
| 58 | + }; |
| 59 | + if let Some(larger) = original.stretch(factor) { |
| 60 | + assert!(larger.can_hold(&original)); |
| 61 | + } |
| 62 | + } |
| 63 | + // ANCHOR_END: stretch_proptest |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +#[cfg(kani)] |
| 68 | +mod verification { |
| 69 | + use super::*; |
| 70 | + |
| 71 | + // ANCHOR: stretch_kani |
| 72 | + #[kani::proof] |
| 73 | + pub fn stretched_rectangle_can_hold_original() { |
| 74 | + let original = Rectangle { width: kani::any(), height: kani::any() }; |
| 75 | + let factor = kani::any(); |
| 76 | + if let Some(larger) = original.stretch(factor) { |
| 77 | + assert!(larger.can_hold(&original)); |
| 78 | + } |
| 79 | + } |
| 80 | + // ANCHOR_END: stretch_kani |
| 81 | + |
| 82 | + // ANCHOR: stretch_kani_fixed |
| 83 | + #[kani::proof] |
| 84 | + pub fn stretched_rectangle_can_hold_original_fixed() { |
| 85 | + let original = Rectangle { width: kani::any(), height: kani::any() }; |
| 86 | + let factor: u8 = kani::any(); //< would like to make u64 |
| 87 | + kani::assume(0 != original.width); |
| 88 | + kani::assume(0 != original.height); |
| 89 | + kani::assume(1 < factor); |
| 90 | + if let Some(larger) = original.stretch(factor as u64) { |
| 91 | + assert!(larger.can_hold(&original)); |
| 92 | + } |
| 93 | + } |
| 94 | + // ANCHOR_END: stretch_kani_fixed |
| 95 | + |
| 96 | + // Currently causes Kani timeout |
| 97 | + #[kani::proof] |
| 98 | + pub fn stretched_rectangle_can_hold_original_fixed_u64() { |
| 99 | + let original = Rectangle { width: kani::any(), height: kani::any() }; |
| 100 | + let factor = kani::any(); |
| 101 | + kani::assume(0 != original.width); |
| 102 | + kani::assume(0 != original.height); |
| 103 | + kani::assume(1 < factor); |
| 104 | + if let Some(larger) = original.stretch(factor) { |
| 105 | + assert!(larger.can_hold(&original)); |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments