1 /*
   2  * Copyright 2000-2008 Sun Microsystems, Inc.  All Rights Reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  */
  23 
  24 /* @test
  25  * @summary Unit test for buffers
  26  * @bug 4413135 4414911 4416536 4416562 4418782 4471053 4472779 4490253 4523725
  27  *      4526177 4463011 4660660 4661219 4663521 4782970 4804304 4938424 6231529
  28  *      6221101 6234263 6535542 6591971 6593946 6795561
  29  * @author Mark Reinhold
  30  */
  31 
  32 
  33 import java.io.PrintStream;
  34 
  35 import java.nio.Buffer;
  36 import java.nio.ByteBuffer;
  37 import java.nio.CharBuffer;
  38 
  39 
  40 public class Basic {
  41 
  42     static PrintStream out = System.err;
  43 
  44     static long ic(int i) {
  45         int j = i % 54;
  46         return j + 'a' + ((j > 26) ? 128 : 0);
  47     }
  48 
  49     static String toString(Buffer b) {
  50         return (b.getClass().getName()
  51                 + "[pos=" + b.position()
  52                 + " lim=" + b.limit()
  53                 + " cap=" + b.capacity()
  54                 + "]");
  55     }
  56 
  57     static void show(int level, Buffer b) {
  58         for (int i = 0; i < level; i++)
  59             out.print("  ");
  60         out.println(toString(b) + " " + Integer.toHexString(b.hashCode()));
  61     }
  62 
  63     static void fail(String s) {
  64         throw new RuntimeException(s);
  65     }
  66 
  67     static void fail(String s, Buffer b) {
  68         throw new RuntimeException(s + ": " + toString(b));
  69     }
  70 
  71     static void fail(String s, Buffer b, Buffer b2) {
  72         throw new RuntimeException(s + ": "
  73                                    + toString(b) + ", " + toString(b2));
  74     }
  75 
  76     static void fail(Buffer b,
  77                      String expected, char expectedChar,
  78                      String got, char gotChar)
  79     {
  80         if (b instanceof ByteBuffer) {
  81             ByteBuffer bb = (ByteBuffer)b;
  82             int n = Math.min(16, bb.limit());
  83             for (int i = 0; i < n; i++)
  84                 out.print(" " + Integer.toHexString(bb.get(i) & 0xff));
  85             out.println();
  86         }
  87         if (b instanceof CharBuffer) {
  88             CharBuffer bb = (CharBuffer)b;
  89             int n = Math.min(16, bb.limit());
  90             for (int i = 0; i < n; i++)
  91                 out.print(" " + Integer.toHexString(bb.get(i) & 0xffff));
  92             out.println();
  93         }
  94         throw new RuntimeException(toString(b)
  95                                    + ": Expected '" + expectedChar + "'=0x"
  96                                    + expected
  97                                    + ", got '" + gotChar + "'=0x"
  98                                    + got);
  99     }
 100 
 101     static void fail(Buffer b, long expected, long got) {
 102         fail(b,
 103              Long.toHexString(expected), (char)expected,
 104              Long.toHexString(got), (char)got);
 105     }
 106 
 107     static void ck(Buffer b, boolean cond) {
 108         if (!cond)
 109             fail("Condition failed", b);
 110     }
 111 
 112     static void ck(Buffer b, long got, long expected) {
 113         if (expected != got)
 114             fail(b, expected, got);
 115     }
 116 
 117     static void ck(Buffer b, float got, float expected) {
 118         if (expected != got)
 119             fail(b,
 120                  Float.toString(expected), (char)expected,
 121                  Float.toString(got), (char)got);
 122     }
 123 
 124     static void ck(Buffer b, double got, double expected) {
 125         if (expected != got)
 126             fail(b,
 127                  Double.toString(expected), (char)expected,
 128                  Double.toString(got), (char)got);
 129     }
 130 
 131     public static void main(String[] args) {
 132         BasicByte.test();
 133         BasicChar.test();
 134         BasicShort.test();
 135         BasicInt.test();
 136         BasicLong.test();
 137         BasicFloat.test();
 138         BasicDouble.test();
 139     }
 140 
 141 }