source: voting/src/test/java/geniusweb/voting/CollectedVotesTest.java@ 52

Last change on this file since 52 was 52, checked in by ruud, 14 months ago

Fixed small issues in domaineditor.

File size: 11.6 KB
Line 
1package geniusweb.voting;
2
3import static org.junit.Assert.assertEquals;
4import static org.junit.Assert.assertNotEquals;
5import static org.junit.Assert.assertTrue;
6import static org.mockito.Mockito.mock;
7
8import java.util.Arrays;
9import java.util.Collections;
10import java.util.HashMap;
11import java.util.HashSet;
12import java.util.Iterator;
13import java.util.LinkedList;
14import java.util.List;
15import java.util.Map;
16import java.util.Set;
17
18import org.junit.Before;
19import org.junit.Test;
20
21import geniusweb.actions.PartyId;
22import geniusweb.actions.Vote;
23import geniusweb.actions.Votes;
24import geniusweb.issuevalue.Bid;
25import tudelft.utilities.junit.GeneralTests;
26
27public class CollectedVotesTest extends GeneralTests<CollectedVotes> {
28
29 private final Bid bidA = mock(Bid.class), bidB = mock(Bid.class);
30 private final Bid a = mock(Bid.class), b = mock(Bid.class),
31 c = mock(Bid.class), d = mock(Bid.class);
32 private final PartyId party1 = new PartyId("party1");
33 private final PartyId party2 = new PartyId("party2");
34 private final PartyId party3 = new PartyId("party3");
35 private final PartyId party4 = new PartyId("party4");
36
37 private final PartyId partyP = new PartyId("partyP");
38 private final PartyId partyQ = new PartyId("partyQ");
39 private final PartyId partyR = new PartyId("partyR");
40 private final PartyId partyS = new PartyId("partyS");
41
42 private final Vote votePA2 = new Vote(partyP, bidA, 2, 9);
43 private final Vote voteQA2 = new Vote(partyQ, bidA, 2, 9);
44 private final Vote voteQA3 = new Vote(partyQ, bidA, 3, 9);
45 private final Vote voteQB3 = new Vote(partyQ, bidB, 3, 9);
46 private final Vote voteQB2 = new Vote(partyQ, bidB, 2, 9);
47 private final Vote voteRA2 = new Vote(partyR, bidA, 2, 9);
48 private final Vote voteRA3 = new Vote(partyR, bidA, 3, 9);
49 private final Vote voteRA4 = new Vote(partyR, bidA, 4, 9);
50 private final Vote voteRB2 = new Vote(partyR, bidB, 2, 9);
51
52 private Map<PartyId, Integer> allpowers = new HashMap<>();
53
54 private CollectedVotes cv1 = new CollectedVotes(Collections.emptyMap(),
55 Collections.emptyMap());
56 private CollectedVotes cv1a = new CollectedVotes(Collections.emptyMap(),
57 Collections.emptyMap());
58
59 private Votes votes = new Votes(partyP, Collections.singleton(votePA2));
60 private CollectedVotes cv2 = new CollectedVotes(
61 Collections.singletonMap(partyP, votes),
62 Collections.singletonMap(partyP, 1));
63
64 @Override
65 public List<List<CollectedVotes>> getGeneralTestData() {
66 return Arrays.asList(Arrays.asList(cv1, cv1a), Arrays.asList(cv2));
67 }
68
69 @Override
70 public List<String> getGeneralTestStrings() {
71 return Arrays.asList("CollectedVotes.\\{\\}.*\\{\\}.*",
72 "CollectedVotes.\\{partyP=Votes.*Vote.partyP,Mock.*,2.*\\}.*\\{partyP=1\\}.*");
73 }
74
75 @Before
76 public void before() {
77 allpowers.put(partyP, 1);
78 allpowers.put(partyQ, 1);
79 allpowers.put(partyR, 1);
80 allpowers.put(partyS, 1);
81 }
82
83 @Test
84 public void testGetAllBids() {
85 Map<PartyId, Votes> allvotes = new HashMap<>();
86 allvotes.put(partyP, new Votes(partyP, Collections.singleton(votePA2)));
87 allvotes.put(partyQ, new Votes(partyQ,
88 new HashSet<>(Arrays.asList(voteQA2, voteQB3))));
89
90 Map<Bid, Set<Vote>> allbids = new CollectedVotes(allvotes, allpowers)
91 .getAllBids();
92 assertEquals(2, allbids.size());
93 assertEquals(new HashSet<>(Arrays.asList(votePA2, voteQA2)),
94 allbids.get(bidA));
95 assertEquals(new HashSet<>(Arrays.asList(voteQB3)), allbids.get(bidB));
96 }
97
98 @Test
99 public void testGetMaxSubset2() {
100 Set<Vote> votes = new HashSet<>(Arrays.asList(votePA2, voteQA2));
101 CollectedVotes cv = new CollectedVotes(Collections.emptyMap(),
102 allpowers);
103 Set<PartyId> parties = cv.getMaxPowerGroup(votes);
104 assertEquals(new HashSet<>(Arrays.asList(partyP, partyQ)), parties);
105
106 // test the optimized code similarly
107 parties = cv.getMaxPowerGroupBreathFirst(votes);
108 assertEquals(new HashSet<>(Arrays.asList(partyP, partyQ)), parties);
109
110 }
111
112 @Test
113 public void testGetMaxSubset3() {
114 Set<Vote> votes = new HashSet<>(
115 Arrays.asList(votePA2, voteQA2, voteRA4));
116 CollectedVotes cv = new CollectedVotes(Collections.emptyMap(),
117 allpowers);
118
119 Set<PartyId> parties = cv.getMaxPowerGroup(votes);
120 // party R wants 4 votes so we can't satisfy
121 assertEquals(new HashSet<>(Arrays.asList(partyP, partyQ)), parties);
122
123 // test the optimized code similarly
124 parties = cv.getMaxPowerGroupBreathFirst(votes);
125 assertEquals(new HashSet<>(Arrays.asList(partyP, partyQ)), parties);
126 }
127
128 @Test
129 public void testGetMaxSubset3b() {
130 Set<Vote> votes = new HashSet<>(
131 Arrays.asList(votePA2, voteQA2, voteRA3));
132 CollectedVotes cv = new CollectedVotes(Collections.emptyMap(),
133 allpowers);
134 Set<PartyId> parties = cv.getMaxPowerGroup(votes);
135 // party R wants 4 votes so we can't satisfy
136 assertEquals(new HashSet<>(Arrays.asList(partyP, partyQ, partyR)),
137 parties);
138
139 // test the optimized code similarly
140 parties = cv.getMaxPowerGroupBreathFirst(votes);
141 assertEquals(new HashSet<>(Arrays.asList(partyP, partyQ, partyR)),
142 parties);
143 }
144
145 @Test
146 public void testNoConsensus() {
147 Set<Vote> votes = new HashSet<>(Arrays.asList(voteQA3, voteRA3));
148 CollectedVotes cv = new CollectedVotes(Collections.emptyMap(),
149 allpowers);
150 assertEquals(Collections.emptySet(), cv.getMaxPowerGroup(votes));
151 assertEquals(Collections.emptySet(),
152 cv.getMaxPowerGroupBreathFirst(votes));
153 }
154
155 @Test
156 public void testGetMaxAgreements() {
157 Map<PartyId, Votes> allvotes = new HashMap<>();
158 allvotes.put(partyP, new Votes(partyP, Collections.singleton(votePA2)));
159 allvotes.put(partyQ, new Votes(partyQ,
160 new HashSet<>(Arrays.asList(voteQA2, voteQB2))));
161 allvotes.put(partyR, new Votes(partyR,
162 new HashSet<>(Arrays.asList(voteRA4, voteRB2))));
163
164 Map<Bid, Set<PartyId>> allagrees = new CollectedVotes(allvotes,
165 allpowers).getMaxAgreements();
166 assertEquals(new HashSet<>(Arrays.asList(partyP, partyQ)),
167 allagrees.get(bidA));
168 assertEquals(new HashSet<>(Arrays.asList(partyQ, partyR)),
169 allagrees.get(bidB));
170
171 }
172
173 @Test
174 public void testGetNoAgreements() {
175 Map<PartyId, Votes> allvotes = new HashMap<>();
176 allvotes.put(partyP, new Votes(partyP, Collections.singleton(votePA2)));
177 Map<Bid, Set<PartyId>> allagrees = new CollectedVotes(allvotes,
178 allpowers).getMaxAgreements();
179 assertEquals(Collections.emptyMap(), allagrees);
180
181 }
182
183 @Test
184 public void getConsensusGroupsTest() {
185 CollectedVotes cv = new CollectedVotes(Collections.emptyMap(),
186 allpowers);
187
188 Set<Set<PartyId>> consensus = cv.getConsensusGroups(
189 new HashSet<>(Arrays.asList(votePA2, voteQA2)));
190 // should give 1 solution: both parties join in.
191 System.out.println(consensus);
192 assertEquals(1, consensus.size());
193 assertEquals(2, consensus.iterator().next().size());
194 }
195
196 @Test
197 public void getConsensusGroupsTestPQR() {
198 CollectedVotes cv = new CollectedVotes(Collections.emptyMap(),
199 allpowers);
200
201 Set<Set<PartyId>> consensus = cv.getConsensusGroups(
202 new HashSet<>(Arrays.asList(votePA2, voteQA2, voteRA4)));
203 // should give 1 solution: P and Q join in. 4 is unreachable.
204 System.out.println(consensus);
205 assertEquals(1, consensus.size());
206 assertEquals(2, consensus.iterator().next().size());
207 }
208
209 @Test
210 public void getConsensusGroupsTestPQR2() {
211 CollectedVotes cv = new CollectedVotes(Collections.emptyMap(),
212 allpowers);
213
214 Set<Set<PartyId>> consensus = cv.getConsensusGroups(
215 new HashSet<>(Arrays.asList(votePA2, voteQA2, voteRA2)));
216 // should give 4 solutions: P+Q, P+R, Q+R and P+Q+R
217 System.out.println(consensus);
218 assertEquals(4, consensus.size());
219 }
220
221 @Test
222 public void getConsensusGroupsTestRExtraPower() {
223 // give R extra power to reach the 4 if other 2 join
224 allpowers = new HashMap<>();
225 allpowers.put(partyP, 1);
226 allpowers.put(partyQ, 1);
227 allpowers.put(partyR, 2);
228
229 CollectedVotes cv = new CollectedVotes(Collections.emptyMap(),
230 allpowers);
231
232 Set<Set<PartyId>> consensus = cv.getConsensusGroups(
233 new HashSet<>(Arrays.asList(votePA2, voteQA2, voteRA4)));
234 // should give solutions P+Q and P+Q+R
235 System.out.println(consensus);
236 assertEquals(2, consensus.size());
237 Iterator<Set<PartyId>> it = consensus.iterator();
238 assertEquals(2, it.next().size());
239 assertEquals(3, it.next().size());
240 }
241
242 @Test
243 public void testWith() {
244 Map<PartyId, Votes> allvotes = new HashMap<>();
245 allvotes.put(partyP, new Votes(partyP, Collections.singleton(votePA2)));
246 CollectedVotes cv = new CollectedVotes(allvotes, allpowers);
247 assertEquals(1, cv.getVotes().keySet().size());
248 cv = cv.with(new Votes(partyQ, Collections.singleton(voteQA2)), 2);
249 assertEquals(2, cv.getVotes().keySet().size());
250 }
251
252 @Test
253 public void testWithout() {
254 Map<PartyId, Votes> allvotes = new HashMap<>();
255 allvotes.put(partyP, new Votes(partyP, Collections.singleton(votePA2)));
256 allvotes.put(partyQ, new Votes(partyQ,
257 new HashSet<>(Arrays.asList(voteQA2, voteQB2))));
258 allvotes.put(partyR, new Votes(partyR,
259 new HashSet<>(Arrays.asList(voteRA4, voteRB2))));
260
261 // give R extra power to reach the 4 if other 2 join
262 allpowers = new HashMap<>();
263 allpowers.put(partyP, 1);
264 allpowers.put(partyQ, 1);
265 allpowers.put(partyR, 2);
266
267 CollectedVotes cv = new CollectedVotes(allvotes, allpowers);
268
269 assertEquals(3, cv.getVotes().size());
270 cv = cv.without(new HashSet<>(Arrays.asList(partyP, partyQ)));
271 assertEquals(1, cv.getVotes().size());
272 assertEquals(1, cv.getPowers().size());
273
274 }
275
276 @Test
277 public void getConsensusGroupMaxPower() {
278 Vote P = new Vote(partyP, bidA, 2, 3);
279 Vote Q = new Vote(partyQ, bidA, 2, 3);
280 Vote R = new Vote(partyR, bidA, 2, 3);
281 Vote S = new Vote(partyS, bidA, 2, 3);
282 Map<PartyId, Votes> allvotes = new HashMap<>();
283 allvotes.put(partyP, new Votes(partyP, Collections.singleton(P)));
284 allvotes.put(partyQ, new Votes(partyQ, Collections.singleton(Q)));
285 allvotes.put(partyR, new Votes(partyR, Collections.singleton(R)));
286 allvotes.put(partyS, new Votes(partyS, Collections.singleton(S)));
287
288 CollectedVotes cv = new CollectedVotes(allvotes, allpowers);
289 Set<Set<PartyId>> consensus = cv.getConsensusGroups(
290 new HashSet<Vote>(Arrays.asList(P, Q, R, S)));
291 System.out.println(consensus);
292 // check that all are size 2 or 3.
293 for (Set<PartyId> set : consensus) {
294 assertTrue(set.size() == 2 || set.size() == 3);
295 }
296
297 }
298
299 @Test
300 public void getConsensusGroupMaxPowerModifiedPartyPowers() {
301 // party R gets power 2.
302 Vote P = new Vote(partyP, bidA, 2, 3);
303 Vote Q = new Vote(partyQ, bidA, 2, 3);
304 Vote R = new Vote(partyR, bidA, 2, 3);
305 Map<PartyId, Votes> allvotes = new HashMap<>();
306 allvotes.put(partyP, new Votes(partyP, Collections.singleton(P)));
307 allvotes.put(partyQ, new Votes(partyQ, Collections.singleton(Q)));
308 allvotes.put(partyR, new Votes(partyR, Collections.singleton(R)));
309
310 Map<PartyId, Integer> powers = new HashMap<PartyId, Integer>();
311 powers.put(partyP, 1);
312 powers.put(partyQ, 1);
313 powers.put(partyR, 2);
314
315 CollectedVotes cv = new CollectedVotes(allvotes, powers);
316 Set<Set<PartyId>> consensus = cv
317 .getConsensusGroups(new HashSet<Vote>(Arrays.asList(P, Q, R)));
318 System.out.println(consensus);
319 // R has power 2 now. That means groups of size 3 are not posisble.
320 // check that all are size 2.
321 for (Set<PartyId> set : consensus) {
322 assertTrue(set.size() == 2);
323 }
324
325 }
326
327 @Test
328 public void testRandomOrderOfSet() {
329 // this test that a Set of parties does not have
330 // the fixed order of the list #1878
331 // not really testing our code but crucial for proper working.
332 List<PartyId> list = Arrays.asList(partyQ, partyR, partyS);
333 // #1878 does Set randomize somewhat?
334 Set<PartyId> parties = new HashSet<>(list);
335 assertNotEquals(list, new LinkedList<>(parties));
336
337 LinkedList<PartyId> list2 = new LinkedList<>(list);
338 list2.add(partyP);
339 parties = new HashSet<>(list2);
340 assertNotEquals(list2, new LinkedList<>(parties));
341
342 }
343}
Note: See TracBrowser for help on using the repository browser.