Skip to content

Commit ec40c34

Browse files
committed
Add unit tests
1 parent 43802aa commit ec40c34

2 files changed

Lines changed: 69 additions & 2 deletions

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using Lucene.Net.Attributes;
3+
using NUnit.Framework;
4+
using Assert = Lucene.Net.TestFramework.Assert;
5+
6+
namespace Lucene.Net.Store
7+
{
8+
/*
9+
* Licensed to the Apache Software Foundation (ASF) under one or more
10+
* contributor license agreements. See the NOTICE file distributed with
11+
* this work for additional information regarding copyright ownership.
12+
* The ASF licenses this file to You under the Apache License, Version 2.0
13+
* (the "License"); you may not use this file except in compliance with
14+
* the License. You may obtain a copy of the License at
15+
*
16+
* http://www.apache.org/licenses/LICENSE-2.0
17+
*
18+
* Unless required by applicable law or agreed to in writing, software
19+
* distributed under the License is distributed on an "AS IS" BASIS,
20+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21+
* See the License for the specific language governing permissions and
22+
* limitations under the License.
23+
*/
24+
25+
using LuceneTestCase = Lucene.Net.Util.LuceneTestCase;
26+
27+
[TestFixture]
28+
[LuceneNetSpecific]
29+
public class TestByteArrayDataOutput : LuceneTestCase
30+
{
31+
[Test]
32+
public virtual void TestWriteString()
33+
{
34+
byte[] bytes = new byte[10];
35+
ByteArrayDataOutput @out = new ByteArrayDataOutput(bytes);
36+
@out.WriteString("ABC");
37+
38+
ByteArrayDataInput @in = new ByteArrayDataInput(bytes);
39+
Assert.AreEqual("ABC", @in.ReadString());
40+
}
41+
42+
[Test]
43+
public virtual void TestWriteChars()
44+
{
45+
byte[] bytes = new byte[10];
46+
ByteArrayDataOutput @out = new ByteArrayDataOutput(bytes);
47+
@out.WriteChars("ABC".AsSpan());
48+
49+
ByteArrayDataInput @in = new ByteArrayDataInput(bytes);
50+
Assert.AreEqual("ABC", @in.ReadString());
51+
}
52+
}
53+
}

src/Lucene.Net.Tests/Util/TestUnicodeUtil.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ public void TestUTF8toUTF16Exception(byte[] invalidUtf8, bool shouldThrow)
348348
}
349349

350350
[Test]
351-
[LuceneNetSpecific] // this is a Lucene.NET specific method
351+
[LuceneNetSpecific]
352352
[Repeat(100)]
353353
public void TestTryUTF8toUTF16()
354354
{
@@ -362,7 +362,7 @@ public void TestTryUTF8toUTF16()
362362
}
363363

364364
[Test]
365-
[LuceneNetSpecific] // this is a Lucene.NET specific method
365+
[LuceneNetSpecific]
366366
[TestCase(new byte[] { 0x63, 0x61, 0xc3 }, "ca\ufffd")] // ca�, start of 2-byte sequence
367367
[TestCase(new byte[] { 0x63, 0x61, 0xe3 }, "ca\ufffd")] // ca�, start of 3-byte sequence
368368
[TestCase(new byte[] { 0x63, 0x61, 0xf3 }, "ca\ufffd")] // ca�, start of 4-byte sequence
@@ -375,5 +375,19 @@ public void TestUTF8toUTF16WithFallback(byte[] utf8, string expected)
375375

376376
Assert.AreEqual(expected, scratch.ToString());
377377
}
378+
379+
[Test]
380+
[LuceneNetSpecific]
381+
[Repeat(100)]
382+
public void TestTryUTF16toUTF8()
383+
{
384+
string unicode = TestUtil.RandomRealisticUnicodeString(Random);
385+
var utf8 = new byte[IOUtils.ENCODING_UTF_8_NO_BOM.GetMaxByteCount(unicode.Length)];
386+
387+
bool success = UnicodeUtil.TryUTF16toUTF8(unicode.AsSpan(), utf8.AsSpan(), out int bytesWritten);
388+
389+
Assert.IsTrue(success);
390+
Assert.AreEqual(unicode, IOUtils.ENCODING_UTF_8_NO_BOM.GetString(utf8, 0, bytesWritten));
391+
}
378392
}
379393
}

0 commit comments

Comments
 (0)