jdk/test/java/nio/Buffer/StringCharBufferSliceTest.java
author prappo
Mon, 16 May 2016 09:54:01 +0100
changeset 37913 3cc95b690353
parent 7668 d4a77089c587
permissions -rw-r--r--
8156931: java.nio.Buffer tests cleanup Reviewed-by: alanb
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: 7275
diff changeset
     2
 * Copyright (c) 2005, 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: 715
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
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
/* @test
7275
608d2708ab12 7000913: (bf) CharBuffer.wrap, slice, position, slice leads to CharBuffer with incorrect offser
alanb
parents: 5506
diff changeset
    25
 * @bug 4997655 7000913
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary (bf) CharBuffer.slice() on wrapped CharSequence results in wrong position
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
public class StringCharBufferSliceTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
    public static void main( String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
        System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
            ">>> StringCharBufferSliceTest-main: testing the slice method...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
        final String in = "for testing";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
        System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
            ">>> StringCharBufferSliceTest-main: testing with the position 0.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        CharBuffer buff = CharBuffer.wrap(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        test(buff, buff.slice());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
            ">>> StringCharBufferSliceTest-main: testing with new position.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        buff.position(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        test(buff, buff.slice());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
          ">>> StringCharBufferSliceTest-main: testing with non zero initial position.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        buff = CharBuffer.wrap(in, 3, in.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        test(buff, buff.slice());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
69
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
    56
        System.out.println(
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
    57
            ">>> StringCharBufferSliceTest-main: testing slice result with get()");
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
    58
        buff.position(4);
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
    59
        buff.limit(7);
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
    60
        CharBuffer slice = buff.slice();
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
    61
        for (int i = 0; i < 3; i++) {
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
    62
            if (slice.get() != buff.get()) {
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
    63
                throw new RuntimeException("Wrong characters in slice result.");
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
    64
            }
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
    65
        }
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
    66
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
    67
        System.out.println(
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
    68
            ">>> StringCharBufferSliceTest-main: testing slice result with get(int)");
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
    69
        buff.position(4);
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
    70
        buff.limit(7);
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
    71
        slice = buff.slice();
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
    72
        for (int i = 0; i < 3; i++) {
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
    73
            if (slice.get(i) != buff.get(4 + i)) {
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
    74
                throw new RuntimeException("Wrong characters in slice result.");
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
    75
            }
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
    76
        }
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
    77
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
    78
        System.out.println(
7275
608d2708ab12 7000913: (bf) CharBuffer.wrap, slice, position, slice leads to CharBuffer with incorrect offser
alanb
parents: 5506
diff changeset
    79
          ">>> StringCharBufferSliceTest-main: testing slice with result of slice");
608d2708ab12 7000913: (bf) CharBuffer.wrap, slice, position, slice leads to CharBuffer with incorrect offser
alanb
parents: 5506
diff changeset
    80
        buff.position(0);
608d2708ab12 7000913: (bf) CharBuffer.wrap, slice, position, slice leads to CharBuffer with incorrect offser
alanb
parents: 5506
diff changeset
    81
        buff.limit(buff.capacity());
608d2708ab12 7000913: (bf) CharBuffer.wrap, slice, position, slice leads to CharBuffer with incorrect offser
alanb
parents: 5506
diff changeset
    82
        slice = buff.slice();
608d2708ab12 7000913: (bf) CharBuffer.wrap, slice, position, slice leads to CharBuffer with incorrect offser
alanb
parents: 5506
diff changeset
    83
        for (int i=0; i<4; i++) {
608d2708ab12 7000913: (bf) CharBuffer.wrap, slice, position, slice leads to CharBuffer with incorrect offser
alanb
parents: 5506
diff changeset
    84
            slice.position(i);
608d2708ab12 7000913: (bf) CharBuffer.wrap, slice, position, slice leads to CharBuffer with incorrect offser
alanb
parents: 5506
diff changeset
    85
            CharBuffer nextSlice = slice.slice();
608d2708ab12 7000913: (bf) CharBuffer.wrap, slice, position, slice leads to CharBuffer with incorrect offser
alanb
parents: 5506
diff changeset
    86
            if (nextSlice.position() != 0)
608d2708ab12 7000913: (bf) CharBuffer.wrap, slice, position, slice leads to CharBuffer with incorrect offser
alanb
parents: 5506
diff changeset
    87
                throw new RuntimeException("New buffer's position should be zero");
608d2708ab12 7000913: (bf) CharBuffer.wrap, slice, position, slice leads to CharBuffer with incorrect offser
alanb
parents: 5506
diff changeset
    88
            if (!nextSlice.equals(slice))
608d2708ab12 7000913: (bf) CharBuffer.wrap, slice, position, slice leads to CharBuffer with incorrect offser
alanb
parents: 5506
diff changeset
    89
                throw new RuntimeException("New buffer should be equal");
608d2708ab12 7000913: (bf) CharBuffer.wrap, slice, position, slice leads to CharBuffer with incorrect offser
alanb
parents: 5506
diff changeset
    90
            slice = nextSlice;
608d2708ab12 7000913: (bf) CharBuffer.wrap, slice, position, slice leads to CharBuffer with incorrect offser
alanb
parents: 5506
diff changeset
    91
        }
608d2708ab12 7000913: (bf) CharBuffer.wrap, slice, position, slice leads to CharBuffer with incorrect offser
alanb
parents: 5506
diff changeset
    92
608d2708ab12 7000913: (bf) CharBuffer.wrap, slice, position, slice leads to CharBuffer with incorrect offser
alanb
parents: 5506
diff changeset
    93
        System.out.println(
69
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
    94
          ">>> StringCharBufferSliceTest-main: testing toString.");
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
    95
        buff.position(4);
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
    96
        buff.limit(7);
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
    97
        slice = buff.slice();
7275
608d2708ab12 7000913: (bf) CharBuffer.wrap, slice, position, slice leads to CharBuffer with incorrect offser
alanb
parents: 5506
diff changeset
    98
        if (!slice.toString().equals("tes")) {
69
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
    99
            throw new RuntimeException("bad toString() after slice(): " + slice.toString());
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
   100
        }
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
   101
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
   102
        System.out.println(
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
   103
          ">>> StringCharBufferSliceTest-main: testing subSequence.");
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
   104
        buff.position(4);
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
   105
        buff.limit(8);
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
   106
        slice = buff.slice();
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
   107
        CharSequence subSeq = slice.subSequence(1, 3);
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
   108
        if (subSeq.charAt(0) != 'e' || subSeq.charAt(1) != 's') {
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
   109
            throw new RuntimeException("bad subSequence() after slice(): '" + subSeq + "'");
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
   110
        }
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
   111
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
   112
        System.out.println(
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
   113
          ">>> StringCharBufferSliceTest-main: testing duplicate.");
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
   114
        buff.position(4);
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
   115
        buff.limit(8);
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
   116
        slice = buff.slice();
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
   117
        CharBuffer dupe = slice.duplicate();
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
   118
        if (dupe.charAt(0) != 't' || dupe.charAt(1) != 'e'
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
   119
            || dupe.charAt(2) != 's' || dupe.charAt(3) != 't') {
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
   120
            throw new RuntimeException("bad duplicate() after slice(): '" + dupe + "'");
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
   121
        }
7275
608d2708ab12 7000913: (bf) CharBuffer.wrap, slice, position, slice leads to CharBuffer with incorrect offser
alanb
parents: 5506
diff changeset
   122
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        System.out.println(">>> StringCharBufferSliceTest-main: done!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public static void test(CharBuffer buff, CharBuffer slice) throws RuntimeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        boolean marked = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            slice.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            marked = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        } catch (InvalidMarkException ime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            // expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (marked ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            slice.position() != 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            buff.remaining() != slice.limit() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            buff.remaining() != slice.capacity()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                 "Calling the CharBuffer.slice method failed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
}