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, see <https://www.gnu.org/licenses/>.
18 */
19
21#include <board_statistics.h>
23#include <base_units.h>
24#include <algorithm>
25#include <vector>
26
27
28BOOST_AUTO_TEST_SUITE( BoardStatistics )
29
30
31
35BOOST_AUTO_TEST_CASE( DrillCompareStrictWeakOrderingPlated )
36{
37 // Create test items with different plated values
38 DRILL_LINE_ITEM platedItem( 100, 100, PAD_DRILL_SHAPE::CIRCLE, true, true, F_Cu, B_Cu );
39 DRILL_LINE_ITEM unplatedItem( 100, 100, PAD_DRILL_SHAPE::CIRCLE, false, true, F_Cu, B_Cu );
40
41 platedItem.m_Qty = 1;
42 unplatedItem.m_Qty = 1;
43
44 // Test ascending comparator
46
47 // Strict weak ordering requirement 1: irreflexivity - compare(a, a) must be false
48 BOOST_CHECK( !cmpAsc( platedItem, platedItem ) );
49 BOOST_CHECK( !cmpAsc( unplatedItem, unplatedItem ) );
50
51 // Strict weak ordering requirement 2: asymmetry - if compare(a, b) then !compare(b, a)
52 bool aLessB = cmpAsc( unplatedItem, platedItem ); // false < true
53 bool bLessA = cmpAsc( platedItem, unplatedItem ); // true < false
54
55 BOOST_CHECK( aLessB != bLessA || (!aLessB && !bLessA) );
56
57 // Test that sorting works without crashing (this would cause undefined behavior with broken comparator)
58 std::vector<DRILL_LINE_ITEM> items;
59 items.push_back( platedItem );
60 items.push_back( unplatedItem );
61 items.push_back( platedItem );
62 items.push_back( unplatedItem );
63
64 BOOST_CHECK_NO_THROW( std::sort( items.begin(), items.end(), cmpAsc ) );
65
66 // Verify the sort order is correct: false (0) values should come before true (1) values
67 BOOST_CHECK( !items[0].isPlated );
68 BOOST_CHECK( !items[1].isPlated );
69 BOOST_CHECK( items[2].isPlated );
70 BOOST_CHECK( items[3].isPlated );
71
72 // Test descending order
74
75 BOOST_CHECK( !cmpDesc( platedItem, platedItem ) );
76 BOOST_CHECK( !cmpDesc( unplatedItem, unplatedItem ) );
77
78 BOOST_CHECK_NO_THROW( std::sort( items.begin(), items.end(), cmpDesc ) );
79
80 // Verify descending order: true (1) values should come before false (0) values
81 BOOST_CHECK( items[0].isPlated );
82 BOOST_CHECK( items[1].isPlated );
83 BOOST_CHECK( !items[2].isPlated );
84 BOOST_CHECK( !items[3].isPlated );
85}
86
87
92BOOST_AUTO_TEST_CASE( DrillCompareStrictWeakOrderingViaPad )
93{
94 // Create test items with different isPad values
95 DRILL_LINE_ITEM padItem( 100, 100, PAD_DRILL_SHAPE::CIRCLE, true, true, F_Cu, B_Cu );
96 DRILL_LINE_ITEM viaItem( 100, 100, PAD_DRILL_SHAPE::CIRCLE, true, false, F_Cu, B_Cu );
97
98 padItem.m_Qty = 1;
99 viaItem.m_Qty = 1;
100
101 // Test ascending comparator
103
104 // Strict weak ordering requirement 1: irreflexivity - compare(a, a) must be false
105 BOOST_CHECK( !cmpAsc( padItem, padItem ) );
106 BOOST_CHECK( !cmpAsc( viaItem, viaItem ) );
107
108 // Strict weak ordering requirement 2: asymmetry - if compare(a, b) then !compare(b, a)
109 bool aLessB = cmpAsc( viaItem, padItem ); // false < true
110 bool bLessA = cmpAsc( padItem, viaItem ); // true < false
111
112 BOOST_CHECK( aLessB != bLessA || (!aLessB && !bLessA) );
113
114 // Test that sorting works without crashing
115 std::vector<DRILL_LINE_ITEM> items;
116 items.push_back( padItem );
117 items.push_back( viaItem );
118 items.push_back( padItem );
119 items.push_back( viaItem );
120
121 BOOST_CHECK_NO_THROW( std::sort( items.begin(), items.end(), cmpAsc ) );
122
123 // Verify the sort order is correct: false (via) values should come before true (pad) values
124 BOOST_CHECK( !items[0].isPad );
125 BOOST_CHECK( !items[1].isPad );
126 BOOST_CHECK( items[2].isPad );
127 BOOST_CHECK( items[3].isPad );
128
129 // Test descending order
131
132 BOOST_CHECK( !cmpDesc( padItem, padItem ) );
133 BOOST_CHECK( !cmpDesc( viaItem, viaItem ) );
134
135 BOOST_CHECK_NO_THROW( std::sort( items.begin(), items.end(), cmpDesc ) );
136
137 // Verify descending order: true (pad) values should come before false (via) values
138 BOOST_CHECK( items[0].isPad );
139 BOOST_CHECK( items[1].isPad );
140 BOOST_CHECK( !items[2].isPad );
141 BOOST_CHECK( !items[3].isPad );
142}
143
144
150BOOST_AUTO_TEST_CASE( FormatReportWithDensity )
151{
154
155 data.hasOutline = true;
156 data.boardWidth = 100000000;
157 data.boardHeight = 50000000;
158 data.boardArea = 5e15;
159 data.frontFootprintDensity = 42.57;
160 data.backFootprintDensity = 18.93;
161
162 UNITS_PROVIDER unitsProvider( pcbIUScale, EDA_UNITS::MM );
163
164 wxString report;
165 BOOST_CHECK_NO_THROW(
166 report = FormatBoardStatisticsReport( data, nullptr, unitsProvider,
167 wxT( "TestProject" ), wxT( "TestBoard" ) ) );
168
169 BOOST_CHECK( report.Contains( wxT( "42.57 %" ) ) );
170 BOOST_CHECK( report.Contains( wxT( "18.93 %" ) ) );
171}
172
173
177BOOST_AUTO_TEST_CASE( FormatJsonWithDensity )
178{
181
182 data.hasOutline = true;
183 data.boardWidth = 100000000;
184 data.boardHeight = 50000000;
185 data.boardArea = 5e15;
186 data.frontFootprintDensity = 42.57;
187 data.backFootprintDensity = 18.93;
188
189 UNITS_PROVIDER unitsProvider( pcbIUScale, EDA_UNITS::MM );
190
191 wxString json;
192 BOOST_CHECK_NO_THROW(
193 json = FormatBoardStatisticsJson( data, nullptr, unitsProvider,
194 wxT( "TestProject" ), wxT( "TestBoard" ) ) );
195
196 BOOST_CHECK( json.Contains( wxT( "42.57" ) ) );
197 BOOST_CHECK( json.Contains( wxT( "18.93" ) ) );
198}
199
200
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:121
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:49
@ B_Cu
Definition layer_ids.h:61
@ F_Cu
Definition layer_ids.h:60
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()