jdk/test/java/io/RandomAccessFile/ParameterCheck.java
author duke
Wed, 05 Jul 2017 22:06:37 +0200
changeset 40471 ad45133ff99e
parent 7668 d4a77089c587
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 5810
diff changeset
     2
 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
  @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
  @bug 4030253 4030278 4030243
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
  @summary Test for correct parameter checking in read(byte[], int, int),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
  readFully(byte[], int, int) and write(byte[], int, int) of RandomAccessFile
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
public class ParameterCheck {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    static int off[] = {-1, -1,  0, 0, 33, 33, 0, 32,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
                        32, 4, 1, 0, -1, Integer.MAX_VALUE, 1};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    static int len[] = {-1,  0, -1, 33, 0, 4, 32,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
                        0, 4, 16, 31, 0, Integer.MAX_VALUE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
                        Integer.MAX_VALUE, Integer.MAX_VALUE};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    static boolean results[] = { false,  false,  false, false, false, false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
                                 true, true, false, true, true, true, false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
                                 false, false };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    static int numBad = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static void doTest(String method) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        File fn = new File("x.ParameterCheck");
5810
e83d67ad8c96 6962419: TEST_BUG: java_io tests fails in samevm mode
alanb
parents: 5506
diff changeset
    47
        RandomAccessFile raf = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            byte b[] = new byte[32];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            int numCases = off.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            int[] got = new int[numCases];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            int numGood = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            FileOutputStream fout = new FileOutputStream(fn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            for (int i = 0; i < 32; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                fout.write(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            fout.close();
5810
e83d67ad8c96 6962419: TEST_BUG: java_io tests fails in samevm mode
alanb
parents: 5506
diff changeset
    59
            raf =  new RandomAccessFile(fn , "rw");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            System.err.println("-----------------------------" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                               "-----------------------------");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            System.err.println("\nRandomAccessFile." + method +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                               "\nTotal test cases = " + (off.length+1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            System.err.println("-----------------------------" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                               "-----------------------------");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            for(int i = 0; i < numCases; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                    if (method.equals("readFully")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                        raf.readFully(b , off[i] , len[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                    if (method.equals("read")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                        raf.read(b , off[i] , len[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                    if (method.equals("write")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                        raf.write(b , off[i] , len[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                    raf.seek(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                } catch(IndexOutOfBoundsException aiobe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                    if (results[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                        printErr(method , numGood,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                                 i, "java.lang.IndexOutOfBoundsException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                        numGood++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                } catch(OutOfMemoryError ome) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                    printErr(method, numGood,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                             i, "java.lang.OutOfMemoryError");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                if (results[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    numGood++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                    printErr(method, numGood,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                             i, "No java.lang.IndexOutOfBoundsException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            raf.seek(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            boolean thrown = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                if (method.equals("readFully")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                    raf.readFully(null, 1, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                if (method.equals("read")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    raf.read(null, 1, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                if (method.equals("write")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    raf.write(null, 1, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            } catch(NullPointerException npe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                numGood++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                thrown = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            if (!thrown) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                printErr(method, numGood, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                         "no NullPointerException for null b");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            System.err.println("\nTotal passed = " + numGood);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            System.err.println("-----------------------------" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                               "-----------------------------");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        } finally {
5810
e83d67ad8c96 6962419: TEST_BUG: java_io tests fails in samevm mode
alanb
parents: 5506
diff changeset
   129
            if (raf != null)
e83d67ad8c96 6962419: TEST_BUG: java_io tests fails in samevm mode
alanb
parents: 5506
diff changeset
   130
                raf.close();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            fn.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    private static void printErr(String method, int numGood,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                                 int i, String expStr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        numBad++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        System.err.println("\nNumber passed so far = " + numGood +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                           "\nUnexpected " + expStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        if ( i < 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            System.err.println("for case : b = null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            System.err.println("for case : b.length = " + 32 +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                               " off = " + off[i] +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                               " len = " + len[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    public static void main(String argv[]) throws Exception{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        doTest("read");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        doTest("readFully");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        doTest("write");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        if (numBad > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            throw new RuntimeException("Failed " + numBad + " tests");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
}