| 1 | /** |
| 2 | * |
| 3 | */ |
| 4 | package de.uka.ipd.sdq.probfunction.math.test; |
| 5 | |
| 6 | import java.util.ArrayList; |
| 7 | import java.util.Arrays; |
| 8 | import java.util.Collections; |
| 9 | import java.util.List; |
| 10 | |
| 11 | import junit.framework.Assert; |
| 12 | import junit.framework.JUnit4TestAdapter; |
| 13 | |
| 14 | import org.junit.Before; |
| 15 | import org.junit.Test; |
| 16 | |
| 17 | import de.uka.ipd.sdq.probfunction.math.IProbabilityDensityFunction; |
| 18 | import de.uka.ipd.sdq.probfunction.math.IProbabilityFunctionFactory; |
| 19 | import de.uka.ipd.sdq.probfunction.math.ISamplePDF; |
| 20 | import de.uka.ipd.sdq.probfunction.math.exception.FunctionNotInTimeDomainException; |
| 21 | import de.uka.ipd.sdq.probfunction.math.exception.FunctionsInDifferenDomainsException; |
| 22 | import de.uka.ipd.sdq.probfunction.math.exception.IncompatibleUnitsException; |
| 23 | import de.uka.ipd.sdq.probfunction.math.exception.InvalidSampleValueException; |
| 24 | import de.uka.ipd.sdq.probfunction.math.exception.NegativeDistanceException; |
| 25 | import de.uka.ipd.sdq.probfunction.math.exception.ProbabilitySumNotOneException; |
| 26 | import de.uka.ipd.sdq.probfunction.math.exception.UnitNameNotSetException; |
| 27 | import de.uka.ipd.sdq.probfunction.math.exception.UnitNotSetException; |
| 28 | import de.uka.ipd.sdq.probfunction.math.exception.UnknownPDFTypeException; |
| 29 | |
| 30 | /** |
| 31 | * @author Ihssane |
| 32 | * |
| 33 | */ |
| 34 | public class SamplePDFTest { |
| 35 | |
| 36 | private ISamplePDF df1, df2, df3; |
| 37 | |
| 38 | private IProbabilityFunctionFactory dfFactory = IProbabilityFunctionFactory.eINSTANCE; |
| 39 | |
| 40 | private ISamplePDF createSamplePDF(double distance, Double[] values) { |
| 41 | return dfFactory.createSamplePDFFromDouble(distance, Arrays |
| 42 | .asList(values), dfFactory.createDefaultUnit()); |
| 43 | } |
| 44 | |
| 45 | @Before |
| 46 | public void setUp() { |
| 47 | df1 = createSamplePDF(10, new Double[]{0.1, 0.2, 0.4, 0.3}); |
| 48 | df2 = createSamplePDF(10, new Double[]{0.0, 0.2, 0.1, 0.2, 0.3, 0.2}); |
| 49 | df3 = createSamplePDF(10, new Double[]{0.0, 0.2, 0.05, 0.15, 0.3, 0.2, |
| 50 | 0.07, 0.03, 0.0}); |
| 51 | } |
| 52 | |
| 53 | @Test |
| 54 | public void timeDomain() throws FunctionNotInTimeDomainException { |
| 55 | Assert.assertTrue(df1.isInTimeDomain()); |
| 56 | Assert.assertTrue(df2.isInTimeDomain()); |
| 57 | Assert.assertFalse(df1.isInFrequencyDomain()); |
| 58 | IProbabilityDensityFunction pdf = df1.getFourierTransform(); |
| 59 | Assert.assertFalse(pdf.isInTimeDomain()); |
| 60 | Assert.assertTrue(pdf.isInFrequencyDomain()); |
| 61 | } |
| 62 | |
| 63 | @Test |
| 64 | public void equals() { |
| 65 | Assert.assertTrue(df1.equals(df1)); |
| 66 | |
| 67 | ISamplePDF df1copy = createSamplePDF(10, new Double[]{0.1, 0.2, 0.4, |
| 68 | 0.3}); |
| 69 | Assert.assertTrue(df1.equals(df1copy)); |
| 70 | |
| 71 | ISamplePDF df1LongCopy = createSamplePDF(10, new Double[]{0.1, 0.2, |
| 72 | 0.4, 0.3, 0.0}); |
| 73 | Assert.assertTrue(df1.equals(df1LongCopy)); |
| 74 | |
| 75 | ISamplePDF df1LongWrongCopy = createSamplePDF(10, new Double[]{0.1, |
| 76 | 0.2, 0.4, 0.3, 0.0, 0.01}); |
| 77 | Assert.assertFalse(df1.equals(df1LongWrongCopy)); |
| 78 | |
| 79 | Assert.assertFalse(df2.equals(df1)); |
| 80 | ISamplePDF df1WrongCopy = createSamplePDF(10, new Double[]{0.2, 0.1, |
| 81 | 0.4, 0.3}); |
| 82 | Assert.assertFalse(df1.equals(df1WrongCopy)); |
| 83 | } |
| 84 | |
| 85 | @Test |
| 86 | public void scale() { |
| 87 | IProbabilityDensityFunction df1scale = df1.scale(0.1); |
| 88 | ISamplePDF spdf = createSamplePDF(10, new Double[]{0.01, 0.02, 0.04, |
| 89 | 0.03}); |
| 90 | Assert.assertEquals(spdf, df1scale); |
| 91 | } |
| 92 | |
| 93 | @Test |
| 94 | public void adjustDistance() throws NegativeDistanceException, |
| 95 | FunctionNotInTimeDomainException { |
| 96 | ISamplePDF pdf = df1.getFunctionWithNewDistance(10.0); |
| 97 | Assert.assertEquals(df1, pdf); |
| 98 | |
| 99 | pdf = df1.getFunctionWithNewDistance(5.0); |
| 100 | ISamplePDF expected = createSamplePDF(5.0, new Double[]{0.05, 0.1, 0.1, |
| 101 | 0.15, 0.2, 0.175, 0.15, 0.075}); |
| 102 | Assert.assertEquals(expected, pdf); |
| 103 | |
| 104 | pdf = df1.getFunctionWithNewDistance(7.0); |
| 105 | expected = createSamplePDF(7, new Double[]{0.07, 0.14, 0.19, 0.28, |
| 106 | 0.215, 0.105}); |
| 107 | Assert.assertEquals(expected, pdf); |
| 108 | |
| 109 | pdf = df1.getFunctionWithNewDistance(15.0); |
| 110 | expected = createSamplePDF(15, new Double[]{0.15, 0.45, 0.4}); |
| 111 | Assert.assertEquals(expected, pdf); |
| 112 | |
| 113 | pdf = df1.getFunctionWithNewDistance(13.0); |
| 114 | expected = createSamplePDF(13, new Double[]{0.13, 0.35, 0.445, 0.075}); |
| 115 | Assert.assertEquals(expected, pdf); |
| 116 | } |
| 117 | |
| 118 | @Test |
| 119 | public void add() throws FunctionsInDifferenDomainsException, |
| 120 | UnknownPDFTypeException, IncompatibleUnitsException { |
| 121 | IProbabilityDensityFunction sum = df1.add(df1); |
| 122 | ISamplePDF sumExpected = createSamplePDF(10, new Double[]{0.2, 0.4, |
| 123 | 0.8, 0.6}); |
| 124 | Assert.assertEquals(sumExpected, sum); |
| 125 | |
| 126 | sum = df1.add(df3); |
| 127 | sumExpected = createSamplePDF(10, new Double[]{0.1, 0.4, 0.45, 0.45, |
| 128 | 0.3, 0.2, 0.07, 0.03, 0.0}); |
| 129 | Assert.assertEquals(sumExpected, sum); |
| 130 | |
| 131 | ISamplePDF pdf = createSamplePDF(5, new Double[]{0.05, 0.05, 0.1, 0.1, |
| 132 | 0.2, 0.2, 0.15, 0.15}); |
| 133 | sum = df1.add(pdf); |
| 134 | sumExpected = createSamplePDF(5, new Double[]{0.1, 0.15, 0.2, 0.25, |
| 135 | 0.4, 0.375, 0.3, 0.225}); |
| 136 | Assert.assertEquals(sumExpected, sum); |
| 137 | } |
| 138 | |
| 139 | @Test |
| 140 | public void mult() throws FunctionsInDifferenDomainsException, |
| 141 | UnknownPDFTypeException, IncompatibleUnitsException { |
| 142 | IProbabilityDensityFunction prod = df1.mult(df1); |
| 143 | ISamplePDF expected = createSamplePDF(10, new Double[]{0.01, 0.04, |
| 144 | 0.16, 0.09}); |
| 145 | Assert.assertEquals(expected, prod); |
| 146 | } |
| 147 | |
| 148 | @Test |
| 149 | public void checkConstraints() throws NegativeDistanceException, |
| 150 | ProbabilitySumNotOneException, FunctionNotInTimeDomainException, |
| 151 | UnitNotSetException, UnitNameNotSetException, |
| 152 | InvalidSampleValueException { |
| 153 | df1.checkConstrains(); |
| 154 | df2.checkConstrains(); |
| 155 | df3.checkConstrains(); |
| 156 | } |
| 157 | |
| 158 | @Test(expected = NegativeDistanceException.class) |
| 159 | public void checkConstraints1() throws NegativeDistanceException, |
| 160 | ProbabilitySumNotOneException, FunctionNotInTimeDomainException, |
| 161 | UnitNotSetException, UnitNameNotSetException, |
| 162 | InvalidSampleValueException { |
| 163 | ISamplePDF s = createSamplePDF(-1.0, new Double[]{0.1, 0.4, 0.2, 0.3, |
| 164 | 0.2}); |
| 165 | s.checkConstrains(); |
| 166 | } |
| 167 | |
| 168 | @Test(expected = ProbabilitySumNotOneException.class) |
| 169 | public void checkConstraints2() throws NegativeDistanceException, |
| 170 | ProbabilitySumNotOneException, FunctionNotInTimeDomainException, |
| 171 | UnitNotSetException, UnitNameNotSetException, |
| 172 | InvalidSampleValueException { |
| 173 | ISamplePDF s = createSamplePDF(1.0, new Double[]{0.1, 0.4, 0.2, 0.3, |
| 174 | 0.2}); |
| 175 | s.checkConstrains(); |
| 176 | } |
| 177 | |
| 178 | @Test(expected = InvalidSampleValueException.class) |
| 179 | public void checkConstraints3() throws NegativeDistanceException, |
| 180 | ProbabilitySumNotOneException, FunctionNotInTimeDomainException, |
| 181 | UnitNotSetException, UnitNameNotSetException, |
| 182 | InvalidSampleValueException { |
| 183 | ISamplePDF s = createSamplePDF(1.0, new Double[]{0.1, -0.4, 0.2, 1.1}); |
| 184 | s.checkConstrains(); |
| 185 | } |
| 186 | |
| 187 | @Test(expected = UnitNotSetException.class) |
| 188 | public void checkConstraints4() throws NegativeDistanceException, |
| 189 | ProbabilitySumNotOneException, FunctionNotInTimeDomainException, |
| 190 | UnitNotSetException, UnitNameNotSetException, |
| 191 | InvalidSampleValueException { |
| 192 | List<Double> samples = new ArrayList<Double>(); |
| 193 | Collections.addAll(samples, 0.1, 0.4, 0.2, 0.1); |
| 194 | ISamplePDF s = dfFactory.createSamplePDFFromDouble(1.0, samples, null); |
| 195 | s.checkConstrains(); |
| 196 | } |
| 197 | |
| 198 | // TODO: what is this supposed to test? |
| 199 | // @Test |
| 200 | // public void getMedian() throws UnorderedDomainException { |
| 201 | // System.out.println("Df1 "+df1); |
| 202 | // Assert.assertEquals(15.0, df1.getMedian()); |
| 203 | // Assert.assertEquals(25.0, df2.getMedian()); |
| 204 | // Assert.assertEquals(40.0, df3.getMedian()); |
| 205 | // |
| 206 | // } |
| 207 | |
| 208 | public static junit.framework.Test suite() { |
| 209 | return new JUnit4TestAdapter(SamplePDFTest.class); |
| 210 | } |
| 211 | } |