/
redgpu
/
ezEngine
Обзор
Документация
Войти
/
redgpu
/
ezEngine
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
dev
Code/UnitTests/FoundationTest/IO/CompressedStreamZlibTest.cpp
121 строка
3 KB
Sanakan8472
Zlib 1.3.1 and archive reader support re-added (#1243)
18 мар 2024, 19:28
Не верифицирован
18 мар 2024, 19:28
4cb862a
Код
Авторство
О чём код?
#include <FoundationTest/FoundationTestPCH.h> #include <Foundation/IO/CompressedStreamZlib.h> #include <Foundation/IO/MemoryStream.h> #include <Foundation/IO/Stream.h> #ifdef BUILDSYSTEM_ENABLE_ZLIB_SUPPORT EZ_CREATE_SIMPLE_TEST(IO, CompressedStreamZlib) { ezDynamicArray<ezUInt32> TestData; // create the test data // a repetition of a counting sequence that is getting longer and longer, ie: // 0, 0,1, 0,1,2, 0,1,2,3, 0,1,2,3,4, ... { TestData.SetCountUninitialized(1024 * 1024 * 8); const ezUInt32 uiItems = TestData.GetCount(); ezUInt32 uiStartPos = 0; for (ezUInt32 uiWrite = 1; uiWrite < uiItems; ++uiWrite) { uiWrite = ezMath::Min(uiWrite, uiItems - uiStartPos); if (uiWrite == 0) break; for (ezUInt32 i = 0; i < uiWrite; ++i) { TestData[uiStartPos + i] = i; } uiStartPos += uiWrite; } } ezDefaultMemoryStreamStorage StreamStorage; ezMemoryStreamWriter MemoryWriter(&StreamStorage); ezMemoryStreamReader MemoryReader(&StreamStorage); ezCompressedStreamReaderZlib CompressedReader(&MemoryReader); ezCompressedStreamWriterZlib CompressedWriter(&MemoryWriter); const float fExpectedCompressionRatio = 25.0f; // this is a guess that is based on the current input data and size EZ_TEST_BLOCK(ezTestBlock::Enabled, "Compress Data") { ezUInt32 uiWrite = 1; for (ezUInt32 i = 0; i < TestData.GetCount();) { uiWrite = ezMath::Min<ezUInt32>(uiWrite, TestData.GetCount() - i); EZ_TEST_BOOL(CompressedWriter.WriteBytes(&TestData[i], sizeof(ezUInt32) * uiWrite) == EZ_SUCCESS); i += uiWrite; uiWrite += 17; // try different sizes to write } // flush all data CompressedWriter.CloseStream().IgnoreResult(); const ezUInt64 uiCompressed = CompressedWriter.GetCompressedSize(); const ezUInt64 uiUncompressed = CompressedWriter.GetUncompressedSize(); EZ_TEST_INT(uiUncompressed, TestData.GetCount() * sizeof(ezUInt32)); EZ_TEST_BOOL((float)uiCompressed <= (float)uiUncompressed / fExpectedCompressionRatio); } EZ_TEST_BLOCK(ezTestBlock::Enabled, "Uncompress Data") { bool bSkip = false; ezUInt32 uiStartPos = 0; ezDynamicArray<ezUInt32> TestDataRead = TestData; // initialize with identical data, makes comparing the skipped parts easier // read the data in blocks that get larger and larger for (ezUInt32 iRead = 1; iRead < TestData.GetCount(); ++iRead) { ezUInt32 iToRead = ezMath::Min(iRead, TestData.GetCount() - uiStartPos); if (iToRead == 0) break; if (bSkip) { const ezUInt64 uiReadFromStream = CompressedReader.SkipBytes(sizeof(ezUInt32) * iToRead); EZ_TEST_BOOL(uiReadFromStream == sizeof(ezUInt32) * iToRead); } else { // overwrite part we are going to read from the stream, to make sure it re-reads the correct data for (ezUInt32 i = 0; i < iToRead; ++i) { TestDataRead[uiStartPos + i] = 0; } const ezUInt64 uiReadFromStream = CompressedReader.ReadBytes(&TestDataRead[uiStartPos], sizeof(ezUInt32) * iToRead); EZ_TEST_BOOL(uiReadFromStream == sizeof(ezUInt32) * iToRead); } bSkip = !bSkip; uiStartPos += iToRead; } EZ_TEST_BOOL(TestData == TestDataRead); // test reading after the end of the stream for (ezUInt32 i = 0; i < 1000; ++i) { ezUInt32 uiTemp = 0; EZ_TEST_BOOL(CompressedReader.ReadBytes(&uiTemp, sizeof(ezUInt32)) == 0); } } } #endif