OpenTally/tests/scotland.rs

188 lines
5.8 KiB
Rust
Raw Normal View History

2021-06-10 07:47:09 +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};
use opentally::numbers::{Fixed, GuardedFixed, Number};
2021-06-10 07:47:09 +02:00
use opentally::stv;
use xmltree::Element;
2021-07-28 19:24:51 +02:00
use std::fs::File;
use std::ops;
2021-06-10 07:47:09 +02:00
#[test]
fn scotland_linn07_fixed5() {
let stv_opts = stv::STVOptions {
round_surplus_fractions: Some(5),
round_values: Some(5),
round_votes: Some(5),
2021-06-10 07:47:09 +02:00
round_quota: Some(0),
2021-06-11 13:22:28 +02:00
sum_surplus_transfers: stv::SumSurplusTransfersMode::PerBallot,
meek_surplus_tolerance: String::new(),
2021-06-12 13:16:53 +02:00
normalise_ballots: true,
2021-06-10 07:47:09 +02:00
quota: stv::QuotaType::Droop,
quota_criterion: stv::QuotaCriterion::GreaterOrEqual,
quota_mode: stv::QuotaMode::Static,
ties: vec![],
2021-06-10 07:47:09 +02:00
surplus: stv::SurplusMethod::WIG,
surplus_order: stv::SurplusOrder::BySize,
transferable_only: false,
exclusion: stv::ExclusionMethod::SingleStage,
2021-06-19 17:37:51 +02:00
meek_nz_exclusion: false,
early_bulk_elect: false,
2021-06-10 07:47:09 +02:00
bulk_exclude: false,
defer_surpluses: false,
meek_immediate_elect: false,
constraints_path: None,
constraint_mode: stv::ConstraintMode::GuardDoom,
hide_excluded: false,
sort_votes: false,
2021-06-10 07:47:09 +02:00
pp_decimals: 5,
};
Fixed::set_dps(5);
scotland_linn07::<Fixed>(stv_opts);
}
#[test]
fn scotland_linn07_gfixed5() {
let stv_opts = stv::STVOptions {
round_surplus_fractions: Some(5),
round_values: Some(5),
round_votes: Some(5),
round_quota: Some(0),
sum_surplus_transfers: stv::SumSurplusTransfersMode::PerBallot,
meek_surplus_tolerance: String::new(),
normalise_ballots: true,
quota: stv::QuotaType::Droop,
quota_criterion: stv::QuotaCriterion::GreaterOrEqual,
quota_mode: stv::QuotaMode::Static,
ties: vec![],
surplus: stv::SurplusMethod::WIG,
surplus_order: stv::SurplusOrder::BySize,
transferable_only: false,
exclusion: stv::ExclusionMethod::SingleStage,
2021-06-19 17:37:51 +02:00
meek_nz_exclusion: false,
early_bulk_elect: false,
bulk_exclude: false,
defer_surpluses: false,
meek_immediate_elect: false,
constraints_path: None,
constraint_mode: stv::ConstraintMode::GuardDoom,
hide_excluded: false,
sort_votes: false,
pp_decimals: 5,
};
GuardedFixed::set_dps(5);
scotland_linn07::<GuardedFixed>(stv_opts);
}
fn scotland_linn07<N: Number>(stv_opts: stv::STVOptions)
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-10 07:47:09 +02:00
// Read XML file
let file = File::open("tests/data/linn07.xml").expect("IO Error");
let root = Element::parse(file).expect("Parse Error");
let mut candidates: Vec<&Element> = root.children.iter()
.filter_map(|n| match n {
xmltree::XMLNode::Element(e) => if e.name == "candidate" { Some(e) } else { None },
_ => None,
})
.collect();
let cand_nt = candidates.pop().unwrap();
// TODO: Validate candidate names
let num_stages = root.get_child("headerrow").expect("Syntax Error").children.len();
// Read BLT
2021-07-31 07:24:23 +02:00
let mut election: Election<N> = Election::from_file("tests/data/linn07.blt").expect("Syntax Error");
// !!! FOR SCOTTISH STV !!!
election.normalise_ballots();
2021-06-10 07:47:09 +02:00
// Initialise count state
let mut state = CountState::new(&election);
// Distribute first preferences
stv::count_init(&mut state, &stv_opts).unwrap();
2021-06-10 07:47:09 +02:00
let mut stage_num = 1;
for i in 0..num_stages {
println!("Stage {}", stage_num);
// Validate NT
let nt_votes = get_cand_stage(cand_nt, i)
.get_child("value").unwrap()
.get_text().unwrap()
.to_string();
assert!((&state.exhausted.votes + &state.loss_fraction.votes) == parse_str(nt_votes));
2021-06-10 07:47:09 +02:00
for (candidate, cand_xml) in state.election.candidates.iter().zip(candidates.iter()) {
let count_card = state.candidates.get(candidate).unwrap();
// Validate candidate votes
let cand_votes = get_cand_stage(cand_xml, i)
.get_child("value").unwrap()
.get_text().unwrap()
.to_string();
let cand_votes = parse_str(cand_votes);
assert!(count_card.votes == cand_votes, "Failed to validate votes for candidate {}. Expected {:}, got {:}", candidate.name, cand_votes, count_card.votes);
2021-06-10 07:47:09 +02:00
// Validate candidate states
let cand_state = get_cand_stage(cand_xml, i)
.get_child("status").unwrap()
.get_text().unwrap()
.to_string();
if cand_state == "Continuing" {
2021-06-11 16:50:01 +02:00
assert!(count_card.state == CandidateState::Hopeful);
2021-06-10 07:47:09 +02:00
} else if cand_state == "Elected" {
2021-06-11 16:50:01 +02:00
assert!(count_card.state == CandidateState::Elected);
2021-06-10 07:47:09 +02:00
} else if cand_state == "Excluded" {
2021-06-11 16:50:01 +02:00
assert!(count_card.state == CandidateState::Excluded);
2021-06-10 07:47:09 +02:00
} else {
panic!("Unknown state descriptor {}", cand_state);
}
}
assert_eq!(stv::count_one_stage(&mut state, &stv_opts).unwrap(), false);
2021-06-10 07:47:09 +02:00
stage_num += 1;
}
assert_eq!(stv::count_one_stage(&mut state, &stv_opts).unwrap(), true);
2021-06-10 07:47:09 +02:00
}
fn get_cand_stage(candidate: &Element, idx: usize) -> &Element {
return candidate.children.iter()
.filter_map(|n| match n {
xmltree::XMLNode::Element(e) => if e.name == "stage" { Some(e) } else { None },
_ => None,
})
.nth(idx).unwrap();
}
fn parse_str<N: Number>(s: String) -> N {
if s == "-" { return N::zero(); }
return N::parse(&s);
2021-06-10 07:47:09 +02:00
}