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>
27
using
magic_enum::iostream_operators::operator<<;
28
29
BOOST_AUTO_TEST_SUITE
( EmbeddedFiles )
30
31
BOOST_AUTO_TEST_CASE
( CompressAndEncode_OK )
32
{
33
EMBEDDED_FILES::EMBEDDED_FILE
file;
34
file.
name
=
"test_file"
;
35
std::string data =
"Hello, World!"
;
36
file.
decompressedData
.assign(data.begin(), data.end());
37
38
MMH3_HASH
hash(
EMBEDDED_FILES::Seed
() );
39
hash.
add
( file.
decompressedData
);
40
file.
data_hash
= hash.digest().ToString();
41
42
EMBEDDED_FILES::RETURN_CODE
result
=
EMBEDDED_FILES::CompressAndEncode
(file);
43
BOOST_CHECK_EQUAL
(
result
,
EMBEDDED_FILES::RETURN_CODE::OK
);
44
}
45
46
BOOST_AUTO_TEST_CASE
( DecompressAndDecode_OK )
47
{
48
EMBEDDED_FILES::EMBEDDED_FILE
file;
49
file.
name
=
"test_file"
;
50
std::string data =
"Hello, World!"
;
51
file.
decompressedData
.assign( data.begin(), data.end() );
52
53
MMH3_HASH
hash(
EMBEDDED_FILES::Seed
() );
54
hash.
add
( file.
decompressedData
);
55
file.
data_hash
= hash.
digest
().
ToString
();
56
57
EMBEDDED_FILES::RETURN_CODE
result
=
EMBEDDED_FILES::CompressAndEncode
( file );
58
BOOST_CHECK_EQUAL
(
result
,
EMBEDDED_FILES::RETURN_CODE::OK
);
59
60
result
=
EMBEDDED_FILES::DecompressAndDecode
( file );
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
79
result
=
EMBEDDED_FILES::DecompressAndDecode
( file );
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
97
result
=
EMBEDDED_FILES::DecompressAndDecode
( file );
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
118
result
=
EMBEDDED_FILES::DecompressAndDecode
( file );
119
BOOST_CHECK_EQUAL
(
result
,
EMBEDDED_FILES::RETURN_CODE::OK
);
120
121
}
122
123
BOOST_AUTO_TEST_CASE
( DecompressAndDecode_ChecksumError )
124
{
125
EMBEDDED_FILES::EMBEDDED_FILE
file;
126
file.
name
=
"test_file"
;
127
std::string data =
"Hello, World!"
;
128
file.
decompressedData
.assign(data.begin(), data.end());
129
130
EMBEDDED_FILES::RETURN_CODE
result
=
EMBEDDED_FILES::CompressAndEncode
(file);
131
BOOST_CHECK_EQUAL
(
result
,
EMBEDDED_FILES::RETURN_CODE::OK
);
132
133
// Modify the checksum
134
file.
data_hash
[0] =
'x'
;
135
136
result
=
EMBEDDED_FILES::DecompressAndDecode
(file);
137
BOOST_CHECK_EQUAL
(
result
,
EMBEDDED_FILES::RETURN_CODE::CHECKSUM_ERROR
);
138
}
139
140
BOOST_AUTO_TEST_SUITE_END
()
EMBEDDED_FILES::RETURN_CODE
RETURN_CODE
Definition
embedded_files.h:93
EMBEDDED_FILES::RETURN_CODE::OK
@ OK
Success.
Definition
embedded_files.h:94
EMBEDDED_FILES::RETURN_CODE::CHECKSUM_ERROR
@ CHECKSUM_ERROR
Checksum in file does not match data.
Definition
embedded_files.h:99
EMBEDDED_FILES::DecompressAndDecode
static RETURN_CODE DecompressAndDecode(EMBEDDED_FILE &aFile)
Takes data from the #compressedEncodedData buffer and Base64 decodes it.
Definition
embedded_files.cpp:253
EMBEDDED_FILES::Seed
static uint32_t Seed()
Definition
embedded_files.h:270
EMBEDDED_FILES::CompressAndEncode
static RETURN_CODE CompressAndEncode(EMBEDDED_FILE &aFile)
Take data from the #decompressedData buffer and compresses it using ZSTD into the #compressedEncodedD...
Definition
embedded_files.cpp:218
MMH3_HASH
A streaming C++ equivalent for MurmurHash3_x64_128.
Definition
mmh3_hash.h:60
MMH3_HASH::add
FORCE_INLINE void add(const std::string &input)
Definition
mmh3_hash.h:95
MMH3_HASH::digest
FORCE_INLINE HASH_128 digest()
Definition
mmh3_hash.h:114
MMH3_HASH::reset
FORCE_INLINE void reset(uint32_t aSeed=0)
Definition
mmh3_hash.h:66
embedded_files.h
rng
static boost::mt19937 rng
Definition
kiid.cpp:52
mmh3_hash.h
EMBEDDED_FILES::EMBEDDED_FILE
Definition
embedded_files.h:44
EMBEDDED_FILES::EMBEDDED_FILE::decompressedData
std::vector< char > decompressedData
Definition
embedded_files.h:88
EMBEDDED_FILES::EMBEDDED_FILE::name
wxString name
Definition
embedded_files.h:84
EMBEDDED_FILES::EMBEDDED_FILE::data_hash
std::string data_hash
Definition
embedded_files.h:89
HASH_128::ToString
std::string ToString() const
Definition
hash_128.h:47
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
Definition
test_api_enums.cpp:134
BOOST_AUTO_TEST_SUITE
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(CompressAndEncode_OK)
Definition
test_embedded_file_compress.cpp:31
BOOST_AUTO_TEST_SUITE_END
BOOST_AUTO_TEST_SUITE_END()
result
wxString result
Test unit parsing edge cases and error handling.
Definition
test_text_eval_numeric_compat.cpp:602
BOOST_CHECK_EQUAL
BOOST_CHECK_EQUAL(result, "25.4")
src
qa
tests
common
test_embedded_file_compress.cpp
Generated on Sun Sep 21 2025 01:05:32 for KiCad PCB EDA Suite by
1.13.2