1 | /** |
2 | * |
3 | */ |
4 | package de.uka.ipd.sdq.probfunction.math.test; |
5 | |
6 | import java.util.ArrayList; |
7 | import java.util.List; |
8 | |
9 | import junit.framework.Assert; |
10 | import junit.framework.JUnit4TestAdapter; |
11 | |
12 | import org.junit.Before; |
13 | import org.junit.Ignore; |
14 | import org.junit.Test; |
15 | |
16 | import de.uka.ipd.sdq.probfunction.math.IBoxedPDF; |
17 | import de.uka.ipd.sdq.probfunction.math.IContinuousSample; |
18 | import de.uka.ipd.sdq.probfunction.math.IProbabilityDensityFunction; |
19 | import de.uka.ipd.sdq.probfunction.math.IProbabilityFunctionFactory; |
20 | import de.uka.ipd.sdq.probfunction.math.ISamplePDF; |
21 | import de.uka.ipd.sdq.probfunction.math.exception.DomainNotNumbersException; |
22 | import de.uka.ipd.sdq.probfunction.math.exception.DoubleSampleException; |
23 | import de.uka.ipd.sdq.probfunction.math.exception.FunctionNotInTimeDomainException; |
24 | import de.uka.ipd.sdq.probfunction.math.exception.FunctionsInDifferenDomainsException; |
25 | import de.uka.ipd.sdq.probfunction.math.exception.IncompatibleUnitsException; |
26 | import de.uka.ipd.sdq.probfunction.math.exception.InvalidSampleValueException; |
27 | import de.uka.ipd.sdq.probfunction.math.exception.NegativeDistanceException; |
28 | import de.uka.ipd.sdq.probfunction.math.exception.ProbabilitySumNotOneException; |
29 | import de.uka.ipd.sdq.probfunction.math.exception.UnitNameNotSetException; |
30 | import de.uka.ipd.sdq.probfunction.math.exception.UnitNotSetException; |
31 | import de.uka.ipd.sdq.probfunction.math.exception.UnknownPDFTypeException; |
32 | import de.uka.ipd.sdq.probfunction.math.exception.UnorderedDomainException; |
33 | |
34 | /** |
35 | * @author Ihssane |
36 | * |
37 | */ |
38 | public class BoxedPDFTest{ |
39 | private IBoxedPDF df1, df2; |
40 | private IProbabilityFunctionFactory dfFactory = IProbabilityFunctionFactory.eINSTANCE; |
41 | |
42 | private IBoxedPDF createBoxedPDF(Double[] samples) |
43 | throws DoubleSampleException { |
44 | List<IContinuousSample> sList = new ArrayList<IContinuousSample>(); |
45 | for (int i = 0; i < samples.length; i += 2) { |
46 | IContinuousSample s = dfFactory.createContinuousSample(samples[i], |
47 | samples[i + 1]); |
48 | sList.add(s); |
49 | } |
50 | return dfFactory.createBoxedPDF(sList, dfFactory.createDefaultUnit()); |
51 | } |
52 | |
53 | @Before |
54 | public void setUp() throws DoubleSampleException { |
55 | df1 = createBoxedPDF(new Double[]{3.0, 0.1, 2.1, 0.2, 4.3, 0.4, 1.5, 0.3}); |
56 | df2 = createBoxedPDF(new Double[]{3.0, 0.0, 2.1, 0.2, 2.2, 0.1, 2.5, |
57 | 0.2, 4.3, 0.3}); |
58 | |
59 | } |
60 | |
61 | @Test |
62 | public void arithmeticMean() throws DomainNotNumbersException, FunctionNotInTimeDomainException{ |
63 | |
64 | double meanDf1 = 0.75 * 0.3 + 1.8 * 0.2 + 2.55 * 0.1 + 3.65 * 0.4; |
65 | System.out.println(df1); |
66 | |
67 | Assert.assertEquals(meanDf1, df1.getArithmeticMeanValue()); |
68 | //Assert.assertEquals(2.43, df1.getArithmeticMeanValue()); |
69 | } |
70 | |
71 | @Test |
72 | public void timeDomain() throws FunctionNotInTimeDomainException { |
73 | Assert.assertTrue(df1.isInTimeDomain()); |
74 | Assert.assertTrue(df2.isInTimeDomain()); |
75 | Assert.assertFalse(df1.isInFrequencyDomain()); |
76 | IProbabilityDensityFunction pdf = df1.getFourierTransform(); |
77 | Assert.assertFalse(pdf.isInTimeDomain()); |
78 | Assert.assertTrue(pdf.isInFrequencyDomain()); |
79 | } |
80 | |
81 | @Test |
82 | public void equals() throws DoubleSampleException { |
83 | Assert.assertTrue(df1.equals(df1)); |
84 | |
85 | IBoxedPDF df1copy = createBoxedPDF(new Double[]{3.0, 0.1, 2.1, 0.2, |
86 | 4.3, 0.4, 1.5, 0.3}); |
87 | Assert.assertTrue(df1.equals(df1copy)); |
88 | |
89 | Assert.assertFalse(df2.equals(df1)); |
90 | IBoxedPDF df1WrongCopy = createBoxedPDF(new Double[]{2.1, 0.2, 3.0, |
91 | 0.1, 4.3, 0.4, 1.5, 0.3}); |
92 | Assert.assertTrue(df1.equals(df1WrongCopy)); |
93 | } |
94 | |
95 | @Test |
96 | public void scale() throws DoubleSampleException { |
97 | IProbabilityDensityFunction df1scale = df1.scale(0.1); |
98 | IBoxedPDF spdf = createBoxedPDF(new Double[]{3.0, 0.01, 2.1, 0.02, 4.3, |
99 | 0.04, 1.5, 0.03}); |
100 | Assert.assertEquals(spdf, df1scale); |
101 | } |
102 | |
103 | @Test |
104 | public void add() throws DoubleSampleException, |
105 | FunctionsInDifferenDomainsException, UnknownPDFTypeException, |
106 | IncompatibleUnitsException { |
107 | IProbabilityDensityFunction sum = df1.add(df1); |
108 | IBoxedPDF sumExpected = createBoxedPDF(new Double[]{3.0, 0.2, 2.1, 0.4, |
109 | 4.3, 0.8, 1.5, 0.6}); |
110 | Assert.assertEquals(dfFactory.transformToSamplePDF(sumExpected), |
111 | (ISamplePDF) sum); |
112 | |
113 | } |
114 | |
115 | @Test |
116 | @Ignore |
117 | public void mult() throws FunctionsInDifferenDomainsException, |
118 | UnknownPDFTypeException, IncompatibleUnitsException, |
119 | DoubleSampleException { |
120 | // IProbabilityDensityFunction prod = df1.mult(df1); |
121 | ISamplePDF df11 = dfFactory.transformToSamplePDF(df1); |
122 | IProbabilityDensityFunction prod = df11.mult(df11); |
123 | System.out.println(prod); |
124 | IBoxedPDF expected = createBoxedPDF(new Double[]{3.0, 0.01, 2.1, 0.04, |
125 | 4.3, 0.16, 1.5, 0.09}); |
126 | ISamplePDF sExpexted = dfFactory.transformToSamplePDF(expected); |
127 | System.out.println(sExpexted); |
128 | Assert.assertEquals(sExpexted, (ISamplePDF) prod); |
129 | } |
130 | |
131 | @Test |
132 | public void percentile() throws IndexOutOfBoundsException, |
133 | UnorderedDomainException { |
134 | Assert.assertEquals(0.3, df1.getPercentile(10)); |
135 | Assert.assertEquals(0.2, df1.getPercentile(40)); |
136 | Assert.assertEquals(0.1, df1.getPercentile(50)); |
137 | Assert.assertEquals(0.4, df1.getPercentile(99)); |
138 | } |
139 | |
140 | @Test |
141 | public void getMedian() throws UnorderedDomainException, |
142 | DoubleSampleException { |
143 | Assert.assertEquals(2.55, df1.getMedian()); |
144 | IBoxedPDF df = createBoxedPDF(new Double[]{3.0, 0.1, 2.1, 0.2, 4.3, |
145 | 0.4, 1.5, 0.1, 1.4, 0.2}); |
146 | Assert.assertEquals(2.1, df.getMedian()); |
147 | } |
148 | |
149 | @Test |
150 | public void checkConstraints() throws NegativeDistanceException, |
151 | ProbabilitySumNotOneException, FunctionNotInTimeDomainException, |
152 | UnitNotSetException, UnitNameNotSetException, |
153 | InvalidSampleValueException { |
154 | df1.checkConstrains(); |
155 | } |
156 | |
157 | @Test(expected = InvalidSampleValueException.class) |
158 | public void checkConstraints1() throws DoubleSampleException, |
159 | NegativeDistanceException, ProbabilitySumNotOneException, |
160 | FunctionNotInTimeDomainException, UnitNotSetException, |
161 | UnitNameNotSetException, InvalidSampleValueException { |
162 | IBoxedPDF b = createBoxedPDF(new Double[]{3.0, 0.1, 2.1, 0.4, 4.3, 0.3, |
163 | -1.5, 0.2}); |
164 | b.checkConstrains(); |
165 | } |
166 | |
167 | @Test(expected = ProbabilitySumNotOneException.class) |
168 | public void checkConstraints2() throws DoubleSampleException, |
169 | NegativeDistanceException, ProbabilitySumNotOneException, |
170 | FunctionNotInTimeDomainException, UnitNotSetException, |
171 | UnitNameNotSetException, InvalidSampleValueException { |
172 | IBoxedPDF b = createBoxedPDF(new Double[]{3.0, 0.1, 2.1, 0.4, 4.3, 0.3, |
173 | 1.5, 0.3}); |
174 | b.checkConstrains(); |
175 | } |
176 | |
177 | //TODO uncomment after refactoring in BoxedPDFImpl.checkConstrains() |
178 | // @Test(expected = UnitNotSetException.class) |
179 | // public void checkConstraints3() throws DoubleSampleException, |
180 | // NegativeDistanceException, ProbabilitySumNotOneException, |
181 | // FunctionNotInTimeDomainException, UnitNotSetException, |
182 | // UnitNameNotSetException, InvalidSampleValueException { |
183 | // List<IContinuousSample> samples = new ArrayList<IContinuousSample>(); |
184 | // Collections.addAll(samples, dfFactory.createContinuousSample(3.0, 0.1), |
185 | // dfFactory.createContinuousSample(2.1, 0.4), dfFactory |
186 | // .createContinuousSample(4.3, 0.5)); |
187 | // |
188 | // IBoxedPDF b = dfFactory.createBoxedPDF(samples, null); |
189 | // b.checkConstrains(); |
190 | // } |
191 | |
192 | public static junit.framework.Test suite() { |
193 | return new JUnit4TestAdapter(BoxedPDFTest.class); |
194 | } |
195 | } |