Пример #1
0
        public static void test(int level, ByteBufferN b, bool direct)
        {
            show(level, b);

            //TODO if (direct != b.isDirect())
            //TODO     fail("Wrong direction", b);

            // Gets and puts

            relPut(b);
            relGet(b);
            absGet(b);
            bulkGet(b);

            absPut(b);
            relGet(b);
            absGet(b);
            bulkGet(b);

            bulkPutArray(b);
            relGet(b);

            bulkPutBuffer(b);
            relGet(b);

            // Compact

            relPut(b);
            b.position(13);
            b.compact();
            b.flip();
            relGet(b, 13);

            // Exceptions

            relPut(b);
            b.limit(b.capacity() / 2);
            b.position(b.limit());

            tryCatch(b, typeof(BufferUnderflowException), () => { b.get(); });

            tryCatch(b, typeof(BufferOverflowException), () => { b.put(42); });

            // The index must be non-negative and lesss than the buffer's limit.
            tryCatch(b, typeof(IndexOutOfBoundsException), () => { b.get(b.limit()); });
            tryCatch(b, typeof(IndexOutOfBoundsException), () => { b.get(-1); });

            tryCatch(b, typeof(IndexOutOfBoundsException), () => { b.put(b.limit(), 42); });

            tryCatch(b, typeof(InvalidMarkException), () =>
            {
                b.position(0);
                b.mark();
                b.compact();
                b.reset();
            });

            // Values

            b.clear();
            b.put(0);
            b.put(0xff);
            b.put(1);
            b.put(Byte.MAX_VALUE);
            b.put(Byte.MIN_VALUE);

            b.flip();
            ck(b, b.get(), 0);
            ck(b, b.get(), 0xff);
            ck(b, b.get(), 1);
            ck(b, b.get(), Byte.MAX_VALUE);
            ck(b, b.get(), Byte.MIN_VALUE);

            // Comparison
            b.rewind();
            ByteBufferN b2 = ByteBufferN.allocate(b.capacity());
            b2.put(b);
            b2.flip();
            b.position(2);
            b2.position(2);
            if (!b.equals(b2))
            {
                for (int i = 2; i < b.limit(); i++)
                {
                    byte x = b.get(i);
                    byte y = b2.get(i);
                    if (x != y
                        )
                        outt.println("[" + i + "] " + x + " != " + y);
                }
                fail("Identical buffers not equal", b, b2);
            }
            if (b.compareTo(b2) != 0)
                fail("Comparison to identical buffer != 0", b, b2);

            b.limit(b.limit() + 1);
            b.position(b.limit() - 1);
            b.put(99);
            b.rewind();
            b2.rewind();
            if (b.equals(b2))
                fail("Non-identical buffers equal", b, b2);
            if (b.compareTo(b2) <= 0)
                fail("Comparison to shorter buffer <= 0", b, b2);
            b.limit(b.limit() - 1);

            b.put(2, 42);
            if (b.equals(b2))
                fail("Non-identical buffers equal", b, b2);
            if (b.compareTo(b2) <= 0)
                fail("Comparison to lesser buffer <= 0", b, b2);

            // Sub, dup

            relPut(b);
            relGet(b.duplicate());
            b.position(13);
            relGet(b.duplicate(), 13);
            relGet(b.duplicate().slice(), 13);
            relGet(b.slice(), 13);
            relGet(b.slice().duplicate(), 13);

            // Slice

            b.position(5);
            ByteBufferN sb = b.slice();
            checkSlice(b, sb);
            b.position(0);
            ByteBufferN sb2 = sb.slice();
            checkSlice(sb, sb2);

            if (!sb.equals(sb2))
                fail("Sliced slices do not match", sb, sb2);
            if ((sb.hasArray()) && (sb.arrayOffset() != sb2.arrayOffset()))
                fail("Array offsets do not match: "
                     + sb.arrayOffset() + " != " + sb2.arrayOffset(), sb, sb2);

            // Views

            b.clear();
            b.order(ByteOrder.BIG_ENDIAN);
            testViews(level + 1, b, direct);

            for (int i = 1; i <= 9; i++)
            {
                b.position(i);
                show(level + 1, b);
                testViews(level + 2, b, direct);
            }

            b.position(0);
            b.order(ByteOrder.LITTLE_ENDIAN);
            testViews(level + 1, b, direct);

            // Heterogeneous accessors

            b.order(ByteOrder.BIG_ENDIAN);
            for (int i = 0; i <= 9; i++)
            {
                b.position(i);
                testHet(level + 1, b);
            }
            b.order(ByteOrder.LITTLE_ENDIAN);
            b.position(3);
            testHet(level + 1, b);

            // Read-only views
            /* TODO
            b.rewind();
            ByteBuffer rb = b.asReadOnlyBuffer();
            if (!b.equals(rb))
                fail("BufferN not equal to read-only view", b, rb);
            show(level + 1, rb);

            tryCatch(b, typeof(ReadOnlyBufferException), () => { relPut(rb); });

            tryCatch(b, typeof(ReadOnlyBufferException), () => { absPut(rb); });

            tryCatch(b, typeof(ReadOnlyBufferException), () => { bulkPutArray(rb); });

            tryCatch(b, typeof(ReadOnlyBufferException), () => { bulkPutBuffer(rb); });

            tryCatch(b, typeof(ReadOnlyBufferException), () => { rb.compact(); });

            tryCatch(b, typeof(ReadOnlyBufferException), () => { rb.putChar((char)1); });
            tryCatch(b, typeof(ReadOnlyBufferException), () => { rb.putChar(0, (char)1); });

            tryCatch(b, typeof(ReadOnlyBufferException), () => { rb.putShort(1); });
            tryCatch(b, typeof(ReadOnlyBufferException), () => { rb.putShort(0, 1); });

            tryCatch(b, typeof(ReadOnlyBufferException), () => { rb.putInt(1); });
            tryCatch(b, typeof(ReadOnlyBufferException), () => { rb.putInt(0, 1); });

            tryCatch(b, typeof(ReadOnlyBufferException), () => { rb.putLong(1); });
            tryCatch(b, typeof(ReadOnlyBufferException), () => { rb.putLong(0, 1); });

            tryCatch(b, typeof(ReadOnlyBufferException), () => { rb.putFloat(1); });
            tryCatch(b, typeof(ReadOnlyBufferException), () => { rb.putFloat(0, 1); });

            tryCatch(b, typeof(ReadOnlyBufferException), () => { rb.putDouble(1); });
            tryCatch(b, typeof(ReadOnlyBufferException), () => { rb.putDouble(0, 1); });

            // Bulk puts from read-only buffers

            b.clear();
            rb.rewind();
            b.put(rb);

            // For byte buffers, test both the direct and non-direct cases
            ByteBuffer ob
                = (b.isDirect()
                       ? ByteBuffer.allocate(rb.capacity())
                       : ByteBuffer.allocateDirect(rb.capacity()));
            rb.rewind();
            ob.put(rb);

             */
            relPut(b); // Required by testViews
        }
