File: conversions_test.cpp

package info (click to toggle)
mapnik 4.2.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 18,548 kB
  • sloc: cpp: 163,861; python: 1,190; sh: 690; xml: 161; makefile: 123; perl: 28; lisp: 13
file content (310 lines) | stat: -rw-r--r-- 8,750 bytes parent folder | download | duplicates (3)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include "catch.hpp"

#include <mapnik/value/types.hpp>
#include <mapnik/value.hpp>
#include <mapnik/unicode.hpp>
#include <mapnik/util/conversions.hpp>

#include <iostream>
#include <unordered_map>
#include <sstream>

#if defined(_MSC_VER) && _MSC_VER < 1900
#include <cstdio>
#endif

TEST_CASE("conversions")
{
    SECTION("to string")
    {
#if defined(_MSC_VER) && _MSC_VER < 1900
        unsigned int old = _set_output_format(_TWO_DIGIT_EXPONENT);
#endif

        using mapnik::util::string2bool;
        using mapnik::util::to_string;

        try
        {
            std::string out;

            // Test double
            to_string(out, double(0));
            REQUIRE(out == "0");
            out.clear();

            to_string(out, double(1));
            REQUIRE(out == "1");
            out.clear();

            to_string(out, double(-1));
            REQUIRE(out == "-1");
            out.clear();

            to_string(out, double(0.1));
            REQUIRE(out == "0.1");
            out.clear();

            to_string(out, double(-0.1));
            REQUIRE(out == "-0.1");
            out.clear();

            to_string(out, double(0.123));
            REQUIRE(out == "0.123");
            out.clear();

            to_string(out, double(-0.123));
            REQUIRE(out == "-0.123");
            out.clear();

            to_string(out, double(1e-06));
            REQUIRE(out == "1e-06");
            out.clear();

            to_string(out, double(-1e-06));
            REQUIRE(out == "-1e-06");
            out.clear();

            to_string(out, double(1e-05));
            REQUIRE(out == "1e-05");
            out.clear();

            to_string(out, double(-1e-05));
            REQUIRE(out == "-1e-05");
            out.clear();

            to_string(out, double(0.0001));
            REQUIRE(out == "0.0001");
            out.clear();

            to_string(out, double(-0.0001));
            REQUIRE(out == "-0.0001");
            out.clear();

            to_string(out, double(0.0001));
            REQUIRE(out == "0.0001");
            out.clear();

            to_string(out, double(0.00001));
            REQUIRE(out == "1e-05");
            out.clear();

            to_string(out, double(0.000001));
            REQUIRE(out == "1e-06");
            out.clear();

            to_string(out, double(0.0000001));
            REQUIRE(out == "1e-07");
            out.clear();

            to_string(out, double(0.00000001));
            REQUIRE(out == "1e-08");
            out.clear();

            to_string(out, double(0.000000001));
            REQUIRE(out == "1e-09");
            out.clear();

            to_string(out, double(0.0000000001));
            REQUIRE(out == "1e-10");
            out.clear();

            to_string(out, double(-1.234e+16));
            REQUIRE(out == "-1.234e+16");
            out.clear();

            // critical failure when karam is used
            // https://github.com/mapnik/mapnik/issues/1741
            // https://github.com/mapbox/tilemill/issues/1456
            to_string(out, double(8.3));
            REQUIRE(out == "8.3");
            out.clear();

            // non-critical failures if karma is used
            to_string(out, double(0.0001234567890123456));
            // TODO: https://github.com/mapnik/mapnik/issues/1676
            REQUIRE(out == "0.000123457");
            out.clear();

            to_string(out, double(0.00000000001));
            REQUIRE(out == "1e-11");
            out.clear();

            to_string(out, double(0.000000000001));
            REQUIRE(out == "1e-12");
            out.clear();

            to_string(out, double(0.0000000000001));
            REQUIRE(out == "1e-13");
            out.clear();

            to_string(out, double(0.00000000000001));
            REQUIRE(out == "1e-14");
            out.clear();

            to_string(out, double(0.000000000000001));
            REQUIRE(out == "1e-15");
            out.clear();

            to_string(out, double(100000));
            REQUIRE(out == "100000");
            out.clear();

            to_string(out, double(1000000));
            REQUIRE(out == "1e+06");
            out.clear();

            to_string(out, double(10000000));
            REQUIRE(out == "1e+07");
            out.clear();

            to_string(out, double(100000000));
            REQUIRE(out == "1e+08");
            out.clear();

            to_string(out, double(1000000000));
            REQUIRE(out == "1e+09");
            out.clear();

            to_string(out, double(10000000000));
            REQUIRE(out == "1e+10");
            out.clear();

            to_string(out, double(100000000000));
            REQUIRE(out == "1e+11");
            out.clear();

            to_string(out, double(1000000000000));
            REQUIRE(out == "1e+12");
            out.clear();

            to_string(out, double(10000000000000));
            REQUIRE(out == "1e+13");
            out.clear();

            to_string(out, double(100000000000000));
            REQUIRE(out == "1e+14");
            out.clear();

            to_string(out, double(1000000000000005));
            REQUIRE(out == "1e+15");
            out.clear();

            to_string(out, double(-1000000000000000));
            REQUIRE(out == "-1e+15");
            out.clear();

            to_string(out, double(100000000000000.1));
            REQUIRE(out == "1e+14");
            out.clear();

            to_string(out, double(1.00001));
            REQUIRE(out == "1.00001");
            out.clear();

            to_string(out, double(67.65));
            REQUIRE(out == "67.65");
            out.clear();

            to_string(out, double(67.35));
            REQUIRE(out == "67.35");
            out.clear();

            to_string(out, double(1234000000000000));
            REQUIRE(out == "1.234e+15");
            out.clear();

            to_string(out, double(1e+16));
            REQUIRE(out == "1e+16");
            out.clear();

            to_string(out, double(1.234e+16));
            REQUIRE(out == "1.234e+16");
            out.clear();

            // int
            to_string(out, int(2));
            REQUIRE(out == "2");
            out.clear();

            to_string(out, int(0));
            REQUIRE(out == "0");
            out.clear();

            to_string(out, int(-2));
            REQUIRE(out == "-2");
            out.clear();

            to_string(out, int(2147483647));
            REQUIRE(out == "2147483647");
            out.clear();

            to_string(out, int(-2147483648));
            REQUIRE(out == "-2147483648");
            out.clear();

            // unsigned
            to_string(out, unsigned(4294967295));
            REQUIRE(out == "4294967295");
            out.clear();

#ifdef BIGINT
            // long long
            to_string(out, mapnik::value_integer(-0));
            REQUIRE(out == "0");
            out.clear();

            to_string(out, mapnik::value_integer(-2));
            REQUIRE(out == "-2");
            out.clear();

            to_string(out, mapnik::value_integer(9223372036854775807));
            REQUIRE(out == "9223372036854775807");
            out.clear();
#else
#ifdef _MSC_VER
#pragma NOTE("BIGINT not defined so skipping large number conversion tests")
#else
#warning BIGINT not defined so skipping large number conversion tests
#endif
#endif
            // bool
            to_string(out, true);
            REQUIRE(out == "true");
            out.clear();

            to_string(out, false);
            REQUIRE(out == "false");
            out.clear();

            bool val = false;
            REQUIRE(!string2bool("this is invalid", val));
            REQUIRE(val == false);
            REQUIRE(string2bool("true", val));
            REQUIRE(val == true);

            // mapnik::value hash() and operator== works for all T in value<Types...>
            mapnik::transcoder tr("utf8");
            using values_container = std::unordered_map<mapnik::value, mapnik::value>;
            values_container vc;
            mapnik::value keys[5] = {true, 123456789, 3.14159f, tr.transcode("Мапник"), mapnik::value_null()};
            for (auto const& k : keys)
            {
                vc.insert({k, k});
                REQUIRE(vc[k] == k);
            }

            // mapnik::value << to ostream
            std::stringstream s;
            mapnik::value_unicode_string ustr = tr.transcode("hello world!");
            mapnik::value streamable(ustr);
            s << streamable;
            CHECK(s.str() == std::string("hello world!"));
        }
        catch (std::exception const& ex)
        {
            std::clog << ex.what() << "\n";
            REQUIRE(false);
        }
    }
}