This is an automated email from the ASF dual-hosted git repository.
paulirwin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/lucenenet.git
The following commit(s) were added to refs/heads/master by this push:
new 6af970814 Add unit tests for StreamExtensions, #1105 (#1109)
6af970814 is described below
commit 6af970814682bf0593d2ef69aac66e29b611e7de
Author: Paul Irwin <[email protected]>
AuthorDate: Tue Jan 21 09:17:27 2025 -0700
Add unit tests for StreamExtensions, #1105 (#1109)
* Add unit tests for StreamExtensions, #1105
---
.../Taxonomy/WriterCache/CharBlockArray.cs | 5 +-
.../Support/IO/TestStreamExtensions.cs | 132 +++++++++++++++++++++
2 files changed, 135 insertions(+), 2 deletions(-)
diff --git a/src/Lucene.Net.Facet/Taxonomy/WriterCache/CharBlockArray.cs
b/src/Lucene.Net.Facet/Taxonomy/WriterCache/CharBlockArray.cs
index 07fc91f4e..ab6aede93 100644
--- a/src/Lucene.Net.Facet/Taxonomy/WriterCache/CharBlockArray.cs
+++ b/src/Lucene.Net.Facet/Taxonomy/WriterCache/CharBlockArray.cs
@@ -36,8 +36,9 @@ namespace Lucene.Net.Facet.Taxonomy.WriterCache
/// </summary>
// LUCENENET NOTE: The serialization features here are strictly for
testing purposes,
// therefore it doesn't make any difference what type of serialization is
used.
- // To make things simpler, we are using BinaryReader and BinaryWriter since
- // BinaryFormatter is not implemented in .NET Standard 1.x.
+ // To make things simpler, we are using extension methods on Stream since
+ // BinaryReader and BinaryWriter throw exceptions if surrogate pairs wind
up
+ // in separate chunks.
internal class CharBlockArray : IAppendable, ICharSequence,
ISpanAppendable /* LUCENENET specific */
{
diff --git a/src/Lucene.Net.Tests/Support/IO/TestStreamExtensions.cs
b/src/Lucene.Net.Tests/Support/IO/TestStreamExtensions.cs
new file mode 100644
index 000000000..a9a0a9cf6
--- /dev/null
+++ b/src/Lucene.Net.Tests/Support/IO/TestStreamExtensions.cs
@@ -0,0 +1,132 @@
+// Some tests adapted from Apache Harmony:
+//
https://github.com/apache/harmony/blob/02970cb7227a335edd2c8457ebdde0195a735733/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/DataInputStreamTest.java
+//
https://github.com/apache/harmony/blob/02970cb7227a335edd2c8457ebdde0195a735733/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/DataOutputStreamTest.java
+
+using J2N.IO;
+using Lucene.Net.Util;
+using NUnit.Framework;
+using System.Diagnostics.CodeAnalysis;
+using System.IO;
+using System.Text;
+
+namespace Lucene.Net.Support.IO
+{
+ /*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+ [TestFixture]
+ public class TestStreamExtensions : LuceneTestCase
+ {
+ private Stream stream;
+
+ private const string fileString =
"Test_All_Tests\nTest_java_io_BufferedInputStream\nTest_java_io_BufferedOutputStream\nTest_java_io_ByteArrayInputStream\nTest_java_io_ByteArrayOutputStream\nTest_DataInputStream\n";
+
+ [Test]
+ // LUCENENET note: adapted from test_read$BII() for ByteBuffer-based
Read extension method
+ public void TestRead()
+ {
+ byte[] bytes = Encoding.UTF8.GetBytes(fileString);
+ stream.Write(bytes, 0, bytes.Length);
+ // stream.Dispose(); // LUCENENET - we will reuse stream
+ ResetStreamForReading();
+ var buffer = ByteBuffer.Allocate((int)stream.Length);
+ stream.Read(buffer, 0);
+
Assert.IsTrue(Encoding.UTF8.GetString(buffer.Array).Equals(fileString));
+ }
+
+ [Test]
+ // LUCENENET note: adapted from test_writeCharsLjava_lang_String()
+ public void TestWrite_CharArray()
+ {
+ stream.Write("Test String".ToCharArray());
+ // stream.Dispose(); // LUCENENET - we will reuse stream
+ ResetStreamForReading();
+ char[] chars = stream.ReadChars((int)stream.Length / 2); //
LUCENENET note: we don't have/need a ReadChar method, so reusing ReadChars here
+ Assert.AreEqual("Test String", new string(chars, 0, chars.Length),
"Incorrect chars written");
+ }
+
+ [Test]
+ // LUCENENET note: adapted from test_read$BII() for ReadChars
extension method
+ public void TestReadChars()
+ {
+ byte[] bytes = Encoding.Unicode.GetBytes(fileString); // NOTE:
ReadChars reads UTF-16 chars
+ stream.Write(bytes, 0, bytes.Length);
+ // stream.Dispose(); // LUCENENET - we will reuse stream
+ ResetStreamForReading();
+ var chars = stream.ReadChars(fileString.Length);
+ Assert.IsTrue(new string(chars).Equals(fileString));
+ }
+
+ [Test]
+ // LUCENENET note: adapted from test_writeIntI()
+ public void TestWrite_Int32()
+ {
+ stream.Write(9087589);
+ // stream.Dispose(); // LUCENENET - we will reuse stream
+ ResetStreamForReading();
+ int c = stream.ReadInt32();
+ // dis.close();
+ Assert.AreEqual(9087589, c, "Incorrect int written");
+ }
+
+ [Test]
+ // LUCENENET note: adapted from test_readInt()
+ public void TestReadInt32()
+ {
+ stream.Write(768347202);
+ // stream.Dispose(); // LUCENENET - we will reuse stream
+ ResetStreamForReading();
+ Assert.AreEqual(768347202, stream.ReadInt32(), "Incorrect int
read");
+ }
+
+ [Test]
+ // LUCENENET note: adapted from test_writeLongJ()
+ public void TestWrite_Int64()
+ {
+ stream.Write(908755555456L);
+ // stream.Dispose(); // LUCENENET - we will reuse stream
+ ResetStreamForReading();
+ long c = stream.ReadInt64();
+ // dis.close();
+ Assert.AreEqual(908755555456L, c, "Incorrect long written");
+ }
+
+ [Test]
+ // LUCENENET note: adapted from test_readLong()
+ public void TestReadInt64()
+ {
+ stream.Write(9875645283333L);
+ // stream.Dispose(); // LUCENENET - we will reuse stream
+ ResetStreamForReading();
+ Assert.AreEqual(9875645283333L, stream.ReadInt64(), "Incorrect
long read");
+ }
+
+ private void ResetStreamForReading() // LUCENENET - was
"OpenDataInputStream" in Harmony tests
+ {
+ // LUCENENET specific - in the Harmony tests, there were separate
streams
+ // for input and output. Here, we'll just reuse the same stream,
but reset
+ // its position to 0 so it's ready to read.
+ stream.Position = 0;
+ }
+
+ public override void SetUp()
+ {
+ base.SetUp();
+ stream = new MemoryStream();
+ }
+ }
+}