mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-29 12:07:46 +00:00
Add test
This commit is contained in:
parent
da94e35044
commit
dad7761be9
3 changed files with 83 additions and 20 deletions
|
@ -74,6 +74,18 @@ impl ExternalSorter {
|
|||
self
|
||||
}
|
||||
|
||||
/// Sorts a given iterator, returning a new iterator with items
|
||||
pub fn sort<T, I>(
|
||||
&self,
|
||||
iterator: I,
|
||||
) -> Result<SortedIterator<T, impl Fn(&T, &T) -> Ordering + Send + Sync>, Error>
|
||||
where
|
||||
T: Sortable + Ord,
|
||||
I: Iterator<Item = T>,
|
||||
{
|
||||
self.sort_by(iterator, |a, b| a.cmp(b))
|
||||
}
|
||||
|
||||
/// Sorts a given iterator with a comparator function, returning a new iterator with items
|
||||
pub fn sort_by<T, I, F>(&self, iterator: I, cmp: F) -> Result<SortedIterator<T, F>, Error>
|
||||
where
|
||||
|
@ -255,3 +267,63 @@ impl<T: Sortable, F: Fn(&T, &T) -> Ordering> Iterator for SortedIterator<T, F> {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod test {
|
||||
use super::*;
|
||||
|
||||
use byteorder::{ReadBytesExt, WriteBytesExt};
|
||||
|
||||
#[test]
|
||||
fn test_smaller_than_segment() {
|
||||
let sorter = ExternalSorter::new();
|
||||
let data: Vec<u32> = (0..100u32).collect();
|
||||
let data_rev: Vec<u32> = data.iter().rev().cloned().collect();
|
||||
|
||||
let sorted_iter = sorter.sort(data_rev.into_iter()).unwrap();
|
||||
|
||||
// should not have used any segments (all in memory)
|
||||
assert_eq!(sorted_iter.segments_file.len(), 0);
|
||||
let sorted_data: Vec<u32> = sorted_iter.collect();
|
||||
|
||||
assert_eq!(data, sorted_data);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_multiple_segments() {
|
||||
let sorter = ExternalSorter::new().with_segment_size(100);
|
||||
let data: Vec<u32> = (0..1000u32).collect();
|
||||
|
||||
let data_rev: Vec<u32> = data.iter().rev().cloned().collect();
|
||||
let sorted_iter = sorter.sort(data_rev.into_iter()).unwrap();
|
||||
assert_eq!(sorted_iter.segments_file.len(), 10);
|
||||
|
||||
let sorted_data: Vec<u32> = sorted_iter.collect();
|
||||
assert_eq!(data, sorted_data);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parallel() {
|
||||
let sorter = ExternalSorter::new()
|
||||
.with_segment_size(100)
|
||||
.with_parallel_sort();
|
||||
let data: Vec<u32> = (0..1000u32).collect();
|
||||
|
||||
let data_rev: Vec<u32> = data.iter().rev().cloned().collect();
|
||||
let sorted_iter = sorter.sort(data_rev.into_iter()).unwrap();
|
||||
assert_eq!(sorted_iter.segments_file.len(), 10);
|
||||
|
||||
let sorted_data: Vec<u32> = sorted_iter.collect();
|
||||
assert_eq!(data, sorted_data);
|
||||
}
|
||||
|
||||
impl Sortable for u32 {
|
||||
fn encode<W: Write>(&self, writer: &mut W) {
|
||||
writer.write_u32::<byteorder::LittleEndian>(*self).unwrap();
|
||||
}
|
||||
|
||||
fn decode<R: Read>(reader: &mut R) -> Option<u32> {
|
||||
reader.read_u32::<byteorder::LittleEndian>().ok()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -90,7 +90,7 @@ static NEGATIVE: char = '-';
|
|||
static POSITIVE: char = '+';
|
||||
|
||||
static DEFAULT_TMPDIR: &str = r"/tmp";
|
||||
// 16GB buffer for Vec<Lines> before we dump to disk
|
||||
// 16GB buffer for Vec<Line> before we dump to disk
|
||||
static DEFAULT_BUF_SIZE: usize = 16000000000;
|
||||
|
||||
#[derive(Eq, Ord, PartialEq, PartialOrd, Clone)]
|
||||
|
@ -890,11 +890,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
|||
.map(String::from)
|
||||
.unwrap_or(format!("{}", DEFAULT_BUF_SIZE));
|
||||
|
||||
if human_numeric_convert(&input) < 128000 {
|
||||
panic!("sort will not operate with less than 128K of memory.");
|
||||
} else {
|
||||
human_numeric_convert(&input)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -8,6 +8,16 @@ fn test_helper(file_name: &str, args: &str) {
|
|||
.stdout_is_fixture(format!("{}.expected", file_name));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_larger_than_specified_segment() {
|
||||
new_ucmd!()
|
||||
.arg("-n")
|
||||
.arg("-S 100")
|
||||
.arg("numeric_unsorted_ints.txt")
|
||||
.succeeds()
|
||||
.stdout_is_fixture(format!("{}", "numeric_unsorted_ints.expected"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_months_whitespace() {
|
||||
test_helper("months-whitespace", "-M");
|
||||
|
@ -100,21 +110,6 @@ fn test_random_shuffle_two_runs_not_the_same() {
|
|||
assert_ne!(result, unexpected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_random_shuffle_contains_two_runs_not_the_same() {
|
||||
// check to verify that two random shuffles are not equal; this has the
|
||||
// potential to fail in the unlikely event that random order is the same
|
||||
// as the starting order, or if both random sorts end up having the same order.
|
||||
const FILE: &'static str = "default_unsorted_ints.expected";
|
||||
let (at, _ucmd) = at_and_ucmd!();
|
||||
let result = new_ucmd!().arg("-R").arg(FILE).run().stdout_move_str();
|
||||
let expected = at.read(FILE);
|
||||
let unexpected = new_ucmd!().arg("-R").arg(FILE).run().stdout_move_str();
|
||||
|
||||
assert_ne!(result, expected);
|
||||
assert_ne!(result, unexpected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_numeric_floats_and_ints() {
|
||||
test_helper("numeric_floats_and_ints", "-n");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue