pow10_helper_test.cc
Go to the documentation of this file.
00001 // Copyright 2018 The Abseil Authors.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 
00015 #include "absl/strings/internal/pow10_helper.h"
00016 
00017 #include <cmath>
00018 
00019 #include "gtest/gtest.h"
00020 #include "absl/strings/str_format.h"
00021 
00022 namespace absl {
00023 namespace strings_internal {
00024 
00025 namespace {
00026 
00027 struct TestCase {
00028   int power;           // Testing Pow10(power)
00029   uint64_t significand;  // Raw bits of the expected value
00030   int radix;           // significand is adjusted by 2^radix
00031 };
00032 
00033 TEST(Pow10HelperTest, Works) {
00034   // The logic in pow10_helper.cc is so simple that theoretically we don't even
00035   // need a test. However, we're paranoid and believe that there may be
00036   // compilers that don't round floating-point literals correctly, even though
00037   // it is specified by the standard. We check various edge cases, just to be
00038   // sure.
00039   constexpr TestCase kTestCases[] = {
00040       // Subnormals
00041       {-323, 0x2, -1074},
00042       {-322, 0x14, -1074},
00043       {-321, 0xca, -1074},
00044       {-320, 0x7e8, -1074},
00045       {-319, 0x4f10, -1074},
00046       {-318, 0x316a2, -1074},
00047       {-317, 0x1ee257, -1074},
00048       {-316, 0x134d761, -1074},
00049       {-315, 0xc1069cd, -1074},
00050       {-314, 0x78a42205, -1074},
00051       {-313, 0x4b6695433, -1074},
00052       {-312, 0x2f201d49fb, -1074},
00053       {-311, 0x1d74124e3d1, -1074},
00054       {-310, 0x12688b70e62b, -1074},
00055       {-309, 0xb8157268fdaf, -1074},
00056       {-308, 0x730d67819e8d2, -1074},
00057       // Values that are very close to rounding the other way.
00058       // Comment shows difference of significand from the true value.
00059       {-307, 0x11fa182c40c60d, -1072},  // -.4588
00060       {-290, 0x18f2b061aea072, -1016},  //  .4854
00061       {-276, 0x11BA03F5B21000, -969},   //  .4709
00062       {-259, 0x1899C2F6732210, -913},   //  .4830
00063       {-252, 0x1D53844EE47DD1, -890},   // -.4743
00064       {-227, 0x1E5297287C2F45, -807},   // -.4708
00065       {-198, 0x1322E220A5B17E, -710},   // -.4714
00066       {-195, 0x12B010D3E1CF56, -700},   //  .4928
00067       {-192, 0x123FF06EEA847A, -690},   //  .4968
00068       {-163, 0x1708D0F84D3DE7, -594},   // -.4977
00069       {-145, 0x13FAAC3E3FA1F3, -534},   // -.4785
00070       {-111, 0x133D4032C2C7F5, -421},   //  .4774
00071       {-106, 0x1D5B561574765B, -405},   // -.4869
00072       {-104, 0x16EF5B40C2FC77, -398},   // -.4741
00073       {-88, 0x197683DF2F268D, -345},    // -.4738
00074       {-86, 0x13E497065CD61F, -338},    //  .4736
00075       {-76, 0x17288E1271F513, -305},    // -.4761
00076       {-63, 0x1A53FC9631D10D, -262},    //  .4929
00077       {-30, 0x14484BFEEBC2A0, -152},    //  .4758
00078       {-21, 0x12E3B40A0E9B4F, -122},    // -.4916
00079       {-5, 0x14F8B588E368F1, -69},      //  .4829
00080       {23, 0x152D02C7E14AF6, 24},       // -.5000 (exactly, round-to-even)
00081       {29, 0x1431E0FAE6D721, 44},       // -.4870
00082       {34, 0x1ED09BEAD87C03, 60},       // -.4721
00083       {70, 0x172EBAD6DDC73D, 180},      //  .4733
00084       {105, 0x1BE7ABD3781ECA, 296},     // -.4850
00085       {126, 0x17A2ECC414A03F, 366},     // -.4999
00086       {130, 0x1CDA62055B2D9E, 379},     //  .4855
00087       {165, 0x115D847AD00087, 496},     // -.4913
00088       {172, 0x14B378469B6732, 519},     //  .4818
00089       {187, 0x1262DFEEBBB0F9, 569},     // -.4805
00090       {210, 0x18557F31326BBB, 645},     // -.4992
00091       {212, 0x1302CB5E6F642A, 652},     // -.4838
00092       {215, 0x1290BA9A38C7D1, 662},     // -.4881
00093       {236, 0x1F736F9B3494E9, 731},     //  .4707
00094       {244, 0x176EC98994F489, 758},     //  .4924
00095       {250, 0x1658E3AB795204, 778},     // -.4963
00096       {252, 0x117571DDF6C814, 785},     //  .4873
00097       {254, 0x1B4781EAD1989E, 791},     // -.4887
00098       {260, 0x1A03FDE214CAF1, 811},     //  .4784
00099       {284, 0x1585041B2C477F, 891},     //  .4798
00100       {304, 0x1D2A1BE4048F90, 957},     // -.4987
00101       // Out-of-range values
00102       {-324, 0x0, 0},
00103       {-325, 0x0, 0},
00104       {-326, 0x0, 0},
00105       {309, 1, 2000},
00106       {310, 1, 2000},
00107       {311, 1, 2000},
00108   };
00109   for (const TestCase& test_case : kTestCases) {
00110     EXPECT_EQ(Pow10(test_case.power),
00111               std::ldexp(test_case.significand, test_case.radix))
00112         << absl::StrFormat("Failure for Pow10(%d): %a vs %a", test_case.power,
00113                            Pow10(test_case.power),
00114                            std::ldexp(test_case.significand, test_case.radix));
00115   }
00116 }
00117 
00118 }  // namespace
00119 }  // namespace strings_internal
00120 }  // namespace absl


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