-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Create BeatTable #238 #239 #240 * CompareBeats #249 #250 * Add unimplemeneted test * Traits pattern
- Loading branch information
1 parent
dfc50dc
commit 35e5aef
Showing
10 changed files
with
318 additions
and
0 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
src/main/java/org/rulez/demokracia/pdengine/BeatTable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package org.rulez.demokracia.pdengine; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
|
||
import org.rulez.demokracia.pdengine.dataobjects.Pair; | ||
import org.rulez.demokracia.types.MapMatrix; | ||
|
||
public class BeatTable extends MapMatrix<Choice, Pair> implements ContainingBeats{ | ||
private static final long serialVersionUID = 1L; | ||
|
||
public enum Direction { | ||
DIRECTION_FORWARD, DIRECTION_BACKWARD | ||
} | ||
|
||
public BeatTable() { | ||
this(new ArrayList<Choice>()); | ||
} | ||
|
||
public BeatTable(final Collection<Choice> keyCollection) { | ||
super(keyCollection); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/main/java/org/rulez/demokracia/pdengine/ContainingBeats.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package org.rulez.demokracia.pdengine; | ||
|
||
import org.rulez.demokracia.pdengine.BeatTable.Direction; | ||
import org.rulez.demokracia.pdengine.dataobjects.Pair; | ||
import org.rulez.demokracia.pdengine.exception.ReportedException; | ||
import org.rulez.demokracia.types.Matrix; | ||
|
||
public interface ContainingBeats extends Matrix<Choice, Pair>{ | ||
|
||
long serialVersionUID = 1L; | ||
default void checkPair(final Pair pair) { | ||
if (pair == null) | ||
throw new ReportedException("Invalid Pair key"); | ||
} | ||
|
||
default int beatInformation(final Choice choice1, final Choice choice2, final Direction direction) { | ||
if (direction == null) | ||
throw new ReportedException("Invalid direction"); | ||
|
||
int result = 0; | ||
Pair pair = getElement(choice1, choice2); | ||
|
||
|
||
if (direction.equals(Direction.DIRECTION_FORWARD)) | ||
result = pair.winning; | ||
else | ||
result = pair.losing; | ||
|
||
return result; | ||
} | ||
|
||
default Pair compareBeats(final Pair beat1, final Pair beat2) { | ||
checkPair(beat1); | ||
checkPair(beat2); | ||
|
||
if (beat1.winning > beat2.winning) | ||
return beat1; | ||
else if (beat1.winning < beat2.winning) | ||
return beat2; | ||
else | ||
if (beat1.losing < beat2.losing) | ||
return beat1; | ||
else if (beat2.losing < beat1.losing) | ||
return beat2; | ||
else | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
|
||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/org/rulez/demokracia/pdengine/dataobjects/Pair.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package org.rulez.demokracia.pdengine.dataobjects; | ||
|
||
|
||
public class Pair extends PairEntity { | ||
private static final long serialVersionUID = 1L; | ||
|
||
public Pair(final int winning, final int losing) { | ||
super(); | ||
this.winning = winning; | ||
this.losing = losing; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/org/rulez/demokracia/pdengine/dataobjects/PairEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package org.rulez.demokracia.pdengine.dataobjects; | ||
|
||
import org.rulez.demokracia.pdengine.persistence.BaseEntity; | ||
|
||
public class PairEntity extends BaseEntity { | ||
|
||
private static final long serialVersionUID = 1L; | ||
public int winning; | ||
public int losing; | ||
} |
43 changes: 43 additions & 0 deletions
43
src/test/java/org/rulez/demokracia/pdengine/BeatTableBeatInformationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package org.rulez.demokracia.pdengine; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
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; | ||
import org.rulez.demokracia.pdengine.exception.ReportedException; | ||
import org.rulez.demokracia.pdengine.testhelpers.CreatedBeatTable; | ||
|
||
@TestedFeature("Supporting functionality") | ||
@TestedOperation("BeatTable") | ||
@TestedBehaviour("the beat information related to a and b can be obtained for forward and backward") | ||
public class BeatTableBeatInformationTest extends CreatedBeatTable { | ||
|
||
@Before | ||
public void setUp() throws ReportedException { | ||
super.setUp(); | ||
} | ||
|
||
@Test | ||
public void beatInformation_throws_an_exception_when_direction_is_not_defined() { | ||
assertThrows(() -> beatTable.beatInformation(null, null, null) | ||
).assertMessageIs("Invalid direction"); | ||
} | ||
|
||
@Test | ||
public void beatInformation_gives_back_the_number_of_winnings_from_choice1_to_choice2() { | ||
createNewBeatTableWithData(); | ||
|
||
assertEquals(pair.winning, beatTable.beatInformation(choice1, choice2, Direction.DIRECTION_FORWARD)); | ||
} | ||
|
||
@Test | ||
public void beatInformation_gives_back_the_number_of_losing_from_choice1_to_choice2() { | ||
createNewBeatTableWithData(); | ||
|
||
assertEquals(pair.losing, beatTable.beatInformation(choice1, choice2, Direction.DIRECTION_BACKWARD)); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/test/java/org/rulez/demokracia/pdengine/BeatTableCompareBeatsLosingTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package org.rulez.demokracia.pdengine; | ||
|
||
import static org.junit.Assert.assertTrue; | ||
|
||
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.exception.ReportedException; | ||
import org.rulez.demokracia.pdengine.testhelpers.CreatedBeatTable; | ||
|
||
@TestedFeature("Schulze method") | ||
@TestedOperation("compare beats") | ||
@TestedBehaviour("if beats tie, looses decide") | ||
public class BeatTableCompareBeatsLosingTest extends CreatedBeatTable { | ||
|
||
@Before | ||
public void setUp() throws ReportedException { | ||
super.setUp(); | ||
} | ||
|
||
@Test | ||
public void compareBeats_gives_back_the_backward_lower_beat1() { | ||
setCompareBeatsAsResult(beats1, beats3); | ||
assertTrue(result.winning == beats1.winning && result.losing == beats1.losing); | ||
} | ||
|
||
@Test | ||
public void compareBeats_gives_back_the_backward_lower_beat2() { | ||
setCompareBeatsAsResult(beats1, beats5); | ||
assertTrue(result.winning == beats5.winning && result.losing == beats5.losing); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/test/java/org/rulez/demokracia/pdengine/BeatTableCompareBeatsTieTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package org.rulez.demokracia.pdengine; | ||
|
||
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.exception.ReportedException; | ||
import org.rulez.demokracia.pdengine.testhelpers.CreatedBeatTable; | ||
|
||
@TestedFeature("Unimplemented") | ||
@TestedOperation("Unimplemented") | ||
@TestedBehaviour("Unimplemented") | ||
public class BeatTableCompareBeatsTieTest extends CreatedBeatTable { | ||
|
||
@Before | ||
public void setUp() throws ReportedException { | ||
super.setUp(); | ||
} | ||
|
||
@Test | ||
public void compareBeats_is_not_yet_implemented_when_the_result_is_tie() { | ||
assertUnimplemented(() -> beatTable.compareBeats(beats1, beats1)); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/test/java/org/rulez/demokracia/pdengine/BeatTableCompareBeatsWinningTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.rulez.demokracia.pdengine; | ||
|
||
import static org.junit.Assert.assertTrue; | ||
|
||
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.exception.ReportedException; | ||
import org.rulez.demokracia.pdengine.testhelpers.CreatedBeatTable; | ||
|
||
@TestedFeature("Schulze method") | ||
@TestedOperation("compare beats") | ||
@TestedBehaviour("if beats are different, the bigger wins") | ||
public class BeatTableCompareBeatsWinningTest extends CreatedBeatTable { | ||
|
||
@Before | ||
public void setUp() throws ReportedException { | ||
super.setUp(); | ||
} | ||
|
||
@Test | ||
public void compareBeats_throws_an_exception_when_first_input_param_is_not_defined() { | ||
assertThrows(() -> beatTable.compareBeats(null, beats2) | ||
).assertMessageIs("Invalid Pair key"); | ||
} | ||
|
||
@Test | ||
public void compareBeats_throws_an_exception_when_second_input_param_is_not_defined() { | ||
assertThrows(() -> beatTable.compareBeats(beats1, null) | ||
).assertMessageIs("Invalid Pair key"); | ||
} | ||
|
||
@Test | ||
public void compareBeats_gives_back_the_forward_bigger_beat1() { | ||
setCompareBeatsAsResult(beats1, beats2); | ||
assertTrue(result.winning == beats1.winning && result.losing == beats1.losing); | ||
} | ||
|
||
@Test | ||
public void compareBeats_gives_back_the_forward_bigger_beat2() { | ||
setCompareBeatsAsResult(beats4, beats3); | ||
assertTrue(result.winning == beats3.winning && result.losing == beats3.losing); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
src/test/java/org/rulez/demokracia/pdengine/testhelpers/CreatedBeatTable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package org.rulez.demokracia.pdengine.testhelpers; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.junit.Before; | ||
import org.rulez.demokracia.pdengine.BeatTable; | ||
import org.rulez.demokracia.pdengine.Choice; | ||
import org.rulez.demokracia.pdengine.dataobjects.Pair; | ||
import org.rulez.demokracia.testhelpers.ThrowableTester; | ||
|
||
public class CreatedBeatTable extends ThrowableTester{ | ||
protected BeatTable beatTable; | ||
protected List<Choice> list; | ||
|
||
protected Pair pair; | ||
protected Pair result; | ||
|
||
protected Choice choice1; | ||
protected Choice choice2; | ||
protected Choice choice3; | ||
|
||
protected Pair beats1; | ||
protected Pair beats2; | ||
protected Pair beats3; | ||
protected Pair beats4; | ||
protected Pair beats5; | ||
|
||
@Before | ||
public void setUp() { | ||
beatTable = new BeatTable(); | ||
list = new ArrayList<Choice>(); | ||
pair = new Pair(4, 5); | ||
choice1 = new Choice("name1", "userName1"); | ||
choice2 = new Choice("name2", "userName2"); | ||
choice3 = new Choice("name3", "userName3"); | ||
list.add(choice1); | ||
list.add(choice2); | ||
list.add(choice3); | ||
|
||
beats1 = new Pair(150, 22); | ||
beats2 = new Pair(100, 40); | ||
beats3 = new Pair(150, 40); | ||
beats4 = new Pair(100, 22); | ||
beats5 = new Pair(150, 10); | ||
} | ||
|
||
protected void createNewBeatTableWithData() { | ||
beatTable = new BeatTable(list); | ||
beatTable.setElement(choice1, choice2, pair); | ||
} | ||
|
||
protected void createNewBeatTableWithComplexData() { | ||
createNewBeatTableWithData(); | ||
beatTable.setElement(choice1, choice2, new Pair(14, 1)); | ||
beatTable.setElement(choice1, choice3, new Pair(13, 2)); | ||
beatTable.setElement(choice2, choice1, new Pair(12, 3)); | ||
beatTable.setElement(choice2, choice3, new Pair(11, 4)); | ||
beatTable.setElement(choice1, choice2, pair); | ||
} | ||
|
||
protected void setGetElementAsResult(final Choice first, final Choice second) { | ||
result = beatTable.getElement(first, second); | ||
} | ||
|
||
protected void setCompareBeatsAsResult(final Pair first, final Pair second) { | ||
result = beatTable.compareBeats(first, second); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters