KiCad PCB EDA Suite
Loading...
Searching...
No Matches
uuid_test_utils.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) 2019 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
26
27namespace KI_TEST
28{
29
30bool IsUUID( const std::string& aStr )
31{
32 std::regex uuid( "[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}" );
33 std::smatch match;
34
35 return std::regex_match( aStr, match, uuid );
36}
37
38
39bool IsUUIDPathWithLevels( const std::string& aStr, unsigned aLevels )
40{
41 // A UUID is formatted as 8-4-4-4-12
42 const unsigned tsLen = 36;
43 const unsigned levelLen = tsLen + 1; // add the /
44
45 if( aStr.size() != aLevels * levelLen + 1 )
46 {
47 BOOST_TEST_INFO( "String is the wrong length for " << aLevels << " levels." );
48 return false;
49 }
50
51 if( aStr[0] != '/' )
52 {
53 BOOST_TEST_INFO( "Doesn't start with '/'" );
54 return false;
55 }
56
57 auto tsBegin = aStr.begin() + 1;
58
59 for( unsigned i = 0; i < aLevels; i++ )
60 {
61 if( !IsUUID( std::string( tsBegin, tsBegin + tsLen ) ) )
62 {
63 BOOST_TEST_INFO( "Not a UUID at level "
64 << i << ": " << std::string( tsBegin, tsBegin + tsLen ) );
65 return false;
66 }
67
68 if( *( tsBegin + tsLen ) != '/' )
69 {
70 BOOST_TEST_INFO( "level doesn't end in '/'" );
71 return false;
72 }
73
74 tsBegin += levelLen;
75 }
76
77 return true;
78}
79
80} // namespace KI_TEST
bool IsUUIDPathWithLevels(const std::string &aStr, unsigned aLevels)
Predicate to check a string is a UUID path format.
bool IsUUID(const std::string &aStr)
Check if the string between the iterators looks like a UUID.
Test utilities for timestamps.