PageRenderTime 33ms CodeModel.GetById 13ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 1ms

/testability-explorer/src/test/java/com/google/test/metric/report/chart/HistogramTest.java

http://testability-explorer.googlecode.com/
Java | 77 lines | 49 code | 13 blank | 15 comment | 2 complexity | a9dae2863cabc2a3c3089015986f0a33 MD5 | raw file
 1/*
 2 * Copyright 2007 Google Inc.
 3 *
 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 5 * use this file except in compliance with the License. You may obtain a copy of
 6 * the License at
 7 *
 8 * http://www.apache.org/licenses/LICENSE-2.0
 9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16package com.google.test.metric.report.chart;
17
18import static java.lang.Integer.MAX_VALUE;
19
20import java.util.Arrays;
21
22import junit.framework.TestCase;
23
24import com.google.test.metric.report.chart.Histogram.Linear;
25import com.google.test.metric.report.chart.Histogram.Logarithmic;
26
27public class HistogramTest extends TestCase {
28
29  public void testSimpleBreakdown() throws Exception {
30    Histogram histogram = new Histogram(1, 1, 3, new Linear());
31    histogram.value(1);
32    histogram.value(1);
33    histogram.value(1);
34    histogram.value(2);
35    histogram.value(2);
36    histogram.value(3);
37    assertArrayEquals(array(3, 2, 1), histogram.getBins());
38    assertArrayEquals(array(30, 0, 0), histogram.getScaledBinRange(1, 2, 30));
39    assertArrayEquals(array(0, 20, 0), histogram.getScaledBinRange(2, 3, 30));
40    assertArrayEquals(array(0, 0, 10), histogram.getScaledBinRange(3, MAX_VALUE, 30));
41  }
42
43  private void assertArrayEquals(int[] expected, int[] actual) {
44    assertTrue(String.format("Expected %s was %s", Arrays.toString(expected),
45        Arrays.toString(actual)), Arrays.equals(expected, actual));
46  }
47
48  private void assertArrayEquals(String[] expected, String[] actual) {
49    assertTrue(String.format("Expected %s was %s", Arrays.toString(expected),
50        Arrays.toString(actual)), Arrays.equals(expected, actual));
51  }
52
53  public void testLogarithmicScaling() throws Exception {
54    Histogram histogram = new Histogram(1, 1, 3, new Logarithmic());
55    for (int i=0; i<100; i++) {
56      histogram.value(1);
57    }
58    for (int i=0; i<10; i++) {
59      histogram.value(2);
60    }
61    histogram.value(3);
62
63    assertArrayEquals(array(100, 10, 1), histogram.getBins());
64    assertArrayEquals(array(30, 0, 0), histogram.getScaledBinRange(1, 2, 30));
65    assertArrayEquals(array(0, 20, 0), histogram.getScaledBinRange(2, 3, 30));
66    assertArrayEquals(array(0, 0, 10), histogram.getScaledBinRange(3, MAX_VALUE, 30));
67  }
68
69  private int[] array(int... values) {
70    return values;
71  }
72
73  private String[] array(String... values) {
74    return values;
75  }
76
77}