File: test_posix_collate.cpp

package info (click to toggle)
boost1.74 1.74.0-9
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 464,084 kB
  • sloc: cpp: 3,338,324; xml: 131,293; python: 33,088; ansic: 14,336; asm: 4,034; sh: 3,351; makefile: 1,193; perl: 1,036; yacc: 478; php: 212; ruby: 102; lisp: 24; sql: 13; csh: 6
file content (120 lines) | stat: -rw-r--r-- 3,300 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//
//  Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
//
//  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)
//
#ifdef BOOST_LOCALE_NO_POSIX_BACKEND
#include <iostream>
int main()
{
        std::cout << "POSIX Backend is not build... Skipping" << std::endl;
}
#else
#include <boost/locale/config.hpp>
#include <boost/locale/conversion.hpp>
#include <boost/locale/localization_backend.hpp>
#include <boost/locale/generator.hpp>
#include <boost/locale/info.hpp>
#include <iomanip>
#include "test_locale.hpp"
#include "test_locale_tools.hpp"
#include "test_posix_tools.hpp"
#include <iostream>

int get_sign(int x)
{
    if(x<0)
        return -1;
    else if(x==0)
        return 0;
    return 1;
}

template<typename CharType>
void test_one(std::locale const &l,std::string ia,std::string ib,int diff)
{
    std::basic_string<CharType> a=to_correct_string<CharType>(ia,l);
    std::basic_string<CharType> b=to_correct_string<CharType>(ib,l);
    if(diff < 0) {
        TEST(l(a,b));
        TEST(!l(b,a));
    }
    else if(diff == 0) {
        TEST(!l(a,b));
        TEST(!l(b,a));
    }
    else {
        TEST(!l(a,b));
        TEST(l(b,a));
    }
    
    std::collate<CharType> const &col = std::use_facet<std::collate<CharType> >(l);

    TEST(diff == col.compare(a.c_str(),a.c_str()+a.size(),b.c_str(),b.c_str()+b.size()));
    TEST(diff == get_sign(col.transform(a.c_str(),a.c_str()+a.size()).compare(col.transform(b.c_str(),b.c_str()+b.size()))));
    if(diff == 0) {
        TEST(col.hash(a.c_str(),a.c_str()+a.size()) == col.hash(b.c_str(),b.c_str()+b.size()));
    }
}

template<typename CharType>
void test_char()
{
    boost::locale::generator gen;
    
    std::cout << "- Testing at least C" << std::endl;

    std::locale l = gen("en_US.UTF-8");

    test_one<CharType>(l,"a","b",-1);
    test_one<CharType>(l,"a","a",0);

    std::string name;


    #if !defined(__APPLE__) && !defined(__FreeBSD__)
    std::string names[] = { "en_US.UTF-8", "en_US.ISO8859-1" };
    for(unsigned i=0;i<sizeof(names)/sizeof(names[0]);i++) {
        if(have_locale(names[i])) {
            name = names[i];
            std::cout << "- Testing " << name << std::endl;
            std::locale l=gen(name);
            test_one<CharType>(l,"a","ç",-1);
            test_one<CharType>(l,"ç","d",-1);
        }
        else {
            std::cout << "- " << names[i] << " not supported, skipping" << std::endl;
        }
    }
    #else
    std::cout << "- Collation is broken on this OS C standard library, skipping" << std::endl;
    #endif
}


int main()
{
    try {
        boost::locale::localization_backend_manager mgr = boost::locale::localization_backend_manager::global();
        mgr.select("posix");
        boost::locale::localization_backend_manager::global(mgr);

        std::cout << "Testing char" << std::endl;
        test_char<char>();
        std::cout << "Testing wchar_t" << std::endl;
        test_char<wchar_t>();
    }
    catch(std::exception const &e) {
        std::cerr << "Failed " << e.what() << std::endl;
        return EXIT_FAILURE;
    }
    FINALIZE();

}
#endif  // NO POSIX
// vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4


// boostinspect:noascii