Skip to content

Commit

Permalink
chore: add tests for traits WithVotes and WithSeats
Browse files Browse the repository at this point in the history
  • Loading branch information
edugzlez committed Oct 5, 2024
1 parent f3ac430 commit 77c7785
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "electosim"
version = "0.2.2"
version = "0.2.3"
edition = "2021"
authors = ["Eduardo González Vaquero <edugonzalezvaq@gmail.com>"]
description = "Library to compute electoral methods (as D'Hondt) and simulate elections"
Expand Down
78 changes: 73 additions & 5 deletions src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ pub trait WithVotes {

/// Sets the number of votes.
fn set_votes(&mut self, votes: u32);

/// Increases the number of votes by a given amount.
fn increase_votes(&mut self, n: i32) {
let current_votes = self.get_votes();
if n < 0 {
self.set_votes(current_votes.saturating_sub(n.abs() as u32));
} else {
self.set_votes(current_votes + n as u32);
}
}
}

/// A trait for objects that have seats.
Expand All @@ -16,9 +26,13 @@ pub trait WithSeats {
fn set_seats(&mut self, seats: u16);

/// Increases the number of seats by a given amount.
fn increase_seats(&mut self, n: u16) {
let actual_seats = self.get_seats();
self.set_seats(actual_seats + n);
fn increase_seats(&mut self, n: i16) {
let current_seats = self.get_seats();
if n < 0 {
self.set_seats(current_seats.saturating_sub(n.abs() as u16));
} else {
self.set_seats(current_seats + n as u16);
}
}
}

Expand Down Expand Up @@ -54,6 +68,7 @@ mod tests {

struct Candidate {
votes: u32,
seats: u16,
}

impl WithVotes for Candidate {
Expand All @@ -66,13 +81,66 @@ mod tests {
}
}

impl WithSeats for Candidate {
fn get_seats(&self) -> u16 {
self.seats
}

fn set_seats(&mut self, seats: u16) {
self.seats = seats;
}
}

#[test]
fn test_with_votes() {
let mut candidate = Candidate { votes: 1000 };
let mut candidate = Candidate {
votes: 1000,
seats: 0,
};

assert_eq!(candidate.get_votes(), 1000);
candidate.set_votes(2000);
assert_eq!(candidate.get_votes(), 2000);

candidate.increase_votes(100);
assert_eq!(candidate.get_votes(), 2100);

candidate.increase_votes(-200);
assert_eq!(candidate.get_votes(), 1900);
}

#[test]
fn test_with_seats() {
let mut candidate = Candidate {
votes: 1000,
seats: 50,
};

assert_eq!(candidate.get_seats(), 50);
candidate.set_seats(100);
assert_eq!(candidate.get_seats(), 100);

candidate.increase_seats(10);
assert_eq!(candidate.get_seats(), 110);

candidate.increase_seats(-20);
assert_eq!(candidate.get_seats(), 90);
}

#[test]
fn test_with_box() {
let mut candidate = Candidate {
votes: 1000,
seats: 50,
};

let mut boxed_candidate = Box::new(&mut candidate);

assert_eq!(boxed_candidate.get_votes(), 1000);
boxed_candidate.set_votes(2000);
assert_eq!(candidate.get_votes(), 2000);
assert_eq!(boxed_candidate.get_votes(), 2000);

boxed_candidate.set_seats(100);
assert_eq!(boxed_candidate.get_seats(), 100);
}
}

0 comments on commit 77c7785

Please sign in to comment.