Skip to content

Commit

Permalink
Add tests for ByteArrayExtensions
Browse files Browse the repository at this point in the history
Fixed up some bugs found while testing.
Added UTF32 Big Endian
  • Loading branch information
M-Zuber committed Oct 30, 2016
1 parent 02ddd24 commit 6bcc1e6
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
using System.IO;
using System.Text;

namespace MoreDotNet.Tests.Extensions.Common.ByteArrayExtensions
{
using MoreDotNet.Extensions.Common;
using Xunit;

public class GetStringTests
{
[Fact]
public void GetString_NullBuffer_ShouldReturnEmptyString()
{
byte[] buffer = null;
var result = buffer.GetString();

Assert.Equal(string.Empty, result);
}

[Fact]
public void GetString_ZeroLengthBuffer_ShouldReturnEmptyString()
{
var buffer = new byte[] { };
var result = buffer.GetString();

Assert.Equal(string.Empty, result);
}

[Fact]
public void GetString_UTF8_ShouldReturnProperString()
{
File.WriteAllText("temp.txt", "More Dot Net", Encoding.UTF8);
var buffer = File.ReadAllBytes("temp.txt");
var result = buffer.GetString();

Assert.Equal(result, "More Dot Net");
}

[Fact]
public void GetString_Unicode_ShouldReturnProperString()
{
File.WriteAllText("temp.txt", "More Dot Net", Encoding.Unicode);
var buffer = File.ReadAllBytes("temp.txt");
var result = buffer.GetString();

Assert.Equal(result, "More Dot Net");
}

[Fact]
public void GetString_BigEndianUnicode_ShouldReturnProperString()
{
File.WriteAllText("temp.txt", "More Dot Net", Encoding.BigEndianUnicode);
var buffer = File.ReadAllBytes("temp.txt");
var result = buffer.GetString();

Assert.Equal(result, "More Dot Net");
}

[Fact]
public void GetString_UTF32_ShouldReturnProperString()
{
File.WriteAllText("temp.txt", "More Dot Net", Encoding.UTF32);
var buffer = File.ReadAllBytes("temp.txt");
var result = buffer.GetString();

Assert.Equal(result, "More Dot Net");
}

[Fact]
public void GetString_UTF32BigEndian_ShouldReturnProperString()
{
File.WriteAllText("temp.txt", "More Dot Net", new UTF32Encoding(true, true));
var buffer = File.ReadAllBytes("temp.txt");
var result = buffer.GetString();

Assert.Equal(result, "More Dot Net");
}

[Fact]
public void GetString_UTF7_ShouldReturnProperString()
{
File.WriteAllText("temp.txt", "More Dot Net", Encoding.UTF7);
var buffer = File.ReadAllBytes("temp.txt");
var result = buffer.GetString();

Assert.Equal(result, "More Dot Net");
}

[Fact]
public void GetString_ANSI_Specified_ShouldReturnProperString()
{
File.WriteAllText("temp.txt", "More Dot Net", Encoding.Default);
var buffer = File.ReadAllBytes("temp.txt");
var result = buffer.GetString();

Assert.Equal(result, "More Dot Net");
}

[Fact]
public void GetString_NoEncodingSpecified_ShouldReturnProperString()
{
File.WriteAllText("temp.txt", "More Dot Net");
var buffer = File.ReadAllBytes("temp.txt");
var result = buffer.GetString();

Assert.Equal(result, "More Dot Net");
}

[Fact]
public void GetString_ASCII_ShouldReturnProperString()
{
File.WriteAllText("temp.txt", "More Dot Net", Encoding.ASCII);
var buffer = File.ReadAllBytes("temp.txt");
var result = buffer.GetString();

Assert.Equal(result, "More Dot Net");
}
}
}
1 change: 1 addition & 0 deletions Source/MoreDotNet.Test/MoreDotNet.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
<Compile Include="Extensions\Common\BooleanExtensions\WhenTrueTests.cs" />
<Compile Include="Extensions\Collections\CollectionExtensions\AddRangeTests.cs" />
<Compile Include="Extensions\Collections\CollectionExtensions\IsNullOrEmptyTests.cs" />
<Compile Include="Extensions\Common\ByteArrayExtensions\GetStringTests.cs" />
<Compile Include="Extensions\Common\ColorExtensions\ToHexStringTests.cs" />
<Compile Include="Extensions\Common\ColorExtensions\ToRgbStringTests.cs" />
<Compile Include="Extensions\Common\ConvertibleExtensions\ToOrDefaultTests.cs" />
Expand Down
14 changes: 9 additions & 5 deletions Source/MoreDotNet/Extensions/Common/ByteArrayExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,22 @@ FE FF UTF-16 big endian
{
encoding = Encoding.UTF8;
}
else if (buffer[0] == 0xfe && buffer[1] == 0xff)
else if (buffer[0] == 0xff && buffer[1] == 0xfe && buffer[2] == 0 && buffer[3] == 0)
{
encoding = Encoding.UTF32;
}
else if (buffer[0] == 0 && buffer[1] == 0 && buffer[2] == 0xfe && buffer[3] == 0xff)
{
encoding = new UTF32Encoding(true, true);
}
else if (buffer[0] == 0xff && buffer[1] == 0xfe)
{
encoding = Encoding.Unicode;
}
else if (buffer[0] == 0xfe && buffer[1] == 0xff)
{
encoding = Encoding.BigEndianUnicode; // utf-16be
}
else if (buffer[0] == 0 && buffer[1] == 0 && buffer[2] == 0xfe && buffer[3] == 0xff)
{
encoding = Encoding.UTF32;
}
else if (buffer[0] == 0x2b && buffer[1] == 0x2f && buffer[2] == 0x76)
{
encoding = Encoding.UTF7;
Expand Down

0 comments on commit 6bcc1e6

Please # to comment.