28#include <boost/test/data/test_case.hpp>
98 const nlohmann::json&
GetBoardJson(
const std::string& aBoardName )
const
100 return m_Json[
"boards"][aBoardName];
111 std::string
path, protocol;
112 const std::string sep =
"://";
114 size_t sepPos = aDataSource.find( sep );
115 if( sepPos != std::string::npos )
117 protocol = aDataSource.substr( 0, sepPos );
118 path = aDataSource.substr( sepPos + sep.size() );
123 throw std::runtime_error(
"Unsupported data source URI (missing protocol): " + aDataSource );
126 if( protocol ==
"file" )
132 throw std::runtime_error(
"Unsupported data source protocol: " + protocol );
139 static const std::map<std::string, ALLEGRO::FMT_VER> fmtVerStrMap{
154 auto it = fmtVerStrMap.find( aFmtVerStr );
155 if( it != fmtVerStrMap.end() )
165 const nlohmann::json& headerTestEntry )
167 std::unique_ptr<HEADER_TEST_INFO> headerTest;
169 if( !headerTestEntry.is_object() )
171 throw std::runtime_error(
"Header test entry is not a valid JSON object" );
175 const std::string headerDataUri = std::string(
"file://" ) + boardDir +
"header.bin";
177 return std::make_unique<HEADER_TEST_INFO>( headerDataUri, headerTestEntry.value(
"skip",
false ) );
184 const nlohmann::json& blockTestJson = aBlockTestDescriptor.
m_BlockTestJson;
185 const bool skipBlock = blockTestJson.value(
"skip",
false );
187 const uint8_t& blockType = aBlockTestDescriptor.
m_BlockType;
188 const size_t& blockOffset = aBlockTestDescriptor.
m_BlockOffset;
191 wxString blockDataLoc = wxString::Format(
"0x%02x_0x%08zx.bin", blockType, blockOffset );
193 const std::string blockDataUri = std::string(
"file://" ) + boardDir + blockDataLoc.ToStdString();
195 const bool extraBlockTest = blockTestJson.value(
"extraBlockTest",
false );
226 void RunHeaderTest(
const std::string& aBrdName,
const nlohmann::json& aBoardTestJson )
234 std::unique_ptr<HEADER_TEST_INFO> headerTest =
nullptr;
236 if( aBoardTestJson.contains(
"header" ) && aBoardTestJson[
"header"].is_object() )
243 if( headerTest->m_Skip )
250 std::vector<uint8_t> headerData =
loadDataByUri( headerTest->m_HeaderDataSource );
251 FILE_STREAM fileStream( headerData.data(), headerData.size() );
254 std::unique_ptr<FILE_HEADER> header = parser.
ParseHeader();
275 void RunBlockTest(
const std::string& aBrdName,
const nlohmann::json& aBoardTestJson,
281 BOOST_TEST_CONTEXT( wxString::Format(
"Testing block type %#02x at offset %#010zx from board %s",
292 FILE_STREAM fileStream( data.data(), data.size() );
296 bool endOfObjectsMarker =
false;
297 std::unique_ptr<BLOCK_BASE> block = parser.
ParseBlock( endOfObjectsMarker );
319 std::ostringstream ss;
320 ss <<
"Expected no errors, but found " << aReporter.
GetErrorCount() <<
" errors:";
326 ss <<
"\n " << msg.text;
335void RunBoardLoad(
const std::string& aBrdName,
const nlohmann::json& aBoardTestJson )
337 BOARD* board =
nullptr;
341 if( aBoardTestJson.contains(
"boardFile" ) )
343 const std::string boardFilePath =
346 if( !std::filesystem::exists( boardFilePath ) )
348 BOOST_FAIL(
"Board file does not exist: " + boardFilePath );
355 const bool expectLoadFailure = aBoardTestJson.value(
"expectLoadFailure",
false );
357 if( expectLoadFailure )
365 BOOST_CHECK_MESSAGE( board !=
nullptr,
"Board load was expected to succeed, but it failed." );
371 if(
reporter.GetWarningCount() > 0 )
373 std::ostringstream ss;
374 ss << aBrdName <<
": " <<
reporter.GetWarningCount() <<
" warnings";
376 for(
const auto& msg :
reporter.GetMessages() )
380 ss <<
"\n " << msg.text;
394static uint8_t
getByteFromHexStr(
const nlohmann::json& aJsonEntry,
const std::string& aFieldName )
396 if( !aJsonEntry.contains( aFieldName ) )
398 throw std::runtime_error(
"Block test entry is missing the '" + aFieldName +
"' field" );
401 if( aJsonEntry[aFieldName].is_number_unsigned() )
403 const unsigned long value = aJsonEntry[aFieldName].get<
unsigned long>();
406 throw std::runtime_error(
"Value is too large to fit in a byte: " + std::to_string( value ) );
408 return static_cast<uint8_t
>( value );
410 else if( aJsonEntry[aFieldName].is_string() )
412 const std::string aHexStr = aJsonEntry[aFieldName];
414 const unsigned long value = std::stoul( aHexStr,
nullptr, 0 );
417 throw std::runtime_error(
"Value is too large to fit in a byte: " + aHexStr );
419 return static_cast<uint8_t
>( value );
422 throw std::runtime_error(
"Block test entry has an invalid '" + aFieldName
423 +
"' field (must be a byte value as a number or hex string)" );
431 if( !aBlockTestJson.contains(
"offset" ) || !aBlockTestJson[
"offset"].is_string() )
433 throw std::runtime_error(
"Block test entry is missing a valid 'offset' field" );
436 const std::string block_offset = aBlockTestJson[
"offset"];
437 const size_t blockOffset = std::stoul( block_offset,
nullptr, 0 );
456 if( !aJson.contains(
"boards" ) || !aJson[
"boards"].is_object() )
458 throw std::runtime_error(
"Test register JSON file does not contain a valid 'boards' object." );
461 std::vector<ALLEGRO_BOARD_TEST_DESCRIPTOR> boardTests;
463 for(
auto& [boardName, boardEntry] : aJson[
"boards"].items() )
466 .m_BrdName = boardName,
467 .m_HasHeaderTest = boardEntry.contains(
"header" ),
469 .m_HasBoardFile = boardEntry.contains(
"boardFile" ),
470 .m_ExpectationTests = {},
473 if( boardEntry.contains(
"blocks" ) && boardEntry[
"blocks"].is_array() )
475 for(
const auto& blockTestEntry : boardEntry[
"blocks"] )
481 if( boardEntry.contains(
"boardExpectations" ) )
483 const auto& expectationsEntry = boardEntry[
"boardExpectations"];
485 boardTestRef.m_ExpectationTests =
489 boardTests.push_back( std::move( boardTestRef ) );
504 const std::filesystem::path testRegisterJsonPath(
AllegroBoardDataDir(
"" ) +
"board_data_registry.json" );
505 std::ifstream jsonFileStream( testRegisterJsonPath );
507 reg.
m_Json = nlohmann::json::parse( jsonFileStream,
nullptr,
true,
true );
522 std::vector<std::string> labels;
524 if( boardTestJson.contains(
"testLabels" ) && boardTestJson[
"testLabels"].is_array() )
526 for(
const auto& label : boardTestJson[
"testLabels"] )
528 if( label.is_string() )
530 labels.push_back( label.get<std::string>() );
550 using BTS = boost::unit_test::test_suite;
551 std::vector<BTS*> suites;
552 BTS* blockSuite = suites.emplace_back( BOOST_TEST_SUITE(
"AllegroBlocks" ) );
559 catch(
const std::exception& ex )
561 std::string msg =
"Failed to load Allegro block test definitions: " + std::string( ex.what() );
564 const auto failingTestFunction = [=]()
570 boost::unit_test::make_test_case(
572 "FailureToLoadTestDefinitions",
583 BTS* boardSuite = BOOST_TEST_SUITE( boardTest.
m_BrdName );
584 blockSuite->add( boardSuite );
591 const auto testRunFunction = [&]()
598 boost::unit_test::make_test_case(
611 const auto testRunFunction = [&boardTest, &boardTestJson, &blockTest]()
618 wxString::Format(
"Block_0x%02x_offset_0x%010zx", blockTest.m_BlockType, blockTest.m_BlockOffset );
621 boost::unit_test::make_test_case(
623 testName.ToStdString(),
633 BTS* boardSuites = suites.emplace_back( BOOST_TEST_SUITE(
"AllegroBoards" ) );
643 BTS* boardSuite = BOOST_TEST_SUITE( boardTest.
m_BrdName );
644 boardSuites->add( boardSuite );
649 boost::unit_test::test_case* loadingTestCase =
nullptr;
653 const auto testLoadFunction = [&boardTest, &boardTestData]()
659 loadingTestCase = boost::unit_test::make_test_case(
666 for(
const std::string& label : testLabels )
668 loadingTestCase->add_label( label );
671 boardSuite->add( loadingTestCase );
676 BTS* expectationsSuite = BOOST_TEST_SUITE(
"Expectations" );
677 boardSuite->add( expectationsSuite );
681 const auto testRunFunction = [&boardTest, &boardTestData, &expectationTestRef]()
683 if( !boardTestData.contains(
"boardFile" ) )
686 "Board test JSON does not contain a 'boardFile' entry - cannot run expectations test" );
691 + boardTestData[
"boardFile"].get<std::string>();
697 BOOST_FAIL(
"Failed to load cached board for expectations test: " + boardFilePath );
704 boost::unit_test::test_case* expectationTestCase = boost::unit_test::make_test_case(
712 for(
const std::string& label : expectationTestRef.
m_Tags )
714 expectationTestCase->add_label( label );
717 expectationsSuite->add( expectationTestCase );
723 expectationsSuite->depends_on( loadingTestCase );
741 for( boost::unit_test::test_suite* suite : suites )
743 boost::unit_test::framework::master_test_suite().add( suite );
General utilities for PCB file IO for QA programs.
The block parser is responsible for parsing individual blocks of data from the file stream.
std::unique_ptr< BLOCK_BASE > ParseBlock(bool &aEndOfObjectsMarker)
Parse one block from the stream, returning a BLOCK_BASE representing the raw data of the block.
Stream that reads primitive types from a memory buffer containing Allegro .brd (or ....
void RunBlockTest(const std::string &aBrdName, const nlohmann::json &aBoardTestJson, const ALLEGRO_BLOCK_TEST_DESCRIPTOR &aBlockTest)
void RunHeaderTest(const std::string &aBrdName, const nlohmann::json &aBoardTestJson)
Information pertinent to a Pcbnew printed circuit board.
static ALLEGRO_CACHED_LOADER & GetInstance()
Get the singleton instance of the Allegro board cache loader.
static std::vector< DESCRIPTOR > ExtractExpectationTestsFromJson(const nlohmann::json &aExpectationArray)
Extracts expectation tests from the given JSON array and returns a list of test references that can b...
static void RunFromRef(const std::string &aBrdName, const BOARD &aBoard, const BOARD_EXPECTATION_TEST::DESCRIPTOR &aExpectationTestRef)
Constructs a BOARD_EXPECTATION_TEST from the given JSON definition, and runs it on the given board.
BOARD * LoadAndCache(const std::string &aFilePath, REPORTER *aReporter)
Load (or reload) board for the given file path and send the load messages to the given reporter.
BOARD * GetCachedBoard(const std::string &aFilePath)
Get a cached board for the given file path, or load it if not already cached, without forcing a reloa...
Custom REPORTER that captures all messages for later analysis in the unit test framework.
int GetErrorCount() const
const std::vector< MESSAGE > & GetMessages() const
FMT_VER
The format of an Allegro file.
Test utilities for timestamps.
std::string AllegroBoardDataDir(const std::string &aBoardName)
void RunAdditionalBlockTest(const std::string &aBoardName, size_t aBlockOffset, const ALLEGRO::BLOCK_BASE &aBlock)
Look up and run any additional ad-hoc tests for a block.
std::vector< uint8_t > LoadBinaryData(const std::string &aFilePath, std::optional< size_t > aLoadBytes=std::nullopt)
Load the contents of a file into a vector of bytes.
void PrintBoardStats(const BOARD *aBoard, const std::string &aBoardName)
Print detailed board statistics for debugging using test-framework logging.
Just enough information about a block-level test to be able to name and register it with the test run...
const nlohmann::json & m_BlockTestJson
Handy ref to the JSON entry for this block test.
The registry of known Allegro board and block tests, populated at static init time by reading the JSO...
const nlohmann::json & GetBoardJson(const std::string &aBoardName) const
std::vector< ALLEGRO_BOARD_TEST_DESCRIPTOR > m_BoardTests
Just enough information about the board test to be able to name and register any tests for this board...
std::vector< BOARD_EXPECTATION_TEST::DESCRIPTOR > m_ExpectationTests
List of expectation tests found in the JSON for this board.
std::vector< ALLEGRO_BLOCK_TEST_DESCRIPTOR > m_BlockTests
ALLEGRO::FMT_VER m_FormatVersion
A complete description of a block test.
uint8_t m_BlockType
The type of the block, as in the first byte.
std::string m_DataSource
The the source of the block data (probably a filename)
bool m_ExtraBlockTest
Do we have an additional block-level test to run for this block?
size_t m_BlockOffset
The offset within the board file where this block is located (used for error messages)
bool m_Skip
Whether to skip this test while parsers don't support a certain format.
Lightweight descriptor for a BOARD_EXPECTATION_TEST, which can be used to refer to the test unambiguo...
std::string m_TestName
If the test has a name, it's that, else an index - this is for naming the test for filtering.
std::vector< std::string > m_Tags
Tags associated with the test, which can be used for filtering.
static struct RegisterBlockSuites s_registerHeaderBlockSuites
static void AssertNoErrors(const CAPTURING_REPORTER &aReporter)
static std::unique_ptr< HEADER_TEST_INFO > createHeaderTestEntry(const std::string &boardDir, const nlohmann::json &headerTestEntry)
static uint8_t getByteFromHexStr(const nlohmann::json &aJsonEntry, const std::string &aFieldName)
static BOARD_TEST_INFO createBoardTestInfo(const std::string &aBrdName, const nlohmann::json &boardTestEntry)
static BLOCK_TEST_INFO createBlockTestEntry(const std::string &boardDir, const ALLEGRO_BLOCK_TEST_DESCRIPTOR &aBlockTestDescriptor)
static std::vector< uint8_t > loadDataByUri(const std::string &aDataSource)
static ALLEGRO_BLOCK_TEST_DESCRIPTOR createBlockTestDescriptor(const nlohmann::json &aBlockTestJson)
static std::vector< boost::unit_test::test_suite * > buildAllegroBoardSuites()
This function initializes the test suites for Allegro block and board parsing.
void RunBoardLoad(const std::string &aBrdName, const nlohmann::json &aBoardTestJson)
static std::vector< std::string > getBoardTestLabels(const nlohmann::json &boardTestJson)
Get the labels associated with a board test, which can be used to e.g.
static std::vector< ALLEGRO_BOARD_TEST_DESCRIPTOR > getBoardTestDefinitions(const nlohmann::json &aJson)
Construct a list of test descriptrs (lightweight objects) for the boards we have test data for,...
static const ALLEGRO_BLOCK_TEST_REGISTRY & buildTestRegistry()
ALLEGRO::FMT_VER getFormatVersionFromStr(const std::string &aFmtVerStr)
BOOST_CHECK(res.column_datatype(0)==SQL_NUMERIC||res.column_datatype(0)==SQL_DECIMAL)
BOOST_REQUIRE(res.columns()==1)
BOOST_TEST(netlist.find("R_G1 ARM_OUT1 DIE_B R='0.001 / ((SW_STATE)") !=std::string::npos)
IbisParser parser & reporter
BOOST_TEST_CONTEXT("Test Clearance")
BOOST_TEST_MESSAGE("Polyline has "<< chain.PointCount()<< " points")