test/jdk/java/io/Reader/ReaderBulkReadContract.java
author mli
Wed, 23 May 2018 14:21:14 +0800
changeset 50230 cae567ae015d
parent 47216 71c04702a3d5
permissions -rw-r--r--
8202771: Migrate Unicode character tests to JDK Repo Reviewed-by: naoto Contributed-by: dan.z.zhou@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
30443
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
     1
/*
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
     4
 *
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
     7
 * published by the Free Software Foundation.
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
     8
 *
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    13
 * accompanied this code).
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    14
 *
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    18
 *
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    21
 * questions.
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    22
 */
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    23
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    24
import java.io.BufferedReader;
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    25
import java.io.ByteArrayInputStream;
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    26
import java.io.CharArrayReader;
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    27
import java.io.File;
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    28
import java.io.FileReader;
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    29
import java.io.FileWriter;
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    30
import java.io.IOException;
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    31
import java.io.InputStreamReader;
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    32
import java.io.LineNumberReader;
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    33
import java.io.PipedReader;
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    34
import java.io.PipedWriter;
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    35
import java.io.PushbackReader;
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    36
import java.io.Reader;
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    37
import java.io.StringReader;
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    38
import java.io.UncheckedIOException;
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    39
import java.util.Arrays;
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    40
import java.util.Collections;
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    41
import java.util.Iterator;
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    42
import java.util.LinkedList;
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    43
import java.util.List;
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    44
import java.util.concurrent.ConcurrentHashMap;
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    45
import java.util.function.Function;
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    46
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    47
import static java.lang.String.format;
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    48
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    49
/*
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    50
 * @test
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    51
 * @bug 8029689
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    52
 * @summary checks the bounds part of the contract of java.io.Reader.read(char[], int, int):
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    53
 *
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    54
 *              0 <= off <= off+len <= cbuf.length
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    55
 *
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    56
 *          for publicly exported subtypes of java.io.Reader
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    57
 */
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    58
public class ReaderBulkReadContract {
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    59
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    60
    public static void main(String[] args) throws IOException {
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    61
        ReaderBulkReadContract t = new ReaderBulkReadContract();
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    62
        t.test();
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    63
    }
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    64
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    65
    private void test() throws IOException {
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    66
        Iterator<Object[]> args = args();
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    67
        while (args.hasNext()) {
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    68
            Object[] a = args.next();
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    69
            Reader r = (Reader) a[0];
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    70
            int size = (int) a[1];
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    71
            int off = (int) a[2];
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    72
            int len = (int) a[3];
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    73
            try {
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    74
                read(r, size, off, len);
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    75
            } finally {
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    76
                r.close();
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    77
            }
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    78
        }
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    79
    }
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    80
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    81
    private Iterator<Object[]> args() {
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    82
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    83
        Integer[] lens = {Integer.MIN_VALUE, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, Integer.MAX_VALUE};
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    84
        Integer[] offs = {Integer.MIN_VALUE, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, Integer.MAX_VALUE};
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    85
        Integer[] sizes = {0, 1, 2, 3, 4, 5};
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    86
        String[] contents = {"", "a", "ab"};
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    87
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    88
        List<Function<String, Reader>> fs = Arrays.asList(
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    89
                (String s) -> new BufferedReader(new StringReader(s)),
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    90
                (String s) -> new LineNumberReader(new StringReader(s)),
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    91
                (String s) -> new CharArrayReader(s.toCharArray()),
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    92
                (String s) -> new InputStreamReader(new ByteArrayInputStream(s.getBytes())),
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    93
                (String s) -> newFileReader(s),
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    94
                (String s) -> new PushbackReader(new StringReader(s)),
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    95
                (String s) -> newPipedReader(s),
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    96
                (String s) -> new StringReader(s)
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    97
        );
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    98
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
    99
        // The easiest way to produce a cartesian product from a small fixed number of sets
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   100
        List<Object[]> tuples = Collections.synchronizedList(new LinkedList<>());
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   101
        for (Integer len : lens)
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   102
            for (Integer off : offs)
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   103
                for (String s : contents)
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   104
                    for (Integer size : sizes)
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   105
                        for (Function<String, Reader> f : fs)
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   106
                            tuples.add(new Object[]{f.apply(s), size, off, len});
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   107
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   108
        return tuples.iterator();
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   109
    }
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   110
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   111
    private void read(Reader r, int size, int off, int len) throws IOException {
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   112
        IndexOutOfBoundsException ex = null;
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   113
        try {
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   114
            r.read(new char[size], off, len);
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   115
        } catch (IndexOutOfBoundsException e) {
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   116
            ex = e;
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   117
        }
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   118
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   119
        boolean incorrectBounds = off < 0 || len < 0 || len > size - off;
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   120
        boolean exceptionThrown = ex != null;
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   121
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   122
        if (incorrectBounds != exceptionThrown) { // incorrectBounds iff exceptionThrown
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   123
            throw new AssertionError(format("r=%s, size=%s, off=%s, len=%s, incorrectBounds=%s, exceptionThrown=%s",
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   124
                    r, size, off, len, incorrectBounds, exceptionThrown));
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   125
        }
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   126
    }
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   127
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   128
    private static PipedReader newPipedReader(String contents) {
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   129
        try (PipedWriter w = new PipedWriter()) {
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   130
            PipedReader r = new PipedReader(w);
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   131
            w.write(contents);
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   132
            return r;
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   133
        } catch (IOException e) {
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   134
            throw new UncheckedIOException(e);
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   135
        }
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   136
    }
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   137
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   138
    private FileReader newFileReader(String contents) {
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   139
        try {
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   140
            // To not create an enormous amount of files
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   141
            File f = cache.computeIfAbsent(contents,
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   142
                    ReaderBulkReadContract::createTempFileWithContents);
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   143
            return new FileReader(f);
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   144
        } catch (IOException e) {
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   145
            throw new UncheckedIOException(e);
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   146
        }
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   147
    }
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   148
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   149
    private static File createTempFileWithContents(String contents) {
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   150
        try {
39570
f774415f91ad 8160215: jdk/test/java/io/Reader/ReaderBulkReadContract.java should clean up better
bpb
parents: 30443
diff changeset
   151
            File testDir = new File(System.getProperty("test.dir", "."));
f774415f91ad 8160215: jdk/test/java/io/Reader/ReaderBulkReadContract.java should clean up better
bpb
parents: 30443
diff changeset
   152
            File file = File.createTempFile("ReaderContract", "", testDir);
30443
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   153
            try (FileWriter w = new FileWriter(file)) {
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   154
                w.write(contents);
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   155
            }
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   156
            return file;
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   157
        } catch (IOException e) {
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   158
            throw new UncheckedIOException(e);
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   159
        }
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   160
    }
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   161
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   162
    //
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   163
    // To avoid myriads of tiny files a cache is used.
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   164
    // ConcurrentHashMap.computeIfAbsent promises a crucial thing:
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   165
    //
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   166
    // ...The entire method invocation is performed atomically, so the
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   167
    // function is applied at most once per key...
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   168
    //  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   169
    //
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   170
    private final ConcurrentHashMap<String, File> cache = new ConcurrentHashMap<>();
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
diff changeset
   171
}