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 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
22#include <increment.h>
23
24#include <limits>
25
26
30BOOST_AUTO_TEST_SUITE( StringIncrement )
31
32
34{
35 wxString input;
36 int delta;
37 size_t part;
38 wxString expected;
39};
40
41
46{
47 const std::vector<INCREMENT_TEST_CASE> cases{
48 // Null
49 { "", 1, 0, "nullopt" },
50 { "", 1, 1, "nullopt" },
51 { "", -1, 1, "nullopt" },
52 // Up
53 { "1", 1, 0, "2" },
54 { "1", 9, 0, "10" },
55 // Down
56 { "2", -1, 0, "1" },
57 { "10", -1, 0, "9" },
58 // Down from 0
59 { "0", -1, 0, "nullopt" },
60 // Ran out of a parts
61 { "1", 1, 1, "nullopt" },
62 // Leading zeros preserved
63 { "01", 1, 0, "02" },
64 // Alpha
65 { "A", 1, 0, "B" },
66 { "E", -1, 0, "D" },
67 // Skip I
68 { "H", 1, 0, "J" },
69 { "J", -1, 0, "H" },
70 // But I works if it's there
71 { "I", 1, 0, "J" },
72 { "I", -1, 0, "H" },
73 // Alpha wrap
74 { "Z", 1, 0, "AA" },
75 // Reject huge alphabetic value
76 { "ABB", 1, 0, "nullopt" },
77 // Dashes skipped
78 { "A-1", 1, 0, "A-2" },
79 { "A-1", 1, 1, "B-1" },
80 // Mixed alphabetic+numeric string behavior
81 { "A12", 1, 0, "A13" },
82 { "A12", -1, 0, "A11" },
83 { "A12", 1, 1, "B12" },
84 { "A12", -1, 1, "nullopt" },
85 // Zero padding is re-applied on the way down and dropped when the number gets wider
86 { "010", -1, 0, "009" },
87 { "01", 1000, 0, "1001" },
88 // No part is that far right
89 { "1", 1, std::numeric_limits<size_t>::max(), "nullopt" },
90 };
91
93 incrementer.SetSkipIOSQXZ( true );
94
95 for( const auto& c : cases )
96 {
97 BOOST_TEST_INFO( "Input: " << c.input << " Delta: " << c.delta << " Part: " << c.part );
98 wxString result = incrementer.Increment( c.input, c.delta, c.part ).value_or( "nullopt" );
99 BOOST_CHECK_EQUAL( result, c.expected );
100 }
101}
102
103
104BOOST_AUTO_TEST_CASE( NonAsciiParts, *boost::unit_test::timeout( 30 ) )
105{
106 // Non-ASCII characters are skippable, not incrementable, and must never stall the scan
107 // (#25299). U+3042 is hiragana A, U+FF11 a full-width one
108 const std::vector<INCREMENT_TEST_CASE> cases{
109 { wxString( L"1あ" ), 1, 0, wxString( L"2あ" ) },
110 { wxString( L"あ1" ), 1, 0, wxString( L"あ2" ) },
111 { wxString( L"あ1" ), 1, 1, "nullopt" },
112 { wxString( L"1" ), 1, 0, "nullopt" },
113 { wxString( L"A-1" ), 1, 0, wxString( L"B-1" ) },
114 };
115
117 incrementer.SetSkipIOSQXZ( true );
118
119 for( const auto& c : cases )
120 {
121 BOOST_TEST_INFO( "Input: " << c.input << " Delta: " << c.delta << " Part: " << c.part );
122 wxString result = incrementer.Increment( c.input, c.delta, c.part ).value_or( "nullopt" );
123 BOOST_CHECK_EQUAL( result, c.expected );
124 }
125}
126
127
129{
130 wxString input;
131 const wxString& alphabet;
133};
134
135
136BOOST_AUTO_TEST_CASE( AlphabeticIndexes )
137{
138 const wxString alphabet = "ABCDEFGHJKLMNPRTUVWY";
139
140 const std::vector<ALPHABETIC_TEST_CASE> cases{ {
141 { "A", alphabet, 0 },
142 { "B", alphabet, 1 },
143 { "Y", alphabet, 19 },
144 { "AA", alphabet, 20 },
145 { "AY", alphabet, 39 },
146 } };
147
148 for( const auto& c : cases )
149 {
150 BOOST_TEST_INFO( "Input: " << c.input << " <-> " << c.expected );
151
152 const int fromString = IndexFromAlphabetic( c.input, c.alphabet );
153 BOOST_CHECK_EQUAL( fromString, c.expected );
154
155 const wxString fromIndex = AlphabeticFromIndex( c.expected, c.alphabet, true );
156 BOOST_CHECK_EQUAL( fromIndex, c.input );
157 }
158}
159
160
Heuristically increment a string's n'th part from the right.
Definition increment.h:44
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",...
wxString KICOMMON_API AlphabeticFromIndex(size_t aN, const wxString &aAlphabet, bool aZeroBasedNonUnitCols)
Get an alphabetic string like A, B, ... Z, AA, AB, ... ZZ, AAA, ...
const wxString & alphabet
Declares a struct as the Boost test fixture.
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_SUITE_END()
STRING_INCREMENTER incrementer
BOOST_AUTO_TEST_CASE(BasicCase)
Check formatting the point.
BOOST_TEST_INFO("Two-port Series .op current = "<< iDevice)
wxString result
Test unit parsing edge cases and error handling.
BOOST_CHECK_EQUAL(result, "25.4")