KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_board_statistics.cpp
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, 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
25#include <board_statistics.h>
27#include <base_units.h>
28#include <algorithm>
29#include <vector>
30
31
32BOOST_AUTO_TEST_SUITE( BoardStatistics )
33
34
35
39BOOST_AUTO_TEST_CASE( DrillCompareStrictWeakOrderingPlated )
40{
41 // Create test items with different plated values
42 DRILL_LINE_ITEM platedItem( 100, 100, PAD_DRILL_SHAPE::CIRCLE, true, true, F_Cu, B_Cu );
43 DRILL_LINE_ITEM unplatedItem( 100, 100, PAD_DRILL_SHAPE::CIRCLE, false, true, F_Cu, B_Cu );
44
45 platedItem.m_Qty = 1;
46 unplatedItem.m_Qty = 1;
47
48 // Test ascending comparator
50
51 // Strict weak ordering requirement 1: irreflexivity - compare(a, a) must be false
52 BOOST_CHECK( !cmpAsc( platedItem, platedItem ) );
53 BOOST_CHECK( !cmpAsc( unplatedItem, unplatedItem ) );
54
55 // Strict weak ordering requirement 2: asymmetry - if compare(a, b) then !compare(b, a)
56 bool aLessB = cmpAsc( unplatedItem, platedItem ); // false < true
57 bool bLessA = cmpAsc( platedItem, unplatedItem ); // true < false
58
59 BOOST_CHECK( aLessB != bLessA || (!aLessB && !bLessA) );
60
61 // Test that sorting works without crashing (this would cause undefined behavior with broken comparator)
62 std::vector<DRILL_LINE_ITEM> items;
63 items.push_back( platedItem );
64 items.push_back( unplatedItem );
65 items.push_back( platedItem );
66 items.push_back( unplatedItem );
67
68 BOOST_CHECK_NO_THROW( std::sort( items.begin(), items.end(), cmpAsc ) );
69
70 // Verify the sort order is correct: false (0) values should come before true (1) values
71 BOOST_CHECK( !items[0].isPlated );
72 BOOST_CHECK( !items[1].isPlated );
73 BOOST_CHECK( items[2].isPlated );
74 BOOST_CHECK( items[3].isPlated );
75
76 // Test descending order
78
79 BOOST_CHECK( !cmpDesc( platedItem, platedItem ) );
80 BOOST_CHECK( !cmpDesc( unplatedItem, unplatedItem ) );
81
82 BOOST_CHECK_NO_THROW( std::sort( items.begin(), items.end(), cmpDesc ) );
83
84 // Verify descending order: true (1) values should come before false (0) values
85 BOOST_CHECK( items[0].isPlated );
86 BOOST_CHECK( items[1].isPlated );
87 BOOST_CHECK( !items[2].isPlated );
88 BOOST_CHECK( !items[3].isPlated );
89}
90
91
96BOOST_AUTO_TEST_CASE( DrillCompareStrictWeakOrderingViaPad )
97{
98 // Create test items with different isPad values
99 DRILL_LINE_ITEM padItem( 100, 100, PAD_DRILL_SHAPE::CIRCLE, true, true, F_Cu, B_Cu );
100 DRILL_LINE_ITEM viaItem( 100, 100, PAD_DRILL_SHAPE::CIRCLE, true, false, F_Cu, B_Cu );
101
102 padItem.m_Qty = 1;
103 viaItem.m_Qty = 1;
104
105 // Test ascending comparator
107
108 // Strict weak ordering requirement 1: irreflexivity - compare(a, a) must be false
109 BOOST_CHECK( !cmpAsc( padItem, padItem ) );
110 BOOST_CHECK( !cmpAsc( viaItem, viaItem ) );
111
112 // Strict weak ordering requirement 2: asymmetry - if compare(a, b) then !compare(b, a)
113 bool aLessB = cmpAsc( viaItem, padItem ); // false < true
114 bool bLessA = cmpAsc( padItem, viaItem ); // true < false
115
116 BOOST_CHECK( aLessB != bLessA || (!aLessB && !bLessA) );
117
118 // Test that sorting works without crashing
119 std::vector<DRILL_LINE_ITEM> items;
120 items.push_back( padItem );
121 items.push_back( viaItem );
122 items.push_back( padItem );
123 items.push_back( viaItem );
124
125 BOOST_CHECK_NO_THROW( std::sort( items.begin(), items.end(), cmpAsc ) );
126
127 // Verify the sort order is correct: false (via) values should come before true (pad) values
128 BOOST_CHECK( !items[0].isPad );
129 BOOST_CHECK( !items[1].isPad );
130 BOOST_CHECK( items[2].isPad );
131 BOOST_CHECK( items[3].isPad );
132
133 // Test descending order
135
136 BOOST_CHECK( !cmpDesc( padItem, padItem ) );
137 BOOST_CHECK( !cmpDesc( viaItem, viaItem ) );
138
139 BOOST_CHECK_NO_THROW( std::sort( items.begin(), items.end(), cmpDesc ) );
140
141 // Verify descending order: true (pad) values should come before false (via) values
142 BOOST_CHECK( items[0].isPad );
143 BOOST_CHECK( items[1].isPad );
144 BOOST_CHECK( !items[2].isPad );
145 BOOST_CHECK( !items[3].isPad );
146}
147
148
154BOOST_AUTO_TEST_CASE( FormatReportWithDensity )
155{
158
159 data.hasOutline = true;
160 data.boardWidth = 100000000;
161 data.boardHeight = 50000000;
162 data.boardArea = 5e15;
163 data.frontFootprintDensity = 42.57;
164 data.backFootprintDensity = 18.93;
165
166 UNITS_PROVIDER unitsProvider( pcbIUScale, EDA_UNITS::MM );
167
168 wxString report;
169 BOOST_CHECK_NO_THROW(
170 report = FormatBoardStatisticsReport( data, nullptr, unitsProvider,
171 wxT( "TestProject" ), wxT( "TestBoard" ) ) );
172
173 BOOST_CHECK( report.Contains( wxT( "42.57 %" ) ) );
174 BOOST_CHECK( report.Contains( wxT( "18.93 %" ) ) );
175}
176
177
181BOOST_AUTO_TEST_CASE( FormatJsonWithDensity )
182{
185
186 data.hasOutline = true;
187 data.boardWidth = 100000000;
188 data.boardHeight = 50000000;
189 data.boardArea = 5e15;
190 data.frontFootprintDensity = 42.57;
191 data.backFootprintDensity = 18.93;
192
193 UNITS_PROVIDER unitsProvider( pcbIUScale, EDA_UNITS::MM );
194
195 wxString json;
196 BOOST_CHECK_NO_THROW(
197 json = FormatBoardStatisticsJson( data, nullptr, unitsProvider,
198 wxT( "TestProject" ), wxT( "TestBoard" ) ) );
199
200 BOOST_CHECK( json.Contains( wxT( "42.57" ) ) );
201 BOOST_CHECK( json.Contains( wxT( "18.93" ) ) );
202}
203
204
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:112
wxString FormatBoardStatisticsJson(const BOARD_STATISTICS_DATA &aData, BOARD *aBoard, const UNITS_PROVIDER &aUnitsProvider, const wxString &aProjectName, const wxString &aBoardName)
wxString FormatBoardStatisticsReport(const BOARD_STATISTICS_DATA &aData, BOARD *aBoard, const UNITS_PROVIDER &aUnitsProvider, const wxString &aProjectName, const wxString &aBoardName)
void InitializeBoardStatisticsData(BOARD_STATISTICS_DATA &aData)
nlohmann::json json
Definition gerbview.cpp:50
@ B_Cu
Definition layer_ids.h:65
@ F_Cu
Definition layer_ids.h:64
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_CASE(DrillCompareStrictWeakOrderingPlated)
Test that DRILL_LINE_ITEM::COMPARE satisfies strict weak ordering for COL_PLATED.
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_SUITE_END()