File: algorithm_tests.cpp

package info (click to toggle)
pion 5.0.5%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 8,852 kB
  • ctags: 17,413
  • sloc: cpp: 13,965; sh: 10,263; perl: 269; makefile: 207; pascal: 152
file content (322 lines) | stat: -rw-r--r-- 15,032 bytes parent folder | download
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
311
312
313
314
315
316
317
318
319
320
321
322
// ---------------------------------------------------------------------
// pion:  a Boost C++ framework for building lightweight HTTP interfaces
// ---------------------------------------------------------------------
// Copyright (C) 2007-2012 Cloudmeter, Inc.  (http://www.cloudmeter.com)
//
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
//

#include <pion/config.hpp>
#include <pion/algorithm.hpp>
#include <boost/test/unit_test.hpp>

using namespace pion;


BOOST_AUTO_TEST_CASE(testURLEncoding) {
    BOOST_CHECK_EQUAL(algorithm::url_encode("hello world"), "hello%20world");
    BOOST_CHECK_EQUAL(algorithm::url_encode("He said, \"Hello, World!\""),
                      "He%20said%2C%20%22Hello%2C%20World!%22");
}

BOOST_AUTO_TEST_CASE(testURLEncodingOfStringWithNegativeCharacter) {
    std::string s = "abcde";
    s[0] = -30;
    BOOST_CHECK_EQUAL(algorithm::url_encode(s), "%E2bcde");
}

BOOST_AUTO_TEST_CASE(checkXmlEncodeWithAlphanumericString) {
    BOOST_CHECK_EQUAL("Freedom7", algorithm::xml_encode("Freedom7"));
}

BOOST_AUTO_TEST_CASE(checkXmlEncodeWithAmpersand) {
    BOOST_CHECK_EQUAL("A&amp;P", algorithm::xml_encode("A&P"));
}

BOOST_AUTO_TEST_CASE(checkXmlEncodeWithVariousSpecialXmlCharacters) {
    BOOST_CHECK_EQUAL("&quot;1&quot; &lt; &quot;2&quot; &amp;&amp; &apos;b&apos; &gt; &apos;a&apos;", algorithm::xml_encode("\"1\" < \"2\" && 'b' > 'a'"));
}

// UTF-8 replacement character
const std::string RC = "\xEF\xBF\xBD";

BOOST_AUTO_TEST_CASE(checkXmlEncodeWithControlCharacters) {
    char cc_array_1[] = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F";
    char cc_array_2[] = "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F";
    std::string cc_str_1(cc_array_1, 16);
    std::string cc_str_2(cc_array_2, 16);
    std::string expected_output_1 = RC + RC + RC + RC + RC + RC + RC + RC + RC + "\x09\x0A" + RC + RC + "\x0D" + RC + RC;
    std::string expected_output_2 = RC + RC + RC + RC + RC + RC + RC + RC + RC + RC + RC + RC + RC + RC + RC + RC;

    BOOST_CHECK_EQUAL(expected_output_1, algorithm::xml_encode(cc_str_1));
    BOOST_CHECK_EQUAL(expected_output_2, algorithm::xml_encode(cc_str_2));
}

BOOST_AUTO_TEST_CASE(checkXmlEncodeWithValidUtf8TwoByteSequences) {
    const char UTF8_ENCODED_TEST_CHAR_ARRAY[] = {
        (char)0xCE, (char)0xB1,             // UTF-8 encoding of U+03B1 (GREEK SMALL LETTER ALPHA)
        0x3D,                               // '='
        0x31,                               // '1'
        0x20,                               // space
        (char)0xCE, (char)0xB2,             // UTF-8 encoding of U+03B2 (GREEK SMALL LETTER BETA)
        0x3D,                               // '='
        0x32};                              // '2'
    const std::string UTF8_ENCODED_TEST_STRING(UTF8_ENCODED_TEST_CHAR_ARRAY, sizeof(UTF8_ENCODED_TEST_CHAR_ARRAY));
    BOOST_CHECK_EQUAL(UTF8_ENCODED_TEST_STRING, algorithm::xml_encode(UTF8_ENCODED_TEST_STRING));
}

