2019-09-10 05:41:51 +08:00
|
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
|
|
|
|
|
2019-10-10 19:48:36 +08:00
|
|
|
|
using System;
|
2019-09-10 05:41:51 +08:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
using osu.Game.IO;
|
|
|
|
|
|
|
|
|
|
namespace osu.Game.Tests.Beatmaps.IO
|
|
|
|
|
{
|
|
|
|
|
[TestFixture]
|
|
|
|
|
public class LineBufferedReaderTest
|
|
|
|
|
{
|
|
|
|
|
[Test]
|
|
|
|
|
public void TestReadLineByLine()
|
|
|
|
|
{
|
2019-10-10 19:48:36 +08:00
|
|
|
|
const string contents = "line 1\rline 2\nline 3";
|
2019-09-10 05:41:51 +08:00
|
|
|
|
|
|
|
|
|
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(contents)))
|
|
|
|
|
using (var bufferedReader = new LineBufferedReader(stream))
|
|
|
|
|
{
|
|
|
|
|
Assert.AreEqual("line 1", bufferedReader.ReadLine());
|
|
|
|
|
Assert.AreEqual("line 2", bufferedReader.ReadLine());
|
|
|
|
|
Assert.AreEqual("line 3", bufferedReader.ReadLine());
|
|
|
|
|
Assert.IsNull(bufferedReader.ReadLine());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void TestPeekLineOnce()
|
|
|
|
|
{
|
2019-10-10 19:48:36 +08:00
|
|
|
|
const string contents = "line 1\r\npeek this\nline 3";
|
2019-09-10 05:41:51 +08:00
|
|
|
|
|
|
|
|
|
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(contents)))
|
|
|
|
|
using (var bufferedReader = new LineBufferedReader(stream))
|
|
|
|
|
{
|
|
|
|
|
Assert.AreEqual("line 1", bufferedReader.ReadLine());
|
|
|
|
|
Assert.AreEqual("peek this", bufferedReader.PeekLine());
|
|
|
|
|
Assert.AreEqual("peek this", bufferedReader.ReadLine());
|
|
|
|
|
Assert.AreEqual("line 3", bufferedReader.ReadLine());
|
|
|
|
|
Assert.IsNull(bufferedReader.ReadLine());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void TestPeekLineMultipleTimes()
|
|
|
|
|
{
|
2019-10-10 19:48:36 +08:00
|
|
|
|
const string contents = "peek this once\nline 2\rpeek this a lot";
|
2019-09-10 05:41:51 +08:00
|
|
|
|
|
|
|
|
|
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(contents)))
|
|
|
|
|
using (var bufferedReader = new LineBufferedReader(stream))
|
|
|
|
|
{
|
|
|
|
|
Assert.AreEqual("peek this once", bufferedReader.PeekLine());
|
|
|
|
|
Assert.AreEqual("peek this once", bufferedReader.ReadLine());
|
|
|
|
|
Assert.AreEqual("line 2", bufferedReader.ReadLine());
|
|
|
|
|
Assert.AreEqual("peek this a lot", bufferedReader.PeekLine());
|
|
|
|
|
Assert.AreEqual("peek this a lot", bufferedReader.PeekLine());
|
|
|
|
|
Assert.AreEqual("peek this a lot", bufferedReader.PeekLine());
|
|
|
|
|
Assert.AreEqual("peek this a lot", bufferedReader.ReadLine());
|
|
|
|
|
Assert.IsNull(bufferedReader.ReadLine());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void TestPeekLineAtEndOfStream()
|
|
|
|
|
{
|
2019-10-10 19:48:36 +08:00
|
|
|
|
const string contents = "first line\r\nsecond line";
|
2019-09-10 05:41:51 +08:00
|
|
|
|
|
|
|
|
|
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(contents)))
|
|
|
|
|
using (var bufferedReader = new LineBufferedReader(stream))
|
|
|
|
|
{
|
|
|
|
|
Assert.AreEqual("first line", bufferedReader.ReadLine());
|
|
|
|
|
Assert.AreEqual("second line", bufferedReader.ReadLine());
|
|
|
|
|
Assert.IsNull(bufferedReader.PeekLine());
|
|
|
|
|
Assert.IsNull(bufferedReader.ReadLine());
|
|
|
|
|
Assert.IsNull(bufferedReader.PeekLine());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void TestPeekReadLineOnEmptyStream()
|
|
|
|
|
{
|
|
|
|
|
using (var stream = new MemoryStream())
|
|
|
|
|
using (var bufferedReader = new LineBufferedReader(stream))
|
|
|
|
|
{
|
|
|
|
|
Assert.IsNull(bufferedReader.PeekLine());
|
|
|
|
|
Assert.IsNull(bufferedReader.ReadLine());
|
|
|
|
|
Assert.IsNull(bufferedReader.ReadLine());
|
|
|
|
|
Assert.IsNull(bufferedReader.PeekLine());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void TestReadToEndNoPeeks()
|
|
|
|
|
{
|
2019-10-10 19:48:36 +08:00
|
|
|
|
const string contents = "first line\r\nsecond line";
|
2019-09-10 05:41:51 +08:00
|
|
|
|
|
|
|
|
|
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(contents)))
|
|
|
|
|
using (var bufferedReader = new LineBufferedReader(stream))
|
|
|
|
|
{
|
|
|
|
|
Assert.AreEqual(contents, bufferedReader.ReadToEnd());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void TestReadToEndAfterReadsAndPeeks()
|
|
|
|
|
{
|
2019-10-10 19:48:36 +08:00
|
|
|
|
const string contents = "this line is gone\rthis one shouldn't be\r\nthese ones\ndefinitely not";
|
2019-09-10 05:41:51 +08:00
|
|
|
|
|
|
|
|
|
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(contents)))
|
|
|
|
|
using (var bufferedReader = new LineBufferedReader(stream))
|
|
|
|
|
{
|
|
|
|
|
Assert.AreEqual("this line is gone", bufferedReader.ReadLine());
|
|
|
|
|
Assert.AreEqual("this one shouldn't be", bufferedReader.PeekLine());
|
2019-10-10 19:48:36 +08:00
|
|
|
|
|
2021-10-27 12:04:41 +08:00
|
|
|
|
string[] endingLines = bufferedReader.ReadToEnd().Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
|
2019-10-10 19:48:36 +08:00
|
|
|
|
Assert.AreEqual(3, endingLines.Length);
|
|
|
|
|
Assert.AreEqual("this one shouldn't be", endingLines[0]);
|
|
|
|
|
Assert.AreEqual("these ones", endingLines[1]);
|
|
|
|
|
Assert.AreEqual("definitely not", endingLines[2]);
|
2019-09-10 05:41:51 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|