Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4 | // |
Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | // <locale> |
| 11 | |
| 12 | // class num_get<charT, InputIterator> |
| 13 | |
| 14 | // iter_type get(iter_type in, iter_type end, ios_base&, |
| 15 | // ios_base::iostate& err, float& v) const; |
| 16 | |
| 17 | #include <locale> |
| 18 | #include <ios> |
| 19 | #include <cassert> |
| 20 | #include <streambuf> |
| 21 | #include <cmath> |
| 22 | #include "iterators.h" |
Howard Hinnant | 0a11111 | 2011-05-13 21:52:40 +0000 | [diff] [blame^] | 23 | #include "../../../../../hexfloat.h" |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 24 | |
| 25 | typedef std::num_get<char, input_iterator<const char*> > F; |
| 26 | |
| 27 | class my_facet |
| 28 | : public F |
| 29 | { |
| 30 | public: |
| 31 | explicit my_facet(std::size_t refs = 0) |
| 32 | : F(refs) {} |
| 33 | }; |
| 34 | |
| 35 | int main() |
| 36 | { |
| 37 | const my_facet f(1); |
| 38 | std::ios ios(0); |
| 39 | float v = -1; |
| 40 | { |
| 41 | const char str[] = "123"; |
| 42 | assert((ios.flags() & ios.basefield) == ios.dec); |
| 43 | assert(ios.getloc().name() == "C"); |
| 44 | std::ios_base::iostate err = ios.goodbit; |
Howard Hinnant | 22a74dc | 2010-08-22 00:39:25 +0000 | [diff] [blame] | 45 | input_iterator<const char*> iter = |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 46 | f.get(input_iterator<const char*>(str), |
| 47 | input_iterator<const char*>(str+sizeof(str)), |
| 48 | ios, err, v); |
| 49 | assert(iter.base() == str+sizeof(str)-1); |
| 50 | assert(err == ios.goodbit); |
| 51 | assert(v == 123); |
| 52 | } |
| 53 | { |
| 54 | const char str[] = "-123"; |
| 55 | std::ios_base::iostate err = ios.goodbit; |
Howard Hinnant | 22a74dc | 2010-08-22 00:39:25 +0000 | [diff] [blame] | 56 | input_iterator<const char*> iter = |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 57 | f.get(input_iterator<const char*>(str), |
| 58 | input_iterator<const char*>(str+sizeof(str)), |
| 59 | ios, err, v); |
| 60 | assert(iter.base() == str+sizeof(str)-1); |
| 61 | assert(err == ios.goodbit); |
| 62 | assert(v == -123); |
| 63 | } |
| 64 | { |
| 65 | const char str[] = "123.5"; |
| 66 | std::ios_base::iostate err = ios.goodbit; |
Howard Hinnant | 22a74dc | 2010-08-22 00:39:25 +0000 | [diff] [blame] | 67 | input_iterator<const char*> iter = |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 68 | f.get(input_iterator<const char*>(str), |
| 69 | input_iterator<const char*>(str+sizeof(str)), |
| 70 | ios, err, v); |
| 71 | assert(iter.base() == str+sizeof(str)-1); |
| 72 | assert(err == ios.goodbit); |
| 73 | assert(v == 123.5); |
| 74 | } |
| 75 | { |
| 76 | const char str[] = "125e-1"; |
| 77 | hex(ios); |
| 78 | std::ios_base::iostate err = ios.goodbit; |
Howard Hinnant | 22a74dc | 2010-08-22 00:39:25 +0000 | [diff] [blame] | 79 | input_iterator<const char*> iter = |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 80 | f.get(input_iterator<const char*>(str), |
| 81 | input_iterator<const char*>(str+sizeof(str)), |
| 82 | ios, err, v); |
| 83 | assert(iter.base() == str+sizeof(str)-1); |
| 84 | assert(err == ios.goodbit); |
| 85 | assert(v == 125e-1); |
| 86 | } |
| 87 | { |
| 88 | const char str[] = "0x125p-1"; |
| 89 | hex(ios); |
| 90 | std::ios_base::iostate err = ios.goodbit; |
Howard Hinnant | 22a74dc | 2010-08-22 00:39:25 +0000 | [diff] [blame] | 91 | input_iterator<const char*> iter = |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 92 | f.get(input_iterator<const char*>(str), |
| 93 | input_iterator<const char*>(str+sizeof(str)), |
| 94 | ios, err, v); |
| 95 | assert(iter.base() == str+sizeof(str)-1); |
| 96 | assert(err == ios.goodbit); |
Howard Hinnant | 0a11111 | 2011-05-13 21:52:40 +0000 | [diff] [blame^] | 97 | assert(v == hexfloat<float>(0x125, 0, -1)); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 98 | } |
| 99 | { |
| 100 | const char str[] = "inf"; |
| 101 | hex(ios); |
| 102 | std::ios_base::iostate err = ios.goodbit; |
Howard Hinnant | 22a74dc | 2010-08-22 00:39:25 +0000 | [diff] [blame] | 103 | input_iterator<const char*> iter = |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 104 | f.get(input_iterator<const char*>(str), |
| 105 | input_iterator<const char*>(str+sizeof(str)), |
| 106 | ios, err, v); |
| 107 | assert(iter.base() == str+sizeof(str)-1); |
| 108 | assert(err == ios.goodbit); |
| 109 | assert(v == INFINITY); |
| 110 | } |
| 111 | { |
| 112 | const char str[] = "INF"; |
| 113 | hex(ios); |
| 114 | std::ios_base::iostate err = ios.goodbit; |
Howard Hinnant | 22a74dc | 2010-08-22 00:39:25 +0000 | [diff] [blame] | 115 | input_iterator<const char*> iter = |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 116 | f.get(input_iterator<const char*>(str), |
| 117 | input_iterator<const char*>(str+sizeof(str)), |
| 118 | ios, err, v); |
| 119 | assert(iter.base() == str+sizeof(str)-1); |
| 120 | assert(err == ios.goodbit); |
| 121 | assert(v == INFINITY); |
| 122 | } |
| 123 | { |
| 124 | const char str[] = "-inf"; |
| 125 | hex(ios); |
| 126 | std::ios_base::iostate err = ios.goodbit; |
Howard Hinnant | 22a74dc | 2010-08-22 00:39:25 +0000 | [diff] [blame] | 127 | input_iterator<const char*> iter = |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 128 | f.get(input_iterator<const char*>(str), |
| 129 | input_iterator<const char*>(str+sizeof(str)), |
| 130 | ios, err, v); |
| 131 | assert(iter.base() == str+sizeof(str)-1); |
| 132 | assert(err == ios.goodbit); |
| 133 | assert(v == -INFINITY); |
| 134 | } |
| 135 | { |
| 136 | const char str[] = "-INF"; |
| 137 | hex(ios); |
| 138 | std::ios_base::iostate err = ios.goodbit; |
Howard Hinnant | 22a74dc | 2010-08-22 00:39:25 +0000 | [diff] [blame] | 139 | input_iterator<const char*> iter = |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 140 | f.get(input_iterator<const char*>(str), |
| 141 | input_iterator<const char*>(str+sizeof(str)), |
| 142 | ios, err, v); |
| 143 | assert(iter.base() == str+sizeof(str)-1); |
| 144 | assert(err == ios.goodbit); |
| 145 | assert(v == -INFINITY); |
| 146 | } |
| 147 | { |
| 148 | const char str[] = "nan"; |
| 149 | hex(ios); |
| 150 | std::ios_base::iostate err = ios.goodbit; |
Howard Hinnant | 22a74dc | 2010-08-22 00:39:25 +0000 | [diff] [blame] | 151 | input_iterator<const char*> iter = |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 152 | f.get(input_iterator<const char*>(str), |
| 153 | input_iterator<const char*>(str+sizeof(str)), |
| 154 | ios, err, v); |
| 155 | assert(iter.base() == str+sizeof(str)-1); |
| 156 | assert(err == ios.goodbit); |
| 157 | assert(std::isnan(v)); |
| 158 | } |
| 159 | { |
| 160 | const char str[] = "NAN"; |
| 161 | hex(ios); |
| 162 | std::ios_base::iostate err = ios.goodbit; |
Howard Hinnant | 22a74dc | 2010-08-22 00:39:25 +0000 | [diff] [blame] | 163 | input_iterator<const char*> iter = |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 164 | f.get(input_iterator<const char*>(str), |
| 165 | input_iterator<const char*>(str+sizeof(str)), |
| 166 | ios, err, v); |
| 167 | assert(iter.base() == str+sizeof(str)-1); |
| 168 | assert(err == ios.goodbit); |
| 169 | assert(std::isnan(v)); |
| 170 | } |
| 171 | } |