BOOST_AUTO_TEST_CASE(checkXmlEncodeWithValidUtf8ThreeByteSequences) {
    const char UTF8_ENCODED_TEST_CHAR_ARRAY[] = {
        (char)0xE2, (char)0x82, (char)0xA4,     // UTF-8 encoding of U+20A4 (LIRA SIGN)
        0x32,                                   // '2'
        0x3D,                                   // '='
        (char)0xE2, (char)0x82, (char)0xA8,     // UTF-8 encoding of U+20A8 (RUPEE SIGN)
        0x32};                                  // '3'
    const std::string UTF8_ENCODED_TEST_STRING(UTF8_ENCODED_TEST_CHAR_ARRAY, sizeof(UTF8_ENCODED_TEST_CHAR_ARRAY));
    BOOST_CHECK_EQUAL(UTF8_ENCODED_TEST_STRING, algorithm::xml_encode(UTF8_ENCODED_TEST_STRING));
}

BOOST_AUTO_TEST_CASE(checkXmlEncodeWithValidUtf8FourByteSequences) {
    char UTF8_ENCODED_TEST_CHAR_ARRAY[] = {
        (char)0xF0, (char)0x90, (char)0x82, (char)0x88,     // UTF-8 encoding of U+10088 (LINEAR B IDEOGRAM B107F SHE-GOAT)
        (char)0xE2, (char)0x82, (char)0xA8,                 // UTF-8 encoding of U+2260 (NOT EQUAL TO)
        (char)0xF0, (char)0x90, (char)0x82, (char)0x89};    // UTF-8 encoding of U+10089 (LINEAR B IDEOGRAM B107M HE-GOAT)
    const std::string UTF8_ENCODED_TEST_STRING(UTF8_ENCODED_TEST_CHAR_ARRAY, sizeof(UTF8_ENCODED_TEST_CHAR_ARRAY));
    BOOST_CHECK_EQUAL(UTF8_ENCODED_TEST_STRING, algorithm::xml_encode(UTF8_ENCODED_TEST_STRING));
}

// Any isolated high byte (i.e. > x7F) is invalid, but they are invalid for a variety of reasons.
BOOST_AUTO_TEST_CASE(checkXmlEncodeWithInvalidSingleHighByte) {
    // These are invalid because the second byte is > x7F and is not allowed as the first byte of a UTF-8 multi-byte sequence.
    BOOST_CHECK_EQUAL("=" + RC + "=", algorithm::xml_encode("=\x80="));
    BOOST_CHECK_EQUAL("=" + RC + "=", algorithm::xml_encode("=\xBF="));
    BOOST_CHECK_EQUAL("=" + RC + "=", algorithm::xml_encode("=\xC0="));
    BOOST_CHECK_EQUAL("=" + RC + "=", algorithm::xml_encode("=\xC1="));
    BOOST_CHECK_EQUAL("=" + RC + "=", algorithm::xml_encode("=\xF5="));
    BOOST_CHECK_EQUAL("=" + RC + "=", algorithm::xml_encode("=\xFF="));

    // These are invalid because the second byte is the first byte of a UTF-8 2-byte sequence, but isn't followed by a valid second byte.
    BOOST_CHECK_EQUAL("=" + RC + "=", algorithm::xml_encode("=\xC2="));
    BOOST_CHECK_EQUAL("=" + RC + "=", algorithm::xml_encode("=\xDF="));

    // These are invalid because the second byte is the first byte of a UTF-8 3-byte sequence, but isn't followed by a valid second byte.
    BOOST_CHECK_EQUAL("=" + RC + "=", algorithm::xml_encode("=\xE0="));
    BOOST_CHECK_EQUAL("=" + RC + "=", algorithm::xml_encode("=\xEF="));

    // These are invalid because the second byte is the first byte of a UTF-8 4-byte sequence, but isn't followed by a valid second byte.
    BOOST_CHECK_EQUAL("=" + RC + "=", algorithm::xml_encode("=\xF0="));
    BOOST_CHECK_EQUAL("=" + RC + "=", algorithm::xml_encode("=\xF4="));
}

