| 1 |
extern crate bytecount; |
| 2 |
#[macro_use] |
| 3 |
extern crate quickcheck; |
| 4 |
extern crate rand; |
| 5 |
|
| 6 |
use bytecount::{count, naive_count, naive_num_chars, num_chars}; |
| 7 |
use rand::RngCore; |
| 8 |
|
| 9 |
fn random_bytes(len: usize) -> Vec<u8> { |
| 10 |
let mut result = vec![0; len]; |
| 11 |
rand::thread_rng().fill_bytes(&mut result); |
| 12 |
result |
| 13 |
} |
| 14 |
|
| 15 |
quickcheck! { |
| 16 |
fn check_count_correct(x: (Vec<u8>, u8)) -> bool { |
| 17 |
let (haystack, needle) = x; |
| 18 |
count(&haystack, needle) == naive_count(&haystack, needle) |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
#[test] |
| 23 |
fn check_count_large() { |
| 24 |
let haystack = vec![0u8; if cfg!(miri) { 2_000 } else { 10_000_000 }]; |
| 25 |
assert_eq!(naive_count(&haystack, 0), count(&haystack, 0)); |
| 26 |
assert_eq!(naive_count(&haystack, 1), count(&haystack, 1)); |
| 27 |
} |
| 28 |
|
| 29 |
#[test] |
| 30 |
fn check_count_large_rand() { |
| 31 |
let haystack = random_bytes(if cfg!(miri) { 200 } else { 100_000 }); |
| 32 |
for i in 0..=255 { |
| 33 |
assert_eq!(naive_count(&haystack, i), count(&haystack, i)); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
#[test] |
| 38 |
fn check_count_some() { |
| 39 |
let haystack = vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 68]; |
| 40 |
let needle = 68; |
| 41 |
assert_eq!(count(&haystack, needle), naive_count(&haystack, needle)); |
| 42 |
} |
| 43 |
|
| 44 |
#[test] |
| 45 |
fn check_count_overflow() { |
| 46 |
let haystack = vec![0, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; |
| 47 |
let needle = 2; |
| 48 |
assert_eq!(count(&haystack, needle), naive_count(&haystack, needle)); |
| 49 |
} |
| 50 |
|
| 51 |
#[test] |
| 52 |
fn check_count_overflow_many() { |
| 53 |
let string = [b'x'; 20000]; |
| 54 |
for i in 0..20000 { |
| 55 |
assert_eq!(count(&string[..i], b'x'), i); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
quickcheck! { |
| 60 |
fn check_num_chars_correct(haystack: Vec<u8>) -> bool { |
| 61 |
num_chars(&haystack) == naive_num_chars(&haystack) |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
#[test] |
| 66 |
fn check_num_chars_large() { |
| 67 |
let haystack = vec![0u8; if cfg!(miri) { 2_000 } else { 10_000_000 }]; |
| 68 |
assert_eq!(naive_num_chars(&haystack), num_chars(&haystack)); |
| 69 |
assert_eq!(naive_num_chars(&haystack), num_chars(&haystack)); |
| 70 |
} |
| 71 |
|
| 72 |
#[test] |
| 73 |
fn check_num_chars_some() { |
| 74 |
let haystack = vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 68]; |
| 75 |
assert_eq!(num_chars(&haystack), naive_num_chars(&haystack)); |
| 76 |
} |
| 77 |
|
| 78 |
#[test] |
| 79 |
fn check_num_chars_overflow() { |
| 80 |
let haystack = vec![0, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; |
| 81 |
assert_eq!(num_chars(&haystack), naive_num_chars(&haystack)); |
| 82 |
} |
| 83 |
|
| 84 |
#[test] |
| 85 |
fn check_num_chars_overflow_many() { |
| 86 |
let string = [b'x'; 20000]; |
| 87 |
for i in 0..20000 { |
| 88 |
assert_eq!(num_chars(&string[..i]), i); |
| 89 |
} |
| 90 |
} |