KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_constraint_system_2d.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 3
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#include <boost/test/unit_test.hpp>
21
23#include <GCS.h>
24
25#include <algorithm>
26#include <chrono>
27#include <vector>
28
29
30BOOST_AUTO_TEST_SUITE( ConstraintSystem2D )
31
32
33BOOST_AUTO_TEST_CASE( CoordinateFrameRoundTripsFarFromOrigin )
34{
36 system.SetCoordinateFrame( VECTOR2I( 500000000, 700000000 ), 1000000.0 );
37
38 BOOST_CHECK_CLOSE( system.NormalizeX( 512345678 ), 12.345678, 1e-9 );
39 BOOST_CHECK_CLOSE( system.NormalizeY( 698765433 ), -1.234567, 1e-9 );
40 BOOST_CHECK_CLOSE( system.DenormalizeX( 12.345678 ), 512345678.0, 1e-9 );
41 BOOST_CHECK_CLOSE( system.DenormalizeY( -1.234567 ), 698765433.0, 1e-9 );
42}
43
44
45BOOST_AUTO_TEST_CASE( ParameterAddressesRemainStable )
46{
48 int first = system.AddParameter( 42.0 );
49 double* address = system.ParameterPointer( first );
50
51 for( int i = 0; i < 16'384; ++i )
52 system.AddParameter( i );
53
54 BOOST_CHECK_EQUAL( system.ParameterPointer( first ), address );
55 BOOST_CHECK_EQUAL( *address, 42.0 );
56}
57
58
59BOOST_AUTO_TEST_CASE( SnapshotRestoresEveryParameter )
60{
62 int x = system.AddParameter( 1.25 );
63 int y = system.AddParameter( -4.5 );
64 auto snapshot = system.Snapshot();
65
66 system.Parameter( x ) = 99.0;
67 system.Parameter( y ) = 101.0;
68 BOOST_REQUIRE( system.Restore( snapshot ) );
69
70 BOOST_CHECK_EQUAL( system.Parameter( x ), 1.25 );
71 BOOST_CHECK_EQUAL( system.Parameter( y ), -4.5 );
72}
73
74
75BOOST_AUTO_TEST_CASE( PrefixRestorePreservesLaterStableStorage )
76{
78 int first = system.AddParameter( 1.0 );
79 CONSTRAINT_SYSTEM_2D::SNAPSHOT snapshot = system.Snapshot();
80 int temporary = system.AddParameter( 2.0 );
81
82 system.Parameter( first ) = 3.0;
83 system.Parameter( temporary ) = 4.0;
84
85 BOOST_CHECK( !system.Restore( snapshot ) );
86 BOOST_REQUIRE( system.RestorePrefix( snapshot ) );
87 BOOST_CHECK_EQUAL( system.Parameter( first ), 1.0 );
88 BOOST_CHECK_EQUAL( system.Parameter( temporary ), 4.0 );
89}
90
91
92BOOST_AUTO_TEST_CASE( BoundedParameterHandlingP95StaysWithinSixteenMilliseconds )
93{
94 std::vector<std::chrono::microseconds> samples;
95
96 for( int run = 0; run < 20; ++run )
97 {
98 auto start = std::chrono::steady_clock::now();
100 GCS::VEC_pD unknowns;
101
102 for( int i = 0; i < 128; ++i )
103 {
104 int parameter = system.AddParameter( i / 2 );
105 unknowns.push_back( system.ParameterPointer( parameter ) );
106 }
107
108 for( int relation = 0; relation < 64; ++relation )
109 system.Solver().addConstraintEqual( unknowns[relation * 2],
110 unknowns[relation * 2 + 1], relation + 1 );
111
112 system.Solver().declareUnknowns( unknowns );
113 system.Solver().initSolution();
114 int solveResult = system.Solver().solve();
115 BOOST_CHECK( solveResult == GCS::Success || solveResult == GCS::Converged );
116 system.Solver().applySolution();
117 samples.push_back( std::chrono::duration_cast<std::chrono::microseconds>(
118 std::chrono::steady_clock::now() - start ) );
119 }
120
121 std::sort( samples.begin(), samples.end() );
122 std::chrono::microseconds p95 = samples[18];
123 BOOST_TEST_MESSAGE( "128-parameter/64-relation solve p95: " << p95.count() << " us" );
124 BOOST_CHECK_LE( p95.count(), 16000 );
125}
126
127
128BOOST_AUTO_TEST_CASE( LargeParameterHandlingRecordsTelemetry )
129{
130 auto start = std::chrono::steady_clock::now();
132 GCS::VEC_pD unknowns;
133
134 for( int i = 0; i < 1024; ++i )
135 {
136 int parameter = system.AddParameter( i / 2 );
137 unknowns.push_back( system.ParameterPointer( parameter ) );
138 }
139
140 for( int relation = 0; relation < 512; ++relation )
141 system.Solver().addConstraintEqual( unknowns[relation * 2],
142 unknowns[relation * 2 + 1], relation + 1 );
143
144 system.Solver().declareUnknowns( unknowns );
145 system.Solver().initSolution();
146 int solveResult = system.Solver().solve();
147 BOOST_CHECK( solveResult == GCS::Success || solveResult == GCS::Converged );
148 system.Solver().applySolution();
149 auto elapsed = std::chrono::duration_cast<std::chrono::microseconds>(
150 std::chrono::steady_clock::now() - start );
151 BOOST_TEST_MESSAGE( "1024-parameter/512-relation solve: " << elapsed.count() << " us" );
152}
153
154
bool RestorePrefix(const SNAPSHOT &aSnapshot)
double DenormalizeY(double aNormalized) const
double NormalizeY(int aIU) const
bool Restore(const SNAPSHOT &aSnapshot)
void SetCoordinateFrame(const VECTOR2I &aOrigin, double aScale)
double NormalizeX(int aIU) const
std::vector< double > SNAPSHOT
double & Parameter(int aIndex)
int AddParameter(double aValue)
double DenormalizeX(double aNormalized) const
double * ParameterPointer(int aIndex)
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_CASE(CoordinateFrameRoundTripsFarFromOrigin)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
BOOST_TEST_MESSAGE("Polyline has "<< chain.PointCount()<< " points")
BOOST_CHECK_EQUAL(result, "25.4")
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683