Пример #2
0
        private static void testHet(int level, ByteBufferN b)
        {
            int p = b.position();
            b.limit(b.capacity());
            show(level, b);
            outt.print("    put:");

            b.putChar((char)1);
            b.putChar(Character.MAX_VALUE);
            outt.print(" char");

            b.putShort(1);
            b.putShort(Short.MAX_VALUE);
            outt.print(" short");

            b.putInt(1);
            b.putInt(Integer.MAX_VALUE);
            outt.print(" int");

            b.putLong(1);
            b.putLong(Long.MAX_VALUE);
            outt.print(" long");

            b.putFloat(1);
            b.putFloat(Float.MIN_VALUE);
            b.putFloat(Float.MAX_VALUE);
            outt.print(" float");

            b.putDouble(1);
            b.putDouble(Double.MIN_VALUE);
            b.putDouble(Double.MAX_VALUE);
            outt.print(" double");

            outt.println();
            b.limit(b.position());
            b.position(p);
            show(level, b);
            outt.print("    get:");

            ck(b, b.getChar(), 1);
            ck(b, b.getChar(), Character.MAX_VALUE);
            outt.print(" char");

            ck(b, b.getShort(), 1);
            ck(b, b.getShort(), Short.MAX_VALUE);
            outt.print(" short");

            ck(b, b.getInt(), 1);
            ck(b, b.getInt(), Integer.MAX_VALUE);
            outt.print(" int");

            ck(b, b.getLong(), 1);
            ck(b, b.getLong(), Long.MAX_VALUE);
            outt.print(" long");

            ck(b, (long)b.getFloat(), 1);
            ck(b, (long)b.getFloat(), (long)Float.MIN_VALUE);
            ck(b, (long)b.getFloat(), (long)Float.MAX_VALUE);
            outt.print(" float");

            ck(b, (long)b.getDouble(), 1);
            ck(b, (long)b.getDouble(), (long)Double.MIN_VALUE);
            ck(b, (long)b.getDouble(), (long)Double.MAX_VALUE);
            outt.print(" double");

            outt.println();
        }
