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.

158 lines
4.3 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2018 KiCad Developers, see CHANGELOG.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 test_array_pad_name_provider.cpp
  25. * Test suite for the #ARRAY_PAD_NAME_PROVIDER class
  26. */
  27. #include <unit_test_utils/unit_test_utils.h>
  28. #include <array_pad_name_provider.h> // UUT
  29. #include <common.h> // make_unique
  30. #include <class_module.h>
  31. #include <class_pad.h>
  32. /**
  33. * Make a module with a given list of named pads
  34. */
  35. static std::unique_ptr<MODULE> ModuleWithPads( const std::vector<wxString> aNames )
  36. {
  37. auto module = std::make_unique<MODULE>( nullptr );
  38. for( const auto& name : aNames )
  39. {
  40. auto pad = std::make_unique<D_PAD>( module.get() );
  41. pad->SetName( name );
  42. module->Add( pad.release() );
  43. }
  44. return module;
  45. }
  46. /**
  47. * Declare the test suite
  48. */
  49. BOOST_AUTO_TEST_SUITE( ArrayPadNameProv )
  50. struct APNP_CASE
  51. {
  52. std::string m_case_name;
  53. bool m_using_module;
  54. std::vector<wxString> m_existing_pads;
  55. std::unique_ptr<ARRAY_OPTIONS> m_arr_opts;
  56. std::vector<wxString> m_exp_arr_names;
  57. };
  58. /**
  59. * Get Array Pad Name Provider cases when a module is looked
  60. * at to determine what names are available.
  61. */
  62. std::vector<APNP_CASE> GetModuleAPNPCases()
  63. {
  64. std::vector<APNP_CASE> cases;
  65. auto opts = std::make_unique<ARRAY_GRID_OPTIONS>();
  66. // simple linear numbering
  67. opts->m_2dArrayNumbering = false;
  68. opts->m_pri_axis.SetOffset( 1 );
  69. opts->m_pri_axis.SetAxisType( ARRAY_AXIS::NUMBERING_TYPE::NUMBERING_NUMERIC );
  70. cases.push_back( {
  71. "Simple linear, skip some",
  72. true,
  73. { "1", "3" },
  74. std::move( opts ),
  75. { "2", "4", "5", "6", "7" },
  76. } );
  77. // one without a module
  78. opts = std::make_unique<ARRAY_GRID_OPTIONS>();
  79. // simple linear numbering (again)
  80. opts->m_2dArrayNumbering = false;
  81. opts->m_pri_axis.SetOffset( 1 );
  82. opts->m_pri_axis.SetAxisType( ARRAY_AXIS::NUMBERING_TYPE::NUMBERING_NUMERIC );
  83. cases.push_back( {
  84. "Simple linear, no module",
  85. false,
  86. {}, // not used
  87. std::move( opts ),
  88. { "1", "2", "3", "4", "5" },
  89. } );
  90. // Grid numberings with skips don't make a lot of sense, there is
  91. // no particular contract made for them
  92. return cases;
  93. }
  94. /**
  95. * Check that an #ARRAY_PAD_NAME_PROVIDER provides the right names
  96. * @param aProvider the provider
  97. * @param aExpNames ordered list of expected names
  98. */
  99. void CheckPadNameProvider( ARRAY_PAD_NAME_PROVIDER& aProvider, std::vector<wxString> aExpNames )
  100. {
  101. std::vector<wxString> got_names;
  102. for( unsigned i = 0; i < aExpNames.size(); ++i )
  103. {
  104. got_names.push_back( aProvider.GetNextPadName() );
  105. }
  106. BOOST_CHECK_EQUAL_COLLECTIONS(
  107. aExpNames.begin(), aExpNames.end(), got_names.begin(), got_names.end() );
  108. }
  109. BOOST_AUTO_TEST_CASE( ModuleCases )
  110. {
  111. for( const auto& c : GetModuleAPNPCases() )
  112. {
  113. BOOST_TEST_CONTEXT( c.m_case_name )
  114. {
  115. std::unique_ptr<MODULE> module;
  116. if( c.m_using_module )
  117. {
  118. module = ModuleWithPads( c.m_existing_pads );
  119. }
  120. ARRAY_PAD_NAME_PROVIDER apnp( module.get(), *c.m_arr_opts );
  121. CheckPadNameProvider( apnp, c.m_exp_arr_names );
  122. }
  123. }
  124. }
  125. BOOST_AUTO_TEST_SUITE_END()