OpenTally/tests/special_cases.rs
RunasSudo 260dee1bb5
Fix bugs
Fix bug excluding-by-value/source candidates with no votes
Fix bug electing too many candidates if more reach the quota than vacancies remain
Add regression test
2021-09-09 01:24:50 +10:00

116 lines
3.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/>.
*/
mod utils;
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::new("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);
}