2021-09-05 22:31:34 +10:00
|
|
|
/* OpenTally: Open-source election vote counting
|
2022-03-11 15:44:28 +11:00
|
|
|
* Copyright © 2021–2022 Lee Yingtong Li (RunasSudo)
|
2021-09-05 22:31:34 +10:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2022-03-11 13:33:28 +11:00
|
|
|
use crate::utils;
|
|
|
|
|
2021-09-09 01:19:31 +10:00
|
|
|
use opentally::election::{CandidateState, CountState, Election};
|
2022-03-11 13:33:28 +11:00
|
|
|
use opentally::numbers::{Fixed, Rational};
|
2021-09-05 22:31:34 +10:00
|
|
|
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)
|
2021-09-09 13:36:27 +10:00
|
|
|
.ties(vec![TieStrategy::Random(String::from("20210908"))])
|
2021-09-09 01:19:31 +10:00
|
|
|
.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);
|
|
|
|
}
|
2021-09-09 13:36:27 +10:00
|
|
|
|
|
|
|
/// 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
|
|
|
|
}
|
2022-03-11 13:33:28 +11:00
|
|
|
|
|
|
|
/// 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"]);
|
|
|
|
}
|