View Javadoc

1   // Copyright (C) 2000 - 2012 Philip Aston
2   // All rights reserved.
3   //
4   // This file is part of The Grinder software distribution. Refer to
5   // the file LICENSE which is part of The Grinder distribution for
6   // licensing details. The Grinder distribution is available on the
7   // Internet at http://grinder.sourceforge.net/
8   //
9   // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
10  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
11  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
12  // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
13  // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
14  // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
15  // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
16  // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
17  // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
18  // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
19  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
20  // OF THE POSSIBILITY OF SUCH DAMAGE.
21  
22  package net.grinder.statistics;
23  
24  import static java.util.Arrays.asList;
25  
26  import java.util.HashSet;
27  import java.util.Set;
28  
29  import junit.framework.TestCase;
30  import net.grinder.statistics.StatisticsIndexMap.LongSampleIndex;
31  
32  /**
33   * Unit test case for {@link StatisticsIndexMap}.
34   *
35   * @author Philip Aston
36   * @see StatisticsSet
37   */
38  public class TestStatisticsIndexMap extends TestCase {
39  
40    private final StatisticsIndexMap m_indexMap =
41      StatisticsServicesImplementation.getInstance().getStatisticsIndexMap();
42  
43    public void testLongs() throws Exception {
44      final String[] data = {
45          "userLong0", "userLong1", "userLong2", "userLong3", };
46  
47      final StatisticsIndexMap.LongIndex[] longResults =
48        new StatisticsIndexMap.LongIndex[data.length];
49  
50      for (int i = 0; i < data.length; i++) {
51        longResults[i] = m_indexMap.getLongIndex(data[i]);
52  
53        assertNotNull(m_indexMap.getLongIndex(data[i]));
54        assertNull(m_indexMap.getDoubleIndex(data[i]));
55  
56        for (int j = 0; j < i; ++j) {
57          assertTrue(longResults[i].getValue() != longResults[j].getValue());
58        }
59      }
60  
61      for (int i = 0; i < data.length; i++) {
62        assertEquals(longResults[i].getValue(), m_indexMap.getLongIndex(
63            data[i]).getValue());
64      }
65    }
66  
67    public void testDoubles() throws Exception {
68      final String[] data = {
69          "userDouble0", "userDouble1", "userDouble2", "userDouble3", };
70  
71      final StatisticsIndexMap.DoubleIndex[] doubleResults =
72        new StatisticsIndexMap.DoubleIndex[data.length];
73  
74      for (int i = 0; i < data.length; i++) {
75        doubleResults[i] = m_indexMap.getDoubleIndex(data[i]);
76  
77        assertNotNull(m_indexMap.getDoubleIndex(data[i]));
78        assertNull(m_indexMap.getLongIndex(data[i]));
79  
80        for (int j = 0; j < i; ++j) {
81          assertTrue(doubleResults[i].getValue() != doubleResults[j].getValue());
82        }
83      }
84  
85      for (int i = 0; i < data.length; i++) {
86        assertEquals(doubleResults[i].getValue(), m_indexMap.getDoubleIndex(
87            data[i]).getValue());
88      }
89    }
90  
91    private static class ExpectedIndices  {
92      private final Set<Integer> expected;
93  
94      public ExpectedIndices(Integer... indices) {
95        expected = new HashSet<Integer>(asList(indices));
96      }
97  
98      public void remove(int index) {
99        assertTrue(expected + " contains " + index, expected.remove(index));
100     }
101 
102     public void assertEmpty() {
103       assertTrue(expected + " is empty", expected.size() == 0);
104     }
105   }
106 
107   public void testSlotsAreUnique() throws Exception {
108     final StatisticsIndexMap map =
109         new StatisticsIndexMap(asList("l1", "l2"),
110                                asList("d1", "d2"),
111                                asList("t1", "t2"),
112                                asList("ls1", "ls2"));
113 
114     assertEquals(6, map.getNumberOfLongs());
115     assertEquals(4, map.getNumberOfDoubles());
116     assertEquals(2, map.getNumberOfTransientLongs());
117     assertEquals(0, map.getDoubleSampleIndicies().size());
118 
119     final ExpectedIndices expectedLongs =
120         new ExpectedIndices(0, 1, 2, 3, 4, 5);
121     final ExpectedIndices expectedDoubles = new ExpectedIndices(0, 1, 2, 3);
122     final ExpectedIndices expectedTransientLongs = new ExpectedIndices(0, 1);
123 
124 
125     expectedLongs.remove(map.getLongIndex("l1").getValue());
126     expectedLongs.remove(map.getLongIndex("l2").getValue());
127 
128     final LongSampleIndex ls1 = map.getLongSampleIndex("ls1");
129     expectedLongs.remove(ls1.getCountIndex().getValue());
130     expectedLongs.remove(ls1.getSumIndex().getValue());
131     expectedDoubles.remove(ls1.getVarianceIndex().getValue());
132 
133     final LongSampleIndex ls2 = map.getLongSampleIndex("ls2");
134     expectedLongs.remove(ls2.getCountIndex().getValue());
135     expectedLongs.remove(ls2.getSumIndex().getValue());
136     expectedDoubles.remove(ls2.getVarianceIndex().getValue());
137 
138     expectedTransientLongs.remove(map.getLongIndex("t1").getValue());
139     expectedTransientLongs.remove(map.getLongIndex("t2").getValue());
140     expectedDoubles.remove(map.getDoubleIndex("d1").getValue());
141     expectedDoubles.remove(map.getDoubleIndex("d2").getValue());
142 
143     expectedLongs.assertEmpty();
144     expectedDoubles.assertEmpty();
145     expectedTransientLongs.assertEmpty();
146   }
147 }