diff --git a/Tests/Public/5. Hello Loop/Main.cpp b/Tests/Public/5. Hello Loop/Main.cpp index baf1a27..fcdbb6d 100644 --- a/Tests/Public/5. Hello Loop/Main.cpp +++ b/Tests/Public/5. Hello Loop/Main.cpp @@ -15,7 +15,6 @@ TEST(Loop, Semaphore) auto semac = Aurora::Loop::NewLSSemaphore(0); auto loop = Aurora::Loop::NewLoopQueue(); - // Add initial loop sources ASSERT_TRUE(loop->SourceAdd(semaA)); ASSERT_TRUE(loop->SourceAdd(semaB)); diff --git a/Tests/Public/6. Hello Compression/Main.cpp b/Tests/Public/6. Hello Compression/Main.cpp new file mode 100644 index 0000000..562aef5 --- /dev/null +++ b/Tests/Public/6. Hello Compression/Main.cpp @@ -0,0 +1,185 @@ +/*** + Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved. + + File: Main.cpp + Date: 2022-2-18 + Author: Reece +***/ +#include +#include + +static const unsigned char compressMe[779] +{ + 0x2F, 0x2A, 0x2A, 0x2A, 0x0D, 0x0A, 0x20, 0x20, 0x20, 0x20, 0x43, 0x6F, + 0x70, 0x79, 0x72, 0x69, 0x67, 0x68, 0x74, 0x20, 0x28, 0x43, 0x29, 0x20, + 0x32, 0x30, 0x32, 0x31, 0x20, 0x4A, 0x20, 0x52, 0x65, 0x65, 0x63, 0x65, + 0x20, 0x57, 0x69, 0x6C, 0x73, 0x6F, 0x6E, 0x20, 0x28, 0x61, 0x2F, 0x6B, + 0x2F, 0x61, 0x20, 0x22, 0x52, 0x65, 0x65, 0x63, 0x65, 0x22, 0x29, 0x2E, + 0x20, 0x41, 0x6C, 0x6C, 0x20, 0x72, 0x69, 0x67, 0x68, 0x74, 0x73, 0x20, + 0x72, 0x65, 0x73, 0x65, 0x72, 0x76, 0x65, 0x64, 0x2E, 0x0D, 0x0A, 0x0D, + 0x0A, 0x20, 0x20, 0x20, 0x20, 0x46, 0x69, 0x6C, 0x65, 0x3A, 0x20, 0x4F, + 0x70, 0x65, 0x6E, 0x2E, 0x55, 0x6E, 0x69, 0x78, 0x2E, 0x63, 0x70, 0x70, + 0x0D, 0x0A, 0x20, 0x20, 0x20, 0x20, 0x44, 0x61, 0x74, 0x65, 0x3A, 0x20, + 0x32, 0x30, 0x32, 0x31, 0x2D, 0x36, 0x2D, 0x31, 0x32, 0x0D, 0x0A, 0x20, + 0x20, 0x20, 0x20, 0x41, 0x75, 0x74, 0x68, 0x6F, 0x72, 0x3A, 0x20, 0x52, + 0x65, 0x65, 0x63, 0x65, 0x0D, 0x0A, 0x2A, 0x2A, 0x2A, 0x2F, 0x0D, 0x0A, + 0x23, 0x70, 0x72, 0x61, 0x67, 0x6D, 0x61, 0x20, 0x6F, 0x6E, 0x63, 0x65, + 0x0D, 0x0A, 0x23, 0x69, 0x6E, 0x63, 0x6C, 0x75, 0x64, 0x65, 0x20, 0x3C, + 0x52, 0x75, 0x6E, 0x74, 0x69, 0x6D, 0x65, 0x49, 0x6E, 0x74, 0x65, 0x72, + 0x6E, 0x61, 0x6C, 0x2E, 0x68, 0x70, 0x70, 0x3E, 0x0D, 0x0A, 0x23, 0x69, + 0x6E, 0x63, 0x6C, 0x75, 0x64, 0x65, 0x20, 0x22, 0x50, 0x72, 0x6F, 0x63, + 0x65, 0x73, 0x73, 0x65, 0x73, 0x2E, 0x68, 0x70, 0x70, 0x22, 0x0D, 0x0A, + 0x23, 0x69, 0x6E, 0x63, 0x6C, 0x75, 0x64, 0x65, 0x20, 0x22, 0x4F, 0x70, + 0x65, 0x6E, 0x2E, 0x55, 0x6E, 0x69, 0x78, 0x2E, 0x68, 0x70, 0x70, 0x22, + 0x0D, 0x0A, 0x0D, 0x0A, 0x23, 0x69, 0x6E, 0x63, 0x6C, 0x75, 0x64, 0x65, + 0x20, 0x3C, 0x75, 0x6E, 0x69, 0x73, 0x74, 0x64, 0x2E, 0x68, 0x3E, 0x0D, + 0x0A, 0x23, 0x69, 0x6E, 0x63, 0x6C, 0x75, 0x64, 0x65, 0x20, 0x3C, 0x53, + 0x6F, 0x75, 0x72, 0x63, 0x65, 0x2F, 0x49, 0x4F, 0x2F, 0x46, 0x53, 0x2F, + 0x46, 0x53, 0x2E, 0x68, 0x70, 0x70, 0x3E, 0x0D, 0x0A, 0x0D, 0x0A, 0x6E, + 0x61, 0x6D, 0x65, 0x73, 0x70, 0x61, 0x63, 0x65, 0x20, 0x41, 0x75, 0x72, + 0x6F, 0x72, 0x61, 0x3A, 0x3A, 0x50, 0x72, 0x6F, 0x63, 0x65, 0x73, 0x73, + 0x65, 0x73, 0x0D, 0x0A, 0x7B, 0x0D, 0x0A, 0x20, 0x20, 0x20, 0x20, 0x73, + 0x74, 0x61, 0x74, 0x69, 0x63, 0x20, 0x76, 0x6F, 0x69, 0x64, 0x20, 0x55, + 0x6E, 0x69, 0x78, 0x4F, 0x70, 0x65, 0x6E, 0x41, 0x73, 0x79, 0x6E, 0x63, + 0x28, 0x63, 0x6F, 0x6E, 0x73, 0x74, 0x20, 0x41, 0x75, 0x53, 0x74, 0x72, + 0x69, 0x6E, 0x67, 0x20, 0x26, 0x6F, 0x70, 0x65, 0x6E, 0x29, 0x0D, 0x0A, + 0x20, 0x20, 0x20, 0x20, 0x7B, 0x0D, 0x0A, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x2F, 0x2F, 0x20, 0x54, 0x4F, 0x44, 0x4F, 0x3A, 0x20, + 0x6D, 0x61, 0x63, 0x20, 0x6F, 0x73, 0x20, 0x69, 0x73, 0x20, 0x73, 0x70, + 0x65, 0x63, 0x69, 0x61, 0x6C, 0x0D, 0x0A, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x69, 0x66, 0x20, 0x28, 0x66, 0x6F, 0x72, 0x6B, 0x28, + 0x29, 0x20, 0x3D, 0x3D, 0x20, 0x30, 0x29, 0x0D, 0x0A, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x7B, 0x0D, 0x0A, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x73, 0x65, 0x74, 0x73, + 0x69, 0x64, 0x28, 0x29, 0x3B, 0x0D, 0x0A, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x65, 0x78, 0x65, 0x63, 0x6C, + 0x28, 0x22, 0x78, 0x64, 0x67, 0x2D, 0x6F, 0x70, 0x65, 0x6E, 0x22, 0x2C, + 0x20, 0x6F, 0x70, 0x65, 0x6E, 0x2E, 0x63, 0x5F, 0x73, 0x74, 0x72, 0x28, + 0x29, 0x29, 0x3B, 0x0D, 0x0A, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x7D, 0x0D, 0x0A, 0x20, 0x20, 0x20, 0x20, 0x7D, 0x0D, 0x0A, 0x0D, + 0x0A, 0x20, 0x20, 0x20, 0x20, 0x41, 0x55, 0x4B, 0x4E, 0x5F, 0x53, 0x59, + 0x4D, 0x20, 0x76, 0x6F, 0x69, 0x64, 0x20, 0x4F, 0x70, 0x65, 0x6E, 0x55, + 0x72, 0x69, 0x28, 0x63, 0x6F, 0x6E, 0x73, 0x74, 0x20, 0x41, 0x75, 0x53, + 0x74, 0x72, 0x69, 0x6E, 0x67, 0x20, 0x26, 0x75, 0x72, 0x69, 0x29, 0x0D, + 0x0A, 0x20, 0x20, 0x20, 0x20, 0x7B, 0x0D, 0x0A, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x55, 0x6E, 0x69, 0x78, 0x4F, 0x70, 0x65, 0x6E, + 0x41, 0x73, 0x79, 0x6E, 0x63, 0x28, 0x75, 0x72, 0x69, 0x29, 0x3B, 0x0D, + 0x0A, 0x20, 0x20, 0x20, 0x20, 0x7D, 0x0D, 0x0A, 0x0D, 0x0A, 0x20, 0x20, + 0x20, 0x20, 0x41, 0x55, 0x4B, 0x4E, 0x5F, 0x53, 0x59, 0x4D, 0x20, 0x76, + 0x6F, 0x69, 0x64, 0x20, 0x4F, 0x70, 0x65, 0x6E, 0x46, 0x69, 0x6C, 0x65, + 0x28, 0x63, 0x6F, 0x6E, 0x73, 0x74, 0x20, 0x41, 0x75, 0x53, 0x74, 0x72, + 0x69, 0x6E, 0x67, 0x20, 0x26, 0x66, 0x69, 0x6C, 0x65, 0x29, 0x0D, 0x0A, + 0x20, 0x20, 0x20, 0x20, 0x7B, 0x0D, 0x0A, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x55, 0x6E, 0x69, 0x78, 0x4F, 0x70, 0x65, 0x6E, 0x41, + 0x73, 0x79, 0x6E, 0x63, 0x28, 0x49, 0x4F, 0x3A, 0x3A, 0x46, 0x53, 0x3A, + 0x3A, 0x4E, 0x6F, 0x72, 0x6D, 0x61, 0x6C, 0x69, 0x7A, 0x65, 0x50, 0x61, + 0x74, 0x68, 0x52, 0x65, 0x74, 0x28, 0x66, 0x69, 0x6C, 0x65, 0x29, 0x29, + 0x3B, 0x0D, 0x0A, 0x20, 0x20, 0x20, 0x20, 0x7D, 0x0D, 0x0A, 0x7D +}; + +static AuByteBuffer GetCompressableBlob() +{ + AuByteBuffer compressMeBytes; + + for (int i = 0; i < 1000; i++) + { + compressMeBytes.Write(compressMe, AuArraySize(compressMe)); + } + + return compressMeBytes; +} + +static void TestBasicCompression(AuCompression::ECompresionType type) +{ + auto compressMeBytes = GetCompressableBlob(); + + AuIO::Buffered::BlobWriter compressed; + { + AuIO::Buffered::BlobReader reader(AuMove(compressMeBytes)); + AuCompression::CompressionPipe pipe; + AuCompression::CompressionInfo info; + + info.type = type; + pipe.inPipe = [&](void *buf, AuUInt len) -> AuUInt + { + AuUInt len2 = len; + if (reader.Read(AuMemory::MemoryViewStreamWrite(buf, len2)) != AuIO::EStreamError::eErrorNone) + { + return {}; + } + return len2; + }; + + pipe.writePipe = [&](const void *buf, AuUInt len) + { + compressed.Write(AuMemory::MemoryViewStreamRead(buf, len)); + }; + info.compressionLevel = 4; + ASSERT_TRUE(AuCompression::Compress(pipe, info)); + } + + AuIO::Buffered::BlobWriter decompressed; + { + AuIO::Buffered::BlobReader reader(compressed.GetBuffer()); + AuCompression::CompressionPipe pipe; + + pipe.inPipe = [&](void *buf, AuUInt len) -> AuUInt + { + AuUInt len2 = len; + if (reader.Read(AuMemory::MemoryViewStreamWrite(buf, len2)) != AuIO::EStreamError::eErrorNone) + { + return {}; + } + return len2; + }; + + pipe.writePipe = [&](const void *buf, AuUInt len) + { + decompressed.Write(AuMemory::MemoryViewStreamRead(buf, len)); + }; + + ASSERT_TRUE(AuCompression::Decompress(pipe, {type, 1024 * 64 * 2})); + } + + compressed.GetBuffer()->ResetReadPointer(); + + ASSERT_EQ(compressMeBytes, *decompressed.GetBuffer()); + + { + auto decompressLength = decompressed.GetBuffer()->size(); + + AuCompression::DecompressInfo decompressStream {type}; + decompressStream.internalStreamSize = decompressLength; + + AuIO::Buffered::BlobReader reader(compressed.GetBuffer()); + auto decompressor = AuCompression::DecompressorShared(AuUnsafeRaiiToShared(&reader), decompressStream); + + auto readWrittenBytes = decompressor->Ingest(decompressLength); + ASSERT_EQ(readWrittenBytes, AuMakePair(AuUInt32(compressed.GetBuffer()->size()), AuUInt32(decompressLength))); + + AuByteBuffer inflated(decompressLength); + ASSERT_TRUE(decompressor->Read(AuMemoryViewWrite(inflated.data(), inflated.size()))); + + ASSERT_EQ(inflated, *decompressed.GetBuffer()); + } + + compressed.GetBuffer()->ResetReadPointer(); +} + +#define ADD_TESTS_BASIC(algorithm) \ +TEST(BasicCompression, algorithm) \ +{ \ + TestBasicCompression(AuCompression::ECompresionType::e ## algorithm); \ +} + +#define ADD_TESTS(...) AU_FOR_EACH(ADD_TESTS_BASIC, __VA_ARGS__) + +ADD_TESTS(LZ4, ZSTD, BZIP2, Deflate, Zip, GZip); + +void RunTests() +{ + Aurora::RuntimeStartInfo info; + info.console.fio.enableLogging = false; + info.console.forceToolKitWindow = false; + Aurora::RuntimeStart(info); +} \ No newline at end of file