KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_increment.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 (C) 2024 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
26#include <increment.h>
27
28
32BOOST_AUTO_TEST_SUITE( StringIncrement )
33
34
36{
37 wxString input;
38 int delta;
39 size_t part;
40 wxString expected;
41};
42
43
48{
49 const std::vector<INCREMENT_TEST_CASE> cases{
50 // Null
51 { "", 1, 0, "nullopt" },
52 { "", 1, 1, "nullopt" },
53 { "", -1, 1, "nullopt" },
54 // Up
55 { "1", 1, 0, "2" },
56 { "1", 9, 0, "10" },
57 // Down
58 { "2", -1, 0, "1" },
59 { "10", -1, 0, "9" },
60 // Down from 0
61 { "0", -1, 0, "nullopt" },
62 // Ran out of a parts
63 { "1", 1, 1, "nullopt" },
64 // Leading zeros preserved
65 { "01", 1, 0, "02" },
66 // Alpha
67 { "A", 1, 0, "B" },
68 { "E", -1, 0, "D" },
69 // Skip I
70 { "H", 1, 0, "J" },
71 { "J", -1, 0, "H" },
72 // But I works if it's there
73 { "I", 1, 0, "J" },
74 { "I", -1, 0, "H" },
75 // Alpha wrap
76 { "Z", 1, 0, "AA" },
77 // Reject huge alphabetic value
78 { "ABB", 1, 0, "nullopt" },
79 // Dashes skipped
80 { "A-1", 1, 0, "A-2" },
81 { "A-1", 1, 1, "B-1" },
82 };
83
84 STRING_INCREMENTER incrementer;
85 incrementer.SetSkipIOSQXZ( true );
86
87 for( const auto& c : cases )
88 {
89 BOOST_TEST_INFO( "Input: " << c.input << " Delta: " << c.delta << " Part: " << c.part );
90 wxString result = incrementer.Increment( c.input, c.delta, c.part ).value_or( "nullopt" );
91 BOOST_CHECK_EQUAL( result, c.expected );
92 }
93}
94
95
97{
98 wxString input;
99 const wxString& alphabet;
101};
102
103
104BOOST_AUTO_TEST_CASE( AlphabeticIndexes )
105{
106 const wxString alphabet = "ABCDEFGHJKLMNPRTUVWY";
107
108 const std::vector<ALPHABETIC_TEST_CASE> cases{ {
109 { "A", alphabet, 0 },
110 { "B", alphabet, 1 },
111 { "Y", alphabet, 19 },
112 { "AA", alphabet, 20 },
113 { "AY", alphabet, 39 },
114 } };
115
116 for( const auto& c : cases )
117 {
118 BOOST_TEST_INFO( "Input: " << c.input << " <-> " << c.expected );
119
120 const int fromString = IndexFromAlphabetic( c.input, c.alphabet );
121 BOOST_CHECK_EQUAL( fromString, c.expected );
122
123 const wxString fromIndex = AlphabeticFromIndex( c.expected, c.alphabet, true );
124 BOOST_CHECK_EQUAL( fromIndex, c.input );
125 }
126}
127
128
Heuristically increment a string's n'th part from the right.
Definition: increment.h:48
void SetSkipIOSQXZ(bool aSkip)
If a alphabetic part is found, skip the letters I, O, S, Q, X, Z.
Definition: increment.h:54
std::optional< wxString > Increment(const wxString &aStr, int aDelta, size_t aRightIndex) const
Increment the n-th part from the right of the given string.
Definition: increment.cpp:85
KICOMMON_API int IndexFromAlphabetic(const wxString &aStr, const wxString &aAlphabet)
Attempt to convert a string to an integer, assuming it is an alphabetic string like "A",...
Definition: increment.cpp:236
wxString KICOMMON_API AlphabeticFromIndex(size_t aN, const wxString &aAlphabet, bool aZeroBasedNonUnitCols)
Get an alphabetic string like A, B, ... Z, AA, AB, ... ZZ, AAA, ...
Definition: increment.cpp:258
const wxString & alphabet
Declares a struct as the Boost test fixture.
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(BasicCase)
Check formatting the point.