Пример #3
0
        private static void testViews(int level, ByteBufferN b, bool direct)
        {
            /*
            ShortBuffer sb = b.asShortBuffer();
            BasicShort.test(level, sb, direct);
            checkBytes(b, new byte[] { 0, (byte)ic(0) });
            checkInvalidMarkException(sb);

            CharBuffer cb = b.asCharBuffer();
            BasicChar.test(level, cb, direct);
            checkBytes(b, new byte[] { 0, (byte)ic(0) });
            checkInvalidMarkException(cb);

            IntBuffer ib = b.asIntBuffer();
            BasicInt.test(level, ib, direct);
            checkBytes(b, new byte[] { 0, 0, 0, (byte)ic(0) });
            checkInvalidMarkException(ib);

            LongBuffer lb = b.asLongBuffer();
            BasicLong.test(level, lb, direct);
            checkBytes(b, new byte[] { 0, 0, 0, 0, 0, 0, 0, (byte)ic(0) });
            checkInvalidMarkException(lb);

            FloatBuffer fb = b.asFloatBuffer();
            BasicFloat.test(level, fb, direct);
            checkBytes(b, new byte[] { 0x42, (byte)0xc2, 0, 0 });
            checkInvalidMarkException(fb);

            DoubleBuffer db = b.asDoubleBuffer();
            BasicDouble.test(level, db, direct);
            checkBytes(b, new byte[] { 0x40, 0x58, 0x40, 0, 0, 0, 0, 0 });
            checkInvalidMarkException(db);*/
        }
Пример #4
0
 private static void relGet(ByteBufferN b, int start)
 {
     int n = b.remaining();
     for (int i = start; i < n; i++)
         ck(b, b.get(), ((byte)ic(i)));
     b.rewind();
 }
Пример #5
0
 private static void relPut(ByteBufferN b)
 {
     int n = b.capacity();
     b.clear();
     for (int i = 0; i < n; i++)
         b.put((byte)ic(i));
     b.flip();
 }
Пример #6
0
 private static void checkSlice(ByteBufferN b, ByteBufferN slice)
 {
     ck(slice, 0, slice.position());
     ck(slice, b.remaining(), slice.limit());
     ck(slice, b.remaining(), slice.capacity());
     if (b.isDirect() != slice.isDirect())
         fail("Lost direction", slice);
     if (b.isReadOnly() != slice.isReadOnly())
         fail("Lost read-only", slice);
 }
Пример #7
0
 private static void relGet(ByteBufferN b)
 {
     int n = b.capacity();
     for (int i = 0; i < n; i++)
         ck(b, b.get(), ((byte)ic(i)));
     b.rewind();
 }
Пример #8
0
        //6231529
        private static void callReset(ByteBufferN b)
        {
            b.position(0);
            b.mark();

            b.duplicate().reset();
            //TODO b.asReadOnlyBuffer().reset();
        }
Пример #9
0
 private static void checkBytes(ByteBufferN b, byte[] bs)
 {
     int n = bs.Length;
     int p = b.position();
     if (b.order() == ByteOrder.BIG_ENDIAN)
     {
         for (int i = 0; i < n; i++)
             ck(b, b.get(), bs[i]);
     }
     else
     {
         for (int i = n - 1; i >= 0; i--)
             ck(b, b.get(), bs[i]);
     }
     b.position(p);
 }
Пример #10
0
 private static void bulkPutArray(ByteBufferN b)
 {
     int n = b.capacity();
     b.clear();
     var a = new byte[n + 7];
     for (int i = 0; i < n; i++)
         a[i + 7] = (byte)ic(i);
     b.put(a, 7, n);
     b.flip();
 }