BOOST_AUTO_TEST_CASE(checkXmlEncodeWithInvalidTwoHighByteSequence) {
    // These are invalid because the second byte is the first byte of a UTF-8 2-byte sequence, but isn't followed by a valid second byte.
    BOOST_CHECK_EQUAL("=" + RC + RC + "=", algorithm::xml_encode("=\xC2\xC0="));
    BOOST_CHECK_EQUAL("=" + RC + RC + "=", algorithm::xml_encode("=\xDF\xFF="));

    // These are invalid because bytes 2 & 3 are the first and second bytes of a UTF-8 3-byte sequence, but aren't followed by a valid third byte.
    BOOST_CHECK_EQUAL("=" + RC + RC + "=", algorithm::xml_encode("=\xE0\x80="));
    BOOST_CHECK_EQUAL("=" + RC + RC + "=", algorithm::xml_encode("=\xEF\xBF="));

    // These are invalid because bytes 2 & 3 are the first and second bytes of a UTF-8 4-byte sequence, but aren't followed by a valid third byte.
    BOOST_CHECK_EQUAL("=" + RC + RC + "=", algorithm::xml_encode("=\xF0\x80="));
    BOOST_CHECK_EQUAL("=" + RC + RC + "=", algorithm::xml_encode("=\xF4\xBF="));
}

BOOST_AUTO_TEST_CASE(checkXmlEncodeWithInvalidThreeHighByteSequence) {
    // These are invalid because bytes 2 & 3 are the first and second bytes of a UTF-8 3-byte sequence, but aren't followed by a valid third byte.
    BOOST_CHECK_EQUAL("=" + RC + RC + RC + "=", algorithm::xml_encode("=\xE0\x80\xC0="));
    BOOST_CHECK_EQUAL("=" + RC + RC + RC + "=", algorithm::xml_encode("=\xEF\xBF\xFF="));

    // These are invalid because bytes 2 & 3 are the first and second bytes of a UTF-8 4-byte sequence, but aren't followed by a valid third byte.
    BOOST_CHECK_EQUAL("=" + RC + RC + RC + "=", algorithm::xml_encode("=\xF0\x80\xC0="));
    BOOST_CHECK_EQUAL("=" + RC + RC + RC + "=", algorithm::xml_encode("=\xF4\xBF\xFF="));
}

BOOST_AUTO_TEST_CASE(checkXmlEncodeWithInvalidFourHighByteSequence) {
    // These are invalid because bytes 2-4  are the first, second and third bytes of a UTF-8 4-byte sequence, but aren't followed by a valid fourth byte.
    BOOST_CHECK_EQUAL("=" + RC + RC + RC + RC + "=", algorithm::xml_encode("=\xF0\x80\x80\xC0="));
    BOOST_CHECK_EQUAL("=" + RC + RC + RC + RC + "=", algorithm::xml_encode("=\xF4\xBF\xBF\xFF="));
}

BOOST_AUTO_TEST_CASE(testBase64Routines) {
    std::string original;
    std::string original_base64;

    std::string encoded;
    std::string decoded;

    original = "mike:123456";
    original_base64 = "bWlrZToxMjM0NTY=";

    BOOST_CHECK(algorithm::base64_encode(original,encoded));
    BOOST_CHECK(encoded == original_base64);
    BOOST_CHECK(algorithm::base64_decode(encoded,decoded));
    BOOST_CHECK(decoded == original);

    original = "mike:12345";
    BOOST_CHECK(algorithm::base64_encode(original,encoded));
    BOOST_CHECK(algorithm::base64_decode(encoded,decoded));
    BOOST_CHECK(decoded == original);

    original = "mike:1234";
    BOOST_CHECK(algorithm::base64_encode(original,encoded));
    BOOST_CHECK(algorithm::base64_decode(encoded,decoded));
    BOOST_CHECK(decoded == original);

    original = "mike:123";
    BOOST_CHECK(algorithm::base64_encode(original,encoded));
    BOOST_CHECK(algorithm::base64_decode(encoded,decoded));
    BOOST_CHECK(decoded == original);

    const char *ptr = "mike\0123\0\0";
    original.assign(ptr, 10);
    BOOST_CHECK(algorithm::base64_encode(original,encoded));
    BOOST_CHECK(algorithm::base64_decode(encoded,decoded));
    BOOST_CHECK_EQUAL(decoded.size(), 10U);
    BOOST_CHECK_EQUAL(memcmp(decoded.c_str(), ptr, 10), 0);
}

