159 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// double stod(const string& str, size_t *idx = 0);12// double stod(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 // char23 {24 assert(std::stod("0") == 0);25 assert(std::stod("-0") == 0);26 assert(std::stod("-10") == -10);27 assert(std::stod(" 10") == 10);28 {29 std::size_t idx = 0;30 assert(std::stod("10g", &idx) == 10);31 assert(idx == 2);32 }33 {34 std::size_t idx = 0;35 assert(std::stod("1.e60", &idx) == 1.e60);36 assert(idx == 5);37 }38 {39 std::size_t idx = 0;40 assert(std::stod("INF", &idx) == INFINITY);41 assert(idx == 3);42 }43 {44 std::size_t idx = 0;45 assert(std::isnan(std::stod("NAN", &idx)));46 assert(idx == 3);47 }48 49#ifndef TEST_HAS_NO_EXCEPTIONS50 {51 std::size_t idx = 0;52 try {53 assert(std::stod("1.e360", &idx) == INFINITY);54 assert(false);55 } catch (const std::out_of_range&) {56 assert(idx == 0);57 }58 }59 {60 std::size_t idx = 0;61 try {62 (void)std::stod("", &idx);63 assert(false);64 } catch (const std::invalid_argument&) {65 assert(idx == 0);66 }67 }68 {69 std::size_t idx = 0;70 try {71 (void)std::stod(" - 8", &idx);72 assert(false);73 } catch (const std::invalid_argument&) {74 assert(idx == 0);75 }76 }77 {78 std::size_t idx = 0;79 try {80 (void)std::stod("a1", &idx);81 assert(false);82 } catch (const std::invalid_argument&) {83 assert(idx == 0);84 }85 }86#endif // TEST_HAS_NO_EXCEPTIONS87 }88 89 // wchar_t90#ifndef TEST_HAS_NO_WIDE_CHARACTERS91 {92 assert(std::stod(L"0") == 0);93 assert(std::stod(L"-0") == 0);94 assert(std::stod(L"-10.5") == -10.5);95 assert(std::stod(L" 10") == 10);96 {97 std::size_t idx = 0;98 assert(std::stod(L"10g", &idx) == 10);99 assert(idx == 2);100 }101 {102 std::size_t idx = 0;103 assert(std::stod(L"1.e60", &idx) == 1.e60);104 assert(idx == 5);105 }106 {107 std::size_t idx = 0;108 assert(std::stod(L"INF", &idx) == INFINITY);109 assert(idx == 3);110 }111 {112 std::size_t idx = 0;113 assert(std::isnan(std::stod(L"NAN", &idx)));114 assert(idx == 3);115 }116# ifndef TEST_HAS_NO_EXCEPTIONS117 {118 std::size_t idx = 0;119 try {120 assert(std::stod(L"1.e360", &idx) == INFINITY);121 assert(false);122 } catch (const std::out_of_range&) {123 assert(idx == 0);124 }125 }126 {127 std::size_t idx = 0;128 try {129 (void)std::stod(L"", &idx);130 assert(false);131 } catch (const std::invalid_argument&) {132 assert(idx == 0);133 }134 }135 {136 std::size_t idx = 0;137 try {138 (void)std::stod(L" - 8", &idx);139 assert(false);140 } catch (const std::invalid_argument&) {141 assert(idx == 0);142 }143 }144 {145 std::size_t idx = 0;146 try {147 (void)std::stod(L"a1", &idx);148 assert(false);149 } catch (const std::invalid_argument&) {150 assert(idx == 0);151 }152 }153# endif // TEST_HAS_NO_EXCEPTIONS154 }155#endif // TEST_HAS_NO_WIDE_CHARACTERS156 157 return 0;158}159