Skip to content

Commit

Permalink
Feature/normalization of beattable 253 (#306)
Browse files Browse the repository at this point in the history
* Normalization of beatTable #253 #254 #255 #256
  • Loading branch information
valentinbujdoso authored and magwas committed Mar 31, 2019
1 parent aa932fa commit c73ac58
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/main/java/org/rulez/demokracia/pdengine/Normalizable.java
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -9,6 +13,27 @@ default void normalize() {
for (String key : getKeyCollection()) {
setElement(key, key, new Pair(0, 0));
}

Collection<String> 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));
}
}
}
}
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,20 @@ protected void createNewBeatTableWithComplexData() {
beatTable.setElement(choice3, choice2, new Pair(4, 11));
}

protected void createNewBeatTableWithEqualData() {
List<String> list = new ArrayList<String>();
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);
}
Expand Down

0 comments on commit c73ac58

Please sign in to comment.