Zhihao Yuan | d274896 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
Chandler Carruth | 57b08b0 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 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-exception |
Zhihao Yuan | d274896 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Marshall Clow | 769c245 | 2019-03-20 18:13:23 +0000 | [diff] [blame] | 9 | // UNSUPPORTED: c++98, c++03 |
Marshall Clow | 61914dc | 2019-03-20 19:43:22 +0000 | [diff] [blame] | 10 | // UNSUPPORTED: !libc++ && c++11 |
| 11 | // UNSUPPORTED: !libc++ && c++14 |
Volodymyr Sapsai | 0b8efae | 2018-08-10 17:03:47 +0000 | [diff] [blame] | 12 | |
Louis Dionne | a5e2b08 | 2018-12-14 20:22:44 +0000 | [diff] [blame] | 13 | // XFAIL: with_system_cxx_lib=macosx10.14 |
Volodymyr Sapsai | 0b8efae | 2018-08-10 17:03:47 +0000 | [diff] [blame] | 14 | // XFAIL: with_system_cxx_lib=macosx10.13 |
| 15 | // XFAIL: with_system_cxx_lib=macosx10.12 |
| 16 | // XFAIL: with_system_cxx_lib=macosx10.11 |
| 17 | // XFAIL: with_system_cxx_lib=macosx10.10 |
| 18 | // XFAIL: with_system_cxx_lib=macosx10.9 |
| 19 | // XFAIL: with_system_cxx_lib=macosx10.8 |
| 20 | // XFAIL: with_system_cxx_lib=macosx10.7 |
| 21 | |
Zhihao Yuan | d274896 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 22 | // <charconv> |
| 23 | |
| 24 | // from_chars_result from_chars(const char* first, const char* last, |
| 25 | // Integral& value, int base = 10) |
| 26 | |
| 27 | #include "charconv_test_helpers.h" |
| 28 | |
| 29 | template <typename T> |
| 30 | struct test_basics : roundtrip_test_base<T> |
| 31 | { |
| 32 | using roundtrip_test_base<T>::test; |
| 33 | |
| 34 | void operator()() |
| 35 | { |
| 36 | test(0); |
| 37 | test(42); |
| 38 | test(32768); |
| 39 | test(0, 10); |
| 40 | test(42, 10); |
| 41 | test(32768, 10); |
| 42 | test(0xf, 16); |
| 43 | test(0xdeadbeaf, 16); |
| 44 | test(0755, 8); |
| 45 | |
| 46 | for (int b = 2; b < 37; ++b) |
| 47 | { |
| 48 | using xl = std::numeric_limits<T>; |
| 49 | |
| 50 | test(1, b); |
Zhihao Yuan | 63ebd3b | 2018-08-01 05:21:26 +0000 | [diff] [blame] | 51 | test(-1, b); |
Zhihao Yuan | d274896 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 52 | test(xl::lowest(), b); |
| 53 | test((xl::max)(), b); |
| 54 | test((xl::max)() / 2, b); |
| 55 | } |
| 56 | |
| 57 | using std::from_chars; |
| 58 | std::from_chars_result r; |
| 59 | T x; |
| 60 | |
| 61 | { |
| 62 | char s[] = "001x"; |
| 63 | |
| 64 | // the expected form of the subject sequence is a sequence of |
| 65 | // letters and digits representing an integer with the radix |
| 66 | // specified by base (C11 7.22.1.4/3) |
| 67 | r = from_chars(s, s + sizeof(s), x); |
| 68 | assert(r.ec == std::errc{}); |
| 69 | assert(r.ptr == s + 3); |
| 70 | assert(x == 1); |
| 71 | } |
| 72 | |
| 73 | { |
| 74 | char s[] = "0X7BAtSGHDkEIXZg "; |
| 75 | |
| 76 | // The letters from a (or A) through z (or Z) are ascribed the |
| 77 | // values 10 through 35; (C11 7.22.1.4/3) |
| 78 | r = from_chars(s, s + sizeof(s), x, 36); |
| 79 | assert(r.ec == std::errc::result_out_of_range); |
| 80 | // The member ptr of the return value points to the first character |
| 81 | // not matching the pattern |
| 82 | assert(r.ptr == s + sizeof(s) - 2); |
| 83 | assert(x == 1); |
| 84 | |
| 85 | // no "0x" or "0X" prefix shall appear if the value of base is 16 |
| 86 | r = from_chars(s, s + sizeof(s), x, 16); |
| 87 | assert(r.ec == std::errc{}); |
| 88 | assert(r.ptr == s + 1); |
| 89 | assert(x == 0); |
| 90 | |
| 91 | // only letters and digits whose ascribed values are less than that |
| 92 | // of base are permitted. (C11 7.22.1.4/3) |
| 93 | r = from_chars(s + 2, s + sizeof(s), x, 12); |
| 94 | // If the parsed value is not in the range representable by the type |
| 95 | // of value, |
| 96 | if (!fits_in<T>(1150)) |
| 97 | { |
| 98 | // value is unmodified and |
| 99 | assert(x == 0); |
| 100 | // the member ec of the return value is equal to |
| 101 | // errc::result_out_of_range |
| 102 | assert(r.ec == std::errc::result_out_of_range); |
| 103 | } |
| 104 | else |
| 105 | { |
| 106 | // Otherwise, value is set to the parsed value, |
| 107 | assert(x == 1150); |
| 108 | // and the member ec is value-initialized. |
| 109 | assert(r.ec == std::errc{}); |
| 110 | } |
| 111 | assert(r.ptr == s + 5); |
| 112 | } |
| 113 | } |
| 114 | }; |
| 115 | |
| 116 | template <typename T> |
| 117 | struct test_signed : roundtrip_test_base<T> |
| 118 | { |
| 119 | using roundtrip_test_base<T>::test; |
| 120 | |
| 121 | void operator()() |
| 122 | { |
| 123 | test(-1); |
| 124 | test(-12); |
| 125 | test(-1, 10); |
| 126 | test(-12, 10); |
| 127 | test(-21734634, 10); |
| 128 | test(-2647, 2); |
| 129 | test(-0xcc1, 16); |
| 130 | |
| 131 | for (int b = 2; b < 37; ++b) |
| 132 | { |
| 133 | using xl = std::numeric_limits<T>; |
| 134 | |
| 135 | test(0, b); |
| 136 | test(xl::lowest(), b); |
| 137 | test((xl::max)(), b); |
| 138 | } |
| 139 | |
| 140 | using std::from_chars; |
| 141 | std::from_chars_result r; |
| 142 | T x; |
| 143 | |
| 144 | { |
| 145 | // If the pattern allows for an optional sign, |
| 146 | // but the string has no digit characters following the sign, |
| 147 | char s[] = "- 9+12"; |
| 148 | r = from_chars(s, s + sizeof(s), x); |
| 149 | // no characters match the pattern. |
| 150 | assert(r.ptr == s); |
| 151 | assert(r.ec == std::errc::invalid_argument); |
| 152 | } |
| 153 | |
| 154 | { |
| 155 | char s[] = "9+12"; |
| 156 | r = from_chars(s, s + sizeof(s), x); |
| 157 | assert(r.ec == std::errc{}); |
| 158 | // The member ptr of the return value points to the first character |
| 159 | // not matching the pattern, |
| 160 | assert(r.ptr == s + 1); |
| 161 | assert(x == 9); |
| 162 | } |
| 163 | |
| 164 | { |
| 165 | char s[] = "12"; |
| 166 | r = from_chars(s, s + 2, x); |
| 167 | assert(r.ec == std::errc{}); |
| 168 | // or has the value last if all characters match. |
| 169 | assert(r.ptr == s + 2); |
| 170 | assert(x == 12); |
| 171 | } |
| 172 | |
| 173 | { |
| 174 | // '-' is the only sign that may appear |
| 175 | char s[] = "+30"; |
| 176 | // If no characters match the pattern, |
| 177 | r = from_chars(s, s + sizeof(s), x); |
| 178 | // value is unmodified, |
| 179 | assert(x == 12); |
| 180 | // the member ptr of the return value is first and |
| 181 | assert(r.ptr == s); |
| 182 | // the member ec is equal to errc::invalid_argument. |
| 183 | assert(r.ec == std::errc::invalid_argument); |
| 184 | } |
| 185 | } |
| 186 | }; |
| 187 | |
JF Bastien | 2df59c5 | 2019-02-04 20:31:13 +0000 | [diff] [blame] | 188 | int main(int, char**) |
Zhihao Yuan | d274896 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 189 | { |
| 190 | run<test_basics>(integrals); |
| 191 | run<test_signed>(all_signed); |
JF Bastien | 2df59c5 | 2019-02-04 20:31:13 +0000 | [diff] [blame] | 192 | |
| 193 | return 0; |
Zhihao Yuan | d274896 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 194 | } |