From 1e8284bc1a3680c70f4273955c245fca0e7b9c9e Mon Sep 17 00:00:00 2001 From: Jon Evans Date: Sat, 20 Nov 2021 14:19:29 -0500 Subject: [PATCH] Add ability to seed the KIID generator Fixes https://gitlab.com/kicad/code/kicad/-/issues/9689 --- common/kiid.cpp | 9 +++++++- include/kiid.h | 11 ++++++++++ qa/common/CMakeLists.txt | 1 + qa/common/test_kiid.cpp | 45 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 qa/common/test_kiid.cpp diff --git a/common/kiid.cpp b/common/kiid.cpp index 32f6a955a6..5b34b80ff3 100644 --- a/common/kiid.cpp +++ b/common/kiid.cpp @@ -37,7 +37,8 @@ // Create only once, as seeding is *very* expensive -static boost::uuids::random_generator randomGenerator; +static boost::mt19937 rng; +static boost::uuids::basic_random_generator randomGenerator( &rng ); // These don't have the same performance penalty, but might as well be consistent 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 ) { for( const wxString& pathStep : wxSplit( aString, '/' ) ) diff --git a/include/kiid.h b/include/kiid.h index 1b8971e1a5..d2a3942150 100644 --- a/include/kiid.h +++ b/include/kiid.h @@ -63,6 +63,17 @@ public: 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. * diff --git a/qa/common/CMakeLists.txt b/qa/common/CMakeLists.txt index 565f222190..bcfa650188 100644 --- a/qa/common/CMakeLists.txt +++ b/qa/common/CMakeLists.txt @@ -37,6 +37,7 @@ set( common_srcs test_eda_rect.cpp test_lib_table.cpp test_kicad_string.cpp + test_kiid.cpp test_property.cpp test_refdes_utils.cpp test_title_block.cpp diff --git a/qa/common/test_kiid.cpp b/qa/common/test_kiid.cpp new file mode 100644 index 0000000000..8c5812f79b --- /dev/null +++ b/qa/common/test_kiid.cpp @@ -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 . + */ + +#include +#include + + +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()