calc: on editing invalidation of view with different zoom is wrong
[LibreOffice.git] / tools / qa / cppunit / test_zcodec.cxx
blob69c4aeb2429c1c81c6e274ae9bd1fef804b8bd70
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include <tools/zcodec.hxx>
11 #include <tools/stream.hxx>
12 #include <rtl/crc.h>
13 #include <cppunit/TestAssert.h>
14 #include <cppunit/TestFixture.h>
15 #include <cppunit/extensions/HelperMacros.h>
16 #include <cppunit/plugin/TestPlugIn.h>
18 namespace
20 // Sample text for compression
21 constexpr const char* DUMMY_TEXT
22 = "Lorem ipsum dolor sit amet, consectetur adipiscing elit,\n"
23 "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n"
24 "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
25 "commodo consequat.\n";
26 constexpr auto DUMMY_SIZE = std::char_traits<char>::length(DUMMY_TEXT);
28 class ZCodecTest : public CppUnit::TestFixture
30 private:
31 void testGzCompressDecompress();
32 void testMakeDummyFile();
33 void testZlibCompressDecompress();
35 CPPUNIT_TEST_SUITE(ZCodecTest);
36 CPPUNIT_TEST(testMakeDummyFile);
37 CPPUNIT_TEST(testGzCompressDecompress);
38 CPPUNIT_TEST(testZlibCompressDecompress);
39 CPPUNIT_TEST_SUITE_END();
42 // Creates a stream from DUMMY_TEXT to compress and decompress
43 std::unique_ptr<SvMemoryStream> makeDummyFile()
45 SvMemoryStream* pRet = new SvMemoryStream();
46 pRet->WriteBytes(DUMMY_TEXT, DUMMY_SIZE);
47 return std::unique_ptr<SvMemoryStream>(pRet);
50 // Test that the stream generated from makeDummyFile is valid
51 void ZCodecTest::testMakeDummyFile()
53 auto pStream = makeDummyFile();
54 // Check for null
55 CPPUNIT_ASSERT_MESSAGE("pStream is null", pStream);
56 decltype(DUMMY_SIZE) size = pStream->GetSize();
57 // Check size
58 CPPUNIT_ASSERT_EQUAL(DUMMY_SIZE, size);
61 // Test that ZCodec::Compress and ZCodec::Decompress work for gzlib = true
62 // Compares the CRC32 checksum of the initial stream and the compressed->decompressed stream
63 void ZCodecTest::testGzCompressDecompress()
65 auto pInitialStream = makeDummyFile();
66 SvMemoryStream pCompressedStream;
67 SvMemoryStream pDecompressedStream;
68 auto nInitialStreamCRC32 = rtl_crc32(0, pInitialStream->GetData(), pInitialStream->GetSize());
69 ZCodec aCodec;
70 aCodec.BeginCompression(ZCODEC_DEFAULT_COMPRESSION, true);
71 // Set compression metadata for compressing a GZ file
72 aCodec.SetCompressionMetadata("dummy.txt", 0, nInitialStreamCRC32);
73 aCodec.Compress(*pInitialStream, pCompressedStream);
74 auto nCompressedSize
75 = aCodec.EndCompression(); // returns compressed size or -1 if compression failed
76 // Check that the compression succeeded
77 CPPUNIT_ASSERT_GREATER(static_cast<tools::Long>(0), nCompressedSize);
78 // No need to Seek(0) here because ZCodec::InitDecompress does that already for gz
79 aCodec.BeginCompression(ZCODEC_DEFAULT_COMPRESSION, true);
80 aCodec.Decompress(pCompressedStream, pDecompressedStream);
81 auto nDecompressedSize
82 = aCodec.EndCompression(); // returns decompressed size or -1 if decompression failed
83 // Check that the decompressed text size is equal to the original
84 CPPUNIT_ASSERT_EQUAL(static_cast<tools::Long>(DUMMY_SIZE), nDecompressedSize);
85 auto nCompressedDecompressedStreamCRC32
86 = rtl_crc32(0, pDecompressedStream.GetData(), pDecompressedStream.GetSize());
87 // Check that the initial and decompressed CRC32 checksums are the same -> gz (de)compression works
88 CPPUNIT_ASSERT_EQUAL(nInitialStreamCRC32, nCompressedDecompressedStreamCRC32);
91 // Test that ZCodec::Compress and ZCodec::Decompress work for gzlib = false
92 // Compares the CRC32 checksum of the initial stream and the compressed->decompressed stream
93 void ZCodecTest::testZlibCompressDecompress()
95 auto pInitialStream = makeDummyFile();
96 SvMemoryStream pCompressedStream;
97 SvMemoryStream pDecompressedStream;
98 auto nInitialStreamCRC32 = rtl_crc32(0, pInitialStream->GetData(), pInitialStream->GetSize());
99 ZCodec aCodec;
100 aCodec.BeginCompression(ZCODEC_DEFAULT_COMPRESSION, false);
101 aCodec.Compress(*pInitialStream, pCompressedStream);
102 auto nCompressedSize
103 = aCodec.EndCompression(); // returns compressed size or -1 if compression failed
104 // Check that the compression succeeded
105 CPPUNIT_ASSERT_GREATER(static_cast<tools::Long>(0), nCompressedSize);
106 pCompressedStream.Seek(0);
107 aCodec.BeginCompression(ZCODEC_DEFAULT_COMPRESSION, false);
108 aCodec.Decompress(pCompressedStream, pDecompressedStream);
109 auto nDecompressedSize
110 = aCodec.EndCompression(); // returns decompressed size or -1 if decompression failed
111 // Check that the decompressed text size is equal to the original
112 CPPUNIT_ASSERT_EQUAL(static_cast<tools::Long>(DUMMY_SIZE), nDecompressedSize);
113 auto nCompressedDecompressedStreamCRC32
114 = rtl_crc32(0, pDecompressedStream.GetData(), pDecompressedStream.GetSize());
115 // Check that the initial and decompressed CRC32 checksums are the same -> zlib (de)compression works
116 CPPUNIT_ASSERT_EQUAL(nInitialStreamCRC32, nCompressedDecompressedStreamCRC32);
119 } // namespace
121 CPPUNIT_TEST_SUITE_REGISTRATION(ZCodecTest);
123 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */