Make transform better

This commit is contained in:
Matt Nadareski
2026-03-18 12:37:48 -04:00
parent 07f048eca7
commit 87cd8cefbb
3 changed files with 22 additions and 54 deletions

View File

@@ -56,7 +56,7 @@ namespace SabreTools.IO.Test.Transform
string odd = Path.Combine(Environment.CurrentDirectory, "TestData", "ascii.txt");
string output = Guid.NewGuid().ToString();
bool actual = Combine.Interleave(even, odd, output, BlockSize.Byte);
bool actual = Combine.Interleave(even, odd, output, 1);
Assert.False(actual);
}
@@ -67,33 +67,33 @@ namespace SabreTools.IO.Test.Transform
string odd = "NOT A REAL PATH";
string output = Guid.NewGuid().ToString();
bool actual = Combine.Interleave(even, odd, output, BlockSize.Byte);
bool actual = Combine.Interleave(even, odd, output, 1);
Assert.False(actual);
}
[Fact]
public void Interleave_InvalidType_False()
public void Interleave_InvalidValue_False()
{
string even = Path.Combine(Environment.CurrentDirectory, "TestData", "ascii.txt");
string odd = Path.Combine(Environment.CurrentDirectory, "TestData", "ascii.txt");
string output = Guid.NewGuid().ToString();
bool actual = Combine.Interleave(even, odd, output, (BlockSize)int.MaxValue);
bool actual = Combine.Interleave(even, odd, output, -1);
Assert.False(actual);
}
[Theory]
[InlineData(BlockSize.Byte, "TThhiiss ddooeessnn''tt mmaattcchh aannyytthhiinngg")]
[InlineData(BlockSize.Word, "ThThisis d doeoesnsn't't m matatchch a anynyththiningg")]
[InlineData(BlockSize.Dword, "ThisThis doe doesn'tsn't mat match ach anythnythinging")]
[InlineData(BlockSize.Qword, "This doeThis doesn't matsn't match anythch anythinging")]
public void Interleave_SameLength_True(BlockSize type, string expected)
[InlineData(1, "TThhiiss ddooeessnn''tt mmaattcchh aannyytthhiinngg")]
[InlineData(2, "ThThisis d doeoesnsn't't m matatchch a anynyththiningg")]
[InlineData(4, "ThisThis doe doesn'tsn't mat match ach anythnythinging")]
[InlineData(8, "This doeThis doesn't matsn't match anythch anythinging")]
public void Interleave_SameLength_True(int blockSize, string expected)
{
string even = Path.Combine(Environment.CurrentDirectory, "TestData", "ascii.txt");
string odd = Path.Combine(Environment.CurrentDirectory, "TestData", "ascii.txt");
string output = Guid.NewGuid().ToString();
bool actual = Combine.Interleave(even, odd, output, type);
bool actual = Combine.Interleave(even, odd, output, blockSize);
Assert.True(actual);
string text = File.ReadAllText(output);
@@ -110,7 +110,7 @@ namespace SabreTools.IO.Test.Transform
string output = Guid.NewGuid().ToString();
bool actual = Combine.Interleave(even, odd, output, BlockSize.Byte);
bool actual = Combine.Interleave(even, odd, output, 1);
Assert.True(actual);
string text = File.ReadAllText(output);

View File

@@ -70,9 +70,9 @@ namespace SabreTools.IO.Transform
/// <param name="even">First file to interleave</param>
/// <param name="odd">Second file to interleave</param>
/// <param name="output">Path to the output file</param>
/// <param name="type"><see cref="BlockSize"> representing how to process the inputs</param>
/// <param name="blockSize">Number of bytes read before switching input</param>
/// <returns>True if the files were interleaved successfully, false otherwise</returns>
public static bool Interleave(string even, string odd, string output, BlockSize type)
public static bool Interleave(string even, string odd, string output, int blockSize)
{
// If either file does not exist
if (!File.Exists(even) || !File.Exists(odd))
@@ -85,7 +85,7 @@ namespace SabreTools.IO.Transform
using var oddStream = File.Open(odd, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
// Interleave the streams
using var interleaved = Interleave(evenStream, oddStream, type);
using var interleaved = Interleave(evenStream, oddStream, blockSize);
if (interleaved is null)
return false;
@@ -111,26 +111,20 @@ namespace SabreTools.IO.Transform
/// <param name="even">First stream to interleave</param>
/// <param name="odd">Second stream to interleave</param>
/// <param name="output">Path to the output file</param>
/// <param name="type"><see cref="BlockSize"> representing how to process the inputs</param>
/// <param name="blockSize">Number of bytes read before switching input</param>
/// <returns>A filled stream on success, null otherwise</returns>
/// <exception cref="ArgumentOutOfRangeException">
/// Thrown if <paramref name="type"/> is not a recognized value.
/// Thrown if <paramref name="blockSize"/> is non-positive.
/// </exception>
public static Stream? Interleave(Stream even, Stream odd, BlockSize type)
public static Stream? Interleave(Stream even, Stream odd, int blockSize)
{
// If either stream is unreadable
if (!even.CanRead || !odd.CanRead)
return null;
// Get the number of bytes to process
int byteCount = type switch
{
BlockSize.Byte => 1,
BlockSize.Word => 2,
BlockSize.Dword => 4,
BlockSize.Qword => 8,
_ => throw new ArgumentOutOfRangeException(nameof(type)),
};
// If the block size is invalid
if (blockSize <= 0)
throw new ArgumentOutOfRangeException(nameof(blockSize));
try
{
@@ -141,8 +135,8 @@ namespace SabreTools.IO.Transform
bool useEven = true;
while (even.Position < even.Length || odd.Position < odd.Length)
{
byte[] read = new byte[byteCount];
int actual = (useEven ? even : odd).Read(read, 0, byteCount);
byte[] read = new byte[blockSize];
int actual = (useEven ? even : odd).Read(read, 0, blockSize);
outputStream.Write(read, 0, actual);
outputStream.Flush();
useEven = !useEven;

View File

@@ -1,31 +1,5 @@
namespace SabreTools.IO.Transform
{
/// <summary>
/// Determines the block size of an operation
/// </summary>
public enum BlockSize
{
/// <summary>
/// 1 byte blocks
/// </summary>
Byte = 1,
/// <summary>
/// 2 byte blocks
/// </summary>
Word = 2,
/// <summary>
/// 4 byte blocks
/// </summary>
Dword = 4,
/// <summary>
/// 8 byte blocks
/// </summary>
Qword = 8,
}
/// <summary>
/// Determines the swapping operation
/// </summary>