KiCad PCB EDA Suite
test_utf8.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) 2018 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 <utf8.h>
27 
28 #include <algorithm>
29 #include <iostream>
30 
31 #define UTF8_INIT "This is a test of UTF-8: ü‱☺😕😱"
33 {
34 };
35 
36 
40 BOOST_FIXTURE_TEST_SUITE( Utf8, Utf8Fixture )
41 
42 
43 
46 BOOST_AUTO_TEST_CASE( Utf8AndStdString )
47 {
48  std::string str { UTF8_INIT };
49 
50  UTF8 utf8_inited { UTF8_INIT };
51  UTF8 utf8_copied_from_stdstr = str;
52 
53  BOOST_CHECK_EQUAL( utf8_inited, utf8_copied_from_stdstr );
54 
55  UTF8 utf8_copied_from_utf8 = utf8_inited;
56 
57  BOOST_CHECK_EQUAL( utf8_inited, utf8_copied_from_utf8 );
58 }
59 
60 
65 {
66  UTF8 utf8_inited { UTF8_INIT };
67  wxString wx_inited = wxString::FromUTF8( UTF8_INIT );
68 
69  // Check that we can copy convert WxString and compare
70  wxString wx_copied_from_utf8 = utf8_inited;
71  BOOST_CHECK_EQUAL( wx_inited, wx_copied_from_utf8 );
72 
73  // Check we can copy-construct from a WxString
74  UTF8 utf8_copied_from_wxstring = wx_inited;
75  BOOST_CHECK_EQUAL( utf8_inited, utf8_copied_from_wxstring );
76 }
77 
81 BOOST_AUTO_TEST_CASE( UniIterNull )
82 {
83  UTF8::uni_iter it;
84  const UTF8::uni_iter null;
85 
86  // Check nulls are equivalent
87  BOOST_CHECK( it == null );
88 
89  // check null string start == end
90  UTF8 uNull { "" };
91  BOOST_CHECK( uNull.ubegin() == uNull.uend() );
92 }
93 
97 BOOST_AUTO_TEST_CASE( UniIterIncrement )
98 {
99  UTF8 u0 { "inp\ua740t" };
100 
101  UTF8::uni_iter it;
102  const UTF8::uni_iter begin = u0.ubegin();
103  const UTF8::uni_iter end = u0.uend();
104 
105  // Check copy-construction and equality operator
106  it = begin;
107  BOOST_CHECK( it == begin );
108  BOOST_CHECK( it >= begin );
109 
110  // Check de-referencing
111  BOOST_CHECK_EQUAL( *it, 'i' );
112 
113  // postfix increment - normal char and inequality operators
114  it++;
115  BOOST_CHECK( it != begin );
116  BOOST_CHECK( it > begin );
117  BOOST_CHECK( !( begin >= it ) );
118  BOOST_CHECK( it < end );
119  BOOST_CHECK( it <= end );
120  BOOST_CHECK_EQUAL( *it, 'n' );
121 
122  // prefix increment - normal char
123  ++it;
124  BOOST_CHECK_EQUAL( *it, 'p' );
125 
126  // increment to a unicode char
127  ++it;
128  BOOST_CHECK_EQUAL( *it, 0xA740 );
129 
130  // and again to the next char - normal again
131  ++it;
132  BOOST_CHECK_EQUAL( *it, 't' );
133 
134  // and now we should be at the end
135  ++it;
136  BOOST_CHECK( it == end );
137  BOOST_CHECK( it <= end );
138  BOOST_CHECK( !( it < end ) );
139 }
140 
141 
142 BOOST_AUTO_TEST_SUITE_END()
An 8 bit string that is assuredly encoded in UTF8, and supplies special conversion support to and fro...
Definition: utf8.h:70
#define UTF8_INIT
Definition: test_utf8.cpp:31
BOOST_CHECK(v2.Cross(v1)==1)
BOOST_AUTO_TEST_CASE(Utf8AndStdString)
Declares a struct as the Boost test fixture.
Definition: test_utf8.cpp:46
uni_iter is a non-mutating iterator that walks through unicode code points in the UTF8 encoded string...
Definition: utf8.h:203