Skip to content

Commit

Permalink
Test sharding
Browse files Browse the repository at this point in the history
  • Loading branch information
sourcefrog committed Dec 16, 2023
1 parent f055b7c commit 4bb2e88
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/shard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ use std::str::FromStr;

use anyhow::{anyhow, ensure, Context, Error};

use crate::Mutant;

/// Select mutants for a particular shard of the total list.
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct Shard {
/// Index modulo n.
Expand Down Expand Up @@ -46,15 +45,15 @@ mod tests {
use super::*;

#[test]
fn test_shard_from_str_valid_input() {
let shard_str = "2/5";
let shard = Shard::from_str(shard_str).unwrap();
fn shard_from_str_valid_input() {
let shard = Shard::from_str("2/5").unwrap();
assert_eq!(shard.k, 2);
assert_eq!(shard.n, 5);
assert_eq!(shard, Shard { k: 2, n: 5 });
}

#[test]
fn test_shard_from_str_invalid_input() {
fn shard_from_str_invalid_input() {
assert_eq!(
Shard::from_str("").unwrap_err().to_string(),
"shard must be k/n"
Expand Down
1 change: 1 addition & 0 deletions tests/cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ mod config;
mod error_value;
mod in_diff;
mod jobs;
mod shard;
mod trace;
#[cfg(windows)]
mod windows;
Expand Down
81 changes: 81 additions & 0 deletions tests/cli/shard.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright 2023 Martin Pool

//! Test `--shard`
use itertools::Itertools;

use super::run;

#[test]
fn shard_divides_all_mutants() {
// For speed, this only lists the mutants, trusting that the mutants
// that are listed are the ones that are run.
let common_args = [
"mutants",
"--list",
"-d",
"testdata/well_tested",
"--no-shuffle",
];
let full_list = String::from_utf8(
run()
.args(common_args)
.assert()
.success()
.get_output()
.stdout
.clone(),
)
.unwrap()
.lines()
.map(ToOwned::to_owned)
.collect_vec();

let n_shards = 5;
let shard_lists = (0..n_shards)
.map(|k| {
String::from_utf8(
run()
.args(common_args)
.args(["--shard", &format!("{}/{}", k, n_shards)])
.assert()
.success()
.get_output()
.stdout
.clone(),
)
.unwrap()
.lines()
.map(ToOwned::to_owned)
.collect_vec()
})
.collect_vec();

// If you combine all the mutants selected for each shard, you get the
// full list, with nothing lost or duplicated, disregarding order.
assert_eq!(
shard_lists.iter().flatten().sorted().collect_vec(),
full_list.iter().sorted().collect_vec()
);

// The shards should also be approximately the same size.
let shard_lens = shard_lists.iter().map(|l| l.len()).collect_vec();
assert!(shard_lens.iter().max().unwrap() - shard_lens.iter().min().unwrap() <= 1);

// And the shards are disjoint
for i in 0..n_shards {
for j in 0..n_shards {
if i != j {
assert!(
shard_lists[i].iter().all(|m| !shard_lists[j].contains(m)),
"shard {} contains {}",
j,
shard_lists[j]
.iter()
.filter(|m| shard_lists[i].contains(m))
.join(", ")
);
}
}
}
}

0 comments on commit 4bb2e88

Please sign in to comment.