test/jdk/java/lang/String/Indent.java
author jlaskey
Wed, 09 Jan 2019 16:41:16 -0400
changeset 53231 ee1f64096d7c
parent 52312 test/jdk/java/lang/String/AlignIndent.java@5b9c8d77a9fe
child 53232 32c6cc430526
child 53245 05c5c27b5a54
permissions -rw-r--r--
8215489: Remove String::align Reviewed-by: vromero, sundar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
51714
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
     1
/*
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
     4
 *
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
     7
 * published by the Free Software Foundation.
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
     8
 *
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    13
 * accompanied this code).
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    14
 *
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    18
 *
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    21
 * questions.
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    22
 */
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    23
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    24
/*
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    25
 * @test
53231
ee1f64096d7c 8215489: Remove String::align
jlaskey
parents: 52312
diff changeset
    26
 * @summary Unit tests for String#indent
ee1f64096d7c 8215489: Remove String::align
jlaskey
parents: 52312
diff changeset
    27
 * @run main Indent
51714
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    28
 */
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    29
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    30
import java.util.Arrays;
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    31
import java.util.List;
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    32
import java.util.stream.Collectors;
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    33
import java.util.stream.Stream;
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    34
53231
ee1f64096d7c 8215489: Remove String::align
jlaskey
parents: 52312
diff changeset
    35
public class Indent {
51714
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    36
    static final List<String> ENDS = List.of("", "\n", "   \n", "\n\n", "\n\n\n");
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    37
    static final List<String> MIDDLES = List.of(
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    38
            "",
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    39
            "xyz",
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    40
            "   xyz",
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    41
            "      xyz",
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    42
            "xyz   ",
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    43
            "   xyz   ",
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    44
            "      xyz   ",
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    45
            "xyz\u2022",
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    46
            "   xyz\u2022",
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    47
            "xyz\u2022   ",
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    48
            "   xyz\u2022   ",
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    49
            "   // comment"
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    50
    );
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    51
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    52
    public static void main(String[] args) {
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    53
        test1();
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    54
    }
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    55
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    56
    /*
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    57
     * Test String#indent(int n) functionality.
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    58
     */
53231
ee1f64096d7c 8215489: Remove String::align
jlaskey
parents: 52312
diff changeset
    59
    static void test1() {
51714
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    60
        for (int adjust : new int[] {-8, -7, -4, -3, -2, -1, 0, 1, 2, 3, 4, 7, 8}) {
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    61
            for (String prefix : ENDS) {
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    62
                for (String suffix : ENDS) {
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    63
                    for (String middle : MIDDLES) {
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    64
                        String input = prefix + "   abc   \n" + middle + "\n   def   \n" + suffix;
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    65
                        String output = input.indent(adjust);
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    66
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    67
                        Stream<String> stream = input.lines();
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    68
                        if (adjust > 0) {
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    69
                            final String spaces = " ".repeat(adjust);
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    70
                            stream = stream.map(s -> s.isBlank() ? s : spaces + s);
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    71
                        } else if (adjust < 0) {
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    72
                            stream = stream.map(s -> s.substring(Math.min(-adjust, indexOfNonWhitespace(s))));
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    73
                        }
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    74
                        String expected = stream.collect(Collectors.joining("\n", "", "\n"));
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    75
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    76
                        if (!output.equals(expected)) {
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    77
                            report("String::indent(int n)",
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    78
                                    "Result indentation not as expected", expected, output);
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    79
                        }
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    80
                    }
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    81
                }
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    82
            }
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    83
        }
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    84
    }
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    85
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    86
    public static int indexOfNonWhitespace(String s) {
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    87
        int left = 0;
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    88
        while (left < s.length()) {
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    89
            char ch = s.charAt(left);
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    90
            if (ch != ' ' && ch != '\t' && !Character.isWhitespace(ch)) {
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    91
                break;
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    92
            }
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    93
            left++;
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    94
        }
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    95
        return left;
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    96
    }
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    97
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    98
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
    99
    private static String[] getBody(String[] inLines) {
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
   100
        int from = -1, to = -1;
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
   101
        for (int i = 0; i < inLines.length; i++) {
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
   102
            String line = inLines[i];
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
   103
            if (!line.isBlank()) {
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
   104
                if (from == -1) {
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
   105
                    from = i;
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
   106
                }
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
   107
                to = i + 1;
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
   108
            }
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
   109
        }
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
   110
        return Arrays.copyOfRange(inLines, from, to);
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
   111
    }
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
   112
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
   113
    /*
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
   114
     * Report difference in result.
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
   115
     */
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
   116
    static void report(String test, String message, String input, String output) {
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
   117
        System.err.println("Testing " + test + ": " + message);
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
   118
        System.err.println();
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
   119
        System.err.println("Input: length = " + input.length());
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
   120
        System.err.println("_".repeat(40));
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
   121
        System.err.print(input.replaceAll(" ", "."));
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
   122
        System.err.println("_".repeat(40));
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
   123
        System.err.println();
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
   124
        System.err.println("Output: length = " + output.length());
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
   125
        System.err.println("_".repeat(40));
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
   126
        System.err.print(output.replaceAll(" ", "."));
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
   127
        System.err.println("_".repeat(40));
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
   128
        throw new RuntimeException();
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
   129
    }
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents:
diff changeset
   130
}