Skip to content

Remove ContainingBeats since it has been used only in tests. #335

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Pair> implements ContainingBeats {
public class BeatTable extends MapMatrix<String, Pair> {

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) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where are these functionalities tested?

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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test data in a unit test. Test data obtained through a method.

)
.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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test data in a unit test.

)
.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");
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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")
Expand All @@ -25,7 +25,7 @@ public class BeatTableNormalizationTest extends ThrowableTester {

@Before
public void setUp() {
BeatTable beatTable = createNewBeatTableWithComplexData();
final BeatTable beatTable = createNewBeatTableWithComplexData();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test data obtained through a method

normalizedBeatTable = beatTableService.normalize(beatTable);
}

Expand All @@ -36,62 +36,69 @@ 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);
}

@TestedBehaviour("the elements for ties are (0,0)")
@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);
}

@TestedBehaviour("the elements for ties are (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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test data created in situ below.

final Collection<String> keyCollection = beatTable.getKeyCollection();
for (final String i : keyCollection)
for (final String j : keyCollection) {
Expand All @@ -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<String> keyCollection,
final String choice1, final String choice2) {
private void assertNoShorterPathBetweenChoices(
final Collection<String> 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);
}
}

Expand Down
Loading