public class BinarySerializeTests
private class FakeBinarySerialize : IBinarySerializable, IEquatable<FakeBinarySerialize>
public void Serialize(BinaryWriter binaryWriter)
public void Deserialize(BinaryReader binaryReader)
F1 = binaryReader.ReadString();
F2 = binaryReader.ReadInt32();
public string F1 { set; get; }
public int F2 { set; get; }
public bool Equals(FakeBinarySerialize other)
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return F1 == other.F1 && F2 == other.F2;
public override bool Equals(object obj)
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((FakeBinarySerialize)obj);
public override int GetHashCode()
return ((F1 != null ? F1.GetHashCode() : 0) * 397) ^ F2;
public void WriteUint32List()
"写入uint列表,可以读取列表内容".Test(() =>
var memoryStream = new MemoryStream();
var binaryWriter = new BinaryWriter(memoryStream);
var fakeBinarySerializeList = new List<uint>();
for (int i = 0; i < 100; i++)
fakeBinarySerializeList.Add((uint)i);
binaryWriter.WriteUint32List(fakeBinarySerializeList);
memoryStream.Seek(0, SeekOrigin.Begin);
var binaryReader = new BinaryReader(memoryStream);
var readList = binaryReader.ReadUint32List();
Equal(fakeBinarySerializeList, readList);
"写入列表内容,可以读取列表".Test(() =>
var memoryStream = new MemoryStream();
var binaryWriter = new BinaryWriter(memoryStream);
var fakeBinarySerializeList = new List<FakeBinarySerialize>();
for (int i = 0; i < 100; i++)
fakeBinarySerializeList.Add(new FakeBinarySerialize()
binaryWriter.WriteList(fakeBinarySerializeList);
memoryStream.Seek(0, SeekOrigin.Begin);
var binaryReader = new BinaryReader(memoryStream);
var readList = binaryReader.ReadList<FakeBinarySerialize>();
Equal(fakeBinarySerializeList, readList);
private void Equal<T>(List<T> a, List<T> b)
Assert.AreEqual(a.Count, b.Count);
for (int i = 0; i < a.Count; i++)
Assert.AreEqual(a[i], b[i]);
public void WriteStringList()
"写入空字符串列表,可以读取空的列表".Test(() =>
var memoryStream = new MemoryStream();
var binaryWriter = new BinaryWriter(memoryStream);
var head = "Font Data 1.0.0";
var stringList = new List<string>();
binaryWriter.WriteHead(head, length);
binaryWriter.Write(test);
binaryWriter.WriteStringList(stringList);
memoryStream.Seek(0, SeekOrigin.Begin);
var binaryReader = new BinaryReader(memoryStream);
var str = binaryReader.ReadHead(length);
var b = binaryReader.ReadByte();
var readList = binaryReader.ReadStringList();
Assert.AreEqual(0, readList.Count);
"写入字符串列表,可以读取写入的值".Test(() =>
var memoryStream = new MemoryStream();
var binaryWriter = new BinaryWriter(memoryStream);
var head = "Font Data 1.0.0";
var stringList = new List<string>()
binaryWriter.WriteHead(head, length);
binaryWriter.Write(test);
binaryWriter.WriteStringList(stringList);
memoryStream.Seek(0, SeekOrigin.Begin);
var binaryReader = new BinaryReader(memoryStream);
var str = binaryReader.ReadHead(length);
var b = binaryReader.ReadByte();
var readList = binaryReader.ReadStringList();
"尝试写入长度小于指定长度的头,可以写入和读取".Test(() =>
var memoryStream = new MemoryStream();
var binaryWriter = new BinaryWriter(memoryStream);
var head = "Font Data 1.0.0";
binaryWriter.WriteHead(head, length);
binaryWriter.Write(test);
memoryStream.Seek(0, SeekOrigin.Begin);
var binaryReader = new BinaryReader(memoryStream);
var str = binaryReader.ReadHead(length);
var b = binaryReader.ReadByte();
Assert.AreEqual(head, str);
Assert.AreEqual(test, b);