OpenTally/tests/tests_impl/special_cases.rs

149 lines
4.8 KiB
Rust
Raw Normal View History

/* 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::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
}