Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bblodfon committed Aug 21, 2024
1 parent 61c0907 commit 8654a38
Showing 1 changed file with 29 additions and 19 deletions.
48 changes: 29 additions & 19 deletions tests/testthat/test_voting_methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,20 @@ w_equal = rep(1, n_voters)

# small data example
cand2 = paste0("V", seq_len(5))
# "V3" candidate in all sets, "V1" in half, "V2", "V4" once, "V5" nowhere!
vot2 = list(
# "V3" candidate in all sets, "V1" in half, "V2", "V4" once, "V5" nowhere!
c("V3", "V1", "V2"),
c("V3", "V1"),
c("V3", "V4"),
c("V3"))
c("V3")
)
# edge case: all voters voted the same candidates, "V4" and "V5" are nowhere!
vot3 = list(
c("V3", "V1", "V2"),
c("V3", "V1", "V2"),
c("V3", "V1", "V2"),
c("V3", "V1", "V2")
)
w2 = runif(length(vot2))
w2_equal = rep(1, length(vot2))

Expand Down Expand Up @@ -51,6 +59,12 @@ test_that("approval voting", {
expect_equal(av2_equalw$feature[5], "V5")
expect_equal(av2_equalw$score, c(4, 2, 1, 1, 0))
expect_equal(av2_equalw$norm_score, c(1, 0.5, 0.25, 0.25, 0))

# tie breaking
av3 = approval_voting(vot3, cand2, w2)
expect_contains(av3$feature[1:3], c("V1", "V2", "V3"))
expect_true(length(unique(av3$score[1:3])) == 1) # all scores the same
expect_contains(av3$feature[4:5], c("V4", "V5"))
})

test_that("satisfaction approval voting", {
Expand Down Expand Up @@ -82,16 +96,22 @@ test_that("satisfaction approval voting", {

sav2_equalw = satisfaction_approval_voting(vot2, cand2, w2_equal)
expect_equal(sav2_equalw$feature, c("V3", "V1", "V4", "V2", "V5"))

# tie breaking
sav3 = satisfaction_approval_voting(vot3, cand2, w2)
expect_contains(sav3$feature[1:3], c("V1", "V2", "V3"))
expect_true(length(unique(sav3$score[1:3])) == 1) # all scores the same
expect_contains(sav3$feature[4:5], c("V4", "V5"))
})

test_that("sequential proportional approval voting", {
# large data
size = 2 # just get top 2 ranked features
size = 5 # get top 5 ranked features
sp = seq_proportional_approval_voting(vot, cand, w, size)
expect_data_table(sp, nrows = size, ncols = 2)
expect_setequal(colnames(sp), c("feature", "borda_score"))
sp1 = seq_proportional_approval_voting(vot, cand, w, committee_size = 1)
expect_equal(sp$feature[1], sp1$feature[1]) # house monotonicity
sp1 = seq_proportional_approval_voting(vot, cand, w, committee_size = 3)
expect_equal(sp$feature[1:3], sp1$feature[1:3]) # house monotonicity

spe = seq_proportional_approval_voting(vot, cand, w_equal, size)
expect_data_table(spe, nrows = size, ncols = 2)
Expand All @@ -110,19 +130,9 @@ test_that("sequential proportional approval voting", {
expect_setequal(colnames(seq_pav2_equal), c("feature", "borda_score"))
expect_equal(seq_pav2_equal$borda_score[1], 1)
expect_equal(seq_pav2_equal$borda_score[length(cand2)], 0)
})

test_that("reverse sequential proportional approval voting", {
# small data
rs_pav = revseq_proportional_approval_voting(vot2, cand2, w2)
expect_data_table(rs_pav, nrows = length(cand2), ncols = 2)
expect_setequal(colnames(rs_pav), c("feature", "borda_score"))
expect_equal(rs_pav$borda_score[1], 1)
expect_equal(rs_pav$borda_score[length(cand2)], 0)

rs_pav_equal = seq_proportional_approval_voting(vot2, cand2, w2_equal)
expect_data_table(rs_pav_equal, nrows = length(cand2), ncols = 2)
expect_setequal(colnames(rs_pav_equal), c("feature", "borda_score"))
expect_equal(rs_pav_equal$borda_score[1], 1)
expect_equal(rs_pav_equal$borda_score[length(cand2)], 0)
# tie breaking
seq_pav3 = seq_proportional_approval_voting(vot3, cand2, w2)
expect_contains(seq_pav3$feature[1:3], c("V1", "V2", "V3"))
expect_contains(seq_pav3$feature[4:5], c("V4", "V5"))
})

0 comments on commit 8654a38

Please sign in to comment.