2021-06-01 21:20:38 +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/>.
* /
use opentally ::election ::{ CandidateState , CountState , Election } ;
2021-06-16 20:28:03 +10:00
use opentally ::numbers ::Number ;
2021-08-20 02:16:54 +10:00
use opentally ::parser ::blt ;
2021-06-01 21:20:38 +10:00
use opentally ::stv ;
2021-08-20 02:16:54 +10:00
use csv ::{ ReaderBuilder , StringRecord } ;
2021-06-01 21:20:38 +10:00
use std ::ops ;
#[ allow(dead_code) ] // Suppress false positive
2021-06-16 20:28:03 +10:00
pub fn read_validate_election < N : Number > ( csv_file : & str , blt_file : & str , stv_opts : stv ::STVOptions , cmp_dps : Option < usize > , sum_rows : & [ & str ] )
where
for < ' r > & ' r N : ops ::Add < & ' r N , Output = N > ,
for < ' r > & ' r N : ops ::Sub < & ' r N , Output = N > ,
for < ' r > & ' r N : ops ::Mul < & ' r N , Output = N > ,
for < ' r > & ' r N : ops ::Div < & ' r N , Output = N > ,
for < ' r > & ' r N : ops ::Neg < Output = N > ,
{
2021-06-01 21:20:38 +10:00
// Read CSV file
2021-08-20 02:16:54 +10:00
let reader = ReaderBuilder ::new ( )
2021-06-01 21:20:38 +10:00
. has_headers ( false )
. from_path ( csv_file )
. expect ( " IO Error " ) ;
let records : Vec < StringRecord > = reader . into_records ( ) . map ( | r | r . expect ( " Syntax Error " ) ) . collect ( ) ;
let mut candidates : Vec < & str > = records . iter ( ) . skip ( 2 ) . map ( | r | & r [ 0 ] ) . collect ( ) ;
// Remove exhausted/LBF rows
2021-06-27 22:46:37 +10:00
candidates . truncate ( candidates . len ( ) - sum_rows . len ( ) ) ;
2021-06-01 21:20:38 +10:00
let stages : Vec < usize > = records . first ( ) . unwrap ( ) . iter ( ) . skip ( 1 ) . step_by ( 2 ) . map ( | s | s . parse ( ) . unwrap ( ) ) . collect ( ) ;
2021-06-09 20:09:20 +10:00
// Read BLT
2021-08-20 02:16:54 +10:00
let election : Election < N > = blt ::parse_path ( blt_file ) . expect ( " Syntax Error " ) ;
2021-06-01 21:20:38 +10:00
2021-06-22 16:29:04 +10:00
// Validate candidate names
for ( i , candidate ) in candidates . iter ( ) . enumerate ( ) {
2021-09-10 01:04:02 +10:00
if ! candidate . is_empty ( ) {
assert_eq! ( election . candidates [ i ] . name , * candidate ) ;
}
2021-06-22 16:29:04 +10:00
}
2021-06-16 20:28:03 +10:00
validate_election ( stages , records , election , stv_opts , cmp_dps , sum_rows ) ;
2021-06-01 21:20:38 +10:00
}
2021-06-16 20:28:03 +10:00
pub fn validate_election < N : Number > ( stages : Vec < usize > , records : Vec < StringRecord > , mut election : Election < N > , stv_opts : stv ::STVOptions , cmp_dps : Option < usize > , sum_rows : & [ & str ] )
2021-06-01 21:20:38 +10:00
where
2021-06-16 20:28:03 +10:00
for < ' r > & ' r N : ops ::Add < & ' r N , Output = N > ,
2021-06-01 21:20:38 +10:00
for < ' r > & ' r N : ops ::Sub < & ' r N , Output = N > ,
2021-06-16 13:00:54 +10:00
for < ' r > & ' r N : ops ::Mul < & ' r N , Output = N > ,
2021-06-01 21:20:38 +10:00
for < ' r > & ' r N : ops ::Div < & ' r N , Output = N > ,
for < ' r > & ' r N : ops ::Neg < Output = N > ,
{
2021-09-05 22:31:34 +10:00
stv ::preprocess_election ( & mut election , & stv_opts ) ;
2021-06-16 20:28:03 +10:00
2021-06-01 21:20:38 +10:00
// Initialise count state
let mut state = CountState ::new ( & election ) ;
// Distribute first preferences
2021-06-28 00:56:28 +10:00
stv ::count_init ( & mut state , & stv_opts ) . unwrap ( ) ;
2021-06-01 21:20:38 +10:00
let mut stage_num = 1 ;
for ( idx , stage ) in stages . into_iter ( ) . enumerate ( ) {
while stage_num < stage {
// Step through stages
// Assert count not yet done
2021-06-12 02:09:26 +10:00
assert_eq! ( stv ::count_one_stage ( & mut state , & stv_opts ) . unwrap ( ) , false ) ;
2021-06-01 21:20:38 +10:00
stage_num + = 1 ;
}
2021-07-22 20:31:06 +10:00
validate_stage ( idx , stage_num , & state , & records , cmp_dps , sum_rows ) ;
2021-06-01 21:20:38 +10:00
}
}
2021-07-22 20:31:06 +10:00
fn validate_stage < N : Number > ( idx : usize , stage_num : usize , state : & CountState < N > , records : & Vec < StringRecord > , cmp_dps : Option < usize > , sum_rows : & [ & str ] )
2021-06-16 20:28:03 +10:00
where
for < ' r > & ' r N : ops ::Add < & ' r N , Output = N > ,
{
2021-06-22 16:29:04 +10:00
// Validate stage name
let stage_name = & records . iter ( ) . nth ( 1 ) . unwrap ( ) [ idx * 2 + 1 ] ;
if stage_name . len ( ) > 0 {
2021-09-06 02:43:33 +10:00
assert_eq! ( format! ( " {} " , state . title ) , stage_name ) ;
2021-06-22 16:29:04 +10:00
}
2021-06-16 20:28:03 +10:00
let mut candidate_votes : Vec < Option < N > > = records . iter ( ) . skip ( 2 )
2021-06-29 15:31:38 +10:00
. map ( | r | if r [ idx * 2 + 1 ] . len ( ) > 0 { Some ( N ::parse ( & r [ idx * 2 + 1 ] ) ) } else { None } )
2021-06-16 20:28:03 +10:00
. collect ( ) ;
2021-06-04 22:54:26 +10:00
2021-07-22 20:31:06 +10:00
// Validate candidate votes
for ( candidate , votes ) in state . election . candidates . iter ( ) . zip ( candidate_votes . iter ( ) ) {
let count_card = state . candidates . get ( candidate ) . unwrap ( ) ;
if votes . is_some ( ) {
approx_eq ( & count_card . votes , votes . as_ref ( ) . unwrap ( ) , cmp_dps , stage_num , & format! ( " votes for candidate {} " , candidate . name ) ) ;
}
}
2021-06-04 22:54:26 +10:00
// Validate exhausted/LBF
2021-06-16 20:28:03 +10:00
for kind in sum_rows . iter ( ) . rev ( ) {
let votes = candidate_votes . pop ( ) . unwrap_or ( None ) ;
if let Some ( votes ) = votes {
match kind {
2021-07-22 20:31:06 +10:00
& " exhausted " = > approx_eq ( & state . exhausted . votes , & votes , cmp_dps , stage_num , " exhausted votes " ) ,
& " lbf " = > approx_eq ( & state . loss_fraction . votes , & votes , cmp_dps , stage_num , " LBF " ) ,
& " nt " = > approx_eq ( & ( & state . exhausted . votes + & state . loss_fraction . votes ) , & votes , cmp_dps , stage_num , " NTs " ) ,
& " quota " = > approx_eq ( state . quota . as_ref ( ) . unwrap ( ) , & votes , cmp_dps , stage_num , " quota " ) ,
//&"vre" => approx_eq(state.vote_required_election.as_ref().unwrap(), &votes, cmp_dps, stage_num, "VRE"),
& " vre " = > approx_eq ( state . vote_required_election . as_ref ( ) . unwrap ( ) , & votes , Some ( 2 ) , stage_num , " VRE " ) ,
2021-06-16 20:28:03 +10:00
_ = > panic! ( " Unknown sum_rows " ) ,
}
}
}
2021-06-01 21:20:38 +10:00
// Validate candidate states
let mut candidate_states : Vec < & str > = records . iter ( ) . skip ( 2 ) . map ( | r | & r [ idx * 2 + 2 ] ) . collect ( ) ;
candidate_states . truncate ( candidate_states . len ( ) - 2 ) ;
for ( candidate , candidate_state ) in state . election . candidates . iter ( ) . zip ( candidate_states ) {
let count_card = state . candidates . get ( candidate ) . unwrap ( ) ;
if candidate_state = = " " {
2021-08-01 23:50:15 +10:00
} else if candidate_state = = " H " {
assert! ( count_card . state = = CandidateState ::Hopeful , " Unexpected state for \" {} \" at stage {}, expected \" Hopeful \" , got \" {:?} \" " , candidate . name , stage_num , count_card . state ) ;
} else if candidate_state = = " G " {
assert! ( count_card . state = = CandidateState ::Guarded , " Unexpected state for \" {} \" at stage {}, expected \" Guarded \" , got \" {:?} \" " , candidate . name , stage_num , count_card . state ) ;
2021-06-01 21:20:38 +10:00
} else if candidate_state = = " EL " | | candidate_state = = " PEL " {
2021-08-01 23:50:15 +10:00
assert! ( count_card . state = = CandidateState ::Elected , " Unexpected state for \" {} \" at stage {}, expected \" Elected \" , got \" {:?} \" " , candidate . name , stage_num , count_card . state ) ;
} else if candidate_state = = " D " {
assert! ( count_card . state = = CandidateState ::Doomed , " Unexpected state for \" {} \" at stage {}, expected \" Doomed \" , got \" {:?} \" " , candidate . name , stage_num , count_card . state ) ;
2021-06-01 21:20:38 +10:00
} else if candidate_state = = " EX " | | candidate_state = = " EXCLUDING " {
2021-08-01 23:50:15 +10:00
assert! ( count_card . state = = CandidateState ::Excluded , " Unexpected state for \" {} \" at stage {}, expected \" Excluded \" , got \" {:?} \" " , candidate . name , stage_num , count_card . state ) ;
2021-06-01 21:20:38 +10:00
} else {
panic! ( " Unknown state descriptor {} " , candidate_state ) ;
}
}
}
2021-06-16 20:28:03 +10:00
2021-07-22 20:31:06 +10:00
fn approx_eq < N : Number > ( n1 : & N , n2 : & N , cmp_dps : Option < usize > , stage_num : usize , description : & str ) {
2021-06-16 20:28:03 +10:00
match cmp_dps {
Some ( dps ) = > {
let s1 = format! ( " {:.dps$} " , n1 , dps = dps ) ;
let s2 = format! ( " {:.dps$} " , n2 , dps = dps ) ;
2021-07-22 20:31:06 +10:00
assert! ( s1 = = s2 , " Failed to verify {} for stage {}. Expected {}, got {} " , description , stage_num , s2 , s1 ) ;
2021-06-16 20:28:03 +10:00
}
None = > {
2021-07-22 20:31:06 +10:00
assert! ( n1 = = n2 , " Failed to verify {} for stage {}. Expected {}, got {} " , description , stage_num , n2 , n1 ) ;
2021-06-16 20:28:03 +10:00
}
}
}