BOOST_AUTO_TEST_CASE(testCharFromToIntRoutines) {
    char buf[8];
    
    algorithm::from_uint8(buf, 129U);
    BOOST_CHECK_EQUAL(buf[0], (char)0x81);
    BOOST_CHECK_EQUAL(algorithm::to_int8(buf), boost::int8_t(0x81));
    BOOST_CHECK_EQUAL(algorithm::to_uint8(buf), 129U);

    algorithm::from_uint16(buf, 32769U);
    BOOST_CHECK_EQUAL(buf[0], (char)0x80);
    BOOST_CHECK_EQUAL(buf[1], (char)0x01);
    BOOST_CHECK_EQUAL(algorithm::to_int16(buf), boost::int16_t(0x8001));
    BOOST_CHECK_EQUAL(algorithm::to_uint16(buf), 32769U);

    algorithm::from_uint24(buf, 9642497U);
    BOOST_CHECK_EQUAL(buf[0], (char)0x93);
    BOOST_CHECK_EQUAL(buf[1], (char)0x22);
    BOOST_CHECK_EQUAL(buf[2], (char)0x01);
    BOOST_CHECK_EQUAL(algorithm::to_int24(buf), boost::int32_t(0x932201));
    BOOST_CHECK_EQUAL(algorithm::to_uint24(buf), 9642497U);

    algorithm::from_uint32(buf, 2147680769UL);
    BOOST_CHECK_EQUAL(buf[0], (char)0x80);
    BOOST_CHECK_EQUAL(buf[1], (char)0x03);
    BOOST_CHECK_EQUAL(buf[2], (char)0x02);
    BOOST_CHECK_EQUAL(buf[3], (char)0x01);
    BOOST_CHECK_EQUAL(algorithm::to_int32(buf), boost::int32_t(0x80030201));
    BOOST_CHECK_EQUAL(algorithm::to_uint32(buf), 2147680769UL);

    algorithm::from_uint32(buf, 1427U);
    BOOST_CHECK_EQUAL(buf[0], (char)0x00);
    BOOST_CHECK_EQUAL(buf[1], (char)0x00);
    BOOST_CHECK_EQUAL(buf[2], (char)0x05);
    BOOST_CHECK_EQUAL(buf[3], (char)0x93);
    BOOST_CHECK_EQUAL(algorithm::to_int32(buf), boost::int32_t(0x00000593));
    BOOST_CHECK_EQUAL(algorithm::to_uint32(buf), 1427U);

    algorithm::from_uint64(buf, 9223378168241586176ULL);
    BOOST_CHECK_EQUAL(buf[0], (char)0x80);
    BOOST_CHECK_EQUAL(buf[1], (char)0x00);
    BOOST_CHECK_EQUAL(buf[2], (char)0x05);
    BOOST_CHECK_EQUAL(buf[3], (char)0x93);
    BOOST_CHECK_EQUAL(buf[4], (char)0x93);
    BOOST_CHECK_EQUAL(buf[5], (char)0x22);
    BOOST_CHECK_EQUAL(buf[6], (char)0x00);
    BOOST_CHECK_EQUAL(buf[7], (char)0x00);
    BOOST_CHECK_EQUAL(algorithm::to_int64(buf), boost::int64_t(0x8000059393220000ULL));
    BOOST_CHECK_EQUAL(algorithm::to_uint64(buf), 9223378168241586176ULL);
}

