marshalling_test.cc
Go to the documentation of this file.
00001 //
00002 //  Copyright 2019 The Abseil Authors.
00003 //
00004 // Licensed under the Apache License, Version 2.0 (the "License");
00005 // you may not use this file except in compliance with the License.
00006 // You may obtain a copy of the License at
00007 //
00008 //      https://www.apache.org/licenses/LICENSE-2.0
00009 //
00010 // Unless required by applicable law or agreed to in writing, software
00011 // distributed under the License is distributed on an "AS IS" BASIS,
00012 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013 // See the License for the specific language governing permissions and
00014 // limitations under the License.
00015 
00016 #include "absl/flags/marshalling.h"
00017 
00018 #include <cmath>
00019 
00020 #include "gtest/gtest.h"
00021 
00022 namespace {
00023 
00024 TEST(MarshallingTest, TestBoolParsing) {
00025   std::string err;
00026   bool value;
00027 
00028   // True values.
00029   EXPECT_TRUE(absl::ParseFlag("True", &value, &err));
00030   EXPECT_TRUE(value);
00031   EXPECT_TRUE(absl::ParseFlag("true", &value, &err));
00032   EXPECT_TRUE(value);
00033   EXPECT_TRUE(absl::ParseFlag("TRUE", &value, &err));
00034   EXPECT_TRUE(value);
00035 
00036   EXPECT_TRUE(absl::ParseFlag("Yes", &value, &err));
00037   EXPECT_TRUE(value);
00038   EXPECT_TRUE(absl::ParseFlag("yes", &value, &err));
00039   EXPECT_TRUE(value);
00040   EXPECT_TRUE(absl::ParseFlag("YES", &value, &err));
00041   EXPECT_TRUE(value);
00042 
00043   EXPECT_TRUE(absl::ParseFlag("t", &value, &err));
00044   EXPECT_TRUE(value);
00045   EXPECT_TRUE(absl::ParseFlag("T", &value, &err));
00046   EXPECT_TRUE(value);
00047 
00048   EXPECT_TRUE(absl::ParseFlag("y", &value, &err));
00049   EXPECT_TRUE(value);
00050   EXPECT_TRUE(absl::ParseFlag("Y", &value, &err));
00051   EXPECT_TRUE(value);
00052 
00053   EXPECT_TRUE(absl::ParseFlag("1", &value, &err));
00054   EXPECT_TRUE(value);
00055 
00056   // False values.
00057   EXPECT_TRUE(absl::ParseFlag("False", &value, &err));
00058   EXPECT_FALSE(value);
00059   EXPECT_TRUE(absl::ParseFlag("false", &value, &err));
00060   EXPECT_FALSE(value);
00061   EXPECT_TRUE(absl::ParseFlag("FALSE", &value, &err));
00062   EXPECT_FALSE(value);
00063 
00064   EXPECT_TRUE(absl::ParseFlag("No", &value, &err));
00065   EXPECT_FALSE(value);
00066   EXPECT_TRUE(absl::ParseFlag("no", &value, &err));
00067   EXPECT_FALSE(value);
00068   EXPECT_TRUE(absl::ParseFlag("NO", &value, &err));
00069   EXPECT_FALSE(value);
00070 
00071   EXPECT_TRUE(absl::ParseFlag("f", &value, &err));
00072   EXPECT_FALSE(value);
00073   EXPECT_TRUE(absl::ParseFlag("F", &value, &err));
00074   EXPECT_FALSE(value);
00075 
00076   EXPECT_TRUE(absl::ParseFlag("n", &value, &err));
00077   EXPECT_FALSE(value);
00078   EXPECT_TRUE(absl::ParseFlag("N", &value, &err));
00079   EXPECT_FALSE(value);
00080 
00081   EXPECT_TRUE(absl::ParseFlag("0", &value, &err));
00082   EXPECT_FALSE(value);
00083 
00084   // Whitespace handling.
00085   EXPECT_TRUE(absl::ParseFlag("  true", &value, &err));
00086   EXPECT_TRUE(value);
00087   EXPECT_TRUE(absl::ParseFlag("true  ", &value, &err));
00088   EXPECT_TRUE(value);
00089   EXPECT_TRUE(absl::ParseFlag("  true   ", &value, &err));
00090   EXPECT_TRUE(value);
00091 
00092   // Invalid input.
00093   EXPECT_FALSE(absl::ParseFlag("", &value, &err));
00094   EXPECT_FALSE(absl::ParseFlag("  ", &value, &err));
00095   EXPECT_FALSE(absl::ParseFlag("\n", &value, &err));
00096   EXPECT_FALSE(absl::ParseFlag("\t", &value, &err));
00097   EXPECT_FALSE(absl::ParseFlag("2", &value, &err));
00098   EXPECT_FALSE(absl::ParseFlag("11", &value, &err));
00099   EXPECT_FALSE(absl::ParseFlag("tt", &value, &err));
00100 }
00101 
00102 // --------------------------------------------------------------------
00103 
00104 TEST(MarshallingTest, TestInt16Parsing) {
00105   std::string err;
00106   int16_t value;
00107 
00108   // Decimal values.
00109   EXPECT_TRUE(absl::ParseFlag("1", &value, &err));
00110   EXPECT_EQ(value, 1);
00111   EXPECT_TRUE(absl::ParseFlag("0", &value, &err));
00112   EXPECT_EQ(value, 0);
00113   EXPECT_TRUE(absl::ParseFlag("-1", &value, &err));
00114   EXPECT_EQ(value, -1);
00115   EXPECT_TRUE(absl::ParseFlag("123", &value, &err));
00116   EXPECT_EQ(value, 123);
00117   EXPECT_TRUE(absl::ParseFlag("-18765", &value, &err));
00118   EXPECT_EQ(value, -18765);
00119   EXPECT_TRUE(absl::ParseFlag("+3", &value, &err));
00120   EXPECT_EQ(value, 3);
00121 
00122   // Leading zero values.
00123   EXPECT_TRUE(absl::ParseFlag("01", &value, &err));
00124   EXPECT_EQ(value, 1);
00125   EXPECT_TRUE(absl::ParseFlag("-001", &value, &err));
00126   EXPECT_EQ(value, -1);
00127   EXPECT_TRUE(absl::ParseFlag("0000100", &value, &err));
00128   EXPECT_EQ(value, 100);
00129 
00130   // Hex values.
00131   EXPECT_TRUE(absl::ParseFlag("0x10", &value, &err));
00132   EXPECT_EQ(value, 16);
00133   EXPECT_TRUE(absl::ParseFlag("0X234", &value, &err));
00134   EXPECT_EQ(value, 564);
00135   // TODO(rogeeff): fix below validations
00136   EXPECT_FALSE(absl::ParseFlag("-0x7FFD", &value, &err));
00137   EXPECT_NE(value, -3);
00138   EXPECT_FALSE(absl::ParseFlag("+0x31", &value, &err));
00139   EXPECT_NE(value, 49);
00140 
00141   // Whitespace handling
00142   EXPECT_TRUE(absl::ParseFlag("10  ", &value, &err));
00143   EXPECT_EQ(value, 10);
00144   EXPECT_TRUE(absl::ParseFlag("  11", &value, &err));
00145   EXPECT_EQ(value, 11);
00146   EXPECT_TRUE(absl::ParseFlag("  012  ", &value, &err));
00147   EXPECT_EQ(value, 12);
00148   EXPECT_TRUE(absl::ParseFlag(" 0x22    ", &value, &err));
00149   EXPECT_EQ(value, 34);
00150 
00151   // Invalid values.
00152   EXPECT_FALSE(absl::ParseFlag("", &value, &err));
00153   EXPECT_FALSE(absl::ParseFlag(" ", &value, &err));
00154   EXPECT_FALSE(absl::ParseFlag("  ", &value, &err));
00155   EXPECT_FALSE(absl::ParseFlag("40000", &value, &err));
00156   EXPECT_FALSE(absl::ParseFlag("--1", &value, &err));
00157   EXPECT_FALSE(absl::ParseFlag("\n", &value, &err));
00158   EXPECT_FALSE(absl::ParseFlag("\t", &value, &err));
00159   EXPECT_FALSE(absl::ParseFlag("2U", &value, &err));
00160   EXPECT_FALSE(absl::ParseFlag("FFF", &value, &err));
00161 }
00162 
00163 // --------------------------------------------------------------------
00164 
00165 TEST(MarshallingTest, TestUint16Parsing) {
00166   std::string err;
00167   uint16_t value;
00168 
00169   // Decimal values.
00170   EXPECT_TRUE(absl::ParseFlag("1", &value, &err));
00171   EXPECT_EQ(value, 1);
00172   EXPECT_TRUE(absl::ParseFlag("0", &value, &err));
00173   EXPECT_EQ(value, 0);
00174   EXPECT_TRUE(absl::ParseFlag("123", &value, &err));
00175   EXPECT_EQ(value, 123);
00176   EXPECT_TRUE(absl::ParseFlag("+3", &value, &err));
00177   EXPECT_EQ(value, 3);
00178 
00179   // Leading zero values.
00180   EXPECT_TRUE(absl::ParseFlag("01", &value, &err));
00181   EXPECT_EQ(value, 1);
00182   EXPECT_TRUE(absl::ParseFlag("001", &value, &err));
00183   EXPECT_EQ(value, 1);
00184   EXPECT_TRUE(absl::ParseFlag("0000100", &value, &err));
00185   EXPECT_EQ(value, 100);
00186 
00187   // Hex values.
00188   EXPECT_TRUE(absl::ParseFlag("0x10", &value, &err));
00189   EXPECT_EQ(value, 16);
00190   EXPECT_TRUE(absl::ParseFlag("0X234", &value, &err));
00191   EXPECT_EQ(value, 564);
00192   // TODO(rogeeff): fix below validations
00193   EXPECT_FALSE(absl::ParseFlag("+0x31", &value, &err));
00194   EXPECT_NE(value, 49);
00195 
00196   // Whitespace handling
00197   EXPECT_TRUE(absl::ParseFlag("10  ", &value, &err));
00198   EXPECT_EQ(value, 10);
00199   EXPECT_TRUE(absl::ParseFlag("  11", &value, &err));
00200   EXPECT_EQ(value, 11);
00201   EXPECT_TRUE(absl::ParseFlag("  012  ", &value, &err));
00202   EXPECT_EQ(value, 12);
00203   EXPECT_TRUE(absl::ParseFlag(" 0x22    ", &value, &err));
00204   EXPECT_EQ(value, 34);
00205 
00206   // Invalid values.
00207   EXPECT_FALSE(absl::ParseFlag("", &value, &err));
00208   EXPECT_FALSE(absl::ParseFlag(" ", &value, &err));
00209   EXPECT_FALSE(absl::ParseFlag("  ", &value, &err));
00210   EXPECT_FALSE(absl::ParseFlag("70000", &value, &err));
00211   EXPECT_FALSE(absl::ParseFlag("-1", &value, &err));
00212   EXPECT_FALSE(absl::ParseFlag("--1", &value, &err));
00213   EXPECT_FALSE(absl::ParseFlag("\n", &value, &err));
00214   EXPECT_FALSE(absl::ParseFlag("\t", &value, &err));
00215   EXPECT_FALSE(absl::ParseFlag("2U", &value, &err));
00216   EXPECT_FALSE(absl::ParseFlag("FFF", &value, &err));
00217 }
00218 
00219 // --------------------------------------------------------------------
00220 
00221 TEST(MarshallingTest, TestInt32Parsing) {
00222   std::string err;
00223   int32_t value;
00224 
00225   // Decimal values.
00226   EXPECT_TRUE(absl::ParseFlag("1", &value, &err));
00227   EXPECT_EQ(value, 1);
00228   EXPECT_TRUE(absl::ParseFlag("0", &value, &err));
00229   EXPECT_EQ(value, 0);
00230   EXPECT_TRUE(absl::ParseFlag("-1", &value, &err));
00231   EXPECT_EQ(value, -1);
00232   EXPECT_TRUE(absl::ParseFlag("123", &value, &err));
00233   EXPECT_EQ(value, 123);
00234   EXPECT_TRUE(absl::ParseFlag("-98765", &value, &err));
00235   EXPECT_EQ(value, -98765);
00236   EXPECT_TRUE(absl::ParseFlag("+3", &value, &err));
00237   EXPECT_EQ(value, 3);
00238 
00239   // Leading zero values.
00240   EXPECT_TRUE(absl::ParseFlag("01", &value, &err));
00241   EXPECT_EQ(value, 1);
00242   EXPECT_TRUE(absl::ParseFlag("-001", &value, &err));
00243   EXPECT_EQ(value, -1);
00244   EXPECT_TRUE(absl::ParseFlag("0000100", &value, &err));
00245   EXPECT_EQ(value, 100);
00246 
00247   // Hex values.
00248   EXPECT_TRUE(absl::ParseFlag("0x10", &value, &err));
00249   EXPECT_EQ(value, 16);
00250   EXPECT_TRUE(absl::ParseFlag("0X234", &value, &err));
00251   EXPECT_EQ(value, 564);
00252   // TODO(rogeeff): fix below validations
00253   EXPECT_FALSE(absl::ParseFlag("-0x7FFFFFFD", &value, &err));
00254   EXPECT_NE(value, -3);
00255   EXPECT_FALSE(absl::ParseFlag("+0x31", &value, &err));
00256   EXPECT_NE(value, 49);
00257 
00258   // Whitespace handling
00259   EXPECT_TRUE(absl::ParseFlag("10  ", &value, &err));
00260   EXPECT_EQ(value, 10);
00261   EXPECT_TRUE(absl::ParseFlag("  11", &value, &err));
00262   EXPECT_EQ(value, 11);
00263   EXPECT_TRUE(absl::ParseFlag("  012  ", &value, &err));
00264   EXPECT_EQ(value, 12);
00265   EXPECT_TRUE(absl::ParseFlag(" 0x22    ", &value, &err));
00266   EXPECT_EQ(value, 34);
00267 
00268   // Invalid values.
00269   EXPECT_FALSE(absl::ParseFlag("", &value, &err));
00270   EXPECT_FALSE(absl::ParseFlag(" ", &value, &err));
00271   EXPECT_FALSE(absl::ParseFlag("  ", &value, &err));
00272   EXPECT_FALSE(absl::ParseFlag("70000000000", &value, &err));
00273   EXPECT_FALSE(absl::ParseFlag("--1", &value, &err));
00274   EXPECT_FALSE(absl::ParseFlag("\n", &value, &err));
00275   EXPECT_FALSE(absl::ParseFlag("\t", &value, &err));
00276   EXPECT_FALSE(absl::ParseFlag("2U", &value, &err));
00277   EXPECT_FALSE(absl::ParseFlag("FFF", &value, &err));
00278 }
00279 
00280 // --------------------------------------------------------------------
00281 
00282 TEST(MarshallingTest, TestUint32Parsing) {
00283   std::string err;
00284   uint32_t value;
00285 
00286   // Decimal values.
00287   EXPECT_TRUE(absl::ParseFlag("1", &value, &err));
00288   EXPECT_EQ(value, 1);
00289   EXPECT_TRUE(absl::ParseFlag("0", &value, &err));
00290   EXPECT_EQ(value, 0);
00291   EXPECT_TRUE(absl::ParseFlag("123", &value, &err));
00292   EXPECT_EQ(value, 123);
00293   EXPECT_TRUE(absl::ParseFlag("+3", &value, &err));
00294   EXPECT_EQ(value, 3);
00295 
00296   // Leading zero values.
00297   EXPECT_TRUE(absl::ParseFlag("01", &value, &err));
00298   EXPECT_EQ(value, 1);
00299   EXPECT_TRUE(absl::ParseFlag("0000100", &value, &err));
00300   EXPECT_EQ(value, 100);
00301 
00302   // Hex values.
00303   EXPECT_TRUE(absl::ParseFlag("0x10", &value, &err));
00304   EXPECT_EQ(value, 16);
00305   EXPECT_TRUE(absl::ParseFlag("0X234", &value, &err));
00306   EXPECT_EQ(value, 564);
00307   EXPECT_TRUE(absl::ParseFlag("0xFFFFFFFD", &value, &err));
00308   EXPECT_EQ(value, 4294967293);
00309   // TODO(rogeeff): fix below validations
00310   EXPECT_FALSE(absl::ParseFlag("+0x31", &value, &err));
00311   EXPECT_NE(value, 49);
00312 
00313   // Whitespace handling
00314   EXPECT_TRUE(absl::ParseFlag("10  ", &value, &err));
00315   EXPECT_EQ(value, 10);
00316   EXPECT_TRUE(absl::ParseFlag("  11", &value, &err));
00317   EXPECT_EQ(value, 11);
00318   EXPECT_TRUE(absl::ParseFlag("  012  ", &value, &err));
00319   EXPECT_EQ(value, 12);
00320   EXPECT_TRUE(absl::ParseFlag(" 0x22    ", &value, &err));
00321   EXPECT_EQ(value, 34);
00322 
00323   // Invalid values.
00324   EXPECT_FALSE(absl::ParseFlag("", &value, &err));
00325   EXPECT_FALSE(absl::ParseFlag(" ", &value, &err));
00326   EXPECT_FALSE(absl::ParseFlag("  ", &value, &err));
00327   EXPECT_FALSE(absl::ParseFlag("140000000000", &value, &err));
00328   EXPECT_FALSE(absl::ParseFlag("-1", &value, &err));
00329   EXPECT_FALSE(absl::ParseFlag("--1", &value, &err));
00330   EXPECT_FALSE(absl::ParseFlag("\n", &value, &err));
00331   EXPECT_FALSE(absl::ParseFlag("\t", &value, &err));
00332   EXPECT_FALSE(absl::ParseFlag("2U", &value, &err));
00333   EXPECT_FALSE(absl::ParseFlag("FFF", &value, &err));
00334 }
00335 
00336 // --------------------------------------------------------------------
00337 
00338 TEST(MarshallingTest, TestInt64Parsing) {
00339   std::string err;
00340   int64_t value;
00341 
00342   // Decimal values.
00343   EXPECT_TRUE(absl::ParseFlag("1", &value, &err));
00344   EXPECT_EQ(value, 1);
00345   EXPECT_TRUE(absl::ParseFlag("0", &value, &err));
00346   EXPECT_EQ(value, 0);
00347   EXPECT_TRUE(absl::ParseFlag("-1", &value, &err));
00348   EXPECT_EQ(value, -1);
00349   EXPECT_TRUE(absl::ParseFlag("123", &value, &err));
00350   EXPECT_EQ(value, 123);
00351   EXPECT_TRUE(absl::ParseFlag("-98765", &value, &err));
00352   EXPECT_EQ(value, -98765);
00353   EXPECT_TRUE(absl::ParseFlag("+3", &value, &err));
00354   EXPECT_EQ(value, 3);
00355 
00356   // Leading zero values.
00357   EXPECT_TRUE(absl::ParseFlag("01", &value, &err));
00358   EXPECT_EQ(value, 1);
00359   EXPECT_TRUE(absl::ParseFlag("001", &value, &err));
00360   EXPECT_EQ(value, 1);
00361   EXPECT_TRUE(absl::ParseFlag("0000100", &value, &err));
00362   EXPECT_EQ(value, 100);
00363 
00364   // Hex values.
00365   EXPECT_TRUE(absl::ParseFlag("0x10", &value, &err));
00366   EXPECT_EQ(value, 16);
00367   EXPECT_TRUE(absl::ParseFlag("0XFFFAAABBBCCCDDD", &value, &err));
00368   EXPECT_EQ(value, 1152827684197027293);
00369   // TODO(rogeeff): fix below validation
00370   EXPECT_FALSE(absl::ParseFlag("-0x7FFFFFFFFFFFFFFE", &value, &err));
00371   EXPECT_NE(value, -2);
00372   EXPECT_FALSE(absl::ParseFlag("+0x31", &value, &err));
00373   EXPECT_NE(value, 49);
00374 
00375   // Whitespace handling
00376   EXPECT_TRUE(absl::ParseFlag("10  ", &value, &err));
00377   EXPECT_EQ(value, 10);
00378   EXPECT_TRUE(absl::ParseFlag("  11", &value, &err));
00379   EXPECT_EQ(value, 11);
00380   EXPECT_TRUE(absl::ParseFlag("  012  ", &value, &err));
00381   EXPECT_EQ(value, 12);
00382   EXPECT_TRUE(absl::ParseFlag(" 0x7F    ", &value, &err));
00383   EXPECT_EQ(value, 127);
00384 
00385   // Invalid values.
00386   EXPECT_FALSE(absl::ParseFlag("", &value, &err));
00387   EXPECT_FALSE(absl::ParseFlag(" ", &value, &err));
00388   EXPECT_FALSE(absl::ParseFlag("  ", &value, &err));
00389   EXPECT_FALSE(absl::ParseFlag("0xFFFFFFFFFFFFFFFFFF", &value, &err));
00390   EXPECT_FALSE(absl::ParseFlag("--1", &value, &err));
00391   EXPECT_FALSE(absl::ParseFlag("\n", &value, &err));
00392   EXPECT_FALSE(absl::ParseFlag("\t", &value, &err));
00393   EXPECT_FALSE(absl::ParseFlag("2U", &value, &err));
00394   EXPECT_FALSE(absl::ParseFlag("FFF", &value, &err));
00395 }
00396 
00397 // --------------------------------------------------------------------
00398 
00399 TEST(MarshallingTest, TestUInt64Parsing) {
00400   std::string err;
00401   uint64_t value;
00402 
00403   // Decimal values.
00404   EXPECT_TRUE(absl::ParseFlag("1", &value, &err));
00405   EXPECT_EQ(value, 1);
00406   EXPECT_TRUE(absl::ParseFlag("0", &value, &err));
00407   EXPECT_EQ(value, 0);
00408   EXPECT_TRUE(absl::ParseFlag("123", &value, &err));
00409   EXPECT_EQ(value, 123);
00410   EXPECT_TRUE(absl::ParseFlag("+13", &value, &err));
00411   EXPECT_EQ(value, 13);
00412 
00413   // Leading zero values.
00414   EXPECT_TRUE(absl::ParseFlag("01", &value, &err));
00415   EXPECT_EQ(value, 1);
00416   EXPECT_TRUE(absl::ParseFlag("001", &value, &err));
00417   EXPECT_EQ(value, 1);
00418   EXPECT_TRUE(absl::ParseFlag("0000300", &value, &err));
00419   EXPECT_EQ(value, 300);
00420 
00421   // Hex values.
00422   EXPECT_TRUE(absl::ParseFlag("0x10", &value, &err));
00423   EXPECT_EQ(value, 16);
00424   EXPECT_TRUE(absl::ParseFlag("0XFFFF", &value, &err));
00425   EXPECT_EQ(value, 65535);
00426   // TODO(rogeeff): fix below validation
00427   EXPECT_FALSE(absl::ParseFlag("+0x31", &value, &err));
00428   EXPECT_NE(value, 49);
00429 
00430   // Whitespace handling
00431   EXPECT_TRUE(absl::ParseFlag("10  ", &value, &err));
00432   EXPECT_EQ(value, 10);
00433   EXPECT_TRUE(absl::ParseFlag("  11", &value, &err));
00434   EXPECT_EQ(value, 11);
00435   EXPECT_TRUE(absl::ParseFlag("  012  ", &value, &err));
00436   EXPECT_EQ(value, 12);
00437 
00438   // Invalid values.
00439   EXPECT_FALSE(absl::ParseFlag("", &value, &err));
00440   EXPECT_FALSE(absl::ParseFlag(" ", &value, &err));
00441   EXPECT_FALSE(absl::ParseFlag("  ", &value, &err));
00442   EXPECT_FALSE(absl::ParseFlag("0xFFFFFFFFFFFFFFFFFF", &value, &err));
00443   EXPECT_FALSE(absl::ParseFlag("-1", &value, &err));
00444   EXPECT_FALSE(absl::ParseFlag("--1", &value, &err));
00445   EXPECT_FALSE(absl::ParseFlag("\n", &value, &err));
00446   EXPECT_FALSE(absl::ParseFlag("\t", &value, &err));
00447   EXPECT_FALSE(absl::ParseFlag("2U", &value, &err));
00448   EXPECT_FALSE(absl::ParseFlag("FFF", &value, &err));
00449 }
00450 
00451 // --------------------------------------------------------------------
00452 
00453 TEST(MarshallingTest, TestFloatParsing) {
00454   std::string err;
00455   float value;
00456 
00457   // Ordinary values.
00458   EXPECT_TRUE(absl::ParseFlag("1.3", &value, &err));
00459   EXPECT_FLOAT_EQ(value, 1.3f);
00460   EXPECT_TRUE(absl::ParseFlag("-0.1", &value, &err));
00461   EXPECT_DOUBLE_EQ(value, -0.1f);
00462   EXPECT_TRUE(absl::ParseFlag("+0.01", &value, &err));
00463   EXPECT_DOUBLE_EQ(value, 0.01f);
00464 
00465   // Scientific values.
00466   EXPECT_TRUE(absl::ParseFlag("1.2e3", &value, &err));
00467   EXPECT_DOUBLE_EQ(value, 1.2e3f);
00468   EXPECT_TRUE(absl::ParseFlag("9.8765402e-37", &value, &err));
00469   EXPECT_DOUBLE_EQ(value, 9.8765402e-37f);
00470   EXPECT_TRUE(absl::ParseFlag("0.11e+3", &value, &err));
00471   EXPECT_DOUBLE_EQ(value, 0.11e+3f);
00472   EXPECT_TRUE(absl::ParseFlag("1.e-2300", &value, &err));
00473   EXPECT_DOUBLE_EQ(value, 0.f);
00474   EXPECT_TRUE(absl::ParseFlag("1.e+2300", &value, &err));
00475   EXPECT_TRUE(std::isinf(value));
00476 
00477   // Leading zero values.
00478   EXPECT_TRUE(absl::ParseFlag("01.6", &value, &err));
00479   EXPECT_DOUBLE_EQ(value, 1.6f);
00480   EXPECT_TRUE(absl::ParseFlag("000.0001", &value, &err));
00481   EXPECT_DOUBLE_EQ(value, 0.0001f);
00482 
00483   // Trailing zero values.
00484   EXPECT_TRUE(absl::ParseFlag("-5.1000", &value, &err));
00485   EXPECT_DOUBLE_EQ(value, -5.1f);
00486 
00487   // Exceptional values.
00488   EXPECT_TRUE(absl::ParseFlag("NaN", &value, &err));
00489   EXPECT_TRUE(std::isnan(value));
00490   EXPECT_TRUE(absl::ParseFlag("Inf", &value, &err));
00491   EXPECT_TRUE(std::isinf(value));
00492 
00493   // Hex values
00494   EXPECT_TRUE(absl::ParseFlag("0x10.23p12", &value, &err));
00495   EXPECT_DOUBLE_EQ(value, 66096.f);
00496   EXPECT_TRUE(absl::ParseFlag("-0xF1.A3p-2", &value, &err));
00497   EXPECT_NEAR(value, -60.4092f, 5e-5f);
00498   EXPECT_TRUE(absl::ParseFlag("+0x0.0AAp-12", &value, &err));
00499   EXPECT_NEAR(value, 1.01328e-05f, 5e-11f);
00500   EXPECT_TRUE(absl::ParseFlag("0x.01p1", &value, &err));
00501   EXPECT_NEAR(value, 0.0078125f, 5e-8f);
00502 
00503   // Whitespace handling
00504   EXPECT_TRUE(absl::ParseFlag("10.1  ", &value, &err));
00505   EXPECT_DOUBLE_EQ(value, 10.1f);
00506   EXPECT_TRUE(absl::ParseFlag("  2.34", &value, &err));
00507   EXPECT_DOUBLE_EQ(value, 2.34f);
00508   EXPECT_TRUE(absl::ParseFlag("  5.7  ", &value, &err));
00509   EXPECT_DOUBLE_EQ(value, 5.7f);
00510   EXPECT_TRUE(absl::ParseFlag("  -0xE0.F3p01  ", &value, &err));
00511   EXPECT_NEAR(value, -449.8984375f, 5e-8f);
00512 
00513   // Invalid values.
00514   EXPECT_FALSE(absl::ParseFlag("", &value, &err));
00515   EXPECT_FALSE(absl::ParseFlag(" ", &value, &err));
00516   EXPECT_FALSE(absl::ParseFlag("  ", &value, &err));
00517   EXPECT_FALSE(absl::ParseFlag("--1", &value, &err));
00518   EXPECT_FALSE(absl::ParseFlag("\n", &value, &err));
00519   EXPECT_FALSE(absl::ParseFlag("\t", &value, &err));
00520   EXPECT_FALSE(absl::ParseFlag("2.3xxx", &value, &err));
00521   EXPECT_FALSE(absl::ParseFlag("0x0.1pAA", &value, &err));
00522   // TODO(rogeeff): below assertion should fail
00523   EXPECT_TRUE(absl::ParseFlag("0x0.1", &value, &err));
00524 }
00525 
00526 // --------------------------------------------------------------------
00527 
00528 TEST(MarshallingTest, TestDoubleParsing) {
00529   std::string err;
00530   double value;
00531 
00532   // Ordinary values.
00533   EXPECT_TRUE(absl::ParseFlag("1.3", &value, &err));
00534   EXPECT_DOUBLE_EQ(value, 1.3);
00535   EXPECT_TRUE(absl::ParseFlag("-0.1", &value, &err));
00536   EXPECT_DOUBLE_EQ(value, -0.1);
00537   EXPECT_TRUE(absl::ParseFlag("+0.01", &value, &err));
00538   EXPECT_DOUBLE_EQ(value, 0.01);
00539 
00540   // Scientific values.
00541   EXPECT_TRUE(absl::ParseFlag("1.2e3", &value, &err));
00542   EXPECT_DOUBLE_EQ(value, 1.2e3);
00543   EXPECT_TRUE(absl::ParseFlag("9.00000002e-123", &value, &err));
00544   EXPECT_DOUBLE_EQ(value, 9.00000002e-123);
00545   EXPECT_TRUE(absl::ParseFlag("0.11e+3", &value, &err));
00546   EXPECT_DOUBLE_EQ(value, 0.11e+3);
00547   EXPECT_TRUE(absl::ParseFlag("1.e-2300", &value, &err));
00548   EXPECT_DOUBLE_EQ(value, 0);
00549   EXPECT_TRUE(absl::ParseFlag("1.e+2300", &value, &err));
00550   EXPECT_TRUE(std::isinf(value));
00551 
00552   // Leading zero values.
00553   EXPECT_TRUE(absl::ParseFlag("01.6", &value, &err));
00554   EXPECT_DOUBLE_EQ(value, 1.6);
00555   EXPECT_TRUE(absl::ParseFlag("000.0001", &value, &err));
00556   EXPECT_DOUBLE_EQ(value, 0.0001);
00557 
00558   // Trailing zero values.
00559   EXPECT_TRUE(absl::ParseFlag("-5.1000", &value, &err));
00560   EXPECT_DOUBLE_EQ(value, -5.1);
00561 
00562   // Exceptional values.
00563   EXPECT_TRUE(absl::ParseFlag("NaN", &value, &err));
00564   EXPECT_TRUE(std::isnan(value));
00565   EXPECT_TRUE(absl::ParseFlag("nan", &value, &err));
00566   EXPECT_TRUE(std::isnan(value));
00567   EXPECT_TRUE(absl::ParseFlag("Inf", &value, &err));
00568   EXPECT_TRUE(std::isinf(value));
00569   EXPECT_TRUE(absl::ParseFlag("inf", &value, &err));
00570   EXPECT_TRUE(std::isinf(value));
00571 
00572   // Hex values
00573   EXPECT_TRUE(absl::ParseFlag("0x10.23p12", &value, &err));
00574   EXPECT_DOUBLE_EQ(value, 66096);
00575   EXPECT_TRUE(absl::ParseFlag("-0xF1.A3p-2", &value, &err));
00576   EXPECT_NEAR(value, -60.4092, 5e-5);
00577   EXPECT_TRUE(absl::ParseFlag("+0x0.0AAp-12", &value, &err));
00578   EXPECT_NEAR(value, 1.01328e-05, 5e-11);
00579   EXPECT_TRUE(absl::ParseFlag("0x.01p1", &value, &err));
00580   EXPECT_NEAR(value, 0.0078125, 5e-8);
00581 
00582   // Whitespace handling
00583   EXPECT_TRUE(absl::ParseFlag("10.1  ", &value, &err));
00584   EXPECT_DOUBLE_EQ(value, 10.1);
00585   EXPECT_TRUE(absl::ParseFlag("  2.34", &value, &err));
00586   EXPECT_DOUBLE_EQ(value, 2.34);
00587   EXPECT_TRUE(absl::ParseFlag("  5.7  ", &value, &err));
00588   EXPECT_DOUBLE_EQ(value, 5.7);
00589   EXPECT_TRUE(absl::ParseFlag("  -0xE0.F3p01  ", &value, &err));
00590   EXPECT_NEAR(value, -449.8984375, 5e-8);
00591 
00592   // Invalid values.
00593   EXPECT_FALSE(absl::ParseFlag("", &value, &err));
00594   EXPECT_FALSE(absl::ParseFlag(" ", &value, &err));
00595   EXPECT_FALSE(absl::ParseFlag("  ", &value, &err));
00596   EXPECT_FALSE(absl::ParseFlag("--1", &value, &err));
00597   EXPECT_FALSE(absl::ParseFlag("\n", &value, &err));
00598   EXPECT_FALSE(absl::ParseFlag("\t", &value, &err));
00599   EXPECT_FALSE(absl::ParseFlag("2.3xxx", &value, &err));
00600   EXPECT_FALSE(absl::ParseFlag("0x0.1pAA", &value, &err));
00601   // TODO(rogeeff): below assertion should fail
00602   EXPECT_TRUE(absl::ParseFlag("0x0.1", &value, &err));
00603 }
00604 
00605 // --------------------------------------------------------------------
00606 
00607 TEST(MarshallingTest, TestStringParsing) {
00608   std::string err;
00609   std::string value;
00610 
00611   EXPECT_TRUE(absl::ParseFlag("", &value, &err));
00612   EXPECT_EQ(value, "");
00613   EXPECT_TRUE(absl::ParseFlag(" ", &value, &err));
00614   EXPECT_EQ(value, " ");
00615   EXPECT_TRUE(absl::ParseFlag("   ", &value, &err));
00616   EXPECT_EQ(value, "   ");
00617   EXPECT_TRUE(absl::ParseFlag("\n", &value, &err));
00618   EXPECT_EQ(value, "\n");
00619   EXPECT_TRUE(absl::ParseFlag("\t", &value, &err));
00620   EXPECT_EQ(value, "\t");
00621   EXPECT_TRUE(absl::ParseFlag("asdfg", &value, &err));
00622   EXPECT_EQ(value, "asdfg");
00623   EXPECT_TRUE(absl::ParseFlag("asdf ghjk", &value, &err));
00624   EXPECT_EQ(value, "asdf ghjk");
00625   EXPECT_TRUE(absl::ParseFlag("a\nb\nc", &value, &err));
00626   EXPECT_EQ(value, "a\nb\nc");
00627   EXPECT_TRUE(absl::ParseFlag("asd\0fgh", &value, &err));
00628   EXPECT_EQ(value, "asd");
00629   EXPECT_TRUE(absl::ParseFlag("\\\\", &value, &err));
00630   EXPECT_EQ(value, "\\\\");
00631 }
00632 
00633 // --------------------------------------------------------------------
00634 
00635 TEST(MarshallingTest, TestVectorOfStringParsing) {
00636   std::string err;
00637   std::vector<std::string> value;
00638 
00639   EXPECT_TRUE(absl::ParseFlag("", &value, &err));
00640   EXPECT_EQ(value, std::vector<std::string>{});
00641   EXPECT_TRUE(absl::ParseFlag("1", &value, &err));
00642   EXPECT_EQ(value, std::vector<std::string>({"1"}));
00643   EXPECT_TRUE(absl::ParseFlag("a,b", &value, &err));
00644   EXPECT_EQ(value, std::vector<std::string>({"a", "b"}));
00645   EXPECT_TRUE(absl::ParseFlag("a,b,c,", &value, &err));
00646   EXPECT_EQ(value, std::vector<std::string>({"a", "b", "c", ""}));
00647   EXPECT_TRUE(absl::ParseFlag("a,,", &value, &err));
00648   EXPECT_EQ(value, std::vector<std::string>({"a", "", ""}));
00649   EXPECT_TRUE(absl::ParseFlag(",", &value, &err));
00650   EXPECT_EQ(value, std::vector<std::string>({"", ""}));
00651   EXPECT_TRUE(absl::ParseFlag("a, b,c ", &value, &err));
00652   EXPECT_EQ(value, std::vector<std::string>({"a", " b", "c "}));
00653 }
00654 
00655 // --------------------------------------------------------------------
00656 
00657 TEST(MarshallingTest, TestBoolUnparsing) {
00658   EXPECT_EQ(absl::UnparseFlag(true), "true");
00659   EXPECT_EQ(absl::UnparseFlag(false), "false");
00660 }
00661 
00662 // --------------------------------------------------------------------
00663 
00664 TEST(MarshallingTest, TestInt16Unparsing) {
00665   int16_t value;
00666 
00667   value = 1;
00668   EXPECT_EQ(absl::UnparseFlag(value), "1");
00669   value = 0;
00670   EXPECT_EQ(absl::UnparseFlag(value), "0");
00671   value = -1;
00672   EXPECT_EQ(absl::UnparseFlag(value), "-1");
00673   value = 9876;
00674   EXPECT_EQ(absl::UnparseFlag(value), "9876");
00675   value = -987;
00676   EXPECT_EQ(absl::UnparseFlag(value), "-987");
00677 }
00678 
00679 // --------------------------------------------------------------------
00680 
00681 TEST(MarshallingTest, TestUint16Unparsing) {
00682   uint16_t value;
00683 
00684   value = 1;
00685   EXPECT_EQ(absl::UnparseFlag(value), "1");
00686   value = 0;
00687   EXPECT_EQ(absl::UnparseFlag(value), "0");
00688   value = 19876;
00689   EXPECT_EQ(absl::UnparseFlag(value), "19876");
00690 }
00691 
00692 // --------------------------------------------------------------------
00693 
00694 TEST(MarshallingTest, TestInt32Unparsing) {
00695   int32_t value;
00696 
00697   value = 1;
00698   EXPECT_EQ(absl::UnparseFlag(value), "1");
00699   value = 0;
00700   EXPECT_EQ(absl::UnparseFlag(value), "0");
00701   value = -1;
00702   EXPECT_EQ(absl::UnparseFlag(value), "-1");
00703   value = 12345;
00704   EXPECT_EQ(absl::UnparseFlag(value), "12345");
00705   value = -987;
00706   EXPECT_EQ(absl::UnparseFlag(value), "-987");
00707 }
00708 
00709 // --------------------------------------------------------------------
00710 
00711 TEST(MarshallingTest, TestUint32Unparsing) {
00712   uint32_t value;
00713 
00714   value = 1;
00715   EXPECT_EQ(absl::UnparseFlag(value), "1");
00716   value = 0;
00717   EXPECT_EQ(absl::UnparseFlag(value), "0");
00718   value = 1234500;
00719   EXPECT_EQ(absl::UnparseFlag(value), "1234500");
00720 }
00721 
00722 // --------------------------------------------------------------------
00723 
00724 TEST(MarshallingTest, TestInt64Unparsing) {
00725   int64_t value;
00726 
00727   value = 1;
00728   EXPECT_EQ(absl::UnparseFlag(value), "1");
00729   value = 0;
00730   EXPECT_EQ(absl::UnparseFlag(value), "0");
00731   value = -1;
00732   EXPECT_EQ(absl::UnparseFlag(value), "-1");
00733   value = 123456789L;
00734   EXPECT_EQ(absl::UnparseFlag(value), "123456789");
00735   value = -987654321L;
00736   EXPECT_EQ(absl::UnparseFlag(value), "-987654321");
00737   value = 0x7FFFFFFFFFFFFFFF;
00738   EXPECT_EQ(absl::UnparseFlag(value), "9223372036854775807");
00739   value = 0xFFFFFFFFFFFFFFFF;
00740   EXPECT_EQ(absl::UnparseFlag(value), "-1");
00741 }
00742 
00743 // --------------------------------------------------------------------
00744 
00745 TEST(MarshallingTest, TestUint64Unparsing) {
00746   uint64_t value;
00747 
00748   value = 1;
00749   EXPECT_EQ(absl::UnparseFlag(value), "1");
00750   value = 0;
00751   EXPECT_EQ(absl::UnparseFlag(value), "0");
00752   value = 123456789L;
00753   EXPECT_EQ(absl::UnparseFlag(value), "123456789");
00754   value = 0xFFFFFFFFFFFFFFFF;
00755   EXPECT_EQ(absl::UnparseFlag(value), "18446744073709551615");
00756 }
00757 
00758 // --------------------------------------------------------------------
00759 
00760 TEST(MarshallingTest, TestFloatUnparsing) {
00761   float value;
00762 
00763   value = 1.1f;
00764   EXPECT_EQ(absl::UnparseFlag(value), "1.1");
00765   value = 0.01f;
00766   EXPECT_EQ(absl::UnparseFlag(value), "0.01");
00767   value = 1.23e-2f;
00768   EXPECT_EQ(absl::UnparseFlag(value), "0.0123");
00769   value = -0.71f;
00770   EXPECT_EQ(absl::UnparseFlag(value), "-0.71");
00771 }
00772 
00773 // --------------------------------------------------------------------
00774 
00775 TEST(MarshallingTest, TestDoubleUnparsing) {
00776   double value;
00777 
00778   value = 1.1;
00779   EXPECT_EQ(absl::UnparseFlag(value), "1.1");
00780   value = 0.01;
00781   EXPECT_EQ(absl::UnparseFlag(value), "0.01");
00782   value = 1.23e-2;
00783   EXPECT_EQ(absl::UnparseFlag(value), "0.0123");
00784   value = -0.71;
00785   EXPECT_EQ(absl::UnparseFlag(value), "-0.71");
00786   value = -0;
00787   EXPECT_EQ(absl::UnparseFlag(value), "0");
00788   value = std::nan("");
00789   EXPECT_EQ(absl::UnparseFlag(value), "nan");
00790   value = std::numeric_limits<double>::infinity();
00791   EXPECT_EQ(absl::UnparseFlag(value), "inf");
00792 }
00793 
00794 // --------------------------------------------------------------------
00795 
00796 TEST(MarshallingTest, TestStringUnparsing) {
00797   EXPECT_EQ(absl::UnparseFlag(""), "");
00798   EXPECT_EQ(absl::UnparseFlag(" "), " ");
00799   EXPECT_EQ(absl::UnparseFlag("qwerty"), "qwerty");
00800   EXPECT_EQ(absl::UnparseFlag("ASDFGH"), "ASDFGH");
00801   EXPECT_EQ(absl::UnparseFlag("\n\t  "), "\n\t  ");
00802 }
00803 
00804 // --------------------------------------------------------------------
00805 
00806 template <typename T>
00807 void TestRoundtrip(T v) {
00808   T new_v;
00809   std::string err;
00810   EXPECT_TRUE(absl::ParseFlag(absl::UnparseFlag(v), &new_v, &err));
00811   EXPECT_EQ(new_v, v);
00812 }
00813 
00814 TEST(MarshallingTest, TestFloatRoundTrip) {
00815   TestRoundtrip(0.1f);
00816   TestRoundtrip(0.12f);
00817   TestRoundtrip(0.123f);
00818   TestRoundtrip(0.1234f);
00819   TestRoundtrip(0.12345f);
00820   TestRoundtrip(0.123456f);
00821   TestRoundtrip(0.1234567f);
00822   TestRoundtrip(0.12345678f);
00823 
00824   TestRoundtrip(0.1e20f);
00825   TestRoundtrip(0.12e20f);
00826   TestRoundtrip(0.123e20f);
00827   TestRoundtrip(0.1234e20f);
00828   TestRoundtrip(0.12345e20f);
00829   TestRoundtrip(0.123456e20f);
00830   TestRoundtrip(0.1234567e20f);
00831   TestRoundtrip(0.12345678e20f);
00832 
00833   TestRoundtrip(0.1e-20f);
00834   TestRoundtrip(0.12e-20f);
00835   TestRoundtrip(0.123e-20f);
00836   TestRoundtrip(0.1234e-20f);
00837   TestRoundtrip(0.12345e-20f);
00838   TestRoundtrip(0.123456e-20f);
00839   TestRoundtrip(0.1234567e-20f);
00840   TestRoundtrip(0.12345678e-20f);
00841 }
00842 
00843 TEST(MarshallingTest, TestDoubleRoundTrip) {
00844   TestRoundtrip(0.1);
00845   TestRoundtrip(0.12);
00846   TestRoundtrip(0.123);
00847   TestRoundtrip(0.1234);
00848   TestRoundtrip(0.12345);
00849   TestRoundtrip(0.123456);
00850   TestRoundtrip(0.1234567);
00851   TestRoundtrip(0.12345678);
00852   TestRoundtrip(0.123456789);
00853   TestRoundtrip(0.1234567891);
00854   TestRoundtrip(0.12345678912);
00855   TestRoundtrip(0.123456789123);
00856   TestRoundtrip(0.1234567891234);
00857   TestRoundtrip(0.12345678912345);
00858   TestRoundtrip(0.123456789123456);
00859   TestRoundtrip(0.1234567891234567);
00860   TestRoundtrip(0.12345678912345678);
00861 
00862   TestRoundtrip(0.1e50);
00863   TestRoundtrip(0.12e50);
00864   TestRoundtrip(0.123e50);
00865   TestRoundtrip(0.1234e50);
00866   TestRoundtrip(0.12345e50);
00867   TestRoundtrip(0.123456e50);
00868   TestRoundtrip(0.1234567e50);
00869   TestRoundtrip(0.12345678e50);
00870   TestRoundtrip(0.123456789e50);
00871   TestRoundtrip(0.1234567891e50);
00872   TestRoundtrip(0.12345678912e50);
00873   TestRoundtrip(0.123456789123e50);
00874   TestRoundtrip(0.1234567891234e50);
00875   TestRoundtrip(0.12345678912345e50);
00876   TestRoundtrip(0.123456789123456e50);
00877   TestRoundtrip(0.1234567891234567e50);
00878   TestRoundtrip(0.12345678912345678e50);
00879 
00880   TestRoundtrip(0.1e-50);
00881   TestRoundtrip(0.12e-50);
00882   TestRoundtrip(0.123e-50);
00883   TestRoundtrip(0.1234e-50);
00884   TestRoundtrip(0.12345e-50);
00885   TestRoundtrip(0.123456e-50);
00886   TestRoundtrip(0.1234567e-50);
00887   TestRoundtrip(0.12345678e-50);
00888   TestRoundtrip(0.123456789e-50);
00889   TestRoundtrip(0.1234567891e-50);
00890   TestRoundtrip(0.12345678912e-50);
00891   TestRoundtrip(0.123456789123e-50);
00892   TestRoundtrip(0.1234567891234e-50);
00893   TestRoundtrip(0.12345678912345e-50);
00894   TestRoundtrip(0.123456789123456e-50);
00895   TestRoundtrip(0.1234567891234567e-50);
00896   TestRoundtrip(0.12345678912345678e-50);
00897 }
00898 
00899 }  // namespace


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