brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.4 KiB · 4ddeb8f Raw
160 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// float stof(const string& str, size_t *idx = 0);12// float stof(const wstring& str, size_t *idx = 0);13 14#include <string>15#include <cmath>16#include <cassert>17#include <stdexcept>18 19#include "test_macros.h"20 21int main(int, char**) {22  assert(std::stof("0") == 0);23  assert(std::stof("-0") == 0);24  assert(std::stof("-10") == -10);25  assert(std::stof(" 10") == 10);26  {27    std::size_t idx = 0;28    assert(std::stof("10g", &idx) == 10);29    assert(idx == 2);30  }31  {32    std::size_t idx = 0;33    assert(std::stof("INF", &idx) == INFINITY);34    assert(idx == 3);35  }36  {37    std::size_t idx = 0;38    assert(std::isnan(std::stof("NAN", &idx)));39    assert(idx == 3);40  }41#ifndef TEST_HAS_NO_EXCEPTIONS42  {43    std::size_t idx = 0;44    try {45      assert(std::stof("1.e60", &idx) == INFINITY);46      assert(false);47    } catch (const std::out_of_range&) {48      assert(idx == 0);49    }50  }51  {52    std::size_t idx = 0;53    try {54      assert(std::stof("1.e360", &idx) == INFINITY);55      assert(false);56    } catch (const std::out_of_range&) {57      assert(idx == 0);58    }59  }60  {61    std::size_t idx = 0;62    try {63      (void)std::stof("", &idx);64      assert(false);65    } catch (const std::invalid_argument&) {66      assert(idx == 0);67    }68  }69  {70    std::size_t idx = 0;71    try {72      (void)std::stof("  - 8", &idx);73      assert(false);74    } catch (const std::invalid_argument&) {75      assert(idx == 0);76    }77  }78  {79    std::size_t idx = 0;80    try {81      (void)std::stof("a1", &idx);82      assert(false);83    } catch (const std::invalid_argument&) {84      assert(idx == 0);85    }86  }87#endif // TEST_HAS_NO_EXCEPTIONS88 89#ifndef TEST_HAS_NO_WIDE_CHARACTERS90  assert(std::stof(L"0") == 0);91  assert(std::stof(L"-0") == 0);92  assert(std::stof(L"-10.5") == -10.5);93  assert(std::stof(L" 10") == 10);94  {95    std::size_t idx = 0;96    assert(std::stof(L"10g", &idx) == 10);97    assert(idx == 2);98  }99  {100    std::size_t idx = 0;101    assert(std::stof(L"INF", &idx) == INFINITY);102    assert(idx == 3);103  }104  {105    std::size_t idx = 0;106    assert(std::isnan(std::stof(L"NAN", &idx)));107    assert(idx == 3);108  }109#  ifndef TEST_HAS_NO_EXCEPTIONS110  {111    std::size_t idx = 0;112    try {113      assert(std::stof(L"1.e60", &idx) == INFINITY);114      assert(false);115    } catch (const std::out_of_range&) {116      assert(idx == 0);117    }118  }119  {120    std::size_t idx = 0;121    try {122      assert(std::stof(L"1.e360", &idx) == INFINITY);123      assert(false);124    } catch (const std::out_of_range&) {125      assert(idx == 0);126    }127  }128  {129    std::size_t idx = 0;130    try {131      (void)std::stof(L"", &idx);132      assert(false);133    } catch (const std::invalid_argument&) {134      assert(idx == 0);135    }136  }137  {138    std::size_t idx = 0;139    try {140      (void)std::stof(L"  - 8", &idx);141      assert(false);142    } catch (const std::invalid_argument&) {143      assert(idx == 0);144    }145  }146  {147    std::size_t idx = 0;148    try {149      (void)std::stof(L"a1", &idx);150      assert(false);151    } catch (const std::invalid_argument&) {152      assert(idx == 0);153    }154  }155#  endif // TEST_HAS_NO_EXCEPTIONS156#endif   // TEST_HAS_NO_WIDE_CHARACTERS157 158  return 0;159}160