public virtual void TestSlice()
        {
            assertTrue(buf.Capacity > 5);
            buf.Position = (1);
            buf.Limit    = (buf.Capacity - 1);

            Int16Buffer slice = buf.Slice();

            assertEquals(buf.IsReadOnly, slice.IsReadOnly);
            //assertEquals(buf.IsDirect, slice.IsDirect); // J2N: IsDirect not supported
            assertEquals(buf.Order, slice.Order);
            assertEquals(slice.Position, 0);
            assertEquals(slice.Limit, buf.Remaining);
            assertEquals(slice.Capacity, buf.Remaining);
            try
            {
                slice.Reset();
                fail("Should throw Exception"); //$NON-NLS-1$
            }
            catch (InvalidMarkException e)
            {
                // expected
            }

            // slice share the same content with buf
            if (!slice.IsReadOnly)
            {
                loadTestData1(slice);
                assertContentLikeTestData1(buf, 1, (short)0, slice.Capacity);
                buf.Put(2, (short)500);
                assertEquals(slice.Get(1), 500);
            }
        }
 void assertContentEquals(Int16Buffer buf, Int16Buffer other)
 {
     assertEquals(buf.Capacity, other.Capacity);
     for (int i = 0; i < buf.Capacity; i++)
     {
         assertEquals(buf.Get(i), other.Get(i));
     }
 }
 void assertContentEquals(Int16Buffer buf, short[] array,
                          int offset, int length)
 {
     for (int i = 0; i < length; i++)
     {
         assertEquals(buf.Get(i), array[offset + i]);
     }
 }
        void assertContentLikeTestData1(Int16Buffer buf,
                                        int startIndex, short startValue, int length)
        {
            short value = startValue;

            for (int i = 0; i < length; i++)
            {
                assertEquals(buf.Get(startIndex + i), value);
                value = (short)(value + 1);
            }
        }
Пример #5
0
        private static void bulkGet(Int16Buffer b)
        {
            int n = b.Capacity;

            short[] a = new short[n + 7];
            b.Get(a, 7, n);
            for (int i = 0; i < n; i++)
            {
                ck(b, (long)a[i + 7], (long)((short)Ic(i)));
            }
        }
Пример #6
0
        private static void absGet(Int16Buffer b)
        {
            int   n = b.Capacity;
            short v;

            for (int i = 0; i < n; i++)
            {
                ck(b, (long)b.Get(), (long)((short)Ic(i)));
            }
            b.Rewind();
        }
Пример #7
0
        private static void relGet(Int16Buffer b, int start)
        {
            int   n = b.Remaining;
            short v;

            for (int i = start; i < n; i++)
            {
                ck(b, (long)b.Get(), (long)((short)Ic(i)));
            }
            b.Rewind();
        }
 public virtual void TestGet()
 {
     buf.Clear();
     for (int i = 0; i < buf.Capacity; i++)
     {
         assertEquals(buf.Position, i);
         assertEquals(buf.Get(), buf.Get(i));
     }
     try
     {
         buf.Get();
         fail("Should throw Exception"); //$NON-NLS-1$
     }
     catch (BufferUnderflowException e)
     {
         // expected
     }
 }
Пример #9
0
        public static void test(int level, Int16Buffer b, bool direct)
        {
            Show(level, b);

            //if (direct != b.IsDirect) // J2N: IsDirect not supported
            //    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((short)42);
            });

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

            tryCatch(b, typeof(ArgumentOutOfRangeException), () =>
            {
                b.Put(b.Limit, (short)42);
            });

            tryCatch(b, typeof(InvalidMarkException), () =>
            {
                b.Position = (0);
                b.Mark();
                b.Compact();
                b.Reset();
            });

            // Values

            b.Clear();
            b.Put((short)0);
            b.Put((short)-1);
            b.Put((short)1);
            b.Put(short.MaxValue);
            b.Put(short.MinValue);

            short v;

            b.Flip();
            ck(b, b.Get(), 0);
            ck(b, b.Get(), (short)-1);
            ck(b, b.Get(), 1);
            ck(b, b.Get(), short.MaxValue);
            ck(b, b.Get(), short.MinValue);


            // Comparison
            b.Rewind();
            Int16Buffer b2 = Int16Buffer.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++)
                {
                    short x = b.Get(i);
                    short y = b2.Get(i);
                    if (x != y)
                    {
                        output.WriteLine("[" + 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((short)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, (short)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);
            }

            // Check equals and compareTo with interesting values
            foreach (short x in VALUES)
            {
                Int16Buffer xb = Int16Buffer.Wrap(new short[] { x });
                if (xb.CompareTo(xb) != 0)
                {
                    fail("compareTo not reflexive", xb, xb, x, x);
                }
                if (!xb.Equals(xb))
                {
                    fail("equals not reflexive", xb, xb, x, x);
                }
                foreach (short y in VALUES)
                {
                    Int16Buffer yb = Int16Buffer.Wrap(new short[] { y });
                    if (xb.CompareTo(yb) != -yb.CompareTo(xb))
                    {
                        fail("compareTo not anti-symmetric",
                             xb, yb, x, y);
                    }
                    if ((xb.CompareTo(yb) == 0) != xb.Equals(yb))
                    {
                        fail("compareTo inconsistent with equals",
                             xb, yb, x, y);
                    }
                    if (xb.CompareTo(yb) != x.CompareTo(y))
                    {
                        fail("Incorrect results for Int16Buffer.compareTo",
                             xb, yb, x, y);
                    }
                    if (xb.Equals(yb) != ((x == y) /*|| ((x != x) && (y != y))*/))
                    {
                        fail("Incorrect results for Int16Buffer.equals",
                             xb, yb, x, y);
                    }
                }
            }

            // 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);
            Int16Buffer sb = b.Slice();

            checkSlice(b, sb);
            b.Position = (0);
            Int16Buffer 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);
            }

            // Read-only views

            b.Rewind();
            Int16Buffer rb = b.AsReadOnlyBuffer();

            if (!b.Equals(rb))
            {
                fail("Buffer 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();
            });

            if (rb.GetType().Name.StartsWith("Heap", StringComparison.Ordinal))
            {
                tryCatch(b, typeof(ReadOnlyBufferException), () =>
                {
                    var _ = rb.Array;
                });

                tryCatch(b, typeof(ReadOnlyBufferException), () =>
                {
                    var _ = rb.ArrayOffset;
                });

                if (rb.HasArray)
                {
                    fail("Read-only heap buffer's backing array is accessible",
                         rb);
                }
            }

            // Bulk puts from read-only buffers

            b.Clear();
            rb.Rewind();
            b.Put(rb);
            relPut(b);                       // Required by testViews
        }