2021-09-05 22:31:34 +10: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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
mod utils;
|
|
|
|
|
2021-09-09 01:19:31 +10:00
|
|
|
use opentally::election::{CandidateState, CountState, Election};
|
2021-09-05 22:31:34 +10:00
|
|
|
use opentally::numbers::Rational;
|
|
|
|
use opentally::parser::blt;
|
|
|
|
use opentally::stv;
|
|
|
|
use opentally::ties::TieStrategy;
|
|
|
|
|
2021-09-09 01:19:31 +10:00
|
|
|
/// Insufficient candidates to fill all vacancies
|
2021-09-05 22:31:34 +10:00
|
|
|
#[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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-09 01:19:31 +10:00
|
|
|
/// After bulk exclusion of candidates with no votes, insufficient candidates remain to fill all vacancies
|
2021-09-05 22:31:34 +10:00
|
|
|
#[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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-09 01:19:31 +10:00
|
|
|
|
|
|
|
/// 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);
|
|
|
|
}
|