|
|
@ -2,12 +2,26 @@ package fr.mieuxvoter.mj; |
|
|
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.*; |
|
|
|
|
|
|
|
import java.util.stream.Stream; |
|
|
|
|
|
|
|
import javax.json.JsonArray; |
|
|
|
import javax.json.JsonObject; |
|
|
|
import javax.json.JsonValue; |
|
|
|
|
|
|
|
import org.junit.jupiter.api.DisplayName; |
|
|
|
import org.junit.jupiter.api.Test; |
|
|
|
import org.junit.jupiter.params.ParameterizedTest; |
|
|
|
import org.junit.jupiter.params.provider.Arguments; |
|
|
|
import org.junit.jupiter.params.provider.MethodSource; |
|
|
|
|
|
|
|
import net.joshka.junit.json.params.JsonFileSource; |
|
|
|
//import net.joshka.junit.json.params.JsonObject; |
|
|
|
|
|
|
|
|
|
|
|
class MajorityJudgmentDeliberatorTest { |
|
|
|
|
|
|
|
@Test |
|
|
|
void testDemoUsage() { |
|
|
|
public void testDemoUsage() { |
|
|
|
DeliberatorInterface mj = new MajorityJudgmentDeliberator(); |
|
|
|
TallyInterface tally = new Tally(new ProposalTallyInterface[] { |
|
|
|
new ProposalTally(new Integer[]{4, 5, 2, 1, 3, 1, 2}), |
|
|
@ -15,17 +29,14 @@ class MajorityJudgmentDeliberatorTest { |
|
|
|
}, 18L); |
|
|
|
ResultInterface result = mj.deliberate(tally); |
|
|
|
|
|
|
|
// System.out.println("Score 0: "+result.getProposalResults()[0].getScore()); |
|
|
|
// System.out.println("Score 1: "+result.getProposalResults()[1].getScore()); |
|
|
|
|
|
|
|
assertNotNull(result); |
|
|
|
assertEquals(2, result.getProposalResults().length); |
|
|
|
assertEquals(2, result.getProposalResults()[0].getRank()); |
|
|
|
assertEquals(1, result.getProposalResults()[1].getRank()); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
void testUsageWithBigNumbers() { |
|
|
|
public void testUsageWithBigNumbers() { |
|
|
|
DeliberatorInterface mj = new MajorityJudgmentDeliberator(); |
|
|
|
TallyInterface tally = new Tally(new ProposalTallyInterface[] { |
|
|
|
new ProposalTally(new Long[]{11312415004L, 21153652410L, 24101523299L, 18758623562L}), |
|
|
@ -43,5 +54,54 @@ class MajorityJudgmentDeliberatorTest { |
|
|
|
assertEquals(2, result.getProposalResults()[0].getRank()); |
|
|
|
assertEquals(1, result.getProposalResults()[1].getRank()); |
|
|
|
} |
|
|
|
|
|
|
|
@DisplayName("Test majority judgment deliberation") |
|
|
|
@ParameterizedTest(name="#{index} {0}") |
|
|
|
@JsonFileSource(resources = "/assertions.json") |
|
|
|
public void testFromJson(JsonObject datum) { |
|
|
|
JsonArray jsonTallies = datum.getJsonArray("tallies"); |
|
|
|
int amountOfProposals = jsonTallies.size(); |
|
|
|
ProposalTallyInterface[] tallies = new ProposalTallyInterface[amountOfProposals]; |
|
|
|
for (int i = 0; i < amountOfProposals; i++) { |
|
|
|
JsonArray jsonTally = jsonTallies.getJsonArray(i); |
|
|
|
int amountOfGrades = jsonTally.size(); |
|
|
|
Long[] tally = new Long[amountOfGrades]; |
|
|
|
for (int g = 0; g < amountOfGrades; g++) { |
|
|
|
JsonValue amountForGrade = jsonTally.get(g); |
|
|
|
tally[g] = Long.valueOf(amountForGrade.toString()); |
|
|
|
} |
|
|
|
tallies[i] = new ProposalTally(tally); |
|
|
|
} |
|
|
|
DeliberatorInterface mj = new MajorityJudgmentDeliberator(); |
|
|
|
TallyInterface tally = new Tally(tallies, 3L); |
|
|
|
ResultInterface result = mj.deliberate(tally); |
|
|
|
|
|
|
|
assertNotNull(result); |
|
|
|
JsonArray jsonRanks = datum.getJsonArray("ranks"); |
|
|
|
for (int i = 0; i < amountOfProposals; i++) { |
|
|
|
assertEquals( |
|
|
|
jsonRanks.getInt(i), |
|
|
|
result.getProposalResults()[i].getRank(), |
|
|
|
"Rank of tally #"+i |
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// @Test |
|
|
|
// public void runBenchmarks() throws Exception { |
|
|
|
// Options options = new OptionsBuilder() |
|
|
|
// .include(this.getClass().getName() + ".*") |
|
|
|
// .mode(Mode.AverageTime) |
|
|
|
// .warmupTime(TimeValue.seconds(1)) |
|
|
|
// .warmupIterations(6) |
|
|
|
// .threads(1) |
|
|
|
// .measurementIterations(6) |
|
|
|
// .forks(1) |
|
|
|
// .shouldFailOnError(true) |
|
|
|
// .shouldDoGC(true) |
|
|
|
// .build(); |
|
|
|
// |
|
|
|
// new Runner(options).run(); |
|
|
|
// } |
|
|
|
|
|
|
|
} |