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 <picosha2.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
37 picosha2::hash256_hex_string(file.decompressedData, file.data_sha);
38
40 BOOST_CHECK_EQUAL(result, EMBEDDED_FILES::RETURN_CODE::OK);
41}
42
43BOOST_AUTO_TEST_CASE( DecompressAndDecode_OK )
44{
46 file.name = "test_file";
47 std::string data = "Hello, World!";
48 file.decompressedData.assign( data.begin(), data.end() );
49
50 picosha2::hash256_hex_string( file.decompressedData, file.data_sha );
51
53 BOOST_CHECK_EQUAL( result, EMBEDDED_FILES::RETURN_CODE::OK );
54
56 BOOST_CHECK_EQUAL( result, EMBEDDED_FILES::RETURN_CODE::OK );
57
58 // Create a large test data
59 data.clear();
60 data.reserve( 13 * 100000 + 1 );
61
62 for( int i = 0; i < 100000; ++i )
63 data += "Hello, World!";
64
65 file.decompressedData.assign( data.begin(), data.end() );
66
67 picosha2::hash256_hex_string( file.decompressedData, file.data_sha );
68
69 result = EMBEDDED_FILES::CompressAndEncode( file );
70 BOOST_CHECK_EQUAL( result, EMBEDDED_FILES::RETURN_CODE::OK );
71
73 BOOST_CHECK_EQUAL( result, EMBEDDED_FILES::RETURN_CODE::OK );
74
75 // Create a sequential test dataset
76 data.clear();
77 data.reserve( 100000 );
78
79 for( int i = 0; i < 100000; ++i )
80 data += static_cast<char>( i % 256 );
81
82 file.decompressedData.assign( data.begin(), data.end() );
83 picosha2::hash256_hex_string( file.decompressedData, file.data_sha );
84
85 result = EMBEDDED_FILES::CompressAndEncode( file );
86 BOOST_CHECK_EQUAL( result, EMBEDDED_FILES::RETURN_CODE::OK );
87
89 BOOST_CHECK_EQUAL( result, EMBEDDED_FILES::RETURN_CODE::OK );
90
91 // Create a random test dataset with a known seed
92 data.clear();
93 data.reserve( 100000 );
94
95 std::mt19937 rng;
96 rng.seed( 0 );
97
98 for( int i = 0; i < 100000; ++i )
99 data += static_cast<char>( rng() % 256 );
100
101 file.decompressedData.assign( data.begin(), data.end() );
102 picosha2::hash256_hex_string( file.decompressedData, file.data_sha );
103
104 result = EMBEDDED_FILES::CompressAndEncode( file );
105 BOOST_CHECK_EQUAL( result, EMBEDDED_FILES::RETURN_CODE::OK );
106
108 BOOST_CHECK_EQUAL( result, EMBEDDED_FILES::RETURN_CODE::OK );
109
110}
111
112BOOST_AUTO_TEST_CASE( DecompressAndDecode_ChecksumError )
113{
115 file.name = "test_file";
116 std::string data = "Hello, World!";
117 file.decompressedData.assign(data.begin(), data.end());
118
120 BOOST_CHECK_EQUAL(result, EMBEDDED_FILES::RETURN_CODE::OK);
121
122 // Modify the checksum
123 file.data_sha[0] = 'x';
124
126 BOOST_CHECK_EQUAL(result, EMBEDDED_FILES::RETURN_CODE::CHECKSUM_ERROR);
127}
128
129BOOST_AUTO_TEST_SUITE_END()
static RETURN_CODE DecompressAndDecode(EMBEDDED_FILE &aFile)
Takes data from the #compressedEncodedData buffer and Base64 decodes it.
static RETURN_CODE CompressAndEncode(EMBEDDED_FILE &aFile)
Takes data from the #decompressedData buffer and compresses it using ZSTD into the #compressedEncoded...
static boost::mt19937 rng
Definition: kiid.cpp:50
std::vector< char > decompressedData
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_CASE(CompressAndEncode_OK)