Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests for StreamExtensions, #1105 #1109

Merged
merged 5 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions src/Lucene.Net.Facet/Taxonomy/WriterCache/CharBlockArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ namespace Lucene.Net.Facet.Taxonomy.WriterCache
/// <para/>
/// @lucene.experimental
/// </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.
paulirwin marked this conversation as resolved.
Show resolved Hide resolved
internal class CharBlockArray : IAppendable, ICharSequence,
ISpanAppendable /* LUCENENET specific */
{
Expand Down
132 changes: 132 additions & 0 deletions src/Lucene.Net.Tests/Support/IO/TestStreamExtensions.cs
Original file line number Diff line number Diff line change
@@ -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
OpenDataInputStream();
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
OpenDataInputStream();
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
OpenDataInputStream();
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
OpenDataInputStream();
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
OpenDataInputStream();
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
OpenDataInputStream();
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
OpenDataInputStream();
Assert.AreEqual(9875645283333L, stream.ReadInt64(), "Incorrect long read");
}

private void OpenDataInputStream()
paulirwin marked this conversation as resolved.
Show resolved Hide resolved
{
// 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()
paulirwin marked this conversation as resolved.
Show resolved Hide resolved
{
base.SetUp();
stream = new MemoryStream();
}
}
}
Loading