2015-11-16 05:25:01 +00:00
|
|
|
extern crate rand;
|
|
|
|
extern crate regex;
|
|
|
|
|
2020-04-13 18:36:03 +00:00
|
|
|
use self::rand::{thread_rng, Rng};
|
2016-08-06 03:18:34 +00:00
|
|
|
use self::regex::Regex;
|
2015-11-16 05:25:01 +00:00
|
|
|
use common::util::*;
|
2020-04-13 18:36:03 +00:00
|
|
|
use std::fs::{read_dir, File};
|
|
|
|
use std::io::Write;
|
|
|
|
use std::path::Path;
|
2015-11-16 05:25:01 +00:00
|
|
|
|
|
|
|
fn random_chars(n: usize) -> String {
|
2020-04-13 18:36:03 +00:00
|
|
|
thread_rng()
|
|
|
|
.sample_iter(&rand::distributions::Alphanumeric)
|
|
|
|
.take(n)
|
|
|
|
.collect::<String>()
|
2015-11-16 05:25:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Glob {
|
|
|
|
directory: AtPath,
|
|
|
|
regex: Regex,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Glob {
|
|
|
|
fn new(at: &AtPath, directory: &str, regex: &str) -> Glob {
|
|
|
|
Glob {
|
|
|
|
directory: AtPath::new(Path::new(&at.plus_as_string(directory))),
|
|
|
|
regex: Regex::new(regex).unwrap(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn count(&self) -> usize {
|
|
|
|
self.collect().len()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn collect(&self) -> Vec<String> {
|
|
|
|
read_dir(Path::new(&self.directory.subdir))
|
|
|
|
.unwrap()
|
|
|
|
.filter_map(|entry| {
|
|
|
|
let path = entry.unwrap().path();
|
2020-04-13 18:36:03 +00:00
|
|
|
let name = self
|
|
|
|
.directory
|
|
|
|
.minus_as_string(path.as_path().to_str().unwrap_or(""));
|
2015-11-16 05:25:01 +00:00
|
|
|
if self.regex.is_match(&name) {
|
|
|
|
Some(name)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn collate(&self) -> Vec<u8> {
|
|
|
|
let mut files = self.collect();
|
|
|
|
files.sort();
|
|
|
|
let mut data: Vec<u8> = vec![];
|
2016-11-25 19:14:46 +00:00
|
|
|
for name in &files {
|
2015-11-16 05:25:01 +00:00
|
|
|
data.extend(self.directory.read(name).into_bytes());
|
|
|
|
}
|
|
|
|
data
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct RandomFile {
|
|
|
|
inner: File,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RandomFile {
|
|
|
|
fn new(at: &AtPath, name: &str) -> RandomFile {
|
2020-04-13 18:36:03 +00:00
|
|
|
RandomFile {
|
|
|
|
inner: File::create(&at.plus(name)).unwrap(),
|
|
|
|
}
|
2015-11-16 05:25:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn add_bytes(&mut self, bytes: usize) {
|
2020-04-13 18:36:03 +00:00
|
|
|
let chunk_size: usize = if bytes >= 1024 { 1024 } else { bytes };
|
2015-11-16 05:25:01 +00:00
|
|
|
let mut n = bytes;
|
|
|
|
while n > chunk_size {
|
|
|
|
let _ = write!(self.inner, "{}", random_chars(chunk_size));
|
|
|
|
n -= chunk_size;
|
|
|
|
}
|
|
|
|
let _ = write!(self.inner, "{}", random_chars(n));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_lines(&mut self, lines: usize) {
|
|
|
|
let line_size: usize = 32;
|
|
|
|
let mut n = lines;
|
|
|
|
while n > 0 {
|
|
|
|
let _ = writeln!(self.inner, "{}", random_chars(line_size));
|
|
|
|
n -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_split_default() {
|
2016-08-23 11:52:43 +00:00
|
|
|
let (at, mut ucmd) = at_and_ucmd!();
|
2015-11-16 05:25:01 +00:00
|
|
|
let name = "split_default";
|
2017-01-11 23:45:21 +00:00
|
|
|
let glob = Glob::new(&at, ".", r"x[[:alpha:]][[:alpha:]]$");
|
2015-11-16 05:25:01 +00:00
|
|
|
RandomFile::new(&at, name).add_lines(2000);
|
2016-08-13 21:59:21 +00:00
|
|
|
ucmd.args(&[name]).succeeds();
|
2015-11-16 05:25:01 +00:00
|
|
|
assert_eq!(glob.count(), 2);
|
|
|
|
assert_eq!(glob.collate(), at.read(name).into_bytes());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_split_num_prefixed_chunks_by_bytes() {
|
2016-08-23 11:52:43 +00:00
|
|
|
let (at, mut ucmd) = at_and_ucmd!();
|
2015-11-16 05:25:01 +00:00
|
|
|
let name = "split_num_prefixed_chunks_by_bytes";
|
|
|
|
let glob = Glob::new(&at, ".", r"a\d\d$");
|
|
|
|
RandomFile::new(&at, name).add_bytes(10000);
|
2016-08-13 21:59:21 +00:00
|
|
|
ucmd.args(&["-d", "-b", "1000", name, "a"]).succeeds();
|
2015-11-16 05:25:01 +00:00
|
|
|
assert_eq!(glob.count(), 10);
|
|
|
|
assert_eq!(glob.collate(), at.read(name).into_bytes());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_split_str_prefixed_chunks_by_bytes() {
|
2016-08-23 11:52:43 +00:00
|
|
|
let (at, mut ucmd) = at_and_ucmd!();
|
2015-11-16 05:25:01 +00:00
|
|
|
let name = "split_str_prefixed_chunks_by_bytes";
|
2017-01-11 23:45:21 +00:00
|
|
|
let glob = Glob::new(&at, ".", r"b[[:alpha:]][[:alpha:]]$");
|
2015-11-16 05:25:01 +00:00
|
|
|
RandomFile::new(&at, name).add_bytes(10000);
|
2016-08-13 21:59:21 +00:00
|
|
|
ucmd.args(&["-b", "1000", name, "b"]).succeeds();
|
2015-11-16 05:25:01 +00:00
|
|
|
assert_eq!(glob.count(), 10);
|
|
|
|
assert_eq!(glob.collate(), at.read(name).into_bytes());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_split_num_prefixed_chunks_by_lines() {
|
2016-08-23 11:52:43 +00:00
|
|
|
let (at, mut ucmd) = at_and_ucmd!();
|
2015-11-16 05:25:01 +00:00
|
|
|
let name = "split_num_prefixed_chunks_by_lines";
|
|
|
|
let glob = Glob::new(&at, ".", r"c\d\d$");
|
|
|
|
RandomFile::new(&at, name).add_lines(10000);
|
2016-08-13 21:59:21 +00:00
|
|
|
ucmd.args(&["-d", "-l", "1000", name, "c"]).succeeds();
|
2015-11-16 05:25:01 +00:00
|
|
|
assert_eq!(glob.count(), 10);
|
|
|
|
assert_eq!(glob.collate(), at.read(name).into_bytes());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_split_str_prefixed_chunks_by_lines() {
|
2016-08-23 11:52:43 +00:00
|
|
|
let (at, mut ucmd) = at_and_ucmd!();
|
2015-11-16 05:25:01 +00:00
|
|
|
let name = "split_str_prefixed_chunks_by_lines";
|
2017-01-11 23:45:21 +00:00
|
|
|
let glob = Glob::new(&at, ".", r"d[[:alpha:]][[:alpha:]]$");
|
2015-11-16 05:25:01 +00:00
|
|
|
RandomFile::new(&at, name).add_lines(10000);
|
2016-08-13 21:59:21 +00:00
|
|
|
ucmd.args(&["-l", "1000", name, "d"]).succeeds();
|
2015-11-16 05:25:01 +00:00
|
|
|
assert_eq!(glob.count(), 10);
|
|
|
|
assert_eq!(glob.collate(), at.read(name).into_bytes());
|
|
|
|
}
|