Browse Source

Add test to ensure that all data written through the EndianBinaryWriter can be read back through EndianBinaryReader

pull/495/head
Jason Nelson 8 years ago
parent
commit
52d2b1ee67
  1. 8
      tests/ImageSharp.Tests/IO/EndianBinaryReaderTests.cs
  2. 58
      tests/ImageSharp.Tests/IO/EndianBinaryReaderWriterTests.cs

8
tests/ImageSharp.Tests/IO/EndianBinaryReaderTests.cs

@ -30,8 +30,8 @@ namespace SixLabors.ImageSharp.Tests.IO
[Fact]
public void ReadCharsBeyondInternalBufferSize()
{
MemoryStream stream = new MemoryStream(TestBytes);
using (EndianBinaryReader subject = new EndianBinaryReader(Endianness.LittleEndian, stream))
var stream = new MemoryStream(TestBytes);
using (var subject = new EndianBinaryReader(Endianness.LittleEndian, stream))
{
char[] chars = new char[TestString.Length];
subject.Read(chars, 0, chars.Length);
@ -48,8 +48,8 @@ namespace SixLabors.ImageSharp.Tests.IO
Assert.Throws<ArgumentException>(
() =>
{
MemoryStream stream = new MemoryStream(TestBytes);
using (EndianBinaryReader subject = new EndianBinaryReader(Endianness.LittleEndian, stream))
var stream = new MemoryStream(TestBytes);
using (var subject = new EndianBinaryReader(Endianness.LittleEndian, stream))
{
char[] chars = new char[TestString.Length - 1];

58
tests/ImageSharp.Tests/IO/EndianBinaryReaderWriterTests.cs

@ -0,0 +1,58 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System.IO;
using SixLabors.ImageSharp.IO;
using Xunit;
namespace SixLabors.ImageSharp.Tests.IO
{
public class EndianBinaryReaderWriterTests
{
/// <summary>
/// Ensures that the data written through a binary writer can be read back through the reader
/// </summary>
[Fact]
public void RoundtripValues()
{
foreach (Endianness endianness in new[] { Endianness.BigEndian, Endianness.LittleEndian })
{
var stream = new MemoryStream();
var writer = new EndianBinaryWriter(endianness, stream);
writer.Write(true); // Bool
writer.Write((byte)1); // Byte
writer.Write((short)1); // Int16
writer.Write(1); // Int32
writer.Write(1L); // Int64
writer.Write(1f); // Single
writer.Write(1d); // Double
writer.Write((sbyte)1); // SByte
writer.Write((ushort)1); // UInt16
writer.Write((uint)1); // UInt32
writer.Write(1UL); // ULong
Assert.Equal(43, stream.Length);
stream.Position = 0;
var reader = new EndianBinaryReader(endianness, stream);
Assert.True(reader.ReadBoolean()); // Bool
Assert.Equal((byte)1, reader.ReadByte()); // Byte
Assert.Equal((short)1, reader.ReadInt16()); // Int16
Assert.Equal(1, reader.ReadInt32()); // Int32
Assert.Equal(1L, reader.ReadInt64()); // Int64
Assert.Equal(1f, reader.ReadSingle()); // Single
Assert.Equal(1d, reader.ReadDouble()); // Double
Assert.Equal((sbyte)1, reader.ReadSByte()); // SByte
Assert.Equal((ushort)1, reader.ReadUInt16()); // UInt16
Assert.Equal((uint)1, reader.ReadUInt32()); // UInt32
Assert.Equal(1UL, reader.ReadUInt64()); // ULong
stream.Dispose();
}
}
}
}
Loading…
Cancel
Save