Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions PowerKit.Tests/Adler32Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using System.IO;
using FluentAssertions;
using PowerKit;
using Xunit;

namespace PowerKit.Tests;

public class Adler32Tests
{
[Fact]
public void Hash_Stream_Test()
{
// Arrange
using var stream = new MemoryStream("Wikipedia"u8.ToArray());

// Act
var hash = Adler32.Hash(stream);

// Assert
hash.Should().Be(0x11E60398u);
}

[Fact]
public void Hash_ByteArray_Test()
{
// Arrange
var data = "Wikipedia"u8.ToArray();

// Act
var hash = Adler32.Hash(data);

// Assert
hash.Should().Be(0x11E60398u);
}

[Fact]
public void Hash_Span_Test()
{
// Arrange
var data = "Wikipedia"u8.ToArray();

// Act
var hash = Adler32.Hash((ReadOnlySpan<byte>)data);

// Assert
hash.Should().Be(0x11E60398u);
}

[Fact]
public void Hash_EmptyData_Test()
{
// Act
var hash = Adler32.Hash(Array.Empty<byte>());

// Assert
hash.Should().Be(1u);
}
}
59 changes: 59 additions & 0 deletions PowerKit.Tests/Crc32Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using System.IO;
using FluentAssertions;
using PowerKit;
using Xunit;

namespace PowerKit.Tests;

public class Crc32Tests
{
[Fact]
public void Hash_Stream_Test()
{
// Arrange
using var stream = new MemoryStream("123456789"u8.ToArray());

// Act
var hash = Crc32.Hash(stream);

// Assert
hash.Should().Be(0xCBF43926u);
}

[Fact]
public void Hash_ByteArray_Test()
{
// Arrange
var data = "123456789"u8.ToArray();

// Act
var hash = Crc32.Hash(data);

// Assert
hash.Should().Be(0xCBF43926u);
}

[Fact]
public void Hash_Span_Test()
{
// Arrange
var data = "123456789"u8.ToArray();

// Act
var hash = Crc32.Hash((ReadOnlySpan<byte>)data);

// Assert
hash.Should().Be(0xCBF43926u);
}

[Fact]
public void Hash_EmptyData_Test()
{
// Act
var hash = Crc32.Hash(Array.Empty<byte>());

// Assert
hash.Should().Be(0u);
}
}
85 changes: 85 additions & 0 deletions PowerKit.Tests/DeflateTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System;
using System.IO;
using FluentAssertions;
using PowerKit;
using Xunit;

namespace PowerKit.Tests;

public class DeflateTests
{
[Fact]
public void Compress_Decompress_Stream_Test()
{
// Arrange
var data = "hello world"u8.ToArray();
using var input = new MemoryStream(data);
using var compressed = new MemoryStream();
using var decompressed = new MemoryStream();

// Act
Deflate.Compress(input, compressed);
compressed.Position = 0;
Deflate.Decompress(compressed, decompressed);

// Assert
decompressed.ToArray().Should().Equal(data);
}

[Fact]
public void Compress_Decompress_ByteArray_Test()
{
// Arrange
var data = "hello world"u8.ToArray();

// Act
var compressed = Deflate.Compress(data);
var decompressed = Deflate.Decompress(compressed);

// Assert
decompressed.Should().Equal(data);
}

[Fact]
public void Compress_Decompress_Span_Test()
{
// Arrange
var data = "hello world"u8.ToArray();

// Act
var compressed = Deflate.Compress((ReadOnlySpan<byte>)data);
var decompressed = Deflate.Decompress((ReadOnlySpan<byte>)compressed);

// Assert
decompressed.Should().Equal(data);
}

[Fact]
public void Compress_Decompress_EmptyData_Test()
{
// Arrange
var data = Array.Empty<byte>();

// Act
var compressed = Deflate.Compress(data);
var decompressed = Deflate.Decompress(compressed);

// Assert
decompressed.Should().BeEmpty();
}

[Fact]
public void Compress_ProducesCompressedOutput_Test()
{
// Arrange
var data = new byte[1000];
for (var i = 0; i < data.Length; i++)
data[i] = (byte)(i % 4);

// Act
var compressed = Deflate.Compress(data);

// Assert
compressed.Should().HaveCountLessThan(data.Length);
}
}
66 changes: 66 additions & 0 deletions PowerKit/Adler32.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System.IO;
#if !NETFRAMEWORK || NET45_OR_GREATER
using System;
#endif


namespace PowerKit;

/// <summary>
/// Provides methods for computing Adler-32 checksums.
/// </summary>
public static class Adler32
{
private const uint Modulus = 65521;

/// <summary>
/// Computes the Adler-32 checksum of data read from the specified stream.
/// </summary>
public static uint Hash(Stream stream)
{
uint a = 1,
b = 0;

var buffer = new byte[4096];
int read;

while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
{
for (var i = 0; i < read; i++)
{
a = (a + buffer[i]) % Modulus;
b = (b + a) % Modulus;
}
}

return (b << 16) | a;
}

/// <summary>
/// Computes the Adler-32 checksum of the specified data.
/// </summary>
public static uint Hash(byte[] data)
{
using var stream = new MemoryStream(data);
return Hash(stream);
}

#if !NETFRAMEWORK || NET45_OR_GREATER
/// <summary>
/// Computes the Adler-32 checksum of the specified data.
/// </summary>
public static uint Hash(ReadOnlySpan<byte> data)
{
uint a = 1,
b = 0;

for (var i = 0; i < data.Length; i++)
{
a = (a + data[i]) % Modulus;
b = (b + a) % Modulus;
}

return (b << 16) | a;
}
#endif
}
77 changes: 77 additions & 0 deletions PowerKit/Crc32.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System.IO;
#if !NETFRAMEWORK || NET45_OR_GREATER
using System;
#endif


namespace PowerKit;

/// <summary>
/// Provides methods for computing CRC-32 checksums.
/// </summary>
public static class Crc32
{
private static readonly uint[] Table = GenerateTable();

private static uint[] GenerateTable()
{
var table = new uint[256];

for (var i = 0; i < 256; i++)
{
var entry = (uint)i;
for (var j = 0; j < 8; j++)
{
if ((entry & 1) != 0)
entry = (entry >> 1) ^ 0xEDB88320u;
else
entry >>= 1;
}

table[i] = entry;
}

return table;
}

/// <summary>
/// Computes the CRC-32 checksum of data read from the specified stream.
/// </summary>
public static uint Hash(Stream stream)
{
var crc = 0xFFFFFFFFu;

var buffer = new byte[4096];
int read;

while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
{
for (var i = 0; i < read; i++)
crc = (crc >> 8) ^ Table[(byte)(crc ^ buffer[i])];
}

return crc ^ 0xFFFFFFFFu;
}

/// <summary>
/// Computes the CRC-32 checksum of the specified data.
/// </summary>
public static uint Hash(byte[] data)
{
using var stream = new MemoryStream(data);
return Hash(stream);
}

#if !NETFRAMEWORK || NET45_OR_GREATER
/// <summary>
/// Computes the CRC-32 checksum of the specified data.
/// </summary>
public static uint Hash(ReadOnlySpan<byte> data)
{
var crc = 0xFFFFFFFFu;
for (var i = 0; i < data.Length; i++)
crc = (crc >> 8) ^ Table[(byte)(crc ^ data[i])];
return crc ^ 0xFFFFFFFFu;
}
#endif
}
Loading
Loading