QA: Place UTF8 tests under Boost unit tests

This transfers the old #if'd-out tests in utf8.cpp into a
proper Boost unit test in /qa (run on `make test`).
This commit is contained in:
John Beard 2018-09-25 13:41:20 +01:00 committed by Wayne Stambaugh
parent 9c78ef5784
commit 04a1084549
3 changed files with 146 additions and 72 deletions

View File

@ -233,74 +233,3 @@ UTF8& UTF8::operator+=( unsigned w_ch )
return (UTF8&) *this; return (UTF8&) *this;
} }
#if 0 // some unit tests:
#include <stdio.h>
wxString wxFunctionTaking_wxString( const wxString& wx )
{
printf( "%s:'%s'\n", __func__, (char*) UTF8( wx ) );
printf( "%s:'%s'\n", __func__, (const char*) UTF8( wx ) );
printf( "%s:'%s'\n", __func__, UTF8( wx ).c_str() );
return wx;
}
int main()
{
std::string str = "input";
UTF8 u0 = L"wide string";
UTF8 u1 = "initial";
wxString wx = wxT( "input2" );
printf( "u0:'%s'\n", u0.c_str() );
printf( "u1:'%s'\n", u1.c_str() );
u1 = str;
wxString wx2 = u1;
// force a std::string into a UTF8, then into a wxString, then copy construct:
wxString wx3 = (UTF8&) u1;
UTF8 u2 = wx2;
u2 += 'X';
printf( "u2:'%s'\n", u2.c_str() );
// key accomplishments here:
// 1) passing a UTF8 to a function which normally takes a wxString.
// 2) return a wxString back into a UTF8.
UTF8 result = wxFunctionTaking_wxString( u2 );
printf( "result:'%s'\n", result.c_str() );
// test the unicode iterator:
for( UTF8::uni_iter it = u2.ubegin(); it < u2.uend(); )
{
// test post-increment:
printf( " _%02x_", *it++ );
}
printf( "\n" );
UTF8::uni_iter it = u2.ubegin();
UTF8::uni_iter it2 = it++;
printf( "post_inc:'%c' should be 'i'\n", *it2 );
it2 = ++it;
printf( "pre_inc:'%c' should be 'p'\n", *it2 );
printf( "u[1]:'%c' should be 'n'\n", u2[1] );
return 0;
}
#endif

View File

@ -27,6 +27,9 @@ add_definitions(-DBOOST_TEST_DYN_LINK)
add_executable( qa_common add_executable( qa_common
test_module.cpp test_module.cpp
test_utf8.cpp
geometry/test_fillet.cpp geometry/test_fillet.cpp
) )

142
qa/common/test_utf8.cpp Normal file
View File

@ -0,0 +1,142 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2018 KiCad Developers, see CHANGELOG.TXT for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <boost/test/unit_test.hpp>
#include <boost/test/test_case_template.hpp>
#include <utf8.h>
#include <algorithm>
struct Utf8Fixture
{
};
/**
* Declares a struct as the Boost test fixture.
*/
BOOST_FIXTURE_TEST_SUITE( Utf8, Utf8Fixture )
/**
* Check direct and copy construction from std::string
*/
BOOST_AUTO_TEST_CASE( Utf8AndStdString )
{
std::string str { "input" };
UTF8 utf8_inited { "input" };
UTF8 utf8_copied_from_stdstr = str;
BOOST_CHECK_EQUAL( utf8_inited, utf8_copied_from_stdstr );
UTF8 utf8_copied_from_utf8 = utf8_inited;
BOOST_CHECK_EQUAL( utf8_inited, utf8_copied_from_utf8 );
}
/**
* Check direct and copy construction from wxString
*/
BOOST_AUTO_TEST_CASE( Utf8AndWx )
{
UTF8 utf8_inited { "input" };
wxString wx_inited { "input" };
// Check that we can copy convert WxString and compare
wxString wx_copied_from_utf8 = utf8_inited;
BOOST_CHECK_EQUAL( wx_inited, wx_copied_from_utf8 );
// Check we can copy-construct from a WxString
UTF8 utf8_copied_from_wxstring = wx_inited;
BOOST_CHECK_EQUAL( utf8_inited, utf8_copied_from_wxstring );
}
/**
* UTF8::uni_iter null tests
*/
BOOST_AUTO_TEST_CASE( UniIterNull )
{
UTF8::uni_iter it;
const UTF8::uni_iter null;
// Check nulls are equivalent
BOOST_CHECK( it == null );
// check null string start == end
UTF8 uNull { "" };
BOOST_CHECK( uNull.ubegin() == uNull.uend() );
}
/**
* UTF8::uni_iter increment tests
*/
BOOST_AUTO_TEST_CASE( UniIterIncrement )
{
UTF8 u0 { "inp\ua740t" };
UTF8::uni_iter it;
const UTF8::uni_iter begin = u0.ubegin();
const UTF8::uni_iter end = u0.uend();
// Check copy-construction and equality operator
it = begin;
BOOST_CHECK( it == begin );
BOOST_CHECK( it >= begin );
// Check de-referencing
BOOST_CHECK_EQUAL( *it, 'i' );
// postfix increment - normal char and inequality operators
it++;
BOOST_CHECK( it != begin );
BOOST_CHECK( it > begin );
BOOST_CHECK( !( begin >= it ) );
BOOST_CHECK( it < end );
BOOST_CHECK( it <= end );
BOOST_CHECK_EQUAL( *it, 'n' );
// prefix increment - normal char
++it;
BOOST_CHECK_EQUAL( *it, 'p' );
// increment to a unicode char
++it;
BOOST_CHECK_EQUAL( *it, 0xA740 );
// and again to the next char - normal again
++it;
BOOST_CHECK_EQUAL( *it, 't' );
// and now we should be at the end
++it;
BOOST_CHECK( it == end );
BOOST_CHECK( it <= end );
BOOST_CHECK( !( it < end ) );
}
BOOST_AUTO_TEST_SUITE_END()