stuff-from-scratch/test/compression/TestStreamCompressor.cpp

63 lines
1.5 KiB
C++
Raw Normal View History

2022-08-01 15:14:14 +00:00
#include <iostream>
2022-11-23 15:41:33 +00:00
#include "BufferBitStream.h"
2022-08-01 15:14:14 +00:00
#include "HuffmanEncoder.h"
2022-11-21 17:45:12 +00:00
#include "RunLengthEncoder.h"
#include "Lz77Encoder.h"
2022-08-01 15:14:14 +00:00
2022-11-29 18:00:19 +00:00
#include "StringUtils.h"
#include "TestFramework.h"
TEST_CASE(TestRunLengthEncoder, "compression")
2022-11-21 17:45:12 +00:00
{
std::string test_data = "BCAAAADDDCCACACAC";
RunLengthEncoder encoder;
2022-11-29 18:00:19 +00:00
auto encoded = encoder.encode(StringUtils::toBytes(test_data));
2022-11-21 17:45:12 +00:00
2022-11-29 18:00:19 +00:00
//std::cout << "Encoded: " << encoded << std::endl;
2022-11-21 17:45:12 +00:00
auto decoded = encoder.decode(encoded);
2022-11-29 18:00:19 +00:00
//std::cout << "Decoded: " << decoded << std::endl;
2022-11-21 17:45:12 +00:00
}
2022-11-29 18:00:19 +00:00
TEST_CASE(TestHuffmanEncoder, "compression")
2022-08-01 15:14:14 +00:00
{
2022-11-21 17:45:12 +00:00
//std::string testData = "BCAADDDCCACACAC";
//std::vector<unsigned char> stream(testData.begin(), testData.end());
std::unordered_map<unsigned char, unsigned> counts;
counts['A'] = 1;
counts['B'] = 1;
counts['C'] = 1;
counts['D'] = 2;
counts['E'] = 3;
counts['F'] = 5;
counts['G'] = 5;
counts['H'] = 12;
2022-08-01 15:14:14 +00:00
2022-11-21 17:45:12 +00:00
HuffmanEncoder encoder;
encoder.encode(counts);
}
2022-11-29 18:00:19 +00:00
TEST_CASE(TestLz77Encoder, "compression")
{
std::string test_data = "sir sid eastman easily teases sea sick seals";
//std::string test_data = "sir sid eastman";
2022-11-23 15:41:33 +00:00
BufferBitStream input_stream;
input_stream.setBuffer(StringUtils::toBytes(test_data));
2022-11-21 17:45:12 +00:00
2022-11-23 15:41:33 +00:00
BufferBitStream output_stream;
Lz77Encoder encoder(&input_stream, &output_stream);
encoder.encode();
2022-11-30 18:28:50 +00:00
//std::cout << "Encoded: " << StringUtils::toString(output_stream.getBuffer()) << std::endl;
//auto decoded = encoder.decode(encoded);
//std::cout << "Decoded: " << decoded << std::endl;
2022-11-21 17:45:12 +00:00
}