From 28179296db72eeb066f46f88ab0f1533177c703d Mon Sep 17 00:00:00 2001 From: Mikolaj Wielgus Date: Tue, 9 Aug 2022 03:04:49 +0200 Subject: [PATCH] Sim QA: Output more debug info when a test fails Also fix a bug due to unsigned int wraparound. --- .../eeschema/test_netlist_exporter_spice.cpp | 47 ++++++++++++++++--- 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/qa/unittests/eeschema/test_netlist_exporter_spice.cpp b/qa/unittests/eeschema/test_netlist_exporter_spice.cpp index 2b9db2ef26..e8beec9431 100644 --- a/qa/unittests/eeschema/test_netlist_exporter_spice.cpp +++ b/qa/unittests/eeschema/test_netlist_exporter_spice.cpp @@ -24,6 +24,7 @@ #ifdef KICAD_SPICE #include +#include // To check if the current test failed (to be moved?). #include #include #include @@ -37,9 +38,14 @@ class TEST_NETLIST_EXPORTER_SPICE_FIXTURE : public TEST_NETLIST_EXPORTER_FIXTURE public: class SPICE_TEST_REPORTER : public SPICE_REPORTER { + public: + SPICE_TEST_REPORTER( std::shared_ptr aLog ) : m_log( std::move( aLog ) ) {} + REPORTER& Report( const wxString& aText, SEVERITY aSeverity = RPT_SEVERITY_UNDEFINED ) override { + *m_log << aText << "\n"; + // You can add a debug trace here. return *this; } @@ -49,14 +55,28 @@ public: void OnSimStateChange( SPICE_SIMULATOR* aObject, SIM_STATE aNewState ) override { } + + private: + std::shared_ptr m_log; }; TEST_NETLIST_EXPORTER_SPICE_FIXTURE() : - TEST_NETLIST_EXPORTER_FIXTURE(), - m_simulator( SPICE_SIMULATOR::CreateInstance( "ngspice" ) ), - m_reporter( std::make_unique() ) + TEST_NETLIST_EXPORTER_FIXTURE(), + m_simulator( SPICE_SIMULATOR::CreateInstance( "ngspice" ) ), + m_log( std::make_shared() ), + m_reporter( std::make_unique( m_log ) ) { - + } + + ~TEST_NETLIST_EXPORTER_SPICE_FIXTURE() + { + using namespace boost::unit_test; + + test_case::id_t id = framework::current_test_case().p_id; + test_results results = results_collector.results( id ); + + // Output a log if the test has failed. + BOOST_CHECK_MESSAGE( results.passed(), "\nNGSPICE LOG\n===========\n" << *m_log ); } wxFileName GetSchematicPath( const wxString& aBaseName ) override @@ -102,6 +122,16 @@ public: // We need to make sure that the number of points always the same. ngspice->Command( "linearize" ); + + // Display all vectors in case we need them for debugging. + ngspice->Command( "echo Available Vectors" ); + ngspice->Command( "echo -----------------" ); + ngspice->Command( "display" ); + + // Display an expanded netlist in case we need it for debugging. + ngspice->Command( "echo Expanded Netlist" ); + ngspice->Command( "echo ----------------" ); + ngspice->Command( "listing runnable" ); } void TestNetlist( const wxString& aBaseName ) @@ -129,9 +159,9 @@ public: { double inf = std::numeric_limits::infinity(); - double leftDelta = ( aXValue - ( i-1 >= 0 ? xVector[i-1] : -inf ) ); + double leftDelta = ( aXValue - ( i >= 1 ? xVector[i - 1] : -inf ) ); double middleDelta = ( aXValue - xVector[i] ); - double rightDelta = ( aXValue - ( i+1 < xVector.size() ? xVector[i+1] : inf ) ); + double rightDelta = ( aXValue - ( i < xVector.size() - 1 ? xVector[i + 1] : inf ) ); // Check if this point is the closest one. if( abs( middleDelta ) <= abs( leftDelta ) @@ -147,6 +177,8 @@ public: { std::vector yVector = ngspice->GetMagPlot( vectorName ); + BOOST_REQUIRE_GE( yVector.size(), i + 1 ); + BOOST_TEST_CONTEXT( "Y vector name: " << vectorName << ", Ref value: " << refValue << ", Actual value: " << yVector[i] ) @@ -193,8 +225,9 @@ public: | NETLIST_EXPORTER_SPICE::OPTION_ADJUST_INCLUDE_PATHS; } - std::unique_ptr m_reporter; std::shared_ptr m_simulator; + std::shared_ptr m_log; + std::unique_ptr m_reporter; };