[52] | 1 | package geniusweb.profile.utilityspace;
|
---|
| 2 |
|
---|
| 3 | import static org.junit.Assert.assertEquals;
|
---|
| 4 | import static org.junit.Assert.assertTrue;
|
---|
| 5 |
|
---|
| 6 | import java.io.IOException;
|
---|
| 7 | import java.math.BigDecimal;
|
---|
| 8 | import java.util.Arrays;
|
---|
| 9 | import java.util.HashMap;
|
---|
| 10 | import java.util.LinkedList;
|
---|
| 11 | import java.util.List;
|
---|
| 12 | import java.util.Map;
|
---|
| 13 |
|
---|
| 14 | import org.junit.Before;
|
---|
| 15 | import org.junit.Test;
|
---|
| 16 |
|
---|
| 17 | import com.fasterxml.jackson.core.JsonParseException;
|
---|
| 18 | import com.fasterxml.jackson.databind.JsonMappingException;
|
---|
| 19 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
| 20 |
|
---|
| 21 | import geniusweb.issuevalue.DiscreteValue;
|
---|
| 22 | import geniusweb.issuevalue.Value;
|
---|
| 23 | import tudelft.utilities.junit.GeneralTests;
|
---|
| 24 |
|
---|
| 25 | public class PartsUtilitiesTest extends GeneralTests<PartsUtilities> {
|
---|
| 26 | private static final String iss2 = "issue2";
|
---|
| 27 |
|
---|
| 28 | private static final String iss1 = "issue1";
|
---|
| 29 |
|
---|
| 30 | private final ObjectMapper jackson = new ObjectMapper();
|
---|
| 31 |
|
---|
| 32 | private static BigDecimal N01 = new BigDecimal("0.1"),
|
---|
| 33 | N02 = new BigDecimal("0.2"), N03 = new BigDecimal("0.3"),
|
---|
| 34 | N05 = new BigDecimal("0.5"), N07 = new BigDecimal("0.7"),
|
---|
| 35 | N08 = new BigDecimal("0.8"), N09 = new BigDecimal("0.9");
|
---|
| 36 | private final static DiscreteValue low = new DiscreteValue("low");
|
---|
| 37 | private final static DiscreteValue high = new DiscreteValue("high");
|
---|
| 38 |
|
---|
| 39 | private final static String jsontext = "{\"PartsUtilities\":{\"issues\":[\"issue1\",\"issue2\"],\"utilslist\":[{\"values\":[\"low\",\"low\"],\"util\":0.3},{\"values\":[\"high\",\"high\"],\"util\":0.9}]}}";
|
---|
| 40 |
|
---|
| 41 | private PartsUtilities parts1, parts1a, parts2, parts3;
|
---|
| 42 |
|
---|
| 43 | @Before
|
---|
| 44 | public void before() {
|
---|
| 45 | List<PartUtil> vals = new LinkedList<>();
|
---|
| 46 | vals.add(new PartUtil(Arrays.asList(low, low), N03));
|
---|
| 47 | vals.add(new PartUtil(Arrays.asList(high, high), N09));
|
---|
| 48 | List<PartUtil> vals1 = new LinkedList<>();
|
---|
| 49 | vals1.add(new PartUtil(Arrays.asList(low, low), N09));
|
---|
| 50 | List<String> issues = Arrays.asList(iss1, iss2);
|
---|
| 51 | parts1 = new PartsUtilities(issues, vals);
|
---|
| 52 | parts1a = new PartsUtilities(issues, vals);
|
---|
| 53 | parts2 = new PartsUtilities(Arrays.asList(iss1), vals);
|
---|
| 54 | parts3 = new PartsUtilities(issues, vals1);
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | @Override
|
---|
| 58 | public List<List<PartsUtilities>> getGeneralTestData() {
|
---|
| 59 | return Arrays.asList(Arrays.asList(parts1, parts1a),
|
---|
| 60 | Arrays.asList(parts2), Arrays.asList(parts3));
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | @Override
|
---|
| 64 | public List<String> getGeneralTestStrings() {
|
---|
| 65 | return Arrays.asList(
|
---|
| 66 | "PartsUtilities..issue1, issue2.,\\{.\"low\", \"low\".=0.3, .\"high\", \"high\".=0.9\\}.",
|
---|
| 67 | "PartsUtilities..issue1.,\\{.\"low\", \"low\".=0.3, .\"high\", \"high\".=0.9\\}.",
|
---|
| 68 | "PartsUtilities..issue1, issue2.,\\{.\"low\", \"low\".=0.9\\}.");
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | @Test(expected = IllegalArgumentException.class)
|
---|
| 72 | public void testWeightsTooHigh() {
|
---|
| 73 | Map<ProductOfValue, BigDecimal> utils = new HashMap<>();
|
---|
| 74 | utils.put(new ProductOfValue(Arrays.asList(low, low)),
|
---|
| 75 | new BigDecimal("1.1"));
|
---|
| 76 | new PartsUtilities(Arrays.asList(iss1, iss2), utils);
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | @Test(expected = IllegalArgumentException.class)
|
---|
| 80 | public void testWeightsTooLow() {
|
---|
| 81 | Map<ProductOfValue, BigDecimal> utils = new HashMap<>();
|
---|
| 82 | utils.put(new ProductOfValue(Arrays.asList(low, low)),
|
---|
| 83 | new BigDecimal("-0.3"));
|
---|
| 84 | new PartsUtilities(Arrays.asList(iss1, iss2), utils);
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | @Test
|
---|
| 88 | public void testBasicSerialize()
|
---|
| 89 | throws JsonParseException, JsonMappingException, IOException {
|
---|
| 90 | System.out.println(jsontext);
|
---|
| 91 | assertEquals(jsontext, jackson.writeValueAsString(parts1));
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | @Test
|
---|
| 95 | public void testBasicDeserialize()
|
---|
| 96 | throws JsonParseException, JsonMappingException, IOException {
|
---|
| 97 | System.out.println(jsontext);
|
---|
| 98 | assertEquals(parts1, jackson.readValue(jsontext, PartsUtilities.class));
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | @Test
|
---|
| 102 | public void testGetUtility() {
|
---|
| 103 | assertEquals(N03,
|
---|
| 104 | parts1.getUtility(new ProductOfValue(Arrays.asList(low, low))));
|
---|
| 105 | assertEquals(N09, parts1
|
---|
| 106 | .getUtility(new ProductOfValue(Arrays.asList(high, high))));
|
---|
| 107 | assertEquals(BigDecimal.ZERO, parts1
|
---|
| 108 | .getUtility(new ProductOfValue(Arrays.asList(low, high))));
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | @Test
|
---|
| 112 | public void testGetIssues() {
|
---|
| 113 | assertEquals(Arrays.asList(iss1, iss2), parts1.getIssues());
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | @Test
|
---|
| 117 | public void testGetIncorrectValue() {
|
---|
| 118 | assertEquals(BigDecimal.ZERO, parts1.getUtility(low));
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | @Test
|
---|
| 122 | public void testGetValue() {
|
---|
| 123 | assertTrue(N03.compareTo(parts1
|
---|
| 124 | .getUtility(new ProductOfValue(Arrays.asList(low, low)))) == 0);
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | @Test
|
---|
| 128 | public void testMerge() {
|
---|
| 129 | Map<ProductOfValue, BigDecimal> extravals = new HashMap<>();
|
---|
| 130 | extravals.put(new ProductOfValue(Arrays.asList(low, low)),
|
---|
| 131 | N01.multiply(N05));
|
---|
| 132 | extravals.put(new ProductOfValue(Arrays.asList(high, high)),
|
---|
| 133 | N01.multiply(N07));
|
---|
| 134 | PartsUtilities extraparts = new PartsUtilities(
|
---|
| 135 | Arrays.asList("extra1", "extra2"), extravals);
|
---|
| 136 |
|
---|
| 137 | PartsUtilities merged = parts1.add(extraparts);
|
---|
| 138 |
|
---|
| 139 | Value pval = new ProductOfValue(Arrays.asList(low, low, high, high));
|
---|
| 140 | assertEquals(0,
|
---|
| 141 | N03.add(N01.multiply(N07)).compareTo(merged.getUtility(pval)));
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | }
|
---|