1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
mod rand;
use rand::{Rng};
pub fn get_lorem(bytes: usize, numbered: bool) -> String {
let lorem = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
get_string(bytes, lorem, numbered)
}
pub fn get_string(bytes: usize, template: &str, numbered: bool) -> String {
let mut ret = String::new();
if numbered {
for i in 0..((bytes / template.len()) + 1) {
ret.push_str(format!("{}. {}", i, template).as_str());
}
} else {
while ret.as_bytes().len() < bytes {
ret.push_str(format!("{}", template).as_str());
}
}
ret.truncate(bytes);
ret
}
pub fn get_rng_blob(bytes: usize) -> Vec<u8> {
let mut ret: Vec<u8> = Vec::new();
let mut rng = Rng::default();
while ret.len() < bytes {
ret.push(rng.next());
}
ret
}
pub fn get_seeded_rng_blob(bytes: usize, seed: u8) -> Vec<u8> {
let mut ret: Vec<u8> = Vec::new();
let mut rng = Rng::new(seed);
while ret.len() < bytes {
ret.push(rng.next());
}
ret
}
pub fn get_blob(bytes: usize, template: &[u8]) -> Vec<u8> {
let mut ret: Vec<u8> = Vec::new();
for _ in 0..((bytes / template.len() as usize) + 1) {
ret.extend(template);
}
ret.truncate(bytes);
ret
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn lorem_test() {
let test_val = get_lorem(1024, true);
let test_val_len = test_val.as_bytes().len();
assert_eq!(test_val_len, 1024);
}
#[test]
fn non_lorem_test() {
let template = "test";
let test_val = get_string(1024, template, false);
assert_eq!(test_val.as_bytes().len(), 1024);
assert!(test_val.contains(template));
let empty_test = test_val.replace(template, "");
assert_eq!(empty_test.len(), 0);
}
#[test]
fn blob_test() {
let template = &[1, 2, 3];
let test_val = get_blob(1024, template);
assert!(test_val.len() >= 1024);
let bytes = get_blob(6, template);
assert_eq!(bytes, &[1, 2, 3, 1, 2, 3]);
}
#[test]
fn rng_blob_test() {
let test_val = get_rng_blob(1024);
assert_eq!(test_val.len(), 1024);
}
}