1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
34
35
36
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 }