File indexing completed on 2024-12-22 04:11:59

0001 /*
0002  * SPDX-FileCopyrightText: 2009 Boudewijn Rempt <boud@valdyas.org>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "compression_test.h"
0008 
0009 #include <QBuffer>
0010 #include <QByteArray>
0011 #include <QCoreApplication>
0012 #include <QDataStream>
0013 #include <cmath>
0014 #include <klocalizedstring.h>
0015 
0016 #include <compression.h>
0017 #include <kis_debug.h>
0018 #include <simpletest.h>
0019 
0020 void CompressionTest::testCompressionRLE()
0021 {
0022     QByteArray ba("Twee eeee aaaaa asdasda47892347981    wwwwwwwwwwwwWWWWWWWWWW");
0023     QByteArray compressed = Compression::compress(ba, psd_compression_type::RLE);
0024     QVERIFY(compressed.size() > 0);
0025     dbgKrita << compressed.size() << "uncompressed" << ba.size();
0026 
0027     QByteArray uncompressed = Compression::uncompress(ba.size(), compressed, psd_compression_type::RLE);
0028     QVERIFY(uncompressed.size() > 0);
0029     QVERIFY(qstrcmp(ba, uncompressed) == 0);
0030 
0031     ba.clear();
0032     QDataStream ds(&ba, QIODevice::WriteOnly);
0033     for (int i = 0; i < 500; ++i) {
0034         ds << rand();
0035     }
0036     compressed = Compression::compress(ba, psd_compression_type::RLE);
0037     dbgKrita << compressed.size() << "uncompressed" << ba.size();
0038     QVERIFY(compressed.size() > 0);
0039     uncompressed = Compression::uncompress(ba.size(), compressed, psd_compression_type::RLE);
0040     QVERIFY(uncompressed.size() > 0);
0041     QVERIFY(qstrcmp(ba, uncompressed) == 0);
0042 }
0043 
0044 void CompressionTest::testCompressionZIP()
0045 {
0046     QByteArray ba("Twee eeee aaaaa asdasda47892347981    wwwwwwwwwwwwWWWWWWWWWW");
0047     QByteArray compressed = Compression::compress(ba, psd_compression_type::ZIP);
0048     QVERIFY(compressed.size() > 0);
0049     dbgKrita << compressed.size() << "uncompressed" << ba.size();
0050 
0051     QByteArray uncompressed = Compression::uncompress(ba.size(), compressed, psd_compression_type::ZIP);
0052     QVERIFY(uncompressed.size() > 0);
0053     QVERIFY(qstrcmp(ba, uncompressed) == 0);
0054 
0055     ba.clear();
0056     QDataStream ds(&ba, QIODevice::WriteOnly);
0057     for (int i = 0; i < 500; ++i) {
0058         ds << rand();
0059     }
0060     compressed = Compression::compress(ba, psd_compression_type::ZIP);
0061     QVERIFY(compressed.size() > 0);
0062     dbgKrita << compressed.size() << "uncompressed" << ba.size();
0063     uncompressed = Compression::uncompress(ba.size(), compressed, psd_compression_type::ZIP);
0064     QVERIFY(uncompressed.size() > 0);
0065     QVERIFY(qstrcmp(ba, uncompressed) == 0);
0066 }
0067 
0068 void CompressionTest::testCompressionUncompressed()
0069 {
0070     QByteArray ba("Twee eeee aaaaa asdasda47892347981    wwwwwwwwwwwwWWWWWWWWWW");
0071     QByteArray compressed = Compression::compress(ba, psd_compression_type::Uncompressed);
0072     QVERIFY(compressed.size() > 0);
0073     dbgKrita << compressed.size() << "uncompressed" << ba.size();
0074 
0075     QByteArray uncompressed = Compression::uncompress(ba.size(), compressed, psd_compression_type::Uncompressed);
0076     QVERIFY(uncompressed.size() > 0);
0077     QVERIFY(qstrcmp(ba, uncompressed) == 0);
0078 
0079     ba.clear();
0080     QDataStream ds(&ba, QIODevice::WriteOnly);
0081     for (int i = 0; i < 500; ++i) {
0082         ds << rand();
0083     }
0084     compressed = Compression::compress(ba, psd_compression_type::Uncompressed);
0085     dbgKrita << compressed.size() << "uncompressed" << ba.size();
0086     QVERIFY(compressed.size() > 0);
0087     uncompressed = Compression::uncompress(ba.size(), compressed, psd_compression_type::Uncompressed);
0088     QVERIFY(uncompressed.size() > 0);
0089     QVERIFY(qstrcmp(ba, uncompressed) == 0);
0090 }
0091 
0092 SIMPLE_TEST_MAIN(CompressionTest)