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 org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.fail;
28 import net.grinder.statistics.StatisticExpressionFactoryImplementation.ParseContext.ParseException;
29
30 import org.junit.Before;
31 import org.junit.Test;
32
33
34
35
36
37
38
39
40 public class TestStatisticExpressionFactory {
41
42 private StatisticExpressionFactory m_factory;
43 private StatisticsIndexMap m_indexMap;
44 private StatisticsSet m_statistics;
45
46 @Before public void setUp() throws Exception {
47 final StatisticsServices statisticsServices =
48 StatisticsServicesImplementation.getInstance();
49 m_factory = statisticsServices.getStatisticExpressionFactory();
50 m_indexMap = statisticsServices.getStatisticsIndexMap();
51 m_statistics = new StatisticsSetImplementation(m_indexMap);
52
53 m_statistics.addValue(m_indexMap.getLongIndex("userLong0"), 1);
54 m_statistics.addValue(m_indexMap.getLongIndex("userLong1"), 2);
55 }
56
57 @Test public void testConstant() throws Exception {
58 final StatisticExpression longExpression = m_factory.createConstant(-22);
59
60 myAssertEquals(-22, longExpression);
61 assertTrue(!longExpression.isDouble());
62
63 final StatisticExpression doubleExpression = m_factory.createConstant(2.3);
64
65 myAssertEquals(2.3d, doubleExpression);
66 assertTrue(doubleExpression.isDouble());
67
68 myAssertEquals(0, m_factory.createExpression("0"));
69 myAssertEquals(99d, m_factory.createExpression("99f"));
70
71 try {
72 m_factory.createExpression("1 2");
73 fail("Expected a ParseException");
74 }
75 catch (ParseException e) {
76 }
77 }
78
79 @Test public void testPrimitive() throws Exception {
80 final StatisticExpression expression =
81 m_factory.createPrimitive(m_indexMap.getLongIndex("userLong0"));
82
83 myAssertEquals(1, expression);
84 assertTrue(!expression.isDouble());
85
86 final StatisticsIndexMap.DoubleIndex anotherIndex = m_indexMap
87 .getDoubleIndex("userDouble4");
88 assertNotNull(anotherIndex);
89
90 final StatisticExpression doubleExpresson = m_factory
91 .createExpression(" userDouble4");
92
93 myAssertEquals(0d, doubleExpresson);
94 assertTrue(doubleExpresson.isDouble());
95
96 myAssertEquals(2, m_factory.createExpression("userLong1"));
97
98 try {
99 m_factory.createExpression("");
100 fail("Expected a ParseException");
101 }
102 catch (ParseException e) {
103 }
104
105 try {
106 m_factory.createExpression("userLong0 userLong1");
107 fail("Expected a ParseException");
108 }
109 catch (ParseException e) {
110 }
111
112 try {
113 m_factory.createExpression("(timedTests)");
114 fail("Expected a ParseException");
115 }
116 catch (ParseException e) {
117 }
118
119 try {
120 m_factory.createExpression("Madeup");
121 fail("Expected a ParseException");
122 }
123 catch (ParseException e) {
124 }
125 }
126
127 @Test public void testSum() throws Exception {
128 final StatisticExpression[] expressions = {
129 m_factory.createExpression("userLong0"),
130 m_factory.createExpression("userLong1"),
131 m_factory.createExpression("userLong1"), };
132
133 final StatisticExpression expression = m_factory.createSum(expressions);
134
135 myAssertEquals(5, expression);
136 assertTrue(!expression.isDouble());
137
138 myAssertEquals(2, m_factory.createExpression("(+ userLong0 userLong0)"));
139
140 myAssertEquals(4, m_factory
141 .createExpression("(+ userLong0 userLong1 userLong0)"));
142
143 myAssertEquals(5, m_factory
144 .createExpression("(+ userLong0 (+ userLong0 userLong1) userLong0)"));
145
146 myAssertEquals(0, m_factory.createExpression("(+)"));
147
148 myAssertEquals(1, m_factory.createExpression("(+ userLong0)"));
149
150 try {
151 m_factory.createExpression("(+ userLong0 timedTests)");
152 fail("Expected a ParseException");
153 }
154 catch (ParseException e) {
155 }
156 }
157
158 @Test public void testNegation() throws Exception {
159 myAssertEquals(-2,
160 m_factory.createNegation(m_factory.createExpression("userLong1")));
161
162 myAssertEquals(0.0,
163 m_factory.createNegation(m_factory.createConstant(0.0)));
164
165 myAssertEquals(100,
166 m_factory.createNegation(m_factory.createConstant(-100)));
167
168 myAssertEquals(-1, m_factory.createExpression("(- 1)"));
169
170 try {
171 m_factory.createExpression("(-)");
172 fail("Expected a ParseException");
173 }
174 catch (ParseException e) {
175 }
176
177 try {
178 m_factory.createExpression("(-1)");
179 fail("Expected a ParseException");
180 }
181 catch (ParseException e) {
182 }
183 }
184
185 @Test public void testMinus() throws Exception {
186 final StatisticExpression[] expressions = {
187 m_factory.createExpression("userLong0"),
188 m_factory.createExpression("userLong1"),
189 m_factory.createExpression("userLong1"), };
190
191 final StatisticExpression expression =
192 m_factory.createMinus(m_factory.createConstant(10), expressions);
193
194 myAssertEquals(5, expression);
195 assertTrue(!expression.isDouble());
196
197 myAssertEquals(1, m_factory.createExpression("(- userLong1 userLong0)"));
198
199 myAssertEquals(-3, m_factory
200 .createExpression("(- userLong0 userLong1 userLong1)"));
201
202 myAssertEquals(0, m_factory
203 .createExpression("(- userLong0 (- userLong0 userLong0) userLong0)"));
204
205
206 myAssertEquals(-1.0, m_factory
207 .createExpression("(- userLong0 (- userLong0 userDouble0) userLong0)"));
208
209 try {
210 m_factory.createExpression("(- userLong0 timedTests)");
211 fail("Expected a ParseException");
212 }
213 catch (ParseException e) {
214 }
215 }
216
217 @Test public void testProduct() throws Exception {
218 final StatisticExpression[] expressions = {
219 m_factory.createExpression("userLong0"),
220 m_factory.createExpression("userLong1"),
221 m_factory.createExpression("userLong1"), };
222
223 final StatisticExpression expression = m_factory.createProduct(expressions);
224
225 myAssertEquals(4, expression);
226 assertTrue(!expression.isDouble());
227
228 myAssertEquals(1, m_factory.createExpression("(* userLong0 userLong0)"));
229
230 myAssertEquals(4, m_factory
231 .createExpression("(* userLong0 userLong1 userLong1)"));
232
233 myAssertEquals(8, m_factory
234 .createExpression("(* userLong1 (* userLong1 userLong1) userLong0)"));
235
236 myAssertEquals(1, m_factory.createExpression("(*)"));
237
238 myAssertEquals(2, m_factory.createExpression("(* userLong1)"));
239
240 try {
241 m_factory.createExpression("(* timedTests timedTests)");
242 fail("Expected a ParseException");
243 }
244 catch (ParseException e) {
245 }
246 }
247
248 @Test public void testDivision() throws Exception {
249 final StatisticExpression expression =
250 m_factory.createDivision(m_factory.createExpression("userLong1"),
251 m_factory.createExpression("userLong1"));
252
253 myAssertEquals(1, expression);
254 assertTrue(expression.isDouble());
255
256 myAssertEquals(1d, m_factory.createExpression("(/ userLong0 userLong0)"));
257
258 myAssertEquals(0.5d, m_factory.createExpression("(/ userLong0 userLong1)"));
259
260 myAssertEquals(2d, m_factory.createExpression("(/ userLong1 userLong0)"));
261
262 try {
263 m_factory.createExpression("(/)");
264 fail("Expected a ParseException");
265 }
266 catch (ParseException e) {
267 }
268
269 try {
270 m_factory.createExpression("(/ userLong0)");
271 fail("Expected a ParseException");
272 }
273 catch (ParseException e) {
274 }
275
276 try {
277 m_factory.createExpression("(/ userLong0 userLong0 userLong0)");
278 fail("Expected a ParseException");
279 }
280 catch (ParseException e) {
281 }
282
283 try {
284 m_factory.createExpression("(/ timedTests userLong0)");
285 fail("Expected a ParseException");
286 }
287 catch (ParseException e) {
288 }
289 }
290
291 @Test public void testDivisionZeroNumerator() throws Exception {
292 myAssertEquals(Double.NaN,
293 m_factory.createExpression("(/ userLong2 userLong2)"));
294 }
295
296 @Test public void testSquareRoot() throws Exception {
297 final StatisticExpression expression =
298 m_factory.createSquareRoot(m_factory.createExpression("userDouble0"));
299
300 myAssertEquals(0, expression);
301 assertTrue(expression.isDouble());
302
303 m_statistics.addValue(m_indexMap.getDoubleIndex("userDouble0"), 4);
304 myAssertEquals(2, expression);
305
306 myAssertEquals(1d, m_factory.createExpression("(sqrt 1)"));
307 assertTrue(
308 Double.isNaN(m_factory.createExpression("(sqrt -1)")
309 .getDoubleValue(m_statistics)));
310
311 try {
312 m_factory.createExpression("(sqrt)");
313 fail("Expected a ParseException");
314 }
315 catch (ParseException e) {
316 }
317
318 try {
319 m_factory.createExpression("(sqtr userDouble0 userDouble0)");
320 fail("Expected a ParseException");
321 }
322 catch (ParseException e) {
323 }
324
325 try {
326 m_factory.createExpression("(sqrt timedTests)");
327 fail("Expected a ParseException");
328 }
329 catch (ParseException e) {
330 }
331 }
332
333 @Test public void testLongPeak() throws Exception {
334 final StatisticsIndexMap.LongIndex peakIndex1 = m_indexMap
335 .getLongIndex("userLong2");
336
337 final StatisticsIndexMap.LongIndex peakIndex2 = m_indexMap
338 .getLongIndex("userLong3");
339
340 final StatisticExpression expression = m_factory.createPeak(peakIndex1,
341 m_factory.createExpression("userLong1"));
342
343 myAssertEquals(0, expression);
344 assertTrue(!expression.isDouble());
345
346 final StatisticsIndexMap.LongIndex statIndex = m_indexMap
347 .getLongIndex("userLong4");
348
349 final PeakStatisticExpression peak = m_factory.createPeak(peakIndex2,
350 m_factory.createExpression("userLong4"));
351
352 final StatisticsSet statistics =
353 new StatisticsSetImplementation(m_indexMap);
354
355 statistics.setValue(statIndex, 2);
356 myAssertEquals(0, peak, statistics);
357 peak.update(statistics, statistics);
358 myAssertEquals(2, peak, statistics);
359
360 statistics.setValue(statIndex, 33);
361 peak.update(statistics, statistics);
362 myAssertEquals(33, peak, statistics);
363
364 statistics.setValue(statIndex, 2);
365 peak.update(statistics, statistics);
366 myAssertEquals(33, peak, statistics);
367 }
368
369 @Test public void testDoublePeak() throws Exception {
370 final StatisticsIndexMap.DoubleIndex peakIndex1 = m_indexMap
371 .getDoubleIndex("userDouble2");
372
373 final StatisticsIndexMap.DoubleIndex peakIndex2 = m_indexMap
374 .getDoubleIndex("userDouble3");
375
376 final StatisticExpression expression = m_factory.createPeak(peakIndex1,
377 m_factory.createExpression("(/ userLong1 userLong0)"));
378
379 myAssertEquals(0, expression);
380 assertTrue(expression.isDouble());
381
382 final StatisticsIndexMap.DoubleIndex statIndex = m_indexMap
383 .getDoubleIndex("userDouble4");
384
385 final PeakStatisticExpression peak = m_factory.createPeak(peakIndex2,
386 m_factory.createExpression("userDouble4"));
387
388 final StatisticsSet statistics =
389 new StatisticsSetImplementation(m_indexMap);
390
391 statistics.setValue(statIndex, 0.5);
392 myAssertEquals(0d, peak, statistics);
393 peak.update(statistics, statistics);
394 myAssertEquals(0.5d, peak, statistics);
395
396 statistics.setValue(statIndex, 33d);
397 peak.update(statistics, statistics);
398 myAssertEquals(33d, peak, statistics);
399
400 statistics.setValue(statIndex, -2d);
401 peak.update(statistics, statistics);
402 myAssertEquals(33d, peak, statistics);
403 }
404
405 @Test public void testLongSample() throws Exception {
406 myAssertEquals(0, m_factory.createExpression("(count timedTests)"));
407 myAssertEquals(0, m_factory.createExpression("(sum timedTests)"));
408 myAssertEquals(0, m_factory.createExpression("(variance timedTests)"));
409
410 m_statistics.addSample(m_indexMap.getLongSampleIndex("timedTests"), 2);
411 m_statistics.addSample(m_indexMap.getLongSampleIndex("timedTests"), -1);
412
413 myAssertEquals(2, m_factory.createExpression("(count timedTests)"));
414 myAssertEquals(1, m_factory.createExpression("(sum timedTests)"));
415 myAssertEquals(2.25, m_factory.createExpression("(variance timedTests)"));
416
417 try {
418 m_factory.createExpression("(sum userLong0)");
419 fail("Expected ParseException");
420 }
421 catch (ParseException e) {
422 }
423
424 try {
425 m_factory.createExpression("(count userLong0)");
426 fail("Expected ParseException");
427 }
428 catch (ParseException e) {
429 }
430 try {
431 m_factory.createExpression("(variance userLong0)");
432 fail("Expected ParseException");
433 }
434 catch (ParseException e) {
435 }
436 }
437
438 @Test public void testDoubleSample() throws Exception {
439 try {
440 final StatisticsIndexMap.DoubleIndex sumIndex =
441 m_indexMap.getDoubleIndex("userDouble0");
442 final StatisticsIndexMap.LongIndex countIndex =
443 m_indexMap.getLongIndex("userLong3");
444 final StatisticsIndexMap.DoubleIndex varianceIndex =
445 m_indexMap.getDoubleIndex("userDouble1");
446
447 final StatisticsIndexMap.DoubleSampleIndex doubleSampleIndex =
448 m_indexMap.createDoubleSampleIndex("testDoubleSampleStatistic",
449 sumIndex,
450 countIndex,
451 varianceIndex);
452 assertNotNull(doubleSampleIndex);
453
454 myAssertEquals(0, m_factory.createExpression(
455 "(count testDoubleSampleStatistic)"));
456 myAssertEquals(0, m_factory.createExpression(
457 "(sum testDoubleSampleStatistic)"));
458 myAssertEquals(0, m_factory.createExpression(
459 "(variance testDoubleSampleStatistic)"));
460
461 final StatisticsIndexMap.DoubleSampleIndex index =
462 m_indexMap.getDoubleSampleIndex("testDoubleSampleStatistic");
463 m_statistics.addSample(index, 2);
464 m_statistics.addSample(index, -1);
465
466 myAssertEquals(2, m_factory.createExpression(
467 "(count testDoubleSampleStatistic)"));
468 myAssertEquals(1, m_factory.createExpression(
469 "(sum testDoubleSampleStatistic)"));
470 myAssertEquals(2.25, m_factory.createExpression(
471 "(variance testDoubleSampleStatistic)"));
472
473 try {
474 m_factory.createExpression("(sum userDouble0)");
475 fail("Expected ParseException");
476 }
477 catch (ParseException e) {
478 }
479
480 try {
481 m_factory.createExpression("(count userDouble0)");
482 fail("Expected ParseException");
483 }
484 catch (ParseException e) {
485 }
486
487 try {
488 m_factory.createExpression("(variance userDouble0)");
489 fail("Expected ParseException");
490 }
491 catch (ParseException e) {
492 }
493 }
494 finally {
495 m_indexMap.removeDoubleSampleIndex("testDoubleSampleStatistic");
496 }
497 }
498
499 @Test public void testParseCompoundExpessions() throws Exception {
500 myAssertEquals(0.5, m_factory
501 .createExpression("(/ userLong0 (+ userLong0 userLong0))"));
502
503 myAssertEquals(
504 2.25d,
505 m_factory.createExpression(
506 "(+ userLong0 (/ userLong0 (+ userLong1 userLong1)) userLong0)"));
507
508 myAssertEquals(
509 2.25d,
510 m_factory.createExpression(
511 "(+ userLong0 (/ userLong0 (* userLong1 userLong1)) userLong0)"));
512
513 myAssertEquals(
514 9d,
515 m_factory.createExpression(
516 "(* 4 (+ userLong0 (/ userLong0 (* userLong1 userLong1)) userLong0))"));
517 }
518
519 @Test public void testParseInvalidExpessions() throws Exception {
520 try {
521 m_factory.createExpression("(+");
522 fail("Expected a ParseException");
523 }
524 catch (ParseException e) {
525 }
526
527 try {
528 m_factory.createExpression("+)");
529 fail("Expected a ParseException");
530 }
531 catch (ParseException e) {
532 }
533
534 try {
535 m_factory.createExpression("(/ 1 2");
536 fail("Expected a ParseException");
537 }
538 catch (ParseException e) {
539 }
540 }
541
542 @Test public void testNormaliseExpressionString() throws Exception {
543 assertEquals("userLong0",
544 m_factory.normaliseExpressionString(" userLong0 "));
545
546 assertEquals("(+ userLong0 userLong1 (* userLong0 userLong1))",
547 m_factory.normaliseExpressionString(
548 "\t(+ userLong0 userLong1( \n * userLong0 userLong1) )"));
549
550 try {
551 m_factory.normaliseExpressionString("userLong0 userLong0");
552 fail("Expected ParseException");
553 }
554 catch (ParseException e) {
555 }
556 }
557
558 private void myAssertEquals(long expected, StatisticExpression expression) {
559 myAssertEquals(expected, expression, m_statistics);
560 }
561
562 private void myAssertEquals(long expected,
563 StatisticExpression expression,
564 StatisticsSet statistics) {
565 assertEquals(expected, expression.getLongValue(statistics));
566 myAssertEquals((double) expected, expression, statistics);
567 }
568
569 private void myAssertEquals(double expected, StatisticExpression expression) {
570 myAssertEquals(expected, expression, m_statistics);
571 }
572
573 private void myAssertEquals(double expected,
574 StatisticExpression expression,
575 StatisticsSet statistics) {
576 assertEquals(expected, expression.getDoubleValue(statistics), 0.00001d);
577 }
578 }