Commit 8e806a7c authored by Ray Koopa's avatar Ray Koopa

Add read methods to SpanReader

parent b7d1bc7c
using System.Text;
namespace Syroot.BinaryData.Memory.Test
{
internal static class Encodings
{
// ---- FIELDS -------------------------------------------------------------------------------------------------
internal static readonly Encoding ShiftJIS;
internal static readonly Encoding UHC;
// ---- CONSTRUCTORS & DESTRUCTOR ------------------------------------------------------------------------------
static Encodings()
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
ShiftJIS = Encoding.GetEncoding(932);
UHC = Encoding.GetEncoding(949);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Syroot.BinaryData.Memory.Test
{
[TestClass]
public class StringTests
{
// ---- FIELDS -------------------------------------------------------------------------------------------------
private static readonly List<(Encoding encoding, string text)> _tests = new List<(Encoding, string)>
{
(Encoding.ASCII, "Many love Donald Drumpf. I heard that many people also love a blender up their asshole."),
(null, "Hello, World! «ταБЬℓσ»"),
(Encoding.UTF8, "٩(-̮̮̃-̃)۶ ٩(●̮̮̃•̃)۶ ٩(͡๏̯͡๏)۶ ٩(-̮̮̃•̃)."),
(Encoding.UTF8, "Testing «ταБЬℓσ»: 1<2 & 4+1>3, now 20% off!"),
(Encoding.Unicode, "Smiley balls 👨👩👧👧"),
(Encoding.BigEndianUnicode, "Smiley balls 👨👩👧👧"),
(Encodings.ShiftJIS, "Hey you, 日本へようこそ!"),
(Encodings.ShiftJIS, "日本へようこそ!"),
(Encodings.UHC, "Not Korean"),
(Encodings.UHC, "여보세요 World!"),
(Encodings.UHC, "Say 여보세요"),
(Encodings.UHC, "Say 여보세요, someone!"),
(Encoding.UTF8, "Ź̢͕͚̖̬͖͚̫͙̣̫͘À͓̮̬̙̦͙̗̦̗̰̰̗̯̹̥̰̙̞̮L̀҉̧̠̲̯͚͔͈̟͇͈͘͠ͅG͜҉̝͕̻̩̼͓̝̦̹̫͉͔͠O̢̥̦͔̰͓̩͇̲͇͔͍̕͘!̴͔̮̰̯"),
(Encoding.UTF8, "M᷿᷿͢͏̴᷿᷿᷿᷿͢͢͏᷿᷿͢e᷿᷿͢͏̴᷿᷿᷿᷿͢͢͏᷿᷿͢s᷿᷿͢͏̴᷿᷿᷿᷿͢͢͏᷿᷿͢s᷿᷿͢͏̴᷿᷿᷿᷿͢͢͏᷿᷿͢")
};
// ---- METHODS (PUBLIC) ---------------------------------------------------------------------------------------
[TestMethod]
public void String()
{
Span<byte> buffer = stackalloc byte[2048];
SpanWriter writer = new SpanWriter(buffer);
foreach (var (encoding, text) in _tests)
{
writer.Encoding = encoding;
writer.WriteString(text);
}
SpanReader reader = new SpanReader(buffer);
foreach (var (encoding, text) in _tests)
{
reader.Encoding = encoding;
Assert.AreEqual(text, reader.ReadString());
}
}
[TestMethod]
public void String0()
{
Span<byte> buffer = stackalloc byte[2048];
SpanWriter writer = new SpanWriter(buffer);
foreach (var (encoding, text) in _tests)
{
writer.Encoding = encoding;
writer.WriteString0(text);
}
SpanReader reader = new SpanReader(buffer);
foreach (var (encoding, text) in _tests)
{
reader.Encoding = encoding;
Assert.AreEqual(text, reader.ReadString0());
}
}
[TestMethod]
public void String1()
{
Span<byte> buffer = stackalloc byte[2048];
SpanWriter writer = new SpanWriter(buffer);
foreach (var (encoding, text) in _tests)
{
writer.Encoding = encoding;
writer.WriteString1(text);
}
SpanReader reader = new SpanReader(buffer);
foreach (var (encoding, text) in _tests)
{
reader.Encoding = encoding;
Assert.AreEqual(text, reader.ReadString1());
}
}
[TestMethod]
public void String2()
{
Span<byte> buffer = stackalloc byte[2048];
SpanWriter writer = new SpanWriter(buffer);
foreach (var (encoding, text) in _tests)
{
writer.Encoding = encoding;
writer.WriteString2(text);
}
SpanReader reader = new SpanReader(buffer);
foreach (var (encoding, text) in _tests)
{
reader.Encoding = encoding;
Assert.AreEqual(text, reader.ReadString2());
}
}
[TestMethod]
public void String4()
{
Span<byte> buffer = stackalloc byte[2048];
SpanWriter writer = new SpanWriter(buffer);
foreach (var (encoding, text) in _tests)
{
writer.Encoding = encoding;
writer.WriteString4(text);
}
SpanReader reader = new SpanReader(buffer);
foreach (var (encoding, text) in _tests)
{
reader.Encoding = encoding;
Assert.AreEqual(text, reader.ReadString4());
}
}
[TestMethod]
public void StringFix()
{
int byteCount = _tests.Max(x => (x.encoding ?? Encoding.UTF8).GetByteCount(x.text));
Span<byte> buffer = stackalloc byte[byteCount * _tests.Count];
buffer.Fill(0xFF); // Check zeroing out memory.
SpanWriter writer = new SpanWriter(buffer);
foreach (var (encoding, text) in _tests)
{
writer.Encoding = encoding;
writer.WriteStringFix(text, byteCount);
}
SpanReader reader = new SpanReader(buffer);
foreach (var (encoding, text) in _tests)
{
reader.Encoding = encoding;
Assert.AreEqual(text, reader.ReadStringFix(byteCount));
}
}
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<IsPackable>false</IsPackable>
<LangVersion>latest</LangVersion>
<TargetFrameworks>net461;netcoreapp2.1</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MSTest.TestAdapter" Version="1.4.0" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="4.5.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="MSTest.TestFramework" Version="1.4.0" />
<ProjectReference Include="..\Syroot.BinaryData.Memory\Syroot.BinaryData.Memory.csproj" />
</ItemGroup>
</Project>
This diff is collapsed.
This diff is collapsed.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Authors>Syroot</Authors>
<Copyright>(c) Syroot, licensed under MIT</Copyright>
<Description>.NET library extending span data reading and writing functionality.</Description>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<LangVersion>latest</LangVersion>
<PackageIconUrl>https://gitlab.com/Syroot/BinaryData/raw/master/res/icon.png</PackageIconUrl>
<PackageLicenseUrl>https://gitlab.com/Syroot/BinaryData/raw/master/LICENSE</PackageLicenseUrl>
<PackageProjectUrl>https://gitlab.com/Syroot/BinaryData</PackageProjectUrl>
<PackageReleaseNotes>Initial release.</PackageReleaseNotes>
<PackageTags>binary data memory span readonlymemory readonlyspan</PackageTags>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://gitlab.com/Syroot/BinaryData</RepositoryUrl>
<TargetFrameworks>net452;netcoreapp2.1;netstandard2.0</TargetFrameworks>
<VersionPrefix>5.1.0-alpha3</VersionPrefix>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Memory" Version="4.5.2" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.5.2" />
<ProjectReference Include="..\Syroot.BinaryData\Syroot.BinaryData.csproj" />
</ItemGroup>
<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Authors>Syroot</Authors>
<Copyright>(c) Syroot, licensed under MIT</Copyright>
<Description>.NET library extending span data reading and writing functionality.</Description>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<LangVersion>latest</LangVersion>
<PackageIconUrl>https://gitlab.com/Syroot/BinaryData/raw/master/res/icon.png</PackageIconUrl>
<PackageLicenseUrl>https://gitlab.com/Syroot/BinaryData/raw/master/LICENSE</PackageLicenseUrl>
<PackageProjectUrl>https://gitlab.com/Syroot/BinaryData</PackageProjectUrl>
<PackageReleaseNotes>Initial release.</PackageReleaseNotes>
<PackageTags>binary data memory span readonlymemory readonlyspan</PackageTags>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://gitlab.com/Syroot/BinaryData</RepositoryUrl>
<TargetFrameworks>netcoreapp2.1;netstandard2.0</TargetFrameworks>
<VersionPrefix>5.2.0-alpha1</VersionPrefix>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Memory" Version="4.5.2" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.5.2" />
<ProjectReference Include="..\Syroot.BinaryData.Core\Syroot.BinaryData.Core.csproj" />
</ItemGroup>
</Project>
......@@ -17,7 +17,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Test Projects", "Test Proje
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Library Projects", "Library Projects", "{651EF913-9C28-49B6-B957-00E958484A0F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Syroot.BinaryData.Scratchpad", "Syroot.BinaryData.Scratchpad\Syroot.BinaryData.Scratchpad.csproj", "{CA2FBEC4-F8F2-4FF7-BD4B-13571F3692B0}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Syroot.BinaryData.Core", "Syroot.BinaryData.Core\Syroot.BinaryData.Core.csproj", "{EDC61761-8F06-4F64-A21E-383B0133F3AF}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Syroot.BinaryData.Memory.Test", "Syroot.BinaryData.Memory.Test\Syroot.BinaryData.Memory.Test.csproj", "{436D9622-68EB-425D-8854-438F25669474}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
......@@ -45,10 +47,14 @@ Global
{C618C9FE-F8AD-40D0-8E7C-608FB938B821}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C618C9FE-F8AD-40D0-8E7C-608FB938B821}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C618C9FE-F8AD-40D0-8E7C-608FB938B821}.Release|Any CPU.Build.0 = Release|Any CPU
{CA2FBEC4-F8F2-4FF7-BD4B-13571F3692B0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CA2FBEC4-F8F2-4FF7-BD4B-13571F3692B0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CA2FBEC4-F8F2-4FF7-BD4B-13571F3692B0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CA2FBEC4-F8F2-4FF7-BD4B-13571F3692B0}.Release|Any CPU.Build.0 = Release|Any CPU
{EDC61761-8F06-4F64-A21E-383B0133F3AF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EDC61761-8F06-4F64-A21E-383B0133F3AF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EDC61761-8F06-4F64-A21E-383B0133F3AF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EDC61761-8F06-4F64-A21E-383B0133F3AF}.Release|Any CPU.Build.0 = Release|Any CPU
{436D9622-68EB-425D-8854-438F25669474}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{436D9622-68EB-425D-8854-438F25669474}.Debug|Any CPU.Build.0 = Debug|Any CPU
{436D9622-68EB-425D-8854-438F25669474}.Release|Any CPU.ActiveCfg = Release|Any CPU
{436D9622-68EB-425D-8854-438F25669474}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
......@@ -59,6 +65,8 @@ Global
{CCE887C9-DB18-401A-91F4-DF3BC4442E68} = {651EF913-9C28-49B6-B957-00E958484A0F}
{CBB0637C-986F-4399-8689-F4F22027D973} = {6198E070-1B04-438F-9EDC-A3A8C16A0F64}
{C618C9FE-F8AD-40D0-8E7C-608FB938B821} = {651EF913-9C28-49B6-B957-00E958484A0F}
{EDC61761-8F06-4F64-A21E-383B0133F3AF} = {651EF913-9C28-49B6-B957-00E958484A0F}
{436D9622-68EB-425D-8854-438F25669474} = {6198E070-1B04-438F-9EDC-A3A8C16A0F64}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {9E175130-7965-40FF-8F75-73EE5334F6CE}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment