jdk/test/java/nio/Buffer/Basic.java
author xdono
Thu, 19 Mar 2009 13:25:23 -0700 (2009-03-19)
changeset 2228 0d989c04422c
parent 1639 a97859015238
child 2593 76032557be03
permissions -rw-r--r--
Added tag jdk7-b51 for changeset c5c3e5f4accc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
1639
a97859015238 6785258: Update copyright year
xdono
parents: 1634
diff changeset
     2
 * Copyright 2000-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
 * @summary Unit test for buffers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4413135 4414911 4416536 4416562 4418782 4471053 4472779 4490253 4523725
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *      4526177 4463011 4660660 4661219 4663521 4782970 4804304 4938424 6231529
1634
3871c2046043 6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents: 2
diff changeset
    28
 *      6221101 6234263 6535542 6591971 6593946
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @author Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.PrintStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.nio.Buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.nio.CharBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
public class Basic {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    static PrintStream out = System.err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    static long ic(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        int j = i % 54;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        return j + 'a' + ((j > 26) ? 128 : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    static String toString(Buffer b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        return (b.getClass().getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                + "[pos=" + b.position()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                + " lim=" + b.limit()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                + " cap=" + b.capacity()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    static void show(int level, Buffer b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        for (int i = 0; i < level; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            out.print("  ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        out.println(toString(b) + " " + Integer.toHexString(b.hashCode()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    static void fail(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        throw new RuntimeException(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    static void fail(String s, Buffer b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        throw new RuntimeException(s + ": " + toString(b));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    static void fail(String s, Buffer b, Buffer b2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        throw new RuntimeException(s + ": "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                                   + toString(b) + ", " + toString(b2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    static void fail(Buffer b,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                     String expected, char expectedChar,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                     String got, char gotChar)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        if (b instanceof ByteBuffer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            ByteBuffer bb = (ByteBuffer)b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            int n = Math.min(16, bb.limit());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            for (int i = 0; i < n; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                out.print(" " + Integer.toHexString(bb.get(i) & 0xff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        if (b instanceof CharBuffer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            CharBuffer bb = (CharBuffer)b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            int n = Math.min(16, bb.limit());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            for (int i = 0; i < n; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                out.print(" " + Integer.toHexString(bb.get(i) & 0xffff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        throw new RuntimeException(toString(b)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                                   + ": Expected '" + expectedChar + "'=0x"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                                   + expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                                   + ", got '" + gotChar + "'=0x"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                                   + got);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    static void fail(Buffer b, long expected, long got) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        fail(b,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
             Long.toHexString(expected), (char)expected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
             Long.toHexString(got), (char)got);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    static void ck(Buffer b, boolean cond) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        if (!cond)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            fail("Condition failed", b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    static void ck(Buffer b, long got, long expected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        if (expected != got)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            fail(b, expected, got);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    static void ck(Buffer b, float got, float expected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        if (expected != got)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            fail(b,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                 Float.toString(expected), (char)expected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                 Float.toString(got), (char)got);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    static void ck(Buffer b, double got, double expected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (expected != got)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            fail(b,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                 Double.toString(expected), (char)expected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                 Double.toString(got), (char)got);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        BasicByte.test();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        BasicChar.test();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        BasicShort.test();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        BasicInt.test();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        BasicLong.test();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        BasicFloat.test();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        BasicDouble.test();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
}