25#include <boost/test/results_collector.hpp>
35#define MAX_DEFAULT_REL_ERROR 2e-2
51 *
m_log << aText <<
"\n";
62 std::shared_ptr<wxString>
m_log;
68 m_log(
std::make_shared<wxString>() ),
76 using namespace boost::unit_test;
78 test_case::id_t
id = framework::current_test_case().p_id;
79 test_results results = results_collector.results(
id );
82 BOOST_CHECK_MESSAGE( results.passed(),
"\nNGSPICE LOG\n===========\n" << *
m_log );
88 fn.AppendDir(
"spice_netlists" );
89 fn.AppendDir( aBaseName );
90 fn.SetName( aBaseName );
101 netFile.SetName( netFile.GetName() +
"_test" );
103 netFile.SetExt(
"spice" );
104 return netFile.GetFullPath();
113 BOOST_REQUIRE( ngspice );
123 ngspice->
Command(
"set ngbehavior=ps" );
124 ngspice->
Command(
"setseed 1" );
143 bool mif_error =
m_log->Find( wxT(
"MIF-ERROR" ) ) != wxNOT_FOUND;
145 BOOST_TEST_INFO(
"Cannot run ngspice. test skipped. Missing code model files?" );
146 BOOST_CHECK( !mif_error );
148 bool err_found =
m_log->Find( wxT(
"Error: circuit not parsed" ) ) != wxNOT_FOUND;
150 BOOST_TEST_INFO(
"Cannot run ngspice. test skipped. Install error?" );
151 BOOST_CHECK( !err_found );
153 if( mif_error || err_found )
158 *
m_log <<
"Original Netlist\n";
159 *
m_log <<
"----------------\n";
166 ngspice->
Command(
"linearize" );
173 ngspice->
Command(
"echo Available Vectors" );
174 ngspice->
Command(
"echo -----------------" );
179 *
m_log <<
"Original Netlist\n";
180 *
m_log <<
"----------------\n";
184 ngspice->
Command(
"echo Expanded Netlist" );
185 ngspice->
Command(
"echo ----------------" );
186 ngspice->
Command(
"listing runnable" );
189 void TestOpPoint(
double aRefValue,
const std::string& aVectorName,
192 BOOST_TEST_CONTEXT(
"Vector name: " << aVectorName )
196 std::vector<double> vector = ngspice->
GetRealVector( aVectorName );
198 BOOST_REQUIRE_EQUAL( vector.size(), 1 );
200 double maxError = abs( aRefValue * aMaxRelError );
201 BOOST_CHECK_LE( abs( vector[0] - aRefValue ), aMaxRelError );
205 void TestPoint(
const std::string& aXVectorName,
double aXValue,
206 const std::map<const std::string, double> aTestVectorsAndValues,
213 BOOST_TEST_CONTEXT(
"X vector name: " << aXVectorName <<
", X value: " << aXValue )
217 std::vector<double> xVector = ngspice->
GetRealVector( aXVectorName );
220 for(; i < xVector.size(); ++i )
222 double inf = std::numeric_limits<double>::infinity();
224 double leftDelta = ( aXValue - ( i >= 1 ? xVector[i - 1] : -inf ) );
225 double middleDelta = ( aXValue - xVector[i] );
226 double rightDelta = ( aXValue - ( i < xVector.size() - 1 ? xVector[i + 1] : inf ) );
229 if( abs( middleDelta ) <= abs( leftDelta )
230 && abs( middleDelta ) <= abs( rightDelta ) )
236 BOOST_REQUIRE_LT( i, xVector.size() );
238 for(
auto& [vectorName, refValue] : aTestVectorsAndValues )
240 std::vector<double> yVector = ngspice->
GetGainVector( vectorName );
242 BOOST_REQUIRE_GE( yVector.size(), i + 1 );
244 BOOST_TEST_CONTEXT(
"Y vector name: " << vectorName
245 <<
", Ref value: " << refValue
246 <<
", Actual value: " << yVector[i] )
248 double maxError = abs( refValue * aMaxRelError );
253 maxError = aMaxRelError;
256 BOOST_CHECK_LE( abs( yVector[i] - refValue ), maxError );
263 const std::map<const std::string, double> aTestVectorsAndValues,
266 TestPoint(
"time", aTime, aTestVectorsAndValues, aMaxRelError );
270 const std::map<const std::string, double> aTestVectorsAndValues,
273 TestPoint(
"frequency", aFrequency, aTestVectorsAndValues, aMaxRelError );
279 netlistPath.SetExt(
"csv" );
281 return netlistPath.GetFullPath();
295 std::shared_ptr<wxString>
m_log;
296 std::unique_ptr<SPICE_TEST_REPORTER>
m_reporter;
@ OPTION_SAVE_ALL_CURRENTS
@ OPTION_SAVE_ALL_VOLTAGES
@ OPTION_SAVE_ALL_DISSIPATIONS
@ OPTION_ADJUST_INCLUDE_PATHS
bool Command(const std::string &aCmd) override final
Set a SIMULATOR_REPORTER object to receive the simulation log.
bool IsRunning() override final
Execute a Spice command as if it was typed into console.
bool Run() override final
Halt the simulation.
bool LoadNetlist(const std::string &aNetlist) override final
Execute the simulation with currently loaded netlist.
std::vector< double > GetGainVector(const std::string &aName, int aMaxLen=-1) override final
Return a requested vector with phase values.
std::vector< double > GetRealVector(const std::string &aName, int aMaxLen=-1) override final
Return a requested vector with imaginary values.
virtual const wxString GetProjectFullName() const
Return the full path and name of the project.
A pure virtual class used to derive REPORTER objects from.
PROJECT & Prj() const override
Return a reference to the project this schematic is part of.
Interface to receive simulation updates from SPICE_SIMULATOR class.
virtual void SetReporter(SIMULATOR_REPORTER *aReporter)
bool HasMessage() const override
Returns true if the reporter client is non-empty.
std::shared_ptr< wxString > m_log
SPICE_TEST_REPORTER(std::shared_ptr< wxString > aLog)
void OnSimStateChange(SIMULATOR *aObject, SIM_STATE aNewState) override
REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
std::unique_ptr< SPICE_TEST_REPORTER > m_reporter
std::shared_ptr< SPICE_SIMULATOR > m_simulator
void TestOpPoint(double aRefValue, const std::string &aVectorName, double aMaxRelError=MAX_DEFAULT_REL_ERROR)
void TestPoint(const std::string &aXVectorName, double aXValue, const std::map< const std::string, double > aTestVectorsAndValues, double aMaxRelError=MAX_DEFAULT_REL_ERROR)
unsigned GetNetlistOptions() override
TEST_NETLIST_EXPORTER_SPICE_FIXTURE()
wxString GetResultsPath(bool aTest=false)
wxFileName GetSchematicPath(const wxString &aBaseName) override
void TestTranPoint(double aTime, const std::map< const std::string, double > aTestVectorsAndValues, double aMaxRelError=MAX_DEFAULT_REL_ERROR)
std::shared_ptr< wxString > m_log
~TEST_NETLIST_EXPORTER_SPICE_FIXTURE()
void TestACPoint(double aFrequency, const std::map< const std::string, double > aTestVectorsAndValues, double aMaxRelError=MAX_DEFAULT_REL_ERROR)
wxString GetNetlistPath(bool aTest=false) override
void CompareNetlists() override
static const std::string KiCadSchematicFileExtension
std::string GetEeschemaTestDataDir()
Get the configured location of Eeschema test data.
#define MAX_DEFAULT_REL_ERROR