KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sexpr_test_utils.h
Go to the documentation of this file.
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
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, see <https://www.gnu.org/licenses/>.
18 */
19
20#ifndef TEST_SEXPR_TEST_UTILS__H
21#define TEST_SEXPR_TEST_UTILS__H
22
23#include <sexpr/sexpr.h>
24
26
27namespace KI_TEST
28{
29
30
34inline SEXPR::SEXPR_TYPE getType( const SEXPR::SEXPR& aSexpr )
35{
36 if( aSexpr.IsList() )
38 else if( aSexpr.IsSymbol() )
40 else if( aSexpr.IsString() )
42 else if( aSexpr.IsDouble() )
44 else if( aSexpr.IsInteger() )
46
47 throw( std::invalid_argument( "<Unknown S-Expression Type!>" ) );
48}
49
53inline std::string getDebugType( SEXPR::SEXPR_TYPE aType )
54{
55 switch( aType )
56 {
58 return "List";
60 return "Symbol";
62 return "String";
64 return "Double";
66 return "Integer";
67 }
68
69 return "<Unknown S-Expression Type!>";
70}
71
72inline std::string GetSexprDebugType( const SEXPR::SEXPR& aSexpr )
73{
74 return getDebugType( getType( aSexpr ) );
75}
76
77inline bool IsSexprOfType( const SEXPR::SEXPR& aSexpr, SEXPR::SEXPR_TYPE aType )
78{
79 if( getType( aSexpr ) != aType )
80 {
81 BOOST_TEST_MESSAGE( "Sexpr is not of type: " << getDebugType( aType ) );
82 return false;
83 }
84
85 return true;
86}
87
93template <typename VAL_T>
94bool IsSexprValueEqual( const VAL_T& aGot, const VAL_T& aExpected )
95{
96 if( aGot != aExpected )
97 {
98 BOOST_TEST_MESSAGE( "Sexpr value not equal: got " << aGot << ", expected " << aExpected );
99 return false;
100 }
101
102 return true;
103}
104
108inline bool SexprIsSymbol( const SEXPR::SEXPR& aSexpr )
109{
111}
112
116inline bool SexprIsSymbolWithValue( const SEXPR::SEXPR& aSexpr, const std::string& aVal )
117{
119 && IsSexprValueEqual( aSexpr.GetSymbol(), aVal );
120}
121
125inline bool SexprIsString( const SEXPR::SEXPR& aSexpr )
126{
128}
129
133inline bool SexprIsStringWithValue( const SEXPR::SEXPR& aSexpr, const std::string& aVal )
134{
136 && IsSexprValueEqual( aSexpr.GetString(), aVal );
137}
138
142inline bool SexprIsInteger( const SEXPR::SEXPR& aSexpr )
143{
145}
146
150inline bool SexprIsIntegerWithValue( const SEXPR::SEXPR& aSexpr, std::int64_t aVal )
151{
153 && IsSexprValueEqual( aSexpr.GetLongInteger(), aVal );
154}
155
159inline bool SexprIsDouble( const SEXPR::SEXPR& aSexpr )
160{
162}
163
167inline bool SexprIsDoubleWithValue( const SEXPR::SEXPR& aSexpr, double aVal )
168{
170 && IsSexprValueEqual( aSexpr.GetDouble(), aVal );
171}
172
176inline bool SexprIsList( const SEXPR::SEXPR& aSexpr )
177{
179}
180
184inline bool SexprIsListOfLength( const SEXPR::SEXPR& aSexpr, size_t aExpectedLength )
185{
187 return false;
188
189 if( aSexpr.GetNumberOfChildren() != aExpectedLength )
190 {
191 BOOST_TEST_MESSAGE( "List is wrong length: got " << aSexpr.GetNumberOfChildren()
192 << ", expected " << aExpectedLength );
193 return false;
194 }
195
196 return true;
197}
198
205inline bool SexprConvertsToString( const SEXPR::SEXPR& aSexpr, const std::string& aExpStr )
206{
207 const std::string converted = aSexpr.AsString();
208
209 bool ok = true;
210
211 if( converted != aExpStr )
212 {
213 BOOST_TEST_INFO( "Sexpr string conversion mismatch: got '" << converted << "', expected '"
214 << aExpStr << "'" );
215 ok = false;
216 }
217
218 return ok;
219}
220
221} // namespace KI_TEST
222
223
224namespace SEXPR
225{
229inline std::ostream& boost_test_print_type( std::ostream& os, const SEXPR& aSexpr )
230{
231 os << "SEXPR [ " << KI_TEST::GetSexprDebugType( aSexpr ) << " ]\n " << aSexpr.AsString();
232 return os;
233}
234
235} // namespace SEXPR
236
237#endif // TEST_SEXPR_TEST_UTILS__H
bool IsInteger() const
Definition sexpr.h:53
std::string const & GetSymbol() const
Definition sexpr.cpp:130
std::string AsString(size_t aLevel=0) const
Definition sexpr.cpp:153
bool IsString() const
Definition sexpr.h:51
size_t GetNumberOfChildren() const
Definition sexpr.cpp:72
bool IsList() const
Definition sexpr.h:49
int64_t GetLongInteger() const
Definition sexpr.cpp:97
bool IsDouble() const
Definition sexpr.h:52
bool IsSymbol() const
Definition sexpr.h:50
std::string const & GetString() const
Definition sexpr.cpp:82
double GetDouble() const
Definition sexpr.cpp:107
bool SexprIsDouble(const SEXPR::SEXPR &aSexpr)
Test predicate: is the s-expression a double?
bool SexprIsList(const SEXPR::SEXPR &aSexpr)
Test predicate: is the s-expression a double?
bool SexprIsSymbolWithValue(const SEXPR::SEXPR &aSexpr, const std::string &aVal)
Test predicate: is the s-expression a symbol with the given value?
std::string getDebugType(SEXPR::SEXPR_TYPE aType)
Get a debug-friendly string for a given s-expr type.
bool IsSexprOfType(const SEXPR::SEXPR &aSexpr, SEXPR::SEXPR_TYPE aType)
bool SexprIsInteger(const SEXPR::SEXPR &aSexpr)
Test predicate: is the s-expression an integer?
bool SexprIsDoubleWithValue(const SEXPR::SEXPR &aSexpr, double aVal)
Test predicate: is the s-expression a double with the given value?
bool SexprIsListOfLength(const SEXPR::SEXPR &aSexpr, size_t aExpectedLength)
Test predicate: is the s-expression a list with the given length?
SEXPR::SEXPR_TYPE getType(const SEXPR::SEXPR &aSexpr)
Get the type of the s-expression.
std::string GetSexprDebugType(const SEXPR::SEXPR &aSexpr)
bool SexprIsIntegerWithValue(const SEXPR::SEXPR &aSexpr, std::int64_t aVal)
Test predicate: is the s-expression an integer with the given value?
bool SexprIsStringWithValue(const SEXPR::SEXPR &aSexpr, const std::string &aVal)
Test predicate: is the s-expression a string with the given value?
bool SexprIsSymbol(const SEXPR::SEXPR &aSexpr)
Test predicate: is the s-expression a symbol?
bool IsSexprValueEqual(const VAL_T &aGot, const VAL_T &aExpected)
Predicate to check two s-expr values (of the same type) are equal.
bool SexprConvertsToString(const SEXPR::SEXPR &aSexpr, const std::string &aExpStr)
Predicate to check an SEXPR object converts to the expected string.
bool SexprIsString(const SEXPR::SEXPR &aSexpr)
Test predicate: is the s-expression a string?
std::ostream & boost_test_print_type(std::ostream &os, const SEXPR &aSexpr)
Boost print helper for SEXPR objects.
SEXPR_TYPE
Definition sexpr.h:35
@ SEXPR_TYPE_ATOM_SYMBOL
Definition sexpr.h:40
@ SEXPR_TYPE_ATOM_INTEGER
Definition sexpr.h:37
@ SEXPR_TYPE_ATOM_DOUBLE
Definition sexpr.h:38
@ SEXPR_TYPE_ATOM_STRING
Definition sexpr.h:39
BOOST_TEST_INFO("Two-port Series .op current = "<< iDevice)
BOOST_TEST_MESSAGE("\n=== Real-World Polygon PIP Benchmark ===\n"<< formatTable(table))