You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

243 lines
6.6 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2019-2023 KiCad Developers, see AUTHORS.TXT for contributors.
  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, you may find one here:
  18. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  19. * or you may search the http://www.gnu.org website for the version 2 license,
  20. * or you may write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  22. */
  23. /**
  24. * @file
  25. * Test suite for SCH_SHEET
  26. */
  27. #include <qa_utils/wx_utils/unit_test_utils.h>
  28. // Code under test
  29. #include <sch_sheet.h>
  30. #include <sch_sheet_pin.h>
  31. #include <schematic.h>
  32. #include <qa_utils/uuid_test_utils.h>
  33. #include <qa_utils/wx_utils/wx_assert.h>
  34. class TEST_SCH_SHEET_FIXTURE
  35. {
  36. public:
  37. TEST_SCH_SHEET_FIXTURE() :
  38. m_schematic( nullptr ),
  39. m_sheet(),
  40. m_csheet( m_sheet )
  41. {
  42. }
  43. ///< Dummy schematic to attach the test sheet to
  44. SCHEMATIC m_schematic;
  45. SCH_SHEET m_sheet;
  46. ///< Can use when you need a const ref (lots of places need fixing here)
  47. const SCH_SHEET& m_csheet;
  48. };
  49. /**
  50. * Print helper.
  51. * Not a print_log_value because old Boosts don't like that in BOOST_CHECK_EQUAL_COLLECTIONS
  52. */
  53. std::ostream& operator<<( std::ostream& os, DANGLING_END_ITEM const& d )
  54. {
  55. os << "DANGLING_END_ITEM[ type " << d.GetType() << " @(" << d.GetPosition().x << ", "
  56. << d.GetPosition().y << "), item " << d.GetItem() << ", parent " << d.GetParent() << " ]";
  57. return os;
  58. }
  59. /**
  60. * Declare the test suite
  61. */
  62. BOOST_FIXTURE_TEST_SUITE( SchSheet, TEST_SCH_SHEET_FIXTURE )
  63. /**
  64. * Check default properties
  65. */
  66. BOOST_AUTO_TEST_CASE( Default )
  67. {
  68. BOOST_CHECK_EQUAL( m_csheet.GetPosition(), VECTOR2I( 0, 0 ) );
  69. BOOST_CHECK_EQUAL( m_sheet.GetParent(), nullptr );
  70. BOOST_CHECK_EQUAL( m_sheet.CountSheets(), 1 );
  71. BOOST_CHECK_EQUAL( m_csheet.GetScreenCount(), 0 );
  72. BOOST_CHECK_EQUAL( m_sheet.SymbolCount(), 0 );
  73. }
  74. /**
  75. * Test setting parent schematic
  76. */
  77. BOOST_AUTO_TEST_CASE( SchematicParent )
  78. {
  79. m_sheet.SetParent( &m_schematic );
  80. BOOST_CHECK_EQUAL( m_sheet.IsRootSheet(), false );
  81. m_schematic.SetRoot( &m_sheet );
  82. BOOST_CHECK_EQUAL( m_sheet.IsRootSheet(), true );
  83. }
  84. /**
  85. * Test adding pins to a sheet
  86. */
  87. BOOST_AUTO_TEST_CASE( AddPins )
  88. {
  89. const VECTOR2I pinPos{ 42, 13 };
  90. // we should catch null insertions
  91. CHECK_WX_ASSERT( m_sheet.AddPin( nullptr ) );
  92. auto newPin = std::make_unique<SCH_SHEET_PIN>( &m_sheet, pinPos, "pinname" );
  93. // can't be const because of RemovePin (?!)
  94. SCH_SHEET_PIN& pinRef = *newPin;
  95. m_sheet.AddPin( newPin.release() );
  96. // now we can find it in the list
  97. BOOST_CHECK_EQUAL( m_sheet.HasPins(), true );
  98. BOOST_CHECK_EQUAL( m_sheet.HasPin( "pinname" ), true );
  99. BOOST_CHECK_EQUAL( m_sheet.HasPin( "PINname" ), false );
  100. BOOST_CHECK_EQUAL( m_sheet.GetPin( pinPos ), &pinRef );
  101. // check the actual list can be retrieved
  102. std::vector<SCH_SHEET_PIN*>& pins = m_sheet.GetPins();
  103. BOOST_CHECK_EQUAL( pins[0], &pinRef );
  104. // catch the bad call
  105. CHECK_WX_ASSERT( m_sheet.RemovePin( nullptr ) );
  106. m_sheet.RemovePin( &pinRef );
  107. // and it's gone
  108. BOOST_CHECK_EQUAL( m_sheet.HasPins(), false );
  109. BOOST_CHECK_EQUAL( m_sheet.HasPin( "pinname" ), false );
  110. BOOST_CHECK_EQUAL( m_sheet.GetPin( pinPos ), nullptr );
  111. delete &pinRef;
  112. }
  113. /**
  114. * Check that pins are added and renumbered to be unique
  115. */
  116. BOOST_AUTO_TEST_CASE( PinRenumbering )
  117. {
  118. for( int i = 0; i < 5; ++i )
  119. {
  120. SCH_SHEET_PIN* pin = new SCH_SHEET_PIN( &m_sheet, VECTOR2I{ i, i }, "name" );
  121. // set the pins to have the same number going in
  122. pin->SetNumber( 2 );
  123. m_sheet.AddPin( pin );
  124. }
  125. std::vector<SCH_SHEET_PIN*>& pins = m_sheet.GetPins();
  126. std::vector<int> numbers;
  127. for( SCH_SHEET_PIN* pin : pins )
  128. numbers.push_back( pin->GetNumber() );
  129. // and now...they are all unique
  130. BOOST_CHECK_PREDICATE( KI_TEST::CollectionHasNoDuplicates<decltype( numbers )>, ( numbers ) );
  131. }
  132. struct TEST_END_CONN_PIN
  133. {
  134. std::string m_pin_name;
  135. VECTOR2I m_pos;
  136. };
  137. /**
  138. * Test the endpoint and connection point collections: we should be able to add pins, then
  139. * have them appear as endpoints.
  140. */
  141. BOOST_AUTO_TEST_CASE( EndconnectionPoints )
  142. {
  143. // x = zero because the pin is clamped to the left side by default
  144. const std::vector<TEST_END_CONN_PIN> pin_defs = {
  145. {
  146. "1name",
  147. { 0, 13 },
  148. },
  149. {
  150. "2name",
  151. { 0, 130 },
  152. },
  153. };
  154. // Insert the pins into the sheet
  155. for( const auto& pin : pin_defs )
  156. m_sheet.AddPin( new SCH_SHEET_PIN( &m_sheet, pin.m_pos, pin.m_pin_name ) );
  157. std::vector<SCH_SHEET_PIN*>& pins = m_sheet.GetPins();
  158. // make sure the pins made it in
  159. BOOST_CHECK_EQUAL( pins.size(), pin_defs.size() );
  160. // Check that the EndPoint getter gets the right things
  161. {
  162. std::vector<DANGLING_END_ITEM> expectedDangling;
  163. // Construct expected from the pin, not defs, as we need the pin address
  164. for( SCH_SHEET_PIN* pin : pins )
  165. {
  166. expectedDangling.emplace_back( DANGLING_END_T::SHEET_LABEL_END, pin,
  167. pin->GetPosition(), pin );
  168. }
  169. std::vector<DANGLING_END_ITEM> dangling;
  170. m_sheet.GetEndPoints( dangling );
  171. BOOST_CHECK_EQUAL_COLLECTIONS( dangling.begin(), dangling.end(),
  172. expectedDangling.begin(), expectedDangling.end() );
  173. }
  174. // And check the connection getter
  175. {
  176. std::vector<VECTOR2I> expectedConnections;
  177. // we want to see every pin that we just added
  178. for( const auto& pin : pin_defs )
  179. {
  180. expectedConnections.push_back( pin.m_pos );
  181. }
  182. std::vector<VECTOR2I> connections = m_sheet.GetConnectionPoints();
  183. BOOST_CHECK_EQUAL_COLLECTIONS( connections.begin(), connections.end(),
  184. expectedConnections.begin(), expectedConnections.end() );
  185. }
  186. }
  187. BOOST_AUTO_TEST_SUITE_END()