This repository was archived by the owner on Oct 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathBeatTableTransitiveClosureTest.java
More file actions
77 lines (66 loc) · 2.53 KB
/
BeatTableTransitiveClosureTest.java
File metadata and controls
77 lines (66 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package org.rulez.demokracia.pdengine.beattable;
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;
import org.mockito.InjectMocks;
import org.mockito.junit.MockitoJUnitRunner;
import org.rulez.demokracia.pdengine.annotations.TestedBehaviour;
import org.rulez.demokracia.pdengine.annotations.TestedFeature;
import org.rulez.demokracia.pdengine.annotations.TestedOperation;
@TestedFeature("Schulze method")
@TestedOperation("compare beats")
@TestedBehaviour("implements the Floyd-Warshall algorithm")
@RunWith(MockitoJUnitRunner.class)
public class BeatTableTransitiveClosureTest {
@InjectMocks
private BeatTableTransitiveClosureServiceImpl beatTableTransitiveClosureService;
private BeatTable beatTable;
@Before
public void setUp() {
beatTable = createNewBeatTableWithComplexData();
}
@Test
public void transitive_closure_on_empty_beat_table_results_empty_result() {
BeatTable actual = new BeatTable();
actual = beatTableTransitiveClosureService.computeTransitiveClosure(actual);
assertTrue(actual.getKeyCollection().isEmpty());
}
@Test
public void transitive_closure_computes_the_shortest_paths_by_pairs() {
beatTable =
beatTableTransitiveClosureService.computeTransitiveClosure(beatTable);
final Collection<String> keyCollection = beatTable.getKeyCollection();
for (final String i : keyCollection)
for (final String j : keyCollection) {
if (i.equals(j))
continue;
assertNoShorterPathBetweenChoices(keyCollection, i, j);
}
}
@Test
public void test_transitive_closure_compute_optimal_route() {
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
) {
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));
assertTrue(greater.compareTo(less) >= 0);
}
}
private Pair lessBeat(final Pair beat1, final Pair beat2) {
return beat1.compareTo(beat2) >= 0 ? beat2 : beat1;
}
}