KiCad PCB EDA Suite
Loading...
Searching...
No Matches
qa_pns_regressions_main.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.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#define BOOST_TEST_NO_MAIN
21
22#include <wx/cmdline.h>
23#include <wx/stdstream.h>
24#include <wx/wfstream.h>
25#include <wx/textfile.h>
26
27#include <io/io_utils.h>
28
32#include <wx/dir.h>
33
34#include "pns_log_file.h"
36
37#include <boost/test/unit_test.hpp>
38
39using namespace boost::unit_test;
40
41std::map<wxString, wxString> g_testBoards;
43
45{
46 PNS_TEST_CASE( const wxString & aName, const wxString& aPath ) : m_name( aName ), m_dataPath( aPath ){};
47
48 wxString GetDataPath() const { return m_dataPath; }
49 wxString GetName() const { return m_name; }
50 wxString m_dataPath;
51 wxString m_name;
52};
53
54
73
74
76{
77public:
78 static bool RunTest( PNS_TEST_CASE* aTestData )
79 {
80 BOOST_TEST_MESSAGE( "Running testcase " << aTestData->GetName() );
81
82 PNS_LOG_FILE logFile;
83 PNS_LOG_PLAYER player;
84
85 auto hash = logFile.GetLogBoardHash( aTestData->GetDataPath() );
86
87 wxString boardFilename;
88
89 if( hash.has_value() )
90 {
91 auto it = g_testBoards.find( *hash );
92 if ( it != g_testBoards.end() )
93 {
94 boardFilename = it->second;
95 BOOST_TEST_MESSAGE( "found matching board: " << boardFilename );
96 }
97 }
98
99 if( !logFile.Load( wxString( aTestData->GetDataPath() ), m_logger.m_Reporter, boardFilename ) )
100 {
101 BOOST_TEST_ERROR( "Failed to load test " << aTestData->GetName() << " from " << aTestData->GetDataPath() );
102 return false;
103 }
104
105 player.SetReporter( m_logger.m_Reporter );
106 player.ReplayLog( &logFile, 0 );
107
108 auto cstate = player.GetRouterUpdatedItems();
109 auto expected = logFile.GetExpectedResult();
110
111 BOOST_TEST_MESSAGE( "- expected: " << expected.m_heads.size() << " heads, " << expected.m_removedIds.size()
112 << " removed items, " << expected.m_addedItems.size() << " added items" );
113 BOOST_TEST_MESSAGE( "- computed: " << cstate.m_heads.size() << " heads, " << cstate.m_removedIds.size()
114 << " removed items, " << cstate.m_addedItems.size() << " added items" );
115
116 bool pass = cstate.Compare( expected );
117
118 BOOST_CHECK( pass );
119
120 return pass;
121 }
122
124};
125
126
128
129std::vector<wxString> scanSubdirs( const wxString& absPath, bool aFiles, wxString filespec = wxEmptyString )
130{
131 std::vector<wxString> rv;
132
133 wxDir dir( absPath );
134 if( !dir.IsOpened() )
135 {
136 BOOST_TEST_ERROR( "Failed to open directory: " << absPath );
137 return rv;
138 }
139
140 wxString dirName;
141
142 bool hasFiles = dir.GetFirst( &dirName, filespec, aFiles ? wxDIR_FILES : wxDIR_DIRS );
143 while( hasFiles )
144 {
145 rv.push_back( dirName );
146 hasFiles = dir.GetNext( &dirName );
147 }
148
149 return rv;
150}
151
152void scanAndHashBoards( const wxString& aPath )
153{
154 for( const wxString& brdFile : scanSubdirs( aPath, true, wxT("*.kicad_pcb") ) )
155 {
156 wxFileName path ( aPath );
157 path.SetName( brdFile );
158 std::optional<wxString> hash = IO_UTILS::fileHashMMH3( path.GetFullPath() );
159 if( hash )
160 {
161 BOOST_TEST_MESSAGE("- file: " << std::left << std::setw(50) << brdFile << " hash: " << ( *hash ) );
162 g_testBoards[ *hash ] = path.GetFullPath();
163 }
164 }
165}
166
167std::vector<PNS_TEST_CASE*> createTestCases()
168{
169 std::vector<PNS_TEST_CASE*> testCases;
170
171 wxFileName absPath( KI_TEST::GetPcbnewTestDataDir() );
172 absPath.AppendDir (wxT("pns_regressions"));
173
174 wxFileName boardsPath( absPath );
175 boardsPath.AppendDir(wxT("boards"));
176
177 scanAndHashBoards( boardsPath.GetFullPath() );
178
179 for( const wxString& subdir : scanSubdirs( absPath.GetFullPath(), false ) )
180 {
181 if( !subdir.CmpNoCase( wxT("boards") ) )
182 continue;
183
184 wxFileName path( absPath );
185 path.AppendDir( subdir );
186
187 for( const wxString& logName: scanSubdirs( path.GetFullPath(), true, wxT("*.log") ) )
188 {
189 wxFileName logPath( path );
190 logPath.SetName( logName );
191
192 BOOST_TEST_MESSAGE( "Add case " << logPath.GetFullPath() );
193
194 testCases.push_back( new PNS_TEST_CASE( subdir, logPath.GetFullPath() ) );
195 }
196 }
197
198 return testCases;
199}
200
201
203{
204 test_suite* pnsTestSuite = BOOST_TEST_SUITE( "pns_regressions" );
205
206 auto testCases = createTestCases();
207
208 for( auto& c : testCases )
209 {
210 pnsTestSuite->add(
211 BOOST_TEST_CASE_NAME( std::bind( &PNS_TEST_FIXTURE::RunTest, c ), c->GetName().ToStdString() ) );
212 }
213
214 framework::master_test_suite().p_name.value = "P&S router regressions";
215 framework::master_test_suite().add( pnsTestSuite );
216 return true;
217}
218
219
220int main( int argc, char* argv[] )
221{
222 return unit_test_main( &init_pns_test_suite, argc, argv );
223}
224
General utilities for PCB file IO for QA programs.
KI_TEST::CONSOLE_LOG * m_Log
KI_TEST::CONSOLE_MSG_REPORTER * m_Reporter
const std::optional< wxString > GetLogBoardHash(const wxString &logFileName)
const COMMIT_STATE & GetExpectedResult() const
bool Load(const wxFileName &logFileName, REPORTER *aRpt, const wxString boardFileName=wxT(""))
const PNS_LOG_FILE::COMMIT_STATE GetRouterUpdatedItems()
void ReplayLog(PNS_LOG_FILE *aLog, int aStartEventIndex=0, int aFrom=0, int aTo=-1, bool aUpdateExpectedResult=false)
void SetReporter(REPORTER *aReporter)
std::optional< wxString > fileHashMMH3(const wxString &aFilePath)
Calculates an MMH3 hash of a given file.
Definition io_utils.cpp:86
std::string GetPcbnewTestDataDir()
Utility which returns a path to the data directory where the test board files are stored.
std::vector< PNS_TEST_CASE * > createTestCases()
void scanAndHashBoards(const wxString &aPath)
std::map< wxString, wxString > g_testBoards
bool init_pns_test_suite()
bool g_showDetailedLog
std::vector< wxString > scanSubdirs(const wxString &absPath, bool aFiles, wxString filespec=wxEmptyString)
wxString GetName() const
PNS_TEST_CASE(const wxString &aName, const wxString &aPath)
wxString GetDataPath() const
static bool RunTest(PNS_TEST_CASE *aTestData)
static FIXTURE_LOGGER m_logger
std::string path
VECTOR3I expected(15, 30, 45)
BOOST_TEST_MESSAGE("\n=== Real-World Polygon PIP Benchmark ===\n"<< formatTable(table))