BOOST_AUTO_TEST_CASE(testCharFromToFloatRoutines) {
    char buf[16];
    
    algorithm::from_float(buf, 0.0);
    BOOST_CHECK_EQUAL(buf[0], (char)0x00);
    BOOST_CHECK_EQUAL(buf[1], (char)0x00);
    BOOST_CHECK_EQUAL(buf[2], (char)0x00);
    BOOST_CHECK_EQUAL(buf[3], (char)0x00);
    BOOST_CHECK_EQUAL(algorithm::to_float(buf), 0.0);
    
    algorithm::from_float(buf, -13021.0);
    BOOST_CHECK_EQUAL(buf[0], (char)0xc6);
    BOOST_CHECK_EQUAL(buf[1], (char)0x4b);
    BOOST_CHECK_EQUAL(buf[2], (char)0x74);
    BOOST_CHECK_EQUAL(buf[3], (char)0x00);
    BOOST_CHECK_EQUAL(algorithm::to_float(buf), -13021.0);
    
    algorithm::from_float(buf, 12.375);
    BOOST_CHECK_EQUAL(buf[0], (char)0x41);
    BOOST_CHECK_EQUAL(buf[1], (char)0x46);
    BOOST_CHECK_EQUAL(buf[2], (char)0x00);
    BOOST_CHECK_EQUAL(buf[3], (char)0x00);
    BOOST_CHECK_EQUAL(algorithm::to_float(buf), 12.375);

    algorithm::from_float(buf, 1);
    BOOST_CHECK_EQUAL(buf[0], (char)0x3f);
    BOOST_CHECK_EQUAL(buf[1], (char)0x80);
    BOOST_CHECK_EQUAL(buf[2], (char)0x00);
    BOOST_CHECK_EQUAL(buf[3], (char)0x00);
    BOOST_CHECK_EQUAL(algorithm::to_float(buf), 1.0);
    
    algorithm::from_float(buf, 0.25);
    BOOST_CHECK_EQUAL(buf[0], (char)0x3e);
    BOOST_CHECK_EQUAL(buf[1], (char)0x80);
    BOOST_CHECK_EQUAL(buf[2], (char)0x00);
    BOOST_CHECK_EQUAL(buf[3], (char)0x00);
    BOOST_CHECK_EQUAL(algorithm::to_float(buf), 0.25);
    
    algorithm::from_float(buf, 0.375);
    BOOST_CHECK_EQUAL(buf[0], (char)0x3e);
    BOOST_CHECK_EQUAL(buf[1], (char)0xc0);
    BOOST_CHECK_EQUAL(buf[2], (char)0x00);
    BOOST_CHECK_EQUAL(buf[3], (char)0x00);
    BOOST_CHECK_EQUAL(algorithm::to_float(buf), 0.375);
    
    algorithm::from_double(buf, 0);
    BOOST_CHECK_EQUAL(algorithm::to_double(buf), 0);

    algorithm::from_double(buf, -13021.0);
    BOOST_CHECK_EQUAL(algorithm::to_double(buf), -13021.0);
    
    algorithm::from_double(buf, 12.375);
    BOOST_CHECK_EQUAL(algorithm::to_double(buf), 12.375);
    
    algorithm::from_double(buf, 1);
    BOOST_CHECK_EQUAL(algorithm::to_double(buf), 1.0);
    
    algorithm::from_double(buf, 0.25);
    BOOST_CHECK_EQUAL(algorithm::to_double(buf), 0.25);
    
    algorithm::from_double(buf, 0.375);
    BOOST_CHECK_EQUAL(algorithm::to_double(buf), 0.375);
    
    algorithm::from_double(buf, 1.0123456789012345e-300);
    BOOST_CHECK_EQUAL(algorithm::to_double(buf), 1.0123456789012345e-300);
    
    algorithm::from_double(buf, 1.0123456789012345e+300);
    BOOST_CHECK_EQUAL(algorithm::to_double(buf), 1.0123456789012345e+300);
    
    algorithm::from_long_double(buf, 0);
    BOOST_CHECK_EQUAL(algorithm::to_long_double(buf), 0);
    
    algorithm::from_long_double(buf, -13021.0);
    BOOST_CHECK_EQUAL(algorithm::to_long_double(buf), -13021.0);
    
    algorithm::from_long_double(buf, 12.375);
    BOOST_CHECK_EQUAL(algorithm::to_long_double(buf), 12.375);
    
    algorithm::from_long_double(buf, 1);
    BOOST_CHECK_EQUAL(algorithm::to_long_double(buf), 1.0);
    
    algorithm::from_long_double(buf, 0.25);
    BOOST_CHECK_EQUAL(algorithm::to_long_double(buf), 0.25);

    algorithm::from_long_double(buf, 0.375);
    BOOST_CHECK_EQUAL(algorithm::to_long_double(buf), 0.375);

    algorithm::from_long_double(buf, 1.0123456789012345e-300);
    BOOST_CHECK_EQUAL(algorithm::to_long_double(buf), 1.0123456789012345e-300);

    algorithm::from_long_double(buf, 1.0123456789012345e+300);
    BOOST_CHECK_EQUAL(algorithm::to_long_double(buf), 1.0123456789012345e+300);
}