1 | package shineagent;
|
---|
2 |
|
---|
3 | import static org.junit.Assert.assertEquals;
|
---|
4 | import static org.junit.Assert.assertTrue;
|
---|
5 | import static org.mockito.Mockito.mock;
|
---|
6 | import static org.mockito.Mockito.when;
|
---|
7 |
|
---|
8 | import java.math.BigDecimal;
|
---|
9 | import java.util.Arrays;
|
---|
10 | import java.util.Collections;
|
---|
11 | import java.util.List;
|
---|
12 |
|
---|
13 | import org.junit.Before;
|
---|
14 | import org.junit.Test;
|
---|
15 |
|
---|
16 | import geniusweb.issuevalue.Bid;
|
---|
17 | import geniusweb.issuevalue.Domain;
|
---|
18 | import geniusweb.profile.DefaultPartialOrdering;
|
---|
19 | import shineagent.SimpleLinearOrdering;
|
---|
20 |
|
---|
21 | public class SimpleLinearOrderingTest {
|
---|
22 | private DefaultPartialOrdering realprofile = mock(
|
---|
23 | DefaultPartialOrdering.class);;
|
---|
24 | private Domain domain = mock(Domain.class);
|
---|
25 | private SimpleLinearOrdering estprofile;
|
---|
26 | private Bid bid1 = makeBid(1);
|
---|
27 | private Bid bid2 = makeBid(2);
|
---|
28 | private Bid bid3 = makeBid(3);
|
---|
29 | private Bid bid4 = makeBid(4);
|
---|
30 | private final List<Bid> emptyList = Collections.emptyList();
|
---|
31 |
|
---|
32 | @Before
|
---|
33 | public void before() {
|
---|
34 | when(realprofile.getDomain()).thenReturn(domain);
|
---|
35 | estprofile = new SimpleLinearOrdering(realprofile);
|
---|
36 | when(bid1.toString()).thenReturn("bid1");
|
---|
37 |
|
---|
38 | }
|
---|
39 |
|
---|
40 | private static Bid makeBid(int n) {
|
---|
41 | Bid mockbid = mock(Bid.class);
|
---|
42 | when(mockbid.toString()).thenReturn("bid " + n);
|
---|
43 | return mockbid;
|
---|
44 | }
|
---|
45 |
|
---|
46 | @Test
|
---|
47 | public void testSmoke() {
|
---|
48 | }
|
---|
49 |
|
---|
50 | @Test
|
---|
51 | public void testInitialBids() {
|
---|
52 | assertTrue(estprofile.getBids().isEmpty());
|
---|
53 | }
|
---|
54 |
|
---|
55 | @Test
|
---|
56 | public void largerInitialListTest() {
|
---|
57 | when(realprofile.getBids()).thenReturn(Arrays.asList(bid1, bid2, bid3));
|
---|
58 | // idea is that 3 > 2 > 1
|
---|
59 | when(realprofile.isPreferredOrEqual(bid2, bid1)).thenReturn(true);
|
---|
60 | when(realprofile.isPreferredOrEqual(bid1, bid2)).thenReturn(false);
|
---|
61 | when(realprofile.isPreferredOrEqual(bid3, bid2)).thenReturn(true);
|
---|
62 | when(realprofile.isPreferredOrEqual(bid2, bid3)).thenReturn(false);
|
---|
63 | when(realprofile.isPreferredOrEqual(bid3, bid1)).thenReturn(true);
|
---|
64 | when(realprofile.isPreferredOrEqual(bid1, bid3)).thenReturn(false);
|
---|
65 | SimpleLinearOrdering testprofile = new SimpleLinearOrdering(
|
---|
66 | realprofile);
|
---|
67 | assertEquals(Arrays.asList(bid1, bid2, bid3), testprofile.getBids());
|
---|
68 | }
|
---|
69 |
|
---|
70 | @Test
|
---|
71 | public void testAddFirstBid() {
|
---|
72 | SimpleLinearOrdering prof1 = estprofile.with(bid1,
|
---|
73 | Collections.emptyList());
|
---|
74 |
|
---|
75 | assertEquals(1, prof1.getBids().size());
|
---|
76 | }
|
---|
77 |
|
---|
78 | @Test
|
---|
79 | public void testAddTwoBidsSecondIsWorse() {
|
---|
80 | SimpleLinearOrdering prof1 = estprofile.with(bid1, emptyList);
|
---|
81 | // new bid is worse as bid1, so list of worse bids is empty
|
---|
82 | SimpleLinearOrdering prof2 = prof1.with(bid2, emptyList);
|
---|
83 |
|
---|
84 | assertEquals(Arrays.asList(bid2, bid1), prof2.getBids());
|
---|
85 | }
|
---|
86 |
|
---|
87 | @Test
|
---|
88 | public void testAddTwoBidsSecondIsBetter() {
|
---|
89 | SimpleLinearOrdering prof1 = estprofile.with(bid1, emptyList);
|
---|
90 | SimpleLinearOrdering prof2 = prof1.with(bid2, Arrays.asList(bid1));
|
---|
91 |
|
---|
92 | assertEquals(Arrays.asList(bid1, bid2), prof2.getBids());
|
---|
93 | }
|
---|
94 |
|
---|
95 | @Test
|
---|
96 | public void testAddThreeBidsThirdIsMiddle() {
|
---|
97 | SimpleLinearOrdering prof1 = new SimpleLinearOrdering(domain,
|
---|
98 | Arrays.asList(bid1, bid2));
|
---|
99 | SimpleLinearOrdering prof2 = prof1.with(bid3, Arrays.asList(bid1));
|
---|
100 |
|
---|
101 | assertEquals(Arrays.asList(bid1, bid3, bid2), prof2.getBids());
|
---|
102 | }
|
---|
103 |
|
---|
104 | @Test
|
---|
105 | public void getUtilityTest3Bids() {
|
---|
106 | SimpleLinearOrdering prof1 = new SimpleLinearOrdering(domain,
|
---|
107 | Arrays.asList(bid1, bid3, bid2));
|
---|
108 |
|
---|
109 | assertEquals(new BigDecimal("0.50000000"), prof1.getUtility(bid3));
|
---|
110 | }
|
---|
111 |
|
---|
112 | @Test
|
---|
113 | public void getUtilityTest4Bids() {
|
---|
114 | // with 4 bids, we get utility 1/3 which causes deximal expansiion issue
|
---|
115 | SimpleLinearOrdering prof1 = new SimpleLinearOrdering(domain,
|
---|
116 | Arrays.asList(bid1, bid2, bid3, bid4));
|
---|
117 |
|
---|
118 | assertEquals(new BigDecimal("0.33333333"), prof1.getUtility(bid2));
|
---|
119 | }
|
---|
120 |
|
---|
121 | }
|
---|