| 1 | use std::collections::hash_map::DefaultHasher; |
| 2 | use std::collections::{HashMap, HashSet}; |
| 3 | use std::ffi::CString; |
| 4 | use std::hash::BuildHasherDefault; |
| 5 | use std::path::PathBuf; |
| 6 | |
| 7 | use super::{quickcheck, Gen, QuickCheck, TestResult}; |
| 8 | |
| 9 | #[test] |
| 10 | fn prop_oob() { |
| 11 | fn prop() -> bool { |
| 12 | let zero: Vec<bool> = vec![]; |
| 13 | zero[0] |
| 14 | } |
| 15 | if let Ok(n) = QuickCheck::new().quicktest(prop as fn() -> bool) { |
| 16 | panic!( |
| 17 | "prop_oob should fail with a runtime error \ |
| 18 | but instead it passed {} tests.", |
| 19 | n |
| 20 | ); |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | #[test] |
| 25 | fn prop_reverse_reverse() { |
| 26 | fn prop(xs: Vec<usize>) -> bool { |
| 27 | let rev: Vec<_> = xs.clone().into_iter().rev().collect(); |
| 28 | let revrev: Vec<_> = rev.into_iter().rev().collect(); |
| 29 | xs == revrev |
| 30 | } |
| 31 | quickcheck(prop as fn(Vec<usize>) -> bool); |
| 32 | } |
| 33 | |
| 34 | quickcheck! { |
| 35 | fn prop_reverse_reverse_macro(xs: Vec<usize>) -> bool { |
| 36 | let rev: Vec<_> = xs.clone().into_iter().rev().collect(); |
| 37 | let revrev: Vec<_> = rev.into_iter().rev().collect(); |
| 38 | xs == revrev |
| 39 | } |
| 40 | |
| 41 | #[should_panic] |
| 42 | fn prop_macro_panic(_x: u32) -> bool { |
| 43 | assert!(false); |
| 44 | false |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | #[test] |
| 49 | fn reverse_single() { |
| 50 | fn prop(xs: Vec<usize>) -> TestResult { |
| 51 | if xs.len() != 1 { |
| 52 | TestResult::discard() |
| 53 | } else { |
| 54 | TestResult::from_bool( |
| 55 | xs == xs.clone().into_iter().rev().collect::<Vec<_>>(), |
| 56 | ) |
| 57 | } |
| 58 | } |
| 59 | quickcheck(prop as fn(Vec<usize>) -> TestResult); |
| 60 | } |
| 61 | |
| 62 | #[test] |
| 63 | fn reverse_app() { |
| 64 | fn prop(xs: Vec<usize>, ys: Vec<usize>) -> bool { |
| 65 | let mut app = xs.clone(); |
| 66 | app.extend(ys.iter().copied()); |
| 67 | let app_rev: Vec<usize> = app.into_iter().rev().collect(); |
| 68 | |
| 69 | let rxs: Vec<usize> = xs.into_iter().rev().collect(); |
| 70 | let mut rev_app = ys.into_iter().rev().collect::<Vec<usize>>(); |
| 71 | rev_app.extend(rxs); |
| 72 | |
| 73 | app_rev == rev_app |
| 74 | } |
| 75 | quickcheck(prop as fn(Vec<usize>, Vec<usize>) -> bool); |
| 76 | } |
| 77 | |
| 78 | #[test] |
| 79 | fn max() { |
| 80 | fn prop(x: isize, y: isize) -> TestResult { |
| 81 | if x > y { |
| 82 | TestResult::discard() |
| 83 | } else { |
| 84 | TestResult::from_bool(::std::cmp::max(x, y) == y) |
| 85 | } |
| 86 | } |
| 87 | quickcheck(prop as fn(isize, isize) -> TestResult); |
| 88 | } |
| 89 | |
| 90 | #[test] |
| 91 | fn sort() { |
| 92 | fn prop(mut xs: Vec<isize>) -> bool { |
| 93 | xs.sort_unstable(); |
| 94 | for i in xs.windows(2) { |
| 95 | if i[0] > i[1] { |
| 96 | return false; |
| 97 | } |
| 98 | } |
| 99 | true |
| 100 | } |
| 101 | quickcheck(prop as fn(Vec<isize>) -> bool); |
| 102 | } |
| 103 | |
| 104 | fn sieve(n: usize) -> Vec<usize> { |
| 105 | if n <= 1 { |
| 106 | return vec![]; |
| 107 | } |
| 108 | |
| 109 | let mut marked = vec![false; n + 1]; |
| 110 | marked[0] = true; |
| 111 | marked[1] = true; |
| 112 | marked[2] = true; |
| 113 | for p in 2..n { |
| 114 | for i in (2 * p..n).filter(|&n| n % p == 0) { |
| 115 | marked[i] = true; |
| 116 | } |
| 117 | } |
| 118 | marked |
| 119 | .iter() |
| 120 | .enumerate() |
| 121 | .filter_map(|(i, &m)| if m { None } else { Some(i) }) |
| 122 | .collect() |
| 123 | } |
| 124 | |
| 125 | fn is_prime(n: usize) -> bool { |
| 126 | n != 0 && n != 1 && (2..).take_while(|i| i * i <= n).all(|i| n % i != 0) |
| 127 | } |
| 128 | |
| 129 | #[test] |
| 130 | #[should_panic] |
| 131 | fn sieve_not_prime() { |
| 132 | fn prop_all_prime(n: u8) -> bool { |
| 133 | sieve(n as usize).into_iter().all(is_prime) |
| 134 | } |
| 135 | quickcheck(prop_all_prime as fn(u8) -> bool); |
| 136 | } |
| 137 | |
| 138 | #[test] |
| 139 | #[should_panic] |
| 140 | fn sieve_not_all_primes() { |
| 141 | fn prop_prime_iff_in_the_sieve(n: u8) -> bool { |
| 142 | let n = n as usize; |
| 143 | sieve(n) == (0..=n).filter(|&i| is_prime(i)).collect::<Vec<_>>() |
| 144 | } |
| 145 | quickcheck(prop_prime_iff_in_the_sieve as fn(u8) -> bool); |
| 146 | } |
| 147 | |
| 148 | #[test] |
| 149 | fn testable_result() { |
| 150 | fn result() -> Result<bool, String> { |
| 151 | Ok(true) |
| 152 | } |
| 153 | quickcheck(result as fn() -> Result<bool, String>); |
| 154 | } |
| 155 | |
| 156 | #[test] |
| 157 | #[should_panic] |
| 158 | fn testable_result_err() { |
| 159 | quickcheck(Err::<bool, i32> as fn(i32) -> Result<bool, i32>); |
| 160 | } |
| 161 | |
| 162 | #[test] |
| 163 | fn testable_unit() { |
| 164 | fn do_nothing() {} |
| 165 | quickcheck(do_nothing as fn()); |
| 166 | } |
| 167 | |
| 168 | #[test] |
| 169 | fn testable_unit_panic() { |
| 170 | fn panic() { |
| 171 | panic!(); |
| 172 | } |
| 173 | assert!(QuickCheck::new().quicktest(panic as fn()).is_err()); |
| 174 | } |
| 175 | |
| 176 | #[test] |
| 177 | fn regression_issue_83() { |
| 178 | fn prop(_: u8) -> bool { |
| 179 | true |
| 180 | } |
| 181 | QuickCheck::new().rng(Gen::new(1024)).quickcheck(prop as fn(u8) -> bool); |
| 182 | } |
| 183 | |
| 184 | #[test] |
| 185 | fn regression_issue_83_signed() { |
| 186 | fn prop(_: i8) -> bool { |
| 187 | true |
| 188 | } |
| 189 | QuickCheck::new().rng(Gen::new(1024)).quickcheck(prop as fn(i8) -> bool); |
| 190 | } |
| 191 | |
| 192 | // Test that we can show the message after panic |
| 193 | #[test] |
| 194 | #[should_panic(expected = "foo")] |
| 195 | fn panic_msg_1() { |
| 196 | fn prop() -> bool { |
| 197 | panic!("foo"); |
| 198 | } |
| 199 | quickcheck(prop as fn() -> bool); |
| 200 | } |
| 201 | |
| 202 | #[test] |
| 203 | #[should_panic(expected = "foo")] |
| 204 | fn panic_msg_2() { |
| 205 | fn prop() -> bool { |
| 206 | assert!("foo" == "bar"); |
| 207 | true |
| 208 | } |
| 209 | quickcheck(prop as fn() -> bool); |
| 210 | } |
| 211 | |
| 212 | #[test] |
| 213 | #[should_panic(expected = "foo")] |
| 214 | fn panic_msg_3() { |
| 215 | fn prop() -> bool { |
| 216 | assert_eq!("foo", "bar"); |
| 217 | true |
| 218 | } |
| 219 | quickcheck(prop as fn() -> bool); |
| 220 | } |
| 221 | |
| 222 | #[test] |
| 223 | #[should_panic] |
| 224 | fn regression_issue_107_hang() { |
| 225 | fn prop(a: Vec<u8>) -> bool { |
| 226 | a.contains(&1) |
| 227 | } |
| 228 | quickcheck(prop as fn(_) -> bool); |
| 229 | } |
| 230 | |
| 231 | #[test] |
| 232 | #[should_panic( |
| 233 | expected = "(Unable to generate enough tests, 0 not discarded.)" |
| 234 | )] |
| 235 | fn all_tests_discarded_min_tests_passed_set() { |
| 236 | fn prop_discarded(_: u8) -> TestResult { |
| 237 | TestResult::discard() |
| 238 | } |
| 239 | |
| 240 | QuickCheck::new() |
| 241 | .tests(16) |
| 242 | .min_tests_passed(8) |
| 243 | .quickcheck(prop_discarded as fn(u8) -> TestResult); |
| 244 | } |
| 245 | |
| 246 | #[test] |
| 247 | fn all_tests_discarded_min_tests_passed_missing() { |
| 248 | fn prop_discarded(_: u8) -> TestResult { |
| 249 | TestResult::discard() |
| 250 | } |
| 251 | |
| 252 | QuickCheck::new().quickcheck(prop_discarded as fn(u8) -> TestResult); |
| 253 | } |
| 254 | |
| 255 | quickcheck! { |
| 256 | /// The following is a very simplistic test, which only verifies |
| 257 | /// that our PathBuf::arbitrary does not panic. Still, that's |
| 258 | /// something! :) |
| 259 | fn pathbuf(_p: PathBuf) -> bool { |
| 260 | true |
| 261 | } |
| 262 | |
| 263 | fn basic_hashset(_set: HashSet<u8>) -> bool { |
| 264 | true |
| 265 | } |
| 266 | |
| 267 | fn basic_hashmap(_map: HashMap<u8, u8>) -> bool { |
| 268 | true |
| 269 | } |
| 270 | |
| 271 | fn substitute_hashset( |
| 272 | _set: HashSet<u8, BuildHasherDefault<DefaultHasher>> |
| 273 | ) -> bool { |
| 274 | true |
| 275 | } |
| 276 | |
| 277 | fn substitute_hashmap( |
| 278 | _map: HashMap<u8, u8, BuildHasherDefault<DefaultHasher>> |
| 279 | ) -> bool { |
| 280 | true |
| 281 | } |
| 282 | |
| 283 | fn cstring(_p: CString) -> bool { |
| 284 | true |
| 285 | } |
| 286 | } |