OpenTally/tests/tests_impl/special_cases.rs
2022-03-11 15:44:28 +11:00

176 lines
5.9 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* OpenTally: Open-source election vote counting
* Copyright © 20212022 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 crate::utils;
use opentally::election::{CandidateState, CountState, Election};
use opentally::numbers::{Fixed, Rational};
use opentally::parser::blt;
use opentally::stv;
use opentally::ties::TieStrategy;
/// Insufficient candidates to fill all vacancies
#[test]
fn insufficient_candidates1() {
let stv_opts = stv::STVOptionsBuilder::default()
.ties(vec![TieStrategy::Random(String::new())])
.build().unwrap();
let mut election: Election<Rational> = blt::parse_path("tests/data/insufficient_candidates1.blt").expect("Syntax Error");
stv::preprocess_election(&mut election, &stv_opts);
let mut state = CountState::new(&election);
stv::count_init(&mut state, &stv_opts).unwrap();
loop {
let result = stv::count_one_stage::<Rational>(&mut state, &stv_opts);
match result {
Ok(done) => { assert_eq!(done, false); }
Err(err) => {
assert_eq!(err, stv::STVError::CannotCompleteCount("Insufficient continuing candidates to complete count"));
break;
}
}
}
}
/// After bulk exclusion of candidates with no votes, insufficient candidates remain to fill all vacancies
#[test]
fn insufficient_candidates2() {
let stv_opts = stv::STVOptionsBuilder::default()
.ties(vec![TieStrategy::Random(String::new())])
.build().unwrap();
let mut election: Election<Rational> = blt::parse_path("tests/data/insufficient_candidates2.blt").expect("Syntax Error");
stv::preprocess_election(&mut election, &stv_opts);
let mut state = CountState::new(&election);
stv::count_init(&mut state, &stv_opts).unwrap();
loop {
let result = stv::count_one_stage::<Rational>(&mut state, &stv_opts);
match result {
Ok(done) => { assert_eq!(done, false); }
Err(err) => {
assert_eq!(err, stv::STVError::CannotCompleteCount("Insufficient continuing candidates to complete count"));
break;
}
}
}
}
/// Tideman A33 election: exclusion of candidate with no votes; more candidates reach the quota than vacancies
#[test]
fn tideman_a33_ers97_rational() {
let stv_opts = stv::STVOptionsBuilder::default()
.round_surplus_fractions(Some(2))
.round_values(Some(2))
.round_votes(Some(2))
.round_quota(Some(2))
.quota(stv::QuotaType::DroopExact)
.quota_criterion(stv::QuotaCriterion::GreaterOrEqual)
.quota_mode(stv::QuotaMode::ERS97)
.ties(vec![TieStrategy::Random(String::from("20210908"))])
.surplus(stv::SurplusMethod::EG)
.transferable_only(true)
.exclusion(stv::ExclusionMethod::ByValue)
.early_bulk_elect(false)
.bulk_exclude(true)
.defer_surpluses(true)
.build().unwrap();
let mut election: Election<Rational> = blt::parse_path("tests/data/A33.blt").expect("Syntax Error");
stv::preprocess_election(&mut election, &stv_opts);
let mut state = CountState::new(&election);
stv::count_init(&mut state, &stv_opts).unwrap();
loop {
let result = stv::count_one_stage::<Rational>(&mut state, &stv_opts);
match result {
Ok(done) => { if done { break; } }
Err(err) => { panic!("{}", err); }
}
}
let num_winners = state.candidates.values().filter(|cc| cc.state == CandidateState::Elected).count();
assert_eq!(num_winners, 3);
}
/// Tideman A34 election: surplus deferred with only 1 hopeful
#[test]
fn tideman_a34_prsa1977_rational() {
let stv_opts = stv::STVOptionsBuilder::default()
.round_surplus_fractions(Some(3))
.round_values(Some(3))
.round_votes(Some(3))
.round_quota(Some(3))
.quota_criterion(stv::QuotaCriterion::GreaterOrEqual)
.ties(vec![TieStrategy::Backwards, TieStrategy::Random(String::from("0"))])
.surplus(stv::SurplusMethod::EG)
.surplus_order(stv::SurplusOrder::ByOrder)
.transferable_only(true)
.exclusion(stv::ExclusionMethod::ParcelsByOrder)
.early_bulk_elect(false)
.build().unwrap();
let mut election: Election<Rational> = blt::parse_path("tests/data/A34.blt").expect("Syntax Error");
stv::preprocess_election(&mut election, &stv_opts);
let mut state = CountState::new(&election);
stv::count_init(&mut state, &stv_opts).unwrap();
loop {
let result = stv::count_one_stage::<Rational>(&mut state, &stv_opts);
match result {
Ok(done) => { if done { break; } }
Err(err) => { panic!("{}", err); }
}
}
// Assert count completes
}
/// Surplus votes need to be transferred at values received
#[test]
fn surplus_values_received() {
let stv_opts = stv::STVOptionsBuilder::default()
.round_surplus_fractions(Some(2))
.round_values(Some(2))
.round_votes(Some(2))
.round_quota(Some(2))
.quota(stv::QuotaType::DroopExact)
.quota_criterion(stv::QuotaCriterion::GreaterOrEqual)
.quota_mode(stv::QuotaMode::ERS97)
.surplus(stv::SurplusMethod::EG)
.transferable_only(true)
.exclusion(stv::ExclusionMethod::ByValue)
.bulk_exclude(true)
.defer_surpluses(true)
.build().unwrap();
Fixed::set_dps(5);
assert_eq!(stv_opts.describe::<Fixed>(), "--numbers fixed --decimals 5 --round-surplus-fractions 2 --round-values 2 --round-votes 2 --round-quota 2 --quota droop_exact --quota-criterion geq --quota-mode ers97 --surplus eg --transferable-only --exclusion by_value --bulk-exclude --defer-surpluses");
utils::read_validate_election::<Fixed>("tests/data/surplus_values_received.csv", "tests/data/surplus_values_received.blt", stv_opts, None, &["exhausted"]);
}