153 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// long double stold(const string& str, size_t *idx = 0);12// long double stold(const wstring& str, size_t *idx = 0);13 14#include <cassert>15#include <cmath>16#include <stdexcept>17#include <string>18 19#include "test_macros.h"20 21int main(int, char**) {22 assert(std::stold("0") == 0);23 assert(std::stold("-0") == 0);24 assert(std::stold("-10") == -10);25 assert(std::stold(" 10") == 10);26 {27 std::size_t idx = 0;28 assert(std::stold("10g", &idx) == 10);29 assert(idx == 2);30 }31 {32 std::size_t idx = 0;33 assert(std::stold("1.e60", &idx) == 1.e60L);34 assert(idx == 5);35 }36 {37 std::size_t idx = 0;38 assert(std::stold("INF", &idx) == INFINITY);39 assert(idx == 3);40 }41 {42 std::size_t idx = 0;43 assert(std::isnan(std::stold("NAN", &idx)));44 assert(idx == 3);45 }46 47#ifndef TEST_HAS_NO_EXCEPTIONS48 {49 std::size_t idx = 0;50 try {51 (void)std::stold("", &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::stold(" - 8", &idx);61 assert(false);62 } catch (const std::invalid_argument&) {63 assert(idx == 0);64 }65 }66 {67 std::size_t idx = 0;68 try {69 (void)std::stold("a1", &idx);70 assert(false);71 } catch (const std::invalid_argument&) {72 assert(idx == 0);73 }74 }75 {76 std::size_t idx = 0;77 try {78 assert(std::stold("1.e6000", &idx) == INFINITY);79 assert(false);80 } catch (const std::out_of_range&) {81 assert(idx == 0);82 }83 }84#endif // TEST_HAS_NO_EXCEPTIONS85 86#ifndef TEST_HAS_NO_WIDE_CHARACTERS87 assert(std::stold(L"0") == 0);88 assert(std::stold(L"-0") == 0);89 assert(std::stold(L"-10.5") == -10.5);90 assert(std::stold(L" 10") == 10);91 {92 std::size_t idx = 0;93 assert(std::stold(L"10g", &idx) == 10);94 assert(idx == 2);95 }96 {97 std::size_t idx = 0;98 assert(std::stold(L"1.e60", &idx) == 1.e60L);99 assert(idx == 5);100 }101 {102 std::size_t idx = 0;103 assert(std::stold(L"INF", &idx) == INFINITY);104 assert(idx == 3);105 }106 {107 std::size_t idx = 0;108 assert(std::isnan(std::stold(L"NAN", &idx)));109 assert(idx == 3);110 }111# ifndef TEST_HAS_NO_EXCEPTIONS112 {113 std::size_t idx = 0;114 try {115 (void)std::stold(L"", &idx);116 assert(false);117 } catch (const std::invalid_argument&) {118 assert(idx == 0);119 }120 }121 {122 std::size_t idx = 0;123 try {124 (void)std::stold(L" - 8", &idx);125 assert(false);126 } catch (const std::invalid_argument&) {127 assert(idx == 0);128 }129 }130 {131 std::size_t idx = 0;132 try {133 (void)std::stold(L"a1", &idx);134 assert(false);135 } catch (const std::invalid_argument&) {136 assert(idx == 0);137 }138 }139 {140 std::size_t idx = 0;141 try {142 assert(std::stold(L"1.e6000", &idx) == INFINITY);143 assert(false);144 } catch (const std::out_of_range&) {145 assert(idx == 0);146 }147 }148# endif // TEST_HAS_NO_EXCEPTIONS149#endif // TEST_HAS_NO_WIDE_CHARACTERS150 151 return 0;152}153