File: test_utf_encoding.cpp

package info (click to toggle)
mapnik 3.0.12%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 17,084 kB
  • ctags: 18,454
  • sloc: cpp: 142,516; python: 1,416; sh: 769; makefile: 170; xml: 140; lisp: 13
file content (112 lines) | stat: -rw-r--r-- 3,273 bytes parent folder | download | duplicates (4)
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
#include "bench_framework.hpp"
#include <mapnik/unicode.hpp>
#include <mapnik/value.hpp>
#include <boost/locale.hpp>
#ifndef __linux__
#include <codecvt>

class test : public benchmark::test_case
{
    std::string utf8_;
public:
    test(mapnik::parameters const& params)
     : test_case(params),
       utf8_(u8"שלום") {}
    bool validate() const
    {
        std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> utf32conv;
        std::u32string utf32 = utf32conv.from_bytes(utf8_);
        if (utf32.size() != 4) return false;
        if (utf32[0] != 0x5e9 &&
            utf32[1] != 0x5dc &&
            utf32[2] != 0x5d5 &&
            utf32[3] != 0x5dd) return false;
        return true;
    }
    bool operator()() const
    {
        std::u32string utf32;
        std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> utf32conv;
        for (std::size_t i=0;i<iterations_;++i) {
             utf32 = utf32conv.from_bytes(utf8_);
        }
        return true;
    }
};

#endif

class test2 : public benchmark::test_case
{
    std::string utf8_;
public:
    test2(mapnik::parameters const& params)
     : test_case(params),
       utf8_(u8"שלום") {}
    bool validate() const
    {
        std::u32string utf32 = boost::locale::conv::utf_to_utf<char32_t>(utf8_);
        if (utf32.size() != 4) return false;
        if (utf32[0] != 0x5e9 &&
            utf32[1] != 0x5dc &&
            utf32[2] != 0x5d5 &&
            utf32[3] != 0x5dd) return false;
        return true;
    }
    bool operator()() const
    {
         std::u32string utf32;
         for (std::size_t i=0;i<iterations_;++i) {
             utf32 = boost::locale::conv::utf_to_utf<char32_t>(utf8_);
         }
        return true;
    }
};

class test3 : public benchmark::test_case
{
    std::string utf8_;
public:
    test3(mapnik::parameters const& params)
     : test_case(params),
       utf8_(u8"שלום") {}
    bool validate() const
    {
        mapnik::transcoder tr_("utf-8");
        mapnik::value_unicode_string utf32 = tr_.transcode(utf8_.data(),utf8_.size());
        //std::u32string utf32 = boost::locale::conv::utf_to_utf<char32_t>(utf8_);
        if (utf32.length() != 4) return false;
        if (utf32[0] != 0x5e9 &&
            utf32[1] != 0x5dc &&
            utf32[2] != 0x5d5 &&
            utf32[3] != 0x5dd) return false;
        return true;
    }
    bool operator()() const
    {
        mapnik::transcoder tr_("utf-8");
        mapnik::value_unicode_string utf32;
        for (std::size_t i=0;i<iterations_;++i) {
            utf32 = tr_.transcode(utf8_.data(),utf8_.size());
        }
        return true;
    }
};

int main(int argc, char** argv)
{
    mapnik::parameters params;
    benchmark::handle_args(argc,argv,params);
    int return_value = 0;
#ifndef __linux__
    test test_runner(params);
    return_value = return_value | run(test_runner,"utf encode std::codecvt");
#else
    std::clog << "skipping 'utf encode std::codecvt' test since <codecvt> is not supported on __linux__\n";
#endif
    test2 test_runner2(params);
    return_value = return_value | run(test_runner2,"utf encode boost::locale");
    test3 test_runner3(params);
    return_value = return_value | run(test_runner3,"utf encode ICU");
    return return_value;
}