public async Task WriteAllData() { var allData = fileData.Data.SelectMany(t => t).ToArray(); await diskManager.WriteAsync(fileData, 0, allData, allData.Length); var offset = 0; foreach (var data in fileData.Data) { Assert.IsTrue(Toolbox.ByteMatch(allData, offset, data, 0, data.Length), "#1"); offset += data.Length; } for (int i = 0; i < fileData.Hashes.Length; i++) { // Check twice because the first check should give us the result from the incremental hash. Assert.IsTrue(Toolbox.ByteMatch(fileData.Hashes [i], await diskManager.GetHashAsync(fileData, i)), "#2." + i); Assert.IsTrue(Toolbox.ByteMatch(fileData.Hashes [i], await diskManager.GetHashAsync(fileData, i)), "#3." + i); } }
public async Task WriteAllData() { var buffer = new byte[Piece.BlockSize]; var allData = fileData.Data.SelectMany(t => t).Partition(Piece.BlockSize).ToArray(); int blocksPerPiece = fileData.PieceLength / Piece.BlockSize; for (int i = 0; i < allData.Length; i++) { var pieceIndex = i / blocksPerPiece; var offset = (i % blocksPerPiece) * Piece.BlockSize; Buffer.BlockCopy(allData[i], 0, buffer, 0, allData[i].Length); await diskManager.WriteAsync(fileData, new BlockInfo (pieceIndex, offset, allData[i].Length), buffer); } for (int i = 0; i < fileData.Hashes.Length; i++) { // Check twice because the first check should give us the result from the incremental hash. Assert.IsTrue(Toolbox.ByteMatch(fileData.Hashes[i], await diskManager.GetHashAsync(fileData, i)), "#2." + i); Assert.IsTrue(Toolbox.ByteMatch(fileData.Hashes[i], await diskManager.GetHashAsync(fileData, i)), "#3." + i); } }
public async Task WriteAllData() { var buffer = new byte[Constants.BlockSize]; var allData = fileData.Data.SelectMany(t => t).Partition(Constants.BlockSize).ToArray(); int blocksPerPiece = fileData.PieceLength / Constants.BlockSize; for (int i = 0; i < allData.Length; i++) { var pieceIndex = i / blocksPerPiece; var offset = (i % blocksPerPiece) * Constants.BlockSize; Buffer.BlockCopy(allData[i], 0, buffer, 0, allData[i].Length); await diskManager.WriteAsync(fileData, new BlockInfo (pieceIndex, offset, allData[i].Length), buffer); } using var _ = MemoryPool.Default.Rent(20, out Memory <byte> memory); for (int i = 0; i < fileData.Hashes.Length; i++) { // Check twice because the first check should give us the result from the incremental hash. memory.Span.Fill(0); Assert.IsTrue(await diskManager.GetHashAsync(fileData, i, memory)); Assert.IsTrue(fileData.Hashes[i].AsSpan().SequenceEqual(memory.Span), "#2." + i); memory.Span.Fill(0); Assert.IsTrue(await diskManager.GetHashAsync(fileData, i, memory)); Assert.IsTrue(fileData.Hashes[i].AsSpan().SequenceEqual(memory.Span), "#3." + i); } }