PageRenderTime 46ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/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
Possible License(s): Apache-2.0
  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. */
  16. package com.google.test.metric.report.chart;
  17. import static java.lang.Integer.MAX_VALUE;
  18. import java.util.Arrays;
  19. import junit.framework.TestCase;
  20. import com.google.test.metric.report.chart.Histogram.Linear;
  21. import com.google.test.metric.report.chart.Histogram.Logarithmic;
  22. public class HistogramTest extends TestCase {
  23. public void testSimpleBreakdown() throws Exception {
  24. Histogram histogram = new Histogram(1, 1, 3, new Linear());
  25. histogram.value(1);
  26. histogram.value(1);
  27. histogram.value(1);
  28. histogram.value(2);
  29. histogram.value(2);
  30. histogram.value(3);
  31. assertArrayEquals(array(3, 2, 1), histogram.getBins());
  32. assertArrayEquals(array(30, 0, 0), histogram.getScaledBinRange(1, 2, 30));
  33. assertArrayEquals(array(0, 20, 0), histogram.getScaledBinRange(2, 3, 30));
  34. assertArrayEquals(array(0, 0, 10), histogram.getScaledBinRange(3, MAX_VALUE, 30));
  35. }
  36. private void assertArrayEquals(int[] expected, int[] actual) {
  37. assertTrue(String.format("Expected %s was %s", Arrays.toString(expected),
  38. Arrays.toString(actual)), Arrays.equals(expected, actual));
  39. }
  40. private void assertArrayEquals(String[] expected, String[] actual) {
  41. assertTrue(String.format("Expected %s was %s", Arrays.toString(expected),
  42. Arrays.toString(actual)), Arrays.equals(expected, actual));
  43. }
  44. public void testLogarithmicScaling() throws Exception {
  45. Histogram histogram = new Histogram(1, 1, 3, new Logarithmic());
  46. for (int i=0; i<100; i++) {
  47. histogram.value(1);
  48. }
  49. for (int i=0; i<10; i++) {
  50. histogram.value(2);
  51. }
  52. histogram.value(3);
  53. assertArrayEquals(array(100, 10, 1), histogram.getBins());
  54. assertArrayEquals(array(30, 0, 0), histogram.getScaledBinRange(1, 2, 30));
  55. assertArrayEquals(array(0, 20, 0), histogram.getScaledBinRange(2, 3, 30));
  56. assertArrayEquals(array(0, 0, 10), histogram.getScaledBinRange(3, MAX_VALUE, 30));
  57. }
  58. private int[] array(int... values) {
  59. return values;
  60. }
  61. private String[] array(String... values) {
  62. return values;
  63. }
  64. }