Skip to content

Commit

Permalink
tests: add test cases for wide character path conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
striezel committed Jul 23, 2024
1 parent 32eabbc commit f819ac1
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ add_subdirectory(image_view)
add_subdirectory(algorithm)
add_subdirectory(image_processing)
add_subdirectory(histogram)
add_subdirectory(io)
1 change: 1 addition & 0 deletions test/core/Jamfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ build-project image_view ;
build-project algorithm ;
build-project image_processing ;
build-project histogram ;
build-project io ;
25 changes: 25 additions & 0 deletions test/core/io/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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()
13 changes: 13 additions & 0 deletions test/core/io/Jamfile
Original file line number Diff line number Diff line change
@@ -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 ;
59 changes: 59 additions & 0 deletions test/core/io/path_spec.cpp
Original file line number Diff line number Diff line change
@@ -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 <boost/core/lightweight_test.hpp>
#include <boost/gil/io/path_spec.hpp>
#include <cstring>
#include <locale>
#include <string>

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();
}

0 comments on commit f819ac1

Please sign in to comment.