brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.8 KiB · 7fe0613 Raw
119 lines · cpp
1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9// <string>10 11// int stoi(const string& str, size_t *idx = 0, int base = 10);12// int stoi(const wstring& str, size_t *idx = 0, int base = 10);13 14#include <string>15#include <cassert>16#include <stdexcept>17 18#include "test_macros.h"19 20int main(int, char**) {21  assert(std::stoi("0") == 0);22  assert(std::stoi("-0") == 0);23  assert(std::stoi("-10") == -10);24  assert(std::stoi(" 10") == 10);25  {26    std::size_t idx = 0;27    assert(std::stoi("10g", &idx, 16) == 16);28    assert(idx == 2);29  }30#ifndef TEST_HAS_NO_EXCEPTIONS31  if (std::numeric_limits<long>::max() > std::numeric_limits<int>::max()) {32    std::size_t idx = 0;33    try {34      (void)std::stoi("0x100000000", &idx, 16);35      assert(false);36    } catch (const std::out_of_range&) {37    }38  }39  {40    std::size_t idx = 0;41    try {42      (void)std::stoi("", &idx);43      assert(false);44    } catch (const std::invalid_argument&) {45      assert(idx == 0);46    }47  }48  {49    std::size_t idx = 0;50    try {51      (void)std::stoi("  - 8", &idx);52      assert(false);53    } catch (const std::invalid_argument&) {54      assert(idx == 0);55    }56  }57  {58    std::size_t idx = 0;59    try {60      (void)std::stoi("a1", &idx);61      assert(false);62    } catch (const std::invalid_argument&) {63      assert(idx == 0);64    }65  }66#endif // TEST_HAS_NO_EXCEPTIONS67 68#ifndef TEST_HAS_NO_WIDE_CHARACTERS69  assert(std::stoi(L"0") == 0);70  assert(std::stoi(L"-0") == 0);71  assert(std::stoi(L"-10") == -10);72  assert(std::stoi(L" 10") == 10);73  {74    std::size_t idx = 0;75    assert(std::stoi(L"10g", &idx, 16) == 16);76    assert(idx == 2);77  }78#  ifndef TEST_HAS_NO_EXCEPTIONS79  if (std::numeric_limits<long>::max() > std::numeric_limits<int>::max()) {80    std::size_t idx = 0;81    try {82      (void)std::stoi(L"0x100000000", &idx, 16);83      assert(false);84    } catch (const std::out_of_range&) {85    }86  }87  {88    std::size_t idx = 0;89    try {90      (void)std::stoi(L"", &idx);91      assert(false);92    } catch (const std::invalid_argument&) {93      assert(idx == 0);94    }95  }96  {97    std::size_t idx = 0;98    try {99      (void)std::stoi(L"  - 8", &idx);100      assert(false);101    } catch (const std::invalid_argument&) {102      assert(idx == 0);103    }104  }105  {106    std::size_t idx = 0;107    try {108      (void)std::stoi(L"a1", &idx);109      assert(false);110    } catch (const std::invalid_argument&) {111      assert(idx == 0);112    }113  }114#  endif // TEST_HAS_NO_EXCEPTIONS115#endif   // TEST_HAS_NO_WIDE_CHARACTERS116 117  return 0;118}119