test: enable the median default grade in the JSON assertions

pull/12/head
Dominique Merle 3 years ago
parent 093ae22354
commit 5e165841df

@ -8,6 +8,13 @@ import java.math.BigInteger;
*/
abstract public class DefaultGradeTally extends Tally implements TallyInterface {
/**
* Override this to choose the default grade for a given proposal.
*/
abstract protected Integer getDefaultGradeForProposal(ProposalTallyInterface proposalTally);
// <domi41> /me is confused with why we need constructors in an abstract class?
public DefaultGradeTally(TallyInterface tally) {
super(tally.getProposalsTallies(), tally.getAmountOfJudges());
}
@ -27,18 +34,17 @@ abstract public class DefaultGradeTally extends Tally implements TallyInterface
protected void fillWithDefaultGrade() {
int amountOfProposals = getAmountOfProposals();
for (int i = 0 ; i < amountOfProposals ; i++) {
ProposalTallyInterface proposal = getProposalsTallies()[i];
Integer defaultGrade = getDefaultGrade(proposal);
BigInteger amountOfJudgments = proposal.getAmountOfJudgments();
ProposalTallyInterface proposalTally = getProposalsTallies()[i];
Integer defaultGrade = getDefaultGradeForProposal(proposalTally);
BigInteger amountOfJudgments = proposalTally.getAmountOfJudgments();
BigInteger missingAmount = this.amountOfJudges.subtract(amountOfJudgments);
int missingSign = missingAmount.compareTo(BigInteger.ZERO);
assert(0 <= missingSign); // ERROR: More judgments than judges!
if (0 < missingSign) {
proposal.getTally()[defaultGrade] = proposal.getTally()[defaultGrade].add(missingAmount);
BigInteger[] rawTally = proposalTally.getTally();
rawTally[defaultGrade] = rawTally[defaultGrade].add(missingAmount);
}
}
}
abstract protected Integer getDefaultGrade(ProposalTallyInterface proposalTally);
}

@ -30,7 +30,7 @@ public class MedianDefaultTally extends DefaultGradeTally implements TallyInterf
}
@Override
protected Integer getDefaultGrade(ProposalTallyInterface proposalTally) {
protected Integer getDefaultGradeForProposal(ProposalTallyInterface proposalTally) {
ProposalTallyAnalysis analysis = new ProposalTallyAnalysis(proposalTally);
return analysis.getMedianGrade();
}

@ -43,7 +43,7 @@ public class TallyWithDefaultGrade extends DefaultGradeTally implements TallyInt
}
@Override
protected Integer getDefaultGrade(ProposalTallyInterface proposalTally) {
protected Integer getDefaultGradeForProposal(ProposalTallyInterface proposalTally) {
return this.defaultGrade;
}

@ -20,6 +20,9 @@ class MajorityJudgmentDeliberatorTest {
@ParameterizedTest(name="#{index} {0}")
@JsonFileSource(resources = "/assertions.json")
public void testFromJson(JsonObject datum) {
// This test uses the JSON file in test/resources/
// It also allows testing the various modes of default grades.
JsonArray jsonTallies = datum.getJsonArray("tallies");
int amountOfProposals = jsonTallies.size();
BigInteger amountOfParticipants = new BigInteger(datum.get("participants").toString());
@ -39,6 +42,8 @@ class MajorityJudgmentDeliberatorTest {
TallyInterface tally;
if ("StaticDefault".equalsIgnoreCase(mode)) {
tally = new TallyWithDefaultGrade(tallies, amountOfParticipants, datum.getInt("default"));
} else if ("MedianDefault".equalsIgnoreCase(mode)) {
tally = new MedianDefaultTally(tallies, amountOfParticipants);
} else if ("Normalized".equalsIgnoreCase(mode)) {
tally = new NormalizedTally(tallies);
} else {
@ -287,9 +292,6 @@ class MajorityJudgmentDeliberatorTest {
// We're using primes to test the upper bounds of our LCM shenanigans.
// This test takes a long time! (3 seconds)
// List<Integer> generatedPrimes = sieveOfEratosthenes(15000);
// System.out.println(generatedPrimes);
int amountOfProposals = primes.length; // 1437
DeliberatorInterface mj = new MajorityJudgmentDeliberator();
ProposalTallyInterface[] tallies = new ProposalTallyInterface[amountOfProposals];
@ -306,7 +308,7 @@ class MajorityJudgmentDeliberatorTest {
assertEquals(amountOfProposals, result.getProposalResults().length);
for (int i = 0 ; i < amountOfProposals ; i++) {
assertEquals(
1 + (i % primes.length), result.getProposalResults()[i].getRank(),
1 + i, result.getProposalResults()[i].getRank(),
"Rank of Proposal #" + i
);
}
@ -316,7 +318,7 @@ class MajorityJudgmentDeliberatorTest {
@DisplayName("Test normalized tallies with thousands of proposals")
public void testNormalizedWithThousandsOfProposals() {
// This test is faster than the primes one (0.4 seconds),
// since primes are the worst case-scenario for our LCM.
// since primes are the worst-case scenario for our LCM.
int amountOfProposals = primes.length; // 1437
DeliberatorInterface mj = new MajorityJudgmentDeliberator();
@ -333,12 +335,14 @@ class MajorityJudgmentDeliberatorTest {
assertEquals(amountOfProposals, result.getProposalResults().length);
for (int i = 0 ; i < amountOfProposals ; i++) {
assertEquals(
1 + (i % primes.length), result.getProposalResults()[i].getRank(),
1 + i, result.getProposalResults()[i].getRank(),
"Rank of Proposal #" + i
);
}
}
// …
// @Test
// public void runBenchmarks() throws Exception {
// Options options = new OptionsBuilder()

@ -90,6 +90,25 @@
2
]
},
{
"title": "Median Default Grade",
"participants": 10,
"mode": "MedianDefault",
"tallies": [
[ 2, 2, 2, 2, 2 ],
[ 2, 2, 1, 2, 2 ],
[ 0, 0, 4, 0, 0 ],
[ 0, 0, 0, 0, 2 ],
[ 0, 0, 0, 1, 1 ]
],
"ranks": [
4,
4,
3,
1,
2
]
},
{
"title": "Normalization",
"participants": 10,

Loading…
Cancel
Save