| 1 |
extern crate rand; |
| 2 |
|
| 3 |
|
| 4 |
use super::*; |
| 5 |
use self::rand::distributions::{Distribution, Range}; |
| 6 |
use self::rand::{Rng, FromEntropy}; |
| 7 |
|
| 8 |
#[test] |
| 9 |
fn line_params_perfect_multiple_of_line_length_lf() { |
| 10 |
let params = line_wrap_parameters(100, 20, lf().len()); |
| 11 |
|
| 12 |
assert_eq!( |
| 13 |
LineWrapParameters { |
| 14 |
line_with_ending_len: 21, |
| 15 |
lines_with_endings: 4, |
| 16 |
last_line_len: 20, |
| 17 |
total_full_wrapped_lines_len: 84, |
| 18 |
total_len: 104, |
| 19 |
total_line_endings_len: 4, |
| 20 |
}, |
| 21 |
params |
| 22 |
); |
| 23 |
} |
| 24 |
|
| 25 |
#[test] |
| 26 |
fn line_params_partial_last_line_crlf() { |
| 27 |
let params = line_wrap_parameters(103, 20, crlf().len()); |
| 28 |
|
| 29 |
assert_eq!( |
| 30 |
LineWrapParameters { |
| 31 |
line_with_ending_len: 22, |
| 32 |
lines_with_endings: 5, |
| 33 |
last_line_len: 3, |
| 34 |
total_full_wrapped_lines_len: 110, |
| 35 |
total_len: 113, |
| 36 |
total_line_endings_len: 10, |
| 37 |
}, |
| 38 |
params |
| 39 |
); |
| 40 |
} |
| 41 |
|
| 42 |
#[test] |
| 43 |
fn line_params_line_len_1_crlf() { |
| 44 |
let params = line_wrap_parameters(100, 1, crlf().len()); |
| 45 |
|
| 46 |
assert_eq!( |
| 47 |
LineWrapParameters { |
| 48 |
line_with_ending_len: 3, |
| 49 |
lines_with_endings: 99, |
| 50 |
last_line_len: 1, |
| 51 |
total_full_wrapped_lines_len: 99 * 3, |
| 52 |
total_len: 99 * 3 + 1, |
| 53 |
total_line_endings_len: 99 * 2, |
| 54 |
}, |
| 55 |
params |
| 56 |
); |
| 57 |
} |
| 58 |
|
| 59 |
#[test] |
| 60 |
fn line_params_line_len_longer_than_input_crlf() { |
| 61 |
let params = line_wrap_parameters(100, 200, crlf().len()); |
| 62 |
|
| 63 |
assert_eq!( |
| 64 |
LineWrapParameters { |
| 65 |
line_with_ending_len: 202, |
| 66 |
lines_with_endings: 0, |
| 67 |
last_line_len: 100, |
| 68 |
total_full_wrapped_lines_len: 0, |
| 69 |
total_len: 100, |
| 70 |
total_line_endings_len: 0, |
| 71 |
}, |
| 72 |
params |
| 73 |
); |
| 74 |
} |
| 75 |
|
| 76 |
#[test] |
| 77 |
fn line_params_line_len_same_as_input_crlf() { |
| 78 |
let params = line_wrap_parameters(100, 100, crlf().len()); |
| 79 |
|
| 80 |
assert_eq!( |
| 81 |
LineWrapParameters { |
| 82 |
line_with_ending_len: 102, |
| 83 |
lines_with_endings: 0, |
| 84 |
last_line_len: 100, |
| 85 |
total_full_wrapped_lines_len: 0, |
| 86 |
total_len: 100, |
| 87 |
total_line_endings_len: 0, |
| 88 |
}, |
| 89 |
params |
| 90 |
); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
#[test] |
| 95 |
fn line_wrap_length_1_lf() { |
| 96 |
let mut buf = vec![0x1, 0x2, 0x3, 0x4]; |
| 97 |
|
| 98 |
assert_eq!(3, do_line_wrap(&mut buf, 1, &lf())); |
| 99 |
|
| 100 |
assert_eq!(vec![0x1, 0xA, 0x2, 0xA, 0x3, 0xA, 0x4], buf); |
| 101 |
} |
| 102 |
|
| 103 |
#[test] |
| 104 |
fn line_wrap_length_1_crlf() { |
| 105 |
let mut buf = vec![0x1, 0x2, 0x3, 0x4]; |
| 106 |
|
| 107 |
assert_eq!(6, do_line_wrap(&mut buf, 1, &crlf())); |
| 108 |
|
| 109 |
assert_eq!(vec![0x1, 0xD, 0xA, 0x2, 0xD, 0xA, 0x3, 0xD, 0xA, 0x4], buf); |
| 110 |
} |
| 111 |
|
| 112 |
#[test] |
| 113 |
fn line_wrap_length_2_lf_full_lines() { |
| 114 |
let mut buf = vec![0x1, 0x2, 0x3, 0x4]; |
| 115 |
|
| 116 |
assert_eq!(1, do_line_wrap(&mut buf, 2, &lf())); |
| 117 |
|
| 118 |
assert_eq!(vec![0x1, 0x2, 0xA, 0x3, 0x4], buf); |
| 119 |
} |
| 120 |
|
| 121 |
#[test] |
| 122 |
fn line_wrap_length_2_crlf_full_lines() { |
| 123 |
let mut buf = vec![0x1, 0x2, 0x3, 0x4]; |
| 124 |
|
| 125 |
assert_eq!(2, do_line_wrap(&mut buf, 2, &crlf())); |
| 126 |
|
| 127 |
assert_eq!(vec![0x1, 0x2, 0xD, 0xA, 0x3, 0x4], buf); |
| 128 |
} |
| 129 |
|
| 130 |
#[test] |
| 131 |
fn line_wrap_length_2_lf_partial_line() { |
| 132 |
let mut buf = vec![0x1, 0x2, 0x3, 0x4, 0x5]; |
| 133 |
|
| 134 |
assert_eq!(2, do_line_wrap(&mut buf, 2, &lf())); |
| 135 |
|
| 136 |
assert_eq!(vec![0x1, 0x2, 0xA, 0x3, 0x4, 0xA, 0x5], buf); |
| 137 |
} |
| 138 |
|
| 139 |
#[test] |
| 140 |
fn line_wrap_length_2_crlf_partial_line() { |
| 141 |
let mut buf = vec![0x1, 0x2, 0x3, 0x4, 0x5]; |
| 142 |
|
| 143 |
assert_eq!(4, do_line_wrap(&mut buf, 2, &crlf())); |
| 144 |
|
| 145 |
assert_eq!(vec![0x1, 0x2, 0xD, 0xA, 0x3, 0x4, 0xD, 0xA, 0x5], buf); |
| 146 |
} |
| 147 |
|
| 148 |
#[test] |
| 149 |
fn line_wrap_random_slice() { |
| 150 |
let mut buf: Vec<u8> = Vec::new(); |
| 151 |
let buf_range = Range::new(10, 1000); |
| 152 |
let line_range = Range::new(10, 100); |
| 153 |
let mut rng = rand::rngs::SmallRng::from_entropy(); |
| 154 |
let mut line_ending_bytes: Vec<u8> = Vec::new(); |
| 155 |
|
| 156 |
for _ in 0..10_000 { |
| 157 |
buf.clear(); |
| 158 |
line_ending_bytes.clear(); |
| 159 |
|
| 160 |
let buf_len = buf_range.sample(&mut rng); |
| 161 |
let line_len = line_range.sample(&mut rng); |
| 162 |
|
| 163 |
for _ in 0..buf_len { |
| 164 |
buf.push(rng.gen()); |
| 165 |
} |
| 166 |
|
| 167 |
line_ending_bytes.clear(); |
| 168 |
for _ in 0..rng.gen_range(1, 11) { |
| 169 |
line_ending_bytes.push(rng.gen()); |
| 170 |
} |
| 171 |
let line_ending = SliceLineEnding::new(&line_ending_bytes); |
| 172 |
|
| 173 |
let _ = do_line_wrap(&mut buf, line_len, &line_ending); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
#[test] |
| 178 |
fn line_wrap_random_single_byte() { |
| 179 |
let mut buf: Vec<u8> = Vec::new(); |
| 180 |
let buf_range = Range::new(10, 1000); |
| 181 |
let line_range = Range::new(10, 100); |
| 182 |
let mut rng = rand::rngs::SmallRng::from_entropy(); |
| 183 |
|
| 184 |
for _ in 0..10_000 { |
| 185 |
buf.clear(); |
| 186 |
|
| 187 |
let buf_len = buf_range.sample(&mut rng); |
| 188 |
let line_len = line_range.sample(&mut rng); |
| 189 |
|
| 190 |
for _ in 0..buf_len { |
| 191 |
buf.push(rng.gen()); |
| 192 |
} |
| 193 |
|
| 194 |
let line_ending = ByteLineEnding::new(rng.gen()); |
| 195 |
|
| 196 |
let _ = do_line_wrap(&mut buf, line_len, &line_ending); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
#[test] |
| 201 |
fn line_wrap_random_two_bytes() { |
| 202 |
let mut buf: Vec<u8> = Vec::new(); |
| 203 |
let buf_range = Range::new(10, 1000); |
| 204 |
let line_range = Range::new(10, 100); |
| 205 |
let mut rng = rand::rngs::SmallRng::from_entropy(); |
| 206 |
|
| 207 |
for _ in 0..10_000 { |
| 208 |
buf.clear(); |
| 209 |
|
| 210 |
let buf_len = buf_range.sample(&mut rng); |
| 211 |
let line_len = line_range.sample(&mut rng); |
| 212 |
|
| 213 |
for _ in 0..buf_len { |
| 214 |
buf.push(rng.gen()); |
| 215 |
} |
| 216 |
|
| 217 |
let line_ending = TwoByteLineEnding::new(rng.gen(), rng.gen()); |
| 218 |
|
| 219 |
let _ = do_line_wrap(&mut buf, line_len, &line_ending); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
fn do_line_wrap<L: LineEnding>(buf: &mut Vec<u8>, line_len: usize, line_ending: &L) -> usize { |
| 224 |
let mut rng = rand::rngs::SmallRng::from_entropy(); |
| 225 |
|
| 226 |
let orig_len = buf.len(); |
| 227 |
let orig_buf = buf.to_vec(); |
| 228 |
|
| 229 |
// Add on extra bytes so we'll have sentinel bytes at the end that shouldn't get changed. |
| 230 |
for _ in 0..(1000 + line_ending.len() * orig_len) { |
| 231 |
buf.push(rng.gen()); |
| 232 |
} |
| 233 |
|
| 234 |
let before_line_wrap = buf.to_vec(); |
| 235 |
|
| 236 |
let params = line_wrap_parameters(orig_len, line_len, line_ending.len()); |
| 237 |
|
| 238 |
let bytes_written = line_wrap::<L>(&mut buf[..], orig_len, line_len, line_ending); |
| 239 |
|
| 240 |
assert_eq!(params.total_line_endings_len, bytes_written); |
| 241 |
assert_eq!(params.lines_with_endings * line_ending.len(), bytes_written); |
| 242 |
assert_eq!(params.total_len, orig_len + bytes_written); |
| 243 |
|
| 244 |
// make sure line_wrap didn't touch anything beyond what it should |
| 245 |
assert_eq!(before_line_wrap[params.total_len..], buf[params.total_len..]); |
| 246 |
|
| 247 |
{ |
| 248 |
// also make sure that line wrapping will fit into a slice no bigger than what it should |
| 249 |
// need |
| 250 |
let mut buf_strict = before_line_wrap.to_vec(); |
| 251 |
buf_strict.truncate(orig_len); |
| 252 |
// fill in some fresh random garbage |
| 253 |
while buf_strict.len() < params.total_len { |
| 254 |
buf_strict.push(rng.gen()); |
| 255 |
} |
| 256 |
let bytes_written_precise_fit = line_wrap::<L>( |
| 257 |
&mut buf_strict, |
| 258 |
orig_len, |
| 259 |
line_len, |
| 260 |
line_ending, |
| 261 |
); |
| 262 |
assert_eq!(params.total_len, buf_strict.len()); |
| 263 |
assert_eq!(bytes_written, bytes_written_precise_fit); |
| 264 |
assert_eq!(&buf[0..(params.total_len)], &buf_strict[..]); |
| 265 |
|
| 266 |
// since we have a copy of the wrapped bytes lying around, remove the endings and see |
| 267 |
// if we get the original |
| 268 |
for line_ending_num in 0..params.lines_with_endings { |
| 269 |
let line_ending_offset = (line_ending_num + 1) * line_len; |
| 270 |
|
| 271 |
for _ in 0..line_ending.len() { |
| 272 |
let _ = buf_strict.remove(line_ending_offset); |
| 273 |
} |
| 274 |
} |
| 275 |
assert_eq!(orig_buf, buf_strict); |
| 276 |
} |
| 277 |
|
| 278 |
buf.truncate(params.total_len); |
| 279 |
|
| 280 |
bytes_written |
| 281 |
} |