Implement Scottish STV test case
This commit is contained in:
parent
537f1f0011
commit
9d4cac2e89
16
Cargo.lock
generated
16
Cargo.lock
generated
@ -345,6 +345,7 @@ dependencies = [
|
|||||||
"paste",
|
"paste",
|
||||||
"rug",
|
"rug",
|
||||||
"wasm-bindgen",
|
"wasm-bindgen",
|
||||||
|
"xmltree",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -576,3 +577,18 @@ name = "winapi-x86_64-pc-windows-gnu"
|
|||||||
version = "0.4.0"
|
version = "0.4.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "xml-rs"
|
||||||
|
version = "0.8.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b07db065a5cf61a7e4ba64f29e67db906fb1787316516c4e6e5ff0fea1efcd8a"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "xmltree"
|
||||||
|
version = "0.10.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d7d8a75eaf6557bb84a65ace8609883db44a29951042ada9b393151532e41fcb"
|
||||||
|
dependencies = [
|
||||||
|
"xml-rs",
|
||||||
|
]
|
||||||
|
@ -25,6 +25,7 @@ paste = "1.0.5"
|
|||||||
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
|
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
|
||||||
csv = "1.1.6"
|
csv = "1.1.6"
|
||||||
flate2 = "1.0"
|
flate2 = "1.0"
|
||||||
|
xmltree = "0.10.3"
|
||||||
|
|
||||||
[target.'cfg(not(target_arch = "wasm32"))'.dependencies.rug]
|
[target.'cfg(not(target_arch = "wasm32"))'.dependencies.rug]
|
||||||
version = "1.12"
|
version = "1.12"
|
||||||
|
@ -109,7 +109,7 @@ tr.info:last-child td, .bb {
|
|||||||
|
|
||||||
/* Table stripes */
|
/* Table stripes */
|
||||||
|
|
||||||
tr.stage-no td:nth-child(even),
|
tr.stage-no td:nth-child(even):not([rowspan]),
|
||||||
tr.stage-comment td:nth-child(odd),
|
tr.stage-comment td:nth-child(odd),
|
||||||
tr.candidate.transfers td:nth-child(even):not(.elected):not(.excluded),
|
tr.candidate.transfers td:nth-child(even):not(.elected):not(.excluded),
|
||||||
tr.candidate.votes td:nth-child(odd):not(.elected):not(.excluded) {
|
tr.candidate.votes td:nth-child(odd):not(.elected):not(.excluded) {
|
||||||
|
@ -103,7 +103,7 @@ impl From<usize> for Fixed {
|
|||||||
|
|
||||||
impl From<f64> for Fixed {
|
impl From<f64> for Fixed {
|
||||||
fn from(n: f64) -> Self {
|
fn from(n: f64) -> Self {
|
||||||
return Self(IBig::from((n * 10_f64.powi(get_dps() as i32)) as u32))
|
return Self(IBig::from((n * 10_f64.powi(get_dps() as i32)).round() as u32))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
2536
tests/data/linn07.blt
Normal file
2536
tests/data/linn07.blt
Normal file
File diff suppressed because it is too large
Load Diff
11
tests/data/linn07.xml
Normal file
11
tests/data/linn07.xml
Normal file
File diff suppressed because one or more lines are too long
152
tests/scotland.rs
Normal file
152
tests/scotland.rs
Normal file
@ -0,0 +1,152 @@
|
|||||||
|
/* 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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// https://web.archive.org/web/20121004213938/http://www.glasgow.gov.uk/en/YourCouncil/Elections_Voting/Election_Results/ElectionScotland2007/LGWardResults.htm?ward=1&wardname=1%20-%20Linn
|
||||||
|
|
||||||
|
use opentally::election::{CandidateState, CountState, Election};
|
||||||
|
use opentally::numbers::Fixed;
|
||||||
|
use opentally::stv;
|
||||||
|
|
||||||
|
use num_traits::Zero;
|
||||||
|
use xmltree::Element;
|
||||||
|
|
||||||
|
use std::io::{self, BufRead};
|
||||||
|
use std::fs::File;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn scotland_linn07_fixed5() {
|
||||||
|
let stv_opts = stv::STVOptions {
|
||||||
|
round_tvs: Some(5),
|
||||||
|
round_weights: None,
|
||||||
|
round_votes: None,
|
||||||
|
round_quota: Some(0),
|
||||||
|
quota: stv::QuotaType::Droop,
|
||||||
|
quota_criterion: stv::QuotaCriterion::GreaterOrEqual,
|
||||||
|
quota_mode: stv::QuotaMode::Static,
|
||||||
|
surplus: stv::SurplusMethod::WIG,
|
||||||
|
surplus_order: stv::SurplusOrder::BySize,
|
||||||
|
transferable_only: false,
|
||||||
|
exclusion: stv::ExclusionMethod::SingleStage,
|
||||||
|
bulk_exclude: false,
|
||||||
|
defer_surpluses: false,
|
||||||
|
pp_decimals: 5,
|
||||||
|
};
|
||||||
|
Fixed::set_dps(5);
|
||||||
|
|
||||||
|
// Read XML file
|
||||||
|
let file = File::open("tests/data/linn07.xml").expect("IO Error");
|
||||||
|
let root = Element::parse(file).expect("Parse Error");
|
||||||
|
|
||||||
|
let mut candidates: Vec<&Element> = root.children.iter()
|
||||||
|
.filter_map(|n| match n {
|
||||||
|
xmltree::XMLNode::Element(e) => if e.name == "candidate" { Some(e) } else { None },
|
||||||
|
_ => None,
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
let cand_nt = candidates.pop().unwrap();
|
||||||
|
|
||||||
|
// TODO: Validate candidate names
|
||||||
|
|
||||||
|
let num_stages = root.get_child("headerrow").expect("Syntax Error").children.len();
|
||||||
|
|
||||||
|
// Read BLT
|
||||||
|
let file = File::open("tests/data/linn07.blt").expect("IO Error");
|
||||||
|
let file_reader = io::BufReader::new(file);
|
||||||
|
let lines = file_reader.lines();
|
||||||
|
|
||||||
|
let election: Election<Fixed> = Election::from_blt(lines.map(|r| r.expect("IO Error").to_string()).into_iter());
|
||||||
|
|
||||||
|
// Initialise count state
|
||||||
|
let mut state = CountState::new(&election);
|
||||||
|
|
||||||
|
// Distribute first preferences
|
||||||
|
stv::count_init(&mut state, &stv_opts);
|
||||||
|
let mut stage_num = 1;
|
||||||
|
|
||||||
|
for i in 0..num_stages {
|
||||||
|
println!("Stage {}", stage_num);
|
||||||
|
|
||||||
|
// Validate NT
|
||||||
|
let nt_votes = get_cand_stage(cand_nt, i)
|
||||||
|
.get_child("value").unwrap()
|
||||||
|
.get_text().unwrap()
|
||||||
|
.to_string();
|
||||||
|
|
||||||
|
assert!(approx_eq(&(&state.exhausted.votes + &state.loss_fraction.votes), &parse_str(nt_votes)));
|
||||||
|
|
||||||
|
for (candidate, cand_xml) in state.election.candidates.iter().zip(candidates.iter()) {
|
||||||
|
let count_card = state.candidates.get(candidate).unwrap();
|
||||||
|
|
||||||
|
// Validate candidate votes
|
||||||
|
let cand_votes = get_cand_stage(cand_xml, i)
|
||||||
|
.get_child("value").unwrap()
|
||||||
|
.get_text().unwrap()
|
||||||
|
.to_string();
|
||||||
|
let cand_votes = parse_str(cand_votes);
|
||||||
|
assert!(approx_eq(&count_card.votes, &cand_votes), "Failed to validate votes for candidate {}. Expected {:}, got {:}", candidate.name, cand_votes, count_card.votes);
|
||||||
|
|
||||||
|
// Validate candidate states
|
||||||
|
let cand_state = get_cand_stage(cand_xml, i)
|
||||||
|
.get_child("status").unwrap()
|
||||||
|
.get_text().unwrap()
|
||||||
|
.to_string();
|
||||||
|
if cand_state == "Continuing" {
|
||||||
|
assert!(count_card.state == CandidateState::HOPEFUL);
|
||||||
|
} else if cand_state == "Elected" {
|
||||||
|
assert!(count_card.state == CandidateState::ELECTED);
|
||||||
|
} else if cand_state == "Excluded" {
|
||||||
|
assert!(count_card.state == CandidateState::EXCLUDED);
|
||||||
|
} else {
|
||||||
|
panic!("Unknown state descriptor {}", cand_state);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assert_eq!(stv::count_one_stage(&mut state, &stv_opts), false);
|
||||||
|
stage_num += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
assert_eq!(stv::count_one_stage(&mut state, &stv_opts), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_cand_stage(candidate: &Element, idx: usize) -> &Element {
|
||||||
|
return candidate.children.iter()
|
||||||
|
.filter_map(|n| match n {
|
||||||
|
xmltree::XMLNode::Element(e) => if e.name == "stage" { Some(e) } else { None },
|
||||||
|
_ => None,
|
||||||
|
})
|
||||||
|
.nth(idx).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_str(s: String) -> Fixed {
|
||||||
|
if s == "-" { return Fixed::zero(); }
|
||||||
|
let f: f64 = s.parse().expect("Syntax Error");
|
||||||
|
return opentally::numbers::From::from(f);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn approx_eq(a: &Fixed, b: &Fixed) -> bool {
|
||||||
|
// Some tolerance required in equality comparisons, as eSTV incorrectly computes transfers
|
||||||
|
// by value, instead of all at once, resulting in increased rounding error
|
||||||
|
let eq_tol: Fixed = opentally::numbers::From::from(0.0001);
|
||||||
|
|
||||||
|
if a - b > eq_tol {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if b - a > eq_tol {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user