Пример #11
0
 private static void bulkPutBuffer(ByteBufferN b)
 {
     int n = b.capacity();
     b.clear();
     ByteBufferN c = ByteBufferN.allocate(n + 7);
     c.position(7);
     for (int i = 0; i < n; i++)
         c.put((byte)ic(i));
     c.flip();
     c.position(7);
     b.put(c);
     b.flip();
 }
Пример #12
0
 private static void bulkGet(ByteBufferN b)
 {
     int n = b.capacity();
     var a = new byte[n + 7];
     b.get(a, 7, n);
     for (int i = 0; i < n; i++)
         ck(b, a[i + 7], ((byte)ic(i)));
 }
Пример #13
0
 private static void absPut(ByteBufferN b)
 {
     int n = b.capacity();
     b.clear();
     for (int i = 0; i < n; i++)
         b.put(i, (byte)ic(i));
     b.limit(n);
     b.position(0);
 }
Пример #14
0
 public override ByteBufferN put(ByteBufferN src)
 {
     if (src is HeapByteBufferN)
     {
         if (src == this)
             throw new IllegalArgumentException();
         var sb = (HeapByteBufferN) src;
         int n = sb.remaining();
         if (n > remaining())
             throw new BufferOverflowException();
         Array.Copy(sb.hb, sb.ix(sb.position()),
                    hb, ix(position()), n);
         sb.position(sb.position() + n);
         position(position() + n);
     }
     else if (src.isDirect())
     {
         int n = src.remaining();
         if (n > remaining())
             throw new BufferOverflowException();
         src.get(hb, ix(position()), n);
         position(position() + n);
     }
     else
     {
         base.put(src);
     }
     return this;
 }
Пример #15
0
 /**
  * Compares this buffer to another.
  *
  * <p> Two byte buffers are compared by comparing their sequences of
  * remaining elements lexicographically, without regard to the starting
  * position of each sequence within its corresponding buffer.
  *
  * <p> A byte buffer is not comparable to any other type of object.
  *
  * @return  A negative integer, zero, or a positive integer as this buffer
  *		is less than, equal to, or greater than the given buffer
  */
 public int compareTo(ByteBufferN that)
 {
     int n = position() + Math.Min(remaining(), that.remaining());
     for (int i = position(), j = that.position(); i < n; i++, j++)
     {
         byte v1 = get(i);
         byte v2 = that.get(j);
         if (v1 == v2)
             continue;
         if (v1 < v2)
             return -1;
         return +1;
     }
     return remaining() - that.remaining();
 }
Пример #16
0
 // -- Bulk put operations --
 /**
  * Relative bulk <i>put</i> method&nbsp;&nbsp;<i>(optional operation)</i>.
  *
  * <p> This method transfers the bytes remaining in the given source
  * buffer into this buffer.  If there are more bytes remaining in the
  * source buffer than in this buffer, that is, if
  * <tt>src.remaining()</tt>&nbsp;<tt>&gt;</tt>&nbsp;<tt>remaining()</tt>,
  * then no bytes are transferred and a {@link
  * BufferOverflowException} is thrown.
  *
  * <p> Otherwise, this method copies
  * <i>n</i>&nbsp;=&nbsp;<tt>src.remaining()</tt> bytes from the given
  * buffer into this buffer, starting at each buffer's current position.
  * The positions of both buffers are then incremented by <i>n</i>.
  *
  * <p> In other words, an invocation of this method of the form
  * <tt>dst.put(src)</tt> has exactly the same effect as the loop
  *
  * <pre>
  *     while (src.hasRemaining())
  *         dst.put(src.get()); </pre>
  *
  * except that it first checks that there is sufficient space in this
  * buffer and it is potentially much more efficient. </p>
  *
  * @param  src
  *         The source buffer from which bytes are to be read;
  *         must not be this buffer
  *
  * @return  This buffer
  *
  * @throws  BufferOverflowException
  *          If there is insufficient space in this buffer
  *          for the remaining bytes in the source buffer
  *
  * @throws  IllegalArgumentException
  *          If the source buffer is this buffer
  *
  * @throws  ReadOnlyBufferException
  *          If this buffer is read-only
  */
 public virtual ByteBufferN put(ByteBufferN src)
 {
     if (src == this)
         throw new IllegalArgumentException();
     int n = src.remaining();
     if (n > remaining())
         throw new BufferOverflowException();
     for (int i = 0; i < n; i++)
         put(src.get());
     return this;
 }