You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucenenet.apache.org by ni...@apache.org on 2016/10/23 13:02:09 UTC
[23/50] [abbrv] lucenenet git commit: Ported the tests for ByteBuffer
from Java and refactored ByteBuffer. Also ported LongBuffer + tests (required
by Core.Util.TestPackedInts.TestEncodeDecode()).
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/d235115d/src/Lucene.Net.Tests/core/Support/BaseBufferTestCase.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Tests/core/Support/BaseBufferTestCase.cs b/src/Lucene.Net.Tests/core/Support/BaseBufferTestCase.cs
new file mode 100644
index 0000000..1e38fe0
--- /dev/null
+++ b/src/Lucene.Net.Tests/core/Support/BaseBufferTestCase.cs
@@ -0,0 +1,130 @@
+\ufeffusing Lucene.Net.Util;
+using NUnit.Framework;
+using System;
+using System.Globalization;
+using System.IO;
+using System.Text;
+
+namespace Lucene.Net.Support
+{
+ /// <summary>
+ /// Base class for tests from JDK/nio/Basic.java
+ /// </summary>
+ public class BaseBufferTestCase : LuceneTestCase
+ {
+ internal static TextWriter output = VERBOSE ? Console.Out : new NullWriter();
+
+ internal static long Ic(int i)
+ {
+ int j = i % 54;
+ return j + 'a' + ((j > 26) ? 128 : 0);
+ }
+
+ internal static string ToString(Lucene.Net.Support.Buffer b)
+ {
+ return (b.GetType().Name
+ + "[pos=" + b.Position
+ + " lim=" + b.Limit
+ + " cap=" + b.Capacity
+ + "]");
+ }
+
+ internal static void Show(int level, Lucene.Net.Support.Buffer b)
+ {
+ for (int i = 0; i < level; i++)
+ output.Write(" ");
+ output.WriteLine(ToString(b) + " " + b.GetHashCode().ToString("x4", CultureInfo.InvariantCulture));
+ }
+
+ //static void fail(string s)
+ //{
+ // Assert.Fail(s);
+ //}
+
+ internal static void fail(string s, Lucene.Net.Support.Buffer b)
+ {
+ fail(s + ": " + ToString(b));
+ }
+
+ internal static void fail(String s, Lucene.Net.Support.Buffer b, Lucene.Net.Support.Buffer b2)
+ {
+ fail(s + ": "
+ + ToString(b) + ", " + ToString(b2));
+ }
+
+ internal static void fail(Lucene.Net.Support.Buffer b,
+ string expected, char expectedChar,
+ string got, char gotChar)
+ {
+ if (b is ByteBuffer) {
+ ByteBuffer bb = (ByteBuffer)b;
+ int n = Math.Min(16, bb.Limit);
+ for (int i = 0; i < n; i++)
+ output.Write(" " + (bb.Get(i) & 0xff).ToString("x4", CultureInfo.InvariantCulture));
+ output.WriteLine();
+ }
+ //if (b is CharBuffer) {
+ // CharBuffer bb = (CharBuffer)b;
+ // int n = Math.Min(16, bb.Limit);
+ // for (int i = 0; i < n; i++)
+ // output.Write(" " + (bb.get(i) & 0xffff).ToString("x4", CultureInfo.InvariantCulture));
+ // output.WriteLine();
+ //}
+ Assert.Fail(ToString(b)
+ + ": Expected '" + expectedChar + "'=0x"
+ + expected
+ + ", got '" + gotChar + "'=0x"
+ + got);
+ }
+
+ internal static void fail(Lucene.Net.Support.Buffer b, long expected, long got)
+ {
+ fail(b,
+ (expected).ToString("x4", CultureInfo.InvariantCulture), (char)expected,
+ (got).ToString("x4", CultureInfo.InvariantCulture), (char)got);
+ }
+
+ internal static void ck(Lucene.Net.Support.Buffer b, bool cond)
+ {
+ if (!cond)
+ fail("Condition failed", b);
+ }
+
+ internal static void ck(Lucene.Net.Support.Buffer b, long got, long expected)
+ {
+ if (expected != got)
+ fail(b, expected, got);
+ }
+
+ internal static void ck(Lucene.Net.Support.Buffer b, float got, float expected)
+ {
+ if (expected != got)
+ fail(b,
+ expected.ToString("0.0##########", CultureInfo.InvariantCulture), (char)expected,
+ got.ToString("0.0##########", CultureInfo.InvariantCulture), (char)got);
+ }
+
+ internal static void ck(Lucene.Net.Support.Buffer b, double got, double expected)
+ {
+ if (expected != got)
+ fail(b,
+ expected.ToString("0.0##########", CultureInfo.InvariantCulture), (char)expected,
+ got.ToString("0.0##########", CultureInfo.InvariantCulture), (char)got);
+ }
+
+ /// <summary>
+ /// A simple writer that implements the null object pattern
+ /// that we can swap in when verbosity is turned off.
+ /// </summary>
+ private class NullWriter : TextWriter
+ {
+ public override Encoding Encoding
+ {
+ get
+ {
+ return Encoding.UTF8;
+ }
+ }
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/d235115d/src/Lucene.Net.Tests/core/Support/TestByteBuffer.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Tests/core/Support/TestByteBuffer.cs b/src/Lucene.Net.Tests/core/Support/TestByteBuffer.cs
new file mode 100644
index 0000000..3a0855d
--- /dev/null
+++ b/src/Lucene.Net.Tests/core/Support/TestByteBuffer.cs
@@ -0,0 +1,739 @@
+\ufeffusing Lucene.Net.Attributes;
+using NUnit.Framework;
+using System;
+using System.Reflection;
+
+namespace Lucene.Net.Support
+{
+ /// <summary>
+ /// Tests from JDK/nio/BasicByte.java
+ /// </summary>
+ [TestFixture]
+ public class TestByteBuffer : BaseBufferTestCase
+ {
+ private static readonly sbyte[] VALUES = {
+ sbyte.MinValue,
+ (sbyte) -1,
+ (sbyte) 0,
+ (sbyte) 1,
+ sbyte.MaxValue,
+ };
+
+
+ private static void relGet(ByteBuffer b)
+ {
+ int n = b.Capacity;
+ byte v;
+ for (int i = 0; i < n; i++)
+ ck(b, (long)b.Get(), (long)((byte)Ic(i)));
+ b.Rewind();
+ }
+
+ private static void relGet(ByteBuffer b, int start)
+ {
+ int n = b.Remaining;
+ byte v;
+ for (int i = start; i < n; i++)
+ ck(b, (long)b.Get(), (long)((byte)Ic(i)));
+ b.Rewind();
+ }
+
+ private static void absGet(ByteBuffer b)
+ {
+ int n = b.Capacity;
+ byte v;
+ for (int i = 0; i < n; i++)
+ ck(b, (long)b.Get(), (long)((byte)Ic(i)));
+ b.Rewind();
+ }
+
+ private static void bulkGet(ByteBuffer b)
+ {
+ int n = b.Capacity;
+ byte[] a = new byte[n + 7];
+ b.Get(a, 7, n);
+ for (int i = 0; i < n; i++)
+ ck(b, (long)a[i + 7], (long)((byte)Ic(i)));
+ }
+
+ private static void relPut(ByteBuffer b)
+ {
+ int n = b.Capacity;
+ b.Clear();
+ for (int i = 0; i < n; i++)
+ b.Put((byte)Ic(i));
+ b.Flip();
+ }
+
+ private static void absPut(ByteBuffer 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);
+ }
+
+ private static void bulkPutArray(ByteBuffer b)
+ {
+ int n = b.Capacity;
+ b.Clear();
+ byte[] 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();
+ }
+
+ private static void bulkPutBuffer(ByteBuffer b)
+ {
+ int n = b.Capacity;
+ b.Clear();
+ ByteBuffer c = ByteBuffer.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();
+ }
+
+ //6231529
+ private static void callReset(ByteBuffer b)
+ {
+ b.Position = (0);
+ b.Mark();
+
+ b.Duplicate().Reset();
+ // LUCENENET: AsReadOnlyBuffer() not implemented
+ //b.AsReadOnlyBuffer().Reset();
+ }
+
+ private static void checkSlice(ByteBuffer b, ByteBuffer 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);
+ }
+
+ private static void checkBytes(ByteBuffer b, byte[] bs)
+ {
+ int n = bs.Length;
+ int p = b.Position;
+ byte v;
+ 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);
+ }
+
+ private static void compact(Buffer b)
+ {
+ try
+ {
+ Type cl = b.GetType();
+ MethodInfo m = cl.GetMethod("Compact", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
+ m.Invoke(b, new object[0]);
+ }
+ catch (Exception e)
+ {
+ fail(e.ToString(), b);
+ }
+ }
+
+ private static void checkInvalidMarkException(Buffer b)
+ {
+ tryCatch(b, typeof(InvalidMarkException), () =>
+ {
+ b.Mark();
+ compact(b);
+ b.Reset();
+ });
+ }
+
+ private static void testViews(int level, ByteBuffer 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();
+ TestLongBuffer.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);
+ }
+
+ private static void testHet(int level, ByteBuffer b)
+ {
+
+ int p = b.Position;
+ b.Limit = (b.Capacity);
+ Show(level, b);
+ output.Write(" put:");
+
+ b.PutChar((char)1);
+ b.PutChar((char)char.MaxValue);
+ output.Write(" char");
+
+ b.PutShort((short)1);
+ b.PutShort((short)short.MaxValue);
+ output.Write(" short");
+
+ b.PutInt(1);
+ b.PutInt(int.MaxValue);
+ output.Write(" int");
+
+ b.PutLong((long)1);
+ b.PutLong((long)long.MaxValue);
+ output.Write(" long");
+
+ b.PutFloat((float)1);
+ b.PutFloat((float)float.MinValue);
+ b.PutFloat((float)float.MaxValue);
+ output.Write(" float");
+
+ b.PutDouble((double)1);
+ b.PutDouble((double)double.MinValue);
+ b.PutDouble((double)double.MaxValue);
+ output.Write(" double");
+
+ output.WriteLine();
+ b.Limit = (b.Position);
+ b.Position = (p);
+ Show(level, b);
+ output.Write(" get:");
+
+ ck(b, b.GetChar(), 1);
+ ck(b, b.GetChar(), char.MaxValue);
+ output.Write(" char");
+
+ ck(b, b.GetShort(), 1);
+ ck(b, b.GetShort(), short.MaxValue);
+ output.Write(" short");
+
+ ck(b, b.GetInt(), 1);
+ ck(b, b.GetInt(), int.MaxValue);
+ output.Write(" int");
+
+ ck(b, b.GetLong(), 1);
+ ck(b, b.GetLong(), long.MaxValue);
+ output.Write(" long");
+
+ ck(b, (long)b.GetFloat(), 1);
+ ck(b, (long)b.GetFloat(), unchecked((long)float.MinValue));
+ ck(b, (long)b.GetFloat(), unchecked((long)float.MaxValue));
+ output.Write(" float");
+
+ ck(b, (long)b.GetDouble(), 1);
+ ck(b, (long)b.GetDouble(), unchecked((long)double.MinValue));
+ ck(b, (long)b.GetDouble(), unchecked((long)double.MaxValue));
+ output.Write(" double");
+
+ output.WriteLine();
+ }
+
+ private static void fail(string problem,
+ ByteBuffer xb, ByteBuffer yb,
+ byte x, byte y)
+ {
+ fail(problem + string.Format(": x={0} y={1}", x, y), xb, yb);
+ }
+
+ private static void tryCatch(Buffer b, Type ex, Action thunk)
+ {
+ bool caught = false;
+ try
+ {
+ thunk();
+ }
+ catch (Exception x)
+ {
+ if (ex.IsAssignableFrom(x.GetType()))
+ {
+ caught = true;
+ }
+ else
+ {
+ fail(x.Message + " not expected");
+ }
+ }
+ if (!caught)
+ fail(ex.Name + " not thrown", b);
+ }
+
+ private static void tryCatch(byte[] t, Type ex, Action thunk)
+ {
+ tryCatch(ByteBuffer.Wrap(t), ex, thunk);
+ }
+
+ public static void test(int level, ByteBuffer b, bool direct)
+ {
+ Show(level, b);
+
+ if (direct != b.IsDirect)
+ 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((byte)42);
+ });
+
+ // The index must be non-negative and lesss than the buffer's limit.
+ tryCatch(b, typeof(IndexOutOfRangeException), () =>
+ {
+ b.Get(b.Limit);
+ });
+ tryCatch(b, typeof(IndexOutOfRangeException), () =>
+ {
+ b.Get(-1);
+ });
+
+ tryCatch(b, typeof(IndexOutOfRangeException), () =>
+ {
+ b.Put(b.Limit, (byte)42);
+ });
+
+ tryCatch(b, typeof(InvalidMarkException), () =>
+ {
+ b.Position = (0);
+ b.Mark();
+ b.Compact();
+ b.Reset();
+ });
+
+ // Values
+
+ b.Clear();
+ b.Put((byte)0);
+ b.Put(unchecked((byte)-1));
+ b.Put((byte)1);
+ b.Put(unchecked((byte)sbyte.MaxValue));
+ b.Put(unchecked((byte)sbyte.MinValue));
+
+ byte v;
+ b.Flip();
+ ck(b, b.Get(), 0);
+ ck(b, b.Get(), unchecked((byte)-1));
+ ck(b, b.Get(), 1);
+ ck(b, b.Get(), unchecked((byte)sbyte.MaxValue));
+ ck(b, b.Get(), unchecked((byte)sbyte.MinValue));
+
+
+ // Comparison
+ b.Rewind();
+ ByteBuffer b2 = Lucene.Net.Support.ByteBuffer.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)
+ 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((byte)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, (byte)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 (byte x in VALUES)
+ {
+ ByteBuffer xb = Lucene.Net.Support.ByteBuffer.Wrap(new byte[] { 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 (byte y in VALUES)
+ {
+ ByteBuffer yb = Lucene.Net.Support.ByteBuffer.Wrap(new byte[] { 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 Byte.compare(x, y)
+ //return x - y;
+ if (xb.CompareTo(yb) != (x - y) /* Byte.Compare(x, y)*/)
+ {
+ fail("Incorrect results for ByteBuffer.compareTo",
+ xb, yb, x, y);
+ }
+ if (xb.equals(yb) != ((x == y) || ((x != x) && (y != y))))
+ {
+ fail("Incorrect results for ByteBuffer.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);
+ ByteBuffer sb = b.Slice();
+ checkSlice(b, sb);
+ b.Position = (0);
+ ByteBuffer 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
+
+ b.Rewind();
+ ByteBuffer 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(ByteBuffer) should not change source position
+ ByteBuffer src = ByteBuffer.Allocate(1);
+ tryCatch(b, typeof(ReadOnlyBufferException), () =>
+ {
+ rb.Put(src);
+ });
+ ck(src, src.Position, 0);
+
+ 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((short)1);
+ });
+ tryCatch(b, typeof(ReadOnlyBufferException), () =>
+ {
+ rb.PutShort(0, (short)1);
+ });
+
+ tryCatch(b, typeof(ReadOnlyBufferException), () =>
+ {
+ rb.PutInt(1);
+ });
+ tryCatch(b, typeof(ReadOnlyBufferException), () =>
+ {
+ rb.PutInt(0, 1);
+ });
+
+ tryCatch(b, typeof(ReadOnlyBufferException), () =>
+ {
+ rb.PutLong((long)1);
+ });
+ tryCatch(b, typeof(ReadOnlyBufferException), () =>
+ {
+ rb.PutLong(0, (long)1);
+ });
+
+ tryCatch(b, typeof(ReadOnlyBufferException), () =>
+ {
+ rb.PutFloat((float)1);
+ });
+ tryCatch(b, typeof(ReadOnlyBufferException), () =>
+ {
+ rb.PutFloat(0, (float)1);
+ });
+
+ tryCatch(b, typeof(ReadOnlyBufferException), () =>
+ {
+ rb.PutDouble((double)1);
+ });
+ tryCatch(b, typeof(ReadOnlyBufferException), () =>
+ {
+ rb.PutDouble(0, (double)1);
+ });
+
+
+ if (rb.GetType().Name.StartsWith("java.nio.Heap"))
+ {
+
+ 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);
+
+ // LUCENENET: AllocateDirect not implemented
+
+ // For byte buffers, test both the direct and non-direct cases
+ //ByteBuffer ob
+ // = (b.IsDirect
+ // ? ByteBuffer.Allocate(rb.Capacity)
+ // : ByteBuffer.AllocateDirect(rb.Capacity));
+ ByteBuffer ob = ByteBuffer.Allocate(rb.Capacity);
+ rb.Rewind();
+ ob.Put(rb);
+
+
+ relPut(b); // Required by testViews
+ }
+
+ public static void test(byte[] ba)
+ {
+ int offset = 47;
+ int length = 900;
+ ByteBuffer b = ByteBuffer.Wrap(ba, offset, length);
+ Show(0, b);
+ ck(b, b.Capacity, ba.Length);
+ ck(b, b.Position, offset);
+ ck(b, b.Limit, offset + length);
+
+ // The offset must be non-negative and no larger than <array.length>.
+ tryCatch(ba, typeof(ArgumentOutOfRangeException), () =>
+ {
+ ByteBuffer.Wrap(ba, -1, ba.Length);
+ });
+ tryCatch(ba, typeof(ArgumentOutOfRangeException), () =>
+ {
+ ByteBuffer.Wrap(ba, ba.Length + 1, ba.Length);
+ });
+ tryCatch(ba, typeof(ArgumentOutOfRangeException), () =>
+ {
+ ByteBuffer.Wrap(ba, 0, -1);
+ });
+ tryCatch(ba, typeof(ArgumentOutOfRangeException), () =>
+ {
+ ByteBuffer.Wrap(ba, 0, ba.Length + 1);
+ });
+
+ // A NullPointerException will be thrown if the array is null.
+ tryCatch(ba, typeof(NullReferenceException), () =>
+ {
+ ByteBuffer.Wrap((byte[])null, 0, 5);
+ });
+ tryCatch(ba, typeof(NullReferenceException), () =>
+ {
+ ByteBuffer.Wrap((byte[])null);
+ });
+ }
+
+ private static void testAllocate()
+ {
+ // An IllegalArgumentException will be thrown for negative capacities.
+ tryCatch((Buffer)null, typeof(ArgumentException), () =>
+ {
+ ByteBuffer.Allocate(-1);
+ });
+
+ // LUCENENET: AllocateDirect not implemented
+ //tryCatch((Buffer)null, typeof(ArgumentException), () =>
+ //{
+ // ByteBuffer.AllocateDirect(-1);
+ //});
+
+ // LUCENENET: AllocateDirect not implemented
+ tryCatch((Buffer)null, typeof(NotImplementedException), () =>
+ {
+ ByteBuffer.AllocateDirect(-1);
+ });
+ }
+
+ [Test, LuceneNetSpecific]
+ public static void Test()
+ {
+ testAllocate();
+ test(0, ByteBuffer.Allocate(7 * 1024), false);
+ test(0, ByteBuffer.Wrap(new byte[7 * 1024], 0, 7 * 1024), false);
+ test(new byte[1024]);
+
+ // LUCENENET: AllocateDirect not implemented
+ //ByteBuffer b = ByteBuffer.AllocateDirect(7 * 1024);
+ //for (b.Position = (0); b.Position < b.Limit;)
+ // ck(b, b.Get(), 0);
+ //test(0, b, true);
+
+ callReset(ByteBuffer.Allocate(10));
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/d235115d/src/Lucene.Net.Tests/core/Support/TestLongBuffer.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Tests/core/Support/TestLongBuffer.cs b/src/Lucene.Net.Tests/core/Support/TestLongBuffer.cs
new file mode 100644
index 0000000..3b9cb7d
--- /dev/null
+++ b/src/Lucene.Net.Tests/core/Support/TestLongBuffer.cs
@@ -0,0 +1,522 @@
+\ufeffusing Lucene.Net.Attributes;
+using NUnit.Framework;
+using System;
+
+namespace Lucene.Net.Support
+{
+ /// <summary>
+ /// Tests from JDK/nio/BasicLong.java
+ /// </summary>
+ public class TestLongBuffer : BaseBufferTestCase
+ {
+ private static readonly long[] VALUES = {
+ long.MinValue,
+ (long) -1,
+ (long) 0,
+ (long) 1,
+ long.MaxValue,
+ };
+
+
+ private static void relGet(LongBuffer b)
+ {
+ int n = b.Capacity;
+ long v;
+ for (int i = 0; i < n; i++)
+ ck(b, (long)b.Get(), (long)((long)Ic(i)));
+ b.Rewind();
+ }
+
+ private static void relGet(LongBuffer b, int start)
+ {
+ int n = b.Remaining;
+ long v;
+ for (int i = start; i < n; i++)
+ ck(b, (long)b.Get(), (long)((long)Ic(i)));
+ b.Rewind();
+ }
+
+ private static void absGet(LongBuffer b)
+ {
+ int n = b.Capacity;
+ long v;
+ for (int i = 0; i < n; i++)
+ ck(b, (long)b.Get(), (long)((long)Ic(i)));
+ b.Rewind();
+ }
+
+ private static void bulkGet(LongBuffer b)
+ {
+ int n = b.Capacity;
+ long[] a = new long[n + 7];
+ b.Get(a, 7, n);
+ for (int i = 0; i < n; i++)
+ ck(b, (long)a[i + 7], (long)((long)Ic(i)));
+ }
+
+ private static void relPut(LongBuffer b)
+ {
+ int n = b.Capacity;
+ b.Clear();
+ for (int i = 0; i < n; i++)
+ b.Put((long)Ic(i));
+ b.Flip();
+ }
+
+ private static void absPut(LongBuffer 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);
+ }
+
+ private static void bulkPutArray(LongBuffer 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();
+ }
+
+ private static void bulkPutBuffer(LongBuffer b)
+ {
+ int n = b.Capacity;
+ b.Clear();
+ LongBuffer c = LongBuffer.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();
+ }
+
+ //6231529
+ private static void callReset(LongBuffer b)
+ {
+ b.Position = (0);
+ b.Mark();
+
+ b.Duplicate().Reset();
+
+ // LUCENENET: AsReadOnlyBuffer() not implemented
+ //b.AsReadOnlyBuffer().Reset();
+ }
+
+
+
+ // 6221101-6234263
+
+ private static void putBuffer()
+ {
+ int cap = 10;
+
+ // LUCENENET: AllocateDirect not implemented
+
+ //LongBuffer direct1 = ByteBuffer.AllocateDirect(cap).AsLongBuffer();
+ LongBuffer nondirect1 = ByteBuffer.Allocate(cap).AsLongBuffer();
+ //direct1.Put(nondirect1);
+
+ //LongBuffer direct2 = ByteBuffer.AllocateDirect(cap).AsLongBuffer();
+ LongBuffer nondirect2 = ByteBuffer.Allocate(cap).AsLongBuffer();
+ //nondirect2.Put(direct2);
+
+ //LongBuffer direct3 = ByteBuffer.AllocateDirect(cap).AsLongBuffer();
+ //LongBuffer direct4 = ByteBuffer.AllocateDirect(cap).AsLongBuffer();
+ //direct3.Put(direct4);
+
+ LongBuffer nondirect3 = ByteBuffer.Allocate(cap).AsLongBuffer();
+ LongBuffer nondirect4 = ByteBuffer.Allocate(cap).AsLongBuffer();
+ nondirect3.Put(nondirect4);
+ }
+
+ private static void checkSlice(LongBuffer b, LongBuffer 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);
+ }
+
+ private static void fail(string problem,
+ LongBuffer xb, LongBuffer yb,
+ long x, long y)
+ {
+ fail(problem + string.Format(": x={0} y={1}", x, y), xb, yb);
+ }
+
+ private static void tryCatch(Buffer b, Type ex, Action thunk)
+ {
+ bool caught = false;
+ try
+ {
+ thunk();
+ }
+ catch (Exception x)
+ {
+ if (ex.IsAssignableFrom(x.GetType()))
+ {
+ caught = true;
+ }
+ else
+ {
+ fail(x.Message + " not expected");
+ }
+ }
+ if (!caught)
+ fail(ex.Name + " not thrown", b);
+ }
+
+ private static void tryCatch(long[] t, Type ex, Action thunk)
+ {
+ tryCatch(LongBuffer.Wrap(t), ex, thunk);
+ }
+
+ public static void test(int level, LongBuffer b, bool direct)
+ {
+
+ Show(level, b);
+
+ if (direct != b.IsDirect)
+ 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(IndexOutOfRangeException), () =>
+ {
+ b.Get(b.Limit);
+ });
+ tryCatch(b, typeof(IndexOutOfRangeException), () =>
+ {
+ b.Get(-1);
+ });
+
+ tryCatch(b, typeof(IndexOutOfRangeException), () =>
+ {
+ 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;
+ 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();
+ LongBuffer b2 = Lucene.Net.Support.LongBuffer.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)
+ {
+ LongBuffer xb = Lucene.Net.Support.LongBuffer.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)
+ {
+ LongBuffer yb = Lucene.Net.Support.LongBuffer.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);
+ LongBuffer sb = b.Slice();
+ checkSlice(b, sb);
+ b.Position = (0);
+ LongBuffer 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();
+
+ // LUCENENET: AsReadOnlyBuffer() not implemented
+ tryCatch(b, typeof(NotImplementedException), () =>
+ {
+ b.AsReadOnlyBuffer();
+ });
+
+ // LUCENENET: AsReadOnlyBuffer() not implemented
+ //LongBuffer 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
+ //LongBuffer src = LongBuffer.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"))
+ //{
+
+ // 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
+
+ }
+
+
+ public static void test(long[] ba)
+ {
+ int offset = 47;
+ int length = 900;
+ LongBuffer b = LongBuffer.Wrap(ba, offset, length);
+ Show(0, b);
+ ck(b, b.Capacity, ba.Length);
+ ck(b, b.Position, offset);
+ ck(b, b.Limit, offset + length);
+
+ // The offset must be non-negative and no larger than <array.length>.
+ tryCatch(ba, typeof(ArgumentOutOfRangeException), () =>
+ {
+ LongBuffer.Wrap(ba, -1, ba.Length);
+ });
+ tryCatch(ba, typeof(ArgumentOutOfRangeException), () =>
+ {
+ LongBuffer.Wrap(ba, ba.Length + 1, ba.Length);
+ });
+ tryCatch(ba, typeof(ArgumentOutOfRangeException), () =>
+ {
+ LongBuffer.Wrap(ba, 0, -1);
+ });
+ tryCatch(ba, typeof(ArgumentOutOfRangeException), () =>
+ {
+ LongBuffer.Wrap(ba, 0, ba.Length + 1);
+ });
+
+ // A NullPointerException will be thrown if the array is null.
+ tryCatch(ba, typeof(NullReferenceException), () =>
+ {
+ LongBuffer.Wrap((long[])null, 0, 5);
+ });
+ tryCatch(ba, typeof(NullReferenceException), () =>
+ {
+ LongBuffer.Wrap((long[])null);
+ });
+ }
+
+
+ public static void TestAllocate()
+ {
+ // An IllegalArgumentException will be thrown for negative capacities.
+ tryCatch((Buffer)null, typeof(ArgumentException), () =>
+ {
+ LongBuffer.Allocate(-1);
+ });
+ }
+
+ [Test, LuceneNetSpecific]
+ public static void Test()
+ {
+ TestAllocate();
+ test(0, LongBuffer.Allocate(7 * 1024), false);
+ test(0, LongBuffer.Wrap(new long[7 * 1024], 0, 7 * 1024), false);
+ test(new long[1024]);
+
+ callReset(LongBuffer.Allocate(10));
+ putBuffer();
+
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/d235115d/src/Lucene.Net.Tests/core/Util/Packed/TestPackedInts.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Tests/core/Util/Packed/TestPackedInts.cs b/src/Lucene.Net.Tests/core/Util/Packed/TestPackedInts.cs
index 60b61bb..310de82 100644
--- a/src/Lucene.Net.Tests/core/Util/Packed/TestPackedInts.cs
+++ b/src/Lucene.Net.Tests/core/Util/Packed/TestPackedInts.cs
@@ -982,7 +982,6 @@ namespace Lucene.Net.Util.Packed
}
[Test]
- [Ignore("LUCENENET TODO: LongBuffer and the method AsLongBuffer are not yet ported, so we cannot currently run this test without porting or finding an alternative way to compare the data.")]
public virtual void TestEncodeDecode()
{
foreach (PackedInts.Format format in PackedInts.Format.Values())
@@ -1059,38 +1058,35 @@ namespace Lucene.Net.Util.Packed
Assert.AreEqual(blocks2, blocks3, msg);
}
- // LUCENENET TODO: LongBuffer and the method AsLongBuffer are not yet ported, so we
- // cannot currently run the following tests.
-
- //// 4. byte[] decoding
- //byte[] byteBlocks = new byte[8 * blocks.Length];
- //ByteBuffer.Wrap(byteBlocks).AsLongBuffer().Put(blocks);
- //long[] values2 = new long[valuesOffset + longIterations * longValueCount];
- //decoder.Decode(byteBlocks, blocksOffset * 8, values2, valuesOffset, byteIterations);
- //foreach (long value in values2)
- //{
- // Assert.IsTrue(value <= PackedInts.MaxValue(bpv), msg);
- //}
- //Assert.AreEqual(values, values2, msg);
- //// test decoding to int[]
- //if (bpv <= 32)
- //{
- // int[] intValues2 = new int[values2.Length];
- // decoder.Decode(byteBlocks, blocksOffset * 8, intValues2, valuesOffset, byteIterations);
- // Assert.IsTrue(Equals(intValues2, values2), msg);
- //}
-
- //// 5. byte[] encoding
- //byte[] blocks3_ = new byte[8 * (blocksOffset2 + blocksLen)];
- //encoder.Encode(values, valuesOffset, blocks3_, 8 * blocksOffset2, byteIterations);
- //assertEquals(msg, LongBuffer.Wrap(blocks2), ByteBuffer.Wrap(blocks3_).AsLongBuffer());
- //// test encoding from int[]
- //if (bpv <= 32)
- //{
- // byte[] blocks4 = new byte[blocks3_.Length];
- // encoder.Encode(intValues, valuesOffset, blocks4, 8 * blocksOffset2, byteIterations);
- // Assert.AreEqual(blocks3_, blocks4, msg);
- //}
+ // 4. byte[] decoding
+ byte[] byteBlocks = new byte[8 * blocks.Length];
+ ByteBuffer.Wrap(byteBlocks).AsLongBuffer().Put(blocks);
+ long[] values2 = new long[valuesOffset + longIterations * longValueCount];
+ decoder.Decode(byteBlocks, blocksOffset * 8, values2, valuesOffset, byteIterations);
+ foreach (long value in values2)
+ {
+ Assert.IsTrue(value <= PackedInts.MaxValue(bpv), msg);
+ }
+ Assert.AreEqual(values, values2, msg);
+ // test decoding to int[]
+ if (bpv <= 32)
+ {
+ int[] intValues2 = new int[values2.Length];
+ decoder.Decode(byteBlocks, blocksOffset * 8, intValues2, valuesOffset, byteIterations);
+ Assert.IsTrue(Equals(intValues2, values2), msg);
+ }
+
+ // 5. byte[] encoding
+ byte[] blocks3_ = new byte[8 * (blocksOffset2 + blocksLen)];
+ encoder.Encode(values, valuesOffset, blocks3_, 8 * blocksOffset2, byteIterations);
+ assertEquals(msg, LongBuffer.Wrap(blocks2), ByteBuffer.Wrap(blocks3_).AsLongBuffer());
+ // test encoding from int[]
+ if (bpv <= 32)
+ {
+ byte[] blocks4 = new byte[blocks3_.Length];
+ encoder.Encode(intValues, valuesOffset, blocks4, 8 * blocksOffset2, byteIterations);
+ Assert.AreEqual(blocks3_, blocks4, msg);
+ }
}
}
}