diff --git a/build.gradle b/build.gradle index 9db2bb7..aee50b6 100644 --- a/build.gradle +++ b/build.gradle @@ -19,7 +19,8 @@ repositories { dependencies { // Use the JUnit test framework with assertions and benchmarks testImplementation 'org.junit.jupiter:junit-jupiter:5.6.3' - testImplementation 'org.openjdk.jmh:jmh-generator-annprocess:1.21' + testImplementation 'net.joshka:junit-json-params:1.1.0' + //testImplementation 'org.openjdk.jmh:jmh-generator-annprocess:1.21' // This dependency is exported to consumers, that is to say found on their compile classpath. //api 'org.apache.commons:commons-math3:3.6.1' diff --git a/pom.xml b/pom.xml index f787e5b..dc327a3 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ fr.mieuxvoter.mj majority-judgment - 0.1.0 + 0.1.1 majority-judgment https://mieuxvoter.fr @@ -24,7 +24,18 @@ 5.6.3 test - + + net.joshka + junit-json-params + 1.1.0 + test + + + org.glassfish + javax.json + 1.1.4 + test + diff --git a/src/main/java/fr/mieuxvoter/mj/MajorityJudgmentDeliberator.java b/src/main/java/fr/mieuxvoter/mj/MajorityJudgmentDeliberator.java index 9a4904a..b222d86 100644 --- a/src/main/java/fr/mieuxvoter/mj/MajorityJudgmentDeliberator.java +++ b/src/main/java/fr/mieuxvoter/mj/MajorityJudgmentDeliberator.java @@ -100,7 +100,7 @@ public class MajorityJudgmentDeliberator implements DeliberatorInterface { analysis.getMedianGrade() ); - if (! onlyNumbers) { + if ( ! onlyNumbers) { score += "_"; } diff --git a/src/test/java/fr/mieuxvoter/mj/MajorityJudgmentDeliberatorTest.java b/src/test/java/fr/mieuxvoter/mj/MajorityJudgmentDeliberatorTest.java index ba65cf4..b826217 100644 --- a/src/test/java/fr/mieuxvoter/mj/MajorityJudgmentDeliberatorTest.java +++ b/src/test/java/fr/mieuxvoter/mj/MajorityJudgmentDeliberatorTest.java @@ -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(); +// } } diff --git a/src/test/resources/assertions.json b/src/test/resources/assertions.json new file mode 100644 index 0000000..5766e40 --- /dev/null +++ b/src/test/resources/assertions.json @@ -0,0 +1,21 @@ +[ + + { + "title": "Few judgments", + "tallies": [ + [1, 1, 1], + [1, 0, 2], + [3, 0, 0], + [2, 0, 1], + [0, 3, 0] + ], + "ranks": [ + 3, + 1, + 5, + 4, + 2 + ] + } + +]