diff --git a/src/main/java/org/rulez/demokracia/pdengine/beattable/BeatTable.java b/src/main/java/org/rulez/demokracia/pdengine/beattable/BeatTable.java index 22539197..2b5cadd4 100644 --- a/src/main/java/org/rulez/demokracia/pdengine/beattable/BeatTable.java +++ b/src/main/java/org/rulez/demokracia/pdengine/beattable/BeatTable.java @@ -5,26 +5,27 @@ import java.util.List; import java.util.Objects; import java.util.Optional; + import org.rulez.demokracia.pdengine.votecast.CastVote; -public class BeatTable extends MapMatrix implements ContainingBeats { +public class BeatTable extends MapMatrix { + private static final long serialVersionUID = 1L; public BeatTable(final BeatTable beatTable) { super(beatTable.getKeyCollection()); - for (String row : getKeyCollection()) { - for (String col : getKeyCollection()) { - Pair sourcePair = beatTable.getElement(col, row); - if (Objects.nonNull(sourcePair)) { - this.setElement(col, row, createPair(sourcePair)); - } + for (final String row : getKeyCollection()) + for (final String col : getKeyCollection()) { + final Pair sourcePair = beatTable.getElement(col, row); + if (Objects.nonNull(sourcePair)) + setElement(col, row, createPair(sourcePair)); } - } } @Override public Pair getElement(final String columnKey, final String rowKey) { - return Optional.ofNullable(super.getElement(columnKey, rowKey)).orElse(new Pair(0, 0)); + return Optional.ofNullable(super.getElement(columnKey, rowKey)) + .orElse(new Pair(0, 0)); } private Pair createPair(final Pair sourcePair) { diff --git a/src/main/java/org/rulez/demokracia/pdengine/beattable/ContainingBeats.java b/src/main/java/org/rulez/demokracia/pdengine/beattable/ContainingBeats.java deleted file mode 100644 index 1def2937..00000000 --- a/src/main/java/org/rulez/demokracia/pdengine/beattable/ContainingBeats.java +++ /dev/null @@ -1,29 +0,0 @@ -package org.rulez.demokracia.pdengine.beattable; - -import org.rulez.demokracia.pdengine.beattable.BeatTable.Direction; -import org.rulez.demokracia.pdengine.exception.ReportedException; - -public interface ContainingBeats extends Matrix { - - default void checkPair(final Pair pair) { - if (pair == null) - throw new ReportedException("Invalid Pair key"); - } - - default int beatInformation(final String choice1, final String choice2, - final Direction direction) { - if (direction == null) - throw new ReportedException("Invalid direction"); - - final Pair pair = getElement(choice1, choice2); - - return direction.equals(Direction.DIRECTION_FORWARD) ? pair.getWinning() : pair.getLosing(); - } - - default Pair compareBeats(final Pair beat1, final Pair beat2) { - checkPair(beat1); - checkPair(beat2); - - return beat1.compareTo(beat2) >= 0 ? beat1 : beat2; - } -} diff --git a/src/test/java/org/rulez/demokracia/pdengine/beattable/BeatTableBeatInformationTest.java b/src/test/java/org/rulez/demokracia/pdengine/beattable/BeatTableBeatInformationTest.java index d5827e65..e0c18f44 100644 --- a/src/test/java/org/rulez/demokracia/pdengine/beattable/BeatTableBeatInformationTest.java +++ b/src/test/java/org/rulez/demokracia/pdengine/beattable/BeatTableBeatInformationTest.java @@ -1,64 +1,55 @@ package org.rulez.demokracia.pdengine.beattable; -import static org.junit.Assert.assertEquals; import static org.rulez.demokracia.pdengine.testhelpers.BeatTableTestHelper.*; + import org.junit.Test; import org.rulez.demokracia.pdengine.annotations.TestedBehaviour; import org.rulez.demokracia.pdengine.annotations.TestedFeature; import org.rulez.demokracia.pdengine.annotations.TestedOperation; -import org.rulez.demokracia.pdengine.beattable.BeatTable.Direction; import org.rulez.demokracia.pdengine.testhelpers.ThrowableTester; @TestedFeature("Supporting functionality") @TestedOperation("BeatTable") -@TestedBehaviour("the beat information related to a and b can be obtained for forward and backward") +@TestedBehaviour( + "the beat information related to a and b can be obtained for forward and backward" +) public class BeatTableBeatInformationTest extends ThrowableTester { @Test - public void beatInformation_throws_an_exception_when_direction_is_not_defined() { - assertThrows(() -> createNewBeatTableWithData().beatInformation(null, null, null)) - .assertMessageIs("Invalid direction"); - } - - @Test - public void beatInformation_gives_back_the_number_of_winnings_from_choice1_to_choice2() { - final BeatTable beatTable = createNewBeatTableWithData(); - - assertEquals(PAIR.getWinning(), - beatTable.beatInformation(CHOICE1, CHOICE2, Direction.DIRECTION_FORWARD)); - } - - @Test - public void beatInformation_gives_back_the_number_of_losing_from_choice1_to_choice2() { - final BeatTable beatTable = createNewBeatTableWithData(); - - assertEquals(PAIR.getLosing(), - beatTable.beatInformation(CHOICE1, CHOICE2, Direction.DIRECTION_BACKWARD)); - } - - @Test - public void beatInformation_throws_exception_when_getting_with_invalid_row_key() { - assertThrows(() -> createNewBeatTableWithData().getElement(CHOICE1, "InvalidRow")) + public void + beatInformation_throws_exception_when_getting_with_invalid_row_key() { + assertThrows( + () -> createNewBeatTableWithData().getElement(CHOICE1, "InvalidRow") + ) .assertMessageIs("Invalid row key"); } @Test - public void beatInformation_throws_exception_when_getting_with_invalid_column_key() { - assertThrows(() -> createNewBeatTableWithData().getElement("InvalidColumn", CHOICE1)) + public void + beatInformation_throws_exception_when_getting_with_invalid_column_key() { + assertThrows( + () -> createNewBeatTableWithData().getElement("InvalidColumn", CHOICE1) + ) .assertMessageIs("Invalid column key"); } @Test - public void beatInformation_throws_exception_when_setting_with_invalid_row_key() { + public void + beatInformation_throws_exception_when_setting_with_invalid_row_key() { assertThrows( - () -> createNewBeatTableWithData().setElement(CHOICE1, "InvalidRow", new Pair(0, 0))) - .assertMessageIs("Invalid row key"); + () -> createNewBeatTableWithData() + .setElement(CHOICE1, "InvalidRow", new Pair(0, 0)) + ) + .assertMessageIs("Invalid row key"); } @Test - public void beatInformation_throws_exception_when_setting_with_invalid_column_key() { + public void + beatInformation_throws_exception_when_setting_with_invalid_column_key() { assertThrows( - () -> createNewBeatTableWithData().setElement("InvalidColumn", CHOICE1, new Pair(0, 0))) - .assertMessageIs("Invalid column key"); + () -> createNewBeatTableWithData() + .setElement("InvalidColumn", CHOICE1, new Pair(0, 0)) + ) + .assertMessageIs("Invalid column key"); } } diff --git a/src/test/java/org/rulez/demokracia/pdengine/beattable/BeatTableCompareBeatsTest.java b/src/test/java/org/rulez/demokracia/pdengine/beattable/BeatTableCompareBeatsTest.java deleted file mode 100644 index 8fa37d6e..00000000 --- a/src/test/java/org/rulez/demokracia/pdengine/beattable/BeatTableCompareBeatsTest.java +++ /dev/null @@ -1,82 +0,0 @@ -package org.rulez.demokracia.pdengine.beattable; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertSame; -import org.junit.Before; -import org.junit.Test; -import org.rulez.demokracia.pdengine.annotations.TestedBehaviour; -import org.rulez.demokracia.pdengine.annotations.TestedFeature; -import org.rulez.demokracia.pdengine.annotations.TestedOperation; -import org.rulez.demokracia.pdengine.testhelpers.ThrowableTester; - -@TestedFeature("Schulze method") -@TestedOperation("compare beats") -public class BeatTableCompareBeatsTest extends ThrowableTester { - - private static final String IF_BEATS_ARE_DIFFERENT_THE_BIGGER_WINS = - "if beats are different, the bigger wins"; - private static final String IF_BEATS_AND_LOSES_ARE_TIE_GIVE_BACK_THE_FIRST_OPTION = - "if beats and loses are tie, give back the first option"; - private static final String IF_BEATS_TIE_LOOSES_DECIDE = "if beats tie, looses decide"; - - private BeatTable beatTable; - - @Before - public void setUp() { - beatTable = new BeatTable(); - } - - @TestedBehaviour(IF_BEATS_TIE_LOOSES_DECIDE) - @Test - public void compareBeats_gives_back_the_backward_lower_beat1() { - Pair beat1 = new Pair(2, 2); - Pair beat2 = new Pair(2, 3); - assertEquals(beat1, beatTable.compareBeats(beat1, beat2)); - } - - @TestedBehaviour(IF_BEATS_TIE_LOOSES_DECIDE) - @Test - public void compareBeats_gives_back_the_backward_lower_beat2() { - Pair beat1 = new Pair(42, 10); - Pair beat2 = new Pair(42, 3); - assertEquals(beat2, beatTable.compareBeats(beat1, beat2)); - } - - @TestedBehaviour(IF_BEATS_AND_LOSES_ARE_TIE_GIVE_BACK_THE_FIRST_OPTION) - @Test - public void compareBeats_returns_the_first_beat_when_the_result_is_tie() { - Pair beat1 = new Pair(4, 3); - Pair beat2 = new Pair(4, 3); - assertSame(beat1, beatTable.compareBeats(beat1, beat2)); - } - - @TestedBehaviour(IF_BEATS_ARE_DIFFERENT_THE_BIGGER_WINS) - @Test - public void compareBeats_throws_an_exception_when_first_input_param_is_not_defined() { - assertThrows(() -> beatTable.compareBeats(null, new Pair(4, 3))) - .assertMessageIs("Invalid Pair key"); - } - - @TestedBehaviour(IF_BEATS_ARE_DIFFERENT_THE_BIGGER_WINS) - @Test - public void compareBeats_throws_an_exception_when_second_input_param_is_not_defined() { - assertThrows(() -> beatTable.compareBeats(new Pair(4, 7), null)) - .assertMessageIs("Invalid Pair key"); - } - - @TestedBehaviour(IF_BEATS_ARE_DIFFERENT_THE_BIGGER_WINS) - @Test - public void compareBeats_gives_back_the_forward_bigger_beat1() { - Pair beat1 = new Pair(7, 3); - Pair beat2 = new Pair(4, 3); - assertSame(beat1, beatTable.compareBeats(beat1, beat2)); - } - - @TestedBehaviour(IF_BEATS_ARE_DIFFERENT_THE_BIGGER_WINS) - @Test - public void compareBeats_gives_back_the_forward_bigger_beat2() { - Pair beat1 = new Pair(7, 3); - Pair beat2 = new Pair(8, 3); - assertSame(beat2, beatTable.compareBeats(beat1, beat2)); - } -} diff --git a/src/test/java/org/rulez/demokracia/pdengine/beattable/BeatTableNormalizationTest.java b/src/test/java/org/rulez/demokracia/pdengine/beattable/BeatTableNormalizationTest.java index b2177494..ebbe454a 100644 --- a/src/test/java/org/rulez/demokracia/pdengine/beattable/BeatTableNormalizationTest.java +++ b/src/test/java/org/rulez/demokracia/pdengine/beattable/BeatTableNormalizationTest.java @@ -2,6 +2,7 @@ import static org.junit.Assert.assertEquals; import static org.rulez.demokracia.pdengine.testhelpers.BeatTableTestHelper.*; + import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -10,7 +11,6 @@ import org.rulez.demokracia.pdengine.annotations.TestedBehaviour; import org.rulez.demokracia.pdengine.annotations.TestedFeature; import org.rulez.demokracia.pdengine.annotations.TestedOperation; -import org.rulez.demokracia.pdengine.beattable.BeatTable.Direction; import org.rulez.demokracia.pdengine.testhelpers.ThrowableTester; @TestedFeature("Schulze method") @@ -25,7 +25,7 @@ public class BeatTableNormalizationTest extends ThrowableTester { @Before public void setUp() { - BeatTable beatTable = createNewBeatTableWithComplexData(); + final BeatTable beatTable = createNewBeatTableWithComplexData(); normalizedBeatTable = beatTableService.normalize(beatTable); } @@ -36,29 +36,34 @@ public void normalization_sets_the_diagonal_to_0_0() { } private void assertAllDiagonalElementsAreZero() { - normalizedBeatTable.getKeyCollection().forEach(k -> assertDiagonalElementIsZero(k)); + normalizedBeatTable.getKeyCollection() + .forEach(k -> assertDiagonalElementIsZero(k)); } @TestedBehaviour("the elements corresponding to loosers are (0,0)") @Test public void normalization_sets_the_looser_to_0_0() { - Pair element = normalizedBeatTable.getElement(CHOICE3, CHOICE1); + final Pair element = normalizedBeatTable.getElement(CHOICE3, CHOICE1); assertEquals(new Pair(0, 0), element); } - @TestedBehaviour("the elements corresponding to winners contain the number of looses backward") + @TestedBehaviour( + "the elements corresponding to winners contain the number of looses backward" + ) @Test public void normalization_does_not_modify_the_winners_number_of_looses() { - int actualResult = - normalizedBeatTable.beatInformation(CHOICE1, CHOICE2, Direction.DIRECTION_BACKWARD); + final int actualResult = + normalizedBeatTable.getElement(CHOICE1, CHOICE2).getLosing(); assertEquals(1, actualResult); } - @TestedBehaviour("the elements corresponding to winners contain the number of wins forward") + @TestedBehaviour( + "the elements corresponding to winners contain the number of wins forward" + ) @Test public void normalization_does_not_modify_the_winners_number_of_wins() { - int actualResult = - normalizedBeatTable.beatInformation(CHOICE1, CHOICE2, Direction.DIRECTION_FORWARD); + final int actualResult = + normalizedBeatTable.getElement(CHOICE1, CHOICE2).getWinning(); assertEquals(5, actualResult); } @@ -66,7 +71,7 @@ public void normalization_does_not_modify_the_winners_number_of_wins() { @Test public void normalization_set_the_ties_to_0_0() { setEqualData(); - Pair element = normalizedBeatTable.getElement(CHOICE2, CHOICE1); + final Pair element = normalizedBeatTable.getElement(CHOICE2, CHOICE1); assertEquals(new Pair(0, 0), element); } @@ -74,24 +79,26 @@ public void normalization_set_the_ties_to_0_0() { @Test public void normalization_set_the_other_part_of_the_ties_to_0_0_too() { setEqualData(); - Pair element = normalizedBeatTable.getElement(CHOICE1, CHOICE2); + final Pair element = normalizedBeatTable.getElement(CHOICE1, CHOICE2); assertEquals(new Pair(0, 0), element); } @TestedBehaviour("the elements for ties are (0,0)") @Test - public void normalization_does_not_modify_the_values_when_the_selected_beats_are_not_ties() { + public void + normalization_does_not_modify_the_values_when_the_selected_beats_are_not_ties() { setEqualData(); - Pair element = normalizedBeatTable.getElement(CHOICE2, CHOICE3); + final Pair element = normalizedBeatTable.getElement(CHOICE2, CHOICE3); assertEquals(new Pair(4, 1), element); } private void setEqualData() { - normalizedBeatTable = beatTableService.normalize(createNewBeatTableWithEqualData()); + normalizedBeatTable = + beatTableService.normalize(createNewBeatTableWithEqualData()); } private void assertDiagonalElementIsZero(final String key) { - Pair element = normalizedBeatTable.getElement(key, key); + final Pair element = normalizedBeatTable.getElement(key, key); assertEquals(new Pair(0, 0), element); } } diff --git a/src/test/java/org/rulez/demokracia/pdengine/beattable/BeatTableTransitiveClosureTest.java b/src/test/java/org/rulez/demokracia/pdengine/beattable/BeatTableTransitiveClosureTest.java index 0adfea67..46318de0 100644 --- a/src/test/java/org/rulez/demokracia/pdengine/beattable/BeatTableTransitiveClosureTest.java +++ b/src/test/java/org/rulez/demokracia/pdengine/beattable/BeatTableTransitiveClosureTest.java @@ -2,8 +2,10 @@ import static org.junit.Assert.*; import static org.rulez.demokracia.pdengine.testhelpers.BeatTableTestHelper.*; + import java.util.Collection; import java.util.Set; + import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -37,7 +39,8 @@ public void transitive_closure_on_empty_beat_table_results_empty_result() { @Test public void transitive_closure_computes_the_shortest_paths_by_pairs() { - beatTable = beatTableTransitiveClosureService.computeTransitiveClosure(beatTable); + beatTable = + beatTableTransitiveClosureService.computeTransitiveClosure(beatTable); final Collection keyCollection = beatTable.getKeyCollection(); for (final String i : keyCollection) for (final String j : keyCollection) { @@ -49,19 +52,22 @@ public void transitive_closure_computes_the_shortest_paths_by_pairs() { @Test public void test_transitive_closure_compute_optimal_route() { - beatTable = beatTableTransitiveClosureService.computeTransitiveClosure(beatTable); + beatTable = + beatTableTransitiveClosureService.computeTransitiveClosure(beatTable); assertEquals(new Pair(5, 1), beatTable.getElement(CHOICE1, CHOICE3)); } - private void assertNoShorterPathBetweenChoices(final Collection keyCollection, - final String choice1, final String choice2) { + private void assertNoShorterPathBetweenChoices( + final Collection keyCollection, + final String choice1, final String choice2 + ) { for (final String k : keyCollection) { if (Set.of(choice1, choice2).contains(k)) continue; final Pair greater = beatTable.getElement(choice1, choice2); final Pair less = lessBeat(beatTable.getElement(choice1, k), beatTable.getElement(k, choice2)); - assertEquals(greater, beatTable.compareBeats(less, greater)); + assertTrue(greater.compareTo(less) >= 0); } } diff --git a/src/test/java/org/rulez/demokracia/pdengine/votecalculator/CalculateWinnersTest.java b/src/test/java/org/rulez/demokracia/pdengine/votecalculator/CalculateWinnersTest.java index 46097f48..ac2d25d9 100644 --- a/src/test/java/org/rulez/demokracia/pdengine/votecalculator/CalculateWinnersTest.java +++ b/src/test/java/org/rulez/demokracia/pdengine/votecalculator/CalculateWinnersTest.java @@ -1,11 +1,12 @@ package org.rulez.demokracia.pdengine.votecalculator; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.*; import static org.mockito.Mockito.when; + import java.util.Collection; import java.util.List; import java.util.Set; + import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -37,15 +38,20 @@ public class CalculateWinnersTest { public void setUp() { beatPathTable = BeatTableTestHelper.createTransitiveClosedBeatTable(); when(beatTableIgnore.ignoreChoices(beatPathTable, List.of("A"))) - .thenReturn(BeatTableTestHelper.createTransitiveClosedBeatTable(List.of("B", "C", "D"))); - when(beatTableIgnore.ignoreChoices(beatPathTable, Set.of())).thenReturn(beatPathTable); + .thenReturn( + BeatTableTestHelper + .createTransitiveClosedBeatTable(List.of("B", "C", "D")) + ); + when(beatTableIgnore.ignoreChoices(beatPathTable, Set.of())) + .thenReturn(beatPathTable); } @TestedBehaviour("only choices not in ignoredChoices are considered") @Test public void calculate_winners_returns_none_of_the_ignored_choices() { - Collection ignoredChoices = List.of("A"); - List winners = winnerCalculator.calculateWinners(beatPathTable, ignoredChoices); + final Collection ignoredChoices = List.of("A"); + final List winners = + winnerCalculator.calculateWinners(beatPathTable, ignoredChoices); assertFalse(winners.contains("A")); } @@ -53,37 +59,47 @@ public void calculate_winners_returns_none_of_the_ignored_choices() { @TestedBehaviour("only choices not in ignoredChoices are considered") @Test public void calculate_winners_returns_not_ignored_winner() { - List winners = winnerCalculator.calculateWinners(beatPathTable, List.of("A")); + final List winners = + winnerCalculator.calculateWinners(beatPathTable, List.of("A")); assertTrue(winners.contains("B")); } @TestedBehaviour("all non-beaten candidates are winners") @Test public void calculate_winners_doesnt_return_beaten_candidates() { - assertNoWinnerIsBeaten(winnerCalculator.calculateWinners(beatPathTable, Set.of())); + assertNoWinnerIsBeaten( + winnerCalculator.calculateWinners(beatPathTable, Set.of()) + ); } @TestedBehaviour("all non-beaten candidates are winners") @Test public void calculate_winners_return_all_non_beaten_candidates() { - assertNonbeatensAreWinner(winnerCalculator.calculateWinners(beatPathTable, Set.of())); + assertNonbeatensAreWinner( + winnerCalculator.calculateWinners(beatPathTable, Set.of()) + ); } private void assertNoWinnerIsBeaten(final List winners) { - assertTrue(winners.stream().allMatch(choice -> isAWinner(choice, beatPathTable))); + assertTrue( + winners.stream().allMatch(choice -> isAWinner(choice, beatPathTable)) + ); } private void assertNonbeatensAreWinner(final List winners) { assertTrue( - beatPathTable.getKeyCollection().stream().filter(choice -> isAWinner(choice, beatPathTable)) - .allMatch(choice -> winners.contains(choice))); + beatPathTable.getKeyCollection().stream() + .filter(choice -> isAWinner(choice, beatPathTable)) + .allMatch(choice -> winners.contains(choice)) + ); } - private boolean isAWinner(final String player1, final BeatTable beatPathTable) { - for (String player2 : beatPathTable.getKeyCollection()) { - Pair forward = beatPathTable.getElement(player1, player2); - Pair backward = beatPathTable.getElement(player2, player1); - if (!forward.equals(beatPathTable.compareBeats(forward, backward))) + private boolean + isAWinner(final String player1, final BeatTable beatPathTable) { + for (final String player2 : beatPathTable.getKeyCollection()) { + final Pair forward = beatPathTable.getElement(player1, player2); + final Pair backward = beatPathTable.getElement(player2, player1); + if (forward.compareTo(backward) < 0) return false; } return true;