1 //
2 // Copyright 2019 The Abseil Authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // https://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15
16 #include "absl/flags/marshalling.h"
17
18 #include <stdint.h>
19
20 #include <cmath>
21 #include <limits>
22 #include <string>
23 #include <vector>
24
25 #include "gtest/gtest.h"
26
27 namespace {
28
TEST(MarshallingTest,TestBoolParsing)29 TEST(MarshallingTest, TestBoolParsing) {
30 std::string err;
31 bool value;
32
33 // True values.
34 EXPECT_TRUE(absl::ParseFlag("True", &value, &err));
35 EXPECT_TRUE(value);
36 EXPECT_TRUE(absl::ParseFlag("true", &value, &err));
37 EXPECT_TRUE(value);
38 EXPECT_TRUE(absl::ParseFlag("TRUE", &value, &err));
39 EXPECT_TRUE(value);
40
41 EXPECT_TRUE(absl::ParseFlag("Yes", &value, &err));
42 EXPECT_TRUE(value);
43 EXPECT_TRUE(absl::ParseFlag("yes", &value, &err));
44 EXPECT_TRUE(value);
45 EXPECT_TRUE(absl::ParseFlag("YES", &value, &err));
46 EXPECT_TRUE(value);
47
48 EXPECT_TRUE(absl::ParseFlag("t", &value, &err));
49 EXPECT_TRUE(value);
50 EXPECT_TRUE(absl::ParseFlag("T", &value, &err));
51 EXPECT_TRUE(value);
52
53 EXPECT_TRUE(absl::ParseFlag("y", &value, &err));
54 EXPECT_TRUE(value);
55 EXPECT_TRUE(absl::ParseFlag("Y", &value, &err));
56 EXPECT_TRUE(value);
57
58 EXPECT_TRUE(absl::ParseFlag("1", &value, &err));
59 EXPECT_TRUE(value);
60
61 // False values.
62 EXPECT_TRUE(absl::ParseFlag("False", &value, &err));
63 EXPECT_FALSE(value);
64 EXPECT_TRUE(absl::ParseFlag("false", &value, &err));
65 EXPECT_FALSE(value);
66 EXPECT_TRUE(absl::ParseFlag("FALSE", &value, &err));
67 EXPECT_FALSE(value);
68
69 EXPECT_TRUE(absl::ParseFlag("No", &value, &err));
70 EXPECT_FALSE(value);
71 EXPECT_TRUE(absl::ParseFlag("no", &value, &err));
72 EXPECT_FALSE(value);
73 EXPECT_TRUE(absl::ParseFlag("NO", &value, &err));
74 EXPECT_FALSE(value);
75
76 EXPECT_TRUE(absl::ParseFlag("f", &value, &err));
77 EXPECT_FALSE(value);
78 EXPECT_TRUE(absl::ParseFlag("F", &value, &err));
79 EXPECT_FALSE(value);
80
81 EXPECT_TRUE(absl::ParseFlag("n", &value, &err));
82 EXPECT_FALSE(value);
83 EXPECT_TRUE(absl::ParseFlag("N", &value, &err));
84 EXPECT_FALSE(value);
85
86 EXPECT_TRUE(absl::ParseFlag("0", &value, &err));
87 EXPECT_FALSE(value);
88
89 // Whitespace handling.
90 EXPECT_TRUE(absl::ParseFlag(" true", &value, &err));
91 EXPECT_TRUE(value);
92 EXPECT_TRUE(absl::ParseFlag("true ", &value, &err));
93 EXPECT_TRUE(value);
94 EXPECT_TRUE(absl::ParseFlag(" true ", &value, &err));
95 EXPECT_TRUE(value);
96
97 // Invalid input.
98 EXPECT_FALSE(absl::ParseFlag("", &value, &err));
99 EXPECT_FALSE(absl::ParseFlag(" ", &value, &err));
100 EXPECT_FALSE(absl::ParseFlag("\n", &value, &err));
101 EXPECT_FALSE(absl::ParseFlag("\t", &value, &err));
102 EXPECT_FALSE(absl::ParseFlag("2", &value, &err));
103 EXPECT_FALSE(absl::ParseFlag("11", &value, &err));
104 EXPECT_FALSE(absl::ParseFlag("tt", &value, &err));
105 }
106
107 // --------------------------------------------------------------------
108
TEST(MarshallingTest,TestInt16Parsing)109 TEST(MarshallingTest, TestInt16Parsing) {
110 std::string err;
111 int16_t value;
112
113 // Decimal values.
114 EXPECT_TRUE(absl::ParseFlag("1", &value, &err));
115 EXPECT_EQ(value, 1);
116 EXPECT_TRUE(absl::ParseFlag("0", &value, &err));
117 EXPECT_EQ(value, 0);
118 EXPECT_TRUE(absl::ParseFlag("-1", &value, &err));
119 EXPECT_EQ(value, -1);
120 EXPECT_TRUE(absl::ParseFlag("123", &value, &err));
121 EXPECT_EQ(value, 123);
122 EXPECT_TRUE(absl::ParseFlag("-18765", &value, &err));
123 EXPECT_EQ(value, -18765);
124 EXPECT_TRUE(absl::ParseFlag("+3", &value, &err));
125 EXPECT_EQ(value, 3);
126
127 // Leading zero values.
128 EXPECT_TRUE(absl::ParseFlag("01", &value, &err));
129 EXPECT_EQ(value, 1);
130 EXPECT_TRUE(absl::ParseFlag("-001", &value, &err));
131 EXPECT_EQ(value, -1);
132 EXPECT_TRUE(absl::ParseFlag("0000100", &value, &err));
133 EXPECT_EQ(value, 100);
134
135 // Hex values.
136 EXPECT_TRUE(absl::ParseFlag("0x10", &value, &err));
137 EXPECT_EQ(value, 16);
138 EXPECT_TRUE(absl::ParseFlag("0X234", &value, &err));
139 EXPECT_EQ(value, 564);
140 // TODO(rogeeff): fix below validations
141 EXPECT_FALSE(absl::ParseFlag("-0x7FFD", &value, &err));
142 EXPECT_NE(value, -3);
143 EXPECT_FALSE(absl::ParseFlag("+0x31", &value, &err));
144 EXPECT_NE(value, 49);
145
146 // Whitespace handling
147 EXPECT_TRUE(absl::ParseFlag("10 ", &value, &err));
148 EXPECT_EQ(value, 10);
149 EXPECT_TRUE(absl::ParseFlag(" 11", &value, &err));
150 EXPECT_EQ(value, 11);
151 EXPECT_TRUE(absl::ParseFlag(" 012 ", &value, &err));
152 EXPECT_EQ(value, 12);
153 EXPECT_TRUE(absl::ParseFlag(" 0x22 ", &value, &err));
154 EXPECT_EQ(value, 34);
155
156 // Invalid values.
157 EXPECT_FALSE(absl::ParseFlag("", &value, &err));
158 EXPECT_FALSE(absl::ParseFlag(" ", &value, &err));
159 EXPECT_FALSE(absl::ParseFlag(" ", &value, &err));
160 EXPECT_FALSE(absl::ParseFlag("40000", &value, &err));
161 EXPECT_FALSE(absl::ParseFlag("--1", &value, &err));
162 EXPECT_FALSE(absl::ParseFlag("\n", &value, &err));
163 EXPECT_FALSE(absl::ParseFlag("\t", &value, &err));
164 EXPECT_FALSE(absl::ParseFlag("2U", &value, &err));
165 EXPECT_FALSE(absl::ParseFlag("FFF", &value, &err));
166 }
167
168 // --------------------------------------------------------------------
169
TEST(MarshallingTest,TestUint16Parsing)170 TEST(MarshallingTest, TestUint16Parsing) {
171 std::string err;
172 uint16_t value;
173
174 // Decimal values.
175 EXPECT_TRUE(absl::ParseFlag("1", &value, &err));
176 EXPECT_EQ(value, 1);
177 EXPECT_TRUE(absl::ParseFlag("0", &value, &err));
178 EXPECT_EQ(value, 0);
179 EXPECT_TRUE(absl::ParseFlag("123", &value, &err));
180 EXPECT_EQ(value, 123);
181 EXPECT_TRUE(absl::ParseFlag("+3", &value, &err));
182 EXPECT_EQ(value, 3);
183
184 // Leading zero values.
185 EXPECT_TRUE(absl::ParseFlag("01", &value, &err));
186 EXPECT_EQ(value, 1);
187 EXPECT_TRUE(absl::ParseFlag("001", &value, &err));
188 EXPECT_EQ(value, 1);
189 EXPECT_TRUE(absl::ParseFlag("0000100", &value, &err));
190 EXPECT_EQ(value, 100);
191
192 // Hex values.
193 EXPECT_TRUE(absl::ParseFlag("0x10", &value, &err));
194 EXPECT_EQ(value, 16);
195 EXPECT_TRUE(absl::ParseFlag("0X234", &value, &err));
196 EXPECT_EQ(value, 564);
197 // TODO(rogeeff): fix below validations
198 EXPECT_FALSE(absl::ParseFlag("+0x31", &value, &err));
199 EXPECT_NE(value, 49);
200
201 // Whitespace handling
202 EXPECT_TRUE(absl::ParseFlag("10 ", &value, &err));
203 EXPECT_EQ(value, 10);
204 EXPECT_TRUE(absl::ParseFlag(" 11", &value, &err));
205 EXPECT_EQ(value, 11);
206 EXPECT_TRUE(absl::ParseFlag(" 012 ", &value, &err));
207 EXPECT_EQ(value, 12);
208 EXPECT_TRUE(absl::ParseFlag(" 0x22 ", &value, &err));
209 EXPECT_EQ(value, 34);
210
211 // Invalid values.
212 EXPECT_FALSE(absl::ParseFlag("", &value, &err));
213 EXPECT_FALSE(absl::ParseFlag(" ", &value, &err));
214 EXPECT_FALSE(absl::ParseFlag(" ", &value, &err));
215 EXPECT_FALSE(absl::ParseFlag("70000", &value, &err));
216 EXPECT_FALSE(absl::ParseFlag("-1", &value, &err));
217 EXPECT_FALSE(absl::ParseFlag("--1", &value, &err));
218 EXPECT_FALSE(absl::ParseFlag("\n", &value, &err));
219 EXPECT_FALSE(absl::ParseFlag("\t", &value, &err));
220 EXPECT_FALSE(absl::ParseFlag("2U", &value, &err));
221 EXPECT_FALSE(absl::ParseFlag("FFF", &value, &err));
222 }
223
224 // --------------------------------------------------------------------
225
TEST(MarshallingTest,TestInt32Parsing)226 TEST(MarshallingTest, TestInt32Parsing) {
227 std::string err;
228 int32_t value;
229
230 // Decimal values.
231 EXPECT_TRUE(absl::ParseFlag("1", &value, &err));
232 EXPECT_EQ(value, 1);
233 EXPECT_TRUE(absl::ParseFlag("0", &value, &err));
234 EXPECT_EQ(value, 0);
235 EXPECT_TRUE(absl::ParseFlag("-1", &value, &err));
236 EXPECT_EQ(value, -1);
237 EXPECT_TRUE(absl::ParseFlag("123", &value, &err));
238 EXPECT_EQ(value, 123);
239 EXPECT_TRUE(absl::ParseFlag("-98765", &value, &err));
240 EXPECT_EQ(value, -98765);
241 EXPECT_TRUE(absl::ParseFlag("+3", &value, &err));
242 EXPECT_EQ(value, 3);
243
244 // Leading zero values.
245 EXPECT_TRUE(absl::ParseFlag("01", &value, &err));
246 EXPECT_EQ(value, 1);
247 EXPECT_TRUE(absl::ParseFlag("-001", &value, &err));
248 EXPECT_EQ(value, -1);
249 EXPECT_TRUE(absl::ParseFlag("0000100", &value, &err));
250 EXPECT_EQ(value, 100);
251
252 // Hex values.
253 EXPECT_TRUE(absl::ParseFlag("0x10", &value, &err));
254 EXPECT_EQ(value, 16);
255 EXPECT_TRUE(absl::ParseFlag("0X234", &value, &err));
256 EXPECT_EQ(value, 564);
257 // TODO(rogeeff): fix below validations
258 EXPECT_FALSE(absl::ParseFlag("-0x7FFFFFFD", &value, &err));
259 EXPECT_NE(value, -3);
260 EXPECT_FALSE(absl::ParseFlag("+0x31", &value, &err));
261 EXPECT_NE(value, 49);
262
263 // Whitespace handling
264 EXPECT_TRUE(absl::ParseFlag("10 ", &value, &err));
265 EXPECT_EQ(value, 10);
266 EXPECT_TRUE(absl::ParseFlag(" 11", &value, &err));
267 EXPECT_EQ(value, 11);
268 EXPECT_TRUE(absl::ParseFlag(" 012 ", &value, &err));
269 EXPECT_EQ(value, 12);
270 EXPECT_TRUE(absl::ParseFlag(" 0x22 ", &value, &err));
271 EXPECT_EQ(value, 34);
272
273 // Invalid values.
274 EXPECT_FALSE(absl::ParseFlag("", &value, &err));
275 EXPECT_FALSE(absl::ParseFlag(" ", &value, &err));
276 EXPECT_FALSE(absl::ParseFlag(" ", &value, &err));
277 EXPECT_FALSE(absl::ParseFlag("70000000000", &value, &err));
278 EXPECT_FALSE(absl::ParseFlag("--1", &value, &err));
279 EXPECT_FALSE(absl::ParseFlag("\n", &value, &err));
280 EXPECT_FALSE(absl::ParseFlag("\t", &value, &err));
281 EXPECT_FALSE(absl::ParseFlag("2U", &value, &err));
282 EXPECT_FALSE(absl::ParseFlag("FFF", &value, &err));
283 }
284
285 // --------------------------------------------------------------------
286
TEST(MarshallingTest,TestUint32Parsing)287 TEST(MarshallingTest, TestUint32Parsing) {
288 std::string err;
289 uint32_t value;
290
291 // Decimal values.
292 EXPECT_TRUE(absl::ParseFlag("1", &value, &err));
293 EXPECT_EQ(value, 1);
294 EXPECT_TRUE(absl::ParseFlag("0", &value, &err));
295 EXPECT_EQ(value, 0);
296 EXPECT_TRUE(absl::ParseFlag("123", &value, &err));
297 EXPECT_EQ(value, 123);
298 EXPECT_TRUE(absl::ParseFlag("+3", &value, &err));
299 EXPECT_EQ(value, 3);
300
301 // Leading zero values.
302 EXPECT_TRUE(absl::ParseFlag("01", &value, &err));
303 EXPECT_EQ(value, 1);
304 EXPECT_TRUE(absl::ParseFlag("0000100", &value, &err));
305 EXPECT_EQ(value, 100);
306
307 // Hex values.
308 EXPECT_TRUE(absl::ParseFlag("0x10", &value, &err));
309 EXPECT_EQ(value, 16);
310 EXPECT_TRUE(absl::ParseFlag("0X234", &value, &err));
311 EXPECT_EQ(value, 564);
312 EXPECT_TRUE(absl::ParseFlag("0xFFFFFFFD", &value, &err));
313 EXPECT_EQ(value, 4294967293);
314 // TODO(rogeeff): fix below validations
315 EXPECT_FALSE(absl::ParseFlag("+0x31", &value, &err));
316 EXPECT_NE(value, 49);
317
318 // Whitespace handling
319 EXPECT_TRUE(absl::ParseFlag("10 ", &value, &err));
320 EXPECT_EQ(value, 10);
321 EXPECT_TRUE(absl::ParseFlag(" 11", &value, &err));
322 EXPECT_EQ(value, 11);
323 EXPECT_TRUE(absl::ParseFlag(" 012 ", &value, &err));
324 EXPECT_EQ(value, 12);
325 EXPECT_TRUE(absl::ParseFlag(" 0x22 ", &value, &err));
326 EXPECT_EQ(value, 34);
327
328 // Invalid values.
329 EXPECT_FALSE(absl::ParseFlag("", &value, &err));
330 EXPECT_FALSE(absl::ParseFlag(" ", &value, &err));
331 EXPECT_FALSE(absl::ParseFlag(" ", &value, &err));
332 EXPECT_FALSE(absl::ParseFlag("140000000000", &value, &err));
333 EXPECT_FALSE(absl::ParseFlag("-1", &value, &err));
334 EXPECT_FALSE(absl::ParseFlag("--1", &value, &err));
335 EXPECT_FALSE(absl::ParseFlag("\n", &value, &err));
336 EXPECT_FALSE(absl::ParseFlag("\t", &value, &err));
337 EXPECT_FALSE(absl::ParseFlag("2U", &value, &err));
338 EXPECT_FALSE(absl::ParseFlag("FFF", &value, &err));
339 }
340
341 // --------------------------------------------------------------------
342
TEST(MarshallingTest,TestInt64Parsing)343 TEST(MarshallingTest, TestInt64Parsing) {
344 std::string err;
345 int64_t value;
346
347 // Decimal values.
348 EXPECT_TRUE(absl::ParseFlag("1", &value, &err));
349 EXPECT_EQ(value, 1);
350 EXPECT_TRUE(absl::ParseFlag("0", &value, &err));
351 EXPECT_EQ(value, 0);
352 EXPECT_TRUE(absl::ParseFlag("-1", &value, &err));
353 EXPECT_EQ(value, -1);
354 EXPECT_TRUE(absl::ParseFlag("123", &value, &err));
355 EXPECT_EQ(value, 123);
356 EXPECT_TRUE(absl::ParseFlag("-98765", &value, &err));
357 EXPECT_EQ(value, -98765);
358 EXPECT_TRUE(absl::ParseFlag("+3", &value, &err));
359 EXPECT_EQ(value, 3);
360
361 // Leading zero values.
362 EXPECT_TRUE(absl::ParseFlag("01", &value, &err));
363 EXPECT_EQ(value, 1);
364 EXPECT_TRUE(absl::ParseFlag("001", &value, &err));
365 EXPECT_EQ(value, 1);
366 EXPECT_TRUE(absl::ParseFlag("0000100", &value, &err));
367 EXPECT_EQ(value, 100);
368
369 // Hex values.
370 EXPECT_TRUE(absl::ParseFlag("0x10", &value, &err));
371 EXPECT_EQ(value, 16);
372 EXPECT_TRUE(absl::ParseFlag("0XFFFAAABBBCCCDDD", &value, &err));
373 EXPECT_EQ(value, 1152827684197027293);
374 // TODO(rogeeff): fix below validation
375 EXPECT_FALSE(absl::ParseFlag("-0x7FFFFFFFFFFFFFFE", &value, &err));
376 EXPECT_NE(value, -2);
377 EXPECT_FALSE(absl::ParseFlag("+0x31", &value, &err));
378 EXPECT_NE(value, 49);
379
380 // Whitespace handling
381 EXPECT_TRUE(absl::ParseFlag("10 ", &value, &err));
382 EXPECT_EQ(value, 10);
383 EXPECT_TRUE(absl::ParseFlag(" 11", &value, &err));
384 EXPECT_EQ(value, 11);
385 EXPECT_TRUE(absl::ParseFlag(" 012 ", &value, &err));
386 EXPECT_EQ(value, 12);
387 EXPECT_TRUE(absl::ParseFlag(" 0x7F ", &value, &err));
388 EXPECT_EQ(value, 127);
389
390 // Invalid values.
391 EXPECT_FALSE(absl::ParseFlag("", &value, &err));
392 EXPECT_FALSE(absl::ParseFlag(" ", &value, &err));
393 EXPECT_FALSE(absl::ParseFlag(" ", &value, &err));
394 EXPECT_FALSE(absl::ParseFlag("0xFFFFFFFFFFFFFFFFFF", &value, &err));
395 EXPECT_FALSE(absl::ParseFlag("--1", &value, &err));
396 EXPECT_FALSE(absl::ParseFlag("\n", &value, &err));
397 EXPECT_FALSE(absl::ParseFlag("\t", &value, &err));
398 EXPECT_FALSE(absl::ParseFlag("2U", &value, &err));
399 EXPECT_FALSE(absl::ParseFlag("FFF", &value, &err));
400 }
401
402 // --------------------------------------------------------------------
403
TEST(MarshallingTest,TestUInt64Parsing)404 TEST(MarshallingTest, TestUInt64Parsing) {
405 std::string err;
406 uint64_t value;
407
408 // Decimal values.
409 EXPECT_TRUE(absl::ParseFlag("1", &value, &err));
410 EXPECT_EQ(value, 1);
411 EXPECT_TRUE(absl::ParseFlag("0", &value, &err));
412 EXPECT_EQ(value, 0);
413 EXPECT_TRUE(absl::ParseFlag("123", &value, &err));
414 EXPECT_EQ(value, 123);
415 EXPECT_TRUE(absl::ParseFlag("+13", &value, &err));
416 EXPECT_EQ(value, 13);
417
418 // Leading zero values.
419 EXPECT_TRUE(absl::ParseFlag("01", &value, &err));
420 EXPECT_EQ(value, 1);
421 EXPECT_TRUE(absl::ParseFlag("001", &value, &err));
422 EXPECT_EQ(value, 1);
423 EXPECT_TRUE(absl::ParseFlag("0000300", &value, &err));
424 EXPECT_EQ(value, 300);
425
426 // Hex values.
427 EXPECT_TRUE(absl::ParseFlag("0x10", &value, &err));
428 EXPECT_EQ(value, 16);
429 EXPECT_TRUE(absl::ParseFlag("0XFFFF", &value, &err));
430 EXPECT_EQ(value, 65535);
431 // TODO(rogeeff): fix below validation
432 EXPECT_FALSE(absl::ParseFlag("+0x31", &value, &err));
433 EXPECT_NE(value, 49);
434
435 // Whitespace handling
436 EXPECT_TRUE(absl::ParseFlag("10 ", &value, &err));
437 EXPECT_EQ(value, 10);
438 EXPECT_TRUE(absl::ParseFlag(" 11", &value, &err));
439 EXPECT_EQ(value, 11);
440 EXPECT_TRUE(absl::ParseFlag(" 012 ", &value, &err));
441 EXPECT_EQ(value, 12);
442
443 // Invalid values.
444 EXPECT_FALSE(absl::ParseFlag("", &value, &err));
445 EXPECT_FALSE(absl::ParseFlag(" ", &value, &err));
446 EXPECT_FALSE(absl::ParseFlag(" ", &value, &err));
447 EXPECT_FALSE(absl::ParseFlag("0xFFFFFFFFFFFFFFFFFF", &value, &err));
448 EXPECT_FALSE(absl::ParseFlag("-1", &value, &err));
449 EXPECT_FALSE(absl::ParseFlag("--1", &value, &err));
450 EXPECT_FALSE(absl::ParseFlag("\n", &value, &err));
451 EXPECT_FALSE(absl::ParseFlag("\t", &value, &err));
452 EXPECT_FALSE(absl::ParseFlag("2U", &value, &err));
453 EXPECT_FALSE(absl::ParseFlag("FFF", &value, &err));
454 }
455
456 // --------------------------------------------------------------------
457
TEST(MarshallingTest,TestFloatParsing)458 TEST(MarshallingTest, TestFloatParsing) {
459 std::string err;
460 float value;
461
462 // Ordinary values.
463 EXPECT_TRUE(absl::ParseFlag("1.3", &value, &err));
464 EXPECT_FLOAT_EQ(value, 1.3f);
465 EXPECT_TRUE(absl::ParseFlag("-0.1", &value, &err));
466 EXPECT_DOUBLE_EQ(value, -0.1f);
467 EXPECT_TRUE(absl::ParseFlag("+0.01", &value, &err));
468 EXPECT_DOUBLE_EQ(value, 0.01f);
469
470 // Scientific values.
471 EXPECT_TRUE(absl::ParseFlag("1.2e3", &value, &err));
472 EXPECT_DOUBLE_EQ(value, 1.2e3f);
473 EXPECT_TRUE(absl::ParseFlag("9.8765402e-37", &value, &err));
474 EXPECT_DOUBLE_EQ(value, 9.8765402e-37f);
475 EXPECT_TRUE(absl::ParseFlag("0.11e+3", &value, &err));
476 EXPECT_DOUBLE_EQ(value, 0.11e+3f);
477 EXPECT_TRUE(absl::ParseFlag("1.e-2300", &value, &err));
478 EXPECT_DOUBLE_EQ(value, 0.f);
479 EXPECT_TRUE(absl::ParseFlag("1.e+2300", &value, &err));
480 EXPECT_TRUE(std::isinf(value));
481
482 // Leading zero values.
483 EXPECT_TRUE(absl::ParseFlag("01.6", &value, &err));
484 EXPECT_DOUBLE_EQ(value, 1.6f);
485 EXPECT_TRUE(absl::ParseFlag("000.0001", &value, &err));
486 EXPECT_DOUBLE_EQ(value, 0.0001f);
487
488 // Trailing zero values.
489 EXPECT_TRUE(absl::ParseFlag("-5.1000", &value, &err));
490 EXPECT_DOUBLE_EQ(value, -5.1f);
491
492 // Exceptional values.
493 EXPECT_TRUE(absl::ParseFlag("NaN", &value, &err));
494 EXPECT_TRUE(std::isnan(value));
495 EXPECT_TRUE(absl::ParseFlag("Inf", &value, &err));
496 EXPECT_TRUE(std::isinf(value));
497
498 // Hex values
499 EXPECT_TRUE(absl::ParseFlag("0x10.23p12", &value, &err));
500 EXPECT_DOUBLE_EQ(value, 66096.f);
501 EXPECT_TRUE(absl::ParseFlag("-0xF1.A3p-2", &value, &err));
502 EXPECT_NEAR(value, -60.4092f, 5e-5f);
503 EXPECT_TRUE(absl::ParseFlag("+0x0.0AAp-12", &value, &err));
504 EXPECT_NEAR(value, 1.01328e-05f, 5e-11f);
505 EXPECT_TRUE(absl::ParseFlag("0x.01p1", &value, &err));
506 EXPECT_NEAR(value, 0.0078125f, 5e-8f);
507
508 // Whitespace handling
509 EXPECT_TRUE(absl::ParseFlag("10.1 ", &value, &err));
510 EXPECT_DOUBLE_EQ(value, 10.1f);
511 EXPECT_TRUE(absl::ParseFlag(" 2.34", &value, &err));
512 EXPECT_DOUBLE_EQ(value, 2.34f);
513 EXPECT_TRUE(absl::ParseFlag(" 5.7 ", &value, &err));
514 EXPECT_DOUBLE_EQ(value, 5.7f);
515 EXPECT_TRUE(absl::ParseFlag(" -0xE0.F3p01 ", &value, &err));
516 EXPECT_NEAR(value, -449.8984375f, 5e-8f);
517
518 // Invalid values.
519 EXPECT_FALSE(absl::ParseFlag("", &value, &err));
520 EXPECT_FALSE(absl::ParseFlag(" ", &value, &err));
521 EXPECT_FALSE(absl::ParseFlag(" ", &value, &err));
522 EXPECT_FALSE(absl::ParseFlag("--1", &value, &err));
523 EXPECT_FALSE(absl::ParseFlag("\n", &value, &err));
524 EXPECT_FALSE(absl::ParseFlag("\t", &value, &err));
525 EXPECT_FALSE(absl::ParseFlag("2.3xxx", &value, &err));
526 EXPECT_FALSE(absl::ParseFlag("0x0.1pAA", &value, &err));
527 // TODO(rogeeff): below assertion should fail
528 EXPECT_TRUE(absl::ParseFlag("0x0.1", &value, &err));
529 }
530
531 // --------------------------------------------------------------------
532
TEST(MarshallingTest,TestDoubleParsing)533 TEST(MarshallingTest, TestDoubleParsing) {
534 std::string err;
535 double value;
536
537 // Ordinary values.
538 EXPECT_TRUE(absl::ParseFlag("1.3", &value, &err));
539 EXPECT_DOUBLE_EQ(value, 1.3);
540 EXPECT_TRUE(absl::ParseFlag("-0.1", &value, &err));
541 EXPECT_DOUBLE_EQ(value, -0.1);
542 EXPECT_TRUE(absl::ParseFlag("+0.01", &value, &err));
543 EXPECT_DOUBLE_EQ(value, 0.01);
544
545 // Scientific values.
546 EXPECT_TRUE(absl::ParseFlag("1.2e3", &value, &err));
547 EXPECT_DOUBLE_EQ(value, 1.2e3);
548 EXPECT_TRUE(absl::ParseFlag("9.00000002e-123", &value, &err));
549 EXPECT_DOUBLE_EQ(value, 9.00000002e-123);
550 EXPECT_TRUE(absl::ParseFlag("0.11e+3", &value, &err));
551 EXPECT_DOUBLE_EQ(value, 0.11e+3);
552 EXPECT_TRUE(absl::ParseFlag("1.e-2300", &value, &err));
553 EXPECT_DOUBLE_EQ(value, 0);
554 EXPECT_TRUE(absl::ParseFlag("1.e+2300", &value, &err));
555 EXPECT_TRUE(std::isinf(value));
556
557 // Leading zero values.
558 EXPECT_TRUE(absl::ParseFlag("01.6", &value, &err));
559 EXPECT_DOUBLE_EQ(value, 1.6);
560 EXPECT_TRUE(absl::ParseFlag("000.0001", &value, &err));
561 EXPECT_DOUBLE_EQ(value, 0.0001);
562
563 // Trailing zero values.
564 EXPECT_TRUE(absl::ParseFlag("-5.1000", &value, &err));
565 EXPECT_DOUBLE_EQ(value, -5.1);
566
567 // Exceptional values.
568 EXPECT_TRUE(absl::ParseFlag("NaN", &value, &err));
569 EXPECT_TRUE(std::isnan(value));
570 EXPECT_TRUE(absl::ParseFlag("nan", &value, &err));
571 EXPECT_TRUE(std::isnan(value));
572 EXPECT_TRUE(absl::ParseFlag("Inf", &value, &err));
573 EXPECT_TRUE(std::isinf(value));
574 EXPECT_TRUE(absl::ParseFlag("inf", &value, &err));
575 EXPECT_TRUE(std::isinf(value));
576
577 // Hex values
578 EXPECT_TRUE(absl::ParseFlag("0x10.23p12", &value, &err));
579 EXPECT_DOUBLE_EQ(value, 66096);
580 EXPECT_TRUE(absl::ParseFlag("-0xF1.A3p-2", &value, &err));
581 EXPECT_NEAR(value, -60.4092, 5e-5);
582 EXPECT_TRUE(absl::ParseFlag("+0x0.0AAp-12", &value, &err));
583 EXPECT_NEAR(value, 1.01328e-05, 5e-11);
584 EXPECT_TRUE(absl::ParseFlag("0x.01p1", &value, &err));
585 EXPECT_NEAR(value, 0.0078125, 5e-8);
586
587 // Whitespace handling
588 EXPECT_TRUE(absl::ParseFlag("10.1 ", &value, &err));
589 EXPECT_DOUBLE_EQ(value, 10.1);
590 EXPECT_TRUE(absl::ParseFlag(" 2.34", &value, &err));
591 EXPECT_DOUBLE_EQ(value, 2.34);
592 EXPECT_TRUE(absl::ParseFlag(" 5.7 ", &value, &err));
593 EXPECT_DOUBLE_EQ(value, 5.7);
594 EXPECT_TRUE(absl::ParseFlag(" -0xE0.F3p01 ", &value, &err));
595 EXPECT_NEAR(value, -449.8984375, 5e-8);
596
597 // Invalid values.
598 EXPECT_FALSE(absl::ParseFlag("", &value, &err));
599 EXPECT_FALSE(absl::ParseFlag(" ", &value, &err));
600 EXPECT_FALSE(absl::ParseFlag(" ", &value, &err));
601 EXPECT_FALSE(absl::ParseFlag("--1", &value, &err));
602 EXPECT_FALSE(absl::ParseFlag("\n", &value, &err));
603 EXPECT_FALSE(absl::ParseFlag("\t", &value, &err));
604 EXPECT_FALSE(absl::ParseFlag("2.3xxx", &value, &err));
605 EXPECT_FALSE(absl::ParseFlag("0x0.1pAA", &value, &err));
606 // TODO(rogeeff): below assertion should fail
607 EXPECT_TRUE(absl::ParseFlag("0x0.1", &value, &err));
608 }
609
610 // --------------------------------------------------------------------
611
TEST(MarshallingTest,TestStringParsing)612 TEST(MarshallingTest, TestStringParsing) {
613 std::string err;
614 std::string value;
615
616 EXPECT_TRUE(absl::ParseFlag("", &value, &err));
617 EXPECT_EQ(value, "");
618 EXPECT_TRUE(absl::ParseFlag(" ", &value, &err));
619 EXPECT_EQ(value, " ");
620 EXPECT_TRUE(absl::ParseFlag(" ", &value, &err));
621 EXPECT_EQ(value, " ");
622 EXPECT_TRUE(absl::ParseFlag("\n", &value, &err));
623 EXPECT_EQ(value, "\n");
624 EXPECT_TRUE(absl::ParseFlag("\t", &value, &err));
625 EXPECT_EQ(value, "\t");
626 EXPECT_TRUE(absl::ParseFlag("asdfg", &value, &err));
627 EXPECT_EQ(value, "asdfg");
628 EXPECT_TRUE(absl::ParseFlag("asdf ghjk", &value, &err));
629 EXPECT_EQ(value, "asdf ghjk");
630 EXPECT_TRUE(absl::ParseFlag("a\nb\nc", &value, &err));
631 EXPECT_EQ(value, "a\nb\nc");
632 EXPECT_TRUE(absl::ParseFlag("asd\0fgh", &value, &err));
633 EXPECT_EQ(value, "asd");
634 EXPECT_TRUE(absl::ParseFlag("\\\\", &value, &err));
635 EXPECT_EQ(value, "\\\\");
636 }
637
638 // --------------------------------------------------------------------
639
TEST(MarshallingTest,TestVectorOfStringParsing)640 TEST(MarshallingTest, TestVectorOfStringParsing) {
641 std::string err;
642 std::vector<std::string> value;
643
644 EXPECT_TRUE(absl::ParseFlag("", &value, &err));
645 EXPECT_EQ(value, std::vector<std::string>{});
646 EXPECT_TRUE(absl::ParseFlag("1", &value, &err));
647 EXPECT_EQ(value, std::vector<std::string>({"1"}));
648 EXPECT_TRUE(absl::ParseFlag("a,b", &value, &err));
649 EXPECT_EQ(value, std::vector<std::string>({"a", "b"}));
650 EXPECT_TRUE(absl::ParseFlag("a,b,c,", &value, &err));
651 EXPECT_EQ(value, std::vector<std::string>({"a", "b", "c", ""}));
652 EXPECT_TRUE(absl::ParseFlag("a,,", &value, &err));
653 EXPECT_EQ(value, std::vector<std::string>({"a", "", ""}));
654 EXPECT_TRUE(absl::ParseFlag(",", &value, &err));
655 EXPECT_EQ(value, std::vector<std::string>({"", ""}));
656 EXPECT_TRUE(absl::ParseFlag("a, b,c ", &value, &err));
657 EXPECT_EQ(value, std::vector<std::string>({"a", " b", "c "}));
658 }
659
660 // --------------------------------------------------------------------
661
TEST(MarshallingTest,TestOptionalBoolParsing)662 TEST(MarshallingTest, TestOptionalBoolParsing) {
663 std::string err;
664 absl::optional<bool> value;
665
666 EXPECT_TRUE(absl::ParseFlag("", &value, &err));
667 EXPECT_FALSE(value.has_value());
668
669 EXPECT_TRUE(absl::ParseFlag("true", &value, &err));
670 EXPECT_TRUE(value.has_value());
671 EXPECT_TRUE(*value);
672
673 EXPECT_TRUE(absl::ParseFlag("false", &value, &err));
674 EXPECT_TRUE(value.has_value());
675 EXPECT_FALSE(*value);
676
677 EXPECT_FALSE(absl::ParseFlag("nullopt", &value, &err));
678 }
679
680 // --------------------------------------------------------------------
681
TEST(MarshallingTest,TestOptionalIntParsing)682 TEST(MarshallingTest, TestOptionalIntParsing) {
683 std::string err;
684 absl::optional<int> value;
685
686 EXPECT_TRUE(absl::ParseFlag("", &value, &err));
687 EXPECT_FALSE(value.has_value());
688
689 EXPECT_TRUE(absl::ParseFlag("10", &value, &err));
690 EXPECT_TRUE(value.has_value());
691 EXPECT_EQ(*value, 10);
692
693 EXPECT_TRUE(absl::ParseFlag("0x1F", &value, &err));
694 EXPECT_TRUE(value.has_value());
695 EXPECT_EQ(*value, 31);
696
697 EXPECT_FALSE(absl::ParseFlag("nullopt", &value, &err));
698 }
699
700 // --------------------------------------------------------------------
701
TEST(MarshallingTest,TestOptionalDoubleParsing)702 TEST(MarshallingTest, TestOptionalDoubleParsing) {
703 std::string err;
704 absl::optional<double> value;
705
706 EXPECT_TRUE(absl::ParseFlag("", &value, &err));
707 EXPECT_FALSE(value.has_value());
708
709 EXPECT_TRUE(absl::ParseFlag("1.11", &value, &err));
710 EXPECT_TRUE(value.has_value());
711 EXPECT_EQ(*value, 1.11);
712
713 EXPECT_TRUE(absl::ParseFlag("-0.12", &value, &err));
714 EXPECT_TRUE(value.has_value());
715 EXPECT_EQ(*value, -0.12);
716
717 EXPECT_FALSE(absl::ParseFlag("nullopt", &value, &err));
718 }
719
720 // --------------------------------------------------------------------
721
TEST(MarshallingTest,TestOptionalStringParsing)722 TEST(MarshallingTest, TestOptionalStringParsing) {
723 std::string err;
724 absl::optional<std::string> value;
725
726 EXPECT_TRUE(absl::ParseFlag("", &value, &err));
727 EXPECT_FALSE(value.has_value());
728
729 EXPECT_TRUE(absl::ParseFlag(" ", &value, &err));
730 EXPECT_TRUE(value.has_value());
731 EXPECT_EQ(*value, " ");
732
733 EXPECT_TRUE(absl::ParseFlag("aqswde", &value, &err));
734 EXPECT_TRUE(value.has_value());
735 EXPECT_EQ(*value, "aqswde");
736
737 EXPECT_TRUE(absl::ParseFlag("nullopt", &value, &err));
738 EXPECT_TRUE(value.has_value());
739 EXPECT_EQ(*value, "nullopt");
740 }
741
742 // --------------------------------------------------------------------
743
TEST(MarshallingTest,TestBoolUnparsing)744 TEST(MarshallingTest, TestBoolUnparsing) {
745 EXPECT_EQ(absl::UnparseFlag(true), "true");
746 EXPECT_EQ(absl::UnparseFlag(false), "false");
747 }
748
749 // --------------------------------------------------------------------
750
TEST(MarshallingTest,TestInt16Unparsing)751 TEST(MarshallingTest, TestInt16Unparsing) {
752 int16_t value;
753
754 value = 1;
755 EXPECT_EQ(absl::UnparseFlag(value), "1");
756 value = 0;
757 EXPECT_EQ(absl::UnparseFlag(value), "0");
758 value = -1;
759 EXPECT_EQ(absl::UnparseFlag(value), "-1");
760 value = 9876;
761 EXPECT_EQ(absl::UnparseFlag(value), "9876");
762 value = -987;
763 EXPECT_EQ(absl::UnparseFlag(value), "-987");
764 }
765
766 // --------------------------------------------------------------------
767
TEST(MarshallingTest,TestUint16Unparsing)768 TEST(MarshallingTest, TestUint16Unparsing) {
769 uint16_t value;
770
771 value = 1;
772 EXPECT_EQ(absl::UnparseFlag(value), "1");
773 value = 0;
774 EXPECT_EQ(absl::UnparseFlag(value), "0");
775 value = 19876;
776 EXPECT_EQ(absl::UnparseFlag(value), "19876");
777 }
778
779 // --------------------------------------------------------------------
780
TEST(MarshallingTest,TestInt32Unparsing)781 TEST(MarshallingTest, TestInt32Unparsing) {
782 int32_t value;
783
784 value = 1;
785 EXPECT_EQ(absl::UnparseFlag(value), "1");
786 value = 0;
787 EXPECT_EQ(absl::UnparseFlag(value), "0");
788 value = -1;
789 EXPECT_EQ(absl::UnparseFlag(value), "-1");
790 value = 12345;
791 EXPECT_EQ(absl::UnparseFlag(value), "12345");
792 value = -987;
793 EXPECT_EQ(absl::UnparseFlag(value), "-987");
794 }
795
796 // --------------------------------------------------------------------
797
TEST(MarshallingTest,TestUint32Unparsing)798 TEST(MarshallingTest, TestUint32Unparsing) {
799 uint32_t value;
800
801 value = 1;
802 EXPECT_EQ(absl::UnparseFlag(value), "1");
803 value = 0;
804 EXPECT_EQ(absl::UnparseFlag(value), "0");
805 value = 1234500;
806 EXPECT_EQ(absl::UnparseFlag(value), "1234500");
807 }
808
809 // --------------------------------------------------------------------
810
TEST(MarshallingTest,TestInt64Unparsing)811 TEST(MarshallingTest, TestInt64Unparsing) {
812 int64_t value;
813
814 value = 1;
815 EXPECT_EQ(absl::UnparseFlag(value), "1");
816 value = 0;
817 EXPECT_EQ(absl::UnparseFlag(value), "0");
818 value = -1;
819 EXPECT_EQ(absl::UnparseFlag(value), "-1");
820 value = 123456789L;
821 EXPECT_EQ(absl::UnparseFlag(value), "123456789");
822 value = -987654321L;
823 EXPECT_EQ(absl::UnparseFlag(value), "-987654321");
824 value = 0x7FFFFFFFFFFFFFFF;
825 EXPECT_EQ(absl::UnparseFlag(value), "9223372036854775807");
826 value = 0xFFFFFFFFFFFFFFFF;
827 EXPECT_EQ(absl::UnparseFlag(value), "-1");
828 }
829
830 // --------------------------------------------------------------------
831
TEST(MarshallingTest,TestUint64Unparsing)832 TEST(MarshallingTest, TestUint64Unparsing) {
833 uint64_t value;
834
835 value = 1;
836 EXPECT_EQ(absl::UnparseFlag(value), "1");
837 value = 0;
838 EXPECT_EQ(absl::UnparseFlag(value), "0");
839 value = 123456789L;
840 EXPECT_EQ(absl::UnparseFlag(value), "123456789");
841 value = 0xFFFFFFFFFFFFFFFF;
842 EXPECT_EQ(absl::UnparseFlag(value), "18446744073709551615");
843 }
844
845 // --------------------------------------------------------------------
846
TEST(MarshallingTest,TestFloatUnparsing)847 TEST(MarshallingTest, TestFloatUnparsing) {
848 float value;
849
850 value = 1.1f;
851 EXPECT_EQ(absl::UnparseFlag(value), "1.1");
852 value = 0.01f;
853 EXPECT_EQ(absl::UnparseFlag(value), "0.01");
854 value = 1.23e-2f;
855 EXPECT_EQ(absl::UnparseFlag(value), "0.0123");
856 value = -0.71f;
857 EXPECT_EQ(absl::UnparseFlag(value), "-0.71");
858 }
859
860 // --------------------------------------------------------------------
861
TEST(MarshallingTest,TestDoubleUnparsing)862 TEST(MarshallingTest, TestDoubleUnparsing) {
863 double value;
864
865 value = 1.1;
866 EXPECT_EQ(absl::UnparseFlag(value), "1.1");
867 value = 0.01;
868 EXPECT_EQ(absl::UnparseFlag(value), "0.01");
869 value = 1.23e-2;
870 EXPECT_EQ(absl::UnparseFlag(value), "0.0123");
871 value = -0.71;
872 EXPECT_EQ(absl::UnparseFlag(value), "-0.71");
873 value = -0;
874 EXPECT_EQ(absl::UnparseFlag(value), "0");
875 value = std::nan("");
876 EXPECT_EQ(absl::UnparseFlag(value), "nan");
877 value = std::numeric_limits<double>::infinity();
878 EXPECT_EQ(absl::UnparseFlag(value), "inf");
879 }
880
881 // --------------------------------------------------------------------
882
TEST(MarshallingTest,TestStringUnparsing)883 TEST(MarshallingTest, TestStringUnparsing) {
884 EXPECT_EQ(absl::UnparseFlag(""), "");
885 EXPECT_EQ(absl::UnparseFlag(" "), " ");
886 EXPECT_EQ(absl::UnparseFlag("qwerty"), "qwerty");
887 EXPECT_EQ(absl::UnparseFlag("ASDFGH"), "ASDFGH");
888 EXPECT_EQ(absl::UnparseFlag("\n\t "), "\n\t ");
889 }
890
891 // --------------------------------------------------------------------
892
TEST(MarshallingTest,TestOptionalBoolUnparsing)893 TEST(MarshallingTest, TestOptionalBoolUnparsing) {
894 absl::optional<bool> value;
895
896 EXPECT_EQ(absl::UnparseFlag(value), "");
897 value = true;
898 EXPECT_EQ(absl::UnparseFlag(value), "true");
899 value = false;
900 EXPECT_EQ(absl::UnparseFlag(value), "false");
901 value = absl::nullopt;
902 EXPECT_EQ(absl::UnparseFlag(value), "");
903 }
904
905 // --------------------------------------------------------------------
906
TEST(MarshallingTest,TestOptionalIntUnparsing)907 TEST(MarshallingTest, TestOptionalIntUnparsing) {
908 absl::optional<int> value;
909
910 EXPECT_EQ(absl::UnparseFlag(value), "");
911 value = 0;
912 EXPECT_EQ(absl::UnparseFlag(value), "0");
913 value = -12;
914 EXPECT_EQ(absl::UnparseFlag(value), "-12");
915 value = absl::nullopt;
916 EXPECT_EQ(absl::UnparseFlag(value), "");
917 }
918
919 // --------------------------------------------------------------------
920
TEST(MarshallingTest,TestOptionalDoubleUnparsing)921 TEST(MarshallingTest, TestOptionalDoubleUnparsing) {
922 absl::optional<double> value;
923
924 EXPECT_EQ(absl::UnparseFlag(value), "");
925 value = 1.;
926 EXPECT_EQ(absl::UnparseFlag(value), "1");
927 value = -1.23;
928 EXPECT_EQ(absl::UnparseFlag(value), "-1.23");
929 value = absl::nullopt;
930 EXPECT_EQ(absl::UnparseFlag(value), "");
931 }
932
933 // --------------------------------------------------------------------
934
TEST(MarshallingTest,TestOptionalStringUnparsing)935 TEST(MarshallingTest, TestOptionalStringUnparsing) {
936 absl::optional<std::string> strvalue;
937 EXPECT_EQ(absl::UnparseFlag(strvalue), "");
938
939 strvalue = "asdfg";
940 EXPECT_EQ(absl::UnparseFlag(strvalue), "asdfg");
941
942 strvalue = " ";
943 EXPECT_EQ(absl::UnparseFlag(strvalue), " ");
944
945 strvalue = ""; // It is UB to set an optional string flag to ""
946 EXPECT_EQ(absl::UnparseFlag(strvalue), "");
947 }
948
949 // --------------------------------------------------------------------
950
951 #if defined(ABSL_HAVE_STD_OPTIONAL) && !defined(ABSL_USES_STD_OPTIONAL)
952
TEST(MarshallingTest,TestStdOptionalUnparsing)953 TEST(MarshallingTest, TestStdOptionalUnparsing) {
954 std::optional<std::string> strvalue;
955 EXPECT_EQ(absl::UnparseFlag(strvalue), "");
956
957 strvalue = "asdfg";
958 EXPECT_EQ(absl::UnparseFlag(strvalue), "asdfg");
959
960 strvalue = " ";
961 EXPECT_EQ(absl::UnparseFlag(strvalue), " ");
962
963 strvalue = ""; // It is UB to set an optional string flag to ""
964 EXPECT_EQ(absl::UnparseFlag(strvalue), "");
965
966 std::optional<int> intvalue;
967 EXPECT_EQ(absl::UnparseFlag(intvalue), "");
968
969 intvalue = 10;
970 EXPECT_EQ(absl::UnparseFlag(intvalue), "10");
971 }
972
973 // --------------------------------------------------------------------
974
975 #endif
976
977 template <typename T>
TestRoundtrip(T v)978 void TestRoundtrip(T v) {
979 T new_v;
980 std::string err;
981 EXPECT_TRUE(absl::ParseFlag(absl::UnparseFlag(v), &new_v, &err));
982 EXPECT_EQ(new_v, v);
983 }
984
TEST(MarshallingTest,TestFloatRoundTrip)985 TEST(MarshallingTest, TestFloatRoundTrip) {
986 TestRoundtrip(0.1f);
987 TestRoundtrip(0.12f);
988 TestRoundtrip(0.123f);
989 TestRoundtrip(0.1234f);
990 TestRoundtrip(0.12345f);
991 TestRoundtrip(0.123456f);
992 TestRoundtrip(0.1234567f);
993 TestRoundtrip(0.12345678f);
994
995 TestRoundtrip(0.1e20f);
996 TestRoundtrip(0.12e20f);
997 TestRoundtrip(0.123e20f);
998 TestRoundtrip(0.1234e20f);
999 TestRoundtrip(0.12345e20f);
1000 TestRoundtrip(0.123456e20f);
1001 TestRoundtrip(0.1234567e20f);
1002 TestRoundtrip(0.12345678e20f);
1003
1004 TestRoundtrip(0.1e-20f);
1005 TestRoundtrip(0.12e-20f);
1006 TestRoundtrip(0.123e-20f);
1007 TestRoundtrip(0.1234e-20f);
1008 TestRoundtrip(0.12345e-20f);
1009 TestRoundtrip(0.123456e-20f);
1010 TestRoundtrip(0.1234567e-20f);
1011 TestRoundtrip(0.12345678e-20f);
1012 }
1013
TEST(MarshallingTest,TestDoubleRoundTrip)1014 TEST(MarshallingTest, TestDoubleRoundTrip) {
1015 TestRoundtrip(0.1);
1016 TestRoundtrip(0.12);
1017 TestRoundtrip(0.123);
1018 TestRoundtrip(0.1234);
1019 TestRoundtrip(0.12345);
1020 TestRoundtrip(0.123456);
1021 TestRoundtrip(0.1234567);
1022 TestRoundtrip(0.12345678);
1023 TestRoundtrip(0.123456789);
1024 TestRoundtrip(0.1234567891);
1025 TestRoundtrip(0.12345678912);
1026 TestRoundtrip(0.123456789123);
1027 TestRoundtrip(0.1234567891234);
1028 TestRoundtrip(0.12345678912345);
1029 TestRoundtrip(0.123456789123456);
1030 TestRoundtrip(0.1234567891234567);
1031 TestRoundtrip(0.12345678912345678);
1032
1033 TestRoundtrip(0.1e50);
1034 TestRoundtrip(0.12e50);
1035 TestRoundtrip(0.123e50);
1036 TestRoundtrip(0.1234e50);
1037 TestRoundtrip(0.12345e50);
1038 TestRoundtrip(0.123456e50);
1039 TestRoundtrip(0.1234567e50);
1040 TestRoundtrip(0.12345678e50);
1041 TestRoundtrip(0.123456789e50);
1042 TestRoundtrip(0.1234567891e50);
1043 TestRoundtrip(0.12345678912e50);
1044 TestRoundtrip(0.123456789123e50);
1045 TestRoundtrip(0.1234567891234e50);
1046 TestRoundtrip(0.12345678912345e50);
1047 TestRoundtrip(0.123456789123456e50);
1048 TestRoundtrip(0.1234567891234567e50);
1049 TestRoundtrip(0.12345678912345678e50);
1050
1051 TestRoundtrip(0.1e-50);
1052 TestRoundtrip(0.12e-50);
1053 TestRoundtrip(0.123e-50);
1054 TestRoundtrip(0.1234e-50);
1055 TestRoundtrip(0.12345e-50);
1056 TestRoundtrip(0.123456e-50);
1057 TestRoundtrip(0.1234567e-50);
1058 TestRoundtrip(0.12345678e-50);
1059 TestRoundtrip(0.123456789e-50);
1060 TestRoundtrip(0.1234567891e-50);
1061 TestRoundtrip(0.12345678912e-50);
1062 TestRoundtrip(0.123456789123e-50);
1063 TestRoundtrip(0.1234567891234e-50);
1064 TestRoundtrip(0.12345678912345e-50);
1065 TestRoundtrip(0.123456789123456e-50);
1066 TestRoundtrip(0.1234567891234567e-50);
1067 TestRoundtrip(0.12345678912345678e-50);
1068 }
1069
1070 } // namespace
1071