Browse Source

test: prepare for a more exhaustive test-suite, using JSON

pull/4/head
Dominique Merle 1 year ago
parent
commit
2cbb7c45d8
  1. 3
      build.gradle
  2. 15
      pom.xml
  3. 2
      src/main/java/fr/mieuxvoter/mj/MajorityJudgmentDeliberator.java
  4. 72
      src/test/java/fr/mieuxvoter/mj/MajorityJudgmentDeliberatorTest.java
  5. 21
      src/test/resources/assertions.json

3
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'

15
pom.xml

@ -6,7 +6,7 @@
<groupId>fr.mieuxvoter.mj</groupId>
<artifactId>majority-judgment</artifactId>
<version>0.1.0</version>
<version>0.1.1</version>
<name>majority-judgment</name>
<url>https://mieuxvoter.fr</url>
@ -24,7 +24,18 @@
<version>5.6.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.joshka</groupId>
<artifactId>junit-json-params</artifactId>
<version>1.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.1.4</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>

2
src/main/java/fr/mieuxvoter/mj/MajorityJudgmentDeliberator.java

@ -100,7 +100,7 @@ public class MajorityJudgmentDeliberator implements DeliberatorInterface {
analysis.getMedianGrade()
);
if (! onlyNumbers) {
if ( ! onlyNumbers) {
score += "_";
}

72
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();
// }
}

21
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
]
}
]
Loading…
Cancel
Save