use crate::party_command::PartyCommand;
pub type CommandBatch = Vec<PartyCommand>;
pub fn schedule_commands(commands: Vec<PartyCommand>) -> Vec<CommandBatch> {
let mut batches = vec![];
let mut batch = vec![];
for command in commands {
if command.is_parallel {
batch.push(command);
} else {
if !batch.is_empty() {
batches.push(batch);
}
batches.push(vec![command]);
batch = vec![];
}
}
if !batch.is_empty() {
batches.push(batch);
}
batches
}
#[cfg(test)]
pub mod test {
use crate::party_command::PartyCommand;
use super::schedule_commands;
fn parallel_command() -> PartyCommand {
PartyCommand::new("".to_string(), true, None)
}
fn sequential_command() -> PartyCommand {
PartyCommand::new("".to_string(), false, None)
}
#[test]
fn test_scheduler_ending_with_sequential() {
let commands = vec![
parallel_command(),
parallel_command(),
sequential_command(),
parallel_command(),
parallel_command(),
parallel_command(),
sequential_command(),
sequential_command(),
];
let batches = schedule_commands(commands);
assert_eq!(batches.len(), 5);
assert_eq!(batches[0].len(), 2);
assert_eq!(batches[1].len(), 1);
assert_eq!(batches[2].len(), 3);
assert_eq!(batches[3].len(), 1);
assert_eq!(batches[4].len(), 1);
}
#[test]
fn test_scheduler_ending_with_parallel() {
let commands = vec![
parallel_command(),
parallel_command(),
sequential_command(),
parallel_command(),
parallel_command(),
parallel_command(),
];
let batches = schedule_commands(commands);
assert_eq!(batches.len(), 3);
assert_eq!(batches[0].len(), 2);
assert_eq!(batches[1].len(), 1);
assert_eq!(batches[2].len(), 3);
}
}