2021-06-01 13:20:38 +02:00
|
|
|
/* 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};
|
2021-06-16 12:28:03 +02:00
|
|
|
use opentally::numbers::Number;
|
2021-06-01 13:20:38 +02:00
|
|
|
use opentally::stv;
|
|
|
|
|
|
|
|
use csv::StringRecord;
|
|
|
|
|
|
|
|
use std::fs::File;
|
|
|
|
use std::io::{self, BufRead};
|
|
|
|
use std::ops;
|
|
|
|
|
|
|
|
#[allow(dead_code)] // Suppress false positive
|
2021-06-16 12:28:03 +02:00
|
|
|
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>,
|
|
|
|
{
|
2021-06-01 13:20:38 +02:00
|
|
|
// Read CSV file
|
|
|
|
let reader = csv::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
|
2021-06-27 14:46:37 +02:00
|
|
|
candidates.truncate(candidates.len() - sum_rows.len());
|
2021-06-01 13:20:38 +02:00
|
|
|
|
|
|
|
let stages: Vec<usize> = records.first().unwrap().iter().skip(1).step_by(2).map(|s| s.parse().unwrap()).collect();
|
|
|
|
|
2021-06-09 12:09:20 +02:00
|
|
|
// Read BLT
|
2021-06-01 13:20:38 +02:00
|
|
|
let file = File::open(blt_file).expect("IO Error");
|
|
|
|
let file_reader = io::BufReader::new(file);
|
|
|
|
let lines = file_reader.lines();
|
|
|
|
|
2021-06-16 12:28:03 +02:00
|
|
|
let election: Election<N> = Election::from_blt(lines.map(|r| r.expect("IO Error").to_string()).into_iter());
|
2021-06-01 13:20:38 +02:00
|
|
|
|
2021-06-22 08:29:04 +02:00
|
|
|
// Validate candidate names
|
|
|
|
for (i, candidate) in candidates.iter().enumerate() {
|
|
|
|
assert_eq!(election.candidates[i].name, *candidate);
|
|
|
|
}
|
|
|
|
|
2021-06-16 12:28:03 +02:00
|
|
|
validate_election(stages, records, election, stv_opts, cmp_dps, sum_rows);
|
2021-06-01 13:20:38 +02:00
|
|
|
}
|
|
|
|
|
2021-06-16 12:28:03 +02:00
|
|
|
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])
|
2021-06-01 13:20:38 +02:00
|
|
|
where
|
2021-06-16 12:28:03 +02:00
|
|
|
for<'r> &'r N: ops::Add<&'r N, Output=N>,
|
2021-06-01 13:20:38 +02:00
|
|
|
for<'r> &'r N: ops::Sub<&'r N, Output=N>,
|
2021-06-16 05:00:54 +02:00
|
|
|
for<'r> &'r N: ops::Mul<&'r N, Output=N>,
|
2021-06-01 13:20:38 +02:00
|
|
|
for<'r> &'r N: ops::Div<&'r N, Output=N>,
|
|
|
|
for<'r> &'r N: ops::Neg<Output=N>,
|
|
|
|
{
|
2021-06-16 12:28:03 +02:00
|
|
|
if stv_opts.normalise_ballots {
|
|
|
|
election.normalise_ballots();
|
|
|
|
}
|
|
|
|
|
2021-06-01 13:20:38 +02:00
|
|
|
// Initialise count state
|
|
|
|
let mut state = CountState::new(&election);
|
|
|
|
|
|
|
|
// Distribute first preferences
|
2021-06-27 16:56:28 +02:00
|
|
|
stv::count_init(&mut state, &stv_opts).unwrap();
|
2021-06-01 13:20:38 +02:00
|
|
|
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
|
2021-06-11 18:09:26 +02:00
|
|
|
assert_eq!(stv::count_one_stage(&mut state, &stv_opts).unwrap(), false);
|
2021-06-01 13:20:38 +02:00
|
|
|
stage_num += 1;
|
|
|
|
}
|
2021-06-16 12:28:03 +02:00
|
|
|
validate_stage(idx, &state, &records, cmp_dps, sum_rows);
|
2021-06-01 13:20:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-16 12:28:03 +02:00
|
|
|
fn validate_stage<N: Number>(idx: 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>,
|
|
|
|
{
|
2021-06-22 08:29:04 +02:00
|
|
|
// Validate stage name
|
|
|
|
let stage_name = &records.iter().nth(1).unwrap()[idx*2 + 1];
|
|
|
|
if stage_name.len() > 0 {
|
|
|
|
match state.kind {
|
|
|
|
Some(kind) => assert_eq!(format!("{} {}", kind, state.title), stage_name),
|
|
|
|
None => assert_eq!(state.title, stage_name),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-16 12:28:03 +02:00
|
|
|
let mut candidate_votes: Vec<Option<N>> = records.iter().skip(2)
|
2021-06-29 07:31:38 +02:00
|
|
|
.map(|r| if r[idx*2 + 1].len() > 0 { Some(N::parse(&r[idx*2 + 1])) } else { None })
|
2021-06-16 12:28:03 +02:00
|
|
|
.collect();
|
2021-06-04 14:54:26 +02:00
|
|
|
|
|
|
|
// Validate exhausted/LBF
|
2021-06-16 12:28:03 +02:00
|
|
|
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, idx, "exhausted votes"),
|
|
|
|
&"lbf" => approx_eq(&state.loss_fraction.votes, &votes, cmp_dps, idx, "LBF"),
|
|
|
|
&"nt" => approx_eq(&(&state.exhausted.votes + &state.loss_fraction.votes), &votes, cmp_dps, idx, "NTs"),
|
|
|
|
&"quota" => approx_eq(state.quota.as_ref().unwrap(), &votes, cmp_dps, idx, "quota"),
|
2021-07-21 05:43:16 +02:00
|
|
|
//&"vre" => approx_eq(state.vote_required_election.as_ref().unwrap(), &votes, cmp_dps, idx, "VRE"),
|
|
|
|
&"vre" => approx_eq(state.vote_required_election.as_ref().unwrap(), &votes, Some(2), idx, "VRE"),
|
2021-06-16 12:28:03 +02:00
|
|
|
_ => panic!("Unknown sum_rows"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-01 13:20:38 +02:00
|
|
|
|
2021-06-04 14:54:26 +02:00
|
|
|
// Validate candidate votes
|
2021-06-01 13:20:38 +02:00
|
|
|
for (candidate, votes) in state.election.candidates.iter().zip(candidate_votes) {
|
|
|
|
let count_card = state.candidates.get(candidate).unwrap();
|
2021-06-22 08:28:38 +02:00
|
|
|
if votes.is_some() {
|
|
|
|
let votes = votes.unwrap();
|
|
|
|
approx_eq(&count_card.votes, &votes, cmp_dps, idx, &format!("votes for candidate {}", candidate.name));
|
|
|
|
}
|
2021-06-01 13:20:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 == "" {
|
2021-06-22 08:28:38 +02:00
|
|
|
}
|
|
|
|
else if candidate_state == "H" {
|
2021-06-27 13:57:24 +02:00
|
|
|
assert!(count_card.state == CandidateState::Hopeful, "Unexpected state for \"{}\" at index {}", candidate.name, idx);
|
2021-06-01 13:20:38 +02:00
|
|
|
} else if candidate_state == "EL" || candidate_state == "PEL" {
|
2021-06-27 13:57:24 +02:00
|
|
|
assert!(count_card.state == CandidateState::Elected, "Unexpected state for \"{}\" at index {}", candidate.name, idx);
|
2021-06-01 13:20:38 +02:00
|
|
|
} else if candidate_state == "EX" || candidate_state == "EXCLUDING" {
|
2021-06-27 13:57:24 +02:00
|
|
|
assert!(count_card.state == CandidateState::Excluded, "Unexpected state for \"{}\" at index {}", candidate.name, idx);
|
2021-06-01 13:20:38 +02:00
|
|
|
} else {
|
|
|
|
panic!("Unknown state descriptor {}", candidate_state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-16 12:28:03 +02:00
|
|
|
|
|
|
|
fn approx_eq<N: Number>(n1: &N, n2: &N, cmp_dps: Option<usize>, idx: 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 idx {}. Expected {}, got {}", description, idx, s2, s1);
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
assert!(n1 == n2, "Failed to verify {} for idx {}. Expected {}, got {}", description, idx, n2, n1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|