tests/libjava-mauve/src/gnu/testlet/java/nio/ByteBuffer/Allocating.java
branchjk_new_structure
changeset 1541 75c2e24dea9a
parent 1540 92ac284961c1
child 1542 be11db817bcf
equal deleted inserted replaced
1540:92ac284961c1 1541:75c2e24dea9a
     1 // Tags: JDK1.4
       
     2 // Uses: ByteBufferFactory
       
     3 
       
     4 // Copyright (C) 2004 Max Gilead <gilead@yellowhedgehog.com>
       
     5 
       
     6 // This file is part of Mauve.
       
     7 
       
     8 // Mauve is free software; you can redistribute it and/or modify
       
     9 // it under the terms of the GNU General Public License as published by
       
    10 // the Free Software Foundation; either version 2, or (at your option)
       
    11 // any later version.
       
    12 
       
    13 // Mauve is distributed in the hope that it will be useful,
       
    14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    16 // GNU General Public License for more details.
       
    17 
       
    18 // You should have received a copy of the GNU General Public License
       
    19 // along with Mauve; see the file COPYING.  If not, write to
       
    20 // the Free Software Foundation, 59 Temple Place - Suite 330,
       
    21 // Boston, MA 02111-1307, USA.
       
    22 
       
    23 package gnu.testlet.java.nio.ByteBuffer;
       
    24 
       
    25 import gnu.testlet.Testlet;
       
    26 import gnu.testlet.TestHarness;
       
    27 
       
    28 import java.nio.BufferOverflowException;
       
    29 import java.nio.BufferUnderflowException;
       
    30 import java.nio.ByteBuffer;
       
    31 import java.nio.ByteOrder;
       
    32 import java.nio.InvalidMarkException;
       
    33 
       
    34 public class Allocating implements Testlet
       
    35 {
       
    36   public void test(TestHarness h)
       
    37   {
       
    38   	//
       
    39   	// allocate(int)
       
    40   	//
       
    41     h.checkPoint("allocate(int)");
       
    42   	h.check(true);
       
    43     ByteBufferFactory allocateFactory = new ByteBufferFactory()
       
    44     {
       
    45       public ByteBuffer newInstance()
       
    46       {
       
    47         return ByteBuffer.allocate(10);
       
    48       }
       
    49     };
       
    50     ByteBuffer bufAll = ByteBuffer.allocate(10);
       
    51     h.check(bufAll.isDirect(), false, "isDirect()");
       
    52     h.check(bufAll.hasArray(), "hasArray()");
       
    53     h.check(bufAll.arrayOffset(), 0, "arrayOffset()");
       
    54     h.check(bufAll.array() != null, "array()");
       
    55 
       
    56     overflow(h, allocateFactory, 10);
       
    57     underflow(h, allocateFactory, 10);
       
    58     compact(h, allocateFactory, 10);
       
    59 
       
    60   	//
       
    61   	// allocateDirect(int)
       
    62   	//
       
    63   	h.checkPoint("allocateDirect(int)");
       
    64   	h.check(true);
       
    65   	ByteBufferFactory allocateDirectFactory = new ByteBufferFactory()
       
    66 	{
       
    67   	  public ByteBuffer newInstance()
       
    68   	  {
       
    69   	  	return ByteBuffer.allocateDirect(10);
       
    70   	  }
       
    71 	};
       
    72     ByteBuffer bufAllDir = ByteBuffer.allocateDirect(10);
       
    73     h.check(bufAllDir.isDirect(), true, "isDirect()");
       
    74     // it's unspecified if this buffer will have backing array so we test it if there's one
       
    75     if (bufAllDir.hasArray())
       
    76     {
       
    77       h.check(bufAllDir.arrayOffset(), 0, "arrayOffset()");
       
    78       h.check(bufAllDir.array() != null, "array()");
       
    79     }
       
    80 
       
    81     overflow(h, allocateDirectFactory, 10);
       
    82     underflow(h, allocateDirectFactory, 10);
       
    83     compact(h, allocateDirectFactory, 10);
       
    84 
       
    85   	//
       
    86   	// wrap(byte[])
       
    87   	//
       
    88   	h.checkPoint("wrap(byte[])");
       
    89   	h.check(true);
       
    90   	ByteBufferFactory wrapFactory = new ByteBufferFactory()
       
    91 	{
       
    92   	  public ByteBuffer newInstance()
       
    93   	  {
       
    94   	  	return ByteBuffer.wrap(new byte[10]);
       
    95   	  }
       
    96 	};
       
    97 	byte[] arrWrap = new byte[10];
       
    98     ByteBuffer bufWrap = ByteBuffer.wrap(arrWrap);
       
    99     h.check(bufWrap.isDirect(), false, "isDirect()");
       
   100     h.check(bufWrap.hasArray(), true, "hasArray()");
       
   101     h.check(bufWrap.arrayOffset(), 0, "arrayOffset()");
       
   102     h.check(bufWrap.array(), arrWrap, "array()");
       
   103 
       
   104     overflow(h, wrapFactory, 10);
       
   105     underflow(h, wrapFactory, 10);
       
   106     compact(h, wrapFactory, 10);
       
   107  
       
   108   	//
       
   109   	// wrap(byte[], int, int)
       
   110   	//
       
   111   	h.checkPoint("wrap(byte[], int, int)");
       
   112   	h.check(true);
       
   113   	ByteBufferFactory wrapWithOffsetFactory = new ByteBufferFactory()
       
   114 	{
       
   115   	  public ByteBuffer newInstance()
       
   116   	  {
       
   117   	  	return ByteBuffer.wrap(new byte[20], 5, 10);
       
   118   	  }
       
   119 	};
       
   120 	byte[] arrWrapOff = new byte[10];
       
   121     ByteBuffer bufWrapOff = ByteBuffer.wrap(arrWrapOff, 1, 1);
       
   122     h.check(bufWrapOff.isDirect(), false, "isDirect()");
       
   123     h.check(bufWrapOff.hasArray(), true, "hasArray()");
       
   124     h.check(bufWrapOff.arrayOffset(), 0, "arrayOffset()");
       
   125     h.check(bufWrapOff.array(), arrWrapOff, "array()");
       
   126 
       
   127     overflow(h, wrapWithOffsetFactory, 15);
       
   128     underflow(h, wrapWithOffsetFactory, 15);
       
   129     compact(h, wrapWithOffsetFactory, 20);
       
   130 
       
   131     array(h);
       
   132     synchWrappedBufferWithArray(h);
       
   133   }
       
   134 
       
   135   private void overflow(TestHarness h, ByteBufferFactory factory, int limit)
       
   136   {
       
   137     ByteBuffer buf = null;
       
   138 
       
   139     buf = factory.newInstance();
       
   140     buf.position(limit - 1);
       
   141     buf.put((byte)0x01);
       
   142     try
       
   143 		{
       
   144     	buf.put((byte)0x01);
       
   145     	h.check(false, "byte overflow");
       
   146 		}
       
   147     	catch(BufferOverflowException boe)
       
   148 			{
       
   149     		h.check(true, "byte overflow");
       
   150 			}
       
   151 
       
   152     buf = factory.newInstance();
       
   153     buf.position(limit - 3);
       
   154     buf.putShort((short)0x0101);
       
   155     try
       
   156 		{
       
   157     	buf.putShort((short)0x0101);
       
   158     	h.check(false, "short overflow");
       
   159 		}
       
   160     	catch(BufferOverflowException boe)
       
   161 			{
       
   162     		h.check(true, "short overflow");
       
   163 			}
       
   164 
       
   165     buf = factory.newInstance();
       
   166     buf.position(limit - 6);
       
   167     buf.putInt(0x01010101);
       
   168     try
       
   169 		{
       
   170     	buf.putInt(0x01010101);
       
   171     	h.check(false, "int overflow");
       
   172 		}
       
   173     	catch(BufferOverflowException boe)
       
   174 			{
       
   175     		h.check(true, "int overflow");
       
   176 			}
       
   177 
       
   178     buf = factory.newInstance();
       
   179     buf.position(limit - 9);
       
   180     buf.putLong(0x0101010101010101L);
       
   181     try
       
   182 		{
       
   183     	buf.putLong(0x0101010101010101L);
       
   184     	h.check(false, "long overflow");
       
   185 		}
       
   186     	catch(BufferOverflowException boe)
       
   187 			{
       
   188     		h.check(true, "long overflow");
       
   189 			}
       
   190 
       
   191     buf = factory.newInstance();
       
   192     buf.position(limit - 6);
       
   193     buf.putFloat(1.0f);
       
   194     try
       
   195 		{
       
   196     	buf.putFloat(1.0f);
       
   197     	h.check(false, "float overflow");
       
   198 		}
       
   199     	catch(BufferOverflowException boe)
       
   200 			{
       
   201     		h.check(true, "float overflow");
       
   202 			}
       
   203 
       
   204     buf = factory.newInstance();
       
   205     buf.position(limit - 9);
       
   206    	buf.putDouble(1.0);
       
   207     try
       
   208 		{
       
   209     	buf.putDouble(1.0);
       
   210     	h.check(false, "double overflow");
       
   211 		}
       
   212     	catch(BufferOverflowException boe)
       
   213 			{
       
   214     		h.check(true, "double overflow");
       
   215 			}
       
   216 
       
   217     buf = factory.newInstance();
       
   218     buf.position(limit - 3);
       
   219     buf.putChar('\u0101');
       
   220     try
       
   221 		{
       
   222     	buf.putChar('\u0101');
       
   223     	h.check(false, "char overflow");
       
   224 		}
       
   225     	catch(BufferOverflowException boe)
       
   226 			{
       
   227     		h.check(true, "char overflow");
       
   228 			}
       
   229   }
       
   230   private void underflow(TestHarness h, ByteBufferFactory factory, int limit)
       
   231   {
       
   232     ByteBuffer buf = null;
       
   233 
       
   234     buf = factory.newInstance();
       
   235     buf.position(limit - 1);
       
   236     buf.get();
       
   237     try
       
   238 		{
       
   239     	buf.get();
       
   240     	h.check(false, "byte underflow");
       
   241 		}
       
   242     	catch(BufferUnderflowException boe)
       
   243 			{
       
   244     		h.check(true, "byte underflow");
       
   245 			}
       
   246 
       
   247     buf = factory.newInstance();
       
   248     buf.position(limit - 3);
       
   249     buf.getShort();
       
   250     try
       
   251 		{
       
   252     	buf.getShort();
       
   253     	h.check(false, "short underflow");
       
   254 		}
       
   255     	catch(BufferUnderflowException boe)
       
   256 			{
       
   257     		h.check(true, "short underflow");
       
   258 			}
       
   259 
       
   260     buf = factory.newInstance();
       
   261     buf.position(limit - 6);
       
   262     buf.getInt();
       
   263     try
       
   264 		{
       
   265     	buf.getInt();
       
   266     	h.check(false, "int underflow");
       
   267 		}
       
   268     	catch(BufferUnderflowException boe)
       
   269 			{
       
   270     		h.check(true, "int underflow");
       
   271 			}
       
   272 
       
   273     buf = factory.newInstance();
       
   274     buf.position(limit - 9);
       
   275     buf.getLong();
       
   276     try
       
   277 		{
       
   278     	buf.getLong();
       
   279     	h.check(false, "long underflow");
       
   280 		}
       
   281     	catch(BufferUnderflowException boe)
       
   282 			{
       
   283     		h.check(true, "long underflow");
       
   284 			}
       
   285 
       
   286     buf = factory.newInstance();
       
   287     buf.position(limit - 6);
       
   288     buf.getFloat();
       
   289     try
       
   290 		{
       
   291     	buf.getFloat();
       
   292     	h.check(false, "float underflow");
       
   293 		}
       
   294     	catch(BufferUnderflowException boe)
       
   295 			{
       
   296     		h.check(true, "float underflow");
       
   297 			}
       
   298 
       
   299     buf = factory.newInstance();
       
   300     buf.position(limit - 9);
       
   301    	buf.getDouble();
       
   302     try
       
   303 		{
       
   304     	buf.getDouble();
       
   305     	h.check(false, "double underflow");
       
   306 		}
       
   307     	catch(BufferUnderflowException boe)
       
   308 			{
       
   309     		h.check(true, "double underflow");
       
   310 			}
       
   311 
       
   312     buf = factory.newInstance();
       
   313     buf.position(limit - 3);
       
   314     buf.getChar();
       
   315     try
       
   316 		{
       
   317     	buf.getChar();
       
   318     	h.check(false, "char underflow");
       
   319 		}
       
   320     	catch(BufferUnderflowException boe)
       
   321 			{
       
   322     		h.check(true, "char underflow");
       
   323 			}
       
   324   }
       
   325 
       
   326   private void compact(TestHarness h, ByteBufferFactory factory, int size)
       
   327   {
       
   328     h.checkPoint("compact()");
       
   329 
       
   330     ByteBuffer buf = null;
       
   331 
       
   332     buf = factory.newInstance();
       
   333     buf.rewind();
       
   334     for (int i = 0; i < 10; i++)
       
   335     {
       
   336       buf.put((byte)(i + 1));
       
   337     }
       
   338     buf.limit(6);
       
   339     buf.position(1);
       
   340     buf.mark();
       
   341     buf.get();
       
   342     
       
   343     h.check(buf.compact(), buf, "compact() return value");
       
   344     h.check(buf.position(), 4, "compact()/position");
       
   345     h.check(buf.limit(), size, "compact()/limit");
       
   346     try
       
   347       {
       
   348         buf.reset();
       
   349         h.check(false, "mark: mark not invalidated");
       
   350       }
       
   351       catch(InvalidMarkException ime)
       
   352       {
       
   353         h.check(true, "mark: invalidated mark");
       
   354       }
       
   355     h.checkPoint("compact()/contents");
       
   356     buf.rewind();
       
   357     h.check(buf.get(), 3);
       
   358     h.check(buf.get(), 4);
       
   359     h.check(buf.get(), 5);
       
   360     h.check(buf.get(), 6);
       
   361   }
       
   362 
       
   363 
       
   364   private void array(TestHarness h)
       
   365   {
       
   366   	byte[] arr = null;
       
   367     ByteBuffer buf = null;
       
   368 
       
   369     h.checkPoint("array");
       
   370     arr = new byte[] { 1, 2, 3 };
       
   371     buf = ByteBuffer.wrap(arr);
       
   372     h.check(buf.array(), arr, "array");
       
   373 
       
   374     
       
   375   }
       
   376 
       
   377   private void synchWrappedBufferWithArray(TestHarness h)
       
   378   {
       
   379   	byte[] arr = null;
       
   380     ByteBuffer buf = null;
       
   381 
       
   382     h.checkPoint("synchWrappedBufferWithArray/wrap(byte[])");
       
   383     arr = new byte[10];
       
   384     buf = ByteBuffer.wrap(arr);
       
   385     for (int i = 0; i < arr.length; i++)
       
   386     {
       
   387       arr[i] = (byte)(i + 1);
       
   388     }
       
   389     buf.order(ByteOrder.BIG_ENDIAN);
       
   390     h.check(buf.getShort(), (short)0x0102);
       
   391     buf.putShort((short)0x0b0c);
       
   392     buf.order(ByteOrder.LITTLE_ENDIAN);
       
   393     h.check(buf.getShort(), (short)0x0605);
       
   394     buf.putShort((short)0x0d0e);
       
   395     h.check(arr[2], 0x0b);
       
   396     h.check(arr[3], 0x0c);
       
   397     h.check(arr[6], 0x0e);
       
   398     h.check(arr[7], 0x0d);
       
   399 
       
   400     h.checkPoint("synchWrappedBufferWithArray/wrap(byte[], int, int)");
       
   401     arr = new byte[10];
       
   402     buf = ByteBuffer.wrap(arr, 2, 8);
       
   403     for (int i = 0; i < arr.length; i++)
       
   404     {
       
   405       arr[i] = (byte)(i + 1);
       
   406     }
       
   407     buf.order(ByteOrder.BIG_ENDIAN);
       
   408     h.check(buf.getShort(), (short)0x0304);
       
   409     buf.putShort((short)0x0b0c);
       
   410     buf.order(ByteOrder.LITTLE_ENDIAN);
       
   411     h.check(buf.getShort(), (short)0x0807);
       
   412     buf.putShort((short)0x0d0e);
       
   413     h.check(arr[4], 0x0b);
       
   414     h.check(arr[5], 0x0c);
       
   415     h.check(arr[8], 0x0e);
       
   416     h.check(arr[9], 0x0d);
       
   417   }
       
   418 }