From f56c4c65a8afc51df57e2e2653c5e35ed29a2423 Mon Sep 17 00:00:00 2001 From: Dirk Stolle Date: Tue, 23 Jul 2024 15:58:12 +0200 Subject: [PATCH] tests: add test cases for wide character path conversions (#754) --- test/core/CMakeLists.txt | 1 + test/core/Jamfile | 1 + test/core/io/CMakeLists.txt | 25 ++++++++++++++++ test/core/io/Jamfile | 13 ++++++++ test/core/io/path_spec.cpp | 59 +++++++++++++++++++++++++++++++++++++ 5 files changed, 99 insertions(+) create mode 100644 test/core/io/CMakeLists.txt create mode 100644 test/core/io/Jamfile create mode 100644 test/core/io/path_spec.cpp diff --git a/test/core/CMakeLists.txt b/test/core/CMakeLists.txt index 2b70482aa0..ee09332906 100644 --- a/test/core/CMakeLists.txt +++ b/test/core/CMakeLists.txt @@ -41,3 +41,4 @@ add_subdirectory(image_view) add_subdirectory(algorithm) add_subdirectory(image_processing) add_subdirectory(histogram) +add_subdirectory(io) diff --git a/test/core/Jamfile b/test/core/Jamfile index 9305b8762c..25156392e4 100644 --- a/test/core/Jamfile +++ b/test/core/Jamfile @@ -33,3 +33,4 @@ build-project image_view ; build-project algorithm ; build-project image_processing ; build-project histogram ; +build-project io ; diff --git a/test/core/io/CMakeLists.txt b/test/core/io/CMakeLists.txt new file mode 100644 index 0000000000..171e354854 --- /dev/null +++ b/test/core/io/CMakeLists.txt @@ -0,0 +1,25 @@ +# +# Copyright (c) 2024 Dirk Stolle +# +# Distributed under the Boost Software License, Version 1.0. +# (See accompanying file LICENSE_1_0.txt or copy at +# http://www.boost.org/LICENSE_1_0.txt) +# +foreach(_name + path_spec) + set(_test t_core_io_${_name}) + set(_target test_core_io_${_name}) + + add_executable(${_target} "") + target_sources(${_target} PRIVATE ${_name}.cpp) + target_link_libraries(${_target} + PRIVATE + gil_compile_options + gil_include_directories + gil_dependencies) + add_test(NAME ${_test} COMMAND ${_target}) + + unset(_name) + unset(_target) + unset(_test) +endforeach() diff --git a/test/core/io/Jamfile b/test/core/io/Jamfile new file mode 100644 index 0000000000..f696d70763 --- /dev/null +++ b/test/core/io/Jamfile @@ -0,0 +1,13 @@ +# Boost.GIL (Generic Image Library) - tests +# +# Copyright (c) 2024 Dirk Stolle +# +# Distributed under the Boost Software License, Version 1.0. +# (See accompanying file LICENSE_1_0.txt or +# copy at http://www.boost.org/LICENSE_1_0.txt) + +import testing ; + +compile path_spec.cpp ; + +run path_spec.cpp ; diff --git a/test/core/io/path_spec.cpp b/test/core/io/path_spec.cpp new file mode 100644 index 0000000000..8179512e6a --- /dev/null +++ b/test/core/io/path_spec.cpp @@ -0,0 +1,59 @@ +// +// Copyright 2024 Dirk Stolle +// +// Distributed under the Boost Software License, Version 1.0 +// See accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt + +#include +#include +#include +#include +#include + +namespace gil = boost::gil; + +void test_convert_to_string_from_wstring() +{ + std::wstring const path = L"/some_path/傳/привет/qwerty"; + std::string const expected = "/some_path/\xE5\x82\xB3/\xD0\xBF\xD1\x80\xD0\xB8\xD0\xB2\xD0\xB5\xD1\x82/qwerty"; + + std::string string = gil::detail::convert_to_string(path); + BOOST_TEST_EQ( 34, string.size() ); + BOOST_TEST_EQ( expected, string ); +} + +void test_convert_to_native_string_from_wchar_t_ptr() +{ + wchar_t const* path = L"/some_path/傳/привет/qwerty"; + char const* expected = "/some_path/\xE5\x82\xB3/\xD0\xBF\xD1\x80\xD0\xB8\xD0\xB2\xD0\xB5\xD1\x82/qwerty"; + + char const* string = gil::detail::convert_to_native_string(path); + BOOST_TEST_EQ( 34, strlen(string) ); + BOOST_TEST_EQ( 0, std::strcmp(expected, string) ); + delete[] string; +} + +void test_convert_to_native_string_from_wstring() +{ + std::wstring const path = L"/some_path/傳/привет/qwerty"; + char const* expected = "/some_path/\xE5\x82\xB3/\xD0\xBF\xD1\x80\xD0\xB8\xD0\xB2\xD0\xB5\xD1\x82/qwerty"; + + char const* string = gil::detail::convert_to_native_string(path); + BOOST_TEST_EQ( 34, strlen(string) ); + BOOST_TEST_EQ( 0, std::strcmp(expected, string) ); + delete[] string; +} + +int main() +{ + // Set global locale to one that uses UTF-8. Could be "en_US.UTF-8" or + // "C.UTF-8" or something similar, as long as it exists on the system. + std::locale::global(std::locale("C.UTF-8")); + + test_convert_to_string_from_wstring(); + test_convert_to_native_string_from_wchar_t_ptr(); + test_convert_to_native_string_from_wstring(); + + return boost::report_errors(); +}