Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
milselarch committed May 3, 2024
1 parent a17e683 commit 431fe4d
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# trie_rcv
Ranked Choice Voting (RCV) implementation using Tries in Rust
[https://crates.io/crates/trie_rcv](https://crates.io/crates/trie_rcv)
Ranked Choice Voting (RCV) implementation using Tries in Rust.
RCV differs from normal first past the post voting in that voters are allowed
to rank candidates from most to least preferable. To determine the winner of an RCV election, the
least votes for the least popular candidate(s) are transferred to their next choice until
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ impl RankedChoiceVoteTrie {
}
}

pub fn set_elimination_strategy(&mut self, strategy: EliminationStrategies) {
self.elimination_strategy = strategy;
}

pub fn insert_votes(&mut self, votes: Vec<VoteStruct>) {
for vote in votes {
self.insert_vote(vote);
Expand Down
65 changes: 64 additions & 1 deletion tests/integration_test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use trie_rcv;
use trie_rcv::RankedChoiceVoteTrie;
use trie_rcv::{EliminationStrategies, RankedChoiceVoteTrie};
use trie_rcv::vote::{SpecialVotes, VoteStruct};

const WITHOLD_VOTE_VAL: i32 = SpecialVotes::WITHHOLD.to_int();
Expand Down Expand Up @@ -70,4 +70,67 @@ fn test_zero_vote_end() {
"Candidate 1's vote should not count after round 1, ",
"no one should have majority"
]);
}

#[test]
fn test_zero_nil_votes_only() {
let votes = VoteStruct::from_vectors(&vec![
vec![WITHOLD_VOTE_VAL],
vec![WITHOLD_VOTE_VAL],
vec![WITHOLD_VOTE_VAL],
vec![ABSTAIN_VOTE_VAL]
]).unwrap();

let rcv = RankedChoiceVoteTrie::new();
let winner = rcv.run_election(votes);
println!("WINNER = {:?}", winner);
assert_eq!(winner, None);
}

#[test]
fn test_null_vote_end() {
let votes = VoteStruct::from_vectors(&vec![
vec![1, ABSTAIN_VOTE_VAL],
vec![2, 1],
vec![3, 2],
vec![3]
]).unwrap();

let rcv = RankedChoiceVoteTrie::new();
let winner = rcv.run_election(votes);
println!("WINNER = {:?}", winner);
assert_eq!(winner, Some(3));
}

#[test]
fn test_dowdall_elimination() {
let votes = VoteStruct::from_vectors(&vec![
vec![1, 6, 15],
vec![1, 2, 6, 15, 5, 4, 7, 3, 11],
vec![6, 15, 1, 11, 10, 16, 17, 8, 2, 3, 5, 7],
vec![9, 8, 6, 11, 13, 3, 1],
vec![13, 14, 16, 6, 3, 4, 5, 2, 1, 8, 9]
]).unwrap();

let rcv = RankedChoiceVoteTrie::new();
let winner = rcv.run_election(votes);
println!("WINNER = {:?}", winner);
assert_eq!(winner, Some(6));
}

#[test]
fn test_all_elimination() {
let votes = VoteStruct::from_vectors(&vec![
vec![1, 6, 15],
vec![1, 2, 6, 15, 5, 4, 7, 3, 11],
vec![6, 15, 1, 11, 10, 16, 17, 8, 2, 3, 5, 7],
vec![9, 8, 6, 11, 13, 3, 1],
vec![13, 14, 16, 6, 3, 4, 5, 2, 1, 8, 9]
]).unwrap();

let mut rcv = RankedChoiceVoteTrie::new();
rcv.set_elimination_strategy(EliminationStrategies::EliminateAll);
let winner = rcv.run_election(votes);
println!("WINNER = {:?}", winner);
assert_eq!(winner, Some(1));
}

0 comments on commit 431fe4d

Please sign in to comment.