public void BitFieldEncoding() { bool[] data = { true, false, false, true, false, true, false, true, false, true, false, true, false, false, false, true, true, true, false, false, false, true, false, true, false, false, true, false, true, false, true, true, false, false, true, false, false, true, true, false }; Assert.AreEqual(data.Length, (int)Math.Ceiling((double)torrentData.Size / torrentData.PieceLength), "#0"); byte[] encoded = new BitfieldMessage(new BitField(data)).Encode(); BitfieldMessage m = (BitfieldMessage)PeerMessage.DecodeMessage(encoded, torrentData).message; Assert.AreEqual(data.Length, m.BitField.Length, "#1"); for (int i = 0; i < data.Length; i++) { Assert.AreEqual(data[i], m.BitField[i], "#2." + i); } }
public void BitFieldDecoding() { byte[] buf = { 0x00, 0x00, 0x00, 0x04, 0x05, 0xff, 0x08, 0xAA, 0xE3, 0x00 }; BitfieldMessage msg = (BitfieldMessage)PeerMessage.DecodeMessage(buf, torrentData).message; for (int i = 0; i < 8; i++) { Assert.IsTrue(msg.BitField[i], i.ToString()); } for (int i = 8; i < 12; i++) { Assert.IsFalse(msg.BitField[i], i.ToString()); } Assert.IsTrue(msg.BitField[12], 12.ToString ()); for (int i = 13; i < 15; i++) { Assert.IsFalse(msg.BitField[i], i.ToString()); } EncodeDecode(msg); }