KiCad PCB EDA Suite
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 CHANGELOG.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 
24 #include "uuid_test_utils.h"
25 
27 
28 namespace KI_TEST
29 {
30 
31 bool IsUUID( const std::string& aStr )
32 {
33  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}" );
34  std::smatch match;
35 
36  return std::regex_match( aStr, match, uuid );
37 }
38 
39 
40 bool IsUUIDPathWithLevels( const std::string& aStr, unsigned aLevels )
41 {
42  // A UUID is formated as 8-4-4-4-12
43  const unsigned tsLen = 36;
44  const unsigned levelLen = tsLen + 1; // add the /
45 
46  if( aStr.size() != aLevels * levelLen + 1 )
47  {
48  BOOST_TEST_INFO( "String is the wrong length for " << aLevels << " levels." );
49  return false;
50  }
51 
52  if( aStr[0] != '/' )
53  {
54  BOOST_TEST_INFO( "Doesn't start with '/'" );
55  return false;
56  }
57 
58  auto tsBegin = aStr.begin() + 1;
59 
60  for( unsigned i = 0; i < aLevels; i++ )
61  {
62  if( !IsUUID( std::string( tsBegin, tsBegin + tsLen ) ) )
63  {
64  BOOST_TEST_INFO( "Not a UUID at level "
65  << i << ": " << std::string( tsBegin, tsBegin + tsLen ) );
66  return false;
67  }
68 
69  if( *( tsBegin + tsLen ) != '/' )
70  {
71  BOOST_TEST_INFO( "level doesn't end in '/'" );
72  return false;
73  }
74 
75  tsBegin += levelLen;
76  }
77 
78  return true;
79 }
80 
81 } // 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.
#define BOOST_TEST_INFO(A)
If HAVE_EXPECTED_FAILURES is defined, this means that boost::unit_test::expected_failures is availabl...
Test utilities for timestamps.