Add ability to seed the KIID generator

Fixes https://gitlab.com/kicad/code/kicad/-/issues/9689
This commit is contained in:
Jon Evans 2021-11-20 14:19:29 -05:00
parent b3e2206377
commit 1e8284bc1a
4 changed files with 65 additions and 1 deletions

View File

@ -37,7 +37,8 @@
// Create only once, as seeding is *very* expensive // Create only once, as seeding is *very* expensive
static boost::uuids::random_generator randomGenerator; static boost::mt19937 rng;
static boost::uuids::basic_random_generator<boost::mt19937> randomGenerator( &rng );
// These don't have the same performance penalty, but might as well be consistent // These don't have the same performance penalty, but might as well be consistent
static boost::uuids::string_generator stringGenerator; static boost::uuids::string_generator stringGenerator;
@ -243,6 +244,12 @@ void KIID::CreateNilUuids( bool aNil )
} }
void KIID::SeedGenerator( unsigned int aSeed )
{
rng.seed( aSeed );
}
KIID_PATH::KIID_PATH( const wxString& aString ) KIID_PATH::KIID_PATH( const wxString& aString )
{ {
for( const wxString& pathStep : wxSplit( aString, '/' ) ) for( const wxString& pathStep : wxSplit( aString, '/' ) )

View File

@ -63,6 +63,17 @@ public:
static void CreateNilUuids( bool aNil = true ); static void CreateNilUuids( bool aNil = true );
/**
* Re-initialize the UUID generator with a given seed (for testing or QA purposes)
*
* WARNING: Do not call this function from within KiCad or via a Python action plugin. It is
* only to be used inside QA tests or in external Python scripts. Resetting the UUID generator
* in the middle of a KiCad GUI run will potentially have harmful effects on file integrity.
*
* @param aSeed is a seed to pass to the boost::mt19937 pseudo-random number generator
*/
static void SeedGenerator( unsigned int aSeed );
/** /**
* Change an existing time stamp based UUID into a true UUID. * Change an existing time stamp based UUID into a true UUID.
* *

View File

@ -37,6 +37,7 @@ set( common_srcs
test_eda_rect.cpp test_eda_rect.cpp
test_lib_table.cpp test_lib_table.cpp
test_kicad_string.cpp test_kicad_string.cpp
test_kiid.cpp
test_property.cpp test_property.cpp
test_refdes_utils.cpp test_refdes_utils.cpp
test_title_block.cpp test_title_block.cpp

45
qa/common/test_kiid.cpp Normal file
View File

@ -0,0 +1,45 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 1992-2021 KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <boost/test/unit_test.hpp>
#include <kiid.h>
BOOST_AUTO_TEST_SUITE( Kiid )
BOOST_AUTO_TEST_CASE( Seeding )
{
KIID::SeedGenerator( 0l );
KIID a;
BOOST_CHECK_EQUAL( a.Hash(), 15552532309556242017ul );
KIID b;
BOOST_CHECK_EQUAL( b.Hash(), 13842873335846156666ul );
KIID c;
BOOST_CHECK_EQUAL( c.Hash(), 15995408467689523943ul );
KIID d;
BOOST_CHECK_EQUAL( d.Hash(), 4943106325342180035ul );
}
BOOST_AUTO_TEST_SUITE_END()