| 1 | package de.uka.ipd.sdq.probfunction.math.test; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.List; |
| 5 | |
| 6 | import junit.framework.Assert; |
| 7 | |
| 8 | import org.junit.Before; |
| 9 | import org.junit.Test; |
| 10 | |
| 11 | import de.uka.ipd.sdq.probfunction.math.IProbabilityFunctionFactory; |
| 12 | import de.uka.ipd.sdq.probfunction.math.IProbabilityMassFunction; |
| 13 | import de.uka.ipd.sdq.probfunction.math.ISample; |
| 14 | import de.uka.ipd.sdq.probfunction.math.IUnit; |
| 15 | import de.uka.ipd.sdq.probfunction.math.exception.DifferentDomainsException; |
| 16 | import de.uka.ipd.sdq.probfunction.math.exception.FunctionNotInTimeDomainException; |
| 17 | import de.uka.ipd.sdq.probfunction.math.exception.InvalidSampleValueException; |
| 18 | import de.uka.ipd.sdq.probfunction.math.exception.NegativeDistanceException; |
| 19 | import de.uka.ipd.sdq.probfunction.math.exception.ProbabilitySumNotOneException; |
| 20 | import de.uka.ipd.sdq.probfunction.math.exception.UnitNameNotSetException; |
| 21 | import de.uka.ipd.sdq.probfunction.math.exception.UnitNotSetException; |
| 22 | import de.uka.ipd.sdq.probfunction.math.exception.UnorderedDomainException; |
| 23 | |
| 24 | public class ProbabilityMassFunctionTest { |
| 25 | |
| 26 | // unordered domain |
| 27 | IProbabilityMassFunction u1, u1extended, u1exDiffProbs, u1same, u2; |
| 28 | |
| 29 | // ordered domain |
| 30 | IProbabilityMassFunction o1, o1extended, o1exDiffProbs, o1same, o2; |
| 31 | |
| 32 | @Before |
| 33 | public void setUp() throws Exception { |
| 34 | u1 = createPMF(new Object[]{"car", 0.1, "house", 0.5, "bike", 0.3, |
| 35 | "street", 0.1}, false); |
| 36 | u1same = createPMF(new Object[]{"car", 0.2, "house", 0.3, "bike", 0.4, |
| 37 | "street", 0.1}, false); |
| 38 | |
| 39 | u1extended = createPMF(new Object[]{"car", 0.1, "house", 0.5, "bike", |
| 40 | 0.3, "street", 0.1, "tree", 0.0}, false); |
| 41 | u1exDiffProbs = createPMF(new Object[]{"car", 0.1, "house", 0.15, |
| 42 | "bike", 0.3, "street", 0.1, "tree", 3.5}, false); |
| 43 | u2 = createPMF(new Object[]{"dog", 0.2, "cat", 0.3, "pig", 0.4, "cow", |
| 44 | 0.1}, false); |
| 45 | |
| 46 | o1 = createPMF(new Object[]{0.1, 0.2, 0.2, 0.3, 0.3, 0.4, 0.4, 0.1}, |
| 47 | true); |
| 48 | o1same = createPMF( |
| 49 | new Object[]{0.1, 0.1, 0.2, 0.4, 0.3, 0.2, 0.4, 0.3}, true); |
| 50 | |
| 51 | o1extended = createPMF(new Object[]{0.1, 0.2, 0.2, 0.3, 0.3, 0.4, 0.4, |
| 52 | 0.1, 0.5, 0.0}, true); |
| 53 | o1exDiffProbs = createPMF(new Object[]{0.1, 0.1, 0.2, 0.15, 0.3, 0.25, |
| 54 | 0.4, 0.3, 0.5, 0.1}, true); |
| 55 | o2 = createPMF(new Object[]{0.2, 0.2, 0.4, 0.3, 0.6, 0.4, 0.8, 0.1}, |
| 56 | true); |
| 57 | } |
| 58 | |
| 59 | @Test |
| 60 | public void testCreatePMF() { |
| 61 | Assert.assertEquals(4, o1.getSamples().size()); |
| 62 | Assert.assertEquals(4, u1.getSamples().size()); |
| 63 | Assert.assertEquals(5, u1extended.getSamples().size()); |
| 64 | } |
| 65 | |
| 66 | @Test |
| 67 | public void equalsUnordered() { |
| 68 | Assert.assertTrue(u1.equals(u1)); |
| 69 | |
| 70 | IProbabilityMassFunction u1copy = createPMF(new Object[]{"car", 0.1, |
| 71 | "house", 0.5, "bike", 0.3, "street", 0.1}, false); |
| 72 | Assert.assertTrue(u1.equals(u1copy)); |
| 73 | |
| 74 | Assert.assertFalse(u1.equals(u1same)); |
| 75 | |
| 76 | Assert.assertFalse(u1.equals(u2)); |
| 77 | |
| 78 | Assert.assertFalse(u1extended.equals(u1exDiffProbs)); |
| 79 | |
| 80 | Assert.assertFalse(u1.equals(u1extended)); |
| 81 | |
| 82 | } |
| 83 | |
| 84 | @Test |
| 85 | public void equalsOrdered() { |
| 86 | Assert.assertTrue(o1.equals(o1)); |
| 87 | |
| 88 | IProbabilityMassFunction o1copy = createPMF(new Object[]{0.1, 0.2, 0.2, |
| 89 | 0.3, 0.3, 0.4, 0.4, 0.1}, true); |
| 90 | Assert.assertTrue(o1.equals(o1copy)); |
| 91 | |
| 92 | Assert.assertFalse(o1.equals(o1same)); |
| 93 | |
| 94 | Assert.assertFalse(o1.equals(o2)); |
| 95 | |
| 96 | Assert.assertFalse(o1extended.equals(o1exDiffProbs)); |
| 97 | |
| 98 | Assert.assertFalse(o1.equals(o1extended)); |
| 99 | } |
| 100 | |
| 101 | @Test |
| 102 | public void hasOrderDomain() { |
| 103 | Assert.assertTrue(o1.hasOrderedDomain()); |
| 104 | Assert.assertFalse(u1.hasOrderedDomain()); |
| 105 | } |
| 106 | |
| 107 | @Test(expected = UnorderedDomainException.class) |
| 108 | public void unorderedGetMedian() throws UnorderedDomainException { |
| 109 | u1.getMedian(); |
| 110 | } |
| 111 | |
| 112 | @Test(expected = UnorderedDomainException.class) |
| 113 | public void unorderedPercentile() throws UnorderedDomainException { |
| 114 | u1.getPercentile(10); |
| 115 | } |
| 116 | |
| 117 | @Test |
| 118 | public void percentile() throws IndexOutOfBoundsException, |
| 119 | UnorderedDomainException { |
| 120 | Assert.assertEquals(0.3, o1.getPercentile(50)); // ?? |
| 121 | } |
| 122 | |
| 123 | @Test |
| 124 | public void getMedian() throws UnorderedDomainException { |
| 125 | Assert.assertEquals(0.3, o1.getMedian()); |
| 126 | } |
| 127 | |
| 128 | @Test |
| 129 | public void addSameDom() throws DifferentDomainsException { |
| 130 | IProbabilityMassFunction sum = o1.add(o1same); |
| 131 | IProbabilityMassFunction expected = createPMF(new Object[]{0.1, 0.3, |
| 132 | 0.2, 0.7, 0.3, 0.6, 0.4, 0.4}, true); |
| 133 | Assert.assertEquals(expected, sum); |
| 134 | |
| 135 | sum = u1.add(u1same); |
| 136 | expected = createPMF(new Object[]{"car", 0.3, "house", 0.8, "bike", |
| 137 | 0.7, "street", 0.2}, true); |
| 138 | Assert.assertEquals(expected, sum); |
| 139 | |
| 140 | IProbabilityMassFunction unsorted = createPMF(new Object[]{"car", 0.2, |
| 141 | "street", 0.1, "house", 0.3, "bike", 0.4}, false); |
| 142 | sum = u1.add(unsorted); |
| 143 | expected = createPMF(new Object[]{"house", 0.8, "bike", 0.7, "car", |
| 144 | 0.3, "street", 0.2}, true); |
| 145 | Assert.assertEquals(expected, sum); |
| 146 | } |
| 147 | |
| 148 | @Test(expected = DifferentDomainsException.class) |
| 149 | public void addExtendedDomOrdered() throws DifferentDomainsException { |
| 150 | o1.add(o1extended); |
| 151 | } |
| 152 | |
| 153 | @Test(expected = DifferentDomainsException.class) |
| 154 | public void addExtendedDomUnOrdered() throws DifferentDomainsException { |
| 155 | u1.add(u1extended); |
| 156 | } |
| 157 | |
| 158 | @Test(expected = DifferentDomainsException.class) |
| 159 | public void addOrderedUnOrdered() throws DifferentDomainsException { |
| 160 | o1.add(u1); |
| 161 | } |
| 162 | |
| 163 | @Test |
| 164 | public void multSameDom() throws DifferentDomainsException { |
| 165 | IProbabilityMassFunction sum = o1.mult(o1same); |
| 166 | IProbabilityMassFunction expected = createPMF(new Object[]{0.1, 0.02, |
| 167 | 0.2, 0.12, 0.3, 0.08, 0.4, 0.03}, true); |
| 168 | Assert.assertEquals(expected, sum); |
| 169 | |
| 170 | sum = u1.mult(u1same); |
| 171 | expected = createPMF(new Object[]{"car", 0.1 * 0.2, "house", 0.5 * 0.3, |
| 172 | "bike", 0.3 * 0.4, "street", 0.1 * 0.1}, false); |
| 173 | Assert.assertEquals(expected, sum); |
| 174 | } |
| 175 | |
| 176 | @Test |
| 177 | public void scale() { |
| 178 | IProbabilityMassFunction result = u1.scale(0.1); |
| 179 | IProbabilityMassFunction expected = createPMF(new Object[]{"car", 0.01, |
| 180 | "house", 0.05, "bike", 0.03, "street", 0.01}, false); |
| 181 | Assert.assertEquals(expected, result); |
| 182 | } |
| 183 | |
| 184 | @Test(expected = DifferentDomainsException.class) |
| 185 | public void multExtendedDomOrdered() throws DifferentDomainsException { |
| 186 | o1.add(o1extended); |
| 187 | } |
| 188 | |
| 189 | @Test(expected = DifferentDomainsException.class) |
| 190 | public void multExtendedDomUnOrdered() throws DifferentDomainsException { |
| 191 | u1.add(u1extended); |
| 192 | } |
| 193 | |
| 194 | @Test(expected = DifferentDomainsException.class) |
| 195 | public void multOrderedUnOrdered() throws DifferentDomainsException { |
| 196 | o1.add(u1); |
| 197 | } |
| 198 | |
| 199 | @Test(expected = ProbabilitySumNotOneException.class) |
| 200 | public void checkConstrains1() throws NegativeDistanceException, |
| 201 | ProbabilitySumNotOneException, FunctionNotInTimeDomainException, |
| 202 | UnitNotSetException, UnitNameNotSetException, |
| 203 | InvalidSampleValueException { |
| 204 | IProbabilityMassFunction pmf = createPMF(new Object[]{0.1, 0.3, 0.2, |
| 205 | 0.3, 0.3, 0.4, 0.4, 0.1}, true); |
| 206 | pmf.checkConstrains(); |
| 207 | } |
| 208 | |
| 209 | @Test(expected = InvalidSampleValueException.class) |
| 210 | public void checkConstrains3() throws NegativeDistanceException, |
| 211 | ProbabilitySumNotOneException, FunctionNotInTimeDomainException, |
| 212 | UnitNotSetException, UnitNameNotSetException, |
| 213 | InvalidSampleValueException { |
| 214 | IProbabilityMassFunction pmf = createPMF(new Object[]{0.1, 0.3, 0.2, |
| 215 | 0.4, 0.4, 0.4, 0.4, -0.1}, true); |
| 216 | pmf.checkConstrains(); |
| 217 | } |
| 218 | |
| 219 | // Commented this because UnitNotSetException is obsolete |
| 220 | // |
| 221 | // @Test(expected = UnitNotSetException.class) |
| 222 | // public void checkConstrains2() throws NegativeDistanceException, |
| 223 | // ProbabilitySumNotOneException, FunctionNotInTimeDomainException, |
| 224 | // UnitNotSetException, UnitNameNotSetException, |
| 225 | // InvalidSampleValueException { |
| 226 | // List<ISample> samples = new ArrayList<ISample>(); |
| 227 | // Collections.addAll(samples, dfFactory.createSample(0.1, 0.3), dfFactory |
| 228 | // .createSample(0.2, 0.2), dfFactory.createSample(0.3, 0.4), |
| 229 | // dfFactory.createSample(0.4, 0.1)); |
| 230 | // IProbabilityMassFunction pmf = dfFactory.createProbabilityMassFunction( |
| 231 | // samples, null, true); |
| 232 | // pmf.checkConstrains(); |
| 233 | // } |
| 234 | |
| 235 | @Test |
| 236 | public void checkConstrains4() throws NegativeDistanceException, |
| 237 | ProbabilitySumNotOneException, FunctionNotInTimeDomainException, |
| 238 | UnitNotSetException, UnitNameNotSetException, |
| 239 | InvalidSampleValueException { |
| 240 | u1.checkConstrains(); |
| 241 | o1.checkConstrains(); |
| 242 | u1extended.checkConstrains(); |
| 243 | } |
| 244 | |
| 245 | private IProbabilityFunctionFactory dfFactory = IProbabilityFunctionFactory.eINSTANCE; |
| 246 | |
| 247 | private IProbabilityMassFunction createPMF(Object[] samples, |
| 248 | boolean isOrderedSet) { |
| 249 | List<ISample> sList = new ArrayList<ISample>(); |
| 250 | for (int i = 0; i < samples.length; i += 2) { |
| 251 | ISample s = dfFactory.createSample(samples[i], |
| 252 | (Double) samples[i + 1]); |
| 253 | sList.add(s); |
| 254 | } |
| 255 | IUnit ms = dfFactory.createUnit("ms"); |
| 256 | IProbabilityMassFunction pmf = dfFactory.createProbabilityMassFunction( |
| 257 | sList, ms, isOrderedSet); |
| 258 | return pmf; |
| 259 | } |
| 260 | } |