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.

201 lines
4.5 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 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
  25. * Test suite for PCB "base" sexpr parsing
  26. */
  27. #include <unit_test_utils/unit_test_utils.h>
  28. // Code under test
  29. #include <pcb/base.h>
  30. #include <sexpr/sexpr_parser.h>
  31. #include <cmath>
  32. BOOST_TEST_PRINT_NAMESPACE_OPEN
  33. {
  34. template <>
  35. struct print_log_value<DOUBLET>
  36. {
  37. inline void operator()( std::ostream& os, DOUBLET const& o )
  38. {
  39. os << "DOUBLET[ " << o.x << ", " << o.y << " ]";
  40. }
  41. };
  42. }
  43. BOOST_TEST_PRINT_NAMESPACE_CLOSE
  44. /**
  45. * Radians from degrees
  46. */
  47. constexpr double DegToRad( double aDeg )
  48. {
  49. return aDeg * M_PI / 180.0;
  50. }
  51. class TEST_PCB_BASE_FIXTURE
  52. {
  53. public:
  54. SEXPR::PARSER m_parser;
  55. };
  56. /**
  57. * Declare the test suite
  58. */
  59. BOOST_FIXTURE_TEST_SUITE( PcbBase, TEST_PCB_BASE_FIXTURE )
  60. struct TEST_2D_POS_ROT
  61. {
  62. std::string m_sexp;
  63. bool m_valid;
  64. DOUBLET m_exp_pos;
  65. double m_exp_rot;
  66. };
  67. /**
  68. * Test the Get2DPositionAndRotation function
  69. */
  70. BOOST_AUTO_TEST_CASE( SexprTo2DPosAndRot )
  71. {
  72. const std::vector<TEST_2D_POS_ROT> cases = {
  73. {
  74. "(at 0 0 0)",
  75. true,
  76. { 0.0, 0.0 },
  77. 0.0,
  78. },
  79. {
  80. "(at 3.14 4.12 90.4)",
  81. true,
  82. { 3.14, 4.12 },
  83. DegToRad( 90.4 ),
  84. },
  85. {
  86. "(at 3.14)", // no enough params
  87. false,
  88. {},
  89. {},
  90. },
  91. {
  92. "(att 3.14 4.12 90.4)", // bad symbol name
  93. false,
  94. {},
  95. {},
  96. },
  97. };
  98. for( const auto& c : cases )
  99. {
  100. BOOST_TEST_CONTEXT( c.m_sexp )
  101. {
  102. DOUBLET gotPos;
  103. double gotRot;
  104. std::unique_ptr<SEXPR::SEXPR> sexpr( m_parser.Parse( c.m_sexp ) );
  105. const bool ret = Get2DPositionAndRotation( sexpr.get(), gotPos, gotRot );
  106. BOOST_CHECK_EQUAL( ret, c.m_valid );
  107. if( !ret )
  108. continue;
  109. const double tolPercent = 0.00001; // seems small enough
  110. BOOST_CHECK_CLOSE( gotPos.x, c.m_exp_pos.x, tolPercent );
  111. BOOST_CHECK_CLOSE( gotPos.y, c.m_exp_pos.y, tolPercent );
  112. BOOST_CHECK_CLOSE( gotRot, c.m_exp_rot, tolPercent );
  113. }
  114. }
  115. }
  116. struct TEST_LAYER_NAME
  117. {
  118. std::string m_sexp;
  119. bool m_valid;
  120. std::string m_layer;
  121. };
  122. /**
  123. * Test the layer list parser.
  124. */
  125. BOOST_AUTO_TEST_CASE( TestGetLayerName )
  126. {
  127. const std::vector<TEST_LAYER_NAME> cases = {
  128. {
  129. // Quoted string - OK
  130. "(layer \"Edge.Cuts\")",
  131. true,
  132. "Edge.Cuts",
  133. },
  134. {
  135. // Old KiCads exported without quotes (so, as symbols)
  136. "(layer Edge.Cuts)",
  137. true,
  138. "Edge.Cuts",
  139. },
  140. {
  141. // No atoms
  142. "(layer)",
  143. false,
  144. {},
  145. },
  146. {
  147. // Too many atoms in list
  148. "(layer \"Edge.Cuts\" 2)",
  149. false,
  150. {},
  151. },
  152. {
  153. // Wrong atom type
  154. "(layer 2)",
  155. false,
  156. {},
  157. },
  158. };
  159. for( const auto& c : cases )
  160. {
  161. BOOST_TEST_CONTEXT( c.m_sexp )
  162. {
  163. std::unique_ptr<SEXPR::SEXPR> sexpr( m_parser.Parse( c.m_sexp ) );
  164. const OPT<std::string> ret = GetLayerName( *sexpr );
  165. BOOST_CHECK_EQUAL( ret.has_value(), c.m_valid );
  166. if( ret )
  167. {
  168. BOOST_CHECK_EQUAL( *ret, c.m_layer );
  169. }
  170. }
  171. }
  172. }
  173. BOOST_AUTO_TEST_SUITE_END()