001/*
002 * Copyright (c) 2013, 2013, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.debug.test;
024
025import java.io.*;
026
027import com.oracle.graal.debug.*;
028import com.oracle.graal.debug.internal.*;
029
030import org.junit.*;
031
032public class DebugHistogramTest {
033
034    @Test
035    public void testEmptyHistogram() {
036        DebugHistogram histogram = Debug.createHistogram("TestHistogram");
037        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
038
039        new DebugHistogramAsciiPrinter(new PrintStream(outputStream)).print(histogram);
040        String line = outputStream.toString().split("\r?\n")[0];
041        Assert.assertEquals("TestHistogram is empty.", line);
042
043        outputStream.reset();
044        new DebugHistogramRPrinter(new PrintStream(outputStream)).print(histogram);
045        Assert.assertEquals("", outputStream.toString());
046    }
047
048    @Test
049    public void testSingleEntryHistogram() {
050        DebugHistogram histogram = Debug.createHistogram("TestHistogram");
051        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
052        histogram.add(new Integer(1));
053        histogram.add(new Integer(1));
054        new DebugHistogramAsciiPrinter(new PrintStream(outputStream)).print(histogram);
055        String[] lines = outputStream.toString().split("\r?\n");
056        // @formatter:off
057        String[] expected = {
058            "TestHistogram has 1 unique elements and 2 total elements:",
059            "--------------------------------------------------------------------------------------------------------------------------------------------------------------------------",
060            "| 1                                                  | 2          | ==================================================================================================== |",
061            "--------------------------------------------------------------------------------------------------------------------------------------------------------------------------"
062        };
063        // @formatter:on
064        Assert.assertArrayEquals(expected, lines);
065
066        outputStream.reset();
067        new DebugHistogramRPrinter(new PrintStream(outputStream)).print(histogram);
068        lines = outputStream.toString().split("\r?\n");
069        // @formatter:off
070        expected = new String[] {
071            "TestHistogram <- c(2);",
072            "names(TestHistogram) <- c(\"1\");"
073        };
074        // @formatter:on
075        Assert.assertArrayEquals(expected, lines);
076    }
077
078    @Test
079    public void testMultipleEntryHistogram() {
080        DebugHistogram histogram = Debug.createHistogram("TestHistogram");
081        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
082        histogram.add(new Integer(1));
083        histogram.add(new Integer(2));
084        histogram.add(new Integer(2));
085        new DebugHistogramAsciiPrinter(new PrintStream(outputStream)).print(histogram);
086        String[] lines = outputStream.toString().split("\r?\n");
087        // @formatter:off
088        String[] expected = new String[] {
089            "TestHistogram has 2 unique elements and 3 total elements:",
090            "--------------------------------------------------------------------------------------------------------------------------------------------------------------------------",
091            "| 2                                                  | 2          | ==================================================================================================== |",
092            "| 1                                                  | 1          | ==================================================                                                   |",
093            "--------------------------------------------------------------------------------------------------------------------------------------------------------------------------"
094        };
095        // @formatter:on
096        Assert.assertArrayEquals(expected, lines);
097
098        outputStream.reset();
099        new DebugHistogramRPrinter(new PrintStream(outputStream)).print(histogram);
100        lines = outputStream.toString().split("\r?\n");
101        // @formatter:off
102        expected = new String[] {
103            "TestHistogram <- c(2, 1);",
104            "names(TestHistogram) <- c(\"2\", \"1\");"
105        };
106        // @formatter:on
107        Assert.assertArrayEquals(expected, lines);
108    }
109
110    @Test
111    public void testTooLongValueString() {
112        DebugHistogram histogram = Debug.createHistogram("TestHistogram");
113        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
114        histogram.add("MyCustomValue");
115        new DebugHistogramAsciiPrinter(new PrintStream(outputStream), Integer.MAX_VALUE, 10, 10, 1).print(histogram);
116        String[] lines = outputStream.toString().split("\r?\n");
117        Assert.assertEquals(4, lines.length);
118        Assert.assertEquals("TestHistogram has 1 unique elements and 1 total elements:", lines[0]);
119        Assert.assertEquals("----------------------------------------", lines[1]);
120        Assert.assertEquals("| MyCusto... | 1          | ========== |", lines[2]);
121        Assert.assertEquals("----------------------------------------", lines[3]);
122    }
123}