KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_embedded_file_compress.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 1992-2021 KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <magic_enum.hpp>
21#include <boost/test/unit_test.hpp>
22#include <mmh3_hash.h>
23#include <embedded_files.h>
24
25#include <random>
26using magic_enum::iostream_operators::operator<<;
27
28BOOST_AUTO_TEST_SUITE( EmbeddedFiles )
29
30BOOST_AUTO_TEST_CASE( CompressAndEncode_OK )
31{
33 file.name = "test_file";
34 std::string data = "Hello, World!";
35 file.decompressedData.assign(data.begin(), data.end());
36
38 hash.add( file.decompressedData );
39 file.data_hash = hash.digest().ToString();
40
42 BOOST_CHECK_EQUAL(result, EMBEDDED_FILES::RETURN_CODE::OK);
43}
44
45BOOST_AUTO_TEST_CASE( DecompressAndDecode_OK )
46{
48 file.name = "test_file";
49 std::string data = "Hello, World!";
50 file.decompressedData.assign( data.begin(), data.end() );
51
53 hash.add( file.decompressedData );
54 file.data_hash = hash.digest().ToString();
55
57 BOOST_CHECK_EQUAL( result, EMBEDDED_FILES::RETURN_CODE::OK );
58
60 BOOST_CHECK_EQUAL( result, EMBEDDED_FILES::RETURN_CODE::OK );
61
62 // Create a large test data
63 data.clear();
64 data.reserve( 13 * 100000 + 1 );
65
66 for( int i = 0; i < 100000; ++i )
67 data += "Hello, World!";
68
69 file.decompressedData.assign( data.begin(), data.end() );
70
71 hash.reset();
72 hash.add( file.decompressedData );
73 file.data_hash = hash.digest().ToString();
74
75 result = EMBEDDED_FILES::CompressAndEncode( file );
76 BOOST_CHECK_EQUAL( result, EMBEDDED_FILES::RETURN_CODE::OK );
77
79 BOOST_CHECK_EQUAL( result, EMBEDDED_FILES::RETURN_CODE::OK );
80
81 // Create a sequential test dataset
82 data.clear();
83 data.reserve( 100000 );
84
85 for( int i = 0; i < 100000; ++i )
86 data += static_cast<char>( i % 256 );
87
88 file.decompressedData.assign( data.begin(), data.end() );
89 hash.reset();
90 hash.add( file.decompressedData );
91 file.data_hash = hash.digest().ToString();
92
93 result = EMBEDDED_FILES::CompressAndEncode( file );
94 BOOST_CHECK_EQUAL( result, EMBEDDED_FILES::RETURN_CODE::OK );
95
97 BOOST_CHECK_EQUAL( result, EMBEDDED_FILES::RETURN_CODE::OK );
98
99 // Create a random test dataset with a known seed
100 data.clear();
101 data.reserve( 100000 );
102
103 std::mt19937 rng;
104 rng.seed( 0 );
105
106 for( int i = 0; i < 100000; ++i )
107 data += static_cast<char>( rng() % 256 );
108
109 file.decompressedData.assign( data.begin(), data.end() );
110 hash.reset();
111 hash.add( file.decompressedData );
112 file.data_hash = hash.digest().ToString();
113
114 result = EMBEDDED_FILES::CompressAndEncode( file );
115 BOOST_CHECK_EQUAL( result, EMBEDDED_FILES::RETURN_CODE::OK );
116
118 BOOST_CHECK_EQUAL( result, EMBEDDED_FILES::RETURN_CODE::OK );
119
120}
121
122BOOST_AUTO_TEST_CASE( DecompressAndDecode_ChecksumError )
123{
125 file.name = "test_file";
126 std::string data = "Hello, World!";
127 file.decompressedData.assign(data.begin(), data.end());
128
130 BOOST_CHECK_EQUAL(result, EMBEDDED_FILES::RETURN_CODE::OK);
131
132 // Modify the checksum
133 file.data_hash[0] = 'x';
134
136 BOOST_CHECK_EQUAL(result, EMBEDDED_FILES::RETURN_CODE::CHECKSUM_ERROR);
137}
138
static RETURN_CODE DecompressAndDecode(EMBEDDED_FILE &aFile)
Takes data from the #compressedEncodedData buffer and Base64 decodes it.
static uint32_t Seed()
static RETURN_CODE CompressAndEncode(EMBEDDED_FILE &aFile)
Takes data from the #decompressedData buffer and compresses it using ZSTD into the #compressedEncoded...
A streaming C++ equivalent for MurmurHash3_x64_128.
Definition: mmh3_hash.h:60
FORCE_INLINE void add(const std::string &input)
Definition: mmh3_hash.h:95
FORCE_INLINE HASH_128 digest()
Definition: mmh3_hash.h:114
FORCE_INLINE void reset(uint32_t aSeed=0)
Definition: mmh3_hash.h:66
static boost::mt19937 rng
Definition: kiid.cpp:50
std::vector< char > decompressedData
std::string ToString() const
Definition: hash_128.h:47
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_CASE(CompressAndEncode_OK)
BOOST_AUTO_TEST_SUITE_END()