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