jdk/test/java/nio/Buffer/StringCharBufferSliceTest.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 69 c7cd7bde95f3
child 5506 202f599c92aa
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 69
diff changeset
     2
 * Copyright 2005-2008 Sun Microsystems, Inc.  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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @bug 4997655
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(
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
    79
          ">>> StringCharBufferSliceTest-main: testing toString.");
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
    80
        buff.position(4);
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
    81
        buff.limit(7);
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
    82
        slice = buff.slice();
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
    83
        if (! slice.toString().equals("tes")) {
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
    84
            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
    85
        }
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
    86
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
    87
        System.out.println(
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
    88
          ">>> StringCharBufferSliceTest-main: testing subSequence.");
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
    89
        buff.position(4);
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
    90
        buff.limit(8);
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
    91
        slice = buff.slice();
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
    92
        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
    93
        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
    94
            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
    95
        }
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
    96
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
    97
        System.out.println(
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
    98
          ">>> StringCharBufferSliceTest-main: testing duplicate.");
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
    99
        buff.position(4);
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
   100
        buff.limit(8);
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
   101
        slice = buff.slice();
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
   102
        CharBuffer dupe = slice.duplicate();
c7cd7bde95f3 6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents: 2
diff changeset
   103
        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
   104
            || 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
   105
            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
   106
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        System.out.println(">>> StringCharBufferSliceTest-main: done!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public static void test(CharBuffer buff, CharBuffer slice) throws RuntimeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        boolean marked = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            slice.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            marked = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        } catch (InvalidMarkException ime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            // expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (marked ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            slice.position() != 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            buff.remaining() != slice.limit() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            buff.remaining() != slice.capacity()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                 "Calling the CharBuffer.slice method failed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
}