1/*
2 * Copyright (c) 2013, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23package org.graalvm.compiler.debug.test;
24
25import java.io.ByteArrayOutputStream;
26import java.io.PrintStream;
27
28import org.junit.Assert;
29import org.junit.Test;
30
31import org.graalvm.compiler.debug.Debug;
32import org.graalvm.compiler.debug.DebugHistogram;
33import org.graalvm.compiler.debug.internal.DebugHistogramAsciiPrinter;
34import org.graalvm.compiler.debug.internal.DebugHistogramRPrinter;
35
36public class DebugHistogramTest {
37
38    @Test
39    public void testEmptyHistogram() {
40        DebugHistogram histogram = Debug.createHistogram("TestHistogram");
41        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
42
43        new DebugHistogramAsciiPrinter(new PrintStream(outputStream)).print(histogram);
44        String line = outputStream.toString().split("\r?\n")[0];
45        Assert.assertEquals("TestHistogram is empty.", line);
46
47        outputStream.reset();
48        new DebugHistogramRPrinter(new PrintStream(outputStream)).print(histogram);
49        Assert.assertEquals("", outputStream.toString());
50    }
51
52    @Test
53    public void testSingleEntryHistogram() {
54        DebugHistogram histogram = Debug.createHistogram("TestHistogram");
55        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
56        histogram.add(new Integer(1));
57        histogram.add(new Integer(1));
58        new DebugHistogramAsciiPrinter(new PrintStream(outputStream)).print(histogram);
59        String[] lines = outputStream.toString().split("\r?\n");
60        // @formatter:off
61        String[] expected = {
62            "TestHistogram has 1 unique elements and 2 total elements:",
63            "--------------------------------------------------------------------------------------------------------------------------------------------------------------------------",
64            "| 1                                                  | 2          | ==================================================================================================== |",
65            "--------------------------------------------------------------------------------------------------------------------------------------------------------------------------"
66        };
67        // @formatter:on
68        Assert.assertArrayEquals(expected, lines);
69
70        outputStream.reset();
71        new DebugHistogramRPrinter(new PrintStream(outputStream)).print(histogram);
72        lines = outputStream.toString().split("\r?\n");
73        // @formatter:off
74        expected = new String[] {
75            "TestHistogram <- c(2);",
76            "names(TestHistogram) <- c(\"1\");"
77        };
78        // @formatter:on
79        Assert.assertArrayEquals(expected, lines);
80    }
81
82    @Test
83    public void testMultipleEntryHistogram() {
84        DebugHistogram histogram = Debug.createHistogram("TestHistogram");
85        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
86        histogram.add(new Integer(1));
87        histogram.add(new Integer(2));
88        histogram.add(new Integer(2));
89        new DebugHistogramAsciiPrinter(new PrintStream(outputStream)).print(histogram);
90        String[] lines = outputStream.toString().split("\r?\n");
91        // @formatter:off
92        String[] expected = new String[] {
93            "TestHistogram has 2 unique elements and 3 total elements:",
94            "--------------------------------------------------------------------------------------------------------------------------------------------------------------------------",
95            "| 2                                                  | 2          | ==================================================================================================== |",
96            "| 1                                                  | 1          | ==================================================                                                   |",
97            "--------------------------------------------------------------------------------------------------------------------------------------------------------------------------"
98        };
99        // @formatter:on
100        Assert.assertArrayEquals(expected, lines);
101
102        outputStream.reset();
103        new DebugHistogramRPrinter(new PrintStream(outputStream)).print(histogram);
104        lines = outputStream.toString().split("\r?\n");
105        // @formatter:off
106        expected = new String[] {
107            "TestHistogram <- c(2, 1);",
108            "names(TestHistogram) <- c(\"2\", \"1\");"
109        };
110        // @formatter:on
111        Assert.assertArrayEquals(expected, lines);
112    }
113
114    @Test
115    public void testTooLongValueString() {
116        DebugHistogram histogram = Debug.createHistogram("TestHistogram");
117        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
118        histogram.add("MyCustomValue");
119        new DebugHistogramAsciiPrinter(new PrintStream(outputStream), Integer.MAX_VALUE, 10, 10, 1).print(histogram);
120        String[] lines = outputStream.toString().split("\r?\n");
121        Assert.assertEquals(4, lines.length);
122        Assert.assertEquals("TestHistogram has 1 unique elements and 1 total elements:", lines[0]);
123        Assert.assertEquals("----------------------------------------", lines[1]);
124        Assert.assertEquals("| MyCusto... | 1          | ========== |", lines[2]);
125        Assert.assertEquals("----------------------------------------", lines[3]);
126    }
127}
128