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.

216 lines
6.4 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software: you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation, either version 3 of the License, or (at your
  9. * option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #define BOOST_TEST_NO_MAIN
  20. #include <boost/test/unit_test.hpp>
  21. #include <lset.h>
  22. #include <lseq.h>
  23. // Macros for easier test specification
  24. #define PCB_LAYER_COUNT PCB_LAYER_ID_COUNT
  25. BOOST_AUTO_TEST_SUITE(LSETTests)
  26. // Initialize an empty LSET
  27. BOOST_AUTO_TEST_CASE(LSETConstructorEmpty)
  28. {
  29. LSET set;
  30. BOOST_CHECK_EQUAL(set.count(), 0);
  31. }
  32. // Initialize LSET from another BASE_SET
  33. BOOST_AUTO_TEST_CASE(LSETConstructorFromBaseSet)
  34. {
  35. BASE_SET base( PCB_LAYER_ID_COUNT );
  36. base.set(F_Cu);
  37. base.set(In1_Cu);
  38. LSET set(base);
  39. BOOST_CHECK_EQUAL(set.count(), 2);
  40. BOOST_CHECK(set.test(F_Cu));
  41. BOOST_CHECK(set.test(In1_Cu));
  42. }
  43. // Initialize LSET from a specific PCB_LAYER_ID
  44. BOOST_AUTO_TEST_CASE(LSETConstructorFromLayer)
  45. {
  46. LSET set( { F_Cu } );
  47. BOOST_CHECK_EQUAL(set.count(), 1);
  48. BOOST_CHECK(set.test(F_Cu));
  49. }
  50. // Initialize LSET from an initializer list
  51. BOOST_AUTO_TEST_CASE(LSETConstructorFromList)
  52. {
  53. LSET set({F_Cu, In1_Cu, In2_Cu});
  54. BOOST_CHECK_EQUAL(set.count(), 3);
  55. BOOST_CHECK(set.test(F_Cu));
  56. BOOST_CHECK(set.test(In1_Cu));
  57. BOOST_CHECK(set.test(In2_Cu));
  58. }
  59. // Initialize LSET from LSEQ
  60. BOOST_AUTO_TEST_CASE(LSETConstructorFromSequence)
  61. {
  62. LSEQ seq = {F_Cu, In1_Cu, In2_Cu};
  63. LSET set(seq);
  64. BOOST_CHECK_EQUAL(set.count(), 3);
  65. BOOST_CHECK(set.test(F_Cu));
  66. BOOST_CHECK(set.test(In1_Cu));
  67. BOOST_CHECK(set.test(In2_Cu));
  68. }
  69. // Test Containment Check
  70. BOOST_AUTO_TEST_CASE(LSETContains)
  71. {
  72. LSET set({F_Cu, In1_Cu, In2_Cu});
  73. BOOST_CHECK(set.Contains(F_Cu));
  74. BOOST_CHECK(set.Contains(In1_Cu));
  75. BOOST_CHECK(!set.Contains(In30_Cu));
  76. }
  77. // Test Sequence Generation
  78. BOOST_AUTO_TEST_CASE(LSETSequenceGeneration)
  79. {
  80. LSET set({F_Cu, In1_Cu, In2_Cu});
  81. LSEQ sequence = set.Seq();
  82. BOOST_CHECK_EQUAL(sequence.size(), 3);
  83. BOOST_CHECK_EQUAL(sequence[0], F_Cu);
  84. BOOST_CHECK_EQUAL(sequence[1], In1_Cu);
  85. BOOST_CHECK_EQUAL(sequence[2], In2_Cu);
  86. }
  87. // Test Hex and Binary Formatting
  88. BOOST_AUTO_TEST_CASE(LSETFormatting)
  89. {
  90. LSET set({F_Cu, In1_Cu, In2_Cu});
  91. std::string hexString = set.FmtHex();
  92. std::string expectedHexString = "00000000_00000000_00000000_00000051"; // depends on bit ordering
  93. BOOST_CHECK_EQUAL(hexString, expectedHexString);
  94. std::string binString = set.FmtBin();
  95. std::string expectedBinString = "0000_0000|0000_0000|0000_0000|0000_0000|0000_0000|0000_0000|0000_0000|0000_0000|"
  96. "0000_0000|0000_0000|0000_0000|0000_0000|0000_0000|0000_0000|0000_0000|0101_0001"; // depends on bit ordering
  97. BOOST_CHECK_EQUAL(binString, expectedBinString);
  98. }
  99. // Test ExtractLayer and Flip
  100. BOOST_AUTO_TEST_CASE(LSETManipulations)
  101. {
  102. LSET set({F_Cu, In1_Cu, In2_Cu});
  103. // Test ExtractLayer: should extract the layer set or undefined if more than one
  104. PCB_LAYER_ID extractedLayer = set.ExtractLayer();
  105. BOOST_CHECK_EQUAL(extractedLayer, UNDEFINED_LAYER);
  106. // Test Flip: should swap front and back layers
  107. set.Flip( 4 );
  108. BOOST_CHECK(set.Contains(B_Cu));
  109. BOOST_CHECK(set.Contains(In1_Cu)); // Internal layers remain unchanged
  110. // Test setting a single layer
  111. set = {F_Cu};
  112. extractedLayer = set.ExtractLayer();
  113. BOOST_CHECK_EQUAL(extractedLayer, F_Cu);
  114. set.Flip();
  115. extractedLayer = set.ExtractLayer();
  116. BOOST_CHECK_EQUAL(extractedLayer, B_Cu);
  117. }
  118. // Test Static Mask Methods
  119. BOOST_AUTO_TEST_CASE(LSETStaticMasks)
  120. {
  121. LSET internalCuMask = LSET::InternalCuMask();
  122. BOOST_CHECK(internalCuMask.Contains(PCB_LAYER_ID::In1_Cu));
  123. BOOST_CHECK(internalCuMask.Contains(PCB_LAYER_ID::In30_Cu));
  124. BOOST_CHECK(!internalCuMask.Contains(PCB_LAYER_ID::F_Cu));
  125. BOOST_CHECK(!internalCuMask.Contains(PCB_LAYER_ID::B_Cu));
  126. }
  127. // Auxiliary function to compare LSEQ objects
  128. bool compareLSEQ( const LSEQ& seq1, const LSEQ& seq2 )
  129. {
  130. if( seq1.size() != seq2.size() )
  131. return false;
  132. for( size_t i = 0; i < seq1.size(); ++i )
  133. {
  134. if( seq1[i] != seq2[i] )
  135. return false;
  136. }
  137. return true;
  138. }
  139. // Test LSET::Seq base case
  140. BOOST_AUTO_TEST_CASE( LSETSeqBaseCase )
  141. {
  142. LSET lset( { F_Cu, B_Cu, In1_Cu, In2_Cu } );
  143. LSEQ expected = { F_Cu, B_Cu, In1_Cu, In2_Cu };
  144. LSEQ result = lset.Seq();
  145. BOOST_CHECK( compareLSEQ( result, expected ) );
  146. }
  147. // Test LSET::Seq empty case
  148. BOOST_AUTO_TEST_CASE( LSETSeqEmptyCase )
  149. {
  150. LSET lset;
  151. LSEQ expected = {};
  152. LSEQ result = lset.Seq();
  153. BOOST_CHECK( compareLSEQ( result, expected ) );
  154. }
  155. // Test LSET::SeqStackupTop2Bottom base case
  156. BOOST_AUTO_TEST_CASE( LSETSeqStackupTop2BottomBaseCase )
  157. {
  158. LSET lset( { F_Cu, B_Cu, In1_Cu, In2_Cu, F_SilkS, B_SilkS, Edge_Cuts, Margin, Dwgs_User } );
  159. LSEQ expected = { Edge_Cuts, Margin, Dwgs_User, F_SilkS, F_Cu,
  160. In1_Cu, In2_Cu, B_Cu, B_SilkS };
  161. LSEQ result = lset.SeqStackupTop2Bottom( UNDEFINED_LAYER );
  162. BOOST_CHECK( compareLSEQ( result, expected ) );
  163. }
  164. // Test LSET::SeqStackupTop2Bottom prioritizing selected layer
  165. BOOST_AUTO_TEST_CASE( LSETSeqStackupTop2BottomWithSelection )
  166. {
  167. LSET lset( { F_Cu, B_Cu, In1_Cu, In2_Cu, F_SilkS, B_SilkS, Edge_Cuts, Margin, Dwgs_User } );
  168. LSEQ expected = { F_SilkS, Edge_Cuts, Margin, Dwgs_User, F_Cu, In1_Cu, In2_Cu, B_Cu, B_SilkS };
  169. LSEQ result = lset.SeqStackupTop2Bottom( F_SilkS );
  170. BOOST_CHECK( compareLSEQ( result, expected ) );
  171. }
  172. // Test LSET::SeqStackupForPlotting base case
  173. BOOST_AUTO_TEST_CASE( LSETSeqStackupForPlottingBaseCase )
  174. {
  175. LSET lset( { F_Cu, B_Cu, In1_Cu, In2_Cu, F_SilkS, B_SilkS, Edge_Cuts, Margin } );
  176. LSEQ expected = { B_Cu, B_SilkS, In2_Cu, In1_Cu, F_Cu, F_SilkS, Margin, Edge_Cuts };
  177. LSEQ result = lset.SeqStackupForPlotting();
  178. BOOST_CHECK( compareLSEQ( result, expected ) );
  179. }
  180. BOOST_AUTO_TEST_SUITE_END()