Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 42 additions & 28 deletions backend/apportionment/src/candidate_nomination/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ mod structs;
use tracing::{debug, info};

use self::structs::{Candidate, ListCandidateNomination, PreferenceThreshold};
pub use structs::CandidateNominationResult;

use super::{
ApportionmentError, ApportionmentInput, CandidateVotesTrait, ListVotesTrait,
fraction::Fraction,
structs::{CandidateNominationInputType, CandidateNumber, LARGE_COUNCIL_THRESHOLD},
};
use crate::structs::ListNumber;
pub use structs::CandidateNominationResult;

/// Candidate nomination
pub(crate) fn candidate_nomination<'a, T: ApportionmentInput>(
Expand Down Expand Up @@ -97,12 +97,16 @@ fn candidate_nomination_per_list<'a, T: ListVotesTrait>(
seats: u32,
list_votes: &'a [T],
preference_threshold: Fraction,
total_seats: &[u32],
total_seats: &[(ListNumber, u32)],
) -> Result<Vec<ListCandidateNomination<'a, T::Cv>>, ApportionmentError> {
let mut list_candidate_nomination: Vec<ListCandidateNomination<T::Cv>> = vec![];
for (index, list) in list_votes.iter().enumerate() {
let list_seats = total_seats[index];
let candidate_votes = list.candidate_votes();
for list in list_votes {
let (list_number, list_seats) = total_seats
.iter()
.find(|(number, _)| *number == list.number())
.expect("Total seats exists")
.to_owned();
let candidate_votes = &list.candidate_votes();
let candidate_votes_meeting_preference_threshold =
candidate_votes_meeting_preference_threshold(preference_threshold, candidate_votes);
let preferential_candidate_nomination = preferential_candidate_nomination::<T::Cv>(
Expand Down Expand Up @@ -142,8 +146,8 @@ fn candidate_nomination_per_list<'a, T: ListVotesTrait>(
}
};

list_candidate_nomination.push(ListCandidateNomination::<T::Cv> {
list_number: list.number(),
list_candidate_nomination.push(ListCandidateNomination {
list_number,
list_seats,
preferential_candidate_nomination,
other_candidate_nomination,
Expand Down Expand Up @@ -267,8 +271,11 @@ mod tests {
structs::ListNumber,
test_helpers::{
ApportionmentInputMock, CandidateVotesMock,
candidate_nomination_fixture_with_given_list_numbers_and_number_of_seats,
candidate_nomination_fixture_with_given_number_of_seats,
seat_assignment_fixture_with_given_candidate_numbers_and_votes,
seat_assignment_fixture_with_given_candidate_votes,
seat_assignment_fixture_with_given_list_numbers_and_candidate_votes,
},
};

Expand Down Expand Up @@ -353,22 +360,29 @@ mod tests {
#[test]
fn test_with_lt_19_seats_and_preferential_candidate_nomination_and_updated_candidate_ranking() {
let quota = Fraction::new(5104, 15);
let seat_assignment_input = seat_assignment_fixture_with_given_candidate_votes(
15,
vec![
vec![1069, 303, 321, 210, 36, 101, 79, 121, 150, 149, 15, 17],
let seat_assignment_input =
seat_assignment_fixture_with_given_list_numbers_and_candidate_votes(
15,
vec![
452, 39, 81, 76, 35, 109, 29, 25, 17, 6, 18, 9, 25, 30, 5, 18, 3,
(
1,
vec![1069, 303, 321, 210, 36, 101, 79, 121, 150, 149, 15, 17],
),
(
2,
vec![
452, 39, 81, 76, 35, 109, 29, 25, 17, 6, 18, 9, 25, 30, 5, 18, 3,
],
),
(4, vec![229, 63, 65, 9, 10, 58, 29, 50, 6, 11, 37]),
(5, vec![347, 33, 14, 82, 30, 30]),
(7, vec![266, 36, 39, 36, 38, 38]),
],
vec![229, 63, 65, 9, 10, 58, 29, 50, 6, 11, 37],
vec![347, 33, 14, 82, 30, 30],
vec![266, 36, 39, 36, 38, 38],
],
);
let input = candidate_nomination_fixture_with_given_number_of_seats(
);
let input = candidate_nomination_fixture_with_given_list_numbers_and_number_of_seats(
quota,
&seat_assignment_input,
vec![8, 3, 2, 1, 1],
vec![(1, 8), (2, 3), (4, 2), (5, 1), (7, 1)],
);
let result = candidate_nomination::<ApportionmentInputMock>(&input).unwrap();

Expand Down Expand Up @@ -432,14 +446,14 @@ mod tests {
#[test]
fn test_with_lt_19_seats_and_no_preferential_candidate_nomination() {
let quota = Fraction::new(105, 5);
let seat_assignment_input = seat_assignment_fixture_with_given_candidate_votes(
let seat_assignment_input = seat_assignment_fixture_with_given_candidate_numbers_and_votes(
5,
vec![
vec![5, 4, 4, 4, 4],
vec![4, 5, 4, 4, 4],
vec![4, 4, 5, 4, 4],
vec![4, 4, 4, 5, 4],
vec![4, 4, 4, 4, 5],
vec![(1, 5), (3, 4), (4, 4), (6, 4), (7, 4)],
vec![(2, 4), (3, 5), (5, 4), (6, 4), (8, 4)],
vec![(1, 4), (2, 4), (3, 5), (4, 4), (6, 4)],
vec![(2, 4), (3, 4), (4, 4), (5, 5), (7, 4)],
vec![(1, 4), (2, 4), (3, 4), (4, 4), (5, 5)],
],
);
let input = candidate_nomination_fixture_with_given_number_of_seats(
Expand All @@ -455,9 +469,9 @@ mod tests {
quota * Fraction::new(result.preference_threshold.percentage, 100)
);
check_list_candidate_nomination(&result.list_candidate_nomination[0], &[], &[1], &[]);
check_list_candidate_nomination(&result.list_candidate_nomination[1], &[], &[1], &[]);
check_list_candidate_nomination(&result.list_candidate_nomination[1], &[], &[2], &[]);
check_list_candidate_nomination(&result.list_candidate_nomination[2], &[], &[1], &[]);
check_list_candidate_nomination(&result.list_candidate_nomination[3], &[], &[1], &[]);
check_list_candidate_nomination(&result.list_candidate_nomination[3], &[], &[2], &[]);
check_list_candidate_nomination(&result.list_candidate_nomination[4], &[], &[1], &[]);

let lists = input.list_votes;
Expand Down
Loading
Loading