1//
2// Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
3//
4// Distributed under the Boost Software License, Version 1.0.
5// https://www.boost.org/LICENSE_1_0.txt
6
7#include <boost/locale/conversion.hpp>
8#include <boost/locale/generator.hpp>
9#include <boost/locale/info.hpp>
10#include <boost/locale/localization_backend.hpp>
11#include "boostLocale/test/tools.hpp"
12#include "boostLocale/test/unit_test.hpp"
13#include <iomanip>
14#include <iostream>
15
16int get_sign(int x)
17{
18 if(x < 0)
19 return -1;
20 else if(x == 0)
21 return 0;
22 return 1;
23}
24
25template<typename CharType>
26void test_one(const std::locale& l, std::string ia, std::string ib, int diff)
27{
28 std::basic_string<CharType> a = to_correct_string<CharType>(ia, l);
29 std::basic_string<CharType> b = to_correct_string<CharType>(ib, l);
30 if(diff < 0) {
31 TEST(l(a, b));
32 TEST(!l(b, a));
33 } else if(diff == 0) {
34 TEST(!l(a, b));
35 TEST(!l(b, a));
36 } else {
37 TEST(!l(a, b));
38 TEST(l(b, a));
39 }
40
41 const std::collate<CharType>& col = std::use_facet<std::collate<CharType>>(l);
42
43 TEST_EQ(diff, col.compare(a.c_str(), a.c_str() + a.size(), b.c_str(), b.c_str() + b.size()));
44 TEST_EQ(
45 diff,
46 get_sign(col.transform(a.c_str(), a.c_str() + a.size()).compare(col.transform(b.c_str(), b.c_str() + b.size()))));
47 if(diff == 0)
48 TEST_EQ(col.hash(a.c_str(), a.c_str() + a.size()), col.hash(b.c_str(), b.c_str() + b.size()));
49}
50
51template<typename CharType>
52void test_char()
53{
54 boost::locale::generator gen;
55
56 std::cout << "- Testing at least C" << std::endl;
57 std::locale l = gen("en_US.UTF-8");
58
59 test_one<CharType>(l, "a", "b", -1);
60 test_one<CharType>(l, "b", "a", 1);
61 test_one<CharType>(l, "a", "a", 0);
62
63#if defined(_LIBCPP_VERSION) && (defined(__APPLE__) || defined(__FreeBSD__))
64 std::cout << "- Collation is broken on this OS's standard C++ library, skipping\n";
65#else
66 for(const std::string name : {"en_US.UTF-8", "sv_SE.UTF-8", "en_US.ISO8859-1"}) {
67 const std::string std_name = get_std_name(name);
68 if(!std_name.empty()) {
69 std::cout << "- Testing " << std_name << std::endl;
70 l = gen(std_name);
71 test_one<CharType>(l, "a", "ç", -1);
72 test_one<CharType>(l, "ç", "d", -1);
73 const auto& info = std::use_facet<boost::locale::info>(loc: l);
74 if(info.utf8()) {
75 // In Swedish locale the collation/ordering of this is different than in English
76 // This makes this a nice test case that the correct collation is used.
77 test_one<CharType>(l, "ängel", "år", info.language() == "sv" ? 1 : -1);
78 }
79 } else
80 std::cout << "- " << name << " not supported, skipping" << std::endl; // LCOV_EXCL_LINE
81 }
82#endif
83}
84
85BOOST_LOCALE_DISABLE_UNREACHABLE_CODE_WARNING
86void test_main(int /*argc*/, char** /*argv*/)
87{
88#ifdef BOOST_LOCALE_NO_STD_BACKEND
89 std::cout << "STD Backend is not build... Skipping\n";
90 return;
91#endif
92 boost::locale::localization_backend_manager mgr = boost::locale::localization_backend_manager::global();
93 mgr.select(backend_name: "std");
94 boost::locale::localization_backend_manager::global(mgr);
95
96 std::cout << "Testing char" << std::endl;
97 test_char<char>();
98 std::cout << "Testing wchar_t" << std::endl;
99 test_char<wchar_t>();
100#ifdef BOOST_LOCALE_ENABLE_CHAR16_T
101 std::cout << "Testing char16_t" << std::endl;
102 test_char<char16_t>();
103#endif
104#ifdef BOOST_LOCALE_ENABLE_CHAR32_T
105 std::cout << "Testing char32_t" << std::endl;
106 test_char<char32_t>();
107#endif
108}
109
110// boostinspect:noascii
111

source code of boost/libs/locale/test/test_std_collate.cpp