public virtual void TestAsReadOnlyBuffer()
        {
            buf.Clear();
            buf.Mark();
            buf.Position = (buf.Limit);

            // readonly's contents should be the same as buf
            Int64Buffer @readonly = buf.AsReadOnlyBuffer();

            assertNotSame(buf, @readonly);
            assertTrue(@readonly.IsReadOnly);
            assertEquals(buf.Position, @readonly.Position);
            assertEquals(buf.Limit, @readonly.Limit);
            //assertEquals(buf.IsDirect, @readonly.IsDirect); // J2N: IsDirect not supported
            assertEquals(buf.Order, @readonly.Order);
            assertContentEquals(buf, @readonly);

            // readonly's position, mark, and limit should be independent to buf
            @readonly.Reset();
            assertEquals(@readonly.Position, 0);
            @readonly.Clear();
            assertEquals(buf.Position, buf.Limit);
            buf.Reset();
            assertEquals(buf.Position, 0);
        }
        public virtual void TestDuplicate()
        {
            buf.Clear();
            buf.Mark();
            buf.Position = (buf.Limit);

            // duplicate's contents should be the same as buf
            Int64Buffer duplicate = buf.Duplicate();

            assertNotSame(buf, duplicate);
            assertEquals(buf.Position, duplicate.Position);
            assertEquals(buf.Limit, duplicate.Limit);
            assertEquals(buf.IsReadOnly, duplicate.IsReadOnly);
            //assertEquals(buf.IsDirect, duplicate.IsDirect); // J2N: IsDirect not supported
            assertEquals(buf.Order, duplicate.Order);
            assertContentEquals(buf, duplicate);

            // duplicate's position, mark, and limit should be independent to buf
            duplicate.Reset();
            assertEquals(duplicate.Position, 0);
            duplicate.Clear();
            assertEquals(buf.Position, buf.Limit);
            buf.Reset();
            assertEquals(buf.Position, 0);

            // duplicate share the same content with buf
            if (!duplicate.IsReadOnly)
            {
                loadTestData1(buf);
                assertContentEquals(buf, duplicate);
                loadTestData2(duplicate);
                assertContentEquals(buf, duplicate);
            }
        }
 internal void loadTestData2(Int64Buffer buf)
 {
     buf.Clear();
     for (int i = 0; i < buf.Capacity; i++)
     {
         buf.Put(i, (long)buf.Capacity - i);
     }
 }
Exemplo n.º 4
0
        private static void relPut(Int64Buffer b)
        {
            int n = b.Capacity;

            b.Clear();
            for (int i = 0; i < n; i++)
            {
                b.Put((long)Ic(i));
            }
            b.Flip();
        }
Exemplo n.º 5
0
        private static void absPut(Int64Buffer b)
        {
            int n = b.Capacity;

            b.Clear();
            for (int i = 0; i < n; i++)
            {
                b.Put(i, (long)Ic(i));
            }
            b.Limit    = (n);
            b.Position = (0);
        }
Exemplo n.º 6
0
        private static void bulkPutArray(Int64Buffer b)
        {
            int n = b.Capacity;

            b.Clear();
            long[] a = new long[n + 7];
            for (int i = 0; i < n; i++)
            {
                a[i + 7] = (long)Ic(i);
            }
            b.Put(a, 7, n);
            b.Flip();
        }
Exemplo n.º 7
0
        private static void bulkPutBuffer(Int64Buffer b)
        {
            int n = b.Capacity;

            b.Clear();
            Int64Buffer c = Int64Buffer.Allocate(n + 7);

            c.Position = (7);
            for (int i = 0; i < n; i++)
            {
                c.Put((long)Ic(i));
            }
            c.Flip();
            c.Position = (7);
            b.Put(c);
            b.Flip();
        }
        public virtual void TestPutInt64Buffer()
        {
            Int64Buffer other = Int64Buffer.Allocate(buf.Capacity);

            try
            {
                buf.Put(buf);
                fail("Should throw Exception"); //$NON-NLS-1$
            }
            catch (ArgumentException e)
            {
                // expected
            }
            try
            {
                buf.Put(Int64Buffer.Allocate(buf.Capacity + 1));
                fail("Should throw Exception"); //$NON-NLS-1$
            }
            catch (BufferOverflowException e)
            {
                // expected
            }
            try
            {
                buf.Flip();
                buf.Put((Int64Buffer)null);
                fail("Should throw Exception"); //$NON-NLS-1$
            }
            catch (ArgumentNullException e)
            {
                // expected
            }

            loadTestData2(other);
            other.Clear();
            buf.Clear();
            Int64Buffer ret = buf.Put(other);

            assertEquals(other.Position, other.Capacity);
            assertEquals(buf.Position, buf.Capacity);
            assertContentEquals(other, buf);
            assertSame(ret, buf);
        }
Exemplo n.º 9
0
        public static void test(int level, Int64Buffer b, bool direct)
        {
            Show(level, b);

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

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

            // Values

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

            //long v; // J2N: Not used
            b.Flip();
            ck(b, b.Get(), 0);
            ck(b, b.Get(), (long)-1);
            ck(b, b.Get(), 1);
            ck(b, b.Get(), long.MaxValue);
            ck(b, b.Get(), long.MinValue);


            // Comparison
            b.Rewind();
            Int64Buffer b2 = Int64Buffer.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++)
                {
                    long x = b.Get(i);
                    long 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((long)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, (long)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 (long x in VALUES)
            {
                Int64Buffer xb = Int64Buffer.Wrap(new long[] { 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 (long y in VALUES)
                {
                    Int64Buffer yb = Int64Buffer.Wrap(new long[] { 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);
                    }
                    // from Long.compare(x, y)
                    if (xb.CompareTo(yb) != ((x < y) ? -1 : ((x == y) ? 0 : 1)))
                    {
                        fail("Incorrect results for LongBuffer.compareTo",
                             xb, yb, x, y);
                    }
                    if (xb.Equals(yb) != ((x == y) /*|| (x != x) && (y != y)*/))
                    {
                        fail("Incorrect results for LongBuffer.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);
            Int64Buffer sb = b.Slice();

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

            Int64Buffer 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);
            });

            // put(LongBuffer) should not change source position
            Int64Buffer src = Int64Buffer.Allocate(1);

            tryCatch(b, typeof(ReadOnlyBufferException), () =>
            {
                rb.Put(src);
            });
            ck(src, src.Position, 0);

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


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

                tryCatch(b, typeof(ReadOnlyBufferException), () =>
                {
                    var x = 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
        }