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.

224 lines
5.3 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2019 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 #ARRAY_AXIS
  26. */
  27. #include <qa_utils/wx_utils/unit_test_utils.h>
  28. #include <array_axis.h>
  29. /**
  30. * Declare the test suite
  31. */
  32. BOOST_AUTO_TEST_SUITE( ArrayAxis )
  33. struct VALID_OFFSET_CASE
  34. {
  35. ARRAY_AXIS::NUMBERING_TYPE m_axis_type;
  36. std::string m_offset_str;
  37. bool m_exp_valid;
  38. int m_exp_offset;
  39. };
  40. /**
  41. * Check we can get valid (or invalid) offsets as expected
  42. */
  43. BOOST_AUTO_TEST_CASE( ValidOffsets )
  44. {
  45. // clang-format off
  46. const std::vector<VALID_OFFSET_CASE> cases = {
  47. {
  48. ARRAY_AXIS::NUMBERING_TYPE::NUMBERING_NUMERIC,
  49. "0",
  50. true,
  51. 0,
  52. },
  53. {
  54. ARRAY_AXIS::NUMBERING_TYPE::NUMBERING_NUMERIC,
  55. "1",
  56. true,
  57. 1,
  58. },
  59. {
  60. ARRAY_AXIS::NUMBERING_TYPE::NUMBERING_NUMERIC,
  61. "1234",
  62. true,
  63. 1234,
  64. },
  65. {
  66. ARRAY_AXIS::NUMBERING_TYPE::NUMBERING_NUMERIC,
  67. "",
  68. false,
  69. 0,
  70. },
  71. {
  72. ARRAY_AXIS::NUMBERING_TYPE::NUMBERING_NUMERIC,
  73. "www",
  74. false,
  75. 0,
  76. },
  77. {
  78. ARRAY_AXIS::NUMBERING_TYPE::NUMBERING_ALPHA_FULL,
  79. "A",
  80. true,
  81. 0,
  82. },
  83. {
  84. ARRAY_AXIS::NUMBERING_TYPE::NUMBERING_ALPHA_FULL,
  85. "XY",
  86. true,
  87. 648,
  88. },
  89. {
  90. ARRAY_AXIS::NUMBERING_TYPE::NUMBERING_HEX,
  91. "A0",
  92. true,
  93. 160,
  94. },
  95. {
  96. ARRAY_AXIS::NUMBERING_TYPE::NUMBERING_HEX,
  97. "G0",
  98. false,
  99. 0,
  100. },
  101. };
  102. // clang-format on
  103. for( const auto& c : cases )
  104. {
  105. ARRAY_AXIS axis;
  106. axis.SetAxisType( c.m_axis_type );
  107. bool offset_ok = axis.SetOffset( c.m_offset_str );
  108. BOOST_CHECK_EQUAL( offset_ok, c.m_exp_valid );
  109. if( c.m_exp_valid )
  110. {
  111. BOOST_CHECK_EQUAL( axis.GetOffset(), c.m_exp_offset );
  112. }
  113. }
  114. }
  115. /**
  116. * Data for testing a single array axis
  117. */
  118. struct ARRAY_AXIS_NAMING_PARAMS
  119. {
  120. ARRAY_AXIS::NUMBERING_TYPE m_axis_type;
  121. std::string m_start_at;
  122. int m_step;
  123. };
  124. struct ARRAY_AXIS_NAMING_CASE
  125. {
  126. std::string m_case_name;
  127. ARRAY_AXIS_NAMING_PARAMS m_prms;
  128. int m_num;
  129. std::vector<std::string> m_exp_names;
  130. };
  131. // clang-format off
  132. static const std::vector<ARRAY_AXIS_NAMING_CASE> axis_name_cases = {
  133. {
  134. "Numeric",
  135. {
  136. ARRAY_AXIS::NUMBERING_TYPE::NUMBERING_NUMERIC,
  137. "1",
  138. 1,
  139. },
  140. 6,
  141. { "1", "2", "3", "4", "5", "6" },
  142. },
  143. {
  144. // Test alphabetical
  145. "Alpha",
  146. {
  147. ARRAY_AXIS::NUMBERING_TYPE::NUMBERING_ALPHA_FULL,
  148. "A",
  149. 1,
  150. },
  151. 3,
  152. { "A", "B", "C" },
  153. },
  154. {
  155. // Test alphabetical with 2nd col
  156. "Alpha 2nd col",
  157. {
  158. ARRAY_AXIS::NUMBERING_TYPE::NUMBERING_ALPHA_FULL,
  159. "Y",
  160. 1,
  161. },
  162. 4,
  163. { "Y", "Z", "AA", "AB" },
  164. },
  165. {
  166. "Numeric skip",
  167. {
  168. ARRAY_AXIS::NUMBERING_TYPE::NUMBERING_NUMERIC,
  169. "11",
  170. 2,
  171. },
  172. 6,
  173. { "11", "13", "15", "17", "19", "21" },
  174. },
  175. };
  176. // clang-format on
  177. /**
  178. * Test of the naming cases
  179. */
  180. BOOST_AUTO_TEST_CASE( Numbering )
  181. {
  182. for( const auto& c : axis_name_cases )
  183. {
  184. BOOST_TEST_CONTEXT( c.m_case_name )
  185. {
  186. ARRAY_AXIS axis;
  187. axis.SetAxisType( c.m_prms.m_axis_type );
  188. axis.SetStep( c.m_prms.m_step );
  189. bool start_ok = axis.SetOffset( c.m_prms.m_start_at );
  190. // All these examples have valid start offsets
  191. BOOST_CHECK( start_ok );
  192. std::vector<std::string> names;
  193. for( int i = 0; i < c.m_num; i++ )
  194. {
  195. names.push_back( axis.GetItemNumber( i ).ToStdString() );
  196. }
  197. BOOST_CHECK_EQUAL_COLLECTIONS(
  198. names.begin(), names.end(), c.m_exp_names.begin(), c.m_exp_names.end() );
  199. }
  200. }
  201. }
  202. BOOST_AUTO_TEST_SUITE_END()