kicad-developers team mailing list archive
-
kicad-developers team
-
Mailing list archive
-
Message #37619
[PATCH] Make generic 'common' test and add UTF8 tests
Hi,
These patches make a place for unit tests of stuff in /common under
/qa. This has to move some existing stuff around a little bit:
* /qa/common -> /qa/qa_utils
* /qa/geometry is now part of the new /qa/common
This makes a place to put libcommon tests and keeps /qa as an analogue
of the main tree, just under /qa.
Followed up by the first (non-geom) unit test of common code - UTF8,
which replaces some very old and ad-hoc manual testing code in
common/utf8.cpp with a proper Boost unit test.
This should not change any aspect of production code, as all changes
are to #if 0 code and /qa.
The alternative to this method is to distribute tests into the various
subdirs. In this case, creating /common/qa. This is how eeschema
currently does it. It would be functionally the same to do it that
way, just needs to move files around. `make test` would still work the
same. Let me know if that way is preferred.
Cheers,
John
From 1f9a290604bf0dd8f2ee5d47d58ed34504dc36de Mon Sep 17 00:00:00 2001
From: John Beard <john.j.beard@xxxxxxxxx>
Date: Tue, 25 Sep 2018 12:11:10 +0100
Subject: [PATCH 1/2] QA: Make a generic common test suite
* Renames /qa/common to /qa/qa_utils, as it is not to do with /common
* Renames /qa/geometry to /qa/common
* Put existing geometry tests into a subdirectory of /qa/common, to
echo the layout of the main tree.
The intention is to provide a place to put tests of other libcommon
code in the /qa/common suite.
---
qa/CMakeLists.txt | 2 +-
qa/{geometry => common}/CMakeLists.txt | 10 +++++-----
qa/{ => common}/geometry/geom_test_utils.h | 0
qa/{ => common}/geometry/test_fillet.cpp | 2 +-
qa/{geometry => common}/test_module.cpp | 2 +-
qa/pcb_test_window/CMakeLists.txt | 6 +++---
qa/polygon_generator/CMakeLists.txt | 2 +-
qa/polygon_triangulation/CMakeLists.txt | 2 +-
qa/{common => qa_utils}/mocks.cpp | 0
qa/{common => qa_utils}/pcb_test_frame.cpp | 0
qa/{common => qa_utils}/pcb_test_frame.h | 0
11 files changed, 13 insertions(+), 13 deletions(-)
rename qa/{geometry => common}/CMakeLists.txt (91%)
rename qa/{ => common}/geometry/geom_test_utils.h (100%)
rename qa/{ => common}/geometry/test_fillet.cpp (99%)
rename qa/{geometry => common}/test_module.cpp (95%)
rename qa/{common => qa_utils}/mocks.cpp (100%)
rename qa/{common => qa_utils}/pcb_test_frame.cpp (100%)
rename qa/{common => qa_utils}/pcb_test_frame.h (100%)
diff --git a/qa/CMakeLists.txt b/qa/CMakeLists.txt
index e82afcd73..a932b8754 100644
--- a/qa/CMakeLists.txt
+++ b/qa/CMakeLists.txt
@@ -12,7 +12,7 @@ if( KICAD_SCRIPTING_MODULES )
endif()
-add_subdirectory( geometry )
+add_subdirectory( common )
add_subdirectory( shape_poly_set_refactor )
add_subdirectory( pcb_test_window )
add_subdirectory( polygon_triangulation )
diff --git a/qa/geometry/CMakeLists.txt b/qa/common/CMakeLists.txt
similarity index 91%
rename from qa/geometry/CMakeLists.txt
rename to qa/common/CMakeLists.txt
index 031c02b3a..293532c18 100644
--- a/qa/geometry/CMakeLists.txt
+++ b/qa/common/CMakeLists.txt
@@ -25,9 +25,9 @@ find_package( wxWidgets 3.0.0 COMPONENTS gl aui adv html core net base xml stc R
add_definitions(-DBOOST_TEST_DYN_LINK)
-add_executable( qa_geometry
+add_executable( qa_common
test_module.cpp
- test_fillet.cpp
+ geometry/test_fillet.cpp
)
include_directories(
@@ -37,13 +37,13 @@ include_directories(
${Boost_INCLUDE_DIR}
)
-target_link_libraries( qa_geometry
+target_link_libraries( qa_common
common
polygon
${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}
${wxWidgets_LIBRARIES}
)
-add_test( NAME geometry
- COMMAND qa_geometry
+add_test( NAME common
+ COMMAND qa_common
)
\ No newline at end of file
diff --git a/qa/geometry/geom_test_utils.h b/qa/common/geometry/geom_test_utils.h
similarity index 100%
rename from qa/geometry/geom_test_utils.h
rename to qa/common/geometry/geom_test_utils.h
diff --git a/qa/geometry/test_fillet.cpp b/qa/common/geometry/test_fillet.cpp
similarity index 99%
rename from qa/geometry/test_fillet.cpp
rename to qa/common/geometry/test_fillet.cpp
index c7d758ec6..119db6b19 100644
--- a/qa/geometry/test_fillet.cpp
+++ b/qa/common/geometry/test_fillet.cpp
@@ -29,7 +29,7 @@
#include <algorithm>
-#include <qa/geometry/geom_test_utils.h>
+#include "geom_test_utils.h"
struct FilletFixture
{
diff --git a/qa/geometry/test_module.cpp b/qa/common/test_module.cpp
similarity index 95%
rename from qa/geometry/test_module.cpp
rename to qa/common/test_module.cpp
index 6fb45de1f..38f18fd03 100644
--- a/qa/geometry/test_module.cpp
+++ b/qa/common/test_module.cpp
@@ -26,7 +26,7 @@
*/
#define BOOST_TEST_MAIN
-#define BOOST_TEST_MODULE "Geometry module tests"
+#define BOOST_TEST_MODULE "Common library module tests"
#include <boost/test/unit_test.hpp>
diff --git a/qa/pcb_test_window/CMakeLists.txt b/qa/pcb_test_window/CMakeLists.txt
index 3bcc664dc..af9f01f81 100644
--- a/qa/pcb_test_window/CMakeLists.txt
+++ b/qa/pcb_test_window/CMakeLists.txt
@@ -35,8 +35,8 @@ add_dependencies( pnsrouter pcbcommon pcad2kicadpcb ${GITHUB_PLUGIN_LIBRARIES} )
add_executable(test_window WIN32
test.cpp
- ../common/pcb_test_frame.cpp
- ../common/mocks.cpp
+ ../qa_utils/pcb_test_frame.cpp
+ ../qa_utils/mocks.cpp
../../common/base_units.cpp
../../pcbnew/tools/pcb_tool.cpp
../../pcbnew/tools/selection.cpp
@@ -57,7 +57,7 @@ include_directories(
${CMAKE_SOURCE_DIR}/pcbnew/dialogs
${CMAKE_SOURCE_DIR}/polygon
${CMAKE_SOURCE_DIR}/common/geometry
- ${CMAKE_SOURCE_DIR}/qa/common
+ ${CMAKE_SOURCE_DIR}/qa/qa_utils
${Boost_INCLUDE_DIR}
${INC_AFTER}
)
diff --git a/qa/polygon_generator/CMakeLists.txt b/qa/polygon_generator/CMakeLists.txt
index 6c600f522..ba79297fb 100644
--- a/qa/polygon_generator/CMakeLists.txt
+++ b/qa/polygon_generator/CMakeLists.txt
@@ -33,7 +33,7 @@ endif()
add_dependencies( pnsrouter pcbcommon pcad2kicadpcb ${GITHUB_PLUGIN_LIBRARIES} )
add_executable(test_polygon_generator
- ../common/mocks.cpp
+ ../qa_utils/mocks.cpp
../../common/base_units.cpp
test_polygon_generator.cpp
)
diff --git a/qa/polygon_triangulation/CMakeLists.txt b/qa/polygon_triangulation/CMakeLists.txt
index c611e73d9..228120691 100644
--- a/qa/polygon_triangulation/CMakeLists.txt
+++ b/qa/polygon_triangulation/CMakeLists.txt
@@ -33,7 +33,7 @@ endif()
add_dependencies( pnsrouter pcbcommon pcad2kicadpcb ${GITHUB_PLUGIN_LIBRARIES} )
add_executable(test_polygon_triangulation
- ../common/mocks.cpp
+ ../qa_utils/mocks.cpp
../../common/base_units.cpp
test_polygon_triangulation.cpp
)
diff --git a/qa/common/mocks.cpp b/qa/qa_utils/mocks.cpp
similarity index 100%
rename from qa/common/mocks.cpp
rename to qa/qa_utils/mocks.cpp
diff --git a/qa/common/pcb_test_frame.cpp b/qa/qa_utils/pcb_test_frame.cpp
similarity index 100%
rename from qa/common/pcb_test_frame.cpp
rename to qa/qa_utils/pcb_test_frame.cpp
diff --git a/qa/common/pcb_test_frame.h b/qa/qa_utils/pcb_test_frame.h
similarity index 100%
rename from qa/common/pcb_test_frame.h
rename to qa/qa_utils/pcb_test_frame.h
--
2.18.0
From 009c845a97255c4b70230058fc12e0c3e68131c6 Mon Sep 17 00:00:00 2001
From: John Beard <john.j.beard@xxxxxxxxx>
Date: Tue, 25 Sep 2018 13:41:20 +0100
Subject: [PATCH 2/2] 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`).
---
common/utf8.cpp | 73 +-------------------
qa/common/CMakeLists.txt | 3 +
qa/common/test_utf8.cpp | 142 +++++++++++++++++++++++++++++++++++++++
3 files changed, 146 insertions(+), 72 deletions(-)
create mode 100644 qa/common/test_utf8.cpp
diff --git a/common/utf8.cpp b/common/utf8.cpp
index 57677ca7c..0386d8ca1 100644
--- a/common/utf8.cpp
+++ b/common/utf8.cpp
@@ -232,75 +232,4 @@ UTF8& UTF8::operator+=( unsigned w_ch )
}
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
+}
\ No newline at end of file
diff --git a/qa/common/CMakeLists.txt b/qa/common/CMakeLists.txt
index 293532c18..b5aae6c3c 100644
--- a/qa/common/CMakeLists.txt
+++ b/qa/common/CMakeLists.txt
@@ -27,6 +27,9 @@ add_definitions(-DBOOST_TEST_DYN_LINK)
add_executable( qa_common
test_module.cpp
+
+ test_utf8.cpp
+
geometry/test_fillet.cpp
)
diff --git a/qa/common/test_utf8.cpp b/qa/common/test_utf8.cpp
new file mode 100644
index 000000000..c52ca1ff7
--- /dev/null
+++ b/qa/common/test_utf8.cpp
@@ -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()
--
2.18.0
Follow ups