wah getting fucked up on endian business

This commit is contained in:
Lani Aung
2024-09-29 15:17:58 -06:00
parent 94ab274246
commit 6dbc577566
11 changed files with 2430 additions and 57 deletions
@@ -0,0 +1,81 @@
using fxl.codes.kisekae.data.Archives;
namespace fxl.codes.kisekae.data.test;
public class BitStreamTests
{
[Test]
public void TestReadBasic()
{
var bytes = BitConverter.GetBytes(uint.MaxValue);
var stream = new MemoryStream(bytes);
var bitStream = new BitStream(stream);
var value = bitStream.ReadBits(8);
Assert.That(value, Is.EqualTo(byte.MaxValue), "8 bits match failed");
value = bitStream.ReadBits(24);
Assert.Multiple(() =>
{
Assert.That(value, Is.EqualTo(Math.Pow(2, 24) - 1), "24 bits match failed");
Assert.That(bitStream.Position, Is.EqualTo(bitStream.Length), "Stream position is wrong");
});
}
[Test]
public void TestReadRandom()
{
var bytes = BitConverter.GetBytes(uint.MaxValue);
var stream = new MemoryStream(bytes);
var bitStream = new BitStream(stream);
var list = new List<int>();
var sum = 0;
while (sum < 32)
{
var temp = Random.Shared.Next(1, 10);
if (sum + temp < 32) list.Add(temp);
sum += temp;
}
if (list.Sum() < 32) list.Add(32 - list.Sum());
foreach (var num in list)
{
var value = bitStream.ReadBits(num);
Assert.That(value, Is.EqualTo(Math.Pow(2, num) - 1), $"{num} bits match failed");
}
}
[Test]
public void TestReadLong()
{
var random = (ulong)Random.Shared.NextInt64();
var bytes = BitConverter.GetBytes(random);
var stream = new MemoryStream(bytes);
var bitStream = new BitStream(stream);
var list = new List<int>();
var sum = 0;
while (sum < 64)
{
var temp = Random.Shared.Next(1, 20);
if (sum + temp < 64) list.Add(temp);
sum += temp;
}
if (list.Sum() < 64) list.Add(64 - list.Sum());
var runningTotal = 0;
foreach (var num in list)
{
var temp = random;
temp <<= runningTotal;
temp >>= 64 - num;
Console.WriteLine($"Original value: {random:b}, bit shifted number: {temp:b}");
var value = bitStream.ReadBits(num);
Assert.That(value, Is.EqualTo(temp), $"{num} bits match failed");
runningTotal += num;
}
}
}
+49
View File
@@ -0,0 +1,49 @@
using fxl.codes.kisekae.data.Archives;
using fxl.codes.kisekae.data.Archives.Algorithms;
namespace fxl.codes.kisekae.data.test;
public class LZWTests
{
private readonly byte[] _encoded = new[] { 65, 66, 66, 256, 257, 259, 65 }.SelectMany(BitConverter.GetBytes).ToArray();
private readonly byte[] _unencoded = "ABBABBBABBA"u8.ToArray();
private LZW _lzw;
[SetUp]
public void Setup()
{
_lzw = new LZW();
}
[Test]
public void TestEncodingAndDecodingBasic()
{
Assert.Multiple(() =>
{
Assert.That(_lzw.Encode(_unencoded), Is.EqualTo(_encoded), "Encoding failed");
Assert.That(_lzw.Decode(_encoded), Is.EqualTo(_unencoded), "Decoding failed");
});
}
[Test]
public void TestDecompressLzh()
{
var assembly = typeof(LZWTests).Assembly;
foreach (var resource in assembly.GetManifestResourceNames())
{
using var stream = assembly.GetManifestResourceStream(resource);
if (stream == null) continue;
var files = LhCodecService.GetFiles(in stream);
foreach (var file in files)
Assert.Multiple(() =>
{
Assert.That(file.CompressedFile, Is.Not.Null);
Assert.That(file.CompressedFile, Is.Not.Empty);
var decompressed = _lzw.Decode(file.CompressedFile);
Assert.That(decompressed, Is.Not.Null);
});
}
}
}
@@ -42,9 +42,23 @@ public class LhContainerTests
Assert.That(file.MethodId, Is.Not.Null, "Method id is {0}", file.MethodId);
Assert.That(file.MethodId, Does.StartWith("-l"));
Assert.That(file.MethodId, Does.EndWith("-"));
Console.WriteLine($"Archive name: {file.FileName}, compression method: {file.MethodId}");
Console.WriteLine($"Archive name: {file.FileName}, compression method: {file.MethodId}, file at {file.FileDataPosition}");
}
});
}
}
[Test]
public void TestBody()
{
foreach (var resource in _resourceNames)
{
using var stream = _assembly.GetManifestResourceStream(resource);
if (stream == null) continue;
var files = LhCodecService.GetFiles(in stream);
foreach (var file in files)
Assert.Multiple(() => { Assert.That(file.CompressedFile, Is.Not.Null); });
}
}
}