-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathHuffmanCompressorTests.cs
61 lines (52 loc) · 2.09 KB
/
HuffmanCompressorTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using Algorithms.DataCompression;
using Algorithms.Sorters.Comparison;
using FluentAssertions;
using NUnit.Framework;
using NUnit.Framework.Internal;
namespace Algorithms.Tests.Compressors;
public static class HuffmanCompressorTests
{
[TestCase("This is a string", "101010110111011101110111100011111010010010010011000")]
[TestCase("Hello", "1101110010")]
[TestCase("dddddddddd", "1111111111")]
[TestCase("a", "1")]
[TestCase("", "")]
public static void CompressingPhrase(string uncompressedText, string expectedCompressedText)
{
//Arrange
var sorter = new BubbleSorter<HuffmanCompressor.ListNode>();
var translator = new Translator();
var huffman = new HuffmanCompressor(sorter, translator);
//Act
var (compressedText, decompressionKeys) = huffman.Compress(uncompressedText);
var decompressedText = translator.Translate(compressedText, decompressionKeys);
//Assert
Assert.That(compressedText, Is.EqualTo(expectedCompressedText));
Assert.That(decompressedText, Is.EqualTo(uncompressedText));
}
[Test]
public static void DecompressedTextTheSameAsOriginal(
[Random(0, 1000, 100, Distinct = true)]
int length)
{
//Arrange
var sorter = new BubbleSorter<HuffmanCompressor.ListNode>();
var translator = new Translator();
var huffman = new HuffmanCompressor(sorter, translator);
var text = Randomizer.CreateRandomizer().GetString(length);
//Act
var (compressedText, decompressionKeys) = huffman.Compress(text);
var decompressedText = translator.Translate(compressedText, decompressionKeys);
//Assert
Assert.That(decompressedText, Is.EqualTo(text));
}
[Test]
public static void ListNodeComparer_NullIsUnordered()
{
var comparer = new HuffmanCompressor.ListNodeComparer();
var node = new HuffmanCompressor.ListNode('a', 0.1);
comparer.Compare(node, null).Should().Be(0);
comparer.Compare(null, node).Should().Be(0);
comparer.Compare(null, null).Should().Be(0);
}
}