OpenTally/tests/utils/mod.rs

164 lines
6.8 KiB
Rust

/* OpenTally: Open-source election vote counting
* Copyright © 2021 Lee Yingtong Li (RunasSudo)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use opentally::election::{CandidateState, CountState, Election};
use opentally::numbers::Number;
use opentally::parser::blt;
use opentally::stv;
use csv::{ReaderBuilder, StringRecord};
use std::ops;
#[allow(dead_code)] // Suppress false positive
pub fn read_validate_election<N: Number>(csv_file: &str, blt_file: &str, stv_opts: stv::STVOptions, cmp_dps: Option<usize>, sum_rows: &[&str])
where
for<'r> &'r N: ops::Add<&'r N, Output=N>,
for<'r> &'r N: ops::Sub<&'r N, Output=N>,
for<'r> &'r N: ops::Mul<&'r N, Output=N>,
for<'r> &'r N: ops::Div<&'r N, Output=N>,
for<'r> &'r N: ops::Neg<Output=N>,
{
// Read CSV file
let reader = ReaderBuilder::new()
.has_headers(false)
.from_path(csv_file)
.expect("IO Error");
let records: Vec<StringRecord> = reader.into_records().map(|r| r.expect("Syntax Error")).collect();
let mut candidates: Vec<&str> = records.iter().skip(2).map(|r| &r[0]).collect();
// Remove exhausted/LBF rows
candidates.truncate(candidates.len() - sum_rows.len());
let stages: Vec<usize> = records.first().unwrap().iter().skip(1).step_by(2).map(|s| s.parse().unwrap()).collect();
// Read BLT
let election: Election<N> = blt::parse_path(blt_file).expect("Syntax Error");
// Validate candidate names
for (i, candidate) in candidates.iter().enumerate() {
if !candidate.is_empty() {
assert_eq!(election.candidates[i].name, *candidate);
}
}
validate_election(stages, records, election, stv_opts, cmp_dps, sum_rows);
}
pub fn validate_election<N: Number>(stages: Vec<usize>, records: Vec<StringRecord>, mut election: Election<N>, stv_opts: stv::STVOptions, cmp_dps: Option<usize>, sum_rows: &[&str])
where
for<'r> &'r N: ops::Add<&'r N, Output=N>,
for<'r> &'r N: ops::Sub<&'r N, Output=N>,
for<'r> &'r N: ops::Mul<&'r N, Output=N>,
for<'r> &'r N: ops::Div<&'r N, Output=N>,
for<'r> &'r N: ops::Neg<Output=N>,
{
stv::preprocess_election(&mut election, &stv_opts);
// Initialise count state
let mut state = CountState::new(&election);
// Distribute first preferences
stv::count_init(&mut state, &stv_opts).unwrap();
let mut stage_num = 1;
for (idx, stage) in stages.into_iter().enumerate() {
while stage_num < stage {
// Step through stages
// Assert count not yet done
assert_eq!(stv::count_one_stage(&mut state, &stv_opts).unwrap(), false);
stage_num += 1;
}
validate_stage(idx, stage_num, &state, &records, cmp_dps, sum_rows);
}
}
fn validate_stage<N: Number>(idx: usize, stage_num: usize, state: &CountState<N>, records: &Vec<StringRecord>, cmp_dps: Option<usize>, sum_rows: &[&str])
where
for<'r> &'r N: ops::Add<&'r N, Output=N>,
{
// Validate stage name
let stage_name = &records.iter().nth(1).unwrap()[idx*2 + 1];
if stage_name.len() > 0 {
assert_eq!(format!("{}", state.title), stage_name);
}
let mut candidate_votes: Vec<Option<N>> = records.iter().skip(2)
.map(|r| if r[idx*2 + 1].len() > 0 { Some(N::parse(&r[idx*2 + 1])) } else { None })
.collect();
// Validate candidate votes
for (candidate, votes) in state.election.candidates.iter().zip(candidate_votes.iter()) {
let count_card = state.candidates.get(candidate).unwrap();
if votes.is_some() {
approx_eq(&count_card.votes, votes.as_ref().unwrap(), cmp_dps, stage_num, &format!("votes for candidate {}", candidate.name));
}
}
// Validate exhausted/LBF
for kind in sum_rows.iter().rev() {
let votes = candidate_votes.pop().unwrap_or(None);
if let Some(votes) = votes {
match kind {
&"exhausted" => approx_eq(&state.exhausted.votes, &votes, cmp_dps, stage_num, "exhausted votes"),
&"lbf" => approx_eq(&state.loss_fraction.votes, &votes, cmp_dps, stage_num, "LBF"),
&"nt" => approx_eq(&(&state.exhausted.votes + &state.loss_fraction.votes), &votes, cmp_dps, stage_num, "NTs"),
&"quota" => approx_eq(state.quota.as_ref().unwrap(), &votes, cmp_dps, stage_num, "quota"),
//&"vre" => approx_eq(state.vote_required_election.as_ref().unwrap(), &votes, cmp_dps, stage_num, "VRE"),
&"vre" => approx_eq(state.vote_required_election.as_ref().unwrap(), &votes, Some(2), stage_num, "VRE"),
_ => panic!("Unknown sum_rows"),
}
}
}
// Validate candidate states
let mut candidate_states: Vec<&str> = records.iter().skip(2).map(|r| &r[idx*2 + 2]).collect();
candidate_states.truncate(candidate_states.len() - 2);
for (candidate, candidate_state) in state.election.candidates.iter().zip(candidate_states) {
let count_card = state.candidates.get(candidate).unwrap();
if candidate_state == "" {
} else if candidate_state == "H" {
assert!(count_card.state == CandidateState::Hopeful, "Unexpected state for \"{}\" at stage {}, expected \"Hopeful\", got \"{:?}\"", candidate.name, stage_num, count_card.state);
} else if candidate_state == "G" {
assert!(count_card.state == CandidateState::Guarded, "Unexpected state for \"{}\" at stage {}, expected \"Guarded\", got \"{:?}\"", candidate.name, stage_num, count_card.state);
} else if candidate_state == "EL" || candidate_state == "PEL" {
assert!(count_card.state == CandidateState::Elected, "Unexpected state for \"{}\" at stage {}, expected \"Elected\", got \"{:?}\"", candidate.name, stage_num, count_card.state);
} else if candidate_state == "D" {
assert!(count_card.state == CandidateState::Doomed, "Unexpected state for \"{}\" at stage {}, expected \"Doomed\", got \"{:?}\"", candidate.name, stage_num, count_card.state);
} else if candidate_state == "EX" || candidate_state == "EXCLUDING" {
assert!(count_card.state == CandidateState::Excluded, "Unexpected state for \"{}\" at stage {}, expected \"Excluded\", got \"{:?}\"", candidate.name, stage_num, count_card.state);
} else {
panic!("Unknown state descriptor {}", candidate_state);
}
}
}
fn approx_eq<N: Number>(n1: &N, n2: &N, cmp_dps: Option<usize>, stage_num: usize, description: &str) {
match cmp_dps {
Some(dps) => {
let s1 = format!("{:.dps$}", n1, dps=dps);
let s2 = format!("{:.dps$}", n2, dps=dps);
assert!(s1 == s2, "Failed to verify {} for stage {}. Expected {}, got {}", description, stage_num, s2, s1);
}
None => {
assert!(n1 == n2, "Failed to verify {} for stage {}. Expected {}, got {}", description, stage_num, n2, n1);
}
}
}