diff --git a/src/main/java/org/rulez/demokracia/pdengine/Normalizable.java b/src/main/java/org/rulez/demokracia/pdengine/Normalizable.java index f7440358..d0a6dffa 100644 --- a/src/main/java/org/rulez/demokracia/pdengine/Normalizable.java +++ b/src/main/java/org/rulez/demokracia/pdengine/Normalizable.java @@ -1,4 +1,8 @@ package org.rulez.demokracia.pdengine; + +import java.util.Collection; + +import org.rulez.demokracia.pdengine.BeatTable.Direction; import org.rulez.demokracia.pdengine.dataobjects.Pair; public interface Normalizable extends ContainingBeats { @@ -9,6 +13,27 @@ default void normalize() { for (String key : getKeyCollection()) { setElement(key, key, new Pair(0, 0)); } + + Collection keys = getKeyCollection(); + + for (String key1 : keys) { + for (String key2 : keys) { + int key1Win = beatInformation(key1, key2, Direction.DIRECTION_FORWARD); + int key2Win = beatInformation(key2, key1, Direction.DIRECTION_FORWARD); + + if (key1Win > key2Win) + setElement(key2, key1, new Pair(0, 0)); + if (key1Win == key2Win) { + int key1Los = beatInformation(key1, key2, Direction.DIRECTION_BACKWARD); + int key2Los = beatInformation(key2, key1, Direction.DIRECTION_BACKWARD); + + if (key1Los == key2Los) { + setElement(key2, key1, new Pair(0, 0)); + setElement(key1, key2, new Pair(0, 0)); + } + } + } + } } } diff --git a/src/test/java/org/rulez/demokracia/pdengine/BeatTableNormalizationTest.java b/src/test/java/org/rulez/demokracia/pdengine/BeatTableNormalizationTest.java index d9282231..65771e20 100644 --- a/src/test/java/org/rulez/demokracia/pdengine/BeatTableNormalizationTest.java +++ b/src/test/java/org/rulez/demokracia/pdengine/BeatTableNormalizationTest.java @@ -1,9 +1,11 @@ package org.rulez.demokracia.pdengine; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import org.junit.Before; import org.junit.Test; +import org.rulez.demokracia.pdengine.BeatTable.Direction; import org.rulez.demokracia.pdengine.annotations.TestedBehaviour; import org.rulez.demokracia.pdengine.annotations.TestedFeature; import org.rulez.demokracia.pdengine.annotations.TestedOperation; @@ -33,4 +35,56 @@ private void assertDiagonalElementIsZero(final String key) { Pair element = beatTable.getElement(key, key); assertEquals(new Pair(0, 0), element); } + + @TestedBehaviour("the elements corresponding to loosers are (0,0)") + @Test + public void normalization_sets_the_looser_to_0_0() { + setPair(choice3, choice1); + assertTrue(pair.winning == 0 && pair.losing == 0); + } + + @TestedBehaviour("the elements corresponding to winners contain the number of looses backward") + @Test + public void normalization_does_not_modify_the_winners_number_of_looses() { + beatTable.normalize(); + int actualResult = beatTable.beatInformation(choice1, choice2, Direction.DIRECTION_BACKWARD); + assertEquals(1, actualResult); + } + + @TestedBehaviour("the elements corresponding to winners contain the number of wins forward") + @Test + public void normalization_does_not_modify_the_winners_number_of_wins() { + beatTable.normalize(); + int actualResult = beatTable.beatInformation(choice1, choice2, Direction.DIRECTION_FORWARD); + assertEquals(5, actualResult); + } + + @TestedBehaviour("the elements for ties are (0,0)") + @Test + public void normalization_set_the_ties_to_0_0() { + createNewBeatTableWithEqualData(); + setPair(choice2, choice1); + assertTrue(pair.winning == 0 && pair.losing == 0); + } + + @TestedBehaviour("the elements for ties are (0,0)") + @Test + public void normalization_set_the_other_part_of_the_ties_to_0_0_too() { + createNewBeatTableWithEqualData(); + setPair(choice1, choice2); + assertTrue(pair.winning == 0 && pair.losing == 0); + } + + @TestedBehaviour("the elements for ties are (0,0)") + @Test + public void normalization_does_not_modify_the_values_when_the_selected_beats_are_not_ties() { + createNewBeatTableWithEqualData(); + setPair(choice2, choice3); + assertTrue(pair.winning == 4 && pair.losing == 1); + } + + private void setPair(final String choice1, final String choice2) { + beatTable.normalize(); + pair = beatTable.getPair(choice1, choice2); + } } diff --git a/src/test/java/org/rulez/demokracia/pdengine/testhelpers/CreatedBeatTable.java b/src/test/java/org/rulez/demokracia/pdengine/testhelpers/CreatedBeatTable.java index 6e2ddfcd..7ee4029f 100644 --- a/src/test/java/org/rulez/demokracia/pdengine/testhelpers/CreatedBeatTable.java +++ b/src/test/java/org/rulez/demokracia/pdengine/testhelpers/CreatedBeatTable.java @@ -60,6 +60,20 @@ protected void createNewBeatTableWithComplexData() { beatTable.setElement(choice3, choice2, new Pair(4, 11)); } + protected void createNewBeatTableWithEqualData() { + List list = new ArrayList(); + list.add(choice1); + list.add(choice2); + list.add(choice3); + beatTable = new BeatTable(list); + beatTable.setElement(choice1, choice2, new Pair(2, 2)); + beatTable.setElement(choice2, choice1, new Pair(2, 2)); + beatTable.setElement(choice1, choice3, new Pair(4, 1)); + beatTable.setElement(choice3, choice1, new Pair(4, 4)); + beatTable.setElement(choice2, choice3, new Pair(4, 1)); + beatTable.setElement(choice3, choice2, new Pair(4, 4)); + } + protected void setGetElementAsResult(final String first, final String second) { result = beatTable.getElement(first, second); }