2019-04-15 17:09:27 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2021-10-06 02:46:53 +00:00
|
|
|
* Copyright (C) 2019 KiCad Developers, see AUTHORS.TXT for contributors.
|
2019-04-15 17:09:27 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Test suite for PCB "base" sexpr parsing
|
|
|
|
*/
|
|
|
|
|
2021-02-06 22:20:31 +00:00
|
|
|
#include <qa_utils/wx_utils/unit_test_utils.h>
|
2019-04-15 17:09:27 +00:00
|
|
|
|
|
|
|
// Code under test
|
|
|
|
#include <pcb/base.h>
|
|
|
|
|
|
|
|
#include <sexpr/sexpr_parser.h>
|
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
|
|
|
|
|
2019-05-24 10:56:04 +00:00
|
|
|
namespace BOOST_TEST_PRINT_NAMESPACE_OPEN
|
2019-04-15 17:09:27 +00:00
|
|
|
{
|
|
|
|
template <>
|
|
|
|
struct print_log_value<DOUBLET>
|
|
|
|
{
|
|
|
|
inline void operator()( std::ostream& os, DOUBLET const& o )
|
|
|
|
{
|
|
|
|
os << "DOUBLET[ " << o.x << ", " << o.y << " ]";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
BOOST_TEST_PRINT_NAMESPACE_CLOSE
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Radians from degrees
|
|
|
|
*/
|
|
|
|
constexpr double DegToRad( double aDeg )
|
|
|
|
{
|
|
|
|
return aDeg * M_PI / 180.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-15 17:29:03 +00:00
|
|
|
class TEST_PCB_BASE_FIXTURE
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
SEXPR::PARSER m_parser;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2019-04-15 17:09:27 +00:00
|
|
|
/**
|
|
|
|
* Declare the test suite
|
|
|
|
*/
|
2019-04-15 17:29:03 +00:00
|
|
|
BOOST_FIXTURE_TEST_SUITE( PcbBase, TEST_PCB_BASE_FIXTURE )
|
2019-04-15 17:09:27 +00:00
|
|
|
|
|
|
|
struct TEST_2D_POS_ROT
|
|
|
|
{
|
|
|
|
std::string m_sexp;
|
|
|
|
bool m_valid;
|
|
|
|
DOUBLET m_exp_pos;
|
|
|
|
double m_exp_rot;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the Get2DPositionAndRotation function
|
|
|
|
*/
|
|
|
|
BOOST_AUTO_TEST_CASE( SexprTo2DPosAndRot )
|
|
|
|
{
|
|
|
|
const std::vector<TEST_2D_POS_ROT> cases = {
|
|
|
|
{
|
|
|
|
"(at 0 0 0)",
|
|
|
|
true,
|
|
|
|
{ 0.0, 0.0 },
|
|
|
|
0.0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"(at 3.14 4.12 90.4)",
|
|
|
|
true,
|
|
|
|
{ 3.14, 4.12 },
|
|
|
|
DegToRad( 90.4 ),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"(at 3.14)", // no enough params
|
|
|
|
false,
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
},
|
|
|
|
{
|
2019-04-15 17:29:03 +00:00
|
|
|
"(att 3.14 4.12 90.4)", // bad symbol name
|
2019-04-15 17:09:27 +00:00
|
|
|
false,
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
for( const auto& c : cases )
|
|
|
|
{
|
|
|
|
BOOST_TEST_CONTEXT( c.m_sexp )
|
|
|
|
{
|
|
|
|
DOUBLET gotPos;
|
|
|
|
double gotRot;
|
|
|
|
|
2019-04-15 17:29:03 +00:00
|
|
|
std::unique_ptr<SEXPR::SEXPR> sexpr( m_parser.Parse( c.m_sexp ) );
|
2019-04-15 17:09:27 +00:00
|
|
|
|
|
|
|
const bool ret = Get2DPositionAndRotation( sexpr.get(), gotPos, gotRot );
|
|
|
|
|
|
|
|
BOOST_CHECK_EQUAL( ret, c.m_valid );
|
|
|
|
|
|
|
|
if( !ret )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const double tolPercent = 0.00001; // seems small enough
|
|
|
|
|
|
|
|
BOOST_CHECK_CLOSE( gotPos.x, c.m_exp_pos.x, tolPercent );
|
|
|
|
BOOST_CHECK_CLOSE( gotPos.y, c.m_exp_pos.y, tolPercent );
|
|
|
|
BOOST_CHECK_CLOSE( gotRot, c.m_exp_rot, tolPercent );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-15 17:29:03 +00:00
|
|
|
struct TEST_LAYER_NAME
|
|
|
|
{
|
|
|
|
std::string m_sexp;
|
|
|
|
bool m_valid;
|
|
|
|
std::string m_layer;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the layer list parser.
|
|
|
|
*/
|
|
|
|
BOOST_AUTO_TEST_CASE( TestGetLayerName )
|
|
|
|
{
|
|
|
|
const std::vector<TEST_LAYER_NAME> cases = {
|
|
|
|
{
|
|
|
|
// Quoted string - OK
|
|
|
|
"(layer \"Edge.Cuts\")",
|
|
|
|
true,
|
|
|
|
"Edge.Cuts",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Old KiCads exported without quotes (so, as symbols)
|
|
|
|
"(layer Edge.Cuts)",
|
|
|
|
true,
|
|
|
|
"Edge.Cuts",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// No atoms
|
|
|
|
"(layer)",
|
|
|
|
false,
|
|
|
|
{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Too many atoms in list
|
|
|
|
"(layer \"Edge.Cuts\" 2)",
|
|
|
|
false,
|
|
|
|
{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Wrong atom type
|
|
|
|
"(layer 2)",
|
|
|
|
false,
|
|
|
|
{},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
for( const auto& c : cases )
|
|
|
|
{
|
|
|
|
BOOST_TEST_CONTEXT( c.m_sexp )
|
|
|
|
{
|
|
|
|
std::unique_ptr<SEXPR::SEXPR> sexpr( m_parser.Parse( c.m_sexp ) );
|
|
|
|
|
|
|
|
const OPT<std::string> ret = GetLayerName( *sexpr );
|
|
|
|
|
2019-04-17 12:19:29 +00:00
|
|
|
BOOST_CHECK_EQUAL( !!ret, c.m_valid );
|
2019-04-15 17:29:03 +00:00
|
|
|
|
|
|
|
if( ret )
|
|
|
|
{
|
|
|
|
BOOST_CHECK_EQUAL( *ret, c.m_layer );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-15 17:09:27 +00:00
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|