marshalling_test.cc
Go to the documentation of this file.
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 <cmath>
19 
20 #include "gtest/gtest.h"
21 
22 namespace {
23 
24 TEST(MarshallingTest, TestBoolParsing) {
25  std::string err;
26  bool value;
27 
28  // True values.
29  EXPECT_TRUE(absl::ParseFlag("True", &value, &err));
30  EXPECT_TRUE(value);
31  EXPECT_TRUE(absl::ParseFlag("true", &value, &err));
32  EXPECT_TRUE(value);
33  EXPECT_TRUE(absl::ParseFlag("TRUE", &value, &err));
34  EXPECT_TRUE(value);
35 
36  EXPECT_TRUE(absl::ParseFlag("Yes", &value, &err));
37  EXPECT_TRUE(value);
38  EXPECT_TRUE(absl::ParseFlag("yes", &value, &err));
39  EXPECT_TRUE(value);
40  EXPECT_TRUE(absl::ParseFlag("YES", &value, &err));
41  EXPECT_TRUE(value);
42 
43  EXPECT_TRUE(absl::ParseFlag("t", &value, &err));
44  EXPECT_TRUE(value);
45  EXPECT_TRUE(absl::ParseFlag("T", &value, &err));
46  EXPECT_TRUE(value);
47 
48  EXPECT_TRUE(absl::ParseFlag("y", &value, &err));
49  EXPECT_TRUE(value);
50  EXPECT_TRUE(absl::ParseFlag("Y", &value, &err));
51  EXPECT_TRUE(value);
52 
53  EXPECT_TRUE(absl::ParseFlag("1", &value, &err));
54  EXPECT_TRUE(value);
55 
56  // False values.
57  EXPECT_TRUE(absl::ParseFlag("False", &value, &err));
58  EXPECT_FALSE(value);
59  EXPECT_TRUE(absl::ParseFlag("false", &value, &err));
60  EXPECT_FALSE(value);
61  EXPECT_TRUE(absl::ParseFlag("FALSE", &value, &err));
62  EXPECT_FALSE(value);
63 
64  EXPECT_TRUE(absl::ParseFlag("No", &value, &err));
65  EXPECT_FALSE(value);
66  EXPECT_TRUE(absl::ParseFlag("no", &value, &err));
67  EXPECT_FALSE(value);
68  EXPECT_TRUE(absl::ParseFlag("NO", &value, &err));
69  EXPECT_FALSE(value);
70 
71  EXPECT_TRUE(absl::ParseFlag("f", &value, &err));
72  EXPECT_FALSE(value);
73  EXPECT_TRUE(absl::ParseFlag("F", &value, &err));
74  EXPECT_FALSE(value);
75 
76  EXPECT_TRUE(absl::ParseFlag("n", &value, &err));
77  EXPECT_FALSE(value);
78  EXPECT_TRUE(absl::ParseFlag("N", &value, &err));
79  EXPECT_FALSE(value);
80 
81  EXPECT_TRUE(absl::ParseFlag("0", &value, &err));
82  EXPECT_FALSE(value);
83 
84  // Whitespace handling.
85  EXPECT_TRUE(absl::ParseFlag(" true", &value, &err));
86  EXPECT_TRUE(value);
87  EXPECT_TRUE(absl::ParseFlag("true ", &value, &err));
88  EXPECT_TRUE(value);
89  EXPECT_TRUE(absl::ParseFlag(" true ", &value, &err));
90  EXPECT_TRUE(value);
91 
92  // Invalid input.
93  EXPECT_FALSE(absl::ParseFlag("", &value, &err));
94  EXPECT_FALSE(absl::ParseFlag(" ", &value, &err));
95  EXPECT_FALSE(absl::ParseFlag("\n", &value, &err));
96  EXPECT_FALSE(absl::ParseFlag("\t", &value, &err));
97  EXPECT_FALSE(absl::ParseFlag("2", &value, &err));
98  EXPECT_FALSE(absl::ParseFlag("11", &value, &err));
99  EXPECT_FALSE(absl::ParseFlag("tt", &value, &err));
100 }
101 
102 // --------------------------------------------------------------------
103 
104 TEST(MarshallingTest, TestInt16Parsing) {
105  std::string err;
106  int16_t value;
107 
108  // Decimal values.
109  EXPECT_TRUE(absl::ParseFlag("1", &value, &err));
110  EXPECT_EQ(value, 1);
111  EXPECT_TRUE(absl::ParseFlag("0", &value, &err));
112  EXPECT_EQ(value, 0);
113  EXPECT_TRUE(absl::ParseFlag("-1", &value, &err));
114  EXPECT_EQ(value, -1);
115  EXPECT_TRUE(absl::ParseFlag("123", &value, &err));
116  EXPECT_EQ(value, 123);
117  EXPECT_TRUE(absl::ParseFlag("-18765", &value, &err));
118  EXPECT_EQ(value, -18765);
119  EXPECT_TRUE(absl::ParseFlag("+3", &value, &err));
120  EXPECT_EQ(value, 3);
121 
122  // Leading zero values.
123  EXPECT_TRUE(absl::ParseFlag("01", &value, &err));
124  EXPECT_EQ(value, 1);
125  EXPECT_TRUE(absl::ParseFlag("-001", &value, &err));
126  EXPECT_EQ(value, -1);
127  EXPECT_TRUE(absl::ParseFlag("0000100", &value, &err));
128  EXPECT_EQ(value, 100);
129 
130  // Hex values.
131  EXPECT_TRUE(absl::ParseFlag("0x10", &value, &err));
132  EXPECT_EQ(value, 16);
133  EXPECT_TRUE(absl::ParseFlag("0X234", &value, &err));
134  EXPECT_EQ(value, 564);
135  // TODO(rogeeff): fix below validations
136  EXPECT_FALSE(absl::ParseFlag("-0x7FFD", &value, &err));
137  EXPECT_NE(value, -3);
138  EXPECT_FALSE(absl::ParseFlag("+0x31", &value, &err));
139  EXPECT_NE(value, 49);
140 
141  // Whitespace handling
142  EXPECT_TRUE(absl::ParseFlag("10 ", &value, &err));
143  EXPECT_EQ(value, 10);
144  EXPECT_TRUE(absl::ParseFlag(" 11", &value, &err));
145  EXPECT_EQ(value, 11);
146  EXPECT_TRUE(absl::ParseFlag(" 012 ", &value, &err));
147  EXPECT_EQ(value, 12);
148  EXPECT_TRUE(absl::ParseFlag(" 0x22 ", &value, &err));
149  EXPECT_EQ(value, 34);
150 
151  // Invalid values.
152  EXPECT_FALSE(absl::ParseFlag("", &value, &err));
153  EXPECT_FALSE(absl::ParseFlag(" ", &value, &err));
154  EXPECT_FALSE(absl::ParseFlag(" ", &value, &err));
155  EXPECT_FALSE(absl::ParseFlag("40000", &value, &err));
156  EXPECT_FALSE(absl::ParseFlag("--1", &value, &err));
157  EXPECT_FALSE(absl::ParseFlag("\n", &value, &err));
158  EXPECT_FALSE(absl::ParseFlag("\t", &value, &err));
159  EXPECT_FALSE(absl::ParseFlag("2U", &value, &err));
160  EXPECT_FALSE(absl::ParseFlag("FFF", &value, &err));
161 }
162 
163 // --------------------------------------------------------------------
164 
165 TEST(MarshallingTest, TestUint16Parsing) {
166  std::string err;
167  uint16_t value;
168 
169  // Decimal values.
170  EXPECT_TRUE(absl::ParseFlag("1", &value, &err));
171  EXPECT_EQ(value, 1);
172  EXPECT_TRUE(absl::ParseFlag("0", &value, &err));
173  EXPECT_EQ(value, 0);
174  EXPECT_TRUE(absl::ParseFlag("123", &value, &err));
175  EXPECT_EQ(value, 123);
176  EXPECT_TRUE(absl::ParseFlag("+3", &value, &err));
177  EXPECT_EQ(value, 3);
178 
179  // Leading zero values.
180  EXPECT_TRUE(absl::ParseFlag("01", &value, &err));
181  EXPECT_EQ(value, 1);
182  EXPECT_TRUE(absl::ParseFlag("001", &value, &err));
183  EXPECT_EQ(value, 1);
184  EXPECT_TRUE(absl::ParseFlag("0000100", &value, &err));
185  EXPECT_EQ(value, 100);
186 
187  // Hex values.
188  EXPECT_TRUE(absl::ParseFlag("0x10", &value, &err));
189  EXPECT_EQ(value, 16);
190  EXPECT_TRUE(absl::ParseFlag("0X234", &value, &err));
191  EXPECT_EQ(value, 564);
192  // TODO(rogeeff): fix below validations
193  EXPECT_FALSE(absl::ParseFlag("+0x31", &value, &err));
194  EXPECT_NE(value, 49);
195 
196  // Whitespace handling
197  EXPECT_TRUE(absl::ParseFlag("10 ", &value, &err));
198  EXPECT_EQ(value, 10);
199  EXPECT_TRUE(absl::ParseFlag(" 11", &value, &err));
200  EXPECT_EQ(value, 11);
201  EXPECT_TRUE(absl::ParseFlag(" 012 ", &value, &err));
202  EXPECT_EQ(value, 12);
203  EXPECT_TRUE(absl::ParseFlag(" 0x22 ", &value, &err));
204  EXPECT_EQ(value, 34);
205 
206  // Invalid values.
207  EXPECT_FALSE(absl::ParseFlag("", &value, &err));
208  EXPECT_FALSE(absl::ParseFlag(" ", &value, &err));
209  EXPECT_FALSE(absl::ParseFlag(" ", &value, &err));
210  EXPECT_FALSE(absl::ParseFlag("70000", &value, &err));
211  EXPECT_FALSE(absl::ParseFlag("-1", &value, &err));
212  EXPECT_FALSE(absl::ParseFlag("--1", &value, &err));
213  EXPECT_FALSE(absl::ParseFlag("\n", &value, &err));
214  EXPECT_FALSE(absl::ParseFlag("\t", &value, &err));
215  EXPECT_FALSE(absl::ParseFlag("2U", &value, &err));
216  EXPECT_FALSE(absl::ParseFlag("FFF", &value, &err));
217 }
218 
219 // --------------------------------------------------------------------
220 
221 TEST(MarshallingTest, TestInt32Parsing) {
222  std::string err;
223  int32_t value;
224 
225  // Decimal values.
226  EXPECT_TRUE(absl::ParseFlag("1", &value, &err));
227  EXPECT_EQ(value, 1);
228  EXPECT_TRUE(absl::ParseFlag("0", &value, &err));
229  EXPECT_EQ(value, 0);
230  EXPECT_TRUE(absl::ParseFlag("-1", &value, &err));
231  EXPECT_EQ(value, -1);
232  EXPECT_TRUE(absl::ParseFlag("123", &value, &err));
233  EXPECT_EQ(value, 123);
234  EXPECT_TRUE(absl::ParseFlag("-98765", &value, &err));
235  EXPECT_EQ(value, -98765);
236  EXPECT_TRUE(absl::ParseFlag("+3", &value, &err));
237  EXPECT_EQ(value, 3);
238 
239  // Leading zero values.
240  EXPECT_TRUE(absl::ParseFlag("01", &value, &err));
241  EXPECT_EQ(value, 1);
242  EXPECT_TRUE(absl::ParseFlag("-001", &value, &err));
243  EXPECT_EQ(value, -1);
244  EXPECT_TRUE(absl::ParseFlag("0000100", &value, &err));
245  EXPECT_EQ(value, 100);
246 
247  // Hex values.
248  EXPECT_TRUE(absl::ParseFlag("0x10", &value, &err));
249  EXPECT_EQ(value, 16);
250  EXPECT_TRUE(absl::ParseFlag("0X234", &value, &err));
251  EXPECT_EQ(value, 564);
252  // TODO(rogeeff): fix below validations
253  EXPECT_FALSE(absl::ParseFlag("-0x7FFFFFFD", &value, &err));
254  EXPECT_NE(value, -3);
255  EXPECT_FALSE(absl::ParseFlag("+0x31", &value, &err));
256  EXPECT_NE(value, 49);
257 
258  // Whitespace handling
259  EXPECT_TRUE(absl::ParseFlag("10 ", &value, &err));
260  EXPECT_EQ(value, 10);
261  EXPECT_TRUE(absl::ParseFlag(" 11", &value, &err));
262  EXPECT_EQ(value, 11);
263  EXPECT_TRUE(absl::ParseFlag(" 012 ", &value, &err));
264  EXPECT_EQ(value, 12);
265  EXPECT_TRUE(absl::ParseFlag(" 0x22 ", &value, &err));
266  EXPECT_EQ(value, 34);
267 
268  // Invalid values.
269  EXPECT_FALSE(absl::ParseFlag("", &value, &err));
270  EXPECT_FALSE(absl::ParseFlag(" ", &value, &err));
271  EXPECT_FALSE(absl::ParseFlag(" ", &value, &err));
272  EXPECT_FALSE(absl::ParseFlag("70000000000", &value, &err));
273  EXPECT_FALSE(absl::ParseFlag("--1", &value, &err));
274  EXPECT_FALSE(absl::ParseFlag("\n", &value, &err));
275  EXPECT_FALSE(absl::ParseFlag("\t", &value, &err));
276  EXPECT_FALSE(absl::ParseFlag("2U", &value, &err));
277  EXPECT_FALSE(absl::ParseFlag("FFF", &value, &err));
278 }
279 
280 // --------------------------------------------------------------------
281 
282 TEST(MarshallingTest, TestUint32Parsing) {
283  std::string err;
284  uint32_t value;
285 
286  // Decimal values.
287  EXPECT_TRUE(absl::ParseFlag("1", &value, &err));
288  EXPECT_EQ(value, 1);
289  EXPECT_TRUE(absl::ParseFlag("0", &value, &err));
290  EXPECT_EQ(value, 0);
291  EXPECT_TRUE(absl::ParseFlag("123", &value, &err));
292  EXPECT_EQ(value, 123);
293  EXPECT_TRUE(absl::ParseFlag("+3", &value, &err));
294  EXPECT_EQ(value, 3);
295 
296  // Leading zero values.
297  EXPECT_TRUE(absl::ParseFlag("01", &value, &err));
298  EXPECT_EQ(value, 1);
299  EXPECT_TRUE(absl::ParseFlag("0000100", &value, &err));
300  EXPECT_EQ(value, 100);
301 
302  // Hex values.
303  EXPECT_TRUE(absl::ParseFlag("0x10", &value, &err));
304  EXPECT_EQ(value, 16);
305  EXPECT_TRUE(absl::ParseFlag("0X234", &value, &err));
306  EXPECT_EQ(value, 564);
307  EXPECT_TRUE(absl::ParseFlag("0xFFFFFFFD", &value, &err));
308  EXPECT_EQ(value, 4294967293);
309  // TODO(rogeeff): fix below validations
310  EXPECT_FALSE(absl::ParseFlag("+0x31", &value, &err));
311  EXPECT_NE(value, 49);
312 
313  // Whitespace handling
314  EXPECT_TRUE(absl::ParseFlag("10 ", &value, &err));
315  EXPECT_EQ(value, 10);
316  EXPECT_TRUE(absl::ParseFlag(" 11", &value, &err));
317  EXPECT_EQ(value, 11);
318  EXPECT_TRUE(absl::ParseFlag(" 012 ", &value, &err));
319  EXPECT_EQ(value, 12);
320  EXPECT_TRUE(absl::ParseFlag(" 0x22 ", &value, &err));
321  EXPECT_EQ(value, 34);
322 
323  // Invalid values.
324  EXPECT_FALSE(absl::ParseFlag("", &value, &err));
325  EXPECT_FALSE(absl::ParseFlag(" ", &value, &err));
326  EXPECT_FALSE(absl::ParseFlag(" ", &value, &err));
327  EXPECT_FALSE(absl::ParseFlag("140000000000", &value, &err));
328  EXPECT_FALSE(absl::ParseFlag("-1", &value, &err));
329  EXPECT_FALSE(absl::ParseFlag("--1", &value, &err));
330  EXPECT_FALSE(absl::ParseFlag("\n", &value, &err));
331  EXPECT_FALSE(absl::ParseFlag("\t", &value, &err));
332  EXPECT_FALSE(absl::ParseFlag("2U", &value, &err));
333  EXPECT_FALSE(absl::ParseFlag("FFF", &value, &err));
334 }
335 
336 // --------------------------------------------------------------------
337 
338 TEST(MarshallingTest, TestInt64Parsing) {
339  std::string err;
340  int64_t value;
341 
342  // Decimal values.
343  EXPECT_TRUE(absl::ParseFlag("1", &value, &err));
344  EXPECT_EQ(value, 1);
345  EXPECT_TRUE(absl::ParseFlag("0", &value, &err));
346  EXPECT_EQ(value, 0);
347  EXPECT_TRUE(absl::ParseFlag("-1", &value, &err));
348  EXPECT_EQ(value, -1);
349  EXPECT_TRUE(absl::ParseFlag("123", &value, &err));
350  EXPECT_EQ(value, 123);
351  EXPECT_TRUE(absl::ParseFlag("-98765", &value, &err));
352  EXPECT_EQ(value, -98765);
353  EXPECT_TRUE(absl::ParseFlag("+3", &value, &err));
354  EXPECT_EQ(value, 3);
355 
356  // Leading zero values.
357  EXPECT_TRUE(absl::ParseFlag("01", &value, &err));
358  EXPECT_EQ(value, 1);
359  EXPECT_TRUE(absl::ParseFlag("001", &value, &err));
360  EXPECT_EQ(value, 1);
361  EXPECT_TRUE(absl::ParseFlag("0000100", &value, &err));
362  EXPECT_EQ(value, 100);
363 
364  // Hex values.
365  EXPECT_TRUE(absl::ParseFlag("0x10", &value, &err));
366  EXPECT_EQ(value, 16);
367  EXPECT_TRUE(absl::ParseFlag("0XFFFAAABBBCCCDDD", &value, &err));
368  EXPECT_EQ(value, 1152827684197027293);
369  // TODO(rogeeff): fix below validation
370  EXPECT_FALSE(absl::ParseFlag("-0x7FFFFFFFFFFFFFFE", &value, &err));
371  EXPECT_NE(value, -2);
372  EXPECT_FALSE(absl::ParseFlag("+0x31", &value, &err));
373  EXPECT_NE(value, 49);
374 
375  // Whitespace handling
376  EXPECT_TRUE(absl::ParseFlag("10 ", &value, &err));
377  EXPECT_EQ(value, 10);
378  EXPECT_TRUE(absl::ParseFlag(" 11", &value, &err));
379  EXPECT_EQ(value, 11);
380  EXPECT_TRUE(absl::ParseFlag(" 012 ", &value, &err));
381  EXPECT_EQ(value, 12);
382  EXPECT_TRUE(absl::ParseFlag(" 0x7F ", &value, &err));
383  EXPECT_EQ(value, 127);
384 
385  // Invalid values.
386  EXPECT_FALSE(absl::ParseFlag("", &value, &err));
387  EXPECT_FALSE(absl::ParseFlag(" ", &value, &err));
388  EXPECT_FALSE(absl::ParseFlag(" ", &value, &err));
389  EXPECT_FALSE(absl::ParseFlag("0xFFFFFFFFFFFFFFFFFF", &value, &err));
390  EXPECT_FALSE(absl::ParseFlag("--1", &value, &err));
391  EXPECT_FALSE(absl::ParseFlag("\n", &value, &err));
392  EXPECT_FALSE(absl::ParseFlag("\t", &value, &err));
393  EXPECT_FALSE(absl::ParseFlag("2U", &value, &err));
394  EXPECT_FALSE(absl::ParseFlag("FFF", &value, &err));
395 }
396 
397 // --------------------------------------------------------------------
398 
399 TEST(MarshallingTest, TestUInt64Parsing) {
400  std::string err;
401  uint64_t value;
402 
403  // Decimal values.
404  EXPECT_TRUE(absl::ParseFlag("1", &value, &err));
405  EXPECT_EQ(value, 1);
406  EXPECT_TRUE(absl::ParseFlag("0", &value, &err));
407  EXPECT_EQ(value, 0);
408  EXPECT_TRUE(absl::ParseFlag("123", &value, &err));
409  EXPECT_EQ(value, 123);
410  EXPECT_TRUE(absl::ParseFlag("+13", &value, &err));
411  EXPECT_EQ(value, 13);
412 
413  // Leading zero values.
414  EXPECT_TRUE(absl::ParseFlag("01", &value, &err));
415  EXPECT_EQ(value, 1);
416  EXPECT_TRUE(absl::ParseFlag("001", &value, &err));
417  EXPECT_EQ(value, 1);
418  EXPECT_TRUE(absl::ParseFlag("0000300", &value, &err));
419  EXPECT_EQ(value, 300);
420 
421  // Hex values.
422  EXPECT_TRUE(absl::ParseFlag("0x10", &value, &err));
423  EXPECT_EQ(value, 16);
424  EXPECT_TRUE(absl::ParseFlag("0XFFFF", &value, &err));
425  EXPECT_EQ(value, 65535);
426  // TODO(rogeeff): fix below validation
427  EXPECT_FALSE(absl::ParseFlag("+0x31", &value, &err));
428  EXPECT_NE(value, 49);
429 
430  // Whitespace handling
431  EXPECT_TRUE(absl::ParseFlag("10 ", &value, &err));
432  EXPECT_EQ(value, 10);
433  EXPECT_TRUE(absl::ParseFlag(" 11", &value, &err));
434  EXPECT_EQ(value, 11);
435  EXPECT_TRUE(absl::ParseFlag(" 012 ", &value, &err));
436  EXPECT_EQ(value, 12);
437 
438  // Invalid values.
439  EXPECT_FALSE(absl::ParseFlag("", &value, &err));
440  EXPECT_FALSE(absl::ParseFlag(" ", &value, &err));
441  EXPECT_FALSE(absl::ParseFlag(" ", &value, &err));
442  EXPECT_FALSE(absl::ParseFlag("0xFFFFFFFFFFFFFFFFFF", &value, &err));
443  EXPECT_FALSE(absl::ParseFlag("-1", &value, &err));
444  EXPECT_FALSE(absl::ParseFlag("--1", &value, &err));
445  EXPECT_FALSE(absl::ParseFlag("\n", &value, &err));
446  EXPECT_FALSE(absl::ParseFlag("\t", &value, &err));
447  EXPECT_FALSE(absl::ParseFlag("2U", &value, &err));
448  EXPECT_FALSE(absl::ParseFlag("FFF", &value, &err));
449 }
450 
451 // --------------------------------------------------------------------
452 
453 TEST(MarshallingTest, TestFloatParsing) {
454  std::string err;
455  float value;
456 
457  // Ordinary values.
458  EXPECT_TRUE(absl::ParseFlag("1.3", &value, &err));
459  EXPECT_FLOAT_EQ(value, 1.3f);
460  EXPECT_TRUE(absl::ParseFlag("-0.1", &value, &err));
461  EXPECT_DOUBLE_EQ(value, -0.1f);
462  EXPECT_TRUE(absl::ParseFlag("+0.01", &value, &err));
463  EXPECT_DOUBLE_EQ(value, 0.01f);
464 
465  // Scientific values.
466  EXPECT_TRUE(absl::ParseFlag("1.2e3", &value, &err));
467  EXPECT_DOUBLE_EQ(value, 1.2e3f);
468  EXPECT_TRUE(absl::ParseFlag("9.8765402e-37", &value, &err));
469  EXPECT_DOUBLE_EQ(value, 9.8765402e-37f);
470  EXPECT_TRUE(absl::ParseFlag("0.11e+3", &value, &err));
471  EXPECT_DOUBLE_EQ(value, 0.11e+3f);
472  EXPECT_TRUE(absl::ParseFlag("1.e-2300", &value, &err));
473  EXPECT_DOUBLE_EQ(value, 0.f);
474  EXPECT_TRUE(absl::ParseFlag("1.e+2300", &value, &err));
475  EXPECT_TRUE(std::isinf(value));
476 
477  // Leading zero values.
478  EXPECT_TRUE(absl::ParseFlag("01.6", &value, &err));
479  EXPECT_DOUBLE_EQ(value, 1.6f);
480  EXPECT_TRUE(absl::ParseFlag("000.0001", &value, &err));
481  EXPECT_DOUBLE_EQ(value, 0.0001f);
482 
483  // Trailing zero values.
484  EXPECT_TRUE(absl::ParseFlag("-5.1000", &value, &err));
485  EXPECT_DOUBLE_EQ(value, -5.1f);
486 
487  // Exceptional values.
488  EXPECT_TRUE(absl::ParseFlag("NaN", &value, &err));
489  EXPECT_TRUE(std::isnan(value));
490  EXPECT_TRUE(absl::ParseFlag("Inf", &value, &err));
491  EXPECT_TRUE(std::isinf(value));
492 
493  // Hex values
494  EXPECT_TRUE(absl::ParseFlag("0x10.23p12", &value, &err));
495  EXPECT_DOUBLE_EQ(value, 66096.f);
496  EXPECT_TRUE(absl::ParseFlag("-0xF1.A3p-2", &value, &err));
497  EXPECT_NEAR(value, -60.4092f, 5e-5f);
498  EXPECT_TRUE(absl::ParseFlag("+0x0.0AAp-12", &value, &err));
499  EXPECT_NEAR(value, 1.01328e-05f, 5e-11f);
500  EXPECT_TRUE(absl::ParseFlag("0x.01p1", &value, &err));
501  EXPECT_NEAR(value, 0.0078125f, 5e-8f);
502 
503  // Whitespace handling
504  EXPECT_TRUE(absl::ParseFlag("10.1 ", &value, &err));
505  EXPECT_DOUBLE_EQ(value, 10.1f);
506  EXPECT_TRUE(absl::ParseFlag(" 2.34", &value, &err));
507  EXPECT_DOUBLE_EQ(value, 2.34f);
508  EXPECT_TRUE(absl::ParseFlag(" 5.7 ", &value, &err));
509  EXPECT_DOUBLE_EQ(value, 5.7f);
510  EXPECT_TRUE(absl::ParseFlag(" -0xE0.F3p01 ", &value, &err));
511  EXPECT_NEAR(value, -449.8984375f, 5e-8f);
512 
513  // Invalid values.
514  EXPECT_FALSE(absl::ParseFlag("", &value, &err));
515  EXPECT_FALSE(absl::ParseFlag(" ", &value, &err));
516  EXPECT_FALSE(absl::ParseFlag(" ", &value, &err));
517  EXPECT_FALSE(absl::ParseFlag("--1", &value, &err));
518  EXPECT_FALSE(absl::ParseFlag("\n", &value, &err));
519  EXPECT_FALSE(absl::ParseFlag("\t", &value, &err));
520  EXPECT_FALSE(absl::ParseFlag("2.3xxx", &value, &err));
521  EXPECT_FALSE(absl::ParseFlag("0x0.1pAA", &value, &err));
522  // TODO(rogeeff): below assertion should fail
523  EXPECT_TRUE(absl::ParseFlag("0x0.1", &value, &err));
524 }
525 
526 // --------------------------------------------------------------------
527 
528 TEST(MarshallingTest, TestDoubleParsing) {
529  std::string err;
530  double value;
531 
532  // Ordinary values.
533  EXPECT_TRUE(absl::ParseFlag("1.3", &value, &err));
534  EXPECT_DOUBLE_EQ(value, 1.3);
535  EXPECT_TRUE(absl::ParseFlag("-0.1", &value, &err));
536  EXPECT_DOUBLE_EQ(value, -0.1);
537  EXPECT_TRUE(absl::ParseFlag("+0.01", &value, &err));
538  EXPECT_DOUBLE_EQ(value, 0.01);
539 
540  // Scientific values.
541  EXPECT_TRUE(absl::ParseFlag("1.2e3", &value, &err));
542  EXPECT_DOUBLE_EQ(value, 1.2e3);
543  EXPECT_TRUE(absl::ParseFlag("9.00000002e-123", &value, &err));
544  EXPECT_DOUBLE_EQ(value, 9.00000002e-123);
545  EXPECT_TRUE(absl::ParseFlag("0.11e+3", &value, &err));
546  EXPECT_DOUBLE_EQ(value, 0.11e+3);
547  EXPECT_TRUE(absl::ParseFlag("1.e-2300", &value, &err));
548  EXPECT_DOUBLE_EQ(value, 0);
549  EXPECT_TRUE(absl::ParseFlag("1.e+2300", &value, &err));
550  EXPECT_TRUE(std::isinf(value));
551 
552  // Leading zero values.
553  EXPECT_TRUE(absl::ParseFlag("01.6", &value, &err));
554  EXPECT_DOUBLE_EQ(value, 1.6);
555  EXPECT_TRUE(absl::ParseFlag("000.0001", &value, &err));
556  EXPECT_DOUBLE_EQ(value, 0.0001);
557 
558  // Trailing zero values.
559  EXPECT_TRUE(absl::ParseFlag("-5.1000", &value, &err));
560  EXPECT_DOUBLE_EQ(value, -5.1);
561 
562  // Exceptional values.
563  EXPECT_TRUE(absl::ParseFlag("NaN", &value, &err));
564  EXPECT_TRUE(std::isnan(value));
565  EXPECT_TRUE(absl::ParseFlag("nan", &value, &err));
566  EXPECT_TRUE(std::isnan(value));
567  EXPECT_TRUE(absl::ParseFlag("Inf", &value, &err));
568  EXPECT_TRUE(std::isinf(value));
569  EXPECT_TRUE(absl::ParseFlag("inf", &value, &err));
570  EXPECT_TRUE(std::isinf(value));
571 
572  // Hex values
573  EXPECT_TRUE(absl::ParseFlag("0x10.23p12", &value, &err));
574  EXPECT_DOUBLE_EQ(value, 66096);
575  EXPECT_TRUE(absl::ParseFlag("-0xF1.A3p-2", &value, &err));
576  EXPECT_NEAR(value, -60.4092, 5e-5);
577  EXPECT_TRUE(absl::ParseFlag("+0x0.0AAp-12", &value, &err));
578  EXPECT_NEAR(value, 1.01328e-05, 5e-11);
579  EXPECT_TRUE(absl::ParseFlag("0x.01p1", &value, &err));
580  EXPECT_NEAR(value, 0.0078125, 5e-8);
581 
582  // Whitespace handling
583  EXPECT_TRUE(absl::ParseFlag("10.1 ", &value, &err));
584  EXPECT_DOUBLE_EQ(value, 10.1);
585  EXPECT_TRUE(absl::ParseFlag(" 2.34", &value, &err));
586  EXPECT_DOUBLE_EQ(value, 2.34);
587  EXPECT_TRUE(absl::ParseFlag(" 5.7 ", &value, &err));
588  EXPECT_DOUBLE_EQ(value, 5.7);
589  EXPECT_TRUE(absl::ParseFlag(" -0xE0.F3p01 ", &value, &err));
590  EXPECT_NEAR(value, -449.8984375, 5e-8);
591 
592  // Invalid values.
593  EXPECT_FALSE(absl::ParseFlag("", &value, &err));
594  EXPECT_FALSE(absl::ParseFlag(" ", &value, &err));
595  EXPECT_FALSE(absl::ParseFlag(" ", &value, &err));
596  EXPECT_FALSE(absl::ParseFlag("--1", &value, &err));
597  EXPECT_FALSE(absl::ParseFlag("\n", &value, &err));
598  EXPECT_FALSE(absl::ParseFlag("\t", &value, &err));
599  EXPECT_FALSE(absl::ParseFlag("2.3xxx", &value, &err));
600  EXPECT_FALSE(absl::ParseFlag("0x0.1pAA", &value, &err));
601  // TODO(rogeeff): below assertion should fail
602  EXPECT_TRUE(absl::ParseFlag("0x0.1", &value, &err));
603 }
604 
605 // --------------------------------------------------------------------
606 
607 TEST(MarshallingTest, TestStringParsing) {
608  std::string err;
609  std::string value;
610 
611  EXPECT_TRUE(absl::ParseFlag("", &value, &err));
612  EXPECT_EQ(value, "");
613  EXPECT_TRUE(absl::ParseFlag(" ", &value, &err));
614  EXPECT_EQ(value, " ");
615  EXPECT_TRUE(absl::ParseFlag(" ", &value, &err));
616  EXPECT_EQ(value, " ");
617  EXPECT_TRUE(absl::ParseFlag("\n", &value, &err));
618  EXPECT_EQ(value, "\n");
619  EXPECT_TRUE(absl::ParseFlag("\t", &value, &err));
620  EXPECT_EQ(value, "\t");
621  EXPECT_TRUE(absl::ParseFlag("asdfg", &value, &err));
622  EXPECT_EQ(value, "asdfg");
623  EXPECT_TRUE(absl::ParseFlag("asdf ghjk", &value, &err));
624  EXPECT_EQ(value, "asdf ghjk");
625  EXPECT_TRUE(absl::ParseFlag("a\nb\nc", &value, &err));
626  EXPECT_EQ(value, "a\nb\nc");
627  EXPECT_TRUE(absl::ParseFlag("asd\0fgh", &value, &err));
628  EXPECT_EQ(value, "asd");
629  EXPECT_TRUE(absl::ParseFlag("\\\\", &value, &err));
630  EXPECT_EQ(value, "\\\\");
631 }
632 
633 // --------------------------------------------------------------------
634 
635 TEST(MarshallingTest, TestVectorOfStringParsing) {
636  std::string err;
637  std::vector<std::string> value;
638 
639  EXPECT_TRUE(absl::ParseFlag("", &value, &err));
640  EXPECT_EQ(value, std::vector<std::string>{});
641  EXPECT_TRUE(absl::ParseFlag("1", &value, &err));
642  EXPECT_EQ(value, std::vector<std::string>({"1"}));
643  EXPECT_TRUE(absl::ParseFlag("a,b", &value, &err));
644  EXPECT_EQ(value, std::vector<std::string>({"a", "b"}));
645  EXPECT_TRUE(absl::ParseFlag("a,b,c,", &value, &err));
646  EXPECT_EQ(value, std::vector<std::string>({"a", "b", "c", ""}));
647  EXPECT_TRUE(absl::ParseFlag("a,,", &value, &err));
648  EXPECT_EQ(value, std::vector<std::string>({"a", "", ""}));
649  EXPECT_TRUE(absl::ParseFlag(",", &value, &err));
650  EXPECT_EQ(value, std::vector<std::string>({"", ""}));
651  EXPECT_TRUE(absl::ParseFlag("a, b,c ", &value, &err));
652  EXPECT_EQ(value, std::vector<std::string>({"a", " b", "c "}));
653 }
654 
655 // --------------------------------------------------------------------
656 
657 TEST(MarshallingTest, TestBoolUnparsing) {
658  EXPECT_EQ(absl::UnparseFlag(true), "true");
659  EXPECT_EQ(absl::UnparseFlag(false), "false");
660 }
661 
662 // --------------------------------------------------------------------
663 
664 TEST(MarshallingTest, TestInt16Unparsing) {
665  int16_t value;
666 
667  value = 1;
668  EXPECT_EQ(absl::UnparseFlag(value), "1");
669  value = 0;
670  EXPECT_EQ(absl::UnparseFlag(value), "0");
671  value = -1;
672  EXPECT_EQ(absl::UnparseFlag(value), "-1");
673  value = 9876;
674  EXPECT_EQ(absl::UnparseFlag(value), "9876");
675  value = -987;
676  EXPECT_EQ(absl::UnparseFlag(value), "-987");
677 }
678 
679 // --------------------------------------------------------------------
680 
681 TEST(MarshallingTest, TestUint16Unparsing) {
682  uint16_t value;
683 
684  value = 1;
685  EXPECT_EQ(absl::UnparseFlag(value), "1");
686  value = 0;
687  EXPECT_EQ(absl::UnparseFlag(value), "0");
688  value = 19876;
689  EXPECT_EQ(absl::UnparseFlag(value), "19876");
690 }
691 
692 // --------------------------------------------------------------------
693 
694 TEST(MarshallingTest, TestInt32Unparsing) {
695  int32_t value;
696 
697  value = 1;
698  EXPECT_EQ(absl::UnparseFlag(value), "1");
699  value = 0;
700  EXPECT_EQ(absl::UnparseFlag(value), "0");
701  value = -1;
702  EXPECT_EQ(absl::UnparseFlag(value), "-1");
703  value = 12345;
704  EXPECT_EQ(absl::UnparseFlag(value), "12345");
705  value = -987;
706  EXPECT_EQ(absl::UnparseFlag(value), "-987");
707 }
708 
709 // --------------------------------------------------------------------
710 
711 TEST(MarshallingTest, TestUint32Unparsing) {
712  uint32_t value;
713 
714  value = 1;
715  EXPECT_EQ(absl::UnparseFlag(value), "1");
716  value = 0;
717  EXPECT_EQ(absl::UnparseFlag(value), "0");
718  value = 1234500;
719  EXPECT_EQ(absl::UnparseFlag(value), "1234500");
720 }
721 
722 // --------------------------------------------------------------------
723 
724 TEST(MarshallingTest, TestInt64Unparsing) {
725  int64_t value;
726 
727  value = 1;
728  EXPECT_EQ(absl::UnparseFlag(value), "1");
729  value = 0;
730  EXPECT_EQ(absl::UnparseFlag(value), "0");
731  value = -1;
732  EXPECT_EQ(absl::UnparseFlag(value), "-1");
733  value = 123456789L;
734  EXPECT_EQ(absl::UnparseFlag(value), "123456789");
735  value = -987654321L;
736  EXPECT_EQ(absl::UnparseFlag(value), "-987654321");
737  value = 0x7FFFFFFFFFFFFFFF;
738  EXPECT_EQ(absl::UnparseFlag(value), "9223372036854775807");
739  value = 0xFFFFFFFFFFFFFFFF;
740  EXPECT_EQ(absl::UnparseFlag(value), "-1");
741 }
742 
743 // --------------------------------------------------------------------
744 
745 TEST(MarshallingTest, TestUint64Unparsing) {
746  uint64_t value;
747 
748  value = 1;
749  EXPECT_EQ(absl::UnparseFlag(value), "1");
750  value = 0;
751  EXPECT_EQ(absl::UnparseFlag(value), "0");
752  value = 123456789L;
753  EXPECT_EQ(absl::UnparseFlag(value), "123456789");
754  value = 0xFFFFFFFFFFFFFFFF;
755  EXPECT_EQ(absl::UnparseFlag(value), "18446744073709551615");
756 }
757 
758 // --------------------------------------------------------------------
759 
760 TEST(MarshallingTest, TestFloatUnparsing) {
761  float value;
762 
763  value = 1.1f;
764  EXPECT_EQ(absl::UnparseFlag(value), "1.1");
765  value = 0.01f;
766  EXPECT_EQ(absl::UnparseFlag(value), "0.01");
767  value = 1.23e-2f;
768  EXPECT_EQ(absl::UnparseFlag(value), "0.0123");
769  value = -0.71f;
770  EXPECT_EQ(absl::UnparseFlag(value), "-0.71");
771 }
772 
773 // --------------------------------------------------------------------
774 
775 TEST(MarshallingTest, TestDoubleUnparsing) {
776  double value;
777 
778  value = 1.1;
779  EXPECT_EQ(absl::UnparseFlag(value), "1.1");
780  value = 0.01;
781  EXPECT_EQ(absl::UnparseFlag(value), "0.01");
782  value = 1.23e-2;
783  EXPECT_EQ(absl::UnparseFlag(value), "0.0123");
784  value = -0.71;
785  EXPECT_EQ(absl::UnparseFlag(value), "-0.71");
786  value = -0;
787  EXPECT_EQ(absl::UnparseFlag(value), "0");
788  value = std::nan("");
789  EXPECT_EQ(absl::UnparseFlag(value), "nan");
790  value = std::numeric_limits<double>::infinity();
791  EXPECT_EQ(absl::UnparseFlag(value), "inf");
792 }
793 
794 // --------------------------------------------------------------------
795 
796 TEST(MarshallingTest, TestStringUnparsing) {
797  EXPECT_EQ(absl::UnparseFlag(""), "");
798  EXPECT_EQ(absl::UnparseFlag(" "), " ");
799  EXPECT_EQ(absl::UnparseFlag("qwerty"), "qwerty");
800  EXPECT_EQ(absl::UnparseFlag("ASDFGH"), "ASDFGH");
801  EXPECT_EQ(absl::UnparseFlag("\n\t "), "\n\t ");
802 }
803 
804 // --------------------------------------------------------------------
805 
806 template <typename T>
807 void TestRoundtrip(T v) {
808  T new_v;
809  std::string err;
810  EXPECT_TRUE(absl::ParseFlag(absl::UnparseFlag(v), &new_v, &err));
811  EXPECT_EQ(new_v, v);
812 }
813 
814 TEST(MarshallingTest, TestFloatRoundTrip) {
815  TestRoundtrip(0.1f);
816  TestRoundtrip(0.12f);
817  TestRoundtrip(0.123f);
818  TestRoundtrip(0.1234f);
819  TestRoundtrip(0.12345f);
820  TestRoundtrip(0.123456f);
821  TestRoundtrip(0.1234567f);
822  TestRoundtrip(0.12345678f);
823 
824  TestRoundtrip(0.1e20f);
825  TestRoundtrip(0.12e20f);
826  TestRoundtrip(0.123e20f);
827  TestRoundtrip(0.1234e20f);
828  TestRoundtrip(0.12345e20f);
829  TestRoundtrip(0.123456e20f);
830  TestRoundtrip(0.1234567e20f);
831  TestRoundtrip(0.12345678e20f);
832 
833  TestRoundtrip(0.1e-20f);
834  TestRoundtrip(0.12e-20f);
835  TestRoundtrip(0.123e-20f);
836  TestRoundtrip(0.1234e-20f);
837  TestRoundtrip(0.12345e-20f);
838  TestRoundtrip(0.123456e-20f);
839  TestRoundtrip(0.1234567e-20f);
840  TestRoundtrip(0.12345678e-20f);
841 }
842 
843 TEST(MarshallingTest, TestDoubleRoundTrip) {
844  TestRoundtrip(0.1);
845  TestRoundtrip(0.12);
846  TestRoundtrip(0.123);
847  TestRoundtrip(0.1234);
848  TestRoundtrip(0.12345);
849  TestRoundtrip(0.123456);
850  TestRoundtrip(0.1234567);
851  TestRoundtrip(0.12345678);
852  TestRoundtrip(0.123456789);
853  TestRoundtrip(0.1234567891);
854  TestRoundtrip(0.12345678912);
855  TestRoundtrip(0.123456789123);
856  TestRoundtrip(0.1234567891234);
857  TestRoundtrip(0.12345678912345);
858  TestRoundtrip(0.123456789123456);
859  TestRoundtrip(0.1234567891234567);
860  TestRoundtrip(0.12345678912345678);
861 
862  TestRoundtrip(0.1e50);
863  TestRoundtrip(0.12e50);
864  TestRoundtrip(0.123e50);
865  TestRoundtrip(0.1234e50);
866  TestRoundtrip(0.12345e50);
867  TestRoundtrip(0.123456e50);
868  TestRoundtrip(0.1234567e50);
869  TestRoundtrip(0.12345678e50);
870  TestRoundtrip(0.123456789e50);
871  TestRoundtrip(0.1234567891e50);
872  TestRoundtrip(0.12345678912e50);
873  TestRoundtrip(0.123456789123e50);
874  TestRoundtrip(0.1234567891234e50);
875  TestRoundtrip(0.12345678912345e50);
876  TestRoundtrip(0.123456789123456e50);
877  TestRoundtrip(0.1234567891234567e50);
878  TestRoundtrip(0.12345678912345678e50);
879 
880  TestRoundtrip(0.1e-50);
881  TestRoundtrip(0.12e-50);
882  TestRoundtrip(0.123e-50);
883  TestRoundtrip(0.1234e-50);
884  TestRoundtrip(0.12345e-50);
885  TestRoundtrip(0.123456e-50);
886  TestRoundtrip(0.1234567e-50);
887  TestRoundtrip(0.12345678e-50);
888  TestRoundtrip(0.123456789e-50);
889  TestRoundtrip(0.1234567891e-50);
890  TestRoundtrip(0.12345678912e-50);
891  TestRoundtrip(0.123456789123e-50);
892  TestRoundtrip(0.1234567891234e-50);
893  TestRoundtrip(0.12345678912345e-50);
894  TestRoundtrip(0.123456789123456e-50);
895  TestRoundtrip(0.1234567891234567e-50);
896  TestRoundtrip(0.12345678912345678e-50);
897 }
898 
899 } // namespace
int v
Definition: variant_test.cc:81
std::string UnparseFlag(const T &v)
Definition: marshalling.h:254
size_t value
bool ParseFlag(absl::string_view input, T *dst, std::string *error)
Definition: marshalling.h:240
TEST(Symbolize, Unimplemented)


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:19:57