/* 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 . */ use crate::utils; use opentally::election::{CandidateState, CountState, Election}; use opentally::numbers::{Fixed, NativeFloat64, Number}; use opentally::parser::blt; use opentally::stv; // Compare ers97old.blt count with result produced by 1987 Hill–Wichmann–Woodall reference implementation #[test] fn meek87_ers97old_float64() { let stv_opts = stv::STVOptionsBuilder::default() //.meek_surplus_tolerance("0.001%".to_string()) .quota(stv::QuotaType::DroopExact) .quota_criterion(stv::QuotaCriterion::GreaterOrEqual) .quota_mode(stv::QuotaMode::DynamicByTotal) .surplus(stv::SurplusMethod::Meek) .immediate_elect(false) .build().unwrap(); assert_eq!(stv_opts.describe::(), "--numbers float64 --quota droop_exact --quota-criterion geq --quota-mode dynamic_by_total --surplus meek --no-immediate-elect"); utils::read_validate_election::("tests/data/ers97old_meek.csv", "tests/data/ers97old.blt", stv_opts, Some(2), &["exhausted", "quota"]); } // Compare ers97old.blt count with result produced by OpenSTV 1.7 "Meek STV" #[test] fn meek06_ers97old_fixed12() { let stv_opts = stv::STVOptionsBuilder::default() .round_surplus_fractions(Some(9)) .round_values(Some(9)) .round_votes(Some(9)) .round_quota(Some(9)) .meek_surplus_tolerance("0.0001".to_string()) .quota_criterion(stv::QuotaCriterion::GreaterOrEqual) .quota_mode(stv::QuotaMode::DynamicByTotal) .surplus(stv::SurplusMethod::Meek) .defer_surpluses(true) .build().unwrap(); Fixed::set_dps(12); assert_eq!(stv_opts.describe::(), "--numbers fixed --decimals 12 --round-surplus-fractions 9 --round-values 9 --round-votes 9 --round-quota 9 --meek-surplus-tolerance 0.0001 --quota-criterion geq --quota-mode dynamic_by_total --surplus meek --defer-surpluses"); // Read BLT let election: Election = blt::parse_path("tests/data/ers97old.blt").expect("Syntax Error"); // Initialise count state let mut state = CountState::new(&election); // Count to completion stv::count_init(&mut state, &stv_opts).unwrap(); while !stv::count_one_stage::(&mut state, &stv_opts).unwrap() {} // Check states and keep values for (candidate, count_card) in state.candidates.iter() { match candidate.name.as_str() { "Smith" => { assert!(count_card.state == CandidateState::Elected); assert_eq!(count_card.keep_value, Some(Fixed::parse("0.452239778"))); } "Carpenter" => { assert!(count_card.state == CandidateState::Elected); assert_eq!(count_card.keep_value, Some(Fixed::parse("0.614726576"))); } "Duke" => { assert!(count_card.state == CandidateState::Elected); assert_eq!(count_card.keep_value, Some(Fixed::parse("0.610901635"))); } "Prince" => { assert!(count_card.state == CandidateState::Elected); assert_eq!(count_card.keep_value, Some(Fixed::parse("0.741971929"))); } "Freeman" => { assert!(count_card.state == CandidateState::Elected); assert_eq!(count_card.keep_value, Some(Fixed::parse("0.715546058"))); } "Vicar" => { assert!(count_card.state == CandidateState::Elected); assert_eq!(count_card.keep_value, Some(Fixed::parse("1.0"))); } _ => {} } } } // Compare ers97old.blt count with result produced by OpenSTV 1.7 "New Zealand Meek STV" (same result as Hill 2006 implementation) #[test] fn meeknz_ers97old_fixed12() { let stv_opts = stv::STVOptionsBuilder::default() .round_surplus_fractions(Some(9)) .round_values(Some(9)) .round_votes(Some(9)) .round_quota(Some(9)) .meek_surplus_tolerance("0.0001".to_string()) .quota_criterion(stv::QuotaCriterion::GreaterOrEqual) .quota_mode(stv::QuotaMode::DynamicByTotal) .surplus(stv::SurplusMethod::Meek) .meek_nz_exclusion(true) .defer_surpluses(true) .build().unwrap(); Fixed::set_dps(12); assert_eq!(stv_opts.describe::(), "--numbers fixed --decimals 12 --round-surplus-fractions 9 --round-values 9 --round-votes 9 --round-quota 9 --meek-surplus-tolerance 0.0001 --quota-criterion geq --quota-mode dynamic_by_total --surplus meek --meek-nz-exclusion --defer-surpluses"); // Read BLT let election: Election = blt::parse_path("tests/data/ers97old.blt").expect("Syntax Error"); // Initialise count state let mut state = CountState::new(&election); // Count to completion stv::count_init(&mut state, &stv_opts).unwrap(); while !stv::count_one_stage::(&mut state, &stv_opts).unwrap() {} // Check states and keep values for (candidate, count_card) in state.candidates.iter() { match candidate.name.as_str() { "Smith" => { assert!(count_card.state == CandidateState::Elected); assert_eq!(count_card.keep_value, Some(Fixed::parse("0.452154292"))); } "Carpenter" => { assert!(count_card.state == CandidateState::Elected); assert_eq!(count_card.keep_value, Some(Fixed::parse("0.614574172"))); } "Duke" => { assert!(count_card.state == CandidateState::Elected); assert_eq!(count_card.keep_value, Some(Fixed::parse("0.610780881"))); } "Prince" => { assert!(count_card.state == CandidateState::Elected); assert_eq!(count_card.keep_value, Some(Fixed::parse("0.741808717"))); } "Freeman" => { assert!(count_card.state == CandidateState::Elected); assert_eq!(count_card.keep_value, Some(Fixed::parse("0.715384286"))); } "Vicar" => { assert!(count_card.state == CandidateState::Elected); assert_eq!(count_card.keep_value, Some(Fixed::parse("1.0"))); } _ => {} } } }