parse_numbers.cpp raw

   1  // Copyright (c) 2009-present The Bitcoin Core developers
   2  // Distributed under the MIT software license, see the accompanying
   3  // file COPYING or http://www.opensource.org/licenses/mit-license.php.
   4  
   5  #include <test/fuzz/fuzz.h>
   6  #include <util/moneystr.h>
   7  #include <util/strencodings.h>
   8  #include <util/string.h>
   9  
  10  #include <cassert>
  11  #include <cstdint>
  12  #include <optional>
  13  #include <string>
  14  
  15  FUZZ_TARGET(parse_numbers)
  16  {
  17      const std::string random_string(buffer.begin(), buffer.end());
  18      {
  19          const auto i8{ToIntegral<int8_t>(random_string)};
  20          const auto u8{ToIntegral<uint8_t>(random_string)};
  21          const auto i16{ToIntegral<int16_t>(random_string)};
  22          const auto u16{ToIntegral<uint16_t>(random_string)};
  23          const auto i32{ToIntegral<int32_t>(random_string)};
  24          const auto u32{ToIntegral<uint32_t>(random_string)};
  25          const auto i64{ToIntegral<int64_t>(random_string)};
  26          const auto u64{ToIntegral<uint64_t>(random_string)};
  27          // Dont check any values, just that each success result must fit into
  28          // the one with the largest bit-width.
  29          if (i8) {
  30              assert(i8 == i64);
  31          }
  32          if (u8) {
  33              assert(u8 == u64);
  34          }
  35          if (i16) {
  36              assert(i16 == i64);
  37          }
  38          if (u16) {
  39              assert(u16 == u64);
  40          }
  41          if (i32) {
  42              assert(i32 == i64);
  43          }
  44          if (u32) {
  45              assert(u32 == u64);
  46          }
  47          constexpr auto digits{"0123456789"};
  48          if (i64) {
  49              assert(util::RemovePrefixView(random_string, "-").find_first_not_of(digits) == std::string::npos);
  50          }
  51          if (u64) {
  52              assert(random_string.find_first_not_of(digits) == std::string::npos);
  53          }
  54      }
  55  
  56      (void)ParseMoney(random_string);
  57  
  58      (void)LocaleIndependentAtoi<int>(random_string);
  59  
  60      int64_t i64;
  61      (void)LocaleIndependentAtoi<int64_t>(random_string);
  62      (void)ParseFixedPoint(random_string, 3, &i64);
  63  }
  64