View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  package org.hipparchus.stat;
23  
24  import static org.junit.Assert.assertEquals;
25  import static org.junit.Assert.assertFalse;
26  import static org.junit.Assert.assertNotNull;
27  import static org.junit.Assert.assertNull;
28  import static org.junit.Assert.assertTrue;
29  
30  import java.io.BufferedReader;
31  import java.io.StringReader;
32  import java.util.ArrayList;
33  import java.util.Iterator;
34  import java.util.List;
35  
36  import org.hipparchus.UnitTestUtils;
37  import org.junit.Test;
38  
39  /**
40   * Test cases for the {@link Frequency} class.
41   */
42  public final class LongFrequencyTest {
43      private static final long ONE_LONG = 1L;
44      private static final long TWO_LONG = 2L;
45      private static final long THREE_LONG = 3L;
46      private static final int  ONE = 1;
47      private static final int  TWO = 2;
48      private static final int  THREE = 3 ;
49  
50      private static final double TOLERANCE = 10E-15d;
51  
52      /** test freq counts */
53      @Test
54      public void testCounts() {
55          LongFrequency f = new LongFrequency();
56  
57          assertEquals("total count", 0, f.getSumFreq());
58          f.addValue(ONE_LONG);
59          f.addValue(TWO_LONG);
60          f.addValue(1L);
61          f.addValue((long) ONE);
62          assertEquals("one frequency count", 3, f.getCount(1L));
63          assertEquals("two frequency count", 1, f.getCount(2L));
64          assertEquals("three frequency count", 0, f.getCount(3L));
65          assertEquals("total count", 4, f.getSumFreq());
66          assertEquals("zero cumulative frequency", 0, f.getCumFreq(0L));
67          assertEquals("one cumulative frequency", 3,  f.getCumFreq(1L));
68          assertEquals("two cumulative frequency", 4,  f.getCumFreq(2L));
69          assertEquals("five cumulative frequency", 4, f.getCumFreq(5L));
70  
71          f.clear();
72          assertEquals("total count", 0, f.getSumFreq());
73      }
74  
75      /** test pcts */
76      @Test
77      public void testPcts() {
78          LongFrequency f = new LongFrequency();
79  
80          f.addValue(ONE_LONG);
81          f.addValue(TWO_LONG);
82          f.addValue((long) ONE);
83          f.addValue((long) TWO);
84          f.addValue(THREE_LONG);
85          f.addValue(THREE_LONG);
86          f.addValue(3L);
87          f.addValue((long) THREE);
88          assertEquals("one pct", 0.25, f.getPct(1L), TOLERANCE);
89          assertEquals("two pct", 0.25, f.getPct(Long.valueOf(2)), TOLERANCE);
90          assertEquals("three pct", 0.5, f.getPct(THREE_LONG), TOLERANCE);
91          assertEquals("five pct", 0, f.getPct(5L), TOLERANCE);
92          assertEquals("one cum pct", 0.25, f.getCumPct(1L), TOLERANCE);
93          assertEquals("two cum pct", 0.50, f.getCumPct(Long.valueOf(2)), TOLERANCE);
94          assertEquals("three cum pct", 1.0, f.getCumPct(THREE_LONG), TOLERANCE);
95          assertEquals("five cum pct", 1.0, f.getCumPct(5L), TOLERANCE);
96          assertEquals("zero cum pct", 0.0, f.getCumPct(0L), TOLERANCE);
97      }
98  
99      /**
100      * Tests toString()
101      */
102     @Test
103     public void testToString() throws Exception {
104         LongFrequency f = new LongFrequency();
105 
106         f.addValue(ONE_LONG);
107         f.addValue(TWO_LONG);
108         f.addValue((long) ONE);
109         f.addValue((long) TWO);
110 
111         String s = f.toString();
112         //System.out.println(s);
113         assertNotNull(s);
114         BufferedReader reader = new BufferedReader(new StringReader(s));
115         String line = reader.readLine(); // header line
116         assertNotNull(line);
117 
118         line = reader.readLine(); // one's or two's line
119         assertNotNull(line);
120 
121         line = reader.readLine(); // one's or two's line
122         assertNotNull(line);
123 
124         line = reader.readLine(); // no more elements
125         assertNull(line);
126     }
127 
128     @Test
129     public void testLongValues() {
130         LongFrequency f = new LongFrequency();
131 
132         Integer obj1 = null;
133         obj1 = Integer.valueOf(1);
134         Integer int1 = Integer.valueOf(1);
135         f.addValue(obj1);
136         f.addValue(int1);
137         f.addValue(2);
138         f.addValue(Long.valueOf(2).intValue());
139         assertEquals("Integer 1 count", 2, f.getCount(1));
140         assertEquals("Integer 1 count", 2, f.getCount(Integer.valueOf(1)));
141         assertEquals("Integer 1 count", 2, f.getCount(Long.valueOf(1).intValue()));
142         assertEquals("Integer 1 cumPct", 0.5, f.getCumPct(1), TOLERANCE);
143         assertEquals("Integer 1 cumPct", 0.5, f.getCumPct(Long.valueOf(1).intValue()), TOLERANCE);
144         assertEquals("Integer 1 cumPct", 0.5, f.getCumPct(Integer.valueOf(1)), TOLERANCE);
145 
146         f.incrementValue(ONE, -2);
147         f.incrementValue(THREE, 5);
148 
149         assertEquals("Integer 1 count", 0, f.getCount(1));
150         assertEquals("Integer 3 count", 5, f.getCount(3));
151 
152         Iterator<?> it = f.valuesIterator();
153         while (it.hasNext()) {
154             assertTrue(it.next() instanceof Long);
155         }
156     }
157 
158     @Test
159     public void testSerial() {
160         LongFrequency f = new LongFrequency();
161 
162         f.addValue(ONE_LONG);
163         f.addValue(TWO_LONG);
164         f.addValue((long) ONE);
165         f.addValue((long) TWO);
166         assertEquals(f, UnitTestUtils.serializeAndRecover(f));
167     }
168 
169     @Test
170     public void testGetUniqueCount() {
171         LongFrequency f = new LongFrequency();
172 
173         assertEquals(0, f.getUniqueCount());
174         f.addValue(ONE_LONG);
175         assertEquals(1, f.getUniqueCount());
176         f.addValue(ONE_LONG);
177         assertEquals(1, f.getUniqueCount());
178         f.addValue((long) TWO);
179         assertEquals(2, f.getUniqueCount());
180     }
181 
182     @Test
183     public void testIncrement() {
184         LongFrequency f = new LongFrequency();
185 
186         assertEquals(0, f.getUniqueCount());
187         f.incrementValue(ONE_LONG, 1);
188         assertEquals(1, f.getCount(ONE_LONG));
189 
190         f.incrementValue(ONE_LONG, 4);
191         assertEquals(5, f.getCount(ONE_LONG));
192 
193         f.incrementValue(ONE_LONG, -5);
194         assertEquals(0, f.getCount(ONE_LONG));
195 
196     }
197 
198     @Test
199     public void testMerge() {
200         LongFrequency f = new LongFrequency();
201 
202         assertEquals(0, f.getUniqueCount());
203         f.addValue(ONE_LONG);
204         f.addValue(TWO_LONG);
205         f.addValue((long) ONE);
206         f.addValue((long) TWO);
207 
208         assertEquals(2, f.getUniqueCount());
209         assertEquals(2, f.getCount((long) ONE));
210         assertEquals(2, f.getCount((long) TWO));
211 
212         LongFrequency g = new LongFrequency();
213         g.addValue(ONE_LONG);
214         g.addValue(THREE_LONG);
215         g.addValue((long) THREE);
216 
217         assertEquals(2, g.getUniqueCount());
218         assertEquals(1, g.getCount((long) ONE));
219         assertEquals(2, g.getCount((long) THREE));
220 
221         f.merge(g);
222 
223         assertEquals(3, f.getUniqueCount());
224         assertEquals(3, f.getCount((long) ONE));
225         assertEquals(2, f.getCount((long) TWO));
226         assertEquals(2, f.getCount((long) THREE));
227     }
228 
229     @Test
230     public void testMergeCollection() {
231         LongFrequency f = new LongFrequency();
232 
233         assertEquals(0, f.getUniqueCount());
234         f.addValue(ONE_LONG);
235 
236         assertEquals(1, f.getUniqueCount());
237         assertEquals(1, f.getCount((long) ONE));
238         assertEquals(0, f.getCount((long) TWO));
239 
240         LongFrequency g = new LongFrequency();
241         g.addValue(TWO_LONG);
242 
243         LongFrequency h = new LongFrequency();
244         h.addValue(THREE_LONG);
245 
246         List<LongFrequency> coll = new ArrayList<>();
247         coll.add(g);
248         coll.add(h);
249         f.merge(coll);
250 
251         assertEquals(3, f.getUniqueCount());
252         assertEquals(1, f.getCount((long) ONE));
253         assertEquals(1, f.getCount((long) TWO));
254         assertEquals(1, f.getCount((long) THREE));
255     }
256 
257     @Test
258     public void testMode() {
259         LongFrequency f = new LongFrequency();
260 
261         List<Long> mode;
262         mode = f.getMode();
263         assertEquals(0, mode.size());
264 
265         f.addValue(3L);
266         mode = f.getMode();
267         assertEquals(1, mode.size());
268         assertEquals(Long.valueOf(3L), mode.get(0));
269 
270         f.addValue(2L);
271         mode = f.getMode();
272         assertEquals(2, mode.size());
273         assertEquals(Long.valueOf(2L), mode.get(0));
274         assertEquals(Long.valueOf(3L),mode.get(1));
275 
276         f.addValue(2L);
277         mode = f.getMode();
278         assertEquals(1, mode.size());
279         assertEquals(Long.valueOf(2L), mode.get(0));
280         assertFalse(mode.contains(1L));
281         assertTrue(mode.contains(2L));
282     }
283 
284 }