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 The 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 <magic_enum_iostream.hpp>
22#include <boost/test/unit_test.hpp>
23#include <mmh3_hash.h>
24#include <embedded_files.h>
25
26#include <random>
27using magic_enum::iostream_operators::operator<<;
28
29BOOST_AUTO_TEST_SUITE( EmbeddedFiles )
30
31BOOST_AUTO_TEST_CASE( CompressAndEncode_OK )
32{
34 file.name = "test_file";
35 std::string data = "Hello, World!";
36 file.decompressedData.assign(data.begin(), data.end());
37
39 hash.add( file.decompressedData );
40 file.data_hash = hash.digest().ToString();
41
43 BOOST_CHECK_EQUAL(result, EMBEDDED_FILES::RETURN_CODE::OK);
44}
45
46BOOST_AUTO_TEST_CASE( DecompressAndDecode_OK )
47{
49 file.name = "test_file";
50 std::string data = "Hello, World!";
51 file.decompressedData.assign( data.begin(), data.end() );
52
54 hash.add( file.decompressedData );
55 file.data_hash = hash.digest().ToString();
56
58 BOOST_CHECK_EQUAL( result, EMBEDDED_FILES::RETURN_CODE::OK );
59
61 BOOST_CHECK_EQUAL( result, EMBEDDED_FILES::RETURN_CODE::OK );
62
63 // Create a large test data
64 data.clear();
65 data.reserve( 13 * 100000 + 1 );
66
67 for( int i = 0; i < 100000; ++i )
68 data += "Hello, World!";
69
70 file.decompressedData.assign( data.begin(), data.end() );
71
72 hash.reset();
73 hash.add( file.decompressedData );
74 file.data_hash = hash.digest().ToString();
75
76 result = EMBEDDED_FILES::CompressAndEncode( file );
77 BOOST_CHECK_EQUAL( result, EMBEDDED_FILES::RETURN_CODE::OK );
78
80 BOOST_CHECK_EQUAL( result, EMBEDDED_FILES::RETURN_CODE::OK );
81
82 // Create a sequential test dataset
83 data.clear();
84 data.reserve( 100000 );
85
86 for( int i = 0; i < 100000; ++i )
87 data += static_cast<char>( i % 256 );
88
89 file.decompressedData.assign( data.begin(), data.end() );
90 hash.reset();
91 hash.add( file.decompressedData );
92 file.data_hash = hash.digest().ToString();
93
94 result = EMBEDDED_FILES::CompressAndEncode( file );
95 BOOST_CHECK_EQUAL( result, EMBEDDED_FILES::RETURN_CODE::OK );
96
98 BOOST_CHECK_EQUAL( result, EMBEDDED_FILES::RETURN_CODE::OK );
99
100 // Create a random test dataset with a known seed
101 data.clear();
102 data.reserve( 100000 );
103
104 std::mt19937 rng;
105 rng.seed( 0 );
106
107 for( int i = 0; i < 100000; ++i )
108 data += static_cast<char>( rng() % 256 );
109
110 file.decompressedData.assign( data.begin(), data.end() );
111 hash.reset();
112 hash.add( file.decompressedData );
113 file.data_hash = hash.digest().ToString();
114
115 result = EMBEDDED_FILES::CompressAndEncode( file );
116 BOOST_CHECK_EQUAL( result, EMBEDDED_FILES::RETURN_CODE::OK );
117
119 BOOST_CHECK_EQUAL( result, EMBEDDED_FILES::RETURN_CODE::OK );
120
121}
122
123BOOST_AUTO_TEST_CASE( DecompressAndDecode_ChecksumError )
124{
126 file.name = "test_file";
127 std::string data = "Hello, World!";
128 file.decompressedData.assign(data.begin(), data.end());
129
131 BOOST_CHECK_EQUAL(result, EMBEDDED_FILES::RETURN_CODE::OK);
132
133 // Modify the checksum
134 file.data_hash[0] = 'x';
135
137 BOOST_CHECK_EQUAL(result, EMBEDDED_FILES::RETURN_CODE::CHECKSUM_ERROR);
138}
139
@ CHECKSUM_ERROR
Checksum in file does not match data.
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)
Take data from the #decompressedData buffer and compresses it using ZSTD into the #compressedEncodedD...
